OSDN Git Service

Merge branch 'v05' of git.sourceforge.jp:/gitroot/pettanr/pettanr into v05
[pettanr/pettanr.git] / spec / models / provider_status_spec.rb
index 9e7b2ff..7887d43 100644 (file)
@@ -200,6 +200,7 @@ describe ProviderStatus do
       end
     end
   end
+  
   describe '待機中一覧取得に於いて' do
     before do
       @ps = FactoryGirl.create :provider_status
@@ -276,6 +277,84 @@ describe ProviderStatus do
       end
     end
   end
+  
+  describe '承認済リスト一覧取得に於いて' do
+    before do
+      @ps = FactoryGirl.create :provider_status, :token => 'aaaaa'
+      @pd = FactoryGirl.create :provider, :provider_status_id => @ps.id, :name => "6"
+    end
+    context 'つつがなく終わるとき' do
+      it '一覧取得オプションを利用している' do
+        ProviderStatus.stub(:list_opt).with(any_args).and_return({:include => {:provider => {}} })
+        ProviderStatus.should_receive(:list_opt).with(any_args).exactly(1)
+        r = ProviderStatus.approve_list
+      end
+    end
+    it 'リストを返す' do
+      r = ProviderStatus.approve_list
+      r.should eq [@ps]
+    end
+    it '管理名で並んでいる' do
+      @ps2 = FactoryGirl.create :provider_status, :token => 'aaaaa'
+      v = FactoryGirl.create :provider, :name => "0", :provider_status_id => @ps2.id
+      r = ProviderStatus.approve_list
+      r.should eq [@ps2, @ps]
+    end
+    it '借受状況のトークンが設定されている借受状況に限る' do
+      @ps2 = FactoryGirl.create :provider_status, :token => nil
+      v = FactoryGirl.create :provider, :name => "0", :provider_status_id => @ps2.id
+      r = ProviderStatus.approve_list
+      r.should eq [@ps]
+    end
+    context 'DBに5件あって1ページの件数を2件に変えたとして' do
+      before do
+        @ps2 = FactoryGirl.create :provider_status, :token => 'aaaaa'
+        @pd2 = FactoryGirl.create :provider, :provider_status_id => @ps2.id, :name => "5"
+        @ps3 = FactoryGirl.create :provider_status, :token => 'aaaaa'
+        @pd3 = FactoryGirl.create :provider, :provider_status_id => @ps3.id, :name => "4"
+        @ps4 = FactoryGirl.create :provider_status, :token => 'aaaaa'
+        @pd4 = FactoryGirl.create :provider, :provider_status_id => @ps4.id, :name => "3"
+        @ps5 = FactoryGirl.create :provider_status, :token => 'aaaaa'
+        @pd5 = FactoryGirl.create :provider, :provider_status_id => @ps5.id, :name => "2"
+        ProviderStatus.stub(:default_page_size).and_return(2)
+      end
+      it '通常は2件を返す' do
+        r = ProviderStatus.approve_list
+        r.should have(2).items 
+      end
+      it 'page=1なら末尾2件を返す' do
+        #管理名で並んでいる
+        r = ProviderStatus.approve_list(1)
+        r.should eq [@ps5, @ps4]
+      end
+      it 'page=2なら中間2件を返す' do
+        r = ProviderStatus.approve_list(2)
+        r.should eq [@ps3, @ps2]
+      end
+      it 'page=3なら先頭1件を返す' do
+        r = ProviderStatus.approve_list(3)
+        r.should eq [@ps]
+      end
+    end
+    context 'DBに5件あって1ページの件数を2件に変えたとして' do
+      before do
+        @ps2 = FactoryGirl.create :provider_status, :token => 'aaaaa'
+        @pd2 = FactoryGirl.create :provider, :provider_status_id => @ps2.id, :name => "5"
+        @ps3 = FactoryGirl.create :provider_status, :token => 'aaaaa'
+        @pd3 = FactoryGirl.create :provider, :provider_status_id => @ps3.id, :name => "4"
+        @ps4 = FactoryGirl.create :provider_status, :token => 'aaaaa'
+        @pd4 = FactoryGirl.create :provider, :provider_status_id => @ps4.id, :name => "3"
+        @ps5 = FactoryGirl.create :provider_status, :token => 'aaaaa'
+        @pd5 = FactoryGirl.create :provider, :provider_status_id => @ps5.id, :name => "2"
+        ProviderStatus.stub(:default_page_size).and_return(2)
+      end
+      it '件数0は全件(5件)を返す' do
+        r = ProviderStatus.approve_list 5, 0
+        r.should have(5).items 
+      end
+    end
+  end
+  
   describe '一覧取得オプションに於いて' do
     it 'includeキーを含んでいる' do
       r = ProviderStatus.list_opt
