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, :license_id => @license.id
25 context 'extを検証するとき' do
27 @op = Factory.build :original_picture, :artist_id => @artist.id, :license_id => @license.id
35 @op.should_not be_valid
39 @op.should_not be_valid
41 it 'png,gif,jpeg以外なら失敗する' do
43 @op.should_not be_valid
46 context 'widthを検証するとき' do
48 @op = Factory.build :original_picture, :artist_id => @artist.id, :license_id => @license.id
56 @op.should_not be_valid
60 @op.should_not be_valid
64 @op.should_not be_valid
68 @op.should_not be_valid
71 context 'heightを検証するとき' do
73 @op = Factory.build :original_picture, :artist_id => @artist.id, :license_id => @license.id
81 @op.should_not be_valid
85 @op.should_not be_valid
89 @op.should_not be_valid
93 @op.should_not be_valid
96 context 'filesizeを検証するとき' do
98 @op = Factory.build :original_picture, :artist_id => @artist.id, :license_id => @license.id
106 @op.should_not be_valid
110 @op.should_not be_valid
114 @op.should_not be_valid
117 @op.filesize = 2000000+1
118 @op.should_not be_valid
121 context 'artist_idを検証するとき' do
123 @op = Factory.build :original_picture, :license_id => @license.id
126 @op.artist_id = @artist.id
131 @op.should_not be_valid
135 @op.should_not be_valid
137 it '存在する絵師でなければ失敗する' do
139 @op.should_not be_valid
142 context 'license_idを検証するとき' do
144 @op = Factory.build :original_picture, :artist_id => @artist.id
147 @op.license_id = @license.id
152 @op.should_not be_valid
156 @op.should_not be_valid
158 it '存在するlicenseでなければ失敗する' do
160 @op.should_not be_valid
165 describe 'データ補充に於いて' do
171 describe '作者判定に於いて' do
175 op = Factory :original_picture, :artist_id => @artist.id
176 op.own?(@author).should == true
179 op = Factory :original_picture, :artist_id => @other_artist.id
180 op.own?(@author).should == false
183 op = Factory :original_picture, :artist_id => @artist.id
184 op.own?(nil).should == false
187 other_user = Factory( :user_yas)
188 op = Factory :original_picture, :artist_id => @artist.id
189 op.own?(@other_author).should == false
192 describe '単体取得に於いて' do
194 @op = Factory :original_picture, :artist_id => @artist.id
197 pic = OriginalPicture.show @op.id, @artist
200 context '関連テーブルオプションがないとき' do
201 it 'ライセンスと素材を含んでいる' do
202 r = OriginalPicture.show_include_opt
203 r.should eq [:license, :resource_picture]
206 context '関連テーブルオプションで絵師を含ませたとき' do
207 it 'ライセンスと素材と作者データを含んでいる' do
208 r = OriginalPicture.show_include_opt(:include => :artist)
209 r.should eq [:license, :resource_picture, :artist]
212 context '他人の原画を開こうとしたとき' do
213 it '403Forbidden例外を返す' do
215 pic = OriginalPicture.show @op.id, @other_artist
216 }.should raise_error(ActiveRecord::Forbidden)
219 context '存在しない原画を開こうとしたとき' do
220 it '404RecordNotFound例外を返す' do
222 pic = OriginalPicture.show 0, @artist
223 }.should raise_error(ActiveRecord::RecordNotFound)
227 describe 'json単体出力オプションに於いて' do
228 it 'includeキーがライセンスと素材を含んでいる' do
229 r = OriginalPicture.show_json_include_opt
230 r[:include].should eq [:license, :resource_picture]
233 describe '一覧取得に於いて' do
235 @op = Factory :original_picture, :artist_id => @artist.id
237 context 'page補正について' do
238 it '文字列から数値に変換される' do
239 OriginalPicture.page('8').should eq 8
242 OriginalPicture.page().should eq 1
245 OriginalPicture.page('0').should eq 1
248 context 'page_size補正について' do
249 it '文字列から数値に変換される' do
250 OriginalPicture.page_size('7').should eq 7
252 it 'nilの場合はOriginalPicture.default_page_sizeになる' do
253 OriginalPicture.page_size().should eq OriginalPicture.default_page_size
255 it '0以下の場合はOriginalPicture.default_page_sizeになる' do
256 OriginalPicture.page_size('0').should eq OriginalPicture.default_page_size
258 it 'OriginalPicture.max_page_sizeを超えた場合はOriginalPicture.max_page_sizeになる' do
259 OriginalPicture.page_size('1000').should eq OriginalPicture.max_page_size
263 pic = OriginalPicture.list @artist.id
267 Factory :original_picture, :artist_id => @other_artist.id
268 pic = OriginalPicture.list @artist.id
272 newpic = Factory :original_picture, :artist_id => @artist.id, :updated_at => Time.now + 100
273 pic = OriginalPicture.list @artist.id
274 pic.should eq [newpic, @op]
276 context 'DBに5件あって1ページの件数を2件に変えたとして' do
278 @op2 = Factory :original_picture, :artist_id => @artist.id, :updated_at => Time.now + 100
279 @op3 = Factory :original_picture, :artist_id => @artist.id, :updated_at => Time.now + 200
280 @op4 = Factory :original_picture, :artist_id => @artist.id, :updated_at => Time.now + 300
281 @op5 = Factory :original_picture, :artist_id => @artist.id, :updated_at => Time.now + 400
282 OriginalPicture.stub(:default_page_size).and_return(2)
285 pic = OriginalPicture.list @artist.id
286 pic.should have(2).items
288 it 'page=1なら末尾2件を返す' do
290 pic = OriginalPicture.list( @artist.id, {}, 1)
291 pic.should eq [@op5, @op4]
293 it 'page=2なら中間2件を返す' do
294 pic = OriginalPicture.list( @artist.id, {}, 2)
295 pic.should eq [@op3, @op2]
297 it 'page=3なら先頭1件を返す' do
298 pic = OriginalPicture.list( @artist.id, {}, 3)
303 describe 'json一覧出力オプションに於いて' do
304 it 'includeキーがライセンスと素材を含んでいる' do
305 r = OriginalPicture.list_json_opt
306 r[:include].should eq [:license, :resource_picture]
309 describe 'RMagick変換に於いて' do
311 @op = Factory.build :original_picture, :artist_id => @artist.id
313 context 'つつがなく終わるとき' do
314 it '画像データをオブジェクト化している' do
315 Magick::Image.stub(:from_blob).and_return(['mgkobj'])
316 @op.data_to_mgk('mgkbin').should eq 'mgkobj'
321 Magick::Image.should_receive(:from_blob).with(any_args).and_raise('StandardError')
322 @op.data_to_mgk('mgkbin').should be_false
324 it '全体エラーがセットされている' do
325 Magick::Image.should_receive(:from_blob).with(any_args).and_raise('StandardError')
327 @op.data_to_mgk('mgkbin')
328 }.should change(@op.errors[:base], :count)
332 describe '作成・更新に於いて' do
334 @op = Factory.build :original_picture, :artist_id => @artist.id
336 class Mgk ; class Image ; end ; end
338 Mgk::Image.stub(:from_blob).with(any_args).and_return([Mgk.new])
339 Mgk.any_instance.stub(:format).with(any_args).and_return('png')
340 Mgk.any_instance.stub(:rows).with(any_args).and_return(200)
341 Mgk.any_instance.stub(:columns).with(any_args).and_return(100)
342 Mgk.any_instance.stub(:filesize).with(any_args).and_return(@filesize)
343 Mgk.any_instance.stub(:to_blob).with(any_args).and_return('data')
344 #原画ファイル削除だけは必ず成功するものとしておく
345 PictureIO::LocalPicture.any_instance.stub(:delete).with(any_args).and_return(true)
346 #素材取得は新規作成ケースでテストしておく
347 ResourcePicture.stub(:update_picture).with(any_args).and_return(
348 Factory.build :resource_picture, :artist_id => @artist.id
353 #すべての処理が正常パターンで通過すれば、一番深い分岐まで通る。
354 #それで外部のメソッド呼び出しだけに着目してテストする。
355 OriginalPicture.any_instance.stub(:data_to_mgk).with(any_args).and_return(Mgk.new)
356 OriginalPicture.any_instance.stub(:save).with(any_args).and_return(true)
\r
357 PictureIO::LocalPicture.any_instance.stub(:put).with(any_args).and_return(true)
358 ResourcePicture.any_instance.stub(:store).with(any_args).and_return(true)
360 it 'RMagick変換を依頼している' do
361 OriginalPicture.any_instance.should_receive(:data_to_mgk).exactly(1)
362 @op.store 'bindata', @artist, @license.id
\r
364 it '自身に属性をセットしている' do
365 OriginalPicture.any_instance.should_receive(:attributes=).exactly(1)
366 @op.store 'bindata', @artist, @license.id
\r
369 OriginalPicture.any_instance.should_receive(:save).exactly(1)
\r
370 @op.store 'bindata', @artist, @license.id
\r
372 it 'PictureIoに画像データの保存を依頼している' do
373 PictureIO::LocalPicture.any_instance.should_receive(:put).with(any_args).exactly(1)
374 @op.store 'bindata', @artist, @license.id
\r
376 it '素材モデルに対象素材を問い合わせている' do
377 ResourcePicture.should_receive(:update_picture).with(any_args).exactly(1)
378 @op.store 'bindata', @artist, @license.id
\r
380 it '対象素材オブジェクトに保存を依頼している' do
381 ResourcePicture.any_instance.should_receive(:store).with(any_args).exactly(1)
382 @op.store 'bindata', @artist, @license.id
\r
385 context 'つつがなく終わるとき' do
387 #すべての処理を正常パターンで通過させ、保存機能をチェックする。
388 OriginalPicture.any_instance.stub(:data_to_mgk).with(any_args).and_return(Mgk.new)
389 # OriginalPicture.any_instance.stub(:save).with(any_args).and_return(true)
\r
390 PictureIO::LocalPicture.any_instance.stub(:put).with(any_args).and_return(true)
391 ResourcePicture.any_instance.stub(:store).with(any_args).and_return(true)
393 it '自身に属性をセットしている' do
395 @op.store 'bindata', @artist, @license.id
\r
396 }.should change @op, :filesize
398 it '原画モデルが作成されている' do
400 @op.store 'bindata', @artist, @license.id
401 }.should change OriginalPicture, :count
404 @op.store 'bindata', @artist, @license.id
405 OriginalPicture.find(@op).should_not be_nil
\r
408 @op.store('bindata', @artist, @license.id).should eq true
\r
411 #以下から例外ケース。処理先頭から失敗させていく
412 context 'RMagick変換が失敗したとき' do
414 OriginalPicture.any_instance.stub(:data_to_mgk).with(any_args).and_return(false)
417 @op.store('bindata', @artist, @license.id).should be_false
\r
419 it '自身の保存は呼ばれていない' do
420 OriginalPicture.any_instance.should_not_receive(:save)
\r
423 context '自身の保存に失敗したとき' do
425 OriginalPicture.any_instance.stub(:data_to_mgk).with(any_args).and_return(Mgk.new)
426 OriginalPicture.any_instance.stub(:save).with(any_args).and_return(false)
\r
429 @op.store('bindata', @artist, @license.id).should be_false
\r
432 @op.store('bindata', @artist, @license.id)
433 @op.should be_a_new OriginalPicture
\r
435 it '原画の保存は呼ばれていない' do
436 PictureIO::LocalPicture.any_instance.should_not_receive(:put)
\r
439 context '原画の保存に失敗したとき' do
441 OriginalPicture.any_instance.stub(:data_to_mgk).with(any_args).and_return(Mgk.new)
442 OriginalPicture.any_instance.stub(:save).with(any_args).and_return(true)
\r
443 PictureIO::LocalPicture.any_instance.stub(:put).with(any_args).and_return(false)
446 @op.store('bindata', @artist, @license.id).should be_false
\r
449 @op.store('bindata', @artist, @license.id)
\r
450 @op.should be_a_new OriginalPicture
\r
452 it '対象素材オブジェクトの保存は呼ばれていない' do
453 ResourcePicture.any_instance.should_not_receive(:save)
\r
454 @op.store('bindata', @artist, @license.id)
\r
456 it '全体エラーメッセージがセットされている' do
458 @op.store('bindata', @artist, @license.id)
\r
459 }.should change(@op.errors[:base], :count)
462 context '対象素材オブジェクトの保存に失敗したとき' do
464 OriginalPicture.any_instance.stub(:data_to_mgk).with(any_args).and_return(Mgk.new)
465 OriginalPicture.any_instance.stub(:save).with(any_args).and_return(true)
\r
466 PictureIO::LocalPicture.any_instance.stub(:put).with(any_args).and_return(true)
467 ResourcePicture.any_instance.stub(:store).with(any_args).and_return(false)
470 @op.store('bindata', @artist, @license.id).should be_false
\r
473 @op.store('bindata', @artist, @license.id)
\r
474 @op.should be_a_new OriginalPicture
\r
476 it '全体エラーメッセージがセットされている' do
478 @op.store('bindata', @artist, @license.id)
\r
479 }.should change(@op.errors[:base], :count)