OSDN Git Service

t#31486:add t and caption on ground
[pettanr/pettanr.git] / spec / models / ground_color_spec.rb
1 # -*- encoding: utf-8 -*-
2 require 'spec_helper'
3 #色地
4
5 describe GroundColor do
6   before do
7     @admin = FactoryGirl.create :admin
8     @user = FactoryGirl.create( :user_yas)
9     @author = FactoryGirl.create :author, :user_id => @user.id
10     @artist = FactoryGirl.create :artist_yas, :author_id => @author.id
11     @other_user = FactoryGirl.create( :user_yas)
12     @other_author = FactoryGirl.create :author, :user_id => @other_user.id
13     @other_artist = FactoryGirl.create :artist_yas, :author_id => @other_author.id
14     @sp = FactoryGirl.create :system_picture
15     @lg = FactoryGirl.create :license_group
16     @license = FactoryGirl.create :license, :license_group_id => @lg.id, :system_picture_id => @sp.id
17     @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
18     @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id
19     @rp = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => @op.id, :picture_id => @p.id
20     @panel = FactoryGirl.create :panel, :author_id => @author.id
21   end
22   
23   describe '検証に於いて' do
24     before do
25       @gc = FactoryGirl.build :ground_color, :panel_id => @panel.id
26     end
27     
28     context 'オーソドックスなデータのとき' do
29       it '下限データが通る' do
30         @gc.code = 0
31         @gc.z = 1
32         @gc.should be_valid
33       end
34       it '上限データが通る' do
35         @gc.code = 99999
36         @gc.z = 99999
37         @gc.should be_valid
38       end
39     end
40     
41     context 'panel_idを検証するとき' do
42       #ネストの保存はnilを許可しなければならないので数値チェックだけ
43       it '数値でなければ失敗する' do
44         @gc.panel_id = 'a'
45         @gc.should_not be_valid
46       end
47     end
48     context 'codeを検証するとき' do
49       it 'テストデータの確認' do
50         @gc.code = 0xffffff
51         @gc.should be_valid
52       end
53       it 'nullなら失敗する' do
54         @gc.code = nil
55         @gc.should_not be_valid
56       end
57       it '数値でなければ失敗する' do
58         @gc.code = 'a'
59         @gc.should_not be_valid
60       end
61       it '負なら失敗する' do
62         @gc.code = -1
63         @gc.should_not be_valid
64       end
65       it '24bit colorでなければ失敗する' do
66         @gc.code = 0x1000000
67         @gc.should_not be_valid
68       end
69     end
70     context 'zを検証するとき' do
71       it 'nullなら失敗する' do
72         @gc.z = nil
73         @gc.should_not be_valid
74       end
75       it '数値でなければ失敗する' do
76         @gc.z = 'a'
77         @gc.should_not be_valid
78       end
79       it '負なら失敗する' do
80         @gc.z = -1
81         @gc.should_not be_valid
82       end
83       it '負なら失敗する' do
84         @gc.z = 0
85         @gc.should_not be_valid
86       end
87     end
88   end
89   
90   describe '文字コード検証に於いて' do
91     before do
92       @gc = FactoryGirl.build :ground_color, :panel_id => @panel.id
93     end
94     
95     context 'captionを検証するとき' do
96       it 'Shift JISなら失敗する' do
97         @gc.caption = "\x83G\x83r\x83]\x83D"
98         lambda{
99           @gc.valid_encode
100         }.should raise_error(Pettanr::BadRequest)
101       end
102     end
103   end
104   
105   describe 'デフォルト値補充に於いて' do
106     it 'defined' do
107       @gc = FactoryGirl.build :ground_color, :panel_id => @panel.id
108       @gc.supply_default
109     end
110   end
111   
112   describe '上書き補充に於いて' do
113     it 'panel_idが設定されている' do
114       @gc = FactoryGirl.build :ground_color, :panel_id => @panel.id
115       @gc.overwrite  @panel.id
116       @gc.panel_id.should eq @panel.id
117     end
118   end
119   
120   describe '閲覧許可に於いて' do
121     before do
122       @gc = FactoryGirl.create :ground_color, :panel_id => @panel.id
123     end
124     context 'オープンモードのとき' do
125       before do
126         MagicNumber['run_mode'] = 0
127       end
128       it '自身にゲスト用ロールチェックを問い合わせしている' do
129         GroundColor.any_instance.stub(:guest_role_check).and_return(true)
130         GroundColor.any_instance.should_receive(:guest_role_check).with(any_args).exactly(1)
131         r = @gc.visible?([@author])
132       end
133       it 'ゲスト用ロールチェックが失敗したとき、falseを返す' do
134         GroundColor.any_instance.stub(:guest_role_check).and_return(false)
135         r = @gc.visible?([@author])
136         r.should be_false
137       end
138     end
139     context 'クローズドモードのとき' do
140       before do
141         MagicNumber['run_mode'] = 1
142       end
143       it '自身に読者用ロールチェックを問い合わせしている' do
144         GroundColor.any_instance.stub(:reader_role_check).and_return(true)
145         GroundColor.any_instance.should_receive(:reader_role_check).with(any_args).exactly(1)
146         r = @gc.visible?([@author])
147       end
148       it '読者用ロールチェックが失敗したとき、falseを返す' do
149         GroundColor.any_instance.stub(:reader_role_check).and_return(false)
150         r = @gc.visible?([@author])
151         r.should be_false
152       end
153     end
154     context '事前チェックする' do
155       before do
156         MagicNumber['run_mode'] = 1
157         GroundColor.any_instance.stub(:reader_role_check).and_return(true)
158       end
159       it '自身のコマに所持判定を問い合わせしている' do
160         Panel.any_instance.stub(:own?).and_return(true)
161         Panel.any_instance.should_receive(:own?).with(any_args).exactly(1)
162         r = @gc.visible?([@author])
163       end
164       it '自身のコマに閲覧許可を問い合わせしている' do
165         Panel.any_instance.stub(:own?).and_return(false)
166         Panel.any_instance.stub(:visible?).and_return(true)
167         Panel.any_instance.should_receive(:visible?).with(any_args).exactly(1)
168         r = @gc.visible?([@author])
169       end
170     end
171     context 'つつがなく終わるとき' do
172       before do
173         MagicNumber['run_mode'] = 1
174         Panel.any_instance.stub(:reader_role_check).and_return(true)
175       end
176       it '自分のコマの色地なら許可する' do
177         Panel.any_instance.stub(:own?).and_return(true)
178         Panel.any_instance.stub(:visible?).and_return(false)
179         r = @gc.visible?([@author])
180         r.should be_true
181       end
182       it '他人の非公開コマの色地なら許可しない' do
183         Panel.any_instance.stub(:own?).and_return(false)
184         Panel.any_instance.stub(:visible?).and_return(false)
185         r = @gc.visible?([@author])
186         r.should be_false
187       end
188       it '他人のコマの色地でも公開なら許可する' do
189         Panel.any_instance.stub(:own?).and_return(false)
190         Panel.any_instance.stub(:visible?).and_return(true)
191         r = @gc.visible?([@author])
192         r.should be_true
193       end
194     end
195   end
196   
197   describe '一覧取得に於いて' do
198     before do
199       @gc = FactoryGirl.create :ground_color, :panel_id => @panel.id
200     end
201     context 'page補正について' do
202       it '文字列から数値に変換される' do
203         GroundColor.page('8').should eq 8
204       end
205       it 'nilの場合は1になる' do
206         GroundColor.page().should eq 1
207       end
208       it '0以下の場合は1になる' do
209         GroundColor.page('0').should eq 1
210       end
211     end
212     context 'page_size補正について' do
213       it '文字列から数値に変換される' do
214         GroundColor.page_size('7').should eq 7
215       end
216       it 'nilの場合はGroundColor.default_page_sizeになる' do
217         GroundColor.page_size().should eq GroundColor.default_page_size
218       end
219       it '0以下の場合はGroundColor.default_page_sizeになる' do
220         GroundColor.page_size('0').should eq GroundColor.default_page_size
221       end
222       it 'GroundColor.max_page_sizeを超えた場合はGroundColor.max_page_sizeになる' do
223         GroundColor.page_size('1000').should eq GroundColor.max_page_size
224       end
225     end
226     context 'つつがなく終わるとき' do
227       it '一覧取得オプションを利用している' do
228         GroundColor.stub(:list_opt).with(any_args).and_return({:include => :panel})
229         GroundColor.should_receive(:list_opt).with(any_args).exactly(1)
230         r = GroundColor.list
231       end
232     end
233     it 'リストを返す' do
234       pl = GroundColor.list
235       pl.should eq [@gc]
236     end
237     it '時系列で並んでいる' do
238       #公開コマなら(他人のコマであっても)含んでいる
239       opl = FactoryGirl.create :panel, :author_id => @other_author.id, :publish => 1
240       npl = FactoryGirl.create :ground_color, :panel_id => opl.id, :updated_at => Time.now + 100
241       pl = GroundColor.list
242       pl.should eq [npl, @gc]
243     end
244     it '非公開のコマの色地は含まない' do
245       hpl = FactoryGirl.create :panel, :author_id => @author.id, :publish => 0
246       npl = FactoryGirl.create :ground_color, :panel_id => hpl.id, :updated_at => Time.now + 100
247       pl = GroundColor.list
248       pl.should eq [@gc]
249     end
250     context 'DBに5件あって1ページの件数を2件に変えたとして' do
251       before do
252         @gc2 = FactoryGirl.create :ground_color, :panel_id => @panel.id, :z => 1, :updated_at => Time.now + 100
253         @gc3 = FactoryGirl.create :ground_color, :panel_id => @panel.id, :z => 2, :updated_at => Time.now + 200
254         @gc4 = FactoryGirl.create :ground_color, :panel_id => @panel.id, :z => 3, :updated_at => Time.now + 300
255         @gc5 = FactoryGirl.create :ground_color, :panel_id => @panel.id, :z => 4, :updated_at => Time.now + 400
256         GroundColor.stub(:default_page_size).and_return(2)
257       end
258       it '通常は2件を返す' do
259         pl = GroundColor.list
260         pl.should have(2).items 
261       end
262       it 'page=1なら末尾2件を返す' do
263         #時系列で並んでいる
264         pl = GroundColor.list(1)
265         pl.should eq [@gc5, @gc4]
266       end
267       it 'page=2なら中間2件を返す' do
268         pl = GroundColor.list(2)
269         pl.should eq [@gc3, @gc2]
270       end
271       it 'page=3なら先頭1件を返す' do
272         pl = GroundColor.list(3)
273         pl.should eq [@gc]
274       end
275     end
276   end
277   
278   describe '自分のコマで使った色地一覧取得に於いて' do
279     before do
280       @gc = FactoryGirl.create :ground_color, :panel_id => @panel.id
281     end
282     context 'つつがなく終わるとき' do
283       it '一覧取得オプションを利用している' do
284         GroundColor.stub(:list_opt).with(any_args).and_return({:include => :panel})
285         GroundColor.should_receive(:list_opt).with(any_args).exactly(1)
286         r = GroundColor.mylist @author
287       end
288     end
289     it 'リストを返す' do
290       pl = GroundColor.mylist @author
291       pl.should eq [@gc]
292     end
293     it '時系列で並んでいる' do
294       npl = FactoryGirl.create :ground_color, :panel_id => @panel.id, :z => 2, :updated_at => Time.now + 100
295       pl = GroundColor.mylist @author
296       pl.should eq [npl, @gc]
297     end
298     it '他人のコマの色地は公開でも含まない' do
299       hpl = FactoryGirl.create :panel, :author_id => @other_author.id, :publish => 1
300       npl = FactoryGirl.create :ground_color, :panel_id => hpl.id
301       pl = GroundColor.mylist @author
302       pl.should eq [@gc]
303     end
304     it '自分のコマの色地は非公開でも含んでいる' do
305       hpl = FactoryGirl.create :panel, :author_id => @author.id, :publish => 0
306       npl = FactoryGirl.create :ground_color, :panel_id => hpl.id, :z => 2, :updated_at => Time.now + 100
307       pl = GroundColor.mylist @author
308       pl.should eq [npl, @gc]
309     end
310     context 'DBに5件あって1ページの件数を2件に変えたとして' do
311       before do
312         @gc2 = FactoryGirl.create :ground_color, :panel_id => @panel.id, :z => 1, :updated_at => Time.now + 100
313         @gc3 = FactoryGirl.create :ground_color, :panel_id => @panel.id, :z => 2, :updated_at => Time.now + 200
314         @gc4 = FactoryGirl.create :ground_color, :panel_id => @panel.id, :z => 3, :updated_at => Time.now + 300
315         @gc5 = FactoryGirl.create :ground_color, :panel_id => @panel.id, :z => 4, :updated_at => Time.now + 400
316       end
317       it '通常は2件を返す' do
318         c = GroundColor.mylist @author, 1, 2
319         c.should have(2).items 
320       end
321       it 'page=1なら末尾2件を返す' do
322         #時系列で並んでいる
323         c = GroundColor.mylist(@author, 1, 2)
324         c.should eq [@gc5, @gc4]
325       end
326       it 'page=2なら中間2件を返す' do
327         c = GroundColor.mylist(@author, 2, 2)
328         c.should eq [@gc3, @gc2]
329       end
330       it 'page=3なら先頭1件を返す' do
331         c = GroundColor.mylist(@author, 3, 2)
332         c.should eq [@gc]
333       end
334     end
335   end
336   
337   describe '他作家の色地一覧取得に於いて' do
338     before do
339       @gc = FactoryGirl.create :ground_color, :panel_id => @panel.id
340       @other_panel = FactoryGirl.create :panel, :author_id => @other_author.id, :publish => 1
341       @other_gc = FactoryGirl.create :ground_color, :panel_id => @other_panel.id
342     end
343     it 'リストを返す' do
344       r = GroundColor.himlist @other_author
345       r.should eq [@other_gc]
346     end
347     it '時系列で並んでいる' do
348       new_panel = FactoryGirl.create :panel, :author_id => @other_author.id, :updated_at => Time.now + 100
349       new_gc = FactoryGirl.create :ground_color, :panel_id => @other_panel.id, :updated_at => Time.now + 100
350       r = GroundColor.himlist @other_author
351       r.should eq [new_gc, @other_gc]
352     end
353     it '公開コマに限る' do
354       hidden_panel = FactoryGirl.create :panel, :author_id => @other_author.id, :publish => 0
355       hidden_gc = FactoryGirl.create :ground_color, :panel_id => hidden_panel.id
356       r = GroundColor.himlist @other_author
357       r.should eq [@other_gc]
358     end
359     context 'DBに5件あって1ページの件数を2件に変えたとして' do
360       before do
361         @other_gc2 = FactoryGirl.create :ground_color, :panel_id => @other_panel.id, :updated_at => Time.now + 100
362         @other_gc3 = FactoryGirl.create :ground_color, :panel_id => @other_panel.id, :updated_at => Time.now + 200
363         @other_gc4 = FactoryGirl.create :ground_color, :panel_id => @other_panel.id, :updated_at => Time.now + 300
364         @other_gc5 = FactoryGirl.create :ground_color, :panel_id => @other_panel.id, :updated_at => Time.now + 400
365       end
366       it '通常は2件を返す' do
367         pl = GroundColor.himlist @other_author, 1, 2
368         pl.should have(2).items 
369       end
370       it 'page=1なら末尾2件を返す' do
371         #時系列で並んでいる
372         pl = GroundColor.himlist @other_author, 1, 2
373         pl.should eq [@other_gc5, @other_gc4]
374       end
375       it 'page=2なら中間2件を返す' do
376         pl = GroundColor.himlist @other_author, 2, 2
377         pl.should eq [@other_gc3, @other_gc2]
378       end
379       it 'page=3なら先頭1件を返す' do
380         pl = GroundColor.himlist @other_author, 3, 2
381         pl.should eq [@other_gc]
382       end
383     end
384   end
385   
386   describe '色地一覧ページ制御に於いて' do
387     before do
388       GroundColor.stub(:count).with(any_args).and_return(100)
389     end
390     it 'ページ制御を返す' do
391       r = GroundColor.list_paginate 
392       r.is_a?(Kaminari::PaginatableArray).should be_true
393     end
394     it '色地一覧の取得条件を利用している' do
395       GroundColor.stub(:list_where).with(any_args).and_return('')
396       GroundColor.should_receive(:list_where).with(any_args).exactly(1)
397       r = GroundColor.list_paginate 
398     end
399     it 'ページ件数10のとき、3ページ目のオフセットは20から始まる' do
400       r = GroundColor.list_paginate 3, 10
401       r.limit_value.should eq 10
402       r.offset_value.should eq 20
403     end
404   end
405   
406   describe '自分の色地一覧ページ制御に於いて' do
407     before do
408       GroundColor.stub(:count).with(any_args).and_return(100)
409     end
410     it 'ページ制御を返す' do
411       r = GroundColor.mylist_paginate @author
412       r.is_a?(Kaminari::PaginatableArray).should be_true
413     end
414     it '自分の色地一覧の取得条件を利用している' do
415       GroundColor.stub(:mylist_where).with(any_args).and_return('')
416       GroundColor.should_receive(:mylist_where).with(any_args).exactly(1)
417       r = GroundColor.mylist_paginate @author
418     end
419     it 'ページ件数10のとき、3ページ目のオフセットは20から始まる' do
420       r = GroundColor.mylist_paginate @author, 3, 10
421       r.limit_value.should eq 10
422       r.offset_value.should eq 20
423     end
424   end
425   
426   describe '他作家の色地一覧ページ制御に於いて' do
427     before do
428       GroundColor.stub(:count).with(any_args).and_return(100)
429     end
430     it 'ページ制御を返す' do
431       r = GroundColor.himlist_paginate @other_author
432       r.is_a?(Kaminari::PaginatableArray).should be_true
433     end
434     it '他作家の色地一覧の取得条件を利用している' do
435       GroundColor.stub(:himlist_where).with(any_args).and_return('')
436       GroundColor.should_receive(:himlist_where).with(any_args).exactly(1)
437       r = GroundColor.himlist_paginate @other_author
438     end
439     it 'ページ件数10のとき、3ページ目のオフセットは20から始まる' do
440       r = GroundColor.himlist_paginate @other_author, 3, 10
441       r.limit_value.should eq 10
442       r.offset_value.should eq 20
443     end
444   end
445   
446   describe '一覧取得オプションに於いて' do
447     it '1つの項目を含んでいる' do
448       r = GroundColor.list_opt
449       r.should have(1).items
450     end
451     it 'コマを含んでいる' do
452       r = GroundColor.list_opt
453       r.has_key?(:panel).should be_true
454     end
455       it 'コマは作家を含んでいる' do
456         r = GroundColor.list_opt
457         r[:panel].has_key?(:author).should be_true
458       end
459   end
460   describe 'json一覧出力オプションに於いて' do
461     before do
462       @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
463       @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id
464       @rp = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => @op.id, :picture_id => @p.id
465       @sbt = FactoryGirl.create :speech_balloon_template
466       @comic = FactoryGirl.create :comic, :author_id => @author.id, :visible => 1
467       @panel = FactoryGirl.create :panel, :author_id => @author.id, :publish => 1
468       @story = FactoryGirl.create :story, :author_id => @author.id, :comic_id => @comic.id, :panel_id => @panel.id
469       @gc = FactoryGirl.create :ground_color, :panel_id => @panel.id
470     end
471     it 'コマを含んでいる' do
472       r = GroundColor.list.to_json GroundColor.list_json_opt
473       j = JSON.parse r
474       i = j.first
475       i.has_key?('panel').should be_true
476     end
477       it 'コマは作家を含んでいる' do
478         r = GroundColor.list.to_json GroundColor.list_json_opt
479         j = JSON.parse r
480         i = j.first
481         s = i['panel']
482         s.has_key?('author').should be_true
483       end
484   end
485   
486   describe '単体取得に於いて' do
487     before do
488       @gc = FactoryGirl.create :ground_color, :panel_id => @panel.id
489     end
490     context 'つつがなく終わるとき' do
491       it '単体取得オプションを利用している' do
492         GroundColor.stub(:show_opt).with(any_args).and_return({:include => :panel})
493         GroundColor.should_receive(:show_opt).with(any_args).exactly(1)
494         r = GroundColor.show @gc.id, @author
495       end
496       it '閲覧許可を問い合わせている' do
497         GroundColor.any_instance.stub(:visible?).with(any_args).and_return(true)
498         GroundColor.any_instance.should_receive(:visible?).with(any_args).exactly(1)
499         r = GroundColor.show @gc.id, @author
500       end
501     end
502     it '指定の色地を返す' do
503       GroundColor.any_instance.stub(:visible?).and_return(true)
504       pl = GroundColor.show @gc.id, @author
505       pl.should eq @gc
506     end
507     context '閲覧許可が出なかったとき' do
508       it '403Forbidden例外を返す' do
509         GroundColor.any_instance.stub(:visible?).and_return(false)
510         lambda{
511           GroundColor.show @gc.id, @author
512         }.should raise_error(ActiveRecord::Forbidden)
513       end
514     end
515     context '存在しない色地を開こうとしたとき' do
516       it '404RecordNotFound例外を返す' do
517         lambda{
518           GroundColor.show 110, @author
519         }.should raise_error(ActiveRecord::RecordNotFound)
520       end
521     end
522   end
523   describe '単体取得オプションに於いて' do
524     it 'includeキーを含んでいる' do
525       r = GroundColor.show_opt
526       r.has_key?(:include).should be_true
527     end
528     it '1つの項目を含んでいる' do
529       r = GroundColor.show_opt[:include]
530       r.should have(1).items
531     end
532     it 'コマを含んでいる' do
533       r = GroundColor.show_opt[:include]
534       r.has_key?(:panel).should be_true
535     end
536       it 'コマは作家を含んでいる' do
537         r = GroundColor.show_opt[:include]
538         r[:panel].has_key?(:author).should be_true
539       end
540   end
541   describe 'json単体出力オプションに於いて' do
542     before do
543       @gc = FactoryGirl.create :ground_color, :panel_id => @panel.id
544     end
545     it 'コマを含んでいる' do
546       r = GroundColor.show(@gc.id, @author).to_json GroundColor.show_json_opt
547       j = JSON.parse r
548       i = j
549       i.has_key?('panel').should be_true
550     end
551       it 'コマは作家を含んでいる' do
552         r = GroundColor.show(@gc.id, @author).to_json GroundColor.show_json_opt
553         j = JSON.parse r
554         i = j
555         s = i['panel']
556         s.has_key?('author').should be_true
557       end
558   end
559   
560 end