OSDN Git Service

389fd69981f232164764f4ca26e70aed1d5fbb41
[pettanr/pettanr.git] / spec / models / common_license_spec.rb
1 # -*- encoding: utf-8 -*-
2 #コモンライセンス
3 require 'spec_helper'
4
5 describe CommonLicense do
6   before do
7     #テストデータを用意してね
8     @f = Rails.root + 'spec/json/common_license.json'
9     @t = File.open(@f, 'r').read
10     @j = JSON.parse @t
11     @fs = Rails.root + 'spec/json/common_licenses.json'
12     @ts = File.open(@fs, 'r').read
13     @js = JSON.parse @ts
14     @fes = Rails.root + 'spec/json/invalid_common_licenses.json'
15     @tes = File.open(@fes, 'r').read
16     @jes = JSON.parse @tes
17   end
18   describe '検証に於いて' do
19     before do
20       @l = Factory :license
21     end
22     
23     it 'オーソドックスなデータなら通る' do
24       @cl = Factory.build :common_license, :license_id => @l.id
25       @cl.should be_valid
26     end
27     
28     context 'nameを検証するとき' do
29       before do
30         @cl = Factory.build :common_license, :license_id => @l.id
31       end
32       it 'テストデータの確認' do
33         @cl.name = 'CC by'
34         @cl.should be_valid
35       end
36       it 'nullなら失敗する' do
37         @cl.name = ''
38         @cl.should_not be_valid
39       end
40       it '51文字以上なら失敗する' do
41         @cl.name = 'a'*51
42         @cl.should_not be_valid
43       end
44     end
45     context 'urlを検証するとき' do
46       before do
47         @cl = Factory.build :common_license, :license_id => @l.id
48       end
49       it 'テストデータの確認' do
50         @cl.url = 'CC by'
51         @cl.should be_valid
52       end
53       it 'nullなら失敗する' do
54         @cl.url = ''
55         @cl.should_not be_valid
56       end
57       it '201文字以上なら失敗する' do
58         @cl.url = 'a'*201
59         @cl.should_not be_valid
60       end
61       it '重複していたら失敗する' do
62         cl = Factory :common_license, :license_id => @l.id
63         @cl.should_not be_valid
64       end
65       it 'url形式でなら失敗する' do
66         @cl.url = ''
67         pending
68       end
69     end
70     context 'license_idを検証するとき' do
71       before do
72         @cl = Factory.build :common_license
73       end
74       it 'テストデータの確認' do
75         @cl.license_id = @l.id
76         @cl.should be_valid
77       end
78       it 'nullなら失敗する' do
79         @cl.license_id = nil
80         @cl.should_not be_valid
81       end
82       it '数値でなければ失敗する' do
83         @cl.license_id = 'a'
84         @cl.should_not be_valid
85       end
86       it '存在するlicenseでなければ失敗する' do
87         @cl.license_id = 0
88         @cl.should_not be_valid
89       end
90     end
91   end
92   
93   describe '対象コモンライセンスの取得に於いて' do
94     before do
95       @lc = Factory :license
96       @cl = Factory :common_license, :license_id => @lc.id
97     end
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
102       end
103     end
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]
111       end
112     end
113   end
114   
115   describe 'コモンライセンス更新に於いて' do
116     before 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')
121     end
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
127       end
128       context '新規のとき' do
129         it 'コモンライセンスを保存しようとしている' do
130           CommonLicense.any_instance.should_receive(:save).exactly(1)
131           CommonLicense.store @newattr
132         end
133         it 'コモンライセンスが作成されている' do
134           lambda {
135             CommonLicense.store @newattr
136           }.should change CommonLicense, :count
137         end
138         context 'ライセンスとの連動' do
139           it 'ライセンスが作成されている' do
140             lambda {
141               CommonLicense.store @newattr
142             }.should change License, :count
143           end
144           it '両者がリンクされている' do
145             r = CommonLicense.store @newattr
146             l = License.find(r.license_id)
147             l.should be_true
148           end
149           it '属性が一致している' do
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
154           end
155         end
156       end
157       context '更新のとき' do
158         it 'コモンライセンスを保存しようとしている' do
159           CommonLicense.any_instance.should_receive(:save).exactly(1)
160           CommonLicense.store @attr
161         end
162         it 'コモンライセンスの数に変化がない' do
163           lambda {
164             CommonLicense.store @attr
165           }.should_not change CommonLicense, :count
166         end
167         context 'ライセンスとの連動' do
168           it 'ライセンスの数に変化がない' do
169             lambda {
170               CommonLicense.store @attr
171             }.should_not change License, :count
172           end
173           it '両者がリンクされている' do
174             r = CommonLicense.store @attr
175             l = License.find(r.license_id)
176             l.should be_true
177           end
178           it '属性が一致している' do
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
183           end
184         end
185       end
186       it '属性が一致している' do
187         r = CommonLicense.store @newattr
188         r.url.should eq @newattr[:url]
189         r.name.should eq @newattr[:name]
190       end
191       it '保存されたCommonLicenseオブジェクトを返す' do
192         r = CommonLicense.store @newattr
193         r.should_not be_a_new CommonLicense
194       end
195     end
196     context 'ライセンスの作成に失敗するとき' do
197       before do
198         License.any_instance.stub(:save).with(any_args).and_return(false)
199       end
200       context '新規のとき' do
201         it 'ライセンスに変化がない' do
202           lambda {
203             r = CommonLicense.store @newattr
204           }.should_not change License, :count
205         end
206         it 'コモンライセンスに変化がない' do
207           lambda {
208             r = CommonLicense.store @newattr
209           }.should_not change CommonLicense, :count
210         end
211       end
212       context '更新のとき' do
213         it 'コモンライセンス属性に変化がない' do
214           lambda {
215             r = CommonLicense.store @attr
216           }.should_not change License.find(@cl.id), :name
217         end
218         it 'ライセンス属性に変化がない' do
219           lambda {
220             r = CommonLicense.store @attr
221           }.should_not change License.find(@lc.id), :name
222         end
223       end
224     end
225     context 'コモンライセンスの作成に失敗するとき' do
226       before do
227         CommonLicense.any_instance.stub(:save).with(any_args).and_return(false)
228       end
229       context '新規のとき' do
230         it 'ライセンスに変化がない' do
231           lambda {
232             r = CommonLicense.store @newattr
233           }.should_not change License, :count
234         end
235         it 'コモンライセンスに変化がない' do
236           lambda {
237             r = CommonLicense.store @newattr
238           }.should_not change CommonLicense, :count
239         end
240       end
241       context '更新のとき' do
242         it 'コモンライセンス属性に変化がない' do
243           lambda {
244             r = CommonLicense.store @attr
245           }.should_not change License.find(@cl.id), :name
246         end
247         it 'ライセンス属性に変化がない' do
248           lambda {
249             r = CommonLicense.store @attr
250           }.should_not change License.find(@lc.id), :name
251         end
252       end
253     end
254   end
255 =begin  
256   describe '単体取得に於いて' do
257     before do
258       @cl = Factory.build :common_license
259       @cl.store
260     end
261     it '指定のライセンスを返す' do
262       l = CommonLicense.show @cl.id
263       l.should eq @cl
264     end
265     context '関連テーブルオプションがないとき' do
266       it 'ライセンスだけを含んでいる' do
267         r = CommonLicense.show_include_opt
268         r.should eq [:license]
269       end
270     end
271     context '関連テーブルオプションでコマを含ませたとき' do
272       it 'ライセンスと作家を含んでいる' do
273         r = CommonLicense.show_include_opt(:include => :author)
274         r.should eq [:license, :author]
275       end
276     end
277   end
278   describe '一覧取得に於いて' do
279     before do
280       @cl = Factory.build :common_license
281       @cl.store
282     end
283     context 'page補正について' do
284       it '文字列から数値に変換される' do
285         CommonLicense.page('8').should eq 8
286       end
287       it 'nilの場合は1になる' do
288         CommonLicense.page().should eq 1
289       end
290       it '0以下の場合は1になる' do
291         CommonLicense.page('0').should eq 1
292       end
293     end
294     context 'page_size補正について' do
295       it '文字列から数値に変換される' do
296         CommonLicense.page_size('7').should eq 7
297       end
298       it 'nilの場合はCommonLicense.default_page_sizeになる' do
299         CommonLicense.page_size().should eq CommonLicense.default_page_size
300       end
301       it '0以下の場合はCommonLicense.default_page_sizeになる' do
302         CommonLicense.page_size('0').should eq CommonLicense.default_page_size
303       end
304       it 'CommonLicense.max_page_sizeを超えた場合はCommonLicense.max_page_sizeになる' do
305         CommonLicense.page_size('1000').should eq CommonLicense.max_page_size
306       end
307     end
308     it 'リストを返す' do
309       l = CommonLicense.list
310       l.should eq [@cl]
311     end
312     it '名前順で並んでいる' do
313       n = Factory.build :common_license, :url => 'tes.to', :name => 'peta2.2'
314       n.store
315       l = CommonLicense.list
316       l.should eq [@cl, n]
317     end
318     context 'DBに5件あって1ページの件数を2件に変えたとして' do
319       before do
320         @license2 = Factory.build :common_license, :url => 'tes.to2', :name => 'peta2.2'
321         @license2.store
322         @license3 = Factory.build :common_license, :url => 'tes.to3', :name => 'peta2.3'
323         @license3.store
324         @license4 = Factory.build :common_license, :url => 'tes.to4', :name => 'peta2.4'
325         @license4.store
326         @license5 = Factory.build :common_license, :url => 'tes.to5', :name => 'peta2.5'
327         @license5.store
328         CommonLicense.stub(:default_page_size).and_return(2)
329       end
330       it '通常は2件を返す' do
331         l = CommonLicense.list
332         l.should have(2).items 
333       end
334       it 'page=1なら末尾2件を返す' do
335         #時系列で並んでいる
336         l = CommonLicense.list({}, 1)
337         l.should eq [@cl, @license2]
338       end
339       it 'page=2なら中間2件を返す' do
340         l = CommonLicense.list({}, 2)
341         l.should eq [@license3, @license4]
342       end
343       it 'page=3なら先頭1件を返す' do
344         l = CommonLicense.list({}, 3)
345         l.should eq [@license5]
346       end
347     end
348   end
349 =end
350   
351   describe 'Json解析に於いて' do
352     before do
353     end
354     context 'テキストを渡されたとき' do
355       it 'Json解析している' do
356         JSON.should_receive(:parse).with(@t).exactly(1)
357         r = CommonLicense.parse @t
358       end
359       it '単数データならHashで返す' do
360         r = CommonLicense.parse @t
361         r.is_a?(Hash).should be_true
362       end
363       it '複数データならArrayで返す' do
364         r = CommonLicense.parse @ts
365         r.is_a?(Array).should be_true
366       end
367     end
368     context 'パース失敗したとき' do
369       it 'Falseを返す' do
370         JSON.should_receive(:parse).with(any_args).and_raise('StandardError')
371         r = CommonLicense.parse @t
372         r.should be_false
373       end
374     end
375   end
376   
377   describe 'Jsonの繰り返し処理に於いて' do
378     before do
379     end
380     context '単体データを渡されたとき' do
381       it '一回処理' do
382         r = []
383         CommonLicense.each_license @j do |i|
384           r << i
385         end
386         r.size.should eq 1
387       end
388     end
389     context '複数を渡されたとき' do
390       it '二回処理' do
391         r = []
392         CommonLicense.each_license @js do |i|
393           r << i
394         end
395         r.size.should eq 2
396       end
397     end
398   end
399   
400   describe 'テキスト取り込みに於いて' do
401     #成功でTrue、パース失敗でFalse、失敗は保存エラーのモデルを配列で返す
402     #Licenseとの連動が完成していること
403     before do
404     end
405     context 'つつがなく終わるとき' do
406       it 'Json解析を依頼する' 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)
410       end
411       it '繰り返し処理を依頼する' do
412         CommonLicense.should_receive(:each_license).with(any_args).exactly(1)
413         CommonLicense.import(@t)
414       end
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)
420       end
421       it 'コモンライセンスが追加される' do
422         lambda {
423           CommonLicense.import(@t)
424         }.should change CommonLicense, :count
425       end
426       it '[]を返す' do
427         CommonLicense.import(@t).should eq []
428       end
429     end
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)
436       end
437       it 'コモンライセンスが二個追加される' do
438         lambda {
439           CommonLicense.import(@ts)
440         }.should change(CommonLicense, :count).by 2
441       end
442       it '[]を返す' do
443         CommonLicense.import(@ts).should eq []
444       end
445     end
446     #例外ケース
447     context 'Json解析に失敗したとき' do
448       before do
449         CommonLicense.stub(:parse).with(any_args).and_return(false)
450       end
451       it 'コモンライセンスの数に変化がない' do
452         lambda {
453           CommonLicense.import(@t)
454         }.should_not change CommonLicense, :count
455       end
456       it 'Falseを返す' do
457         CommonLicense.import(@t).should be_false
458       end
459     end
460     context 'コモンライセンス作成に失敗したとき' do
461       before 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)
464       end
465       it 'コモンライセンスの数に変化がない' do
466         lambda {
467           CommonLicense.import(@t)
468         }.should_not change CommonLicense, :count
469       end
470       it '配列を返す' do
471         r = CommonLicense.import(@t)
472         r.is_a?(Array).should be_true
473       end
474       it '配列の中身は一件' do
475         r = CommonLicense.import(@t)
476         r.should have(1).items
477       end
478       it 'コモンライセンスオブジェクトが入っている' do
479         r = CommonLicense.import(@t)
480         r.first.is_a?(CommonLicense).should be_true
481       end
482     end
483     context '複数のコモンライセンス作成に失敗したとき' do
484       #三件中、二件の失敗、一件を成功させ、成功データは戻り値に含まないことを確認する
485       it 'コモンライセンスの数に変化がない' do
486         lambda {
487           CommonLicense.import(@tes)
488         }.should_not change CommonLicense, :count
489       end
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)
494       end
495       it '配列を返す' do
496         r = CommonLicense.import(@tes)
497         r.is_a?(Array).should be_true
498       end
499       it '配列の中身は2件' do
500         r = CommonLicense.import(@tes)
501         r.should have(2).items
502       end
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'
509       end
510     end
511   end
512   
513   describe 'インポートエラーの表示に於いて' do
514     before do
515       @l = Factory :license
516       @cl = Factory.build :common_license, :license_id => @l.id
517     end
518     it '全体エラーだけなら、そのまま返す' do
519       @cl.errors.add :base, 'base error'
520       @cl.import_error_message.should eq 'base error'
521     end
522     context '複数でエラーのとき' do
523       it '各エラーを改行で区切って結合して返す' do
524         @cl.errors.add :name, 'name error'
525         @cl.errors.add :url, 'url error'
526         @cl.import_error_message.should eq 'name error\nurl error'
527       end
528     end
529     context '区切り指定が<br>で複数でエラーのとき' do
530       it '各エラーを改行で区切って結合して返す' do
531         @cl.errors.add :name, 'name error'
532         @cl.errors.add :url, 'url error'
533         @cl.import_error_message('<br>').should eq 'name error<br>url error'
534       end
535     end
536   end
537   
538   describe 'ファイル取り込みに於いて' do
539     before do
540       CommonLicense.stub(:import).with(any_args).and_return(true)
541     end
542     context 'つつがなく終わるとき' do
543       before do
544         CommonLicense.stub(:import).with(any_args).and_return(true)
545       end
546       it 'ファイルを開いてテキストを読む' do
547         File.should_receive(:open).with(@f, 'r').exactly(1)
548         CommonLicense.import_file(@f)
549       end
550       it 'テキスト取り込みを依頼する' do
551         CommonLicense.should_receive(:import).with(any_args).exactly(1)
552         CommonLicense.import_file(@f)
553       end
554       #テキスト取り込み成功でTrueが返る
555       it 'Trueを返す' do
556         CommonLicense.import_file(@f).should be_true
557       end
558     end
559     context 'ファイルが開けないとき' do
560       before do
561         File.stub(:open).with(any_args).and_raise('StandardError')
562       end
563       it 'ファイルエラーのメッセージを出力する' do
564         pending
565       end
566       it 'Falseを返す' do
567         CommonLicense.import_file(@f).should be_false
568       end
569     end
570     #失敗したときは、失敗したライセンスが配列で返る
571     context 'テキスト取り込みが失敗したとき' do
572       before do
573         CommonLicense.stub(:import).with(any_args).and_return(false)
574       end
575       it '各コモンライセンスのエラーメッセージを出力する' do
576         pending
577       end
578       it 'Falseを返す' do
579         CommonLicense.import_file(@f).should be_false
580       end
581     end
582   end
583   
584 end