OSDN Git Service

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