1 # -*- encoding: utf-8 -*-
4 describe OriginalPicture do
7 @user = Factory( :user_yas)
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
20 it 'オーソドックスなデータなら通る' do
21 @op = Factory.build :original_picture, :artist_id => @artist.id
25 context 'extを検証するとき' do
27 @op = Factory.build :original_picture, :artist_id => @artist.id, :ext => ''
28 @op.should_not be_valid
31 @op = Factory.build :original_picture, :artist_id => @artist.id, :ext => 'a'*5
32 @op.should_not be_valid
34 it 'png,gif,jpeg以外なら失敗する' do
35 @op = Factory.build :original_picture, :artist_id => @artist.id, :ext => 'bmp'
36 @op.should_not be_valid
39 context 'widthを検証するとき' do
41 @op = Factory.build :original_picture, :artist_id => @artist.id, :width => nil
42 @op.should_not be_valid
45 @op = Factory.build :original_picture, :artist_id => @artist.id, :width => 'a'
46 @op.should_not be_valid
49 @op = Factory.build :original_picture, :artist_id => @artist.id, :width => '0'
50 @op.should_not be_valid
53 @op = Factory.build :original_picture, :artist_id => @artist.id, :width => -1
54 @op.should_not be_valid
57 @op = Factory.build :original_picture, :artist_id => @artist.id, :width => 1
61 context 'heightを検証するとき' do
63 @op = Factory.build :original_picture, :artist_id => @artist.id, :height => nil
64 @op.should_not be_valid
67 @op = Factory.build :original_picture, :artist_id => @artist.id, :height => 'a'
68 @op.should_not be_valid
71 @op = Factory.build :original_picture, :artist_id => @artist.id, :height => '0'
72 @op.should_not be_valid
75 @op = Factory.build :original_picture, :artist_id => @artist.id, :height => -1
76 @op.should_not be_valid
79 @op = Factory.build :original_picture, :artist_id => @artist.id, :height => 1
83 context 'filesizeを検証するとき' do
85 @op = Factory.build :original_picture, :artist_id => @artist.id, :filesize => nil
86 @op.should_not be_valid
89 @op = Factory.build :original_picture, :artist_id => @artist.id, :filesize => 'a'
90 @op.should_not be_valid
93 @op = Factory.build :original_picture, :artist_id => @artist.id, :filesize => '-1'
94 @op.should_not be_valid
97 @op = Factory.build :original_picture, :artist_id => @artist.id, :filesize => 2000000+1
98 @op.should_not be_valid
101 context 'artist_idを検証するとき' do
103 @op = Factory.build :original_picture, :artist_id => nil
104 @op.should_not be_valid
107 @op = Factory.build :original_picture, :artist_id => 'a'
108 @op.should_not be_valid
110 it '存在する絵師でなければ失敗する' do
111 @op = Factory.build :original_picture, :artist_id => 0
112 @op.should_not be_valid
115 context 'license_idを検証するとき' do
117 @op = Factory.build :original_picture, :artist_id => @artist.id, :license_id => nil
118 @op.should_not be_valid
121 @op = Factory.build :original_picture, :artist_id => 'a', :license_id => 'a'
122 @op.should_not be_valid
124 it '存在する絵師でなければ失敗する' do
125 @op = Factory.build :original_picture, :artist_id => 0, :license_id => 0
126 @op.should_not be_valid
131 describe 'データ補充に於いて' do
137 describe '作者判定に於いて' do
141 op = Factory :original_picture, :artist_id => @artist.id
142 op.own?(@author).should == true
145 op = Factory :original_picture, :artist_id => @other_artist.id
146 op.own?(@author).should == false
149 op = Factory :original_picture, :artist_id => @artist.id
150 op.own?(nil).should == false
153 other_user = Factory( :user_yas)
154 op = Factory :original_picture, :artist_id => @artist.id
155 op.own?(@other_author).should == false
158 describe '単体取得に於いて' do
160 @op = Factory :original_picture, :artist_id => @artist.id
163 pic = OriginalPicture.show @op.id, @author
166 context '関連テーブルオプションがないとき' do
167 it 'ライセンスデータだけを含んでいる' do
168 r = OriginalPicture.show_include_opt
169 r.should eq [:license]
172 context '関連テーブルオプションで絵師を含ませたとき' do
173 it 'ライセンスデータと作者データを含んでいる' do
174 r = OriginalPicture.show_include_opt(:include => :artist)
175 r.should eq [:license, :artist]
179 describe '一覧取得に於いて' do
181 @op = Factory :original_picture, :artist_id => @artist.id
183 context 'page補正について' do
184 it '文字列から数値に変換される' do
185 OriginalPicture.page('8').should eq 8
188 OriginalPicture.page().should eq 1
191 OriginalPicture.page('0').should eq 1
194 context 'page_size補正について' do
195 it '文字列から数値に変換される' do
196 OriginalPicture.page_size('7').should eq 7
198 it 'nilの場合はOriginalPicture.default_page_sizeになる' do
199 OriginalPicture.page_size().should eq OriginalPicture.default_page_size
201 it '0以下の場合はOriginalPicture.default_page_sizeになる' do
202 OriginalPicture.page_size('0').should eq OriginalPicture.default_page_size
204 it 'OriginalPicture.max_page_sizeを超えた場合はOriginalPicture.max_page_sizeになる' do
205 OriginalPicture.page_size('1000').should eq OriginalPicture.max_page_size
209 pic = OriginalPicture.list @artist.id
213 Factory :original_picture, :artist_id => @other_artist.id
214 pic = OriginalPicture.list @artist.id
218 newpic = Factory :original_picture, :artist_id => @artist.id
219 pic = OriginalPicture.list @artist.id
220 pic.should eq [newpic, @op]
222 context 'DBに5件あって1ページの件数を2件に変えたとして' 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)
231 pic = OriginalPicture.list @artist.id
232 pic.should have(2).items
234 it 'page=1なら末尾2件を返す' do
236 pic = OriginalPicture.list( @artist.id, {}, 1)
237 pic.should eq [@op5, @op4]
239 it 'page=2なら中間2件を返す' do
240 pic = OriginalPicture.list( @artist.id, {}, 2)
241 pic.should eq [@op3, @op2]
243 it 'page=3なら先頭1件を返す' do
244 pic = OriginalPicture.list( @artist.id, {}, 3)
249 describe 'RMagick変換に於いて' do
251 @op = Factory.build :original_picture, :artist_id => @artist.id
253 context 'つつがなく終わるとき' do
254 it '画像データをオブジェクト化している' do
255 Magick::Image.stub(:from_blob).and_return(['mgkobj'])
256 @op.data_to_mgk('mgkbin').should eq 'mgkobj'
261 Magick::Image.should_receive(:from_blob).with(any_args).and_raise('StandardError')
262 @op.data_to_mgk('mgkbin').should be_false
264 it '全体エラーがセットされている' do
265 Magick::Image.should_receive(:from_blob).with(any_args).and_raise('StandardError')
267 @op.data_to_mgk('mgkbin')
268 }.should change(@op.errors[:base], :count)
272 describe '作成・更新に於いて' do
274 @op = Factory.build :original_picture, :artist_id => @artist.id
275 class Mgk ; class Image ; end ; end
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)
285 ResourcePicture.stub(:update_picture).with(any_args).and_return(
286 Factory.build :resource_picture, :artist_id => @artist.id
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)
296 it 'RMagick変換を依頼している' do
297 OriginalPicture.any_instance.should_receive(:data_to_mgk).exactly(1)
298 @op.store 'bindata', @artist, @license.id
\r
300 it '自身に属性をセットしている' do
301 OriginalPicture.any_instance.should_receive(:attributes=).exactly(1)
302 @op.store 'bindata', @artist, @license.id
\r
305 OriginalPicture.any_instance.should_receive(:save).exactly(1)
\r
306 @op.store 'bindata', @artist, @license.id
\r
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
312 it '素材モデルに対象素材を問い合わせている' do
313 ResourcePicture.should_receive(:update_picture).with(any_args).exactly(1)
314 @op.store 'bindata', @artist, @license.id
\r
316 it '対象素材オブジェクトに保存を依頼している' do
317 ResourcePicture.any_instance.should_receive(:store).with(any_args).exactly(1)
318 @op.store 'bindata', @artist, @license.id
\r
321 context 'つつがなく終わるとき' 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)
328 it '自身に属性をセットしている' do
330 @op.store 'bindata', @artist, @license.id
\r
331 }.should change @op, :filesize
333 it '原画モデルが作成されている' do
335 @op.store 'bindata', @artist, @license.id
336 }.should change OriginalPicture, :count
339 @op.store 'bindata', @artist, @license.id
340 OriginalPicture.find(@op).should_not be_nil
\r
343 @op.store('bindata', @artist, @license.id).should eq true
\r
347 context 'RMagick変換が失敗したとき' do
349 OriginalPicture.any_instance.stub(:data_to_mgk).with(any_args).and_return(false)
352 @op.store('bindata', @artist, @license.id).should be_false
\r
354 it '自身の保存は呼ばれていない' do
355 OriginalPicture.any_instance.should_not_receive(:save)
\r
358 context '自身の保存に失敗したとき' 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
364 @op.store('bindata', @artist, @license.id).should be_false
\r
367 @op.store('bindata', @artist, @license.id)
368 @op.should be_a_new OriginalPicture
\r
370 it '原画の保存は呼ばれていない' do
371 PictureIO::LocalPicture.any_instance.should_not_receive(:put)
\r
374 context '原画の保存に失敗したとき' 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)
381 @op.store('bindata', @artist, @license.id).should be_false
\r
384 @op.store('bindata', @artist, @license.id)
\r
385 @op.should be_a_new OriginalPicture
\r
387 it '対象素材オブジェクトの保存は呼ばれていない' do
388 ResourcePicture.any_instance.should_not_receive(:save)
\r
389 @op.store('bindata', @artist, @license.id)
\r
391 it '全体エラーメッセージがセットされている' do
393 @op.store('bindata', @artist, @license.id)
\r
394 }.should change(@op.errors[:base], :count)
397 context '対象素材オブジェクトの保存に失敗したとき' 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)
405 @op.store('bindata', @artist, @license.id).should be_false
\r
408 @op.store('bindata', @artist, @license.id)
\r
409 @op.should be_a_new OriginalPicture
\r
411 it '全体エラーメッセージがセットされている' do
413 @op.store('bindata', @artist, @license.id)
\r
414 }.should change(@op.errors[:base], :count)