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
198 @op = Factory :original_picture, :artist_id => @artist.id
201 pic = OriginalPicture.edit @op.id, @artist
204 context '他人の原画を開こうとしたとき' do
205 it '403Forbidden例外を返す' do
207 pic = OriginalPicture.edit @op.id, @other_artist
208 }.should raise_error(ActiveRecord::Forbidden)
211 context '存在しない原画を開こうとしたとき' do
212 it '404RecordNotFound例外を返す' do
214 pic = OriginalPicture.edit 0, @artist
215 }.should raise_error(ActiveRecord::RecordNotFound)
219 describe 'json単体出力オプションに於いて' do
220 it 'includeキーを含んでいる' do
\r
221 r = OriginalPicture.show_json_include_opt
\r
222 r.has_key?(:include).should be_true
\r
224 it '1つの項目を含んでいる' do
\r
225 r = OriginalPicture.show_json_include_opt[:include]
\r
226 r.should have(1).items
\r
229 r = OriginalPicture.show_json_include_opt[:include]
\r
230 r.has_key?(:resource_picture).should be_true
\r
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 'list関連テーブルプションに於いて' do
\r
304 it 'includeキーを含んでいる' do
\r
305 r = OriginalPicture.list_opt
\r
306 r.has_key?(:include).should be_true
\r
308 it '1つの項目を含んでいる' do
\r
309 r = OriginalPicture.list_opt[:include]
\r
310 r.should have(1).items
\r
313 r = OriginalPicture.list_opt[:include]
\r
314 r.has_key?(:resource_picture).should be_true
\r
317 describe 'json一覧出力オプションに於いて' do
\r
318 it 'includeキーを含んでいる' do
\r
319 r = OriginalPicture.list_json_opt
\r
320 r.has_key?(:include).should be_true
\r
322 it '1つの項目を含んでいる' do
\r
323 r = OriginalPicture.list_json_opt[:include]
\r
324 r.should have(1).items
\r
327 r = OriginalPicture.list_json_opt[:include]
\r
328 r.has_key?(:resource_picture).should be_true
\r
331 describe 'RMagick変換に於いて' do
333 @op = Factory.build :original_picture, :artist_id => @artist.id
335 context 'つつがなく終わるとき' do
336 it '画像データをオブジェクト化している' do
337 Magick::Image.stub(:from_blob).and_return(['mgkobj'])
338 @op.data_to_mgk('mgkbin').should eq 'mgkobj'
343 Magick::Image.should_receive(:from_blob).with(any_args).and_raise('StandardError')
344 @op.data_to_mgk('mgkbin').should be_false
346 it '全体エラーがセットされている' do
347 Magick::Image.should_receive(:from_blob).with(any_args).and_raise('StandardError')
349 @op.data_to_mgk('mgkbin')
350 }.should change(@op.errors[:base], :count)
354 describe '作成・更新に於いて' do
356 @op = Factory.build :original_picture, :artist_id => @artist.id
358 class Mgk ; class Image ; end ; end
360 Mgk::Image.stub(:from_blob).with(any_args).and_return([Mgk.new])
361 Mgk.any_instance.stub(:format).with(any_args).and_return('png')
362 Mgk.any_instance.stub(:rows).with(any_args).and_return(200)
363 Mgk.any_instance.stub(:columns).with(any_args).and_return(100)
364 Mgk.any_instance.stub(:filesize).with(any_args).and_return(@filesize)
365 Mgk.any_instance.stub(:to_blob).with(any_args).and_return('data')
366 #原画ファイル削除だけは必ず成功するものとしておく
367 PictureIO::LocalPicture.any_instance.stub(:delete).with(any_args).and_return(true)
368 #素材取得は新規作成ケースでテストしておく
369 ResourcePicture.stub(:update_picture).with(any_args).and_return(
370 Factory.build :resource_picture, :artist_id => @artist.id
375 #すべての処理が正常パターンで通過すれば、一番深い分岐まで通る。
376 #それで外部のメソッド呼び出しだけに着目してテストする。
377 OriginalPicture.any_instance.stub(:data_to_mgk).with(any_args).and_return(Mgk.new)
378 OriginalPicture.any_instance.stub(:save).with(any_args).and_return(true)
\r
379 PictureIO::LocalPicture.any_instance.stub(:put).with(any_args).and_return(true)
381 it 'RMagick変換を依頼している' do
382 OriginalPicture.any_instance.should_receive(:data_to_mgk).exactly(1)
383 @op.store 'bindata', @artist
\r
385 it '自身に属性をセットしている' do
386 OriginalPicture.any_instance.should_receive(:attributes=).exactly(1)
387 @op.store 'bindata', @artist
\r
390 OriginalPicture.any_instance.should_receive(:save).exactly(1)
\r
391 @op.store 'bindata', @artist
\r
393 it 'PictureIoに画像データの保存を依頼している' do
394 PictureIO::LocalPicture.any_instance.should_receive(:put).with(any_args).exactly(1)
395 @op.store 'bindata', @artist
\r
398 context 'つつがなく終わるとき' do
400 #すべての処理を正常パターンで通過させ、保存機能をチェックする。
401 OriginalPicture.any_instance.stub(:data_to_mgk).with(any_args).and_return(Mgk.new)
402 # OriginalPicture.any_instance.stub(:save).with(any_args).and_return(true)
\r
403 PictureIO::LocalPicture.any_instance.stub(:put).with(any_args).and_return(true)
405 it '自身に属性をセットしている' do
407 @op.store 'bindata', @artist
\r
408 }.should change @op, :filesize
410 it '原画モデルが作成されている' do
412 @op.store 'bindata', @artist
413 }.should change OriginalPicture, :count
416 @op.store 'bindata', @artist
417 OriginalPicture.find(@op).should_not be_nil
\r
420 @op.store('bindata', @artist).should eq true
\r
423 #以下から例外ケース。処理先頭から失敗させていく
424 context 'RMagick変換が失敗したとき' do
426 OriginalPicture.any_instance.stub(:data_to_mgk).with(any_args).and_return(false)
429 @op.store('bindata', @artist).should be_false
\r
431 it '自身の保存は呼ばれていない' do
432 OriginalPicture.any_instance.should_not_receive(:save)
\r
433 @op.store('bindata', @artist)
\r
436 context '自身の保存に失敗したとき' do
438 OriginalPicture.any_instance.stub(:data_to_mgk).with(any_args).and_return(Mgk.new)
439 OriginalPicture.any_instance.stub(:save).with(any_args).and_return(false)
\r
442 @op.store('bindata', @artist).should be_false
\r
445 @op.store('bindata', @artist)
446 @op.should be_a_new OriginalPicture
\r
448 it '原画の保存は呼ばれていない' do
449 PictureIO::LocalPicture.any_instance.should_not_receive(:put)
\r
452 context '原画の保存に失敗したとき' do
454 OriginalPicture.any_instance.stub(:data_to_mgk).with(any_args).and_return(Mgk.new)
455 OriginalPicture.any_instance.stub(:save).with(any_args).and_return(true)
\r
456 PictureIO::LocalPicture.any_instance.stub(:put).with(any_args).and_return(false)
459 @op.store('bindata', @artist).should be_false
\r
462 @op.store('bindata', @artist)
\r
463 @op.should be_a_new OriginalPicture
\r
465 it '対象素材オブジェクトの保存は呼ばれていない' do
466 ResourcePicture.any_instance.should_not_receive(:save)
\r
467 @op.store('bindata', @artist)
\r
469 it '全体エラーメッセージがセットされている' do
471 @op.store('bindata', @artist)
\r
472 }.should change(@op.errors[:base], :count)