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
27 context 'extを検証するとき' do
29 @op = Factory.build :original_picture, :artist_id => @artist.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
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
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
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
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
146 describe 'データ補充に於いて' do
152 describe '作者判定に於いて' do
156 op = Factory :original_picture, :artist_id => @artist.id
157 op.own?(@author).should == true
160 op = Factory :original_picture, :artist_id => @other_artist.id
161 op.own?(@author).should == false
164 op = Factory :original_picture, :artist_id => @artist.id
165 op.own?(nil).should == false
168 other_user = Factory( :user_yas)
169 op = Factory :original_picture, :artist_id => @artist.id
170 op.own?(@other_author).should == false
173 describe '単体取得に於いて' do
175 @op = Factory :original_picture, :artist_id => @artist.id
178 pic = OriginalPicture.show @op.id, @artist
181 context '他人の原画を開こうとしたとき' do
182 it '403Forbidden例外を返す' do
184 pic = OriginalPicture.show @op.id, @other_artist
185 }.should raise_error(ActiveRecord::Forbidden)
188 context '存在しない原画を開こうとしたとき' do
189 it '404RecordNotFound例外を返す' do
191 pic = OriginalPicture.show 0, @artist
192 }.should raise_error(ActiveRecord::RecordNotFound)
196 describe '関連テーブルプションに於いて' do
\r
197 context 'オプションがないとき' do
\r
198 it '1つの項目を含んでいる' do
\r
199 r = OriginalPicture.show_include_opt
\r
200 r.should have(1).items
\r
203 r = OriginalPicture.show_include_opt
\r
204 r.has_key?(:resource_picture).should be_true
\r
207 context 'オプションでtestを含ませたとき' do
\r
208 it '2つの項目を含んでいる' do
\r
209 r = OriginalPicture.show_include_opt(:include => {:test => {}})
\r
210 r.should have(2).items
\r
213 r = OriginalPicture.show_include_opt(:include => {:test => {}})
\r
214 r.has_key?(:test).should be_true
\r
218 describe 'json単体出力オプションに於いて' do
219 it 'includeキーを含んでいる' do
\r
220 r = OriginalPicture.show_json_include_opt
\r
221 r.has_key?(:include).should be_true
\r
223 it '1つの項目を含んでいる' do
\r
224 r = OriginalPicture.show_json_include_opt[:include]
\r
225 r.should have(1).items
\r
228 r = OriginalPicture.show_json_include_opt[:include]
\r
229 r.has_key?(:resource_picture).should be_true
\r
232 describe '一覧取得に於いて' do
234 @op = Factory :original_picture, :artist_id => @artist.id
236 context 'page補正について' do
237 it '文字列から数値に変換される' do
238 OriginalPicture.page('8').should eq 8
241 OriginalPicture.page().should eq 1
244 OriginalPicture.page('0').should eq 1
247 context 'page_size補正について' do
248 it '文字列から数値に変換される' do
249 OriginalPicture.page_size('7').should eq 7
251 it 'nilの場合はOriginalPicture.default_page_sizeになる' do
252 OriginalPicture.page_size().should eq OriginalPicture.default_page_size
254 it '0以下の場合はOriginalPicture.default_page_sizeになる' do
255 OriginalPicture.page_size('0').should eq OriginalPicture.default_page_size
257 it 'OriginalPicture.max_page_sizeを超えた場合はOriginalPicture.max_page_sizeになる' do
258 OriginalPicture.page_size('1000').should eq OriginalPicture.max_page_size
262 pic = OriginalPicture.list @artist.id
266 Factory :original_picture, :artist_id => @other_artist.id
267 pic = OriginalPicture.list @artist.id
271 newpic = Factory :original_picture, :artist_id => @artist.id, :updated_at => Time.now + 100
272 pic = OriginalPicture.list @artist.id
273 pic.should eq [newpic, @op]
275 context 'DBに5件あって1ページの件数を2件に変えたとして' do
277 @op2 = Factory :original_picture, :artist_id => @artist.id, :updated_at => Time.now + 100
278 @op3 = Factory :original_picture, :artist_id => @artist.id, :updated_at => Time.now + 200
279 @op4 = Factory :original_picture, :artist_id => @artist.id, :updated_at => Time.now + 300
280 @op5 = Factory :original_picture, :artist_id => @artist.id, :updated_at => Time.now + 400
281 OriginalPicture.stub(:default_page_size).and_return(2)
284 pic = OriginalPicture.list @artist.id
285 pic.should have(2).items
287 it 'page=1なら末尾2件を返す' do
289 pic = OriginalPicture.list( @artist.id, {}, 1)
290 pic.should eq [@op5, @op4]
292 it 'page=2なら中間2件を返す' do
293 pic = OriginalPicture.list( @artist.id, {}, 2)
294 pic.should eq [@op3, @op2]
296 it 'page=3なら先頭1件を返す' do
297 pic = OriginalPicture.list( @artist.id, {}, 3)
302 describe 'list関連テーブルプションに於いて' do
\r
303 it 'includeキーを含んでいる' do
\r
304 r = OriginalPicture.list_opt
\r
305 r.has_key?(:include).should be_true
\r
307 it '1つの項目を含んでいる' do
\r
308 r = OriginalPicture.list_opt[:include]
\r
309 r.should have(1).items
\r
312 r = OriginalPicture.list_opt[:include]
\r
313 r.has_key?(:resource_picture).should be_true
\r
316 describe 'json一覧出力オプションに於いて' do
\r
317 it 'includeキーを含んでいる' do
\r
318 r = OriginalPicture.list_json_opt
\r
319 r.has_key?(:include).should be_true
\r
321 it '1つの項目を含んでいる' do
\r
322 r = OriginalPicture.list_json_opt[:include]
\r
323 r.should have(1).items
\r
326 r = OriginalPicture.list_json_opt[:include]
\r
327 r.has_key?(:resource_picture).should be_true
\r
330 describe 'RMagick変換に於いて' do
332 @op = Factory.build :original_picture, :artist_id => @artist.id
334 context 'つつがなく終わるとき' do
335 it '画像データをオブジェクト化している' do
336 Magick::Image.stub(:from_blob).and_return(['mgkobj'])
337 @op.data_to_mgk('mgkbin').should eq 'mgkobj'
342 Magick::Image.should_receive(:from_blob).with(any_args).and_raise('StandardError')
343 @op.data_to_mgk('mgkbin').should be_false
345 it '全体エラーがセットされている' do
346 Magick::Image.should_receive(:from_blob).with(any_args).and_raise('StandardError')
348 @op.data_to_mgk('mgkbin')
349 }.should change(@op.errors[:base], :count)
353 describe '作成・更新に於いて' do
355 @op = Factory.build :original_picture, :artist_id => @artist.id
357 class Mgk ; class Image ; end ; end
359 Mgk::Image.stub(:from_blob).with(any_args).and_return([Mgk.new])
360 Mgk.any_instance.stub(:format).with(any_args).and_return('png')
361 Mgk.any_instance.stub(:rows).with(any_args).and_return(200)
362 Mgk.any_instance.stub(:columns).with(any_args).and_return(100)
363 Mgk.any_instance.stub(:filesize).with(any_args).and_return(@filesize)
364 Mgk.any_instance.stub(:to_blob).with(any_args).and_return('data')
365 #原画ファイル削除だけは必ず成功するものとしておく
366 PictureIO::LocalPicture.any_instance.stub(:delete).with(any_args).and_return(true)
367 #素材取得は新規作成ケースでテストしておく
368 ResourcePicture.stub(:update_picture).with(any_args).and_return(
369 Factory.build :resource_picture, :artist_id => @artist.id
374 #すべての処理が正常パターンで通過すれば、一番深い分岐まで通る。
375 #それで外部のメソッド呼び出しだけに着目してテストする。
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(true)
380 it 'RMagick変換を依頼している' do
381 OriginalPicture.any_instance.should_receive(:data_to_mgk).exactly(1)
382 @op.store 'bindata', @artist
\r
384 it '自身に属性をセットしている' do
385 OriginalPicture.any_instance.should_receive(:attributes=).exactly(1)
386 @op.store 'bindata', @artist
\r
389 OriginalPicture.any_instance.should_receive(:save).exactly(1)
\r
390 @op.store 'bindata', @artist
\r
392 it 'PictureIoに画像データの保存を依頼している' do
393 PictureIO::LocalPicture.any_instance.should_receive(:put).with(any_args).exactly(1)
394 @op.store 'bindata', @artist
\r
397 context 'つつがなく終わるとき' do
399 #すべての処理を正常パターンで通過させ、保存機能をチェックする。
400 OriginalPicture.any_instance.stub(:data_to_mgk).with(any_args).and_return(Mgk.new)
401 # OriginalPicture.any_instance.stub(:save).with(any_args).and_return(true)
\r
402 PictureIO::LocalPicture.any_instance.stub(:put).with(any_args).and_return(true)
404 it '自身に属性をセットしている' do
406 @op.store 'bindata', @artist
\r
407 }.should change @op, :filesize
409 it '原画モデルが作成されている' do
411 @op.store 'bindata', @artist
412 }.should change OriginalPicture, :count
415 @op.store 'bindata', @artist
416 OriginalPicture.find(@op).should_not be_nil
\r
419 @op.store('bindata', @artist).should eq true
\r
422 #以下から例外ケース。処理先頭から失敗させていく
423 context 'RMagick変換が失敗したとき' do
425 OriginalPicture.any_instance.stub(:data_to_mgk).with(any_args).and_return(false)
428 @op.store('bindata', @artist).should be_false
\r
430 it '自身の保存は呼ばれていない' do
431 OriginalPicture.any_instance.should_not_receive(:save)
\r
432 @op.store('bindata', @artist)
\r
435 context '自身の保存に失敗したとき' do
437 OriginalPicture.any_instance.stub(:data_to_mgk).with(any_args).and_return(Mgk.new)
438 OriginalPicture.any_instance.stub(:save).with(any_args).and_return(false)
\r
441 @op.store('bindata', @artist).should be_false
\r
444 @op.store('bindata', @artist)
445 @op.should be_a_new OriginalPicture
\r
447 it '原画の保存は呼ばれていない' do
448 PictureIO::LocalPicture.any_instance.should_not_receive(:put)
\r
451 context '原画の保存に失敗したとき' do
453 OriginalPicture.any_instance.stub(:data_to_mgk).with(any_args).and_return(Mgk.new)
454 OriginalPicture.any_instance.stub(:save).with(any_args).and_return(true)
\r
455 PictureIO::LocalPicture.any_instance.stub(:put).with(any_args).and_return(false)
458 @op.store('bindata', @artist).should be_false
\r
461 @op.store('bindata', @artist)
\r
462 @op.should be_a_new OriginalPicture
\r
464 it '対象素材オブジェクトの保存は呼ばれていない' do
465 ResourcePicture.any_instance.should_not_receive(:save)
\r
466 @op.store('bindata', @artist)
\r
468 it '全体エラーメッセージがセットされている' do
470 @op.store('bindata', @artist)
\r
471 }.should change(@op.errors[:base], :count)