OSDN Git Service

Merge branch 'v04' of git.sourceforge.jp:/gitroot/pettanr/pettanr into v04
[pettanr/pettanr.git] / spec / models / panel_picture_spec.rb
1 # -*- encoding: utf-8 -*-
2 require 'spec_helper'
3 #コマ絵
4 describe PanelPicture do
5   before do
6     FactoryGirl.create :admin
7     @user = FactoryGirl.create( :user_yas)
8     @author = @user.author
9     @artist = FactoryGirl.create :artist_yas, :author_id => @author.id
10     @other_user = FactoryGirl.create( :user_yas)
11     @other_author = @other_user.author
12     @other_artist = FactoryGirl.create :artist_yas, :author_id => @other_author.id
13     @sp = FactoryGirl.create :system_picture
14     @lg = FactoryGirl.create :license_group
15     @license = FactoryGirl.create :license, :license_group_id => @lg.id, :system_picture_id => @sp.id
16     @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
17     @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id
18     @rp = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => @op.id, :picture_id => @p.id
19     @panel = FactoryGirl.create :panel, :author_id => @author.id
20   end
21   
22   describe '検証に於いて' do
23     before do
24       @pp = FactoryGirl.build :panel_picture, :panel_id => @panel.id, :picture_id => @p.id,
25         :width => @p.width, :height => @p.height
26     end
27     it 'オーソドックスなデータなら通る' do
28       @pp.save!
29       @pp.should be_valid
30     end
31     
32     context 'panel_idを検証するとき' do
33       #ネストの保存はnilを許可しなければならないので数値チェックだけ
34       it 'テストデータの確認' do
35         @pp.panel_id = @panel.id
36         @pp.should be_valid
37       end
38       it '数値でなければ失敗する' do
39         @pp.panel_id = 'a'
40         @pp.should_not be_valid
41       end
42     end
43     context 'linkを検証するとき' do
44       it 'テストデータの確認' do
45         @pp.link = 'abcdefghi0abcdefghi0abcdefghi0abcdefghi0abcdefghi0'*4
46         @pp.should be_valid
47       end
48       it 'nullでも通る' do
49         @pp.link = ''
50         @pp.should be_valid
51       end
52       it '201文字以上なら失敗する' do
53         @pp.link = 'a'*201
54         @pp.should_not be_valid
55       end
56     end
57     context 'xを検証するとき' do
58       it 'テストデータの確認' do
59         @pp.x = '1'
60         @pp.should be_valid
61       end
62       it 'nullなら失敗する' do
63         @pp.x = nil
64         @pp.should_not be_valid
65       end
66       it '数値でなければ失敗する' do
67         @pp.x = 'a'
68         @pp.should_not be_valid
69       end
70       it '0なら通る' do
71         @pp.x = '0'
72         @pp.should be_valid
73       end
74       it '負でも通る' do
75         @pp.x = -1
76         @pp.should be_valid
77       end
78     end
79     context 'yを検証するとき' do
80       it 'テストデータの確認' do
81         @pp.y = '1'
82         @pp.should be_valid
83       end
84       it 'nullなら失敗する' do
85         @pp.y = nil
86         @pp.should_not be_valid
87       end
88       it '数値でなければ失敗する' do
89         @pp.y = 'a'
90         @pp.should_not be_valid
91       end
92       it '0なら通る' do
93         @pp.y = '0'
94         @pp.should be_valid
95       end
96       it '負でも通る' do
97         @pp.y = -1
98         @pp.should be_valid
99       end
100     end
101     context 'widthを検証するとき' do
102       before do
103         Picture.any_instance.stub(:flag_reverse).with(any_args).and_return(0)
104         Picture.any_instance.stub(:flag_resize).with(any_args).and_return(0)
105         Picture.any_instance.stub(:flag_sync_vh).with(any_args).and_return(0)
106       end
107       it 'テストデータの確認' do
108         @pp.width = 1
109         @pp.should be_valid
110       end
111       it 'nullなら失敗する' do
112         @pp.width = nil
113         @pp.should_not be_valid
114       end
115       it '数値でなければ失敗する' do
116         @pp.width = 'a'
117         @pp.should_not be_valid
118       end
119       it '0なら失敗する' do
120         @pp.width = '0'
121         @pp.should_not be_valid
122       end
123       context  '反転許可のとき' do
124         before do
125           Picture.any_instance.stub(:flag_reverse).with(any_args).and_return(0)
126         end
127         it '負でも通る' do
128           @pp.width = -1
129           @pp.should be_valid
130         end
131       end
132       context  '反転禁止のとき' do
133         before do
134           Picture.any_instance.stub(:flag_reverse).with(any_args).and_return(-1)
135         end
136         it '負なら失敗する' do
137           @pp.width = -1
138           @pp.should_not be_valid
139         end
140       end
141       context  'サイズ変更許可のとき' do
142         before do
143           Picture.any_instance.stub(:flag_resize).with(any_args).and_return(0)
144         end
145         it '実素材のサイズと違っても通る' do
146           @pp.width = @p.width-1
147           @pp.should be_valid
148         end
149       end
150       context  'サイズ変更禁止のとき' do
151         before do
152           Picture.any_instance.stub(:flag_resize).with(any_args).and_return(-1)
153         end
154         it '実素材のサイズと違うなら失敗する' do
155           @pp.width = @p.width-1
156           @pp.should_not be_valid
157         end
158       end
159       context  '縦横比変更許可のとき' do
160         before do
161           Picture.any_instance.stub(:flag_sync_vh).with(any_args).and_return(0)
162         end
163         it '実素材の縦横比と違っても通る' do
164           @pp.width = @p.width / 2
165           @pp.should be_valid
166         end
167       end
168       context  '縦横比変更禁止のとき' do
169         before do
170           Picture.any_instance.stub(:flag_sync_vh).with(any_args).and_return(-1)
171         end
172         it '実素材の縦横比と違うなら失敗する' do
173           @pp.width = @p.width / 2
174           @pp.should_not be_valid
175         end
176       end
177     end
178     context 'heightを検証するとき' do
179       before do
180         Picture.any_instance.stub(:flag_reverse).with(any_args).and_return(0)
181         Picture.any_instance.stub(:flag_resize).with(any_args).and_return(0)
182         Picture.any_instance.stub(:flag_sync_vh).with(any_args).and_return(0)
183       end
184       it 'テストデータの確認' do
185         @pp.height = '1'
186         @pp.should be_valid
187       end
188       it 'nullなら失敗する' do
189         @pp.height = nil
190         @pp.should_not be_valid
191       end
192       it '数値でなければ失敗する' do
193         @pp.height = 'a'
194         @pp.should_not be_valid
195       end
196       it '0なら失敗する' do
197         @pp.height = '0'
198         @pp.should_not be_valid
199       end
200       it '負でも通る' do
201         @pp.height = -1
202         @pp.should be_valid
203       end
204       context  '反転許可のとき' do
205         before do
206           Picture.any_instance.stub(:flag_reverse).with(any_args).and_return(0)
207         end
208         it '負でも通る' do
209           @pp.height = -1
210           @pp.should be_valid
211         end
212       end
213       context  '反転禁止のとき' do
214         before do
215           Picture.any_instance.stub(:flag_reverse).with(any_args).and_return(-1)
216         end
217         it '負なら失敗する' do
218           @pp.height = -1
219           @pp.should_not be_valid
220         end
221       end
222       context  'サイズ変更許可のとき' do
223         before do
224           Picture.any_instance.stub(:flag_resize).with(any_args).and_return(0)
225         end
226         it '実素材のサイズと違っても通る' do
227           @pp.height = @p.height-1
228           @pp.should be_valid
229         end
230       end
231       context  'サイズ変更禁止のとき' do
232         before do
233           Picture.any_instance.stub(:flag_resize).with(any_args).and_return(-1)
234         end
235         it '実素材のサイズと違うなら失敗する' do
236           @pp.height = @p.height-1
237           @pp.should_not be_valid
238         end
239       end
240       context  '縦横比変更許可のとき' do
241         before do
242           Picture.any_instance.stub(:flag_sync_vh).with(any_args).and_return(0)
243         end
244         it '実素材の縦横比と違っても通る' do
245           @pp.height = @p.height / 2
246           @pp.should be_valid
247         end
248       end
249       context  '縦横比変更禁止のとき' do
250         before do
251           Picture.any_instance.stub(:flag_sync_vh).with(any_args).and_return(-1)
252         end
253         it '実素材の縦横比と違うなら失敗する' do
254           @pp.height = @p.height / 2
255           @pp.should_not be_valid
256         end
257       end
258     end
259     context 'zを検証するとき' do
260       it 'テストデータの確認' do
261         @pp.z = 1
262         @pp.should be_valid
263       end
264       it 'nullなら失敗する' do
265         @pp.z = nil
266         @pp.should_not be_valid
267       end
268       it '数値でなければ失敗する' do
269         @pp.z = 'a'
270         @pp.should_not be_valid
271       end
272       it '負なら失敗する' do
273         @pp.z = -1
274         @pp.should_not be_valid
275       end
276       it '負なら失敗する' do
277         @pp.z = 0
278         @pp.should_not be_valid
279       end
280     end
281     context 'tを検証するとき' do
282       it 'テストデータの確認' do
283         @pp.t = 0
284         @pp.should be_valid
285       end
286       it 'nullなら失敗する' do
287         @pp.t = nil
288         @pp.should_not be_valid
289       end
290       it '数値でなければ失敗する' do
291         @pp.t = 'a'
292         @pp.should_not be_valid
293       end
294       it '負なら失敗する' do
295         @pp.t = -1
296         @pp.should_not be_valid
297       end
298     end
299   end
300   describe '一覧取得に於いて' do
301     before do
302       @pp = FactoryGirl.create :panel_picture, :panel_id => @panel.id, :picture_id => @p.id,
303         :width => @p.width, :height => @p.height
304     end
305     context 'page補正について' do
306       it '文字列から数値に変換される' do
307         PanelPicture.page('8').should eq 8
308       end
309       it 'nilの場合は1になる' do
310         PanelPicture.page().should eq 1
311       end
312       it '0以下の場合は1になる' do
313         PanelPicture.page('0').should eq 1
314       end
315     end
316     context 'page_size補正について' do
317       it '文字列から数値に変換される' do
318         PanelPicture.page_size('7').should eq 7
319       end
320       it 'nilの場合はPanelPicture.default_page_sizeになる' do
321         PanelPicture.page_size().should eq PanelPicture.default_page_size
322       end
323       it '0以下の場合はPanelPicture.default_page_sizeになる' do
324         PanelPicture.page_size('0').should eq PanelPicture.default_page_size
325       end
326       it 'PanelPicture.max_page_sizeを超えた場合はPanelPicture.max_page_sizeになる' do
327         PanelPicture.page_size('1000').should eq PanelPicture.max_page_size
328       end
329     end
330     it 'リストを返す' do
331       pl = PanelPicture.list
332       pl.should eq [@pp]
333     end
334     it '時系列で並んでいる' do
335       npl = FactoryGirl.create :panel_picture, :panel_id => @panel.id, :t => 1, :picture_id => @p.id,
336         :width => @p.width, :height => @p.height, :updated_at => Time.now + 100
337       pl = PanelPicture.list
338       pl.should eq [npl, @pp]
339     end
340     it '非公開のコマは含まない' do
341       hpl = FactoryGirl.create :panel, :author_id => @author.id, :publish => 0
342       npl = FactoryGirl.create :panel_picture, :panel_id => hpl.id, :picture_id => @p.id,
343         :width => @p.width, :height => @p.height
344       pl = PanelPicture.list
345       pl.should eq [@pp]
346     end
347     context 'DBに5件あって1ページの件数を2件に変えたとして' do
348       before do
349         @npl2 = FactoryGirl.create :panel_picture, :panel_id => @panel.id, :t => 1, :picture_id => @p.id,
350         :width => @p.width, :height => @p.height, :updated_at => Time.now + 100
351         @npl3 = FactoryGirl.create :panel_picture, :panel_id => @panel.id, :t => 2, :picture_id => @p.id,
352         :width => @p.width, :height => @p.height, :updated_at => Time.now + 200
353         @npl4 = FactoryGirl.create :panel_picture, :panel_id => @panel.id, :t => 3, :picture_id => @p.id,
354         :width => @p.width, :height => @p.height, :updated_at => Time.now + 300
355         @npl5 = FactoryGirl.create :panel_picture, :panel_id => @panel.id, :t => 4, :picture_id => @p.id,
356         :width => @p.width, :height => @p.height, :updated_at => Time.now + 400
357         PanelPicture.stub(:default_page_size).and_return(2)
358       end
359       it '通常は2件を返す' do
360         pl = PanelPicture.list
361         pl.should have(2).items 
362       end
363       it 'page=1なら末尾2件を返す' do
364         #時系列で並んでいる
365         pl = PanelPicture.list( {}, 1)
366         pl.should eq [@npl5, @npl4]
367       end
368       it 'page=2なら中間2件を返す' do
369         pl = PanelPicture.list({}, 2)
370         pl.should eq [@npl3, @npl2]
371       end
372       it 'page=3なら先頭1件を返す' do
373         pl = PanelPicture.list({}, 3)
374         pl.should eq [@pp]
375       end
376     end
377   end
378   describe 'list関連テーブルプションに於いて' do
379     it 'includeキーを含んでいる' do
380       r = PanelPicture.list_opt
381       r.has_key?(:include).should be_true
382     end
383     it '2つの項目を含んでいる' do
384       r = PanelPicture.list_opt[:include]
385       r.should have(2).items
386     end
387     it 'コマを含んでいる' do
388       r = PanelPicture.list_opt[:include]
389       r.has_key?(:panel).should be_true
390     end
391       it 'コマは作家を含んでいる' do
392         r = PanelPicture.list_opt[:include]
393         r[:panel].has_key?(:author).should be_true
394       end
395     it '実素材を含んでいる' do
396       r = PanelPicture.list_opt[:include]
397       r.has_key?(:picture).should be_true
398     end
399       it '実素材は絵師を含んでいる' do
400         r = PanelPicture.list_opt[:include]
401         r[:picture].has_key?(:artist).should be_true
402       end
403       it '実素材はライセンスを含んでいる' do
404         r = PanelPicture.list_opt[:include]
405         r[:picture].has_key?(:license).should be_true
406       end
407   end
408   describe 'json一覧出力オプションに於いて' do
409     it 'includeキーを含んでいる' do
410       r = PanelPicture.list_json_opt
411       r.has_key?(:include).should be_true
412     end
413     it '2つの項目を含んでいる' do
414       r = PanelPicture.list_json_opt[:include]
415       r.should have(2).items
416     end
417     it 'コマを含んでいる' do
418       r = PanelPicture.list_json_opt[:include]
419       r.has_key?(:panel).should be_true
420     end
421       it 'コマは作家を含んでいる' do
422         r = PanelPicture.list_json_opt[:include]
423         r[:panel].has_key?(:author).should be_true
424       end
425     it '実素材を含んでいる' do
426       r = PanelPicture.list_json_opt[:include]
427       r.has_key?(:picture).should be_true
428     end
429       it '実素材は絵師を含んでいる' do
430         r = PanelPicture.list_json_opt[:include]
431         r[:picture].has_key?(:artist).should be_true
432       end
433       it '実素材はライセンスを含んでいる' do
434         r = PanelPicture.list_json_opt[:include]
435         r[:picture].has_key?(:license).should be_true
436       end
437   end
438   
439   describe '自分のコマ一覧取得に於いて' do
440     before do
441       @pp = FactoryGirl.create :panel_picture, :panel_id => @panel.id, :picture_id => @p.id,
442         :width => @p.width, :height => @p.height
443     end
444     it 'リストを返す' do
445       pl = PanelPicture.mylist @author
446       pl.should eq [@pp]
447     end
448     it '時系列で並んでいる' do
449       npl = FactoryGirl.create :panel_picture, :panel_id => @panel.id, :t => 1, :picture_id => @p.id,
450         :width => @p.width, :height => @p.height, :updated_at => Time.now + 100
451       pl = PanelPicture.mylist @author
452       pl.should eq [npl, @pp]
453     end
454     it '他人のコマは公開でも含まない' do
455       hpl = FactoryGirl.create :panel, :author_id => @other_author.id, :publish => 1
456       npl = FactoryGirl.create :panel_picture, :panel_id => hpl.id, :picture_id => @p.id,
457         :width => @p.width, :height => @p.height
458       pl = PanelPicture.mylist @author
459       pl.should eq [@pp]
460     end
461   end
462   
463 end