OSDN Git Service

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