--- /dev/null
+# -*- encoding: utf-8 -*-
+#ライセンス対照表
+require 'spec_helper'
+
+describe ProviderLicense 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 = @user.author #ユーザ作成時に連動して作成される
+ @provider_status = FactoryGirl.create :provider_status
+ @provider = FactoryGirl.create :provider, :provider_status_id => @provider_status.id
+ end
+ describe '検証に於いて' do
+ before do
+ @pl = FactoryGirl.build :provider_license, :provider_id => @provider.id, :providers_license_id => 2, :demanders_license_id => @license.id
+ end
+
+ context 'オーソドックスなデータのとき' do
+ it '下限データが通る' do
+ @pl.should be_valid
+ end
+ it '上限データが通る' do
+ @pl.should be_valid
+ end
+ end
+
+ context 'provider_idを検証するとき' do
+ it 'nullなら失敗する' do
+ @pl.provider_id = nil
+ @pl.should_not be_valid
+ end
+ it '数値でなければ失敗する' do
+ @pl.provider_id = 'a'
+ @pl.should_not be_valid
+ end
+ it '存在する貸手でなければ失敗する' do
+ @pl.provider_id = 0
+ @pl.should_not be_valid
+ end
+ end
+ context 'providers_license_idを検証するとき' do
+ it 'nullなら失敗する' do
+ @pl.providers_license_id = nil
+ @pl.should_not be_valid
+ end
+ it '数値でなければ失敗する' do
+ @pl.providers_license_id = 'a'
+ @pl.should_not be_valid
+ end
+ end
+ context 'demanders_license_idを検証するとき' do
+ it 'nullなら失敗する' do
+ @pl.demanders_license_id = nil
+ @pl.should_not be_valid
+ end
+ it '数値でなければ失敗する' do
+ @pl.demanders_license_id = 'a'
+ @pl.should_not be_valid
+ end
+ end
+ end
+
+ describe '対照表取得に於いて' do
+ before do
+ @pl = FactoryGirl.create :provider_license, :provider_id => @provider.id, :providers_license_id => 2, :demanders_license_id => @license.id
+ end
+ it '取得できれば真を返す' do
+ r = ProviderLicense.exist_license @provider.id, 2
+ r.should be_true
+ end
+ it '取得できなければ偽を返す' do
+ r = ProviderLicense.exist_license @provider.id, 1
+ r.should be_false
+ r = ProviderLicense.exist_license 0, 2
+ r.should be_false
+ end
+ end
+
+ describe 'インポートに於いて' do
+ before do
+ @license2 = FactoryGirl.create :license, :license_group_id => @lg.id, :system_picture_id => @sp.id, :name => 'license2', :url => 'http://licen.se/2'
+ end
+ context '事前チェックする' do
+ it '対照表取得を問い合わせている' do
+ ProviderLicense.stub(:exist_license).with(any_args).and_return(true)
+ ProviderLicense.should_receive(:exist_license).with(any_args).exactly(1)
+ r = ProviderLicense.import @provider.id, [@license.attributes]
+ end
+ it 'ライセンスモデルに管理名取得を依頼している' do
+ License.stub(:find_by_name).with(any_args).and_return(@license)
+ License.should_receive(:find_by_name).with(any_args).exactly(1)
+ r = ProviderLicense.import @provider.id, [@license.attributes]
+ end
+ it '対照表オブジェクトを保存している' do
+ ProviderLicense.any_instance.stub(:save).with(any_args).and_return(true)
+ ProviderLicense.any_instance.should_receive(:save).with(any_args).exactly(1)
+ r = ProviderLicense.import @provider.id, [@license.attributes]
+ end
+ end
+ context 'つつがなく終わるとき' do
+ it '空っぽの配列を返す' do
+ r = ProviderLicense.import @provider.id, [@license.attributes]
+ r.should be_empty
+ end
+ it '対照表が追加される' do
+ lambda {
+ r = ProviderLicense.import @provider.id, [@license.attributes]
+ }.should change ProviderLicense, :count
+ end
+ end
+ context '複数インポートのとき' do
+ it '空っぽの配列を返す' do
+ r = ProviderLicense.import @provider.id, [@license.attributes, @license2.attributes]
+ r.should be_empty
+ end
+ it '対照表が追加される' do
+ lambda {
+ r = ProviderLicense.import @provider.id, [@license.attributes, @license2.attributes]
+ }.should change(ProviderLicense, :count).by(2)
+ end
+ end
+ #警告ケース
+ context '対照表オブジェクトの保存に失敗したとき' do
+ before do
+ ProviderLicense.any_instance.stub(:save).with(any_args).and_return(false)
+ end
+ it '結果に貸手側ライセンスのカラム値を追加している' do
+ r = ProviderLicense.import @provider.id, [@license.attributes]
+ r.should_not be_empty
+ end
+ end
+ context 'ライセンスの管理名取得に失敗したとき' do
+ before do
+ License.stub(:find_by_name).with(any_args).and_return(nil)
+ end
+ it '結果に貸手側ライセンスのカラム値を追加している' do
+ r = ProviderLicense.import @provider.id, [@license.attributes]
+ r.should_not be_empty
+ end
+ end
+ end
+end
+