OSDN Git Service

pass license model test
[pettanr/pettanr.git] / spec / models / license_spec.rb
1 # -*- encoding: utf-8 -*-
2 #ライセンス
3 require 'spec_helper'
4
5 describe License do
6   before do
7   end
8   describe '検証に於いて' do
9     before do
10       @sp = Factory :system_picture
11       @lg = Factory :license_group
12       @l = Factory.build :license, :license_group_id => @lg.id, :system_picture_id => @sp.id
13     end
14     
15     it 'オーソドックスなデータなら通る' do
16       @l.should be_valid
17     end
18     
19     context 'license_group_idを検証するとき' do
20       it 'テストデータの確認' do\r
21         @l.license_group_id = @lg.id\r
22         @l.should be_valid\r
23       end\r
24       it 'nullなら失敗する' do
25         @l.license_group_id = ''
26         @l.should_not be_valid
27       end
28       it '数値でなければ失敗する' do\r
29         @l.license_group_id = 'a'\r
30         @l.should_not be_valid\r
31       end\r
32       it '存在するライセンスグループでなければ失敗する' do\r
33         @l.license_group_id = 0\r
34         @l.should_not be_valid\r
35       end\r
36     end
37     context 'nameを検証するとき' do
38       it 'テストデータの確認' do\r
39         @l.name = 'a'*50\r
40         @l.should be_valid\r
41       end\r
42       it 'nullなら失敗する' do
43         @l.name = ''
44         @l.should_not be_valid
45       end
46       it '51文字以上なら失敗する' do
47         @l.name = 'a'*51
48         @l.should_not be_valid
49       end
50       it '重複していたら失敗する' do
51         lc = Factory :license
52         @l.should_not be_valid
53       end
54     end
55     context 'captionを検証するとき' do
56       it 'テストデータの確認' do\r
57         @l.caption = 'a'*30\r
58         @l.should be_valid\r
59       end\r
60       it 'nullなら失敗する' do
61         @l.caption = ''
62         @l.should_not be_valid
63       end
64       it '31文字以上なら失敗する' do
65         @l.caption = 'a'*31
66         @l.should_not be_valid
67       end
68     end
69     context 'urlを検証するとき' do
70       it 'テストデータの確認' do\r
71         @l.url = 'http://test.jp/aaaaa' + 'a' * 180
72         @l.save!\r
73         @l.should be_valid\r
74       end\r
75       it 'nullなら失敗する' do
76         @l.url = ''
77         @l.should_not be_valid
78       end
79       it '201文字以上なら失敗する' do
80         @l.url = 'http://test.jp/aaaaa' + 'a' * 181
81         @l.should_not be_valid
82       end
83       it 'url形式でなら失敗する' do
84         @l.url = 'a'*200\r
85         @l.should_not be_valid\r
86       end
87     end
88     context 'system_picture_idを検証するとき' do
89       it 'テストデータの確認' do\r
90         @l.system_picture_id = @sp.id\r
91         @l.should be_valid\r
92       end\r
93       it 'nullなら失敗する' do
94         @l.system_picture_id = ''
95         @l.should_not be_valid
96       end
97       it '数値でなければ失敗する' do\r
98         @l.system_picture_id = 'a'\r
99         @l.should_not be_valid\r
100       end\r
101       it '存在するシステム画像でなければ失敗する' do\r
102         @l.system_picture_id = 0\r
103         @l.should_not be_valid\r
104       end\r
105     end
106   end
107   
108   describe '更新に於いて' do
109     before do
110       @lg = Factory :license_group
111       @f = Rails.root + 'spec/json/license_group.json'
112       @t = File.open(@f, 'r').read
113       @j = JSON.parse @t
114       @n = @j.keys.first
115       @a = @j.values.first
116       @attr = @a["licenses_attributes"]
117       @ln = @attr.keys.first
118       @la = @attr.values.first
119       @la["license_group_id"] = @lg.id
120     end
121     context 'つつがなく終わるとき' do
122       it 'システム画像置換を依頼する' do
123         License.stub(:replace_system_picture).with(any_args).and_return(true)
124         License.should_receive(:replace_system_picture).with(any_args).exactly(1)
125         License.stub(:modify_object).with(any_args).and_return(License.new)
126         License.any_instance.stub(:save).with(any_args).and_return(true)
127         r = License.store(@ln, @la)
128       end
129       it 'データ更新準備を依頼する' do
130         License.stub(:replace_system_picture).with(any_args).and_return(true)
131         License.stub(:modify_object).with(any_args).and_return(License.new)
132         License.should_receive(:modify_object).with(any_args).exactly(1)
133         License.any_instance.stub(:save).with(any_args).and_return(true)
134         r = License.store(@ln, @la)
135       end
136       it '保存を依頼する' do
137         License.stub(:replace_system_picture).with(any_args).and_return(true)
138         License.stub(:modify_object).with(any_args).and_return(License.new)
139         License.any_instance.stub(:save).with(any_args).and_return(true)
140         License.any_instance.should_receive(:save).with(any_args).exactly(1)
141         r = License.store(@ln, @la)
142       end
143       it 'オブジェクトを返す' do
144         r = License.store(@ln, @la)
145         r.is_a?(License).should be_true
146         r.name.should eq @ln
147         r.url.should eq @la["url"]
148       end
149       it 'ライセンスが作成されている' do
150         lambda {
151           r = License.store(@ln, @la)
152         }.should change License, :count
153       end
154       it 'システム画像が作成されている' do
155         lambda {
156           r = License.store(@ln, @la)
157         }.should change SystemPicture, :count
158       end
159     end
160     context 'システム画像置換が失敗するとき' do
161       before do
162         License.stub(:replace_system_picture).with(any_args).and_return(false)
163         License.stub(:modify_object).with(any_args).and_return(License.new)
164       end
165       it '全体エラーメッセージがセットされている' do
166         r = License.store(@ln, @la)
167         r.errors[:base].should_not be_blank
168       end
169       it 'ライセンスが作成されていない' do
170         lambda {
171           r = License.store(@ln, @la)
172         }.should_not change License, :count
173       end
174     end
175   end
176   
177   describe '複数の更新に於いて' do
178     before do
179       @fs = Rails.root + 'spec/json/license_groups.json'
180       @ts = File.open(@fs, 'r').read
181       @js = JSON.parse @ts
182       @n = @js.keys.last
183       @a = @js.values.last
184       @attr = @a["licenses_attributes"]
185     end
186     context '2件データでつつがなく終わるとき' do
187       it '更新を2回依頼する' do
188         License.stub(:store).with(any_args).and_return(License.new)
189         License.should_receive(:store).with(any_args).exactly(2)
190         License.any_instance.stub(:valid?).with(any_args).and_return(true)
191         r = License.stores(@attr)
192       end
193       it '失敗件数0を返す' do
194         License.stub(:store).with(any_args).and_return(License.new)
195         License.any_instance.stub(:valid?).with(any_args).and_return(true)
196         r = License.stores(@attr)
197         r.should eq 0
198       end
199     end
200     context '2件データで失敗するとき' do
201       it '更新を2回依頼する' do
202         License.stub(:store).with(any_args).and_return(License.new)
203         License.should_receive(:store).with(any_args).exactly(2)
204         License.any_instance.stub(:valid?).with(any_args).and_return(false)
205         r = License.stores(@attr)
206       end
207       it '失敗件数2を返す' do
208         License.stub(:store).with(any_args).and_return(License.new)
209         License.any_instance.stub(:valid?).with(any_args).and_return(false)
210         r = License.stores(@attr)
211         r.should eq 2
212       end
213     end
214     context 'attrsがnilなどのHashでないとき' do
215       it '処理にかけず0を返す' do
216         r = License.stores(nil)
217         r.should eq 0
218       end
219     end
220   end
221   
222   describe '単体取得に於いて' do
223     before do
224       @sp = Factory :system_picture
225       @lg = Factory :license_group
226       @l = Factory :license, :license_group_id => @lg.id, :system_picture_id => @sp.id
227     end
228     it '指定のコマを返す' do
229       l = License.show @l.id
230       l.should eq @l
231     end
232   end
233   describe '関連テーブルプションに於いて' do
234     context 'オプションがないとき' do
235       it 'ライセンスグループを含んでいる' do
236         r = License.show_include_opt
237         r.should eq [:license_group]
238       end
239     end
240     context 'オプションで素材ライセンスを含ませたとき' do
241       it 'ライセンスグループと素材ライセンスを含んでいる' do
242         r = License.show_include_opt(:include => :resource_picture_license)
243         r.should eq [:license_group, :resource_picture_license]
244       end
245     end
246   end
247   describe 'json単体出力オプションに於いて' do
248     it 'includeキーを含んでいる' do
249       r = License.show_json_include_opt
250       r.has_key?(:include).should be_true
251     end
252     it '1つの項目を含んでいる' do
253       r = License.show_json_include_opt[:include]
254       r.should have(1).items
255     end
256     it 'ライセンスグループを含んでいる' do
257       r = License.show_json_include_opt[:include]
258       r.has_key?(:license_group).should be_true
259     end
260   end
261   describe '一覧取得に於いて' do
262     before do
263       @sp = Factory :system_picture
264       @lg = Factory :license_group
265       @l = Factory :license, :name => 'peta2.0', :license_group_id => @lg.id, :system_picture_id => @sp.id
266       @lg2 = Factory :license_group, :name => 'pubdm'
267     end
268     it 'リストを返す' do
269       l = License.list @lg.id
270       l.should eq [@l]
271     end
272     it 'グループ順,名前順で並んでいる' do
273       @l2 = Factory :license, :name => 'peta3.0', :url => 'http://pe.ta/3.0', :license_group_id => @lg.id, :system_picture_id => @sp.id
274       @l3 = Factory :license, :name => 'pd1.0', :url => 'http://pb.dm/1.0', :license_group_id => @lg2.id, :system_picture_id => @sp.id
275       l = License.list @lg.id
276       l.should eq [@l, @l2]
277     end
278   end
279   describe 'list関連テーブルプションに於いて' do
280     it 'includeキーを含んでいる' do
281       r = License.list_opt
282       r.has_key?(:include).should be_true
283     end
284     it '1つの項目を含んでいる' do
285       r = License.list_opt[:include]
286       r.should have(1).items
287     end
288     it 'ライセンスグループを含んでいる' do
289       r = License.list_opt[:include]
290       r.has_key?(:license_group).should be_true
291     end
292   end
293   describe 'json一覧出力オプションに於いて' do
294     it 'includeキーを含んでいる' do
295       r = License.list_json_opt
296       r.has_key?(:include).should be_true
297     end
298     it '1つの項目を含んでいる' do
299       r = License.list_json_opt[:include]
300       r.should have(1).items
301     end
302     it 'ライセンスグループを含んでいる' do
303       r = License.list_json_opt[:include]
304       r.has_key?(:license_group).should be_true
305     end
306   end
307   
308 end