OSDN Git Service

pass test
[pettanr/pettanr.git] / spec / models / panel_spec.rb
1 # -*- encoding: utf-8 -*-\r
2 require 'spec_helper'\r
3 #コマ\r
4 describe Panel do\r
5   before do\r
6     Factory :admin\r
7     @license = Factory :license\r
8     @user = Factory( :user_yas)\r
9     @author = @user.author\r
10     @artist = Factory :artist_yas, :author_id => @author.id\r
11     @other_user = Factory( :user_yas)\r
12     @other_author = @other_user.author\r
13     @other_artist = Factory :artist_yas, :author_id => @other_author.id\r
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\r
16     @sbt = Factory :speech_balloon_template\r
17   end\r
18   \r
19   describe '検証に於いて' do\r
20     before do\r
21       @comic = Factory :comic, :author_id => @author.id\r
22     end\r
23     \r
24     it 'オーソドックスなデータなら通る' do\r
25       @panel = Factory.build :panel, :author_id => @author.id, :comic_id => @comic.id\r
26       @panel.should be_valid\r
27     end\r
28     \r
29     context 'comic_idを検証するとき' do\r
30       before do\r
31         @panel = Factory.build :panel, :author_id => @author.id, :comic_id => nil\r
32       end\r
33       it 'テストデータの確認' do\r
34         @panel.comic_id = @comic.id\r
35         @panel.should be_valid\r
36       end\r
37     end\r
38     context 'widthを検証するとき' do\r
39       before do\r
40         @panel = Factory.build :panel, :author_id => @author.id, :comic_id => @comic.id\r
41       end\r
42       it 'テストデータの確認' do\r
43         @panel.width = 1\r
44         @panel.should be_valid\r
45       end\r
46       it 'nullなら失敗する' do\r
47         @panel.width = nil\r
48         @panel.should_not be_valid\r
49       end\r
50       it '数値でなければ失敗する' do\r
51         @panel.width = 'a'\r
52         @panel.should_not be_valid\r
53       end\r
54       it '0なら失敗する' do\r
55         @panel.width = '0'\r
56         @panel.should_not be_valid\r
57       end\r
58       it '負でも失敗する' do\r
59         @panel.width = -1\r
60         @panel.should_not be_valid\r
61       end\r
62     end\r
63     context 'heightを検証するとき' do\r
64       before do\r
65         @panel = Factory.build :panel, :author_id => @author.id, :comic_id => @comic.id\r
66       end\r
67       it 'テストデータの確認' do\r
68         @panel.height = '1'\r
69         @panel.should be_valid\r
70       end\r
71       it 'nullなら失敗する' do\r
72         @panel.height = nil\r
73         @panel.should_not be_valid\r
74       end\r
75       it '数値でなければ失敗する' do\r
76         @panel.height = 'a'\r
77         @panel.should_not be_valid\r
78       end\r
79       it '0なら失敗する' do\r
80         @panel.height = '0'\r
81         @panel.should_not be_valid\r
82       end\r
83       it '負でも失敗する' do\r
84         @panel.height = -1\r
85         @panel.should_not be_valid\r
86       end\r
87     end\r
88     context 'borderを検証するとき' do\r
89       before do\r
90         @panel = Factory.build :panel, :author_id => @author.id, :comic_id => @comic.id\r
91       end\r
92       it 'テストデータの確認' do\r
93         @panel.border = '1'\r
94         @panel.should be_valid\r
95       end\r
96       it 'nullなら失敗する' do\r
97         @panel.border = nil\r
98         @panel.should_not be_valid\r
99       end\r
100       it '数値でなければ失敗する' do\r
101         @panel.border = 'a'\r
102         @panel.should_not be_valid\r
103       end\r
104       it '負なら失敗する' do\r
105         @panel.border = '-1'\r
106         @panel.should_not be_valid\r
107       end\r
108       it '0なら通る' do\r
109         @panel.border = 0\r
110         @panel.should be_valid\r
111       end\r
112     end\r
113     context 'xを検証するとき' do\r
114       before do\r
115         @panel = Factory.build :panel, :author_id => @author.id, :comic_id => @comic.id\r
116       end\r
117       it 'テストデータの確認' do\r
118         @panel.x = '1'\r
119         @panel.should be_valid\r
120       end\r
121       it '数値でなければ失敗する' do\r
122         @panel.x = 'a'\r
123         @panel.should_not be_valid\r
124       end\r
125       it '0なら通る' do\r
126         @panel.x = '0'\r
127         @panel.should be_valid\r
128       end\r
129       it '負でも通る' do\r
130         @panel.x = -1\r
131         @panel.should be_valid\r
132       end\r
133     end\r
134     context 'yを検証するとき' do\r
135       before do\r
136         @panel = Factory.build :panel, :author_id => @author.id, :comic_id => @comic.id\r
137       end\r
138       it 'テストデータの確認' do\r
139         @panel.y = '1'\r
140         @panel.should be_valid\r
141       end\r
142       it '数値でなければ失敗する' do\r
143         @panel.y = 'a'\r
144         @panel.should_not be_valid\r
145       end\r
146       it '0なら通る' do\r
147         @panel.y = '0'\r
148         @panel.should be_valid\r
149       end\r
150       it '負でも通る' do\r
151         @panel.y = -1\r
152         @panel.should be_valid\r
153       end\r
154     end\r
155     context 'zを検証するとき' do\r
156       before do\r
157         @panel = Factory.build :panel, :author_id => @author.id, :comic_id => @comic.id\r
158       end\r
159       it 'テストデータの確認' do\r
160         @panel.z = '1'\r
161         @panel.should be_valid\r
162       end\r
163       it '数値でなければ失敗する' do\r
164         @panel.z = 'a'\r
165         @panel.should_not be_valid\r
166       end\r
167       it '0なら失敗する' do\r
168         @panel.z = '0'\r
169         @panel.should_not be_valid\r
170       end\r
171       it '負なら失敗する' do\r
172         @panel.z = -1\r
173         @panel.should_not be_valid\r
174       end\r
175     end\r
176     context 'tを検証するとき' do\r
177       before do\r
178         @panel = Factory.build :panel, :author_id => @author.id, :comic_id => @comic.id\r
179       end\r
180       it 'テストデータの確認' do\r
181         @panel.t = '1'\r
182         @panel.should be_valid\r
183       end\r
184       it '数値でなければ失敗する' do\r
185         @panel.t = 'a'\r
186         @panel.should_not be_valid\r
187       end\r
188       it '0なら通る' do\r
189         @panel.t = '0'\r
190         @panel.should be_valid\r
191       end\r
192       it '負でも失敗する' do\r
193         @panel.t = -1\r
194         @panel.should_not be_valid\r
195       end\r
196     end\r
197     context 'author_idを検証するとき' do\r
198       before do\r
199         @panel = Factory.build :panel, :author_id => @author.id, :comic_id => @comic.id\r
200       end\r
201       it 'テストデータの確認' do\r
202         @panel.author_id = @author.id\r
203         @panel.should be_valid\r
204       end\r
205       it 'nullなら失敗する' do\r
206         @panel.author_id = nil\r
207         @panel.should_not be_valid\r
208       end\r
209       it '数値でなければ失敗する' do\r
210         @panel.author_id = 'a'\r
211         @panel.should_not be_valid\r
212       end\r
213       it '存在する絵師でなければ失敗する' do\r
214         @panel.author_id = 0\r
215         @panel.should_not be_valid\r
216       end\r
217     end\r
218     context '全体を検証するとき' do\r
219       before do\r
220         @panel = Factory :panel, :author_id => @author.id, :comic_id => @comic.id\r
221       end\r
222     end\r
223   end\r
224   \r
225   describe 'データ補充に於いて' do\r
226     before do\r
227       @comic = Factory :comic, :author_id => @author.id\r
228       @panel = Factory.build :panel, :comic_id => @comic.id\r
229     end\r
230     context 'widthを補充' do\r
231       it '空の時はコミックから補充する' do\r
232         @panel.width = nil\r
233         @panel.supply_default @author\r
234         @panel.width.should eq @comic.width\r
235       end\r
236       it '空の時でもコミックが不在なら補充しない' do\r
237         @panel.width = nil\r
238         @panel.comic_id = nil\r
239         @panel.supply_default @author\r
240         @panel.width.should be_nil\r
241       end\r
242       it '空でない時は変化しない' do\r
243         @panel.width = 45\r
244         lambda {\r
245           @panel.supply_default @author\r
246         }.should_not change @panel, :width\r
247       end\r
248     end\r
249     context 'heightを補充' do\r
250       it '空の時はコミックから補充する' do\r
251         @panel.height = nil\r
252         @panel.supply_default @author\r
253         @panel.height.should eq @comic.height\r
254       end\r
255       it '空の時でもコミックが不在なら補充しない' do\r
256         @panel.height = nil\r
257         @panel.comic_id = nil\r
258         @panel.supply_default @author\r
259         @panel.height.should be_nil\r
260       end\r
261       it '空でない時は変化しない' do\r
262         @panel.height = 87\r
263         lambda {\r
264           @panel.supply_default @author\r
265         }.should_not change @panel, :height\r
266       end\r
267     end\r
268     context 'borderを補充' do\r
269       it '空の時は0を補充する' do\r
270         @panel.border = nil\r
271         @panel.supply_default @author\r
272         @panel.border.should eq 0\r
273       end\r
274       it '空でない時は変化しない' do\r
275         @panel.border = 1\r
276         lambda {\r
277           @panel.supply_default @author\r
278         }.should_not change @panel, :border\r
279       end\r
280     end\r
281     context 'tを補充' do\r
282       it '空の時はコミック内のtの最大値+1を補充する' do\r
283         pl = Factory :panel, :author_id => @author.id, :comic_id => @comic.id, :t => 0\r
284         @panel.t = nil\r
285         @panel.supply_default @author\r
286         @panel.t.should eq 1\r
287       end\r
288       it '空でコミック初のコマなら0を補充する' do\r
289         @panel.t = nil\r
290         @panel.supply_default @author\r
291         @panel.t.should eq 0\r
292       end\r
293       it '空の時でも更新ケースなら補充しない' do\r
294         pl = Factory :panel, :author_id => @author.id, :comic_id => @comic.id, :t => 1\r
295         pl.t = nil\r
296         lambda {\r
297           pl.supply_default @author\r
298         }.should_not change pl, :t\r
299       end\r
300       it '空でない時は変化しない' do\r
301         @panel.t = 1\r
302         lambda {\r
303           @panel.supply_default @author\r
304         }.should_not change @panel, :t\r
305       end\r
306     end\r
307     context 'author_idを補充' do\r
308       it 'ログイン中の作家idを補充する' do\r
309         @panel.author_id = nil\r
310         @panel.supply_default @author\r
311         @panel.author_id.should eq @author.id\r
312       end\r
313     end\r
314     \r
315   end\r
316   \r
317   describe '作者判定に於いて' do\r
318     before do\r
319       @comic = Factory :comic, :author_id => @author.id\r
320     end\r
321     it '自作のコマならyes' do\r
322       panel = Factory :panel, :author_id => @author.id, :comic_id => @comic.id\r
323       panel.own?(@author).should == true\r
324     end\r
325     it '他人のコマならno' do\r
326       panel = Factory :panel, :author_id => @other_author.id, :comic_id => @comic.id\r
327       panel.own?(@author).should == false\r
328     end\r
329     it '作家が不明ならno' do\r
330       panel = Factory :panel, :author_id => @author.id, :comic_id => @comic.id\r
331       panel.own?(nil).should == false\r
332     end\r
333   end\r
334   describe '閲覧許可に於いて' do\r
335     before do\r
336       @comic = Factory :comic, :author_id => @author.id\r
337     end\r
338     it '自作の公開コミックのコマを見るときは許可する' do\r
339       Comic.any_instance.stub(:visible?).and_return(true)\r
340       panel = Factory :panel, :author_id => @author.id, :comic_id => @comic.id\r
341       panel.visible?(@author).should == true\r
342     end\r
343     it '自作のコマは非公開コミックでも許可する' do\r
344       Comic.any_instance.stub(:visible?).and_return(false)\r
345       panel = Factory :panel, :author_id => @author.id, :comic_id => @comic.id\r
346       panel.visible?(@author).should == true\r
347     end\r
348     it '他人のコマでも公開コミックなら許可する' do\r
349       Comic.any_instance.stub(:visible?).and_return(true)\r
350       panel = Factory :panel, :author_id => @other_author.id, :comic_id => @comic.id\r
351       panel.visible?(@author).should == true\r
352     end\r
353     it '他人のコマで非公開コミックなら許可しない' do\r
354       Comic.any_instance.stub(:visible?).and_return(false)\r
355       panel = Factory :panel, :author_id => @other_author.id, :comic_id => @comic.id\r
356       panel.visible?(@author).should == false\r
357     end\r
358   end\r
359   describe '単体取得に於いて' do\r
360     before do\r
361       @comic = Factory :comic, :author_id => @author.id\r
362       @panel = Factory :panel, :comic_id => @comic.id, :author_id => @author.id\r
363     end\r
364     it '指定のコマを返す' do\r
365       Comic.any_instance.stub(:visible?).and_return(true)\r
366       pl = Panel.show @panel.id, @author\r
367       pl.should eq @panel\r
368     end\r
369     context '他人の非公開コミックのコマを開こうとしたとき' do\r
370       it '403Forbidden例外を返す' do\r
371         Panel.any_instance.stub(:visible?).and_return(false)\r
372         lambda{\r
373           Panel.show @panel.id, @author\r
374         }.should raise_error(ActiveRecord::Forbidden)\r
375       end\r
376     end\r
377     context '存在しないコマを開こうとしたとき' do\r
378       it '404RecordNotFound例外を返す' do\r
379         lambda{\r
380           Panel.show 110, @author\r
381         }.should raise_error(ActiveRecord::RecordNotFound)\r
382       end\r
383     end\r
384   end\r
385   describe '関連テーブルプションに於いて' do\r
386     context 'オプションがないとき' do\r
387       it '4つの項目を含んでいる' do\r
388         r = Panel.show_include_opt\r
389         r.should have(4).items\r
390       end\r
391       it 'コミックを含んでいる' do\r
392         r = Panel.show_include_opt\r
393         r.has_key?(:comic).should be_true\r
394       end\r
395       it 'コマ絵を含んでいる' do\r
396         r = Panel.show_include_opt\r
397         r.has_key?(:panel_pictures).should be_true\r
398       end\r
399         it 'コマ絵は素材を含んでいる' do\r
400           r = Panel.show_include_opt\r
401           r[:panel_pictures].has_key?(:resource_picture).should be_true\r
402         end\r
403           it '素材は絵師を含んでいる' do\r
404             r = Panel.show_include_opt\r
405             r[:panel_pictures][:resource_picture].has_key?(:artist).should be_true\r
406           end\r
407           it '素材はライセンスを含んでいる' do\r
408             r = Panel.show_include_opt\r
409             r[:panel_pictures][:resource_picture].has_key?(:license).should be_true\r
410           end\r
411       it 'フキダシを含んでいる' do\r
412         r = Panel.show_include_opt\r
413         r.has_key?(:speech_balloons).should be_true\r
414       end\r
415         it 'フキダシはフキダシ枠を含んでいる' do\r
416           r = Panel.show_include_opt\r
417           r[:speech_balloons].has_key?(:balloons).should be_true\r
418         end\r
419         it 'フキダシはセリフを含んでいる' do\r
420           r = Panel.show_include_opt\r
421           r[:speech_balloons].has_key?(:speeches).should be_true\r
422         end\r
423       it '作家を含んでいる' do\r
424         r = Panel.show_include_opt\r
425         r.has_key?(:author).should be_true\r
426       end\r
427     end\r
428     context 'オプションで原画を含ませたとき' do\r
429       it '5つの項目を含んでいる' do\r
430         r = Panel.show_include_opt(:include => {:test => {}})\r
431         r.should have(5).items\r
432       end\r
433       it 'testを含んでいる' do\r
434         r = Panel.show_include_opt(:include => {:test => {}})\r
435         r.has_key?(:test).should be_true\r
436       end\r
437     end\r
438   end\r
439   describe 'json単体出力オプションに於いて' do\r
440     it 'includeキーを含んでいる' do\r
441       r = Panel.show_json_include_opt\r
442       r.has_key?(:include).should be_true\r
443     end\r
444     it '4つの項目を含んでいる' do\r
445       r = Panel.show_json_include_opt[:include]\r
446       r.should have(4).items\r
447     end\r
448     it 'コミックを含んでいる' do\r
449       r = Panel.show_json_include_opt[:include]\r
450       r.has_key?(:comic).should be_true\r
451     end\r
452     it 'コマ絵を含んでいる' do\r
453       r = Panel.show_json_include_opt[:include]\r
454       r.has_key?(:panel_pictures).should be_true\r
455     end\r
456       it 'コマ絵は素材を含んでいる' do\r
457         r = Panel.show_json_include_opt[:include]\r
458         r[:panel_pictures].has_key?(:resource_picture).should be_true\r
459       end\r
460         it '素材は絵師を含んでいる' do\r
461           r = Panel.show_json_include_opt[:include]\r
462           r[:panel_pictures][:resource_picture].has_key?(:artist).should be_true\r
463         end\r
464         it '素材はライセンスを含んでいる' do\r
465           r = Panel.show_json_include_opt[:include]\r
466           r[:panel_pictures][:resource_picture].has_key?(:license).should be_true\r
467         end\r
468     it 'フキダシを含んでいる' do\r
469       r = Panel.show_include_opt\r
470       r.has_key?(:speech_balloons).should be_true\r
471     end\r
472       it 'フキダシはフキダシ枠を含んでいる' do\r
473         r = Panel.show_include_opt\r
474         r[:speech_balloons].has_key?(:balloons).should be_true\r
475       end\r
476       it 'フキダシはセリフを含んでいる' do\r
477         r = Panel.show_include_opt\r
478         r[:speech_balloons].has_key?(:speeches).should be_true\r
479       end\r
480     it '作家を含んでいる' do\r
481       r = Panel.show_json_include_opt[:include]\r
482       r.has_key?(:author).should be_true\r
483     end\r
484   end\r
485   describe '一覧取得に於いて' do\r
486     before do\r
487       @comic = Factory :comic, :author_id => @author.id\r
488       @panel = Factory :panel, :comic_id => @comic.id, :author_id => @author.id\r
489     end\r
490     context 'page補正について' do\r
491       it '文字列から数値に変換される' do\r
492         Panel.page('8').should eq 8\r
493       end\r
494       it 'nilの場合は1になる' do\r
495         Panel.page().should eq 1\r
496       end\r
497       it '0以下の場合は1になる' do\r
498         Panel.page('0').should eq 1\r
499       end\r
500     end\r
501     context 'page_size補正について' do\r
502       it '文字列から数値に変換される' do\r
503         Panel.page_size('7').should eq 7\r
504       end\r
505       it 'nilの場合はPanel.default_page_sizeになる' do\r
506         Panel.page_size().should eq Panel.default_page_size\r
507       end\r
508       it '0以下の場合はPanel.default_page_sizeになる' do\r
509         Panel.page_size('0').should eq Panel.default_page_size\r
510       end\r
511       it 'Panel.max_page_sizeを超えた場合はPanel.max_page_sizeになる' do\r
512         Panel.page_size('1000').should eq Panel.max_page_size\r
513       end\r
514     end\r
515     it 'リストを返す' do\r
516       pl = Panel.list\r
517       pl.should eq [@panel]\r
518     end\r
519     it '時系列で並んでいる' do\r
520       npl = Factory :panel, :comic_id => @comic.id, :author_id => @author.id, :t => 1\r
521       pl = Panel.list\r
522       pl.should eq [npl, @panel]\r
523     end\r
524     context 'DBに5件あって1ページの件数を2件に変えたとして' do\r
525       before do\r
526         @npl2 = Factory :panel, :comic_id => @comic.id, :author_id => @author.id, :t => 1\r
527         @npl3 = Factory :panel, :comic_id => @comic.id, :author_id => @author.id, :t => 2\r
528         @npl4 = Factory :panel, :comic_id => @comic.id, :author_id => @author.id, :t => 3\r
529         @npl5 = Factory :panel, :comic_id => @comic.id, :author_id => @author.id, :t => 4\r
530         Panel.stub(:default_page_size).and_return(2)\r
531       end\r
532       it '通常は2件を返す' do\r
533         pl = Panel.list\r
534         pl.should have(2).items \r
535       end\r
536       it 'page=1なら末尾2件を返す' do\r
537         #時系列で並んでいる\r
538         pl = Panel.list( {}, 1)\r
539         pl.should eq [@npl5, @npl4]\r
540       end\r
541       it 'page=2なら中間2件を返す' do\r
542         pl = Panel.list({}, 2)\r
543         pl.should eq [@npl3, @npl2]\r
544       end\r
545       it 'page=3なら先頭1件を返す' do\r
546         pl = Panel.list({}, 3)\r
547         pl.should eq [@panel]\r
548       end\r
549     end\r
550   end\r
551   describe 'list関連テーブルプションに於いて' do\r
552     it 'includeキーを含んでいる' do\r
553       r = Panel.list_opt\r
554       r.has_key?(:include).should be_true\r
555     end\r
556     it '4つの項目を含んでいる' do\r
557       r = Panel.list_opt[:include]\r
558       r.should have(4).items\r
559     end\r
560     it 'コミックを含んでいる' do\r
561       r = Panel.list_opt[:include]\r
562       r.has_key?(:comic).should be_true\r
563     end\r
564     it 'コマ絵を含んでいる' do\r
565       r = Panel.list_opt[:include]\r
566       r.has_key?(:panel_pictures).should be_true\r
567     end\r
568       it 'コマ絵は素材を含んでいる' do\r
569         r = Panel.list_opt[:include]\r
570         r[:panel_pictures].has_key?(:resource_picture).should be_true\r
571       end\r
572         it '素材は絵師を含んでいる' do\r
573           r = Panel.list_opt[:include]\r
574           r[:panel_pictures][:resource_picture].has_key?(:artist).should be_true\r
575         end\r
576         it '素材はライセンスを含んでいる' do\r
577           r = Panel.list_opt[:include]\r
578           r[:panel_pictures][:resource_picture].has_key?(:license).should be_true\r
579         end\r
580     it 'フキダシを含んでいる' do\r
581       r = Panel.show_include_opt\r
582       r.has_key?(:speech_balloons).should be_true\r
583     end\r
584       it 'フキダシはフキダシ枠を含んでいる' do\r
585         r = Panel.show_include_opt\r
586         r[:speech_balloons].has_key?(:balloons).should be_true\r
587       end\r
588       it 'フキダシはセリフを含んでいる' do\r
589         r = Panel.show_include_opt\r
590         r[:speech_balloons].has_key?(:speeches).should be_true\r
591       end\r
592     it '作家を含んでいる' do\r
593       r = Panel.list_opt[:include]\r
594       r.has_key?(:author).should be_true\r
595     end\r
596   end\r
597   describe 'json一覧出力オプションに於いて' do\r
598     it 'includeキーを含んでいる' do\r
599       r = Panel.list_json_opt\r
600       r.has_key?(:include).should be_true\r
601     end\r
602     it '4つの項目を含んでいる' do\r
603       r = Panel.list_json_opt[:include]\r
604       r.should have(4).items\r
605     end\r
606     it 'コミックを含んでいる' do\r
607       r = Panel.list_json_opt[:include]\r
608       r.has_key?(:comic).should be_true\r
609     end\r
610     it 'コマ絵を含んでいる' do\r
611       r = Panel.list_json_opt[:include]\r
612       r.has_key?(:panel_pictures).should be_true\r
613     end\r
614       it 'コマ絵は素材を含んでいる' do\r
615         r = Panel.list_json_opt[:include]\r
616         r[:panel_pictures].has_key?(:resource_picture).should be_true\r
617       end\r
618         it '素材は絵師を含んでいる' do\r
619           r = Panel.list_json_opt[:include]\r
620           r[:panel_pictures][:resource_picture].has_key?(:artist).should be_true\r
621         end\r
622         it '素材はライセンスを含んでいる' do\r
623           r = Panel.list_json_opt[:include]\r
624           r[:panel_pictures][:resource_picture].has_key?(:license).should be_true\r
625         end\r
626     it 'フキダシを含んでいる' do\r
627       r = Panel.show_include_opt\r
628       r.has_key?(:speech_balloons).should be_true\r
629     end\r
630       it 'フキダシはフキダシ枠を含んでいる' do\r
631         r = Panel.show_include_opt\r
632         r[:speech_balloons].has_key?(:balloons).should be_true\r
633       end\r
634       it 'フキダシはセリフを含んでいる' do\r
635         r = Panel.show_include_opt\r
636         r[:speech_balloons].has_key?(:speeches).should be_true\r
637       end\r
638     it '作家を含んでいる' do\r
639       r = Panel.list_json_opt[:include]\r
640       r.has_key?(:author).should be_true\r
641     end\r
642   end\r
643   \r
644   #異常データ検出\r
645     #コマはコミックに従属しなくなったのでtで入れ替えるチェックは要らなくなった\r
646     #コマ絵とフキダシの整合チェックをする\r
647   describe 'id収集に於いて' do\r
648     context 'つつがなく終わるとき' do\r
649       it '第一パラメータで指定された配列中から第二引数のidを収集している' do\r
650         a = [[{:panel_id => 1, :a => 'a'}, {:panel_id => 2, :a => 'a'}], \r
651           [{:panel_id => 3, :a => 'a'}, {:panel_id => 4, :a => 'a'}]]\r
652         r = Panel.collect_element_value a, :panel_id\r
653         r.should eq [1, 2, 3, 4]\r
654       end\r
655     end\r
656     context 'Nil除外指示があるとき' do\r
657       it '収集したpanel_idのうちnilは除外している' do\r
658         a = [[{:panel_id => 1, :a => 'a'}, {:panel_id => 2, :a => 'a'}, {:panel_id => nil, :a => 'a'}], \r
659           [{:panel_id => 3, :a => 'a'}, {:panel_id => nil, :a => 'a'}, {:panel_id => 4, :a => 'a'}]]\r
660         r = Panel.collect_element_value a, :panel_id, true\r
661         r.should eq [1, 2, 3, 4]\r
662       end\r
663     end\r
664   end\r
665   describe 'id一致チェックに於いて' do\r
666     #parent.idがNilのときはすべてNil。数値のときは全一致\r
667     context '親が既存(idが数値)のとき' do\r
668       it 'すべてid一致するときTrueを返す' do\r
669         r = Panel.validate_id [1, 1, 1, 1], 1\r
670         r.should be_true\r
671       end\r
672       it 'idが一致する中にNilが混じってもTrueを返す' do\r
673         r = Panel.validate_id [1, 1, 1, 1, nil], 1\r
674         r.should be_true\r
675       end\r
676       it 'すべて数値でも一致しないときFalseを返す' do\r
677         r = Panel.validate_id [1, 1, 1, 1, 2], 1\r
678         r.should be_false\r
679       end\r
680       it '数値とNilが混ざってもIDが一致しないときFalseを返す' do\r
681         r = Panel.validate_id [1, 1, nil, 2], 1\r
682         r.should be_false\r
683       end\r
684     end\r
685     context '親が新規(idがNil)のとき' do\r
686       it 'すべてNilはTrueを返す' do\r
687         r = Panel.validate_id [ nil,nil,nil,nil], nil\r
688         r.should be_true\r
689       end\r
690       it 'すべてnilでなければFalseを返す' do\r
691         r = Panel.validate_id [nil,nil,nil,nil, 2], nil\r
692         r.should be_false\r
693       end\r
694     end\r
695   end\r
696   describe 'idチェック単体に於いて' do\r
697     before do\r
698 #      @pp = Factory :panel_picture, :panel_id => @panel.id, :t => 1\r
699     end\r
700     context 'つつがなく終わるとき' do\r
701       it 'idを収集依頼している' do\r
702         Panel.should_receive(:collect_element_value).with(any_args).exactly(1)\r
703         Panel.stub(:collect_element_value).with(any_args).and_return([])\r
704         Panel.stub(:validate_id).with(any_args).and_return(true)\r
705         r = Panel.validate_element_id [], :panel_id, nil\r
706       end\r
707       it '収集したidを一致チェック依頼している' do\r
708         Panel.should_receive(:validate_id).with(any_args).exactly(1)\r
709         Panel.stub(:collect_element_value).with(any_args).and_return([])\r
710         Panel.stub(:validate_id).with(any_args).and_return(true)\r
711         r = Panel.validate_element_id [], :panel_id, nil\r
712       end\r
713     end\r
714     #実データでチェック\r
715     #依頼チェックだけでは不安なので最低限のチェックを\r
716     context 'コマ新規のとき' do\r
717       it 'コマ絵で正常通過している' do\r
718         @panel = Factory.build :panel, :author_id => @author.id\r
719         @panel.panel_pictures.build(\r
720           Factory.attributes_for(:panel_picture, :panel_id => nil, :resource_picture_id => @rp.id, :t => nil)\r
721         )\r
722         r = Panel.validate_element_id [@panel.panel_pictures, @panel.speech_balloons], :panel_id, @panel.id\r
723         r.should be_true\r
724       end\r
725       it 'フキダシで正常通過している' do\r
726         @panel = Factory.build :panel, :author_id => @author.id\r
727         @panel.speech_balloons.build(\r
728           Factory.attributes_for(:speech_balloon, :panel_id => nil, :speech_balloon_template_id => @sbt.id)\r
729         )\r
730         r = Panel.validate_element_id [@panel.panel_pictures, @panel.speech_balloons], :panel_id, @panel.id\r
731         r.should be_true\r
732       end\r
733     end\r
734     context 'コマ既存のとき' do\r
735       it 'コマ絵で正常通過している' do\r
736         @panel = Factory :panel, :author_id => @author.id\r
737         @panel.panel_pictures.build(\r
738           Factory.attributes_for(:panel_picture, :panel_id => @panel.id, :resource_picture_id => @rp.id, :t => nil)\r
739         )\r
740         r = Panel.validate_element_id [@panel.panel_pictures, @panel.speech_balloons], :panel_id, @panel.id\r
741         r.should be_true\r
742       end\r
743       it 'フキダシで正常通過している' do\r
744         @panel = Factory :panel, :author_id => @author.id\r
745         @panel.speech_balloons.build(\r
746           Factory.attributes_for(:speech_balloon, :panel_id => @panel.id, :speech_balloon_template_id => @sbt.id)\r
747         )\r
748         r = Panel.validate_element_id [@panel.panel_pictures, @panel.speech_balloons], :panel_id, @panel.id\r
749         r.should be_true\r
750       end\r
751     end\r
752     context 'フキダシ新規のとき' do\r
753       it 'バルーンで正常通過している' do\r
754         @panel = Factory.build :panel, :author_id => @author.id\r
755         @panel.speech_balloons.build(\r
756           Factory.attributes_for(:speech_balloon, :panel_id => nil, :speech_balloon_template_id => @sbt.id)\r
757         )\r
758         @panel.speech_balloons.first.balloons.build(\r
759           Factory.attributes_for(:balloon, :speech_balloon_id => nil)\r
760         )\r
761         r = Panel.validate_element_id [@panel.speech_balloons.first.speeches, @panel.speech_balloons.first.balloons], :speech_balloon_id, @panel.speech_balloons.first.id\r
762         r.should be_true\r
763       end\r
764       it 'セリフで正常通過している' do\r
765         @panel = Factory.build :panel, :author_id => @author.id\r
766         @panel.speech_balloons.build(\r
767           Factory.attributes_for(:speech_balloon, :panel_id => nil, :speech_balloon_template_id => @sbt.id)\r
768         )\r
769         @panel.speech_balloons.first.speeches.build(\r
770           Factory.attributes_for(:speech, :speech_balloon_id => nil)\r
771         )\r
772         r = Panel.validate_element_id [@panel.speech_balloons.first.speeches, @panel.speech_balloons.first.balloons], :speech_balloon_id, @panel.speech_balloons.first.id\r
773         r.should be_true\r
774       end\r
775     end\r
776   end\r
777   describe 'idチェックリスト生成に於いて' do\r
778     before do\r
779       @panel = Factory.build :panel, :author_id => @author.id\r
780     end\r
781     context 'つつがなく終わるとき' do\r
782       it 'コマ部品とフキダシ部品のトータル2を返す' do\r
783         @panel.panel_pictures.build(\r
784           Factory.attributes_for(:panel_picture, :panel_id => nil, :resource_picture_id => @rp.id, :t => nil)\r
785         )\r
786         @panel.speech_balloons.build(\r
787           Factory.attributes_for(:speech_balloon, :panel_id => nil, :speech_balloon_template_id => @sbt.id)\r
788         )\r
789         @panel.speech_balloons.first.balloons.build(\r
790           Factory.attributes_for(:balloon, :speech_balloon_id => nil)\r
791         )\r
792         @panel.speech_balloons.first.speeches.build(\r
793           Factory.attributes_for(:speech, :speech_balloon_id => nil)\r
794         )\r
795         r = @panel.validate_id_list\r
796         r.should have(2).items\r
797       end\r
798       it 'コマ部品とフキダシ部品[複数:2]のケースでトータル3を返す' do\r
799         @panel.panel_pictures.build(\r
800           Factory.attributes_for(:panel_picture, :panel_id => @panel.id, :resource_picture_id => @rp.id, :t => nil)\r
801         )\r
802         sb1 = @panel.speech_balloons.build(\r
803           Factory.attributes_for(:speech_balloon, :panel_id => @panel.id, :speech_balloon_template_id => @sbt.id)\r
804         )\r
805         sb1.balloons.build(\r
806           Factory.attributes_for(:balloon, :speech_balloon_id => sb1.id)\r
807         )\r
808         sb1.speeches.build(\r
809           Factory.attributes_for(:speech, :speech_balloon_id => sb1.id)\r
810         )\r
811         sb2 = @panel.speech_balloons.build(\r
812           Factory.attributes_for(:speech_balloon, :panel_id => @panel.id, :speech_balloon_template_id => @sbt.id)\r
813         )\r
814         sb2.balloons.build(\r
815           Factory.attributes_for(:balloon, :speech_balloon_id => sb2.id)\r
816         )\r
817         sb2.speeches.build(\r
818           Factory.attributes_for(:speech, :speech_balloon_id => sb2.id)\r
819         )\r
820         r = @panel.validate_id_list\r
821         r.should have(3).items\r
822       end\r
823     end\r
824   end\r
825   describe 'idチェック複合に於いて' do\r
826     before do\r
827       @panel = Factory.build :panel, :author_id => @author.id\r
828     end\r
829     context 'つつがなく終わるとき' do\r
830       it 'コマ部品とフキダシ部品の二回チェック依頼している' do\r
831         Panel.should_receive(:validate_element_id).with(any_args).exactly(2)\r
832         Panel.stub(:validate_element_id).with(any_args).and_return(true)\r
833         @panel.panel_pictures.build(\r
834           Factory.attributes_for(:panel_picture, :panel_id => @panel.id, :resource_picture_id => @rp.id, :t => nil)\r
835         )\r
836         sb1 = @panel.speech_balloons.build(\r
837           Factory.attributes_for(:speech_balloon, :panel_id => @panel.id, :speech_balloon_template_id => @sbt.id)\r
838         )\r
839         sb1.balloons.build(\r
840           Factory.attributes_for(:balloon, :speech_balloon_id => sb1.id)\r
841         )\r
842         sb1.speeches.build(\r
843           Factory.attributes_for(:speech, :speech_balloon_id => sb1.id)\r
844         )\r
845         r = Panel.validate_elements_id(@panel.validate_id_list)\r
846       end\r
847     end\r
848   end\r
849   describe 'シリアライズチェックに於いて' do\r
850     context 'つつがなく終わるとき' do\r
851       it '0からシリアライズされているならTrueを返す' do\r
852         r = Panel.validate_t [0, 1, 2]\r
853         r.should be_true\r
854       end\r
855       it '見た目はシリアライズされてなくてもソート結果が無事ならtrueを返す' do\r
856         r = Panel.validate_t [0, 2, 1]\r
857         r.should be_true\r
858       end\r
859       it '見た目はシリアライズされてなくてもソート結果が無事ならtrueを返す' do\r
860         r = Panel.validate_t [ 2, 1, 4, 3, 0]\r
861         r.should be_true\r
862       end\r
863     end\r
864     context '異常なとき' do\r
865       it '0から始まらないならFalseを返す' do\r
866         r = Panel.validate_t [1, 2, 3]\r
867         r.should be_false\r
868       end\r
869       it '連続していないならFalseを返す' do\r
870         r = Panel.validate_t [0, 1, 2, 4]\r
871         r.should be_false\r
872       end\r
873       it '連続していないならFalseを返す' do\r
874         r = Panel.validate_t [0, 1, 2, 4, 5]\r
875         r.should be_false\r
876       end\r
877     end\r
878   end\r
879   describe 'tチェック単体に於いて' do\r
880     before do\r
881     end\r
882     context 'つつがなく終わるとき' do\r
883       it 'コマ絵とフキダシのtを収集依頼している' do\r
884         Panel.should_receive(:collect_element_value).with(any_args).exactly(1)\r
885         Panel.stub(:collect_element_value).with(any_args).and_return([])\r
886         Panel.stub(:validate_t).with(any_args).and_return(true)\r
887         r = Panel.validate_element_t [], :t\r
888       end\r
889       it '収集したtをシリアライズチェック依頼している' do\r
890         Panel.stub(:collect_element_value).with(any_args).and_return([])\r
891         Panel.should_receive(:validate_t).with(any_args).exactly(1)\r
892         Panel.stub(:validate_t).with(any_args).and_return(true)\r
893         r = Panel.validate_element_t [], :t\r
894       end\r
895     end\r
896     #実データでチェック\r
897     #依頼チェックだけでは不安なので最低限のチェックを\r
898     context '新規のとき' do\r
899       it 'コマ絵で正常通過している' do\r
900         @panel = Factory.build :panel, :author_id => @author.id\r
901         @panel.panel_pictures.build(\r
902           Factory.attributes_for(:panel_picture, :panel_id => nil, :resource_picture_id => @rp.id, :t => nil)\r
903         )\r
904         r = Panel.validate_element_t [@panel.panel_pictures, @panel.speech_balloons], :t\r
905         r.should be_true\r
906       end\r
907       it 'フキダシで正常通過している' do\r
908         @panel = Factory.build :panel, :author_id => @author.id\r
909         @panel.speech_balloons.build(\r
910           Factory.attributes_for(:speech_balloon, :panel_id => nil, :speech_balloon_template_id => @sbt.id, :t => nil)\r
911         )\r
912         r = Panel.validate_element_t [@panel.panel_pictures, @panel.speech_balloons], :t\r
913         r.should be_true\r
914       end\r
915     end\r
916     context '既存のとき' do\r
917       it 'コマ絵で正常通過している' do\r
918         @panel = Factory :panel, :author_id => @author.id\r
919         @panel.panel_pictures.build(\r
920           Factory.attributes_for(:panel_picture, :panel_id => @panel.id, :resource_picture_id => @rp.id, :t => 0)\r
921         )\r
922         r = Panel.validate_element_t [@panel.panel_pictures, @panel.speech_balloons], :t\r
923         r.should be_true\r
924       end\r
925       it 'フキダシで正常通過している' do\r
926         @panel = Factory :panel, :author_id => @author.id\r
927         @panel.speech_balloons.build(\r
928           Factory.attributes_for(:speech_balloon, :panel_id => @panel.id, :speech_balloon_template_id => @sbt.id, :t => 0)\r
929         )\r
930         r = Panel.validate_element_t [@panel.panel_pictures, @panel.speech_balloons], :t\r
931         r.should be_true\r
932       end\r
933     end\r
934   end\r
935   describe '複合チェックに於いて' do\r
936     context 'つつがなく終わるとき' do\r
937       it 'している' do\r
938         @panel = Factory.build :panel, :author_id => @author.id\r
939         @panel.panel_pictures.build(\r
940           Factory.attributes_for(:panel_picture, :panel_id => @panel.id, :resource_picture_id => @rp.id, :t => 0)\r
941         )\r
942         @panel.panel_pictures.build(\r
943           Factory.attributes_for(:panel_picture, :panel_id => @panel.id, :resource_picture_id => @rp.id, :t => 1)\r
944         )\r
945         sb1 = @panel.speech_balloons.build(\r
946           Factory.attributes_for(:speech_balloon, :panel_id => @panel.id, :speech_balloon_template_id => @sbt.id, :t => 2)\r
947         )\r
948         sb1.balloons.build(\r
949           Factory.attributes_for(:balloon, :speech_balloon_id => sb1.id)\r
950         )\r
951         sb1.speeches.build(\r
952           Factory.attributes_for(:speech, :speech_balloon_id => sb1.id)\r
953         )\r
954         sb2 = @panel.speech_balloons.build(\r
955           Factory.attributes_for(:speech_balloon, :panel_id => @panel.id, :speech_balloon_template_id => @sbt.id, :t => 3)\r
956         )\r
957         sb2.balloons.build(\r
958           Factory.attributes_for(:balloon, :speech_balloon_id => sb2.id)\r
959         )\r
960         sb2.speeches.build(\r
961           Factory.attributes_for(:speech, :speech_balloon_id => sb2.id)\r
962         )\r
963         r = @panel.validate_child\r
964         r.should be_true\r
965       end\r
966     end\r
967   end\r
968 end\r