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 @sp = Factory :system_picture
14 @lg = Factory :license_group
15 @license = Factory :license, :license_group_id => @lg.id, :system_picture_id => @sp.id
16 @original_picture = Factory :original_picture, :artist_id => @artist.id, :license_id => @license.id
\r
18 class Mgk ; class Image ; end ; end
19 Mgk::Image.stub(:from_blob).with(any_args).and_return([Mgk.new])
20 Mgk.any_instance.stub(:format).with(any_args).and_return('png')
21 Mgk.any_instance.stub(:format=).with(any_args)
22 Mgk.any_instance.stub(:rows).with(any_args).and_return(200)
23 Mgk.any_instance.stub(:columns).with(any_args).and_return(100)
24 Mgk.any_instance.stub(:filesize).with(any_args).and_return(20000)
25 Mgk.any_instance.stub(:to_blob).with(any_args).and_return('data')
26 #原画ファイル削除だけは必ず成功するものとしておく
27 PictureIO::LocalPicture.any_instance.stub(:delete).with(any_args).and_return(true)
34 it 'オーソドックスなデータなら通る' do
35 @rp = Factory.build :resource_picture, :artist_id => @artist.id, :original_picture_id => @original_picture.id, :license_id => @license.id
39 context 'extを検証するとき' do
41 @rp = Factory.build :resource_picture,
\r
42 :artist_id => @artist.id, :original_picture_id => @original_picture.id, :license_id => @license.id
50 @rp.should_not be_valid
54 @rp.should_not be_valid
56 it 'png,gif,jpeg以外なら失敗する' do
58 @rp.should_not be_valid
61 context 'widthを検証するとき' do
63 @rp = Factory.build :resource_picture,
\r
64 :artist_id => @artist.id, :original_picture_id => @original_picture.id, :license_id => @license.id
72 @rp.should_not be_valid
76 @rp.should_not be_valid
80 @rp.should_not be_valid
84 @rp.should_not be_valid
87 context 'heightを検証するとき' do
89 @rp = Factory.build :resource_picture,
\r
90 :artist_id => @artist.id, :original_picture_id => @original_picture.id, :license_id => @license.id
98 @rp.should_not be_valid
102 @rp.should_not be_valid
106 @rp.should_not be_valid
110 @rp.should_not be_valid
113 context 'filesizeを検証するとき' do
115 @rp = Factory.build :resource_picture,
\r
116 :artist_id => @artist.id, :original_picture_id => @original_picture.id, :license_id => @license.id
124 @rp.should_not be_valid
128 @rp.should_not be_valid
132 @rp.should_not be_valid
135 @rp.filesize = 2000000+1
136 @rp.should_not be_valid
139 context 'artist_idを検証するとき' do
141 @rp = Factory.build :resource_picture,
\r
142 :original_picture_id => @original_picture.id, :license_id => @license.id
145 @rp.artist_id = @artist.id
150 @rp.should_not be_valid
154 @rp.should_not be_valid
156 it '存在する絵師でなければ失敗する' do
158 @rp.should_not be_valid
161 context 'license_idを検証するとき' do
163 @rp = Factory.build :resource_picture,
\r
164 :artist_id => @artist.id, :original_picture_id => @original_picture.id
167 @rp.license_id = @license.id
172 @rp.should_not be_valid
176 @rp.should_not be_valid
178 it '存在する絵師でなければ失敗する' do
180 @rp.should_not be_valid
183 context 'original_picture_idを検証するとき' do
185 @rp = Factory.build :resource_picture,
\r
186 :artist_id => @artist.id, :license_id => @license.id
189 @rp.original_picture_id = @original_picture.id
193 @rp.original_picture_id = nil
194 @rp.should_not be_valid
197 @rp.original_picture_id = 'a'
198 @rp.should_not be_valid
200 it '存在する原画でなければ失敗する' do
201 @rp.original_picture_id = 0
202 @rp.should_not be_valid
207 describe 'データ補充に於いて' do
213 describe '単体取得に於いて' do
215 @op = Factory :original_picture, :artist_id => @artist.id, :license_id => @license.id
216 @rp = Factory :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => @op.id
219 pic = ResourcePicture.show @rp.id
222 context '関連テーブルオプションがないとき' do
223 it 'ライセンスと絵師を含んでいる' do
224 r = ResourcePicture.show_include_opt
225 r.should eq [:license, :artist]
228 context '関連テーブルオプションで原画を含ませたとき' do
229 it 'ライセンスと絵師と原画データを含んでいる' do
230 r = ResourcePicture.show_include_opt(:include => :original_picture)
231 r.should eq [:license, :artist, :original_picture]
234 context '存在しない素材を開こうとしたとき' do
235 it '404RecordNotFound例外を返す' do
237 pic = ResourcePicture.show 0
238 }.should raise_error(ActiveRecord::RecordNotFound)
242 describe 'json単体出力オプションに於いて' do
243 it 'includeキーがライセンスと絵師を含んでいる' do
244 r = ResourcePicture.show_json_include_opt
245 r[:include].should eq [:license, :artist]
248 describe '一覧取得に於いて' do
250 @op = Factory :original_picture, :artist_id => @artist.id, :license_id => @license.id
251 @rp = Factory :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => @op.id
253 context 'page補正について' do
254 it '文字列から数値に変換される' do
255 ResourcePicture.page('8').should eq 8
258 ResourcePicture.page().should eq 1
261 ResourcePicture.page('0').should eq 1
264 context 'page_size補正について' do
265 it '文字列から数値に変換される' do
266 ResourcePicture.page_size('7').should eq 7
268 it 'nilの場合はResourcePicture.default_page_sizeになる' do
269 ResourcePicture.page_size().should eq ResourcePicture.default_page_size
271 it '0以下の場合はResourcePicture.default_page_sizeになる' do
272 ResourcePicture.page_size('0').should eq ResourcePicture.default_page_size
274 it 'ResourcePicture.max_page_sizeを超えた場合はResourcePicture.max_page_sizeになる' do
275 ResourcePicture.page_size('1000').should eq ResourcePicture.max_page_size
279 pic = ResourcePicture.list
283 nop = Factory :original_picture, :artist_id => @artist.id, :license_id => @license.id
284 nrp = Factory :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => nop.id
285 pic = ResourcePicture.list
286 pic.should eq [nrp, @rp]
288 context 'DBに5件あって1ページの件数を2件に変えたとして' do
290 nop2 = Factory :original_picture, :artist_id => @artist.id, :license_id => @license.id
291 @nrp2 = Factory :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => nop2.id
292 nop3 = Factory :original_picture, :artist_id => @artist.id, :license_id => @license.id
293 @nrp3 = Factory :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => nop3.id
294 nop4 = Factory :original_picture, :artist_id => @artist.id, :license_id => @license.id
295 @nrp4 = Factory :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => nop4.id
296 nop5 = Factory :original_picture, :artist_id => @artist.id, :license_id => @license.id
297 @nrp5 = Factory :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => nop5.id
298 ResourcePicture.stub(:default_page_size).and_return(2)
301 pic = ResourcePicture.list
302 pic.should have(2).items
304 it 'page=1なら末尾2件を返す' do
306 pic = ResourcePicture.list({}, 1)
307 pic.should eq [@nrp5, @nrp4]
309 it 'page=2なら中間2件を返す' do
310 pic = ResourcePicture.list({}, 2)
311 pic.should eq [@nrp3, @nrp2]
313 it 'page=3なら先頭1件を返す' do
314 pic = ResourcePicture.list({}, 3)
319 describe 'json一覧出力オプションに於いて' do
320 it 'includeキーがライセンスと絵師を含んでいる' do
321 r = ResourcePicture.list_json_opt
322 r[:include].should eq [:license, :artist]
326 describe 'サイズの調整に於いて' do
330 it 'サイズに収まるときはそのまま使う' do
331 ResourcePicture.fix_size_both(64, 64, 64, 64).should eq [64, 64]
334 ResourcePicture.fix_size_both(64, 64, 32, 32).should eq [32, 32]
336 it '縦長のときは縦に合わせて縮小' do
337 ResourcePicture.fix_size_both(64, 64, 64, 128).should eq [32, 64]
339 it '横長のときは横に合わせて縮小' do
340 ResourcePicture.fix_size_both(64, 64, 128, 64).should eq [64, 32]
344 describe 'フォーマット変換対象判定に於いて' do
346 @rp = Factory.build :resource_picture,
\r
347 :artist_id => @artist.id, :license_id => @license.id
349 context '変換するケース' do
\r
350 it '画像フォーマットがpngかつライセンスの変換禁止フラグが無効のときTrue' do
\r
351 License.any_instance.stub(:no_convert).with(any_args).and_return(0)
353 @rp.to_gif?.should be_true
356 context '変換しないケース' do
357 it '画像フォーマットがでない' do
\r
358 License.any_instance.stub(:no_convert).with(any_args).and_return(0)
360 @rp.to_gif?.should be_false
362 it 'ライセンスの変換禁止フラグが無効' do
\r
363 License.any_instance.stub(:no_convert).with(any_args).and_return(1)
365 @rp.to_gif?.should be_false
370 describe 'Gifフォーマット変換に於いて' do
372 Magick::Image.stub(:from_blob).with(any_args).and_return([Mgk.new])
374 context 'つつがなく終わるとき' do
376 mgk = ResourcePicture.png_to_gif('mgkbin')
\r
377 mgk.is_a?(Mgk).should be_true
379 it 'Mgkオブジェクトはgif変換されている' do
\r
380 #スタブばかりで変換できないので代入されているかでチェックする
381 Mgk.any_instance.should_receive(:format=).with('gif').exactly(1)
382 ResourcePicture.png_to_gif('mgkbin')
\r
385 context 'RMagick変換が失敗したとき' do
387 Magick::Image.should_receive(:from_blob).with(any_args).and_raise('StandardError')
390 res = ResourcePicture.png_to_gif('mgkbin')
\r
391 res.should be_false
\r
396 describe '対象素材の取得に於いて' do
398 @op = Factory.build :original_picture, :ext => 'jpeg', :width => 264, :height => 265, :filesize => 266,
\r
399 :artist_id => @artist.id, :license_id => @license.id
400 @rp = Factory.build :resource_picture,
\r
401 :artist_id => @artist.id, :license_id => @license.id
403 context '原画オブジェクトが素材を持っている(更新ケース)' do
405 OriginalPicture.any_instance.stub(:resource_picture).with(any_args).and_return(@rp)
\r
408 res = ResourcePicture.update_picture(@op)
\r
412 context '持っていない(新規作成ケース)' do
414 OriginalPicture.any_instance.stub(:resource_picture).with(any_args).and_return(nil)
\r
416 it '新規素材モデルを用意し、それを対象素材として返す' do
417 res = ResourcePicture.update_picture(@op)
\r
418 res.should be_a_new ResourcePicture
\r
420 it '原画オブジェクトから属性を取り出して対象素材にセットしている' do
421 res = ResourcePicture.update_picture(@op)
\r
422 res.ext.should eq 'jpeg'
\r
423 res.width.should eq 264
\r
424 res.height.should eq 265
\r
425 res.filesize.should eq 266
\r
426 res.artist_id.should eq @artist.id
\r
427 res.license_id.should eq @license.id
\r
433 describe '作成・更新に於いて' do
435 @op = Factory :original_picture, :artist_id => @artist.id, :license_id => @license.id
436 @rp = Factory.build :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => @op.id
440 #すべての処理が正常パターンで通過すれば、一番深い分岐まで通る。
441 #それで外部のメソッド呼び出しだけに着目してテストする。
442 ResourcePicture.any_instance.stub(:save).with(any_args).and_return(true)
\r
443 ResourcePicture.any_instance.stub(:store_picture).with(any_args).and_return(true)
\r
444 ResourcePicture.stub(:to_gif?).with(any_args).and_return(true)
\r
445 class GifMgk < Mgk ; end #store_pictureは二回呼び出される。区別をつけるために
\r
446 @gifmgk = GifMgk.new #パラメータを二種類用意する。
\r
447 ResourcePicture.stub(:png_to_gif).with(any_args).and_return(@gifmgk)
450 ResourcePicture.any_instance.should_receive(:save).with(any_args).exactly(1)
451 res = @rp.store(Mgk.new)
\r
453 it '画像ファイルの作成・更新機能で画像を保存している' do
\r
454 mgk = Mgk.new #一回目の本画像保存は与えたオブジェクトを使って保存する。
455 ResourcePicture.any_instance.should_receive(:store_picture).with(mgk).exactly(1)
456 res = @rp.store(mgk)
\r
458 it '自身にフォーマット変換対象かを問い合わせている' do
459 ResourcePicture.any_instance.should_receive(:to_gif?).with(any_args).exactly(1)
460 res = @rp.store(Mgk.new)
\r
462 it '自身にGifフォーマット変換を依頼している' do
463 ResourcePicture.should_receive(:png_to_gif).with(any_args).exactly(1)
464 res = @rp.store(Mgk.new)
\r
466 it '画像ファイルの作成・更新機能でgif画像を保存している' do
\r
468 ResourcePicture.any_instance.should_receive(:store_picture).with(@gifmgk).exactly(1)
469 res = @rp.store(Mgk.new)
\r
472 context 'つつがなく終わるとき' do
474 #すべての処理を正常パターンで通過させ、保存機能をチェックする。
475 # ResourcePicture.any_instance.stub(:save).with(any_args).and_return(true)
\r
476 ResourcePicture.any_instance.stub(:store_picture).with(any_args).and_return(true)
\r
477 ResourcePicture.any_instance.stub(:to_gif?).with(any_args).and_return(true)
\r
478 class GifMgk < Mgk ; end #store_pictureは二回呼び出される。区別をつけるために
\r
479 @gifmgk = GifMgk.new #パラメータを二種類用意する。
\r
480 ResourcePicture.stub(:png_to_gif).with(any_args).and_return(@gifmgk)
483 res = @rp.store(Mgk.new)
\r
488 res = @rp.store(Mgk.new)
\r
489 }.should change ResourcePicture, :count
\r
492 context 'gif変換なしで、つつがなく終わるとき' do
494 #すべての処理を正常パターンで通過させ、保存機能をチェックする。
495 ResourcePicture.any_instance.stub(:save).with(any_args).and_return(true)
\r
496 ResourcePicture.any_instance.stub(:store_picture).with(any_args).and_return(true)
\r
497 ResourcePicture.any_instance.stub(:to_gif?).with(any_args).and_return(false)
\r
500 res = @rp.store(Mgk.new)
\r
503 it 'gif保存は呼ばれていない' do
\r
504 ResourcePicture.any_instance.should_receive(:store_picture).with(any_args).exactly(1)
505 res = @rp.store(Mgk.new)
\r
508 #以下から例外ケース。処理先頭から失敗させていく
509 context '自身の保存に失敗したとき' do
511 ResourcePicture.any_instance.stub(:save).with(any_args).and_return(false)
\r
514 res = @rp.store(Mgk.new)
\r
515 res.should be_false
\r
519 @rp.should be_a_new ResourcePicture
\r
522 context '画像の保存に失敗したとき' do
524 ResourcePicture.any_instance.stub(:save).with(any_args).and_return(true)
\r
525 ResourcePicture.any_instance.stub(:store_picture).with(any_args).and_return(false)
\r
528 res = @rp.store(Mgk.new)
\r
529 res.should be_false
\r
531 it 'gif変換判定は呼ばれていない' do
\r
532 ResourcePicture.any_instance.should_not_receive(:to_gif?).with(any_args)
533 res = @rp.store(Mgk.new)
\r
536 context 'gif変換に失敗したとき' do
538 ResourcePicture.any_instance.stub(:save).with(any_args).and_return(true)
\r
539 ResourcePicture.any_instance.stub(:store_picture).with(any_args).and_return(true)
\r
540 ResourcePicture.any_instance.stub(:to_gif?).with(any_args).and_return(true)
\r
541 ResourcePicture.stub(:png_to_gif).with(any_args).and_return(false)
544 res = @rp.store(Mgk.new)
\r
545 res.should be_false
\r
547 it 'gif画像の保存は呼ばれていない' do
\r
548 #本画像の保存があるので、一度は呼ばれる
\r
549 ResourcePicture.any_instance.should_receive(:store_picture).with(any_args).exactly(1)
550 res = @rp.store(Mgk.new)
\r
553 context 'gif画像の保存に失敗したとき' do
556 ResourcePicture.any_instance.stub(:save).with(any_args).and_return(true)
\r
557 ResourcePicture.any_instance.stub(:store_picture).with(@mgk).and_return(true)
\r
558 ResourcePicture.any_instance.stub(:to_gif?).with(any_args).and_return(true)
\r
559 class GifMgk < Mgk ; end #store_pictureは二回呼び出される。区別をつけるために
\r
560 @gifmgk = GifMgk.new #パラメータを二種類用意する。
\r
561 ResourcePicture.stub(:png_to_gif).with(any_args).and_return(@gifmgk)
562 ResourcePicture.any_instance.stub(:store_picture).with(@gifmgk).and_return(false)
\r
565 res = @rp.store(@mgk)
\r
566 res.should be_false
\r
571 describe '画像ファイルの作成・更新に於いて' do
\r
572 #PictureIo経由で画像を保存するための機能。ファイル名に自身のidを使うので事前に自身の保存が必要。
\r
574 PictureIO.resource_picture_io.class.stub(:subdirs).with(any_args).and_return(['', 'v', 'h', 'vh', 'thumbnail'])
\r
575 ResourcePicture.any_instance.stub(:h).with(any_args).and_return('data')
576 ResourcePicture.any_instance.stub(:v).with(any_args).and_return('data')
577 ResourcePicture.any_instance.stub(:vh).with(any_args).and_return('data')
578 ResourcePicture.any_instance.stub(:thumbnail).with(any_args).and_return('data')
579 @op = Factory :original_picture, :artist_id => @artist.id, :license_id => @license.id
580 @rp = Factory :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => @op.id
584 #すべての処理を正常パターンで通過させ、保存機能をチェックする。
585 PictureIO.resource_picture_io.stub(:put).with(any_args).and_return(true)
587 it '画像ファイルの保存が5回呼ばれる' do
\r
588 PictureIO.resource_picture_io.should_receive(:put).with(any_args).exactly(5)
589 res = @rp.store_picture(Mgk.new)
\r
591 it '画像ファイルのベースへの保存が1回呼ばれる' do
\r
592 PictureIO.resource_picture_io.should_receive(:put).with('data', @rp.filename, '').exactly(1)
593 res = @rp.store_picture(Mgk.new)
\r
595 it '画像ファイルの垂直反転への保存が1回呼ばれる' do
\r
596 PictureIO.resource_picture_io.should_receive(:put).with('data', @rp.filename, 'v').exactly(1)
597 res = @rp.store_picture(Mgk.new)
\r
599 it '画像ファイルの水平反転への保存が1回呼ばれる' do
\r
600 PictureIO.resource_picture_io.should_receive(:put).with('data', @rp.filename, 'h').exactly(1)
601 res = @rp.store_picture(Mgk.new)
\r
603 it '画像ファイルの垂直水平反転への保存が1回呼ばれる' do
\r
604 PictureIO.resource_picture_io.should_receive(:put).with('data', @rp.filename, 'vh').exactly(1)
605 res = @rp.store_picture(Mgk.new)
\r
607 it '画像ファイルのサムネイルへの保存が1回呼ばれる' do
\r
608 PictureIO.resource_picture_io.should_receive(:put).with('data', @rp.filename, 'thumbnail').exactly(1)
609 res = @rp.store_picture(Mgk.new)
\r
611 it '垂直反転が1回呼ばれる' do
\r
612 ResourcePicture.any_instance.should_receive(:v).with(any_args).exactly(1)
613 res = @rp.store_picture(Mgk.new)
\r
615 it '水平反転が1回呼ばれる' do
\r
616 ResourcePicture.any_instance.should_receive(:h).with(any_args).exactly(1)
617 res = @rp.store_picture(Mgk.new)
\r
619 it '垂直水平反転が1回呼ばれる' do
\r
620 ResourcePicture.any_instance.should_receive(:vh).with(any_args).exactly(1)
621 res = @rp.store_picture(Mgk.new)
\r
623 it 'サムネイル化が1回呼ばれる' do
\r
624 ResourcePicture.any_instance.should_receive(:thumbnail).with(any_args).exactly(1)
625 res = @rp.store_picture(Mgk.new)
\r
628 context 'つつがなく終わるとき' do
630 #すべての処理を正常パターンで通過させ、保存機能をチェックする。
\r
631 PictureIO.resource_picture_io.stub(:put).with(any_args).and_return(true)
634 res = @rp.store_picture(Mgk.new)
\r
640 PictureIO.resource_picture_io.stub(:put).with(any_args).and_return(false)
643 res = @rp.store_picture(Mgk.new)
\r
644 res.should be_false
\r
650 describe 'サムネイル変換に於いて' do
651 #サムネイル化した画像データを返すが、スタブをおくので、リサイズと画像データ化を試みるかをチェックする
653 ResourcePicture.stub(:resize).with(any_args).and_return(Mgk.new)
654 @rp = Factory :resource_picture, :artist_id => @artist.id, :license_id => @license.id
656 it 'サムネイルサイズに縮小した画像データを返す' do
\r
657 ResourcePicture.should_receive(:resize).with(any_args).exactly(1)
658 @rp.thumbnail(Mgk.new)
\r
662 @rp.thumbnail(Mgk.new).should eq 'data'
\r
666 describe '垂直反転変換に於いて' do
667 #垂直反転した画像データを返すが、スタブをおくので、反転と画像データ化を試みるかをチェックする
669 Mgk.any_instance.stub(:flip).with(any_args).and_return(Mgk.new)
670 @rp = Factory :resource_picture, :artist_id => @artist.id, :license_id => @license.id
673 Mgk.any_instance.should_receive(:flip).exactly(1)
677 Mgk.any_instance.should_receive(:to_blob).exactly(1)
682 @rp.v(Mgk.new).should eq 'data'
\r
686 describe '水平反転変換に於いて' do
687 #水平反転した画像データを返すが、スタブをおくので、反転と画像データ化を試みるかをチェックする
689 Mgk.any_instance.stub(:flop).with(any_args).and_return(Mgk.new)
690 @rp = Factory :resource_picture, :artist_id => @artist.id, :license_id => @license.id
693 Mgk.any_instance.should_receive(:flop).exactly(1)
697 Mgk.any_instance.should_receive(:to_blob).exactly(1)
702 @rp.h(Mgk.new).should eq 'data'
\r
706 describe '垂直水平反転変換に於いて' do
707 #垂直水平反転した画像データを返すが、スタブをおくので、反転と画像データ化を試みるかをチェックする
709 Mgk.any_instance.stub(:flip).with(any_args).and_return(Mgk.new)
710 Mgk.any_instance.stub(:flop).with(any_args).and_return(Mgk.new)
711 @rp = Factory :resource_picture, :artist_id => @artist.id, :license_id => @license.id
714 Mgk.any_instance.should_receive(:flip).exactly(1)
718 Mgk.any_instance.should_receive(:flop).exactly(1)
722 Mgk.any_instance.should_receive(:to_blob).exactly(1)
727 @rp.vh(Mgk.new).should eq 'data'
\r