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 @sp = Factory :system_picture
14 @lg = Factory :license_group
15 @license = Factory :license, :license_group_id => @lg.id, :system_picture_id => @sp.id
22 it 'オーソドックスなデータなら通る' do
23 @op = Factory.build :original_picture, :artist_id => @artist.id, :license_id => @license.id
27 context 'extを検証するとき' do
29 @op = Factory.build :original_picture, :artist_id => @artist.id, :license_id => @license.id
37 @op.should_not be_valid
41 @op.should_not be_valid
43 it 'png,gif,jpeg以外なら失敗する' do
45 @op.should_not be_valid
48 context 'widthを検証するとき' do
50 @op = Factory.build :original_picture, :artist_id => @artist.id, :license_id => @license.id
58 @op.should_not be_valid
62 @op.should_not be_valid
66 @op.should_not be_valid
70 @op.should_not be_valid
73 context 'heightを検証するとき' do
75 @op = Factory.build :original_picture, :artist_id => @artist.id, :license_id => @license.id
83 @op.should_not be_valid
87 @op.should_not be_valid
91 @op.should_not be_valid
95 @op.should_not be_valid
98 context 'filesizeを検証するとき' do
100 @op = Factory.build :original_picture, :artist_id => @artist.id, :license_id => @license.id
108 @op.should_not be_valid
112 @op.should_not be_valid
116 @op.should_not be_valid
119 @op.filesize = 2000000+1
120 @op.should_not be_valid
123 context 'artist_idを検証するとき' do
125 @op = Factory.build :original_picture, :license_id => @license.id
128 @op.artist_id = @artist.id
133 @op.should_not be_valid
137 @op.should_not be_valid
139 it '存在する絵師でなければ失敗する' do
141 @op.should_not be_valid
144 context 'license_idを検証するとき' do
146 @op = Factory.build :original_picture, :artist_id => @artist.id
149 @op.license_id = @license.id
154 @op.should_not be_valid
158 @op.should_not be_valid
160 it '存在するlicenseでなければ失敗する' do
162 @op.should_not be_valid
167 describe 'データ補充に於いて' do
173 describe '作者判定に於いて' do
177 op = Factory :original_picture, :artist_id => @artist.id
178 op.own?(@author).should == true
181 op = Factory :original_picture, :artist_id => @other_artist.id
182 op.own?(@author).should == false
185 op = Factory :original_picture, :artist_id => @artist.id
186 op.own?(nil).should == false
189 other_user = Factory( :user_yas)
190 op = Factory :original_picture, :artist_id => @artist.id
191 op.own?(@other_author).should == false
194 describe '単体取得に於いて' do
196 @op = Factory :original_picture, :artist_id => @artist.id
199 pic = OriginalPicture.show @op.id, @artist
202 context '関連テーブルオプションがないとき' do
203 it 'ライセンスと素材を含んでいる' do
204 r = OriginalPicture.show_include_opt
205 r.should eq [:license, :resource_picture]
208 context '関連テーブルオプションで絵師を含ませたとき' do
209 it 'ライセンスと素材と作者データを含んでいる' do
210 r = OriginalPicture.show_include_opt(:include => :artist)
211 r.should eq [:license, :resource_picture, :artist]
214 context '他人の原画を開こうとしたとき' do
215 it '403Forbidden例外を返す' do
217 pic = OriginalPicture.show @op.id, @other_artist
218 }.should raise_error(ActiveRecord::Forbidden)
221 context '存在しない原画を開こうとしたとき' do
222 it '404RecordNotFound例外を返す' do
224 pic = OriginalPicture.show 0, @artist
225 }.should raise_error(ActiveRecord::RecordNotFound)
229 describe 'json単体出力オプションに於いて' do
230 it 'includeキーがライセンスと素材を含んでいる' do
231 r = OriginalPicture.show_json_include_opt
232 r[:include].should eq [:license, :resource_picture]
235 describe '一覧取得に於いて' do
237 @op = Factory :original_picture, :artist_id => @artist.id
239 context 'page補正について' do
240 it '文字列から数値に変換される' do
241 OriginalPicture.page('8').should eq 8
244 OriginalPicture.page().should eq 1
247 OriginalPicture.page('0').should eq 1
250 context 'page_size補正について' do
251 it '文字列から数値に変換される' do
252 OriginalPicture.page_size('7').should eq 7
254 it 'nilの場合はOriginalPicture.default_page_sizeになる' do
255 OriginalPicture.page_size().should eq OriginalPicture.default_page_size
257 it '0以下の場合はOriginalPicture.default_page_sizeになる' do
258 OriginalPicture.page_size('0').should eq OriginalPicture.default_page_size
260 it 'OriginalPicture.max_page_sizeを超えた場合はOriginalPicture.max_page_sizeになる' do
261 OriginalPicture.page_size('1000').should eq OriginalPicture.max_page_size
265 pic = OriginalPicture.list @artist.id
269 Factory :original_picture, :artist_id => @other_artist.id
270 pic = OriginalPicture.list @artist.id
274 newpic = Factory :original_picture, :artist_id => @artist.id, :updated_at => Time.now + 100
275 pic = OriginalPicture.list @artist.id
276 pic.should eq [newpic, @op]
278 context 'DBに5件あって1ページの件数を2件に変えたとして' do
280 @op2 = Factory :original_picture, :artist_id => @artist.id, :updated_at => Time.now + 100
281 @op3 = Factory :original_picture, :artist_id => @artist.id, :updated_at => Time.now + 200
282 @op4 = Factory :original_picture, :artist_id => @artist.id, :updated_at => Time.now + 300
283 @op5 = Factory :original_picture, :artist_id => @artist.id, :updated_at => Time.now + 400
284 OriginalPicture.stub(:default_page_size).and_return(2)
287 pic = OriginalPicture.list @artist.id
288 pic.should have(2).items
290 it 'page=1なら末尾2件を返す' do
292 pic = OriginalPicture.list( @artist.id, {}, 1)
293 pic.should eq [@op5, @op4]
295 it 'page=2なら中間2件を返す' do
296 pic = OriginalPicture.list( @artist.id, {}, 2)
297 pic.should eq [@op3, @op2]
299 it 'page=3なら先頭1件を返す' do
300 pic = OriginalPicture.list( @artist.id, {}, 3)
305 describe 'json一覧出力オプションに於いて' do
306 it 'includeキーがライセンスと素材を含んでいる' do
307 r = OriginalPicture.list_json_opt
308 r[:include].should eq [:license, :resource_picture]
311 describe 'RMagick変換に於いて' do
313 @op = Factory.build :original_picture, :artist_id => @artist.id
315 context 'つつがなく終わるとき' do
316 it '画像データをオブジェクト化している' do
317 Magick::Image.stub(:from_blob).and_return(['mgkobj'])
318 @op.data_to_mgk('mgkbin').should eq 'mgkobj'
323 Magick::Image.should_receive(:from_blob).with(any_args).and_raise('StandardError')
324 @op.data_to_mgk('mgkbin').should be_false
326 it '全体エラーがセットされている' do
327 Magick::Image.should_receive(:from_blob).with(any_args).and_raise('StandardError')
329 @op.data_to_mgk('mgkbin')
330 }.should change(@op.errors[:base], :count)
334 describe '作成・更新に於いて' do
336 @op = Factory.build :original_picture, :artist_id => @artist.id
338 class Mgk ; class Image ; end ; end
340 Mgk::Image.stub(:from_blob).with(any_args).and_return([Mgk.new])
341 Mgk.any_instance.stub(:format).with(any_args).and_return('png')
342 Mgk.any_instance.stub(:rows).with(any_args).and_return(200)
343 Mgk.any_instance.stub(:columns).with(any_args).and_return(100)
344 Mgk.any_instance.stub(:filesize).with(any_args).and_return(@filesize)
345 Mgk.any_instance.stub(:to_blob).with(any_args).and_return('data')
346 #原画ファイル削除だけは必ず成功するものとしておく
347 PictureIO::LocalPicture.any_instance.stub(:delete).with(any_args).and_return(true)
348 #素材取得は新規作成ケースでテストしておく
349 ResourcePicture.stub(:update_picture).with(any_args).and_return(
350 Factory.build :resource_picture, :artist_id => @artist.id
355 #すべての処理が正常パターンで通過すれば、一番深い分岐まで通る。
356 #それで外部のメソッド呼び出しだけに着目してテストする。
357 OriginalPicture.any_instance.stub(:data_to_mgk).with(any_args).and_return(Mgk.new)
358 OriginalPicture.any_instance.stub(:save).with(any_args).and_return(true)
\r
359 PictureIO::LocalPicture.any_instance.stub(:put).with(any_args).and_return(true)
360 ResourcePicture.any_instance.stub(:store).with(any_args).and_return(true)
362 it 'RMagick変換を依頼している' do
363 OriginalPicture.any_instance.should_receive(:data_to_mgk).exactly(1)
364 @op.store 'bindata', @artist, @license.id
\r
366 it '自身に属性をセットしている' do
367 OriginalPicture.any_instance.should_receive(:attributes=).exactly(1)
368 @op.store 'bindata', @artist, @license.id
\r
371 OriginalPicture.any_instance.should_receive(:save).exactly(1)
\r
372 @op.store 'bindata', @artist, @license.id
\r
374 it 'PictureIoに画像データの保存を依頼している' do
375 PictureIO::LocalPicture.any_instance.should_receive(:put).with(any_args).exactly(1)
376 @op.store 'bindata', @artist, @license.id
\r
378 it '素材モデルに対象素材を問い合わせている' do
379 ResourcePicture.should_receive(:update_picture).with(any_args).exactly(1)
380 @op.store 'bindata', @artist, @license.id
\r
382 it '対象素材オブジェクトに保存を依頼している' do
383 ResourcePicture.any_instance.should_receive(:store).with(any_args).exactly(1)
384 @op.store 'bindata', @artist, @license.id
\r
387 context 'つつがなく終わるとき' do
389 #すべての処理を正常パターンで通過させ、保存機能をチェックする。
390 OriginalPicture.any_instance.stub(:data_to_mgk).with(any_args).and_return(Mgk.new)
391 # OriginalPicture.any_instance.stub(:save).with(any_args).and_return(true)
\r
392 PictureIO::LocalPicture.any_instance.stub(:put).with(any_args).and_return(true)
393 ResourcePicture.any_instance.stub(:store).with(any_args).and_return(true)
395 it '自身に属性をセットしている' do
397 @op.store 'bindata', @artist, @license.id
\r
398 }.should change @op, :filesize
400 it '原画モデルが作成されている' do
402 @op.store 'bindata', @artist, @license.id
403 }.should change OriginalPicture, :count
406 @op.store 'bindata', @artist, @license.id
407 OriginalPicture.find(@op).should_not be_nil
\r
410 @op.store('bindata', @artist, @license.id).should eq true
\r
413 #以下から例外ケース。処理先頭から失敗させていく
414 context 'RMagick変換が失敗したとき' do
416 OriginalPicture.any_instance.stub(:data_to_mgk).with(any_args).and_return(false)
419 @op.store('bindata', @artist, @license.id).should be_false
\r
421 it '自身の保存は呼ばれていない' do
422 OriginalPicture.any_instance.should_not_receive(:save)
\r
425 context '自身の保存に失敗したとき' do
427 OriginalPicture.any_instance.stub(:data_to_mgk).with(any_args).and_return(Mgk.new)
428 OriginalPicture.any_instance.stub(:save).with(any_args).and_return(false)
\r
431 @op.store('bindata', @artist, @license.id).should be_false
\r
434 @op.store('bindata', @artist, @license.id)
435 @op.should be_a_new OriginalPicture
\r
437 it '原画の保存は呼ばれていない' do
438 PictureIO::LocalPicture.any_instance.should_not_receive(:put)
\r
441 context '原画の保存に失敗したとき' do
443 OriginalPicture.any_instance.stub(:data_to_mgk).with(any_args).and_return(Mgk.new)
444 OriginalPicture.any_instance.stub(:save).with(any_args).and_return(true)
\r
445 PictureIO::LocalPicture.any_instance.stub(:put).with(any_args).and_return(false)
448 @op.store('bindata', @artist, @license.id).should be_false
\r
451 @op.store('bindata', @artist, @license.id)
\r
452 @op.should be_a_new OriginalPicture
\r
454 it '対象素材オブジェクトの保存は呼ばれていない' do
455 ResourcePicture.any_instance.should_not_receive(:save)
\r
456 @op.store('bindata', @artist, @license.id)
\r
458 it '全体エラーメッセージがセットされている' do
460 @op.store('bindata', @artist, @license.id)
\r
461 }.should change(@op.errors[:base], :count)
464 context '対象素材オブジェクトの保存に失敗したとき' do
466 OriginalPicture.any_instance.stub(:data_to_mgk).with(any_args).and_return(Mgk.new)
467 OriginalPicture.any_instance.stub(:save).with(any_args).and_return(true)
\r
468 PictureIO::LocalPicture.any_instance.stub(:put).with(any_args).and_return(true)
469 ResourcePicture.any_instance.stub(:store).with(any_args).and_return(false)
472 @op.store('bindata', @artist, @license.id).should be_false
\r
475 @op.store('bindata', @artist, @license.id)
\r
476 @op.should be_a_new OriginalPicture
\r
478 it '全体エラーメッセージがセットされている' do
480 @op.store('bindata', @artist, @license.id)
\r
481 }.should change(@op.errors[:base], :count)