OSDN Git Service

t#30021:add repeat on gp
[pettanr/pettanr.git] / spec / models / ground_picture_spec.rb
1 # -*- encoding: utf-8 -*-
2 require 'spec_helper'
3 #コマの画像背景
4
5 describe GroundPicture do
6   before do
7     FactoryGirl.create :admin
8     @user = FactoryGirl.create( :user_yas)
9     @author = @user.author
10     @artist = FactoryGirl.create :artist_yas, :author_id => @author.id
11     @other_user = FactoryGirl.create( :user_yas)
12     @other_author = @other_user.author
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       @gp = FactoryGirl.build :ground_picture, :panel_id => @panel.id, :picture_id => @p.id
26     end
27     
28     context 'オーソドックスなデータのとき' do
29       it '下限データが通る' do
30         @gp.repeat = 0
31         @gp.x = -99999
32         @gp.y = -99999
33         @gp.z = 1
34         @gp.should be_valid
35       end
36       it '上限データが通る' do
37         @gp.repeat = 3
38         @gp.x = 99999
39         @gp.y = 99999
40         @gp.z = 99999
41         @gp.should be_valid
42       end
43     end
44     
45     
46     context 'panel_idを検証するとき' do
47       #ネストの保存はnilを許可しなければならないので数値チェックだけ
48       it '数値でなければ失敗する' do
49         @gp.panel_id = 'a'
50         @gp.should_not be_valid
51       end
52     end
53     context 'picture_idを検証するとき' do
54       it 'nullなら失敗する' do
55         @gp.picture_id = nil
56         @gp.should_not be_valid
57       end
58       it '数値でなければ失敗する' do
59         @gp.picture_id = 'a'
60         @gp.should_not be_valid
61       end
62       it '存在する実素材でなければ失敗する' do
63         @gp.picture_id = 0
64         @gp.should_not be_valid
65       end
66     end
67     context 'repeatを検証するとき' do
68       it 'nullなら失敗する' do
69         @gp.repeat = nil
70         @gp.should_not be_valid
71       end
72       it '数値でなければ失敗する' do
73         @gp.repeat = 'a'
74         @gp.should_not be_valid
75       end
76       it '-1なら失敗する' do
77         @gp.repeat = -1
78         @gp.should_not be_valid
79       end
80       it '4なら失敗する' do
81         @gp.repeat = 4
82         @gp.should_not be_valid
83       end
84     end
85     context 'xを検証するとき' do
86       it 'nullなら失敗する' do
87         @gp.x = nil
88         @gp.should_not be_valid
89       end
90       it '数値でなければ失敗する' do
91         @gp.x = 'a'
92         @gp.should_not be_valid
93       end
94     end
95     context 'yを検証するとき' do
96       it 'nullなら失敗する' do
97         @gp.y = nil
98         @gp.should_not be_valid
99       end
100       it '数値でなければ失敗する' do
101         @gp.y = 'a'
102         @gp.should_not be_valid
103       end
104     end
105     context 'zを検証するとき' do
106       it 'nullなら失敗する' do
107         @gp.z = nil
108         @gp.should_not be_valid
109       end
110       it '数値でなければ失敗する' do
111         @gp.z = 'a'
112         @gp.should_not be_valid
113       end
114       it '負なら失敗する' do
115         @gp.z = -1
116         @gp.should_not be_valid
117       end
118       it '負なら失敗する' do
119         @gp.z = 0
120         @gp.should_not be_valid
121       end
122     end
123   end
124   
125   describe 'デフォルト値補充に於いて' do
126     before do
127       @gp = FactoryGirl.create :ground_picture, :panel_id => @panel.id, :picture_id => @p.id
128     end
129     it 'xに0を補充している' do
130       @gp.supply_default
131       @gp.x.should eq 0
132     end
133     it 'yに0を補充している' do
134       @gp.supply_default
135       @gp.y.should eq 0
136     end
137     it '繰り返しに0を補充している' do
138       @gp.supply_default
139       @gp.repeat.should eq 0
140     end
141   end
142   
143   describe '上書き補充に於いて' do
144     it 'defined' do
145       @gp = FactoryGirl.build :ground_picture, :panel_id => @panel.id, :picture_id => @p.id
146       @gp.overwrite
147     end
148   end
149   
150   describe '一覧取得に於いて' do
151     before do
152       @gp = FactoryGirl.create :ground_picture, :panel_id => @panel.id, :picture_id => @p.id
153     end
154     context 'page補正について' do
155       it '文字列から数値に変換される' do
156         GroundPicture.page('8').should eq 8
157       end
158       it 'nilの場合は1になる' do
159         GroundPicture.page().should eq 1
160       end
161       it '0以下の場合は1になる' do
162         GroundPicture.page('0').should eq 1
163       end
164     end
165     context 'page_size補正について' do
166       it '文字列から数値に変換される' do
167         GroundPicture.page_size('7').should eq 7
168       end
169       it 'nilの場合はGroundPicture.default_page_sizeになる' do
170         GroundPicture.page_size().should eq GroundPicture.default_page_size
171       end
172       it '0以下の場合はGroundPicture.default_page_sizeになる' do
173         GroundPicture.page_size('0').should eq GroundPicture.default_page_size
174       end
175       it 'GroundPicture.max_page_sizeを超えた場合はGroundPicture.max_page_sizeになる' do
176         GroundPicture.page_size('1000').should eq GroundPicture.max_page_size
177       end
178     end
179     context 'つつがなく終わるとき' do
180       it '一覧取得オプションを利用している' do
181         GroundPicture.stub(:list_opt).with(any_args).and_return({:include => :panel})
182         GroundPicture.should_receive(:list_opt).with(any_args).exactly(1)
183         r = GroundPicture.list
184       end
185     end
186     it 'リストを返す' do
187       pl = GroundPicture.list
188       pl.should eq [@gp]
189     end
190     it '時系列で並んでいる' do
191       #公開コマなら(他人のコマであっても)含んでいる
192       opl = FactoryGirl.create :panel, :author_id => @other_author.id, :publish => 1
193       npl = FactoryGirl.create :ground_picture, :panel_id => opl.id, :picture_id => @p.id, :updated_at => Time.now + 100
194       pl = GroundPicture.list
195       pl.should eq [npl, @gp]
196     end
197     it '非公開のコマの景色は含まない' do
198       hpl = FactoryGirl.create :panel, :author_id => @author.id, :publish => 0
199       npl = FactoryGirl.create :ground_picture, :panel_id => hpl.id, :picture_id => @p.id, :updated_at => Time.now + 100
200       pl = GroundPicture.list
201       pl.should eq [@gp]
202     end
203     context 'DBに5件あって1ページの件数を2件に変えたとして' do
204       before do
205         @gp2 = FactoryGirl.create :ground_picture, :panel_id => @panel.id, :z => 1, :picture_id => @p.id, :updated_at => Time.now + 100
206         @gp3 = FactoryGirl.create :ground_picture, :panel_id => @panel.id, :z => 2, :picture_id => @p.id, :updated_at => Time.now + 200
207         @gp4 = FactoryGirl.create :ground_picture, :panel_id => @panel.id, :z => 3, :picture_id => @p.id, :updated_at => Time.now + 300
208         @gp5 = FactoryGirl.create :ground_picture, :panel_id => @panel.id, :z => 4, :picture_id => @p.id, :updated_at => Time.now + 400
209         GroundPicture.stub(:default_page_size).and_return(2)
210       end
211       it '通常は2件を返す' do
212         pl = GroundPicture.list
213         pl.should have(2).items 
214       end
215       it 'page=1なら末尾2件を返す' do
216         #時系列で並んでいる
217         pl = GroundPicture.list(1)
218         pl.should eq [@gp5, @gp4]
219       end
220       it 'page=2なら中間2件を返す' do
221         pl = GroundPicture.list(2)
222         pl.should eq [@gp3, @gp2]
223       end
224       it 'page=3なら先頭1件を返す' do
225         pl = GroundPicture.list(3)
226         pl.should eq [@gp]
227       end
228     end
229     context 'DBに5件あって1ページの件数を0件に変えたとして' do
230       before do
231         @gp2 = FactoryGirl.create :ground_picture, :panel_id => @panel.id, :z => 1, :picture_id => @p.id, :updated_at => Time.now + 100
232         @gp3 = FactoryGirl.create :ground_picture, :panel_id => @panel.id, :z => 2, :picture_id => @p.id, :updated_at => Time.now + 200
233         @gp4 = FactoryGirl.create :ground_picture, :panel_id => @panel.id, :z => 3, :picture_id => @p.id, :updated_at => Time.now + 300
234         @gp5 = FactoryGirl.create :ground_picture, :panel_id => @panel.id, :z => 4, :picture_id => @p.id, :updated_at => Time.now + 400
235         GroundPicture.stub(:default_page_size).and_return(2)
236       end
237       it '通常は全件(5件)を返す' do
238         r = GroundPicture.list 5, 0
239         r.should have(5).items 
240       end
241     end
242   end
243   describe '一覧取得オプションに於いて' do
244     it 'includeキーを含んでいる' do
245       r = GroundPicture.list_opt
246       r.has_key?(:include).should be_true
247     end
248     it '2つの項目を含んでいる' do
249       r = GroundPicture.list_opt[:include]
250       r.should have(2).items
251     end
252     it 'コマを含んでいる' do
253       r = GroundPicture.list_opt[:include]
254       r.has_key?(:panel).should be_true
255     end
256       it 'コマは作家を含んでいる' do
257         r = GroundPicture.list_opt[:include]
258         r[:panel].has_key?(:author).should be_true
259       end
260     it '実素材を含んでいる' do
261       r = GroundPicture.list_opt[:include]
262       r.has_key?(:picture).should be_true
263     end
264       it '実素材は絵師を含んでいる' do
265         r = GroundPicture.list_opt[:include]
266         r[:picture].has_key?(:artist).should be_true
267       end
268       it '実素材はライセンスを含んでいる' do
269         r = GroundPicture.list_opt[:include]
270         r[:picture].has_key?(:license).should be_true
271       end
272   end
273   describe 'json一覧出力オプションに於いて' do
274     before do
275       @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
276       @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id
277       @rp = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => @op.id, :picture_id => @p.id
278       @sbt = FactoryGirl.create :speech_balloon_template
279       @comic = FactoryGirl.create :comic, :author_id => @author.id, :visible => 1
280       @panel = FactoryGirl.create :panel, :author_id => @author.id, :publish => 1
281       @story = FactoryGirl.create :story, :author_id => @author.id, :comic_id => @comic.id, :panel_id => @panel.id
282       @gp = FactoryGirl.create :ground_picture, :panel_id => @panel.id, :picture_id => @p.id
283     end
284     it 'コマを含んでいる' do
285       r = GroundPicture.list.to_json GroundPicture.list_json_opt
286       j = JSON.parse r
287       i = j.first
288       i.has_key?('panel').should be_true
289     end
290       it 'コマは作家を含んでいる' do
291         r = GroundPicture.list.to_json GroundPicture.list_json_opt
292         j = JSON.parse r
293         i = j.first
294         s = i['panel']
295         s.has_key?('author').should be_true
296       end
297     it '実素材を含んでいる' do
298       r = GroundPicture.list.to_json GroundPicture.list_json_opt
299       j = JSON.parse r
300       i = j.first
301       i.has_key?('picture').should be_true
302     end
303       it '実素材は絵師を含んでいる' do
304         r = GroundPicture.list.to_json GroundPicture.list_json_opt
305         j = JSON.parse r
306         i = j.first
307         s = i['picture']
308         s.has_key?('artist').should be_true
309       end
310       it '実素材はライセンスを含んでいる' do
311         r = GroundPicture.list.to_json GroundPicture.list_json_opt
312         j = JSON.parse r
313         i = j.first
314         s = i['picture']
315         s.has_key?('license').should be_true
316       end
317   end
318   
319   describe '自分のコマで使った景色画像一覧取得に於いて' do
320     before do
321       @gp = FactoryGirl.create :ground_picture, :panel_id => @panel.id, :picture_id => @p.id
322     end
323     context 'つつがなく終わるとき' do
324       it '一覧取得オプションを利用している' do
325         GroundPicture.stub(:list_opt).with(any_args).and_return({:include => :panel})
326         GroundPicture.should_receive(:list_opt).with(any_args).exactly(1)
327         r = GroundPicture.mylist @author
328       end
329     end
330     it 'リストを返す' do
331       pl = GroundPicture.mylist @author
332       pl.should eq [@gp]
333     end
334     it '時系列で並んでいる' do
335       npl = FactoryGirl.create :ground_picture, :panel_id => @panel.id, :picture_id => @p.id, :z => 2, :updated_at => Time.now + 100
336       pl = GroundPicture.mylist @author
337       pl.should eq [npl, @gp]
338     end
339     it '他人のコマの景色は公開でも含まない' do
340       hpl = FactoryGirl.create :panel, :author_id => @other_author.id, :publish => 1
341       npl = FactoryGirl.create :ground_picture, :panel_id => hpl.id, :picture_id => @p.id
342       pl = GroundPicture.mylist @author
343       pl.should eq [@gp]
344     end
345     it '自分のコマの景色は非公開でも含んでいる' do
346       hpl = FactoryGirl.create :panel, :author_id => @author.id, :publish => 0
347       npl = FactoryGirl.create :ground_picture, :panel_id => hpl.id, :picture_id => @p.id, :z => 2, :updated_at => Time.now + 100
348       pl = GroundPicture.mylist @author
349       pl.should eq [npl, @gp]
350     end
351     context 'DBに5件あって1ページの件数を2件に変えたとして' do
352       before do
353         @gp2 = FactoryGirl.create :ground_picture, :panel_id => @panel.id, :z => 1, :picture_id => @p.id, :updated_at => Time.now + 100
354         @gp3 = FactoryGirl.create :ground_picture, :panel_id => @panel.id, :z => 2, :picture_id => @p.id, :updated_at => Time.now + 200
355         @gp4 = FactoryGirl.create :ground_picture, :panel_id => @panel.id, :z => 3, :picture_id => @p.id, :updated_at => Time.now + 300
356         @gp5 = FactoryGirl.create :ground_picture, :panel_id => @panel.id, :z => 4, :picture_id => @p.id, :updated_at => Time.now + 400
357       end
358       it '通常は2件を返す' do
359         c = GroundPicture.mylist @author, 1, 2
360         c.should have(2).items 
361       end
362       it 'page=1なら末尾2件を返す' do
363         #時系列で並んでいる
364         c = GroundPicture.mylist(@author, 1, 2)
365         c.should eq [@gp5, @gp4]
366       end
367       it 'page=2なら中間2件を返す' do
368         c = GroundPicture.mylist(@author, 2, 2)
369         c.should eq [@gp3, @gp2]
370       end
371       it 'page=3なら先頭1件を返す' do
372         c = GroundPicture.mylist(@author, 3, 2)
373         c.should eq [@gp]
374       end
375     end
376     context 'DBに5件あって1ページの件数を0件に変えたとして' do
377       before do
378         @gp2 = FactoryGirl.create :ground_picture, :panel_id => @panel.id, :z => 1, :picture_id => @p.id, :updated_at => Time.now + 100
379         @gp3 = FactoryGirl.create :ground_picture, :panel_id => @panel.id, :z => 2, :picture_id => @p.id, :updated_at => Time.now + 200
380         @gp4 = FactoryGirl.create :ground_picture, :panel_id => @panel.id, :z => 3, :picture_id => @p.id, :updated_at => Time.now + 300
381         @gp5 = FactoryGirl.create :ground_picture, :panel_id => @panel.id, :z => 4, :picture_id => @p.id, :updated_at => Time.now + 400
382         Author.stub(:default_ground_picture_page_size).and_return(2)
383       end
384       it '通常は全件(5件)を返す' do
385         r = GroundPicture.mylist @author, 5, 0
386         r.should have(5).items 
387       end
388     end
389   end
390   
391   
392 end