@@ -406,4 +485,267 @@ describe ProviderStatus do
     end
   end
   
+  describe 'エクスポートurl取得に於いて' do
+    before do
+      @token = 'aaaaaaaaaaaaaaaaa'
+      @ps = FactoryGirl.create :provider_status, :token => @token
+      @provider = FactoryGirl.create :provider, :provider_status_id => @ps.id
+    end
+    context '事前チェックしておく' do
+      before do
+        Provider.any_instance.stub(:export_url).with(any_args).and_return('http://demander.url/action.json')
+      end
+      it '借手にエクスポートurl取得を依頼している' do
+        Provider.any_instance.should_receive(:export_url).exactly(1)
+        @ps.export_url 'action', Time.now
+      end
+    end
+    context 'つつがなく終わるとき' do
+      before do
+        Provider.any_instance.stub(:export_url).with(any_args).and_return('http://demander.url/action.json')
+      end
+      it 'エクスポートurl文字列を返す' do
+        r = @ps.export_url 'action', nil
+        r.should eq 'http://demander.url/action.json?auth_token=' + @token
+      end
+    end
+    context 'エクスポート日時が設定されているとき' do
+      before do
+        Provider.any_instance.stub(:export_url).with(any_args).and_return('http://demander.url/action.json')
+      end
+      it 'エクスポート日時を追加する' do
+        r = @ps.export_url 'action', Time.parse('2000/1/1')
+        r.should match /20000101/
+      end
+    end
+  end
+  
+  describe '貸手からのエクスポート通信に於いて' do
+    before do
+      @ps = FactoryGirl.create :provider_status, :token => 'aaaaaaaaaaaaaaaaa'
+      @provider = FactoryGirl.create :provider, :provider_status_id => @ps.id
+    end
+    context '事前チェックしておく' do
+      before do
+        RestClient.stub(:get).with(any_args).and_return('{}')
+        JSON.stub(:parse).with(any_args).and_return({})
+      end
+      it 'Restクライアントに取得通信を依頼している' do
+        RestClient.should_receive(:get).exactly(1)
+        @ps.export_from_provider 'url'
+      end
+      it 'JSONライブラリにパースを依頼している' do
+        JSON.should_receive(:parse).exactly(1)
+        @ps.export_from_provider 'url'
+      end
+    end
+    context 'つつがなく終わるとき' do
+      before do
+        RestClient.stub(:get).with(any_args).and_return('{}')
+        JSON.stub(:parse).with(any_args).and_return({})
+      end
+      it 'JSONデータを返す' do
+        r = @ps.export_from_provider 'url'
+        r.is_a?(Hash).should be_true
+      end
+    end
+    context 'エクスポートでエラーが発生したとき' do
+      before do
+        RestClient.stub(:get).with(any_args).and_raise(StandardError)
+      end
+      it 'nilを返す' do
+        r = @ps.export_from_provider 'url'
+        r.should be_nil
+      end
+    end
+    context 'パースでエラーが発生したとき' do
+      before do
+        ProviderStatus.any_instance.stub(:export_by).with(any_args).and_return(nil)
+      end
+      it 'nilを返す' do
+        r = @ps.export_from_provider 'url'
+        r.should be_nil
+      end
+    end
+  end
+  
+  describe '貸手からのエクスポートに於いて' do
+    before do
+      @ps = FactoryGirl.create :provider_status, :token => 'aaaaaaaaaaaaaaaaa'
+      @provider = FactoryGirl.create :provider, :provider_status_id => @ps.id
+    end
+    context '事前チェックしておく' do
+      before do
+        ProviderStatus.any_instance.stub(:ymd_to_time).with(any_args).and_return(Time.parse('2000/1/1'))
+        ProviderStatus.any_instance.stub(:export_url).with(any_args).and_return('http://demander.url/action.json?auth_token=aaaaaaaaaaaaaaaa')
+        ProviderStatus.any_instance.stub(:export_from_provider).with(any_args).and_return([@license.attributes])
+      end
+      it '日付文字列変換を依頼している' do
+        ProviderStatus.any_instance.should_receive(:ymd_to_time).exactly(1)
+        @ps.export_by 'action', '20111010'
+      end
+      it 'エクスポートurl取得を依頼している' do
+        ProviderStatus.any_instance.should_receive(:export_url).exactly(1)
+        @ps.export_by 'action', '20111010'
+      end
+      it '貸手からのエクスポート通信を依頼している' do
+        ProviderStatus.any_instance.should_receive(:export_from_provider).exactly(1)
+        @ps.export_by 'action', '20111010'
+      end
+    end
+    context 'つつがなく終わるとき' do
+      before do
+        ProviderStatus.any_instance.stub(:ymd_to_time).with(any_args).and_return(Time.parse('2000/1/1'))
+        ProviderStatus.any_instance.stub(:export_url).with(any_args).and_return('http://demander.url/action.json?auth_token=aaaaaaaaaaaaaaaa')
+        ProviderStatus.any_instance.stub(:export_from_provider).with(any_args).and_return([@license.attributes])
+      end
+      it 'リストを返す' do
+        r = @ps.export_by 'action', '20111010'
+        r.is_a?(Array).should be_true
+      end
+    end
+    context 'エクスポートでエラーが発生したとき' do
+      before do
+        ProviderStatus.any_instance.stub(:ymd_to_time).with(any_args).and_return(Time.parse('2000/1/1'))
+        ProviderStatus.any_instance.stub(:export_url).with(any_args).and_return('http://demander.url/action.json?auth_token=aaaaaaaaaaaaaaaa')
+        ProviderStatus.any_instance.stub(:export_from_provider).with(any_args).and_return(nil)
+      end
+      it 'nilを返す' do
+        r = @ps.export_by 'action', '20111010'
+        r.should be_nil
+      end
+    end
+    context 'パースでエラーが発生したとき' do
+      before do
+        ProviderStatus.any_instance.stub(:ymd_to_time).with(any_args).and_return(Time.parse('2000/1/1'))
+        ProviderStatus.any_instance.stub(:export_url).with(any_args).and_return('http://demander.url/action.json?auth_token=aaaaaaaaaaaaaaaa')
+        ProviderStatus.any_instance.stub(:export_from_provider).with(any_args).and_return(nil)
+      end
+      it 'nilを返す' do
+        r = @ps.export_by 'action', '20111010'
+        r.should be_nil
+      end
+    end
+  end
+  
+  describe 'ライセンスインポートに於いて' do
+    before do
+      @ps = FactoryGirl.create :provider_status, :token => 'aaaaaaaaaaaaaaaaa'
+      @provider = FactoryGirl.create :provider, :provider_status_id => @ps.id
+    end
+    context '事前チェックしておく' do
+      before do
+        ProviderStatus.any_instance.stub(:export_by).with(any_args).and_return([@license.attributes])
+        ProviderLicense.stub(:import).with(any_args).and_return(LicenseImportResult.new([]))
+      end
+      it '貸手からのエクスポートを依頼している' do
+        ProviderStatus.any_instance.should_receive(:export_by).exactly(1)
+        r = @ps.licenses_import '20111010'
+      end
+      it 'ライセンス対照表モデルにインポートを依頼している' do
+        ProviderLicense.should_receive(:import).exactly(1)
+        r = @ps.licenses_import '20111010'
+      end
+    end
+    context 'つつがなく終わるとき' do
+      before do
+        ProviderStatus.any_instance.stub(:export_by).with(any_args).and_return([@license.attributes])
+        ProviderLicense.stub(:import).with(any_args).and_return(LicenseImportResult.new([]))
+      end
+      it 'ライセンスインポート結果を返す' do
+        r = @ps.licenses_import '20111010'
+        r.should be_response
+      end
+    end
+    context 'エクスポートに失敗したとき' do
+      before do
+        ProviderStatus.any_instance.stub(:export_by).with(any_args).and_return(nil)
+      end
+      it '受信に失敗したImportResultを返す' do
+        r = @ps.licenses_import '20111010'
+        r.should_not be_response
+      end
+    end
+  end
+  
+  describe '絵師インポートに於いて' do
+    before do
+      @ps = FactoryGirl.create :provider_status, :token => 'aaaaaaaaaaaaaaaaa'
+      @provider = FactoryGirl.create :provider, :provider_status_id => @ps.id
+    end
+    context '事前チェックしておく' do
+      before do
+        ProviderStatus.any_instance.stub(:export_by).with(any_args).and_return([])
+        ProviderArtist.stub(:import).with(any_args).and_return(ArtistImportResult.new([]))
+      end
+      it '貸手からのエクスポートを依頼している' do
+        ProviderStatus.any_instance.should_receive(:export_by).exactly(1)
+        @ps.artists_import '20111010'
+      end
+      it '絵師対照表モデルにインポートを依頼している' do
+        ProviderArtist.should_receive(:import).exactly(1)
+        @ps.artists_import '20111010'
+      end
+    end
+    context 'つつがなく終わるとき' do
+      before do
+        ProviderStatus.any_instance.stub(:export_by).with(any_args).and_return([])
+        ProviderArtist.stub(:import).with(any_args).and_return(ArtistImportResult.new([]))
+      end
+      it '絵師インポート結果を返す' do
+        r = @ps.artists_import '20111010'
+        r.should be_response
+      end
+    end
+    context 'エクスポートに失敗したとき' do
+      before do
+        ProviderStatus.any_instance.stub(:export_by).with(any_args).and_return(nil)
+      end
+      it '受信に失敗したImportResultを返す' do
+        r = @ps.licenses_import '20111010'
+        r.should_not be_response
+      end
+    end
+  end
+  
+  describe '素材インポートに於いて' do
+    before do
+      @ps = FactoryGirl.create :provider_status, :token => 'aaaaaaaaaaaaaaaaa'
+      @provider = FactoryGirl.create :provider, :provider_status_id => @ps.id
+    end
+    context '事前チェックしておく' do
+      before do
+        ProviderStatus.any_instance.stub(:export_by).with(any_args).and_return([])
+        ProviderOriginalPicture.stub(:import).with(any_args).and_return(OriginalPictureImportResult.new([]))
+      end
+      it '貸手からのエクスポートを依頼している' do
+        ProviderStatus.any_instance.should_receive(:export_by).exactly(2)
+        @ps.original_pictures_import '20111010'
+      end
+      it '原画対照表モデルにインポートを依頼している' do
+        ProviderOriginalPicture.should_receive(:import).exactly(1)
+        @ps.original_pictures_import '20111010'
+      end
+    end
+    context 'つつがなく終わるとき' do
+      before do
+        ProviderStatus.any_instance.stub(:export_by).with(any_args).and_return([])
+        ProviderOriginalPicture.stub(:import).with(any_args).and_return(OriginalPictureImportResult.new([]))
+      end
+      it '原画インポート結果を返す' do
+        r = @ps.original_pictures_import '20111010'
+        r.should be_response
+      end
+    end
+    context 'エクスポートに失敗したとき' do
+      before do
+        ProviderStatus.any_instance.stub(:export_by).with(any_args).and_return(nil)
+      end
+      it '受信に失敗したImportResultを返す' do
+        r = @ps.licenses_import '20111010'
+        r.should_not be_response
+      end
+    end
+  end
+  
 end