1 # -*- encoding: utf-8 -*-
5 describe CommonLicense do
8 @f = Rails.root + 'spec/json/common_license.json'
9 @t = File.open(@f, 'r').read
11 @fs = Rails.root + 'spec/json/common_licenses.json'
12 @ts = File.open(@fs, 'r').read
14 @fes = Rails.root + 'spec/json/invalid_common_licenses.json'
15 @tes = File.open(@fes, 'r').read
16 @jes = JSON.parse @tes
23 it 'オーソドックスなデータなら通る' do
24 @cl = Factory.build :common_license, :license_id => @l.id
28 context 'nameを検証するとき' do
30 @cl = Factory.build :common_license, :license_id => @l.id
38 @cl.should_not be_valid
42 @cl.should_not be_valid
45 context 'urlを検証するとき' do
47 @cl = Factory.build :common_license, :license_id => @l.id
55 @cl.should_not be_valid
59 @cl.should_not be_valid
62 cl = Factory :common_license, :license_id => @l.id
63 @cl.should_not be_valid
70 context 'license_idを検証するとき' do
72 @cl = Factory.build :common_license
75 @cl.license_id = @l.id
80 @cl.should_not be_valid
84 @cl.should_not be_valid
86 it '存在するlicenseでなければ失敗する' do
88 @cl.should_not be_valid
93 describe '対象コモンライセンスの取得に於いて' do
95 @lc = Factory :license
96 @cl = Factory :common_license, :license_id => @lc.id
98 context 'urlが一致するコモンライセンスがないとき' do
99 it '新規コモンライセンスを準備して返す' do
100 r = CommonLicense.update_common_license Factory.attributes_for(:common_license, :url => 'http://domain.no')
101 r.should be_a_new CommonLicense
104 context 'urlが一致するコモンライセンスがあるとき' do
105 it '該当コモンライセンスを返す' do
106 prm = Factory.attributes_for(:common_license)
107 r = CommonLicense.update_common_license prm
108 r.is_a?(CommonLicense).should be_true
109 r.should_not be_a_new CommonLicense
110 r[:url].should eq prm[:url]
115 describe 'コモンライセンス更新に於いて' do
117 @lc = Factory :license
118 @cl = Factory :common_license, :license_id => @lc.id
119 @attr = Factory.attributes_for(:common_license, :name => 'exist case')
120 @newattr = Factory.attributes_for(:common_license, :url => 'http://domain.no')
122 context 'つつがなく終わるとき' do
123 it '対象コモンライセンスを問い合わせている' do
124 CommonLicense.stub(:update_common_license).with(any_args).and_return(CommonLicense.new(@attr))
125 CommonLicense.should_receive(:update_common_license).exactly(1)
126 CommonLicense.store @attr
129 it 'コモンライセンスを保存しようとしている' do
130 CommonLicense.any_instance.should_receive(:save).exactly(1)
131 CommonLicense.store @newattr
133 it 'コモンライセンスが作成されている' do
135 CommonLicense.store @newattr
136 }.should change CommonLicense, :count
138 context 'ライセンスとの連動' do
139 it 'ライセンスが作成されている' do
141 CommonLicense.store @newattr
142 }.should change License, :count
145 r = CommonLicense.store @newattr
146 l = License.find(r.license_id)
150 r = CommonLicense.store @newattr
151 l = License.find(r.license_id)
152 l.url.should eq r.url
153 l.name.should eq r.name
158 it 'コモンライセンスを保存しようとしている' do
159 CommonLicense.any_instance.should_receive(:save).exactly(1)
160 CommonLicense.store @attr
162 it 'コモンライセンスの数に変化がない' do
164 CommonLicense.store @attr
165 }.should_not change CommonLicense, :count
167 context 'ライセンスとの連動' do
168 it 'ライセンスの数に変化がない' do
170 CommonLicense.store @attr
171 }.should_not change License, :count
174 r = CommonLicense.store @attr
175 l = License.find(r.license_id)
179 r = CommonLicense.store @attr
180 l = License.find(r.license_id)
181 l.url.should eq r.url
182 l.name.should eq r.name
187 r = CommonLicense.store @newattr
188 r.url.should eq @newattr[:url]
189 r.name.should eq @newattr[:name]
191 it '保存されたCommonLicenseオブジェクトを返す' do
192 r = CommonLicense.store @newattr
193 r.should_not be_a_new CommonLicense
196 context 'ライセンスの作成に失敗するとき' do
198 License.any_instance.stub(:save).with(any_args).and_return(false)
203 r = CommonLicense.store @newattr
204 }.should_not change License, :count
206 it 'コモンライセンスに変化がない' do
208 r = CommonLicense.store @newattr
209 }.should_not change CommonLicense, :count
213 it 'コモンライセンス属性に変化がない' do
215 r = CommonLicense.store @attr
216 }.should_not change License.find(@cl.id), :name
218 it 'ライセンス属性に変化がない' do
220 r = CommonLicense.store @attr
221 }.should_not change License.find(@lc.id), :name
225 context 'コモンライセンスの作成に失敗するとき' do
227 CommonLicense.any_instance.stub(:save).with(any_args).and_return(false)
232 r = CommonLicense.store @newattr
233 }.should_not change License, :count
235 it 'コモンライセンスに変化がない' do
237 r = CommonLicense.store @newattr
238 }.should_not change CommonLicense, :count
242 it 'コモンライセンス属性に変化がない' do
244 r = CommonLicense.store @attr
245 }.should_not change License.find(@cl.id), :name
247 it 'ライセンス属性に変化がない' do
249 r = CommonLicense.store @attr
250 }.should_not change License.find(@lc.id), :name
256 describe '単体取得に於いて' do
258 @cl = Factory.build :common_license
262 l = CommonLicense.show @cl.id
265 context '関連テーブルオプションがないとき' do
266 it 'ライセンスだけを含んでいる' do
267 r = CommonLicense.show_include_opt
268 r.should eq [:license]
271 context '関連テーブルオプションでコマを含ませたとき' do
272 it 'ライセンスと作家を含んでいる' do
273 r = CommonLicense.show_include_opt(:include => :author)
274 r.should eq [:license, :author]
278 describe '一覧取得に於いて' do
280 @cl = Factory.build :common_license
283 context 'page補正について' do
284 it '文字列から数値に変換される' do
285 CommonLicense.page('8').should eq 8
288 CommonLicense.page().should eq 1
291 CommonLicense.page('0').should eq 1
294 context 'page_size補正について' do
295 it '文字列から数値に変換される' do
296 CommonLicense.page_size('7').should eq 7
298 it 'nilの場合はCommonLicense.default_page_sizeになる' do
299 CommonLicense.page_size().should eq CommonLicense.default_page_size
301 it '0以下の場合はCommonLicense.default_page_sizeになる' do
302 CommonLicense.page_size('0').should eq CommonLicense.default_page_size
304 it 'CommonLicense.max_page_sizeを超えた場合はCommonLicense.max_page_sizeになる' do
305 CommonLicense.page_size('1000').should eq CommonLicense.max_page_size
309 l = CommonLicense.list
313 n = Factory.build :common_license, :url => 'tes.to', :name => 'peta2.2'
315 l = CommonLicense.list
318 context 'DBに5件あって1ページの件数を2件に変えたとして' do
320 @license2 = Factory.build :common_license, :url => 'tes.to2', :name => 'peta2.2'
322 @license3 = Factory.build :common_license, :url => 'tes.to3', :name => 'peta2.3'
324 @license4 = Factory.build :common_license, :url => 'tes.to4', :name => 'peta2.4'
326 @license5 = Factory.build :common_license, :url => 'tes.to5', :name => 'peta2.5'
328 CommonLicense.stub(:default_page_size).and_return(2)
331 l = CommonLicense.list
332 l.should have(2).items
334 it 'page=1なら末尾2件を返す' do
336 l = CommonLicense.list({}, 1)
337 l.should eq [@cl, @license2]
339 it 'page=2なら中間2件を返す' do
340 l = CommonLicense.list({}, 2)
341 l.should eq [@license3, @license4]
343 it 'page=3なら先頭1件を返す' do
344 l = CommonLicense.list({}, 3)
345 l.should eq [@license5]
351 describe 'Json解析に於いて' do
354 context 'テキストを渡されたとき' do
356 JSON.should_receive(:parse).with(@t).exactly(1)
357 r = CommonLicense.parse @t
359 it '単数データならHashで返す' do
360 r = CommonLicense.parse @t
361 r.is_a?(Hash).should be_true
363 it '複数データならArrayで返す' do
364 r = CommonLicense.parse @ts
365 r.is_a?(Array).should be_true
368 context 'パース失敗したとき' do
370 JSON.should_receive(:parse).with(any_args).and_raise('StandardError')
371 r = CommonLicense.parse @t
377 describe 'Jsonの繰り返し処理に於いて' do
380 context '単体データを渡されたとき' do
383 CommonLicense.each_license @j do |i|
389 context '複数を渡されたとき' do
392 CommonLicense.each_license @js do |i|
400 describe 'テキスト取り込みに於いて' do
401 #成功でTrue、パース失敗でFalse、失敗は保存エラーのモデルを配列で返す
402 #Licenseとの連動が完成していること
405 context 'つつがなく終わるとき' do
407 CommonLicense.should_receive(:parse).with(any_args).exactly(1)
408 CommonLicense.stub(:parse).with(any_args).and_return(@j)
409 CommonLicense.import(@t)
412 CommonLicense.should_receive(:each_license).with(any_args).exactly(1)
413 CommonLicense.import(@t)
415 it 'ライセンス更新を一回依頼する' do
416 CommonLicense.stub(:store).with(any_args).and_return(CommonLicense.new)
417 CommonLicense.any_instance.stub(:valid?).with(any_args).and_return(true)
418 CommonLicense.should_receive(:store).with(any_args).exactly(1)
419 CommonLicense.import(@t)
421 it 'コモンライセンスが追加される' do
423 CommonLicense.import(@t)
424 }.should change CommonLicense, :count
427 CommonLicense.import(@t).should eq []
430 context '複数データがつつがなく終わるとき' do
431 it 'ライセンス更新を二回依頼する' do
432 CommonLicense.stub(:store).with(any_args).and_return(CommonLicense.new)
433 CommonLicense.any_instance.stub(:valid?).with(any_args).and_return(true)
434 CommonLicense.should_receive(:store).with(any_args).exactly(2)
435 CommonLicense.import(@ts)
437 it 'コモンライセンスが二個追加される' do
439 CommonLicense.import(@ts)
440 }.should change(CommonLicense, :count).by 2
443 CommonLicense.import(@ts).should eq []
447 context 'Json解析に失敗したとき' do
449 CommonLicense.stub(:parse).with(any_args).and_return(false)
451 it 'コモンライセンスの数に変化がない' do
453 CommonLicense.import(@t)
454 }.should_not change CommonLicense, :count
457 CommonLicense.import(@t).should be_false
460 context 'コモンライセンス作成に失敗したとき' do
462 CommonLicense.any_instance.stub(:save).with(any_args).and_return(false)
463 CommonLicense.any_instance.stub(:valid?).with(any_args).and_return(false)
465 it 'コモンライセンスの数に変化がない' do
467 CommonLicense.import(@t)
468 }.should_not change CommonLicense, :count
471 r = CommonLicense.import(@t)
472 r.is_a?(Array).should be_true
475 r = CommonLicense.import(@t)
476 r.should have(1).items
478 it 'コモンライセンスオブジェクトが入っている' do
479 r = CommonLicense.import(@t)
480 r.first.is_a?(CommonLicense).should be_true
483 context '複数のコモンライセンス作成に失敗したとき' do
484 #三件中、二件の失敗、一件を成功させ、成功データは戻り値に含まないことを確認する
485 it 'コモンライセンスの数に変化がない' do
487 CommonLicense.import(@tes)
488 }.should_not change CommonLicense, :count
490 it '途中で保存に失敗しても全件更新依頼する' do
491 CommonLicense.stub(:store).with(any_args).and_return(CommonLicense.new)
492 CommonLicense.should_receive(:store).with(any_args).exactly(3)
493 CommonLicense.import(@tes)
496 r = CommonLicense.import(@tes)
497 r.is_a?(Array).should be_true
500 r = CommonLicense.import(@tes)
501 r.should have(2).items
503 it '配列の中身は失敗したコモンライセンスオブジェクトが入っている' do
504 r = CommonLicense.import(@tes)
505 r[0].is_a?(CommonLicense).should be_true
506 r[0]["name"].should eq 'fail1'
507 r[1].is_a?(CommonLicense).should be_true
508 r[1]["name"].should eq 'fail2'
513 describe 'ファイル取り込みに於いて' do
515 CommonLicense.stub(:import).with(any_args).and_return(true)
517 context 'つつがなく終わるとき' do
519 CommonLicense.stub(:import).with(any_args).and_return(true)
521 it 'ファイルを開いてテキストを読む' do
522 File.should_receive(:open).with(@f, 'r').exactly(1)
523 CommonLicense.import_file(@f)
525 it 'テキスト取り込みを依頼する' do
526 CommonLicense.should_receive(:import).with(any_args).exactly(1)
527 CommonLicense.import_file(@f)
531 CommonLicense.import_file(@f).should be_true
534 context 'ファイルが開けないとき' do
536 File.stub(:open).with(any_args).and_raise('StandardError')
538 it 'ファイルエラーのメッセージを出力する' do
542 CommonLicense.import_file(@f).should be_false
545 #失敗したときは、失敗したライセンスが配列で返る
546 context 'テキスト取り込みが失敗したとき' do
548 CommonLicense.stub(:import).with(any_args).and_return(false)
550 it '各コモンライセンスのエラーメッセージを出力する' do
554 CommonLicense.import_file(@f).should be_false