OSDN Git Service

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