OSDN Git Service

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