OSDN Git Service

2b07d604cbc0758b517ddee44fd63ad06de0cd97
[pettanr/pettanr.git] / spec / models / panel_spec.rb
1 # -*- encoding: utf-8 -*-\r
2 require 'spec_helper'
3
4 describe Panel do
5   before do
6     Factory :admin
7     @license = Factory :license
8     @user = Factory( :user_yas)
9     @author = @user.author
10     @artist = Factory :artist_yas, :author_id => @author.id\r
11     @other_user = Factory( :user_yas)
12     @other_author = @other_user.author
13     @other_artist = Factory :artist_yas, :author_id => @other_author.id
14     
15   end\r
16   
17   describe '検証に於いて' do
18     before do\r
19       @comic = Factory :comic, :author_id => @author.id
20     end
21     
22     it 'オーソドックスなデータなら通る' do
23       @panel = Factory.build :panel, :author_id => @author.id, :comic_id => @comic.id
24       @panel.should be_valid
25     end
26     
27     context 'comic_idを検証するとき' do
28       before do
29         @panel = Factory.build :panel, :author_id => @author.id, :comic_id => nil
30       end
31       it 'テストデータの確認' do
32         @panel.comic_id = @comic.id
33         @panel.should be_valid
34       end
35       it 'nullなら失敗する' do
36         @panel.comic_id = nil
37         @panel.should_not be_valid
38       end
39       it '数値でなければ失敗する' do
40         @panel.comic_id = 'a'
41         @panel.should_not be_valid
42       end
43       it '存在するコミックでなければ失敗する' do
44         @panel.comic_id = 0
45         @panel.should_not be_valid
46       end
47       it 'コミックidとtが重複していると失敗する' do
48         @panel.save\r
49         @panel2 = Factory.build :panel, :author_id => @author.id, :comic_id => @comic.id
50         @panel2.should_not be_valid
51       end
52     end
53     context 'resource_picture_idを検証するとき' do
54       before do
55         @panel = Factory.build :panel, :author_id => @author.id, :comic_id => @comic.id
56       end
57       it 'テストデータの確認' do
58         @panel.resource_picture_id = 1
59         @panel.should be_valid
60       end
61       it 'nullなら通る' do
62         @panel.resource_picture_id = nil
63         @panel.should be_valid
64       end
65       it '数値でなければ失敗する' do
66         @panel.resource_picture_id = 'a'
67         @panel.should_not be_valid
68       end
69       it '存在する素材でなければ失敗する' do
70         @panel.resource_picture_id = 0
71         @panel.should_not be_valid
72       end
73     end
74     context 'widthを検証するとき' do
75       before do
76         @panel = Factory.build :panel, :author_id => @author.id, :comic_id => @comic.id
77       end
78       it 'テストデータの確認' do
79         @panel.width = 1
80         @panel.should be_valid
81       end
82       it 'nullなら失敗する' do
83         @panel.width = nil
84         @panel.should_not be_valid
85       end
86       it '数値でなければ失敗する' do
87         @panel.width = 'a'
88         @panel.should_not be_valid
89       end
90       it '0なら失敗する' do
91         @panel.width = '0'
92         @panel.should_not be_valid
93       end
94       it '負でも失敗する' do
95         @panel.width = -1
96         @panel.should_not be_valid
97       end
98     end
99     context 'heightを検証するとき' do
100       before do
101         @panel = Factory.build :panel, :author_id => @author.id, :comic_id => @comic.id
102       end
103       it 'テストデータの確認' do
104         @panel.height = '1'
105         @panel.should be_valid
106       end
107       it 'nullなら失敗する' do
108         @panel.height = nil
109         @panel.should_not be_valid
110       end
111       it '数値でなければ失敗する' do
112         @panel.height = 'a'
113         @panel.should_not be_valid
114       end
115       it '0なら失敗する' do
116         @panel.height = '0'
117         @panel.should_not be_valid
118       end
119       it '負でも失敗する' do
120         @panel.height = -1
121         @panel.should_not be_valid
122       end
123     end
124     context 'borderを検証するとき' do
125       before do
126         @panel = Factory.build :panel, :author_id => @author.id, :comic_id => @comic.id
127       end
128       it 'テストデータの確認' do
129         @panel.border = '1'
130         @panel.should be_valid
131       end
132       it 'nullなら失敗する' do
133         @panel.border = nil
134         @panel.should_not be_valid
135       end
136       it '数値でなければ失敗する' do
137         @panel.border = 'a'
138         @panel.should_not be_valid
139       end
140       it '負なら失敗する' do
141         @panel.border = '-1'
142         @panel.should_not be_valid
143       end
144       it '0なら通る' do
145         @panel.border = 0
146         @panel.should be_valid
147       end
148     end
149     context 'xを検証するとき' do
150       before do
151         @panel = Factory.build :panel, :author_id => @author.id, :comic_id => @comic.id
152       end
153       it 'テストデータの確認' do
154         @panel.x = '1'
155         @panel.should be_valid
156       end
157       it '数値でなければ失敗する' do
158         @panel.x = 'a'
159         @panel.should_not be_valid
160       end
161       it '0なら通る' do
162         @panel.x = '0'
163         @panel.should be_valid
164       end
165       it '負でも通る' do
166         @panel.x = -1
167         @panel.should be_valid
168       end
169     end
170     context 'yを検証するとき' do
171       before do
172         @panel = Factory.build :panel, :author_id => @author.id, :comic_id => @comic.id
173       end
174       it 'テストデータの確認' do
175         @panel.y = '1'
176         @panel.should be_valid
177       end
178       it '数値でなければ失敗する' do
179         @panel.y = 'a'
180         @panel.should_not be_valid
181       end
182       it '0なら通る' do
183         @panel.y = '0'
184         @panel.should be_valid
185       end
186       it '負でも通る' do
187         @panel.y = -1
188         @panel.should be_valid
189       end
190     end
191     context 'zを検証するとき' do
192       before do
193         @panel = Factory.build :panel, :author_id => @author.id, :comic_id => @comic.id
194       end
195       it 'テストデータの確認' do
196         @panel.z = '1'
197         @panel.should be_valid
198       end
199       it '数値でなければ失敗する' do
200         @panel.z = 'a'
201         @panel.should_not be_valid
202       end
203       it '0なら失敗する' do
204         @panel.z = '0'
205         @panel.should_not be_valid
206       end
207       it '負なら失敗する' do
208         @panel.z = -1
209         @panel.should_not be_valid
210       end
211     end
212     context 'tを検証するとき' do
213       before do
214         @panel = Factory.build :panel, :author_id => @author.id, :comic_id => @comic.id
215       end
216       it 'テストデータの確認' do
217         @panel.t = '1'
218         @panel.should be_valid
219       end
220       it '数値でなければ失敗する' do
221         @panel.t = 'a'
222         @panel.should_not be_valid
223       end
224       it '0なら通る' do
225         @panel.t = '0'
226         @panel.should be_valid
227       end
228       it '負でも失敗する' do
229         @panel.t = -1
230         @panel.should_not be_valid
231       end
232     end
233     context 'author_idを検証するとき' do
234       before do
235         @panel = Factory.build :panel, :author_id => @author.id, :comic_id => @comic.id
236       end
237       it 'テストデータの確認' do
238         @panel.author_id = @author.id
239         @panel.should be_valid
240       end
241       it 'nullなら失敗する' do
242         @panel.author_id = nil
243         @panel.should_not be_valid
244       end
245       it '数値でなければ失敗する' do
246         @panel.author_id = 'a'
247         @panel.should_not be_valid
248       end
249       it '存在する絵師でなければ失敗する' do
250         @panel.author_id = 0
251         @panel.should_not be_valid
252       end
253     end
254     context '全体を検証するとき' do
255       before do
256         @panel = Factory :panel, :author_id => @author.id, :comic_id => @comic.id
257       end
258     end
259   end
260   
261   describe 'データ補充に於いて' do
262     before do\r
263       @comic = Factory :comic, :author_id => @author.id
264       @panel = Factory.build :panel, :comic_id => @comic.id
265     end
266     context 'widthを補充' do
267       it '空の時はコミックから補充する' do\r
268         @panel.width = nil
269         @panel.supply_default @author\r
270         @panel.width.should eq @comic.width
271       end
272       it '空の時でもコミックが不在なら補充しない' do\r
273         @panel.width = nil
274         @panel.comic_id = nil
275         @panel.supply_default @author\r
276         @panel.width.should be_nil
277       end
278       it '空でない時は変化しない' do
279         @panel.width = 45\r
280         lambda {
281           @panel.supply_default @author\r
282         }.should_not change @panel, :width
283       end
284     end
285     context 'heightを補充' do
286       it '空の時はコミックから補充する' do
287         @panel.height = nil
288         @panel.supply_default @author\r
289         @panel.height.should eq @comic.height
290       end
291       it '空の時でもコミックが不在なら補充しない' do\r
292         @panel.height = nil
293         @panel.comic_id = nil
294         @panel.supply_default @author\r
295         @panel.height.should be_nil
296       end
297       it '空でない時は変化しない' do
298         @panel.height = 87\r
299         lambda {
300           @panel.supply_default @author\r
301         }.should_not change @panel, :height
302       end
303     end
304     context 'borderを補充' do
305       it '空の時は0を補充する' do
306         @panel.border = nil
307         @panel.supply_default @author\r
308         @panel.border.should eq 0
309       end
310       it '空でない時は変化しない' do
311         @panel.border = 1\r
312         lambda {
313           @panel.supply_default @author\r
314         }.should_not change @panel, :border
315       end
316     end
317     context 'tを補充' do
318       it '空の時はコミック内のtの最大値+1を補充する' do
319         pl = Factory :panel, :author_id => @author.id, :comic_id => @comic.id, :t => 0\r
320         @panel.t = nil
321         @panel.supply_default @author\r
322         @panel.t.should eq 1
323       end
324       it '空でコミック初のコマなら0を補充する' do
325         @panel.t = nil
326         @panel.supply_default @author\r
327         @panel.t.should eq 0
328       end
329       it '空の時でも更新ケースなら補充しない' do
330         pl = Factory :panel, :author_id => @author.id, :comic_id => @comic.id, :t => 1\r
331         pl.t = nil\r
332         lambda {
333           pl.supply_default @author\r
334         }.should_not change pl, :t
335       end
336       it '空でない時は変化しない' do
337         @panel.t = 1\r
338         lambda {
339           @panel.supply_default @author\r
340         }.should_not change @panel, :t
341       end
342     end
343     context 'author_idを補充' do
344       it 'ログイン中の作家idを補充する' do
345         @panel.author_id = nil
346         @panel.supply_default @author\r
347         @panel.author_id.should eq @author.id
348       end
349     end
350     
351   end
352   \r
353   describe '作者判定に於いて' do
354     before do
355       @comic = Factory :comic, :author_id => @author.id
356     end
357     it '自作のコマならyes' do
358       panel = Factory :panel, :author_id => @author.id, :comic_id => @comic.id
359       panel.own?(@author).should == true
360     end
361     it '他人のコマならno' do
362       panel = Factory :panel, :author_id => @other_author.id, :comic_id => @comic.id
363       panel.own?(@author).should == false
364     end
365     it '作家が不明ならno' do
366       panel = Factory :panel, :author_id => @author.id, :comic_id => @comic.id
367       panel.own?(nil).should == false
368     end
369   end
370   describe '閲覧許可に於いて' do
371     before do
372       @comic = Factory :comic, :author_id => @author.id
373     end
374     it '自作の公開コミックのコマを見るときは許可する' do
375       Comic.any_instance.stub(:visible?).and_return(true)
376       panel = Factory :panel, :author_id => @author.id, :comic_id => @comic.id
377       panel.visible?(@author).should == true
378     end
379     it '自作のコマは非公開コミックでも許可する' do
380       Comic.any_instance.stub(:visible?).and_return(false)
381       panel = Factory :panel, :author_id => @author.id, :comic_id => @comic.id
382       panel.visible?(@author).should == true
383     end
384     it '他人のコマでも公開コミックなら許可する' do
385       Comic.any_instance.stub(:visible?).and_return(true)
386       panel = Factory :panel, :author_id => @other_author.id, :comic_id => @comic.id
387       panel.visible?(@author).should == true
388     end
389     it '他人のコマで非公開コミックなら許可しない' do
390       Comic.any_instance.stub(:visible?).and_return(false)
391       panel = Factory :panel, :author_id => @other_author.id, :comic_id => @comic.id
392       panel.visible?(@author).should == false
393     end
394   end
395   describe '単体取得に於いて' do
396     before do
397       @comic = Factory :comic, :author_id => @author.id
398       @panel = Factory :panel, :comic_id => @comic.id, :author_id => @author.id
399     end
400     it '指定のコマを返す' do
401       Comic.any_instance.stub(:visible?).and_return(true)
402       pl = Panel.show @panel.id, @author
403       pl.should eq @panel
404     end
405     context '他人の非公開コミックのコマを開こうとしたとき' do
406       it '403Forbidden例外を返す' do
407         Panel.any_instance.stub(:visible?).and_return(false)
408         lambda{
409           Panel.show @panel.id, @author
410         }.should raise_error(ActiveRecord::Forbidden)
411       end
412     end
413     context '存在しないコマを開こうとしたとき' do
414       it '404RecordNotFound例外を返す' do
415         lambda{
416           Panel.show 110, @author
417         }.should raise_error(ActiveRecord::RecordNotFound)
418       end
419     end
420   end
421   describe '関連テーブルプションに於いて' do
422     context 'オプションがないとき' do
423       it '4つの項目を含んでいる' do
424         r = Panel.show_include_opt
425         r.should have(4).items\r
426       end
427       it 'コミックを含んでいる' do
428         r = Panel.show_include_opt
429         r.has_key?(:comic).should be_true\r
430       end
431       it 'コマ絵を含んでいる' do
432         r = Panel.show_include_opt
433         r.has_key?(:panel_pictures).should be_true\r
434       end
435         it 'コマ絵は素材を含んでいる' do
436           r = Panel.show_include_opt
437           r[:panel_pictures].has_key?(:resource_picture).should be_true\r
438         end
439           it '素材は絵師を含んでいる' do
440             r = Panel.show_include_opt
441             r[:panel_pictures][:resource_picture].has_key?(:artist).should be_true\r
442           end
443           it '素材はライセンスを含んでいる' do
444             r = Panel.show_include_opt
445             r[:panel_pictures][:resource_picture].has_key?(:license).should be_true\r
446           end
447       it 'フキダシを含んでいる' do
448         r = Panel.show_include_opt
449         r.has_key?(:balloons).should be_true\r
450       end
451         it 'フキダシはセリフを含んでいる' do
452           r = Panel.show_include_opt
453           r[:balloons].has_key?(:speeches).should be_true\r
454         end
455       it '作家を含んでいる' do
456         r = Panel.show_include_opt
457         r.has_key?(:author).should be_true\r
458       end
459     end
460     context 'オプションで原画を含ませたとき' do
461       it '5つの項目を含んでいる' do
462         r = Panel.show_include_opt(:include => {:test => {}})
463         r.should have(5).items\r
464       end
465       it 'testを含んでいる' do
466         r = Panel.show_include_opt(:include => {:test => {}})
467         r.has_key?(:test).should be_true\r
468       end
469     end
470   end
471   describe 'json単体出力オプションに於いて' do
472     it 'includeキーを含んでいる' do
473       r = Panel.show_json_include_opt
474       r.has_key?(:include).should be_true
475     end
476     it '4つの項目を含んでいる' do
477       r = Panel.show_json_include_opt[:include]
478       r.should have(4).items\r
479     end
480     it 'コミックを含んでいる' do
481       r = Panel.show_json_include_opt[:include]
482       r.has_key?(:comic).should be_true\r
483     end
484     it 'コマ絵を含んでいる' do
485       r = Panel.show_json_include_opt[:include]
486       r.has_key?(:panel_pictures).should be_true\r
487     end
488       it 'コマ絵は素材を含んでいる' do
489         r = Panel.show_json_include_opt[:include]
490         r[:panel_pictures].has_key?(:resource_picture).should be_true\r
491       end
492         it '素材は絵師を含んでいる' do
493           r = Panel.show_json_include_opt[:include]
494           r[:panel_pictures][:resource_picture].has_key?(:artist).should be_true\r
495         end
496         it '素材はライセンスを含んでいる' do
497           r = Panel.show_json_include_opt[:include]
498           r[:panel_pictures][:resource_picture].has_key?(:license).should be_true\r
499         end
500     it 'フキダシを含んでいる' do
501       r = Panel.show_json_include_opt[:include]
502       r.has_key?(:balloons).should be_true\r
503     end
504       it 'フキダシはセリフを含んでいる' do
505         r = Panel.show_json_include_opt[:include]
506         r[:balloons].has_key?(:speeches).should be_true\r
507       end
508     it '作家を含んでいる' do
509       r = Panel.show_json_include_opt[:include]
510       r.has_key?(:author).should be_true\r
511     end
512   end
513   describe '一覧取得に於いて' do
514     before do
515       @comic = Factory :comic, :author_id => @author.id
516       @panel = Factory :panel, :comic_id => @comic.id, :author_id => @author.id
517     end
518     context 'page補正について' do
519       it '文字列から数値に変換される' do
520         Panel.page('8').should eq 8
521       end
522       it 'nilの場合は1になる' do
523         Panel.page().should eq 1
524       end
525       it '0以下の場合は1になる' do
526         Panel.page('0').should eq 1
527       end
528     end
529     context 'page_size補正について' do
530       it '文字列から数値に変換される' do
531         Panel.page_size('7').should eq 7
532       end
533       it 'nilの場合はPanel.default_page_sizeになる' do
534         Panel.page_size().should eq Panel.default_page_size
535       end
536       it '0以下の場合はPanel.default_page_sizeになる' do
537         Panel.page_size('0').should eq Panel.default_page_size
538       end
539       it 'Panel.max_page_sizeを超えた場合はPanel.max_page_sizeになる' do
540         Panel.page_size('1000').should eq Panel.max_page_size
541       end
542     end
543     it 'リストを返す' do
544       pl = Panel.list
545       pl.should eq [@panel]
546     end
547     it '時系列で並んでいる' do
548       npl = Factory :panel, :comic_id => @comic.id, :author_id => @author.id, :t => 1
549       pl = Panel.list
550       pl.should eq [npl, @panel]
551     end
552     context 'DBに5件あって1ページの件数を2件に変えたとして' do
553       before do
554         @npl2 = Factory :panel, :comic_id => @comic.id, :author_id => @author.id, :t => 1
555         @npl3 = Factory :panel, :comic_id => @comic.id, :author_id => @author.id, :t => 2
556         @npl4 = Factory :panel, :comic_id => @comic.id, :author_id => @author.id, :t => 3
557         @npl5 = Factory :panel, :comic_id => @comic.id, :author_id => @author.id, :t => 4
558         Panel.stub(:default_page_size).and_return(2)
559       end
560       it '通常は2件を返す' do
561         pl = Panel.list
562         pl.should have(2).items 
563       end
564       it 'page=1なら末尾2件を返す' do
565         #時系列で並んでいる
566         pl = Panel.list( {}, 1)
567         pl.should eq [@npl5, @npl4]
568       end
569       it 'page=2なら中間2件を返す' do
570         pl = Panel.list({}, 2)
571         pl.should eq [@npl3, @npl2]
572       end
573       it 'page=3なら先頭1件を返す' do
574         pl = Panel.list({}, 3)
575         pl.should eq [@panel]
576       end
577     end
578   end
579   describe 'list関連テーブルプションに於いて' do
580     it 'includeキーを含んでいる' do
581       r = Panel.list_opt
582       r.has_key?(:include).should be_true
583     end
584     it '4つの項目を含んでいる' do
585       r = Panel.list_opt[:include]
586       r.should have(4).items\r
587     end
588     it 'コミックを含んでいる' do
589       r = Panel.list_opt[:include]
590       r.has_key?(:comic).should be_true\r
591     end
592     it 'コマ絵を含んでいる' do
593       r = Panel.list_opt[:include]
594       r.has_key?(:panel_pictures).should be_true\r
595     end
596       it 'コマ絵は素材を含んでいる' do
597         r = Panel.list_opt[:include]
598         r[:panel_pictures].has_key?(:resource_picture).should be_true\r
599       end
600         it '素材は絵師を含んでいる' do
601           r = Panel.list_opt[:include]
602           r[:panel_pictures][:resource_picture].has_key?(:artist).should be_true\r
603         end
604         it '素材はライセンスを含んでいる' do
605           r = Panel.list_opt[:include]
606           r[:panel_pictures][:resource_picture].has_key?(:license).should be_true\r
607         end
608     it 'フキダシを含んでいる' do
609       r = Panel.list_opt[:include]
610       r.has_key?(:balloons).should be_true\r
611     end
612       it 'フキダシはセリフを含んでいる' do
613         r = Panel.list_opt[:include]
614         r[:balloons].has_key?(:speeches).should be_true\r
615       end
616     it '作家を含んでいる' do
617       r = Panel.list_opt[:include]
618       r.has_key?(:author).should be_true\r
619     end
620   end
621   describe 'json一覧出力オプションに於いて' do
622     it 'includeキーを含んでいる' do
623       r = Panel.list_json_opt
624       r.has_key?(:include).should be_true
625     end
626     it '4つの項目を含んでいる' do
627       r = Panel.list_json_opt[:include]
628       r.should have(4).items\r
629     end
630     it 'コミックを含んでいる' do
631       r = Panel.list_json_opt[:include]
632       r.has_key?(:comic).should be_true\r
633     end
634     it 'コマ絵を含んでいる' do
635       r = Panel.list_json_opt[:include]
636       r.has_key?(:panel_pictures).should be_true\r
637     end
638       it 'コマ絵は素材を含んでいる' do
639         r = Panel.list_json_opt[:include]
640         r[:panel_pictures].has_key?(:resource_picture).should be_true\r
641       end
642         it '素材は絵師を含んでいる' do
643           r = Panel.list_json_opt[:include]
644           r[:panel_pictures][:resource_picture].has_key?(:artist).should be_true\r
645         end
646         it '素材はライセンスを含んでいる' do
647           r = Panel.list_json_opt[:include]
648           r[:panel_pictures][:resource_picture].has_key?(:license).should be_true\r
649         end
650     it 'フキダシを含んでいる' do
651       r = Panel.list_json_opt[:include]
652       r.has_key?(:balloons).should be_true\r
653     end
654       it 'フキダシはセリフを含んでいる' do
655         r = Panel.list_json_opt[:include]
656         r[:balloons].has_key?(:speeches).should be_true\r
657       end
658     it '作家を含んでいる' do
659       r = Panel.list_json_opt[:include]
660       r.has_key?(:author).should be_true\r
661     end
662   end\r
663   
664 end