OSDN Git Service

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