OSDN Git Service

Merge branch 'v04' of git.sourceforge.jp:/gitroot/pettanr/pettanr into v04
[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       end
181       it '通常は全件(5件)を返す' do
182         r = GroundPicture.list 5, 0
183         r.should have(5).items 
184       end
185     end
186   end
187   describe 'list関連テーブルプションに於いて' do
188     it 'includeキーを含んでいる' do
189       r = GroundPicture.list_opt
190       r.has_key?(:include).should be_true
191     end
192     it '2つの項目を含んでいる' do
193       r = GroundPicture.list_opt[:include]
194       r.should have(2).items
195     end
196     it 'コマを含んでいる' do
197       r = GroundPicture.list_opt[:include]
198       r.has_key?(:panel).should be_true
199     end
200       it 'コマは作家を含んでいる' do
201         r = GroundPicture.list_opt[:include]
202         r[:panel].has_key?(:author).should be_true
203       end
204     it '実素材を含んでいる' do
205       r = GroundPicture.list_opt[:include]
206       r.has_key?(:picture).should be_true
207     end
208       it '実素材は絵師を含んでいる' do
209         r = GroundPicture.list_opt[:include]
210         r[:picture].has_key?(:artist).should be_true
211       end
212       it '実素材はライセンスを含んでいる' do
213         r = GroundPicture.list_opt[:include]
214         r[:picture].has_key?(:license).should be_true
215       end
216   end
217   describe 'json一覧出力オプションに於いて' do
218     it 'includeキーを含んでいる' do
219       r = GroundPicture.list_json_opt
220       r.has_key?(:include).should be_true
221     end
222     it '2つの項目を含んでいる' do
223       r = GroundPicture.list_json_opt[:include]
224       r.should have(2).items
225     end
226     it 'コマを含んでいる' do
227       r = GroundPicture.list_json_opt[:include]
228       r.has_key?(:panel).should be_true
229     end
230       it 'コマは作家を含んでいる' do
231         r = GroundPicture.list_json_opt[:include]
232         r[:panel].has_key?(:author).should be_true
233       end
234     it '実素材を含んでいる' do
235       r = GroundPicture.list_json_opt[:include]
236       r.has_key?(:picture).should be_true
237     end
238       it '実素材は絵師を含んでいる' do
239         r = GroundPicture.list_json_opt[:include]
240         r[:picture].has_key?(:artist).should be_true
241       end
242       it '実素材はライセンスを含んでいる' do
243         r = GroundPicture.list_json_opt[:include]
244         r[:picture].has_key?(:license).should be_true
245       end
246   end
247   
248   describe '自分のコマで使った景色画像一覧取得に於いて' do
249     before do
250       @gp = FactoryGirl.create :ground_picture, :panel_id => @panel.id, :picture_id => @p.id
251     end
252     context 'つつがなく終わるとき' do\r
253       it '一覧取得オプションを利用している' do\r
254         GroundPicture.stub(:list_opt).with(any_args).and_return({:include => :panel})\r
255         GroundPicture.should_receive(:list_opt).with(any_args).exactly(1)\r
256         r = GroundPicture.mylist @author
257       end\r
258     end\r
259     it 'リストを返す' do
260       pl = GroundPicture.mylist @author
261       pl.should eq [@gp]
262     end
263     it '時系列で並んでいる' do
264       npl = FactoryGirl.create :ground_picture, :panel_id => @panel.id, :picture_id => @p.id, :z => 2, :updated_at => Time.now + 100
265       pl = GroundPicture.mylist @author
266       pl.should eq [npl, @gp]
267     end
268     it '他人のコマの景色は公開でも含まない' do
269       hpl = FactoryGirl.create :panel, :author_id => @other_author.id, :publish => 1
270       npl = FactoryGirl.create :ground_picture, :panel_id => hpl.id, :picture_id => @p.id
271       pl = GroundPicture.mylist @author
272       pl.should eq [@gp]
273     end
274     it '自分のコマの景色は非公開でも含んでいる' do
275       hpl = FactoryGirl.create :panel, :author_id => @author.id, :publish => 0
276       npl = FactoryGirl.create :ground_picture, :panel_id => hpl.id, :picture_id => @p.id, :z => 2, :updated_at => Time.now + 100
277       pl = GroundPicture.mylist @author
278       pl.should eq [npl, @gp]
279     end
280     context 'DBに5件あって1ページの件数を2件に変えたとして' do
281       before do
282         @gp2 = FactoryGirl.create :ground_picture, :panel_id => @panel.id, :z => 1, :picture_id => @p.id, :updated_at => Time.now + 100
283         @gp3 = FactoryGirl.create :ground_picture, :panel_id => @panel.id, :z => 2, :picture_id => @p.id, :updated_at => Time.now + 200
284         @gp4 = FactoryGirl.create :ground_picture, :panel_id => @panel.id, :z => 3, :picture_id => @p.id, :updated_at => Time.now + 300
285         @gp5 = FactoryGirl.create :ground_picture, :panel_id => @panel.id, :z => 4, :picture_id => @p.id, :updated_at => Time.now + 400
286       end
287       it '通常は2件を返す' do
288         c = GroundPicture.mylist @author, 1, 2
289         c.should have(2).items 
290       end
291       it 'page=1なら末尾2件を返す' do
292         #時系列で並んでいる
293         c = GroundPicture.mylist(@author, 1, 2)
294         c.should eq [@gp5, @gp4]
295       end
296       it 'page=2なら中間2件を返す' do
297         c = GroundPicture.mylist(@author, 2, 2)
298         c.should eq [@gp3, @gp2]
299       end
300       it 'page=3なら先頭1件を返す' do
301         c = GroundPicture.mylist(@author, 3, 2)
302         c.should eq [@gp]
303       end
304     end
305     context 'DBに5件あって1ページの件数を0件に変えたとして' do
306       before do
307         @gp2 = FactoryGirl.create :ground_picture, :panel_id => @panel.id, :z => 1, :picture_id => @p.id, :updated_at => Time.now + 100
308         @gp3 = FactoryGirl.create :ground_picture, :panel_id => @panel.id, :z => 2, :picture_id => @p.id, :updated_at => Time.now + 200
309         @gp4 = FactoryGirl.create :ground_picture, :panel_id => @panel.id, :z => 3, :picture_id => @p.id, :updated_at => Time.now + 300
310         @gp5 = FactoryGirl.create :ground_picture, :panel_id => @panel.id, :z => 4, :picture_id => @p.id, :updated_at => Time.now + 400
311       end
312       it '通常は全件(5件)を返す' do
313         r = GroundPicture.mylist @author, 5, 0
314         r.should have(5).items 
315       end
316     end
317   end
318   
319   
320 end