OSDN Git Service

5bdc074bdb298fc93ea5bb2b9fdda505885f190d
[pettanr/pettanr.git] / spec / models / original_picture_spec.rb
1 # -*- encoding: utf-8 -*-
2 require 'spec_helper'
3
4 describe OriginalPicture do
5   before do
6     Factory :admin
7     @user = Factory( :user_yas)
8     @author = @user.author
9     @artist = Factory :artist_yas, :author_id => @author.id
10     @other_user = Factory( :user_yas)
11     @other_author = @other_user.author
12     @other_artist = Factory :artist_yas, :author_id => @other_author.id
13     @license = Factory :license
14   end
15   
16   describe '検証に於いて' do
17     before do
18     end
19     
20     it 'オーソドックスなデータなら通る' do
21       @op = Factory.build :original_picture, :artist_id => @artist.id
22       @op.should be_valid
23     end
24     
25     context 'extを検証するとき' do
26       it 'nullなら失敗する' do
27         @op = Factory.build :original_picture, :artist_id => @artist.id, :ext => ''
28         @op.should_not be_valid
29       end
30       it '5文字以上なら失敗する' do
31         @op = Factory.build :original_picture, :artist_id => @artist.id, :ext => 'a'*5
32         @op.should_not be_valid
33       end
34       it 'png,gif,jpeg以外なら失敗する' do
35         @op = Factory.build :original_picture, :artist_id => @artist.id, :ext => 'bmp'
36         @op.should_not be_valid
37       end
38     end
39     context 'widthを検証するとき' do
40       it 'nullなら失敗する' do
41         @op = Factory.build :original_picture, :artist_id => @artist.id, :width => nil
42         @op.should_not be_valid
43       end
44       it '数値でなければ失敗する' do
45         @op = Factory.build :original_picture, :artist_id => @artist.id, :width => 'a'
46         @op.should_not be_valid
47       end
48       it '0なら失敗する' do
49         @op = Factory.build :original_picture, :artist_id => @artist.id, :width => '0'
50         @op.should_not be_valid
51       end
52       it '負でも失敗する' do
53         @op = Factory.build :original_picture, :artist_id => @artist.id, :width => -1
54         @op.should_not be_valid
55       end
56       it '正なら通る' do
57         @op = Factory.build :original_picture, :artist_id => @artist.id, :width => 1
58         @op.should be_valid
59       end
60     end
61     context 'heightを検証するとき' do
62       it 'nullなら失敗する' do
63         @op = Factory.build :original_picture, :artist_id => @artist.id, :height => nil
64         @op.should_not be_valid
65       end
66       it '数値でなければ失敗する' do
67         @op = Factory.build :original_picture, :artist_id => @artist.id, :height => 'a'
68         @op.should_not be_valid
69       end
70       it '0なら失敗する' do
71         @op = Factory.build :original_picture, :artist_id => @artist.id, :height => '0'
72         @op.should_not be_valid
73       end
74       it '負でも失敗する' do
75         @op = Factory.build :original_picture, :artist_id => @artist.id, :height => -1
76         @op.should_not be_valid
77       end
78       it '正なら通る' do
79         @op = Factory.build :original_picture, :artist_id => @artist.id, :height => 1
80         @op.should be_valid
81       end
82     end
83     context 'filesizeを検証するとき' do
84       it 'nullなら失敗する' do
85         @op = Factory.build :original_picture, :artist_id => @artist.id, :filesize => nil
86         @op.should_not be_valid
87       end
88       it '数値でなければ失敗する' do
89         @op = Factory.build :original_picture, :artist_id => @artist.id, :filesize => 'a'
90         @op.should_not be_valid
91       end
92       it '負なら失敗する' do
93         @op = Factory.build :original_picture, :artist_id => @artist.id, :filesize => '-1'
94         @op.should_not be_valid
95       end
96       it '2MB以上なら失敗する' do
97         @op = Factory.build :original_picture, :artist_id => @artist.id, :filesize => 2000000+1
98         @op.should_not be_valid
99       end
100     end
101     context 'artist_idを検証するとき' do
102       it 'nullなら失敗する' do
103         @op = Factory.build :original_picture, :artist_id => nil
104         @op.should_not be_valid
105       end
106       it '数値でなければ失敗する' do
107         @op = Factory.build :original_picture, :artist_id => 'a'
108         @op.should_not be_valid
109       end
110       it '存在する絵師でなければ失敗する' do
111         @op = Factory.build :original_picture, :artist_id => 0
112         @op.should_not be_valid
113       end
114     end
115     context 'license_idを検証するとき' do
116       it 'nullなら失敗する' do
117         @op = Factory.build :original_picture, :artist_id => @artist.id, :license_id => nil
118         @op.should_not be_valid
119       end
120       it '数値でなければ失敗する' do
121         @op = Factory.build :original_picture, :artist_id => 'a', :license_id => 'a'
122         @op.should_not be_valid
123       end
124       it '存在する絵師でなければ失敗する' do
125         @op = Factory.build :original_picture, :artist_id => 0, :license_id => 0
126         @op.should_not be_valid
127       end
128     end
129   end
130   
131   describe 'データ補充に於いて' do
132     before do
133     end
134     
135   end
136   
137   describe '作者判定に於いて' do
138     before do
139     end
140     it '自作の原画ならyes' do
141       op = Factory :original_picture, :artist_id => @artist.id
142       op.own?(@author).should == true
143     end
144     it '他人のならno' do
145       op = Factory :original_picture, :artist_id => @other_artist.id
146       op.own?(@author).should == false
147     end
148     it '作家が不明ならno' do
149       op = Factory :original_picture, :artist_id => @artist.id
150       op.own?(nil).should == false
151     end
152     it '作家が絵師でないならno' do
153       other_user = Factory( :user_yas)
154       op = Factory :original_picture, :artist_id => @artist.id
155       op.own?(@other_author).should == false
156     end
157   end
158   describe '単体取得に於いて' do
159     before do
160       @op = Factory :original_picture, :artist_id => @artist.id
161     end
162     it '指定のコミックを返す' do
163       pic = OriginalPicture.show @op.id, @author
164       pic.should eq @op
165     end
166     context '関連テーブルオプションがないとき' do
167       it 'ライセンスデータだけを含んでいる' do
168         r = OriginalPicture.show_include_opt
169         r.should eq [:license]
170       end
171     end
172     context '関連テーブルオプションで絵師を含ませたとき' do
173       it 'ライセンスデータと作者データを含んでいる' do
174         r = OriginalPicture.show_include_opt(:include => :artist)
175         r.should eq [:license, :artist]
176       end
177     end
178   end
179   describe '一覧取得に於いて' do
180     before do
181       @op = Factory :original_picture, :artist_id => @artist.id
182     end
183     context 'page補正について' do
184       it '文字列から数値に変換される' do
185         OriginalPicture.page('8').should eq 8
186       end
187       it 'nilの場合は1になる' do
188         OriginalPicture.page().should eq 1
189       end
190       it '0以下の場合は1になる' do
191         OriginalPicture.page('0').should eq 1
192       end
193     end
194     context 'page_size補正について' do
195       it '文字列から数値に変換される' do
196         OriginalPicture.page_size('7').should eq 7
197       end
198       it 'nilの場合はOriginalPicture.default_page_sizeになる' do
199         OriginalPicture.page_size().should eq OriginalPicture.default_page_size
200       end
201       it '0以下の場合はOriginalPicture.default_page_sizeになる' do
202         OriginalPicture.page_size('0').should eq OriginalPicture.default_page_size
203       end
204       it 'OriginalPicture.max_page_sizeを超えた場合はOriginalPicture.max_page_sizeになる' do
205         OriginalPicture.page_size('1000').should eq OriginalPicture.max_page_size
206       end
207     end
208     it 'リストを返す' do
209       pic = OriginalPicture.list @artist.id
210       pic.should eq [@op]
211     end
212     it '他人の原画は含んでいない' do
213       Factory :original_picture, :artist_id => @other_artist.id
214       pic = OriginalPicture.list @artist.id
215       pic.should eq [@op]
216     end
217     it '時系列で並んでいる' do
218       newpic = Factory :original_picture, :artist_id => @artist.id
219       pic = OriginalPicture.list @artist.id
220       pic.should eq [newpic, @op]
221     end
222     context 'DBに5件あって1ページの件数を2件に変えたとして' do
223       before do
224         @op2 = Factory :original_picture, :artist_id => @artist.id
225         @op3 = Factory :original_picture, :artist_id => @artist.id
226         @op4 = Factory :original_picture, :artist_id => @artist.id
227         @op5 = Factory :original_picture, :artist_id => @artist.id
228         OriginalPicture.stub(:default_page_size).and_return(2)
229       end
230       it '通常は2件を返す' do
231         pic = OriginalPicture.list @artist.id
232         pic.should have(2).items 
233       end
234       it 'page=1なら末尾2件を返す' do
235         #時系列で並んでいる
236         pic = OriginalPicture.list( @artist.id, {}, 1)
237         pic.should eq [@op5, @op4]
238       end
239       it 'page=2なら中間2件を返す' do
240         pic = OriginalPicture.list( @artist.id, {}, 2)
241         pic.should eq [@op3, @op2]
242       end
243       it 'page=3なら先頭1件を返す' do
244         pic = OriginalPicture.list( @artist.id, {}, 3)
245         pic.should eq [@op]
246       end
247     end
248   end
249   describe 'RMagick変換に於いて' do
250     before do
251       @op = Factory.build :original_picture, :artist_id => @artist.id
252     end
253     context 'つつがなく終わるとき' do
254       it '画像データをオブジェクト化している' do
255         Magick::Image.stub(:from_blob).and_return(['mgkobj'])
256         @op.data_to_mgk('mgkbin').should eq 'mgkobj'
257       end
258     end
259     context '失敗するとき' do
260       it 'Falseを返す' do
261         Magick::Image.should_receive(:from_blob).with(any_args).and_raise('StandardError')
262         @op.data_to_mgk('mgkbin').should be_false
263       end
264       it '全体エラーがセットされている' do
265         Magick::Image.should_receive(:from_blob).with(any_args).and_raise('StandardError')
266         lambda {
267           @op.data_to_mgk('mgkbin')
268         }.should change(@op.errors[:base], :count)
269       end
270     end
271   end
272   describe '作成・更新に於いて' do
273     before do
274       @op = Factory.build :original_picture, :artist_id => @artist.id
275       class Mgk ; class Image ; end ; end
276       @filesize = 76543
277       Mgk::Image.stub(:from_blob).with(any_args).and_return([Mgk.new])
278       Mgk.any_instance.stub(:format).with(any_args).and_return('png')
279       Mgk.any_instance.stub(:rows).with(any_args).and_return(200)
280       Mgk.any_instance.stub(:columns).with(any_args).and_return(100)
281       Mgk.any_instance.stub(:filesize).with(any_args).and_return(@filesize)
282       Mgk.any_instance.stub(:to_blob).with(any_args).and_return('data')
283       PictureIO::LocalPicture.any_instance.stub(:delete).with(any_args).and_return(true)
284       
285       ResourcePicture.stub(:update_picture).with(any_args).and_return(
286         Factory.build :resource_picture, :artist_id => @artist.id
287       )
288     end
289     context '事前チェック' do
290       before do
291         OriginalPicture.any_instance.stub(:data_to_mgk).with(any_args).and_return(Mgk.new)
292         OriginalPicture.any_instance.stub(:save).with(any_args).and_return(true)\r
293         PictureIO::LocalPicture.any_instance.stub(:put).with(any_args).and_return(true)
294         ResourcePicture.any_instance.stub(:store).with(any_args).and_return(true)
295       end
296       it 'RMagick変換を依頼している' do
297         OriginalPicture.any_instance.should_receive(:data_to_mgk).exactly(1)
298         @op.store 'bindata', @artist, @license.id\r
299       end
300       it '自身に属性をセットしている' do
301         OriginalPicture.any_instance.should_receive(:attributes=).exactly(1)
302         @op.store 'bindata', @artist, @license.id\r
303       end
304       it '自身が保存されている' do
305         OriginalPicture.any_instance.should_receive(:save).exactly(1)\r
306         @op.store 'bindata', @artist, @license.id\r
307       end
308       it 'PictureIoに画像データの保存を依頼している' do
309         PictureIO::LocalPicture.any_instance.should_receive(:put).with(any_args).exactly(1)
310         @op.store 'bindata', @artist, @license.id\r
311       end
312       it '素材モデルに対象素材を問い合わせている' do
313         ResourcePicture.should_receive(:update_picture).with(any_args).exactly(1)
314         @op.store 'bindata', @artist, @license.id\r
315       end
316       it '対象素材オブジェクトに保存を依頼している' do
317         ResourcePicture.any_instance.should_receive(:store).with(any_args).exactly(1)
318         @op.store 'bindata', @artist, @license.id\r
319       end
320     end
321     context 'つつがなく終わるとき' do
322       before do
323         OriginalPicture.any_instance.stub(:data_to_mgk).with(any_args).and_return(Mgk.new)
324 #        OriginalPicture.any_instance.stub(:save).with(any_args).and_return(true)\r
325         PictureIO::LocalPicture.any_instance.stub(:put).with(any_args).and_return(true)
326         ResourcePicture.any_instance.stub(:store).with(any_args).and_return(true)
327       end
328       it '自身に属性をセットしている' do
329         lambda {
330           @op.store 'bindata', @artist, @license.id\r
331         }.should change @op, :filesize
332       end
333       it '原画モデルが作成されている' do
334         lambda {
335           @op.store 'bindata', @artist, @license.id
336         }.should change OriginalPicture, :count
337       end
338       it '原画が保存されている' do
339         @op.store 'bindata', @artist, @license.id
340         OriginalPicture.find(@op).should_not be_nil\r
341       end
342       it 'Trueを返す' do
343         @op.store('bindata', @artist, @license.id).should eq true\r
344       end
345     end
346     
347     context 'RMagick変換が失敗したとき' do
348       before do
349         OriginalPicture.any_instance.stub(:data_to_mgk).with(any_args).and_return(false)
350       end
351       it 'falseを返す' do
352         @op.store('bindata', @artist, @license.id).should be_false\r
353       end
354       it '自身の保存は呼ばれていない' do
355         OriginalPicture.any_instance.should_not_receive(:save)\r
356       end
357     end
358     context '自身の保存に失敗したとき' do
359       before do
360         OriginalPicture.any_instance.stub(:data_to_mgk).with(any_args).and_return(Mgk.new)
361         OriginalPicture.any_instance.stub(:save).with(any_args).and_return(false)\r
362       end
363       it 'falseを返す' do
364         @op.store('bindata', @artist, @license.id).should be_false\r
365       end
366       it '更新されていない' do
367         @op.store('bindata', @artist, @license.id)
368         @op.should be_a_new OriginalPicture\r
369       end
370       it '原画の保存は呼ばれていない' do
371         PictureIO::LocalPicture.any_instance.should_not_receive(:put)\r
372       end
373     end
374     context '原画の保存に失敗したとき' do
375       before do
376         OriginalPicture.any_instance.stub(:data_to_mgk).with(any_args).and_return(Mgk.new)
377         OriginalPicture.any_instance.stub(:save).with(any_args).and_return(true)\r
378         PictureIO::LocalPicture.any_instance.stub(:put).with(any_args).and_return(false)
379       end
380       it 'falseを返す' do
381         @op.store('bindata', @artist, @license.id).should be_false\r
382       end
383       it '更新されていない' do
384         @op.store('bindata', @artist, @license.id)\r
385         @op.should be_a_new OriginalPicture\r
386       end
387       it '対象素材オブジェクトの保存は呼ばれていない' do
388         ResourcePicture.any_instance.should_not_receive(:save)\r
389         @op.store('bindata', @artist, @license.id)\r
390       end
391       it '全体エラーメッセージがセットされている' do
392         lambda {
393           @op.store('bindata', @artist, @license.id)\r
394         }.should change(@op.errors[:base], :count)
395       end
396     end
397     context '対象素材オブジェクトの保存に失敗したとき' do
398       before do
399         OriginalPicture.any_instance.stub(:data_to_mgk).with(any_args).and_return(Mgk.new)
400         OriginalPicture.any_instance.stub(:save).with(any_args).and_return(true)\r
401         PictureIO::LocalPicture.any_instance.stub(:put).with(any_args).and_return(true)
402         ResourcePicture.any_instance.stub(:store).with(any_args).and_return(false)
403       end
404       it 'falseを返す' do
405         @op.store('bindata', @artist, @license.id).should be_false\r
406       end
407       it '更新されていない' do
408         @op.store('bindata', @artist, @license.id)\r
409         @op.should be_a_new OriginalPicture\r
410       end
411       it '全体エラーメッセージがセットされている' do
412         lambda {
413           @op.store('bindata', @artist, @license.id)\r
414         }.should change(@op.errors[:base], :count)
415       end
416     end
417   end
418 end