OSDN Git Service

t#30328:pull porting
[pettanr/pettanr.git] / spec / models / provider_artist_spec.rb
diff --git a/spec/models/provider_artist_spec.rb b/spec/models/provider_artist_spec.rb
new file mode 100644 (file)
index 0000000..1f4e275
--- /dev/null
@@ -0,0 +1,187 @@
+# -*- encoding: utf-8 -*-
+#絵師対照表
+require 'spec_helper'
+
+describe ProviderArtist do
+  before do
+    @admin = FactoryGirl.create :admin
+    @sp = FactoryGirl.create :system_picture
+    @lg = FactoryGirl.create :license_group
+    @license = FactoryGirl.create :license, :license_group_id => @lg.id, :system_picture_id => @sp.id
+    @user = FactoryGirl.create :user_yas
+    @author = FactoryGirl.create :author, :user_id => @user.id
+    @artist = FactoryGirl.create :artist, :author_id => @author.id
+    @other_user = FactoryGirl.create :user_yas
+    @other_author = FactoryGirl.create :author, :user_id => @other_user.id
+    @provider_status = FactoryGirl.create :provider_status
+    @provider = FactoryGirl.create :provider, :provider_status_id => @provider_status.id
+  end
+  describe '検証に於いて' do
+    before do
+      @pa = FactoryGirl.build :provider_artist, :provider_id => @provider.id, :providers_artist_id => 2, :demanders_artist_id => @artist.id
+    end
+    
+    context 'オーソドックスなデータのとき' do
+      it '下限データが通る' do
+        @pa.should be_valid
+      end
+      it '上限データが通る' do
+        @pa.should be_valid
+      end
+    end
+    
+    context 'provider_idを検証するとき' do
+      it 'nullなら失敗する' do
+        @pa.provider_id = nil
+        @pa.should_not be_valid
+      end
+      it '数値でなければ失敗する' do
+        @pa.provider_id = 'a'
+        @pa.should_not be_valid
+      end
+      it '存在する貸手でなければ失敗する' do
+        @pa.provider_id = 0
+        @pa.should_not be_valid
+      end
+    end
+    context 'providers_artist_idを検証するとき' do
+      it 'nullなら失敗する' do
+        @pa.providers_artist_id = nil
+        @pa.should_not be_valid
+      end
+      it '数値でなければ失敗する' do
+        @pa.providers_artist_id = 'a'
+        @pa.should_not be_valid
+      end
+    end
+    context 'demanders_artist_idを検証するとき' do
+      it 'nullなら失敗する' do
+        @pa.demanders_artist_id = nil
+        @pa.should_not be_valid
+      end
+      it '数値でなければ失敗する' do
+        @pa.demanders_artist_id = 'a'
+        @pa.should_not be_valid
+      end
+    end
+  end
+  
+  describe '対照表取得に於いて' do
+    before do
+      @pa = FactoryGirl.create :provider_artist, :provider_id => @provider.id, :providers_artist_id => 2, :demanders_artist_id => @artist.id
+    end
+    it '取得した対照表を返す' do
+      r = ProviderArtist.get_one @provider.id, 2
+      r.should be_true
+    end
+    it '該当する対照表が存在しなかったら、新規に興した対照表オブジェクトを返す' do
+      r = ProviderArtist.get_one @provider.id, 1
+      r.should be_a_new ProviderArtist
+      r = ProviderArtist.get_one 0, 2
+      r.should be_a_new ProviderArtist
+    end
+  end
+  
+  describe '貸手側絵師同期に於いて' do
+    before do
+      @pa = FactoryGirl.create :provider_artist, :provider_id => @provider.id, :providers_artist_id => 2, :demanders_artist_id => @artist.id
+      @newpa = ProviderArtist.new
+    end
+    context '自身に絵師がリンクしてないとき' do
+      before do
+      end
+      it '絵師オブジェクトを新規に興す' do
+        lambda {
+          r = @newpa.modify_artist @artist.attributes
+        }.should change Artist, :count
+      end
+    end
+    context '自身に絵師がリンクしているとき' do
+      it '自身から絵師を取得する' do
+        lambda {
+          r = @pa.modify_artist @artist.attributes
+        }.should_not change Artist, :count
+      end
+    end
+    it '貸手側絵師カラム値から絵師名をセットする 絵師オブジェクトを保存する' do
+      r = @pa.modify_artist :name => 'artist2'
+      @artist.reload
+      @artist.name.should eq 'artist2'
+    end
+    it '作家idをクリアする。(外絵師の証明)' do
+      r = @pa.modify_artist :name => 'artist2'
+      @artist.reload
+      @artist.author_id.should be_blank
+    end
+    it '絵師オブジェクトを返す' do
+      r = @pa.modify_artist @artist.attributes
+      r.should eq @artist
+    end
+  end
+
+  describe 'インポートに於いて' do
+    before do
+      @artist2 = FactoryGirl.create :artist, :author_id => @other_author.id, :name => 'artist2'
+    end
+    context '事前チェックする' do
+      before do
+        ProviderArtist.stub(:get_one).with(any_args).and_return(ProviderArtist.new)
+        ProviderArtist.any_instance.stub(:modify_artist).with(any_args).and_return(@artist)
+        ProviderArtist.any_instance.stub(:save).with(any_args).and_return(true)
+      end
+      it '対照表取得を問い合わせている' do
+        ProviderArtist.should_receive(:get_one).with(any_args).exactly(1)
+        r = ProviderArtist.import @provider.id, [@artist.attributes]
+      end
+      it '対照表に絵師同期を依頼してしている' do
+        ProviderArtist.any_instance.should_receive(:modify_artist).with(any_args).exactly(1)
+        r = ProviderArtist.import @provider.id, [@artist.attributes]
+      end
+      it '対照表オブジェクトを保存している' do
+        ProviderArtist.any_instance.should_receive(:save).with(any_args).exactly(1)
+        r = ProviderArtist.import @provider.id, [@artist.attributes]
+      end
+    end
+    context 'つつがなく終わるとき' do
+      it '空っぽの配列を返す' do
+        r = ProviderArtist.import @provider.id, [@artist.attributes]
+        r.should be_empty
+      end
+      it '対照表が追加される' do
+        lambda {
+          r = ProviderArtist.import @provider.id, [@artist.attributes]
+        }.should change ProviderArtist, :count
+      end
+    end
+    context '複数インポートのとき' do
+      it '空っぽの配列を返す' do
+        r = ProviderArtist.import @provider.id, [@artist.attributes, @artist2.attributes]
+        r.should be_empty
+      end
+      it '対照表が追加される' do
+        lambda {
+          r = ProviderArtist.import @provider.id, [@artist.attributes, @artist2.attributes]
+        }.should change(ProviderArtist, :count).by(2)
+      end
+    end
+    #警告ケース
+    context '対照表オブジェクトの保存に失敗したとき' do
+      before do
+        ProviderArtist.any_instance.stub(:save).with(any_args).and_return(false)
+      end
+     it '結果に貸手側絵師のカラム値を追加している' do
+        r = ProviderArtist.import @provider.id, [@artist.attributes]
+        r.should_not be_empty
+      end
+    end
+    context '絵師オブジェクトの保存に失敗したとき' do
+      before do
+        Artist.any_instance.stub(:save).with(any_args).and_return(false)
+      end
+      it '結果に貸手側絵師のカラム値を追加している' do
+        r = ProviderArtist.import @provider.id, [@artist.attributes]
+        r.should_not be_empty
+      end
+    end
+  end
+end