1 # -*- encoding: utf-8 -*-
\r
4 describe ResourcePicture do
7 @user = Factory( :user_yas)
9 @artist = Factory :artist_yas, :author_id => @author.id
\r
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
14 @original_picture = Factory :original_picture, :artist_id => @artist.id, :license_id => @license.id
\r
16 class Mgk ; class Image ; end ; end
17 Mgk::Image.stub(:from_blob).with(any_args).and_return([Mgk.new])
18 Mgk.any_instance.stub(:format).with(any_args).and_return('png')
19 Mgk.any_instance.stub(:format=).with(any_args)
20 Mgk.any_instance.stub(:rows).with(any_args).and_return(200)
21 Mgk.any_instance.stub(:columns).with(any_args).and_return(100)
22 Mgk.any_instance.stub(:filesize).with(any_args).and_return(20000)
23 Mgk.any_instance.stub(:to_blob).with(any_args).and_return('data')
24 #原画ファイル削除だけは必ず成功するものとしておく
25 PictureIO::LocalPicture.any_instance.stub(:delete).with(any_args).and_return(true)
32 it 'オーソドックスなデータなら通る' do
33 @rp = Factory.build :resource_picture, :artist_id => @artist.id, :original_picture_id => @original_picture.id, :license_id => @license.id
37 context 'extを検証するとき' do
39 @rp = Factory.build :resource_picture,
\r
40 :artist_id => @artist.id, :original_picture_id => @original_picture.id, :license_id => @license.id
48 @rp.should_not be_valid
52 @rp.should_not be_valid
54 it 'png,gif,jpeg以外なら失敗する' do
56 @rp.should_not be_valid
59 context 'widthを検証するとき' do
61 @rp = Factory.build :resource_picture,
\r
62 :artist_id => @artist.id, :original_picture_id => @original_picture.id, :license_id => @license.id
70 @rp.should_not be_valid
74 @rp.should_not be_valid
78 @rp.should_not be_valid
82 @rp.should_not be_valid
85 context 'heightを検証するとき' do
87 @rp = Factory.build :resource_picture,
\r
88 :artist_id => @artist.id, :original_picture_id => @original_picture.id, :license_id => @license.id
96 @rp.should_not be_valid
100 @rp.should_not be_valid
104 @rp.should_not be_valid
108 @rp.should_not be_valid
111 context 'filesizeを検証するとき' do
113 @rp = Factory.build :resource_picture,
\r
114 :artist_id => @artist.id, :original_picture_id => @original_picture.id, :license_id => @license.id
122 @rp.should_not be_valid
126 @rp.should_not be_valid
130 @rp.should_not be_valid
133 @rp.filesize = 2000000+1
134 @rp.should_not be_valid
137 context 'artist_idを検証するとき' do
139 @rp = Factory.build :resource_picture,
\r
140 :original_picture_id => @original_picture.id, :license_id => @license.id
143 @rp.artist_id = @artist.id
148 @rp.should_not be_valid
152 @rp.should_not be_valid
154 it '存在する絵師でなければ失敗する' do
156 @rp.should_not be_valid
159 context 'license_idを検証するとき' do
161 @rp = Factory.build :resource_picture,
\r
162 :artist_id => @artist.id, :original_picture_id => @original_picture.id
165 @rp.license_id = @license.id
170 @rp.should_not be_valid
174 @rp.should_not be_valid
176 it '存在する絵師でなければ失敗する' do
178 @rp.should_not be_valid
181 context 'original_picture_idを検証するとき' do
183 @rp = Factory.build :resource_picture,
\r
184 :artist_id => @artist.id, :license_id => @license.id
187 @rp.original_picture_id = @original_picture.id
191 @rp.original_picture_id = nil
192 @rp.should_not be_valid
195 @rp.original_picture_id = 'a'
196 @rp.should_not be_valid
198 it '存在する原画でなければ失敗する' do
199 @rp.original_picture_id = 0
200 @rp.should_not be_valid
205 describe 'データ補充に於いて' do
211 describe '単体取得に於いて' do
213 @op = Factory :original_picture, :artist_id => @artist.id, :license_id => @license.id
214 @rp = Factory :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => @op.id
217 pic = ResourcePicture.show @rp.id
220 context '関連テーブルオプションがないとき' do
221 it 'ライセンスと絵師を含んでいる' do
222 r = ResourcePicture.show_include_opt
223 r.should eq [:license, :artist]
226 context '関連テーブルオプションで原画を含ませたとき' do
227 it 'ライセンスと絵師と原画データを含んでいる' do
228 r = ResourcePicture.show_include_opt(:include => :original_picture)
229 r.should eq [:license, :artist, :original_picture]
232 context '存在しない素材を開こうとしたとき' do
233 it '404RecordNotFound例外を返す' do
235 pic = ResourcePicture.show 0
236 }.should raise_error(ActiveRecord::RecordNotFound)
240 describe 'json単体出力オプションに於いて' do
241 it 'includeキーがライセンスと絵師を含んでいる' do
242 r = ResourcePicture.show_json_include_opt
243 r[:include].should eq [:license, :artist]
246 describe '一覧取得に於いて' do
248 @op = Factory :original_picture, :artist_id => @artist.id, :license_id => @license.id
249 @rp = Factory :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => @op.id
251 context 'page補正について' do
252 it '文字列から数値に変換される' do
253 ResourcePicture.page('8').should eq 8
256 ResourcePicture.page().should eq 1
259 ResourcePicture.page('0').should eq 1
262 context 'page_size補正について' do
263 it '文字列から数値に変換される' do
264 ResourcePicture.page_size('7').should eq 7
266 it 'nilの場合はResourcePicture.default_page_sizeになる' do
267 ResourcePicture.page_size().should eq ResourcePicture.default_page_size
269 it '0以下の場合はResourcePicture.default_page_sizeになる' do
270 ResourcePicture.page_size('0').should eq ResourcePicture.default_page_size
272 it 'ResourcePicture.max_page_sizeを超えた場合はResourcePicture.max_page_sizeになる' do
273 ResourcePicture.page_size('1000').should eq ResourcePicture.max_page_size
277 pic = ResourcePicture.list
281 nop = Factory :original_picture, :artist_id => @artist.id, :license_id => @license.id
282 nrp = Factory :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => nop.id
283 pic = ResourcePicture.list
284 pic.should eq [nrp, @rp]
286 context 'DBに5件あって1ページの件数を2件に変えたとして' do
288 nop2 = Factory :original_picture, :artist_id => @artist.id, :license_id => @license.id
289 @nrp2 = Factory :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => nop2.id
290 nop3 = Factory :original_picture, :artist_id => @artist.id, :license_id => @license.id
291 @nrp3 = Factory :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => nop3.id
292 nop4 = Factory :original_picture, :artist_id => @artist.id, :license_id => @license.id
293 @nrp4 = Factory :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => nop4.id
294 nop5 = Factory :original_picture, :artist_id => @artist.id, :license_id => @license.id
295 @nrp5 = Factory :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => nop5.id
296 ResourcePicture.stub(:default_page_size).and_return(2)
299 pic = ResourcePicture.list
300 pic.should have(2).items
302 it 'page=1なら末尾2件を返す' do
304 pic = ResourcePicture.list({}, 1)
305 pic.should eq [@nrp5, @nrp4]
307 it 'page=2なら中間2件を返す' do
308 pic = ResourcePicture.list({}, 2)
309 pic.should eq [@nrp3, @nrp2]
311 it 'page=3なら先頭1件を返す' do
312 pic = ResourcePicture.list({}, 3)
317 describe 'json一覧出力オプションに於いて' do
318 it 'includeキーがライセンスと絵師を含んでいる' do
319 r = ResourcePicture.list_json_opt
320 r[:include].should eq [:license, :artist]
324 describe 'サイズの調整に於いて' do
328 it 'サイズに収まるときはそのまま使う' do
329 ResourcePicture.fix_size_both(64, 64, 64, 64).should eq [64, 64]
332 ResourcePicture.fix_size_both(64, 64, 32, 32).should eq [32, 32]
334 it '縦長のときは縦に合わせて縮小' do
335 ResourcePicture.fix_size_both(64, 64, 64, 128).should eq [32, 64]
337 it '横長のときは横に合わせて縮小' do
338 ResourcePicture.fix_size_both(64, 64, 128, 64).should eq [64, 32]
342 describe 'フォーマット変換対象判定に於いて' do
344 @rp = Factory.build :resource_picture,
\r
345 :artist_id => @artist.id, :license_id => @license.id
347 context '変換するケース' do
\r
348 it '画像フォーマットがpngかつライセンスの変換禁止フラグが無効のときTrue' do
\r
349 License.any_instance.stub(:no_convert).with(any_args).and_return(0)
351 @rp.to_gif?.should be_true
354 context '変換しないケース' do
355 it '画像フォーマットがでない' do
\r
356 License.any_instance.stub(:no_convert).with(any_args).and_return(0)
358 @rp.to_gif?.should be_false
360 it 'ライセンスの変換禁止フラグが無効' do
\r
361 License.any_instance.stub(:no_convert).with(any_args).and_return(1)
363 @rp.to_gif?.should be_false
368 describe 'Gifフォーマット変換に於いて' do
370 Magick::Image.stub(:from_blob).with(any_args).and_return([Mgk.new])
372 context 'つつがなく終わるとき' do
374 mgk = ResourcePicture.png_to_gif('mgkbin')
\r
375 mgk.is_a?(Mgk).should be_true
377 it 'Mgkオブジェクトはgif変換されている' do
\r
378 #スタブばかりで変換できないので代入されているかでチェックする
379 Mgk.any_instance.should_receive(:format=).with('gif').exactly(1)
380 ResourcePicture.png_to_gif('mgkbin')
\r
383 context 'RMagick変換が失敗したとき' do
385 Magick::Image.should_receive(:from_blob).with(any_args).and_raise('StandardError')
388 res = ResourcePicture.png_to_gif('mgkbin')
\r
389 res.should be_false
\r
394 describe '対象素材の取得に於いて' do
396 @op = Factory.build :original_picture, :ext => 'jpeg', :width => 264, :height => 265, :filesize => 266,
\r
397 :artist_id => @artist.id, :license_id => @license.id
398 @rp = Factory.build :resource_picture,
\r
399 :artist_id => @artist.id, :license_id => @license.id
401 context '原画オブジェクトが素材を持っている(更新ケース)' do
403 OriginalPicture.any_instance.stub(:resource_picture).with(any_args).and_return(@rp)
\r
406 res = ResourcePicture.update_picture(@op)
\r
410 context '持っていない(新規作成ケース)' do
412 OriginalPicture.any_instance.stub(:resource_picture).with(any_args).and_return(nil)
\r
414 it '新規素材モデルを用意し、それを対象素材として返す' do
415 res = ResourcePicture.update_picture(@op)
\r
416 res.should be_a_new ResourcePicture
\r
418 it '原画オブジェクトから属性を取り出して対象素材にセットしている' do
419 res = ResourcePicture.update_picture(@op)
\r
420 res.ext.should eq 'jpeg'
\r
421 res.width.should eq 264
\r
422 res.height.should eq 265
\r
423 res.filesize.should eq 266
\r
424 res.artist_id.should eq @artist.id
\r
425 res.license_id.should eq @license.id
\r
431 describe '作成・更新に於いて' do
433 @op = Factory :original_picture, :artist_id => @artist.id, :license_id => @license.id
434 @rp = Factory.build :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => @op.id
438 #すべての処理が正常パターンで通過すれば、一番深い分岐まで通る。
439 #それで外部のメソッド呼び出しだけに着目してテストする。
440 ResourcePicture.any_instance.stub(:save).with(any_args).and_return(true)
\r
441 ResourcePicture.any_instance.stub(:store_picture).with(any_args).and_return(true)
\r
442 ResourcePicture.stub(:to_gif?).with(any_args).and_return(true)
\r
443 class GifMgk < Mgk ; end #store_pictureは二回呼び出される。区別をつけるために
\r
444 @gifmgk = GifMgk.new #パラメータを二種類用意する。
\r
445 ResourcePicture.stub(:png_to_gif).with(any_args).and_return(@gifmgk)
448 ResourcePicture.any_instance.should_receive(:save).with(any_args).exactly(1)
449 res = @rp.store(Mgk.new)
\r
451 it '画像ファイルの作成・更新機能で画像を保存している' do
\r
452 mgk = Mgk.new #一回目の本画像保存は与えたオブジェクトを使って保存する。
453 ResourcePicture.any_instance.should_receive(:store_picture).with(mgk).exactly(1)
454 res = @rp.store(mgk)
\r
456 it '自身にフォーマット変換対象かを問い合わせている' do
457 ResourcePicture.any_instance.should_receive(:to_gif?).with(any_args).exactly(1)
458 res = @rp.store(Mgk.new)
\r
460 it '自身にGifフォーマット変換を依頼している' do
461 ResourcePicture.should_receive(:png_to_gif).with(any_args).exactly(1)
462 res = @rp.store(Mgk.new)
\r
464 it '画像ファイルの作成・更新機能でgif画像を保存している' do
\r
466 ResourcePicture.any_instance.should_receive(:store_picture).with(@gifmgk).exactly(1)
467 res = @rp.store(Mgk.new)
\r
470 context 'つつがなく終わるとき' do
472 #すべての処理を正常パターンで通過させ、保存機能をチェックする。
473 # ResourcePicture.any_instance.stub(:save).with(any_args).and_return(true)
\r
474 ResourcePicture.any_instance.stub(:store_picture).with(any_args).and_return(true)
\r
475 ResourcePicture.any_instance.stub(:to_gif?).with(any_args).and_return(true)
\r
476 class GifMgk < Mgk ; end #store_pictureは二回呼び出される。区別をつけるために
\r
477 @gifmgk = GifMgk.new #パラメータを二種類用意する。
\r
478 ResourcePicture.stub(:png_to_gif).with(any_args).and_return(@gifmgk)
481 res = @rp.store(Mgk.new)
\r
486 res = @rp.store(Mgk.new)
\r
487 }.should change ResourcePicture, :count
\r
490 context 'gif変換なしで、つつがなく終わるとき' do
492 #すべての処理を正常パターンで通過させ、保存機能をチェックする。
493 ResourcePicture.any_instance.stub(:save).with(any_args).and_return(true)
\r
494 ResourcePicture.any_instance.stub(:store_picture).with(any_args).and_return(true)
\r
495 ResourcePicture.any_instance.stub(:to_gif?).with(any_args).and_return(false)
\r
498 res = @rp.store(Mgk.new)
\r
501 it 'gif保存は呼ばれていない' do
\r
502 ResourcePicture.any_instance.should_receive(:store_picture).with(any_args).exactly(1)
503 res = @rp.store(Mgk.new)
\r
506 #以下から例外ケース。処理先頭から失敗させていく
507 context '自身の保存に失敗したとき' do
509 ResourcePicture.any_instance.stub(:save).with(any_args).and_return(false)
\r
512 res = @rp.store(Mgk.new)
\r
513 res.should be_false
\r
517 @rp.should be_a_new ResourcePicture
\r
520 context '画像の保存に失敗したとき' do
522 ResourcePicture.any_instance.stub(:save).with(any_args).and_return(true)
\r
523 ResourcePicture.any_instance.stub(:store_picture).with(any_args).and_return(false)
\r
526 res = @rp.store(Mgk.new)
\r
527 res.should be_false
\r
529 it 'gif変換判定は呼ばれていない' do
\r
530 ResourcePicture.any_instance.should_not_receive(:to_gif?).with(any_args)
531 res = @rp.store(Mgk.new)
\r
534 context 'gif変換に失敗したとき' do
536 ResourcePicture.any_instance.stub(:save).with(any_args).and_return(true)
\r
537 ResourcePicture.any_instance.stub(:store_picture).with(any_args).and_return(true)
\r
538 ResourcePicture.any_instance.stub(:to_gif?).with(any_args).and_return(true)
\r
539 ResourcePicture.stub(:png_to_gif).with(any_args).and_return(false)
542 res = @rp.store(Mgk.new)
\r
543 res.should be_false
\r
545 it 'gif画像の保存は呼ばれていない' do
\r
546 #本画像の保存があるので、一度は呼ばれる
\r
547 ResourcePicture.any_instance.should_receive(:store_picture).with(any_args).exactly(1)
548 res = @rp.store(Mgk.new)
\r
551 context 'gif画像の保存に失敗したとき' do
554 ResourcePicture.any_instance.stub(:save).with(any_args).and_return(true)
\r
555 ResourcePicture.any_instance.stub(:store_picture).with(@mgk).and_return(true)
\r
556 ResourcePicture.any_instance.stub(:to_gif?).with(any_args).and_return(true)
\r
557 class GifMgk < Mgk ; end #store_pictureは二回呼び出される。区別をつけるために
\r
558 @gifmgk = GifMgk.new #パラメータを二種類用意する。
\r
559 ResourcePicture.stub(:png_to_gif).with(any_args).and_return(@gifmgk)
560 ResourcePicture.any_instance.stub(:store_picture).with(@gifmgk).and_return(false)
\r
563 res = @rp.store(@mgk)
\r
564 res.should be_false
\r
569 describe '画像ファイルの作成・更新に於いて' do
\r
570 #PictureIo経由で画像を保存するための機能。ファイル名に自身のidを使うので事前に自身の保存が必要。
\r
572 PictureIO.resource_picture_io.class.stub(:subdirs).with(any_args).and_return(['', 'v', 'h', 'vh', 'thumbnail'])
\r
573 ResourcePicture.any_instance.stub(:h).with(any_args).and_return('data')
574 ResourcePicture.any_instance.stub(:v).with(any_args).and_return('data')
575 ResourcePicture.any_instance.stub(:vh).with(any_args).and_return('data')
576 ResourcePicture.any_instance.stub(:thumbnail).with(any_args).and_return('data')
577 @op = Factory :original_picture, :artist_id => @artist.id, :license_id => @license.id
578 @rp = Factory :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => @op.id
582 #すべての処理を正常パターンで通過させ、保存機能をチェックする。
583 PictureIO.resource_picture_io.stub(:put).with(any_args).and_return(true)
585 it '画像ファイルの保存が5回呼ばれる' do
\r
586 PictureIO.resource_picture_io.should_receive(:put).with(any_args).exactly(5)
587 res = @rp.store_picture(Mgk.new)
\r
589 it '画像ファイルのベースへの保存が1回呼ばれる' do
\r
590 PictureIO.resource_picture_io.should_receive(:put).with('data', @rp.filename, '').exactly(1)
591 res = @rp.store_picture(Mgk.new)
\r
593 it '画像ファイルの垂直反転への保存が1回呼ばれる' do
\r
594 PictureIO.resource_picture_io.should_receive(:put).with('data', @rp.filename, 'v').exactly(1)
595 res = @rp.store_picture(Mgk.new)
\r
597 it '画像ファイルの水平反転への保存が1回呼ばれる' do
\r
598 PictureIO.resource_picture_io.should_receive(:put).with('data', @rp.filename, 'h').exactly(1)
599 res = @rp.store_picture(Mgk.new)
\r
601 it '画像ファイルの垂直水平反転への保存が1回呼ばれる' do
\r
602 PictureIO.resource_picture_io.should_receive(:put).with('data', @rp.filename, 'vh').exactly(1)
603 res = @rp.store_picture(Mgk.new)
\r
605 it '画像ファイルのサムネイルへの保存が1回呼ばれる' do
\r
606 PictureIO.resource_picture_io.should_receive(:put).with('data', @rp.filename, 'thumbnail').exactly(1)
607 res = @rp.store_picture(Mgk.new)
\r
609 it '垂直反転が1回呼ばれる' do
\r
610 ResourcePicture.any_instance.should_receive(:v).with(any_args).exactly(1)
611 res = @rp.store_picture(Mgk.new)
\r
613 it '水平反転が1回呼ばれる' do
\r
614 ResourcePicture.any_instance.should_receive(:h).with(any_args).exactly(1)
615 res = @rp.store_picture(Mgk.new)
\r
617 it '垂直水平反転が1回呼ばれる' do
\r
618 ResourcePicture.any_instance.should_receive(:vh).with(any_args).exactly(1)
619 res = @rp.store_picture(Mgk.new)
\r
621 it 'サムネイル化が1回呼ばれる' do
\r
622 ResourcePicture.any_instance.should_receive(:thumbnail).with(any_args).exactly(1)
623 res = @rp.store_picture(Mgk.new)
\r
626 context 'つつがなく終わるとき' do
628 #すべての処理を正常パターンで通過させ、保存機能をチェックする。
\r
629 PictureIO.resource_picture_io.stub(:put).with(any_args).and_return(true)
632 res = @rp.store_picture(Mgk.new)
\r
638 PictureIO.resource_picture_io.stub(:put).with(any_args).and_return(false)
641 res = @rp.store_picture(Mgk.new)
\r
642 res.should be_false
\r
648 describe 'サムネイル変換に於いて' do
649 #サムネイル化した画像データを返すが、スタブをおくので、リサイズと画像データ化を試みるかをチェックする
651 ResourcePicture.stub(:resize).with(any_args).and_return(Mgk.new)
652 @rp = Factory :resource_picture, :artist_id => @artist.id, :license_id => @license.id
654 it 'サムネイルサイズに縮小した画像データを返す' do
\r
655 ResourcePicture.should_receive(:resize).with(any_args).exactly(1)
656 @rp.thumbnail(Mgk.new)
\r
660 @rp.thumbnail(Mgk.new).should eq 'data'
\r
664 describe '垂直反転変換に於いて' do
665 #垂直反転した画像データを返すが、スタブをおくので、反転と画像データ化を試みるかをチェックする
667 Mgk.any_instance.stub(:flip).with(any_args).and_return(Mgk.new)
668 @rp = Factory :resource_picture, :artist_id => @artist.id, :license_id => @license.id
671 Mgk.any_instance.should_receive(:flip).exactly(1)
675 Mgk.any_instance.should_receive(:to_blob).exactly(1)
680 @rp.v(Mgk.new).should eq 'data'
\r
684 describe '水平反転変換に於いて' do
685 #水平反転した画像データを返すが、スタブをおくので、反転と画像データ化を試みるかをチェックする
687 Mgk.any_instance.stub(:flop).with(any_args).and_return(Mgk.new)
688 @rp = Factory :resource_picture, :artist_id => @artist.id, :license_id => @license.id
691 Mgk.any_instance.should_receive(:flop).exactly(1)
695 Mgk.any_instance.should_receive(:to_blob).exactly(1)
700 @rp.h(Mgk.new).should eq 'data'
\r
704 describe '垂直水平反転変換に於いて' do
705 #垂直水平反転した画像データを返すが、スタブをおくので、反転と画像データ化を試みるかをチェックする
707 Mgk.any_instance.stub(:flip).with(any_args).and_return(Mgk.new)
708 Mgk.any_instance.stub(:flop).with(any_args).and_return(Mgk.new)
709 @rp = Factory :resource_picture, :artist_id => @artist.id, :license_id => @license.id
712 Mgk.any_instance.should_receive(:flip).exactly(1)
716 Mgk.any_instance.should_receive(:flop).exactly(1)
720 Mgk.any_instance.should_receive(:to_blob).exactly(1)
725 @rp.vh(Mgk.new).should eq 'data'
\r