OSDN Git Service

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