OSDN Git Service

t#31223:add them contents list for author and artist
[pettanr/pettanr.git] / spec / models / provider_license_spec.rb
1 # -*- encoding: utf-8 -*-
2 #ライセンス対照表
3 require 'spec_helper'
4
5 describe ProviderLicense do
6   before do
7     @admin = FactoryGirl.create :admin
8     @sp = FactoryGirl.create :system_picture
9     @lg = FactoryGirl.create :license_group
10     @license = FactoryGirl.create :license, :license_group_id => @lg.id, :system_picture_id => @sp.id
11     @user = FactoryGirl.create :user_yas
12     @author = @user.author    #ユーザ作成時に連動して作成される
13     @provider_status = FactoryGirl.create :provider_status
14     @provider = FactoryGirl.create :provider, :provider_status_id => @provider_status.id
15   end
16   describe '検証に於いて' do
17     before do
18       @pl = FactoryGirl.build :provider_license, :provider_id => @provider.id, :providers_license_id => 2, :demanders_license_id => @license.id
19     end
20     
21     context 'オーソドックスなデータのとき' do
22       it '下限データが通る' do
23         @pl.should be_valid
24       end
25       it '上限データが通る' do
26         @pl.should be_valid
27       end
28     end
29     
30     context 'provider_idを検証するとき' do
31       it 'nullなら失敗する' do
32         @pl.provider_id = nil
33         @pl.should_not be_valid
34       end
35       it '数値でなければ失敗する' do
36         @pl.provider_id = 'a'
37         @pl.should_not be_valid
38       end
39       it '存在する貸手でなければ失敗する' do
40         @pl.provider_id = 0
41         @pl.should_not be_valid
42       end
43     end
44     context 'providers_license_idを検証するとき' do
45       it 'nullなら失敗する' do
46         @pl.providers_license_id = nil
47         @pl.should_not be_valid
48       end
49       it '数値でなければ失敗する' do
50         @pl.providers_license_id = 'a'
51         @pl.should_not be_valid
52       end
53     end
54     context 'demanders_license_idを検証するとき' do
55       it 'nullなら失敗する' do
56         @pl.demanders_license_id = nil
57         @pl.should_not be_valid
58       end
59       it '数値でなければ失敗する' do
60         @pl.demanders_license_id = 'a'
61         @pl.should_not be_valid
62       end
63     end
64   end
65   
66   describe '対照表取得に於いて' do
67     before do
68       @pl = FactoryGirl.create :provider_license, :provider_id => @provider.id, :providers_license_id => 2, :demanders_license_id => @license.id
69     end
70     it '取得できれば真を返す' do
71       r = ProviderLicense.exist_license @provider.id, 2
72       r.should be_true
73     end
74     it '取得できなければ偽を返す' do
75       r = ProviderLicense.exist_license @provider.id, 1
76       r.should be_false
77       r = ProviderLicense.exist_license 0, 2
78       r.should be_false
79     end
80   end
81   
82   describe 'インポートに於いて' do
83     before do
84       @license2 = FactoryGirl.create :license, :license_group_id => @lg.id, :system_picture_id => @sp.id, :name => 'license2', :url => 'http://licen.se/2'
85     end
86     context '事前チェックする' do
87       it '対照表取得を問い合わせている' do
88         ProviderLicense.stub(:exist_license).with(any_args).and_return(true)
89         ProviderLicense.should_receive(:exist_license).with(any_args).exactly(1)
90         r = ProviderLicense.import @provider.id, [@license.attributes]
91       end
92       it 'ライセンスモデルに管理名取得を依頼している' do
93         License.stub(:find_by_name).with(any_args).and_return(@license)
94         License.should_receive(:find_by_name).with(any_args).exactly(1)
95         r = ProviderLicense.import @provider.id, [@license.attributes]
96       end
97       it '対照表オブジェクトを保存している' do
98         ProviderLicense.any_instance.stub(:save).with(any_args).and_return(true)
99         ProviderLicense.any_instance.should_receive(:save).with(any_args).exactly(1)
100         r = ProviderLicense.import @provider.id, [@license.attributes]
101       end
102     end
103     context 'つつがなく終わるとき' do
104       it '空っぽの配列を返す' do
105         r = ProviderLicense.import @provider.id, [@license.attributes]
106         r.should be_empty
107       end
108       it '対照表が追加される' do
109         lambda {
110           r = ProviderLicense.import @provider.id, [@license.attributes]
111         }.should change ProviderLicense, :count
112       end
113     end
114     context '複数インポートのとき' do
115       it '空っぽの配列を返す' do
116         r = ProviderLicense.import @provider.id, [@license.attributes, @license2.attributes]
117         r.should be_empty
118       end
119       it '対照表が追加される' do
120         lambda {
121           r = ProviderLicense.import @provider.id, [@license.attributes, @license2.attributes]
122         }.should change(ProviderLicense, :count).by(2)
123       end
124     end
125     #警告ケース
126     context '対照表オブジェクトの保存に失敗したとき' do
127       before do
128         ProviderLicense.any_instance.stub(:save).with(any_args).and_return(false)
129       end
130       it '結果に貸手側ライセンスのカラム値を追加している' do
131         r = ProviderLicense.import @provider.id, [@license.attributes]
132         r.should_not be_empty
133       end
134     end
135     context 'ライセンスの管理名取得に失敗したとき' do
136       before do
137         License.stub(:find_by_name).with(any_args).and_return(nil)
138       end
139       it '結果に貸手側ライセンスのカラム値を追加している' do
140         r = ProviderLicense.import @provider.id, [@license.attributes]
141         r.should_not be_empty
142       end
143     end
144   end
145 end
146