OSDN Git Service

pass model import test
authoryasushiito <yas@pen-chan.jp>
Tue, 26 Jun 2012 07:33:36 +0000 (16:33 +0900)
committeryasushiito <yas@pen-chan.jp>
Tue, 26 Jun 2012 07:33:36 +0000 (16:33 +0900)
15 files changed:
.gitignore
app/models/license_group.rb
app/models/panel_picture.rb
spec/json/invalid_license_groups.json [new file with mode: 0644]
spec/json/license_groups.json [new file with mode: 0644]
spec/models/license_group_spec.rb
spec/models/panel_picture_spec.rb
spec/requests/comics_spec.rb [deleted file]
spec/routing/comics_routing_spec.rb [deleted file]
spec/views/comics/edit.html.erb_spec.rb [deleted file]
spec/views/comics/index.html.erb_spec.rb [deleted file]
spec/views/comics/new.html.erb_spec.rb [deleted file]
spec/views/comics/show.html.erb_spec.rb [deleted file]
vendor/plugins/pettan_importer/lib/pettan_importer.rb [new file with mode: 0644]
vendor/plugins/pettan_importer/test/import_spec.rb [new file with mode: 0644]

index 84d418e..1cf8695 100644 (file)
@@ -1,5 +1,6 @@
 .bundle
 db/*.sqlite3
+db/schema.rb
 log/*.log
 tmp/
 .sass-cache/
index a1ab14c..34fbf32 100644 (file)
@@ -3,4 +3,15 @@ class LicenseGroup < ActiveRecord::Base
   validates :classname, :presence => true, :length => {:maximum => 50}
   validates :caption, :presence => true, :length => {:maximum => 30}
   validates :url, :presence => true, :length => {:maximum => 200}, :url => true
+  
+  def self.store name, attr
+    r = LicenseGroup.modify_object name, attr
+    r.save
+    r
+  end
+  
+  def self.import filename
+    LicenseGroup.import_file(filename) {|name, attr| LicenseGroup.store(name, attr)}
+  end
+  
 end
index f719af1..b93c667 100644 (file)
@@ -2,8 +2,15 @@ class PanelPicture < ActiveRecord::Base
   belongs_to :panel
   belongs_to :resource_picture
   
+  validates :panel_id, :numericality => {:allow_blank => true}
+  validates :resource_picture_id, :presence => true, :numericality => true, :existence => true
+  validates :link, :length => {:maximum => 200}
+  validates :x, :presence => true, :numericality => true
+  validates :y, :presence => true, :numericality => true
   validates :width, :presence => true, :numericality => true, :not_zero => true
   validates :height, :presence => true, :numericality => true, :not_zero => true
+  validates :z, :presence => true, :numericality => {:greater_than => 0}
+  validates :t, :presence => true, :numericality => {:greater_than_or_equal_to => 0}
   
   def flip
     res = (self.height < 0 ? '' : 'v') + (self.width == 0 ? '' : 'h')
diff --git a/spec/json/invalid_license_groups.json b/spec/json/invalid_license_groups.json
new file mode 100644 (file)
index 0000000..afed992
--- /dev/null
@@ -0,0 +1,15 @@
+{\r
+  "PublicDomain": {\r
+    "classname": "PublicDomain", \r
+    "caption": "Public Domain", \r
+    "url": "http://test.com/"\r
+  },\r
+  "UnknownUrl": {\r
+    "classname": "Unknown", \r
+    "caption": "Unknown owner"\r
+  },\r
+  "UnknownClassname": {\r
+    "caption": "Unknown owner", \r
+    "url": "http://test.uk/"\r
+  }\r
+}\r
diff --git a/spec/json/license_groups.json b/spec/json/license_groups.json
new file mode 100644 (file)
index 0000000..afb7d37
--- /dev/null
@@ -0,0 +1,12 @@
+{\r
+  "PublicDomain": {\r
+    "classname": "PublicDomain", \r
+    "caption": "Public Domain", \r
+    "url": "http://test.com/"\r
+  },\r
+  "Unknown": {\r
+    "classname": "Unknown", \r
+    "caption": "Unknown owner", \r
+    "url": "http://test.uk/"\r
+  }\r
+}\r
index fbf9f0f..96431fa 100644 (file)
@@ -8,6 +8,8 @@ describe LicenseGroup do
     @f = Rails.root + 'spec/json/license_group.json'
     @t = File.open(@f, 'r').read
     @j = JSON.parse @t
+    @fs = Rails.root + 'spec/json/license_groups.json'
+    @fes = Rails.root + 'spec/json/invalid_license_groups.json'
   end
   describe '検証に於いて' do
     before do
@@ -96,95 +98,122 @@ describe LicenseGroup do
     end
   end
   
+  describe '更新に於いて' do
+    before do
+      @n = @j.keys.first
+      @a = @j.values.first
+    end
+    context 'つつがなく終わるとき' do
+      it 'データ更新準備を依頼する' do
+        LicenseGroup.stub(:modify_object).with(any_args).and_return(LicenseGroup.new)
+        LicenseGroup.should_receive(:modify_object).with(any_args).exactly(1)
+        LicenseGroup.any_instance.stub(:save).with(any_args).and_return(true)
+        r = LicenseGroup.store(@n, @a)
+      end
+      it '保存を依頼する' do
+        LicenseGroup.stub(:modify_object).with(any_args).and_return(LicenseGroup.new)
+        LicenseGroup.any_instance.stub(:save).with(any_args).and_return(true)
+        LicenseGroup.any_instance.should_receive(:save).with(any_args).exactly(1)
+        r = LicenseGroup.store(@n, @a)
+      end
+      it 'オブジェクトを返す' do
+        r = LicenseGroup.store(@n, @a)
+        r.is_a?(LicenseGroup).should be_true
+        r.name.should eq @n
+        r.url.should eq @a["url"]
+      end
+    end
+  end
+  
   describe 'インポートに於いて' do
     before do
     end
     context 'つつがなく終わるとき' do
-      it 'ã\83\86ã\82­ã\82¹ã\83\88インポートを依頼する' do
-        CommonLicense.should_receive(:parse).with(any_args).exactly(1)
-        CommonLicense.stub(:parse).with(any_args).and_return(@j)
-        CommonLicense.import(@t)
+      it 'ã\83\95ã\82¡ã\82¤ã\83«インポートを依頼する' do
+        LicenseGroup.should_receive(:import_file).with(any_args).exactly(1)
+        LicenseGroup.stub(:import_file).with(any_args).and_return([])
+        LicenseGroup.import(@f)
       end
       it 'ライセンスグループ更新を一回依頼する' do
-        CommonLicense.stub(:store).with(any_args).and_return(CommonLicense.new)
-        CommonLicense.any_instance.stub(:valid?).with(any_args).and_return(true)
-        CommonLicense.should_receive(:store).with(any_args).exactly(1)
-        CommonLicense.import(@t)
+        LicenseGroup.stub(:store).with(any_args).and_return(LicenseGroup.new)
+        LicenseGroup.should_receive(:store).with(any_args).exactly(1)
+        LicenseGroup.import(@f)
       end
       it 'ライセンスグループが追加される' do
         lambda {
-          CommonLicense.import(@t)
-        }.should change CommonLicense, :count
+          LicenseGroup.import(@f)
+        }.should change LicenseGroup, :count
       end
       it '[]を返す' do
-        CommonLicense.import(@t).should eq []
+        r = LicenseGroup.import(@f)
+        r.should eq []
       end
     end
     context '複数データがつつがなく終わるとき' do
       it 'ライセンスグループ更新を二回依頼する' do
-        CommonLicense.stub(:store).with(any_args).and_return(CommonLicense.new)
-        CommonLicense.any_instance.stub(:valid?).with(any_args).and_return(true)
-        CommonLicense.should_receive(:store).with(any_args).exactly(2)
-        CommonLicense.import(@ts)
+        LicenseGroup.stub(:store).with(any_args).and_return(LicenseGroup.new)
+        LicenseGroup.should_receive(:store).with(any_args).exactly(2)
+        LicenseGroup.import(@fs)
       end
       it 'ライセンスグループが二個追加される' do
         lambda {
-          CommonLicense.import(@ts)
-        }.should change(CommonLicense, :count).by 2
+          r = LicenseGroup.import(@fs)
+        }.should change(LicenseGroup, :count).by 2
       end
       it '[]を返す' do
-        CommonLicense.import(@ts).should eq []
+        r = LicenseGroup.import(@fs)
+        r.should eq []
       end
     end
-    context 'ã\82³ã\83¢ã\83³ã\83©ã\82¤ã\82»ã\83³ã\82¹作成に失敗したとき' do
+    context 'ã\83©ã\82¤ã\82»ã\83³ã\82¹ã\82°ã\83«ã\83¼ã\83\97作成に失敗したとき' do
       before do
-        CommonLicense.any_instance.stub(:save).with(any_args).and_return(false)
-        CommonLicense.any_instance.stub(:valid?).with(any_args).and_return(false)
+        LicenseGroup.any_instance.stub(:save).with(any_args).and_return(false)
+        LicenseGroup.any_instance.stub(:valid?).with(any_args).and_return(false)
       end
-      it 'ã\82³ã\83¢ã\83³ã\83©ã\82¤ã\82»ã\83³ã\82¹の数に変化がない' do
+      it 'ã\83©ã\82¤ã\82»ã\83³ã\82¹ã\82°ã\83«ã\83¼ã\83\97の数に変化がない' do
         lambda {
-          CommonLicense.import(@t)
-        }.should_not change CommonLicense, :count
+          LicenseGroup.import(@f)
+        }.should_not change LicenseGroup, :count
       end
       it '配列を返す' do
-        r = CommonLicense.import(@t)
+        r = LicenseGroup.import(@f)
         r.is_a?(Array).should be_true
       end
       it '配列の中身は一件' do
-        r = CommonLicense.import(@t)
+        r = LicenseGroup.import(@f)
         r.should have(1).items
       end
-      it 'ã\82³ã\83¢ã\83³ã\83©ã\82¤ã\82»ã\83³ã\82¹オブジェクトが入っている' do
-        r = CommonLicense.import(@t)
-        r.first.is_a?(CommonLicense).should be_true
+      it 'ã\83©ã\82¤ã\82»ã\83³ã\82¹ã\82°ã\83«ã\83¼ã\83\97オブジェクトが入っている' do
+        r = LicenseGroup.import(@f)
+        r.first.is_a?(LicenseGroup).should be_true
       end
     end
-    context 'è¤\87æ\95°ã\81®ã\82³ã\83¢ã\83³ã\83©ã\82¤ã\82»ã\83³ã\82¹作成に失敗したとき' do
+    context 'è¤\87æ\95°ã\81®ã\83©ã\82¤ã\82»ã\83³ã\82¹ã\82°ã\83«ã\83¼ã\83\97作成に失敗したとき' do
       #三件中、二件の失敗、一件を成功させ、成功データは戻り値に含まないことを確認する
-      it 'ã\82³ã\83¢ã\83³ã\83©ã\82¤ã\82»ã\83³ã\82¹の数に変化がない' do
+      it 'ã\83©ã\82¤ã\82»ã\83³ã\82¹ã\82°ã\83«ã\83¼ã\83\97の数に変化がない' do
         lambda {
-          CommonLicense.import(@tes)
-        }.should_not change CommonLicense, :count
+          LicenseGroup.import(@fes)
+        }.should_not change LicenseGroup, :count
       end
       it '途中で保存に失敗しても全件更新依頼する' do
-        CommonLicense.stub(:store).with(any_args).and_return(CommonLicense.new)
-        CommonLicense.should_receive(:store).with(any_args).exactly(3)
-        CommonLicense.import(@tes)
+        LicenseGroup.stub(:store).with(any_args).and_return(LicenseGroup.new)
+        LicenseGroup.should_receive(:store).with(any_args).exactly(3)
+        LicenseGroup.import(@fes)
       end
       it '配列を返す' do
-        r = CommonLicense.import(@tes)
+        r = LicenseGroup.import(@fes)
         r.is_a?(Array).should be_true
       end
       it '配列の中身は2件' do
-        r = CommonLicense.import(@tes)
+        r = LicenseGroup.import(@fes)
         r.should have(2).items
       end
-      it 'é\85\8då\88\97ã\81®ä¸­èº«ã\81¯å¤±æ\95\97ã\81\97ã\81\9fã\82³ã\83¢ã\83³ã\83©ã\82¤ã\82»ã\83³ã\82¹オブジェクトが入っている' do
-        r = CommonLicense.import(@tes)
-        r[0].is_a?(CommonLicense).should be_true
-        r[0]["name"].should eq 'fail1'
-        r[1].is_a?(CommonLicense).should be_true
-        r[1]["name"].should eq 'fail2'
+      it 'é\85\8då\88\97ã\81®ä¸­èº«ã\81¯å¤±æ\95\97ã\81\97ã\81\9fã\83©ã\82¤ã\82»ã\83³ã\82¹ã\82°ã\83«ã\83¼ã\83\97オブジェクトが入っている' do
+        r = LicenseGroup.import(@fes)
+        r[0].is_a?(LicenseGroup).should be_true
+        r[0]["name"].should eq 'UnknownUrl'
+        r[1].is_a?(LicenseGroup).should be_true
+        r[1]["name"].should eq 'UnknownClassname'
       end
     end
   end
index 4deb967..1520382 100644 (file)
@@ -1,9 +1,223 @@
 # -*- encoding: utf-8 -*-
 require 'spec_helper'
-
+#コマ絵
 describe PanelPicture do
+  before do
+    Factory :admin
+    @user = Factory( :user_yas)
+    @author = @user.author
+    @artist = Factory :artist_yas, :author_id => @author.id
+    @license = Factory :license
+    @op = Factory :original_picture, :artist_id => @artist.id, :license_id => @license.id
+    @rp = Factory :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => @op.id
+    @panel = Factory :panel, :author_id => @author.id
+  end
+  
   describe '検証に於いて' do
     before do
     end
+    it 'オーソドックスなデータなら通る' do
+      @pp = Factory.build :panel_picture, :panel_id => @panel.id, :resource_picture_id => @rp.id
+      @pp.save!
+      @pp.should be_valid
+    end
+    
+    context 'panel_idを検証するとき' do
+      before do
+        @pp = Factory.build :panel_picture, :panel_id => @panel.id, :resource_picture_id => @rp.id
+      end
+      it 'テストデータの確認' do
+        @pp.panel_id = @panel.id
+        @pp.should be_valid
+      end
+      it '数値でなければ失敗する' do
+        @pp.panel_id = 'a'
+        @pp.should_not be_valid
+      end
+    end
+    context 'resource_picture_idを検証するとき' do
+      before do
+        @pp = Factory.build :panel_picture, :panel_id => @panel.id, :resource_picture_id => @rp.id
+      end
+      it 'テストデータの確認' do
+        @pp.resource_picture_id = @rp.id
+        @pp.should be_valid
+      end
+      it 'nullなら失敗する' do
+        @pp.resource_picture_id = nil
+        @pp.should_not be_valid
+      end
+      it '数値でなければ失敗する' do
+        @pp.resource_picture_id = 'a'
+        @pp.should_not be_valid
+      end
+      it '存在するフキダシテンプレートでなければ失敗する' do
+        @pp.resource_picture_id = 0
+        @pp.should_not be_valid
+      end
+    end
+    context 'linkを検証するとき' do
+      before do
+        @pp = Factory.build :panel_picture, :panel_id => @panel.id, :resource_picture_id => @rp.id
+      end
+      it 'テストデータの確認' do
+        @pp.link = 'abcdefghi0abcdefghi0abcdefghi0abcdefghi0abcdefghi0'*4
+        @pp.should be_valid
+      end
+      it 'nullでも通る' do
+        @pp.link = ''
+        @pp.should be_valid
+      end
+      it '201文字以上なら失敗する' do
+        @pp.link = 'a'*201
+        @pp.should_not be_valid
+      end
+    end
+    context 'xを検証するとき' do
+      before do
+        @pp = Factory.build :panel_picture, :panel_id => @panel.id, :resource_picture_id => @rp.id
+      end
+      it 'テストデータの確認' do
+        @pp.x = '1'
+        @pp.should be_valid
+      end
+      it 'nullなら失敗する' do
+        @pp.x = nil
+        @pp.should_not be_valid
+      end
+      it '数値でなければ失敗する' do
+        @pp.x = 'a'
+        @pp.should_not be_valid
+      end
+      it '0なら通る' do
+        @pp.x = '0'
+        @pp.should be_valid
+      end
+      it '負でも通る' do
+        @pp.x = -1
+        @pp.should be_valid
+      end
+    end
+    context 'yを検証するとき' do
+      before do
+        @pp = Factory.build :panel_picture, :panel_id => @panel.id, :resource_picture_id => @rp.id
+      end
+      it 'テストデータの確認' do
+        @pp.y = '1'
+        @pp.should be_valid
+      end
+      it 'nullなら失敗する' do
+        @pp.y = nil
+        @pp.should_not be_valid
+      end
+      it '数値でなければ失敗する' do
+        @pp.y = 'a'
+        @pp.should_not be_valid
+      end
+      it '0なら通る' do
+        @pp.y = '0'
+        @pp.should be_valid
+      end
+      it '負でも通る' do
+        @pp.y = -1
+        @pp.should be_valid
+      end
+    end
+    context 'widthを検証するとき' do
+      before do
+        @pp = Factory.build :panel_picture, :panel_id => @panel.id, :resource_picture_id => @rp.id
+      end
+      it 'テストデータの確認' do
+        @pp.width = 1
+        @pp.should be_valid
+      end
+      it 'nullなら失敗する' do
+        @pp.width = nil
+        @pp.should_not be_valid
+      end
+      it '数値でなければ失敗する' do
+        @pp.width = 'a'
+        @pp.should_not be_valid
+      end
+      it '0なら失敗する' do
+        @pp.width = '0'
+        @pp.should_not be_valid
+      end
+      it '負でも通る' do
+        @pp.width = -1
+        @pp.should be_valid
+      end
+    end
+    context 'heightを検証するとき' do
+      before do
+        @pp = Factory.build :panel_picture, :panel_id => @panel.id, :resource_picture_id => @rp.id
+      end
+      it 'テストデータの確認' do
+        @pp.height = '1'
+        @pp.should be_valid
+      end
+      it 'nullなら失敗する' do
+        @pp.height = nil
+        @pp.should_not be_valid
+      end
+      it '数値でなければ失敗する' do
+        @pp.height = 'a'
+        @pp.should_not be_valid
+      end
+      it '0なら失敗する' do
+        @pp.height = '0'
+        @pp.should_not be_valid
+      end
+      it '負でも通る' do
+        @pp.height = -1
+        @pp.should be_valid
+      end
+    end
+    context 'zを検証するとき' do
+      before do
+        @pp = Factory.build :panel_picture, :panel_id => @panel.id, :resource_picture_id => @rp.id
+      end
+      it 'テストデータの確認' do
+        @pp.z = 1
+        @pp.should be_valid
+      end
+      it 'nullなら失敗する' do
+        @pp.z = nil
+        @pp.should_not be_valid
+      end
+      it '数値でなければ失敗する' do
+        @pp.z = 'a'
+        @pp.should_not be_valid
+      end
+      it '負なら失敗する' do
+        @pp.z = -1
+        @pp.should_not be_valid
+      end
+      it '負なら失敗する' do
+        @pp.z = 0
+        @pp.should_not be_valid
+      end
+    end
+    context 'tを検証するとき' do
+      before do
+        @pp = Factory.build :panel_picture, :panel_id => @panel.id, :resource_picture_id => @rp.id
+      end
+      it 'テストデータの確認' do
+        @pp.t = 0
+        @pp.should be_valid
+      end
+      it 'nullなら失敗する' do
+        @pp.t = nil
+        @pp.should_not be_valid
+      end
+      it '数値でなければ失敗する' do
+        @pp.t = 'a'
+        @pp.should_not be_valid
+      end
+      it '負なら失敗する' do
+        @pp.t = -1
+        @pp.should_not be_valid
+      end
+    end
   end
 end
diff --git a/spec/requests/comics_spec.rb b/spec/requests/comics_spec.rb
deleted file mode 100644 (file)
index 20de573..0000000
+++ /dev/null
@@ -1,11 +0,0 @@
-require 'spec_helper'
-
-describe "Comics" do
-  describe "GET /comics" do
-    it "works! (now write some real specs)" do
-      # Run the generator again with the --webrat flag if you want to use webrat methods/matchers
-      get comics_path
-      response.status.should be(200)
-    end
-  end
-end
diff --git a/spec/routing/comics_routing_spec.rb b/spec/routing/comics_routing_spec.rb
deleted file mode 100644 (file)
index 173c1ec..0000000
+++ /dev/null
@@ -1,35 +0,0 @@
-require "spec_helper"
-
-describe ComicsController do
-  describe "routing" do
-
-    it "routes to #index" do
-      get("/comics").should route_to("comics#index")
-    end
-
-    it "routes to #new" do
-      get("/comics/new").should route_to("comics#new")
-    end
-
-    it "routes to #show" do
-      get("/comics/1").should route_to("comics#show", :id => "1")
-    end
-
-    it "routes to #edit" do
-      get("/comics/1/edit").should route_to("comics#edit", :id => "1")
-    end
-
-    it "routes to #create" do
-      post("/comics").should route_to("comics#create")
-    end
-
-    it "routes to #update" do
-      put("/comics/1").should route_to("comics#update", :id => "1")
-    end
-
-    it "routes to #destroy" do
-      delete("/comics/1").should route_to("comics#destroy", :id => "1")
-    end
-
-  end
-end
diff --git a/spec/views/comics/edit.html.erb_spec.rb b/spec/views/comics/edit.html.erb_spec.rb
deleted file mode 100644 (file)
index 42357cc..0000000
+++ /dev/null
@@ -1,15 +0,0 @@
-require 'spec_helper'
-
-describe "comics/edit.html.erb" do
-  before(:each) do
-    @comic = assign(:comic, stub_model(Comic))
-  end
-
-  it "renders the edit comic form" do
-    render
-
-    # Run the generator again with the --webrat flag if you want to use webrat matchers
-    assert_select "form", :action => comics_path(@comic), :method => "post" do
-    end
-  end
-end
diff --git a/spec/views/comics/index.html.erb_spec.rb b/spec/views/comics/index.html.erb_spec.rb
deleted file mode 100644 (file)
index 306c04c..0000000
+++ /dev/null
@@ -1,14 +0,0 @@
-require 'spec_helper'
-
-describe "comics/index.html.erb" do
-  before(:each) do
-    assign(:comics, [
-      stub_model(Comic),
-      stub_model(Comic)
-    ])
-  end
-
-  it "renders a list of comics" do
-    render
-  end
-end
diff --git a/spec/views/comics/new.html.erb_spec.rb b/spec/views/comics/new.html.erb_spec.rb
deleted file mode 100644 (file)
index d850215..0000000
+++ /dev/null
@@ -1,15 +0,0 @@
-require 'spec_helper'
-
-describe "comics/new.html.erb" do
-  before(:each) do
-    assign(:comic, stub_model(Comic).as_new_record)
-  end
-
-  it "renders new comic form" do
-    render
-
-    # Run the generator again with the --webrat flag if you want to use webrat matchers
-    assert_select "form", :action => comics_path, :method => "post" do
-    end
-  end
-end
diff --git a/spec/views/comics/show.html.erb_spec.rb b/spec/views/comics/show.html.erb_spec.rb
deleted file mode 100644 (file)
index b0fc5f2..0000000
+++ /dev/null
@@ -1,11 +0,0 @@
-require 'spec_helper'
-
-describe "comics/show.html.erb" do
-  before(:each) do
-    @comic = assign(:comic, stub_model(Comic))
-  end
-
-  it "renders attributes in <p>" do
-    render
-  end
-end
diff --git a/vendor/plugins/pettan_importer/lib/pettan_importer.rb b/vendor/plugins/pettan_importer/lib/pettan_importer.rb
new file mode 100644 (file)
index 0000000..808cf7a
--- /dev/null
@@ -0,0 +1,68 @@
+#インポート処理
+require 'common'
+
+module ActiveRecord\r
+  class Base\r
+    module PettanImporter
+      def self.included(base)
+        base.extend(ClassMethods)
+        base.__send__ :include, InstanceMethods
+      end
+      
+      module ClassMethods
+        def each_import data
+          data.each do |n, d|
+            yield n, d
+          end
+        end
+        
+        def modify_object(name, attr, key = 'name')
+          c = 'find_by_' + key.to_s
+          r = self.__send__ c, name
+          if r
+            r.attributes = attr
+          else
+            r = self.new attr
+            r[key] = name if r[key].blank?
+          end
+          r
+        end
+        
+        def import_text(data, &blk)
+          d = JSON.parse_no_except(data)
+          return false unless d
+          res = []
+          self.transaction do
+            d.each do |name, item|
+              m = blk.call(name, item)
+              res.push(m) unless m.valid?
+            end
+            raise ActiveRecord::Rollback unless res.empty?
+          end
+          res
+        end
+        
+        def import_file(filename, &blk)
+          t = nil
+          begin
+            t = File.open(filename, 'r').read
+          rescue
+            return false
+          end
+          self.import_text t, &blk
+        end
+        
+      end
+      
+      module InstanceMethods
+        private
+        
+        public
+        
+      end
+      
+    end
+    include PettanImporter
+  end
+end
+
diff --git a/vendor/plugins/pettan_importer/test/import_spec.rb b/vendor/plugins/pettan_importer/test/import_spec.rb
new file mode 100644 (file)
index 0000000..1dd7a84
--- /dev/null
@@ -0,0 +1,261 @@
+# -*- encoding: utf-8 -*-
+require 'spec_helper'
+require File.expand_path(File.dirname(__FILE__) + '/import')\r
+#インポート処理
+
+describe Import do
+  before do
+    @t = '{"Z": {"a": 1, "b": "Z"}}'
+    @j = JSON.parse @t
+    @ts = '{"Z": {"a": 1, "b": "Z"}, "X": {"a": 2, "b": "X"}}'
+    @js = JSON.parse @ts
+    @tes = '{"1": {"a": 0}, "2": {"a": 0}, "Z": {"a": 2, "b": "Z"}}'
+    @jes = JSON.parse @tes
+    @f = File.expand_path(File.dirname(__FILE__) + '/import.json')
+  end
+
+  describe '対象オブジェクトの更新準備に於いて' do
+    context 'つつがなく終わるとき' do
+      before do
+        @n = @j.keys.first
+        @a = @j.values.first
+      end
+      it 'キーカラムをnameで補充している' do
+        Import.stub(:find_by_name).with(any_args).and_return(nil)
+        Import.should_receive(:__send__).with('find_by_name', @n).exactly(1)
+        r = Import.modify_object(@n, @a)
+      end
+      it 'オブジェクト取得を依頼している' do
+        Import.stub(:find_by_b).with(any_args).and_return(nil)
+        Import.should_receive(:__send__).with('find_by_b', @n).exactly(1)
+        r = Import.modify_object(@n, @a, :b)
+      end
+      it 'カラム書き換えを依頼している' do
+        Import.create! @a
+        Import.any_instance.should_receive(:attributes=).with(any_args).exactly(1)
+        r = Import.modify_object(@n, @a, :b)
+      end
+      it 'オブジェクトを返す' do
+        Import.create! @a
+        r = Import.modify_object(@n, @a, :b)
+        r.is_a?(Import).should be_true
+      end
+    end
+    context 'キー値が一致する行がないとき' do
+      before do
+        @n = @j.keys.first
+        @a = @j.values.first
+      end
+      it '新規オブジェクトを準備して返す' do
+        r = Import.modify_object(@n, @a, :b)
+        r.should be_a_new Import
+      end
+      it 'オブジェクトが渡したデータで書き換えられている' do
+        r = Import.modify_object(@n, @a, :b)
+        r["a"].should eq 1
+        r["b"].should eq "Z"
+      end
+    end
+    context 'キー値が一致する行があるとき' do
+      before do
+        @n = @j.keys.first
+        @a = @j.values.first
+        Import.create!(@a)
+      end
+      it '該当行を返す' do
+        r = Import.modify_object(@n, {:a => 3}, :b)
+        r.is_a?(Import).should be_true
+        r.should_not be_a_new Import
+        r[:b].should eq @n
+      end
+      it 'オブジェクトが渡したデータで書き換えられている' do
+        r = Import.modify_object(@n, {:a => 3}, :b)
+        r[:a].should eq 3
+      end
+    end
+  end
+  
+  describe '繰り返し処理に於いて' do
+    before do
+    end
+    context '単体データを渡されたとき' do
+      it '一回処理' do
+        r = []
+        Import.each_import @j do |n, i|
+          r << i
+        end
+        r.size.should eq 1
+      end
+      it '一回処理' do
+        r = []
+        Import.each_import @j do |n, i|
+          r << n
+        end
+        r.first.should eq "Z"
+      end
+      it '一回処理' do
+        r = []
+        Import.each_import @j do |n, i|
+          r << i
+        end
+        r.first["a"].should eq 1
+        r.first["b"].should eq "Z"
+      end
+    end
+    context '複数を渡されたとき' do
+      it '二回処理' do
+        r = []
+        Import.each_import @js do |n, i|
+          r << i
+        end
+        r.size.should eq 2
+      end
+    end
+  end
+  
+  describe 'テキスト取り込みに於いて' do
+    #成功でTrue、パース失敗でFalse、失敗は保存エラーのモデルを配列で返す
+    before do
+    end
+    context 'つつがなく終わるとき' do
+      it 'Json解析を依頼する' do
+        JSON.should_receive(:parse_no_except).with(any_args).exactly(1)
+        JSON.stub(:parse_no_except).with(any_args).and_return(@j)
+        Import.import_text(@t) {|name, attr| Import.new(attr)}
+      end
+      it '更新を一回依頼する' do
+        JSON.stub(:parse_no_except).with(any_args).and_return(@j)
+        Import.any_instance.stub(:valid?).with(any_args).and_return(true)
+        #newでスタブを作るとインスタンス生成ができないので、テスト用スタブで
+        Import.should_receive(:etest).with(any_args).exactly(1)
+        Import.import_text(@t) {|name, attr| Import.etest ; Import.new(attr) }
+      end
+      it '[]を返す' do
+        JSON.stub(:parse_no_except).with(any_args).and_return(@j)
+        Import.any_instance.stub(:valid?).with(any_args).and_return(true)
+        Import.import_text(@t) {|name, attr| Import.new(attr)}.should eq []
+      end
+    end
+    context '複数データがつつがなく終わるとき' do
+      it 'ライセンス更新を二回依頼する' do
+        JSON.stub(:parse_no_except).with(any_args).and_return(@js)
+        Import.any_instance.stub(:valid?).with(any_args).and_return(true)
+        #newでスタブを作るとインスタンス生成ができないので、テスト用スタブで
+        Import.should_receive(:etest).with(any_args).exactly(2)
+        Import.import_text(@ts) {|name, attr| Import.etest ; Import.new(attr)}.should eq []
+      end
+      it '[]を返す' do
+        Import.import_text(@ts) {|name, attr| Import.new(attr)}.should eq []
+      end
+    end
+    #例外ケース
+    context 'Json解析に失敗したとき' do
+      before do
+        JSON.stub(:parse_no_except).with(any_args).and_return(false)
+      end
+      it 'Falseを返す' do
+        Import.import_text(@t){|name, attr| Import.new(attr)}.should be_false
+      end
+    end
+    context '作成に失敗したとき' do
+      before do
+        Import.any_instance.stub(:valid?).with(any_args).and_return(false)
+      end
+      it '配列を返す' do
+        r = Import.import_text(@t){|name, attr| Import.new(attr)}
+        r.is_a?(Array).should be_true
+      end
+      it '配列の中身は一件' do
+        r = Import.import_text(@t){|name, attr| Import.new(attr)}
+        r.should have(1).items
+      end
+      it 'オブジェクトが入っている' do
+        r = Import.import_text(@t){|name, attr| Import.new(attr)}
+        r.first.is_a?(Import).should be_true
+      end
+    end
+    context '複数の作成に失敗したとき' do
+      #三件中、二件の失敗、一件を成功させ、成功データは戻り値に含まないことを確認する
+      it '行の数に変化がない' do
+        lambda {
+          Import.import_text(@tes){|name, attr| Import.create(attr)}
+        }.should_not change Import, :count
+      end
+      it '途中で保存に失敗しても全件更新依頼する' do
+        Import.import_text(@tes){|name, attr| Import.create(attr)}
+      end
+      it '配列を返す' do
+        r = Import.import_text(@tes){|name, attr| Import.create(attr)}
+        r.is_a?(Array).should be_true
+      end
+      it '配列の中身は2件' do
+        r = Import.import_text(@tes){|name, attr| Import.create(attr)}
+        r.should have(2).items
+      end
+      it '配列の中身は失敗したオブジェクトが入っている' do
+        r = Import.import_text(@tes){|name, attr| Import.create(attr)}
+        r[0].is_a?(Import).should be_true
+        r[0]["a"].should eq 0
+        r[1].is_a?(Import).should be_true
+        r[1]["a"].should eq 0
+      end
+    end
+  end
+  
+  describe 'インポートエラーの表示に於いて' do
+  end
+  
+  describe 'ファイル取り込みに於いて' do
+    before do
+      Import.stub(:import).with(any_args).and_return(true)
+    end
+    context 'つつがなく終わるとき' do
+      before do
+        Import.stub(:import).with(any_args).and_return(true)
+      end
+      it 'ファイルを開いてテキストを読む' do
+        File.should_receive(:open).with(@f, 'r').exactly(1)
+        Import.import_file(@f){|name, attr| Import.new(attr) }
+      end
+      it 'テキスト取り込みを依頼する' do
+        Import.should_receive(:import_text).with(any_args).exactly(1)
+        Import.import_file(@f){|name, attr| Import.new(attr) }
+      end
+      it 'ブロックがあるとき更新を一回依頼する' do
+        Import.any_instance.stub(:valid?).with(any_args).and_return(true)
+        #newでスタブを作るとインスタンス生成ができないので、テスト用スタブで
+        Import.should_receive(:etest).with(any_args).exactly(1)
+        Import.import_file(@f) {|name, attr| Import.etest ; Import.new(attr) }
+      end
+      #テキスト取り込み成功でTrueが返る
+      it 'Trueを返す' do
+        Import.import_file(@f){|name, attr| Import.new(attr) }.should be_true
+      end
+    end
+    context 'ファイルが開けないとき' do
+      before do
+        File.stub(:open).with(any_args).and_raise('StandardError')
+      end
+      it 'ファイルエラーのメッセージを出力する' do
+        pending
+      end
+      it 'Falseを返す' do
+        Import.import_file(@f){|name, attr| Import.new(attr) }.should be_false
+      end
+    end
+    #失敗したときは、失敗したライセンスが配列で返る
+    context 'テキスト取り込みが失敗したとき' do
+      before do
+        Import.stub(:import_text).with(any_args).and_return(false)
+      end
+      it '各コモンライセンスのエラーメッセージを出力する' do
+        pending
+      end
+      it 'Falseを返す' do
+        Import.import_file(@f) {|name, attr| Import.new(attr) }.should be_false
+      end
+    end
+  end
+  
+end
+