OSDN Git Service

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