OSDN Git Service

t#31326:drop color
[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     context 'DBに5件あって1ページの件数を0件に変えたとして' do
261       before do
262         @gc2 = FactoryGirl.create :ground_color, :panel_id => @panel.id, :z => 1, :updated_at => Time.now + 100
263         @gc3 = FactoryGirl.create :ground_color, :panel_id => @panel.id, :z => 2, :updated_at => Time.now + 200
264         @gc4 = FactoryGirl.create :ground_color, :panel_id => @panel.id, :z => 3, :updated_at => Time.now + 300
265         @gc5 = FactoryGirl.create :ground_color, :panel_id => @panel.id, :z => 4, :updated_at => Time.now + 400
266         GroundColor.stub(:default_page_size).and_return(2)
267       end
268       it '通常は全件(5件)を返す' do
269         r = GroundColor.list 5, 0
270         r.should have(5).items 
271       end
272     end
273   end
274   
275   describe '自分のコマで使った色地一覧取得に於いて' do
276     before do
277       @gc = FactoryGirl.create :ground_color, :panel_id => @panel.id
278     end
279     context 'つつがなく終わるとき' do
280       it '一覧取得オプションを利用している' do
281         GroundColor.stub(:list_opt).with(any_args).and_return({:include => :panel})
282         GroundColor.should_receive(:list_opt).with(any_args).exactly(1)
283         r = GroundColor.mylist @author
284       end
285     end
286     it 'リストを返す' do
287       pl = GroundColor.mylist @author
288       pl.should eq [@gc]
289     end
290     it '時系列で並んでいる' do
291       npl = FactoryGirl.create :ground_color, :panel_id => @panel.id, :z => 2, :updated_at => Time.now + 100
292       pl = GroundColor.mylist @author
293       pl.should eq [npl, @gc]
294     end
295     it '他人のコマの色地は公開でも含まない' do
296       hpl = FactoryGirl.create :panel, :author_id => @other_author.id, :publish => 1
297       npl = FactoryGirl.create :ground_color, :panel_id => hpl.id
298       pl = GroundColor.mylist @author
299       pl.should eq [@gc]
300     end
301     it '自分のコマの色地は非公開でも含んでいる' do
302       hpl = FactoryGirl.create :panel, :author_id => @author.id, :publish => 0
303       npl = FactoryGirl.create :ground_color, :panel_id => hpl.id, :z => 2, :updated_at => Time.now + 100
304       pl = GroundColor.mylist @author
305       pl.should eq [npl, @gc]
306     end
307     context 'DBに5件あって1ページの件数を2件に変えたとして' do
308       before do
309         @gc2 = FactoryGirl.create :ground_color, :panel_id => @panel.id, :z => 1, :updated_at => Time.now + 100
310         @gc3 = FactoryGirl.create :ground_color, :panel_id => @panel.id, :z => 2, :updated_at => Time.now + 200
311         @gc4 = FactoryGirl.create :ground_color, :panel_id => @panel.id, :z => 3, :updated_at => Time.now + 300
312         @gc5 = FactoryGirl.create :ground_color, :panel_id => @panel.id, :z => 4, :updated_at => Time.now + 400
313       end
314       it '通常は2件を返す' do
315         c = GroundColor.mylist @author, 1, 2
316         c.should have(2).items 
317       end
318       it 'page=1なら末尾2件を返す' do
319         #時系列で並んでいる
320         c = GroundColor.mylist(@author, 1, 2)
321         c.should eq [@gc5, @gc4]
322       end
323       it 'page=2なら中間2件を返す' do
324         c = GroundColor.mylist(@author, 2, 2)
325         c.should eq [@gc3, @gc2]
326       end
327       it 'page=3なら先頭1件を返す' do
328         c = GroundColor.mylist(@author, 3, 2)
329         c.should eq [@gc]
330       end
331     end
332     context 'DBに5件あって1ページの件数を0件に変えたとして' do
333       before do
334         @gc2 = FactoryGirl.create :ground_color, :panel_id => @panel.id, :z => 1, :updated_at => Time.now + 100
335         @gc3 = FactoryGirl.create :ground_color, :panel_id => @panel.id, :z => 2, :updated_at => Time.now + 200
336         @gc4 = FactoryGirl.create :ground_color, :panel_id => @panel.id, :z => 3, :updated_at => Time.now + 300
337         @gc5 = FactoryGirl.create :ground_color, :panel_id => @panel.id, :z => 4, :updated_at => Time.now + 400
338         Author.stub(:default_ground_color_page_size).and_return(2)
339       end
340       it '通常は全件(5件)を返す' do
341         r = GroundColor.mylist @author, 5, 0
342         r.should have(5).items 
343       end
344     end
345   end
346   
347   describe '他作家の色地一覧取得に於いて' do
348     before do
349       @gc = FactoryGirl.create :ground_color, :panel_id => @panel.id
350       @other_panel = FactoryGirl.create :panel, :author_id => @other_author.id, :publish => 1
351       @other_gc = FactoryGirl.create :ground_color, :panel_id => @other_panel.id
352     end
353     it 'リストを返す' do
354       r = GroundColor.himlist @other_author
355       r.should eq [@other_gc]
356     end
357     it '時系列で並んでいる' do
358       new_panel = FactoryGirl.create :panel, :author_id => @other_author.id, :updated_at => Time.now + 100
359       new_gc = FactoryGirl.create :ground_color, :panel_id => @other_panel.id, :updated_at => Time.now + 100
360       r = GroundColor.himlist @other_author
361       r.should eq [new_gc, @other_gc]
362     end
363     it '公開コマに限る' do
364       hidden_panel = FactoryGirl.create :panel, :author_id => @other_author.id, :publish => 0
365       hidden_gc = FactoryGirl.create :ground_color, :panel_id => hidden_panel.id
366       r = GroundColor.himlist @other_author
367       r.should eq [@other_gc]
368     end
369     context 'DBに5件あって1ページの件数を2件に変えたとして' do
370       before do
371         @other_gc2 = FactoryGirl.create :ground_color, :panel_id => @other_panel.id, :updated_at => Time.now + 100
372         @other_gc3 = FactoryGirl.create :ground_color, :panel_id => @other_panel.id, :updated_at => Time.now + 200
373         @other_gc4 = FactoryGirl.create :ground_color, :panel_id => @other_panel.id, :updated_at => Time.now + 300
374         @other_gc5 = FactoryGirl.create :ground_color, :panel_id => @other_panel.id, :updated_at => Time.now + 400
375       end
376       it '通常は2件を返す' do
377         pl = GroundColor.himlist @other_author, 1, 2
378         pl.should have(2).items 
379       end
380       it 'page=1なら末尾2件を返す' do
381         #時系列で並んでいる
382         pl = GroundColor.himlist @other_author, 1, 2
383         pl.should eq [@other_gc5, @other_gc4]
384       end
385       it 'page=2なら中間2件を返す' do
386         pl = GroundColor.himlist @other_author, 2, 2
387         pl.should eq [@other_gc3, @other_gc2]
388       end
389       it 'page=3なら先頭1件を返す' do
390         pl = GroundColor.himlist @other_author, 3, 2
391         pl.should eq [@other_gc]
392       end
393     end
394     context 'DBに5件あって1ページの件数を0件に変えたとして' do
395       before do
396         @other_gc2 = FactoryGirl.create :ground_color, :panel_id => @other_panel.id, :updated_at => Time.now + 100
397         @other_gc3 = FactoryGirl.create :ground_color, :panel_id => @other_panel.id, :updated_at => Time.now + 200
398         @other_gc4 = FactoryGirl.create :ground_color, :panel_id => @other_panel.id, :updated_at => Time.now + 300
399         @other_gc5 = FactoryGirl.create :ground_color, :panel_id => @other_panel.id, :updated_at => Time.now + 400
400         Author.stub(:default_ground_color_page_size).and_return(2)
401       end
402       it '通常は全件(5件)を返す' do
403         r = GroundColor.himlist @other_author, 5, 0
404         r.should have(5).items 
405       end
406     end
407   end
408   
409   describe '一覧取得オプションに於いて' do
410     it 'includeキーを含んでいる' do
411       r = GroundColor.list_opt
412       r.has_key?(:include).should be_true
413     end
414     it '1つの項目を含んでいる' do
415       r = GroundColor.list_opt[:include]
416       r.should have(1).items
417     end
418     it 'コマを含んでいる' do
419       r = GroundColor.list_opt[:include]
420       r.has_key?(:panel).should be_true
421     end
422       it 'コマは作家を含んでいる' do
423         r = GroundColor.list_opt[:include]
424         r[:panel].has_key?(:author).should be_true
425       end
426   end
427   describe 'json一覧出力オプションに於いて' do
428     before do
429       @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
430       @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id
431       @rp = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => @op.id, :picture_id => @p.id
432       @sbt = FactoryGirl.create :speech_balloon_template
433       @comic = FactoryGirl.create :comic, :author_id => @author.id, :visible => 1
434       @panel = FactoryGirl.create :panel, :author_id => @author.id, :publish => 1
435       @story = FactoryGirl.create :story, :author_id => @author.id, :comic_id => @comic.id, :panel_id => @panel.id
436       @gc = FactoryGirl.create :ground_color, :panel_id => @panel.id
437     end
438     it 'コマを含んでいる' do
439       r = GroundColor.list.to_json GroundColor.list_json_opt
440       j = JSON.parse r
441       i = j.first
442       i.has_key?('panel').should be_true
443     end
444       it 'コマは作家を含んでいる' do
445         r = GroundColor.list.to_json GroundColor.list_json_opt
446         j = JSON.parse r
447         i = j.first
448         s = i['panel']
449         s.has_key?('author').should be_true
450       end
451   end
452   
453   describe '単体取得に於いて' do
454     before do
455       @gc = FactoryGirl.create :ground_color, :panel_id => @panel.id
456     end
457     context 'つつがなく終わるとき' do
458       it '単体取得オプションを利用している' do
459         GroundColor.stub(:show_opt).with(any_args).and_return({:include => :panel})
460         GroundColor.should_receive(:show_opt).with(any_args).exactly(1)
461         r = GroundColor.show @gc.id, @author
462       end
463       it '閲覧許可を問い合わせている' do
464         GroundColor.any_instance.stub(:visible?).with(any_args).and_return(true)
465         GroundColor.any_instance.should_receive(:visible?).with(any_args).exactly(1)
466         r = GroundColor.show @gc.id, @author
467       end
468     end
469     it '指定の色地を返す' do
470       GroundColor.any_instance.stub(:visible?).and_return(true)
471       pl = GroundColor.show @gc.id, @author
472       pl.should eq @gc
473     end
474     context '閲覧許可が出なかったとき' do
475       it '403Forbidden例外を返す' do
476         GroundColor.any_instance.stub(:visible?).and_return(false)
477         lambda{
478           GroundColor.show @gc.id, @author
479         }.should raise_error(ActiveRecord::Forbidden)
480       end
481     end
482     context '存在しない色地を開こうとしたとき' do
483       it '404RecordNotFound例外を返す' do
484         lambda{
485           GroundColor.show 110, @author
486         }.should raise_error(ActiveRecord::RecordNotFound)
487       end
488     end
489   end
490   describe '単体取得オプションに於いて' do
491     it 'includeキーを含んでいる' do
492       r = GroundColor.show_opt
493       r.has_key?(:include).should be_true
494     end
495     it '1つの項目を含んでいる' do
496       r = GroundColor.show_opt[:include]
497       r.should have(1).items
498     end
499     it 'コマを含んでいる' do
500       r = GroundColor.show_opt[:include]
501       r.has_key?(:panel).should be_true
502     end
503       it 'コマは作家を含んでいる' do
504         r = GroundColor.show_opt[:include]
505         r[:panel].has_key?(:author).should be_true
506       end
507   end
508   describe 'json単体出力オプションに於いて' do
509     before do
510       @gc = FactoryGirl.create :ground_color, :panel_id => @panel.id
511     end
512     it 'コマを含んでいる' do
513       r = GroundColor.show(@gc.id, @author).to_json GroundColor.show_json_opt
514       j = JSON.parse r
515       i = j
516       i.has_key?('panel').should be_true
517     end
518       it 'コマは作家を含んでいる' do
519         r = GroundColor.show(@gc.id, @author).to_json GroundColor.show_json_opt
520         j = JSON.parse r
521         i = j
522         s = i['panel']
523         s.has_key?('author').should be_true
524       end
525   end
526   
527 end