1 # -*- encoding: utf-8 -*-
\r
3 require 'spec_helper'
\r
5 describe ResourcePicture do
\r
7 FactoryGirl.create :admin
\r
8 @user = FactoryGirl.create( :user_yas)
\r
9 @author = @user.author
\r
10 @artist = FactoryGirl.create :artist_yas, :author_id => @author.id
\r
11 @other_user = FactoryGirl.create( :user_yas)
\r
12 @other_author = @other_user.author
\r
13 @other_artist = FactoryGirl.create :artist_yas, :author_id => @other_author.id
\r
14 @sp = FactoryGirl.create :system_picture
\r
15 @lg = FactoryGirl.create :license_group
\r
16 @license = FactoryGirl.create :license, :license_group_id => @lg.id, :system_picture_id => @sp.id
\r
17 @original_picture = FactoryGirl.create :original_picture, :artist_id => @artist.id
\r
19 class Mgk ; class Image ; end ; end
\r
20 Mgk::Image.stub(:from_blob).with(any_args).and_return([Mgk.new])
\r
21 Mgk.any_instance.stub(:format).with(any_args).and_return('png')
\r
22 Mgk.any_instance.stub(:format=).with(any_args)
\r
23 Mgk.any_instance.stub(:rows).with(any_args).and_return(200)
\r
24 Mgk.any_instance.stub(:columns).with(any_args).and_return(100)
\r
25 Mgk.any_instance.stub(:filesize).with(any_args).and_return(20000)
\r
26 Mgk.any_instance.stub(:to_blob).with(any_args).and_return('data')
\r
27 #原画ファイル削除だけは必ず成功するものとしておく
\r
28 PictureIO::LocalPicture.any_instance.stub(:delete).with(any_args).and_return(true)
\r
29 Picture.any_instance.stub(:subdirs).with(any_args).and_return(['', 'v', 'h', 'vh'])
\r
30 Picture.any_instance.stub(:h).with(any_args).and_return('data')
\r
31 Picture.any_instance.stub(:v).with(any_args).and_return('data')
\r
32 Picture.any_instance.stub(:vh).with(any_args).and_return('data')
\r
35 describe '検証に於いて' do
\r
37 @p = FactoryGirl.create :picture, :original_picture_id => @original_picture.id, :license_id => @license.id, :artist_id => @artist.id
\r
38 @rp = FactoryGirl.build :resource_picture, :artist_id => @artist.id, :original_picture_id => @original_picture.id, :license_id => @license.id, :picture_id => @p.id
\r
41 it 'オーソドックスなデータなら通る' do
\r
45 context 'extを検証するとき' do
\r
52 @rp.should_not be_valid
\r
56 @rp.should_not be_valid
\r
58 it 'png,gif,jpeg以外なら失敗する' do
\r
60 @rp.should_not be_valid
\r
63 context 'widthを検証するとき' do
\r
70 @rp.should_not be_valid
\r
74 @rp.should_not be_valid
\r
78 @rp.should_not be_valid
\r
82 @rp.should_not be_valid
\r
85 context 'heightを検証するとき' do
\r
92 @rp.should_not be_valid
\r
96 @rp.should_not be_valid
\r
100 @rp.should_not be_valid
\r
104 @rp.should_not be_valid
\r
107 context 'filesizeを検証するとき' do
\r
110 @rp.should be_valid
\r
114 @rp.should_not be_valid
\r
116 it '数値でなければ失敗する' do
\r
118 @rp.should_not be_valid
\r
121 @rp.filesize = '-1'
\r
122 @rp.should_not be_valid
\r
124 it '2MB以上なら失敗する' do
\r
125 @rp.filesize = 2000000+1
\r
126 @rp.should_not be_valid
\r
129 context 'artist_idを検証するとき' do
\r
131 @rp.artist_id = @artist.id
\r
132 @rp.should be_valid
\r
135 @rp.artist_id = nil
\r
136 @rp.should_not be_valid
\r
138 it '数値でなければ失敗する' do
\r
139 @rp.artist_id = 'a'
\r
140 @rp.should_not be_valid
\r
142 it '存在する絵師でなければ失敗する' do
\r
144 @rp.should_not be_valid
\r
147 context 'md5を検証するとき' do
\r
149 @rp = FactoryGirl.build :picture
\r
152 @rp.md5 = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'
\r
153 @rp.should be_valid
\r
157 @rp.should_not be_valid
\r
159 it '32文字以上なら失敗する' do
\r
161 @rp.should_not be_valid
\r
164 context 'license_idを検証するとき' do
\r
166 @rp.license_id = @license.id
\r
167 @rp.should be_valid
\r
170 @rp.license_id = nil
\r
171 @rp.should_not be_valid
\r
173 it '数値でなければ失敗する' do
\r
174 @rp.license_id = 'a'
\r
175 @rp.should_not be_valid
\r
177 it '存在する絵師でなければ失敗する' do
\r
179 @rp.should_not be_valid
\r
182 context 'original_picture_idを検証するとき' do
\r
184 @rp.original_picture_id = @original_picture.id
\r
185 @rp.should be_valid
\r
188 @rp.original_picture_id = nil
\r
189 @rp.should_not be_valid
\r
191 it '数値でなければ失敗する' do
\r
192 @rp.original_picture_id = 'a'
\r
193 @rp.should_not be_valid
\r
195 it '存在する原画でなければ失敗する' do
\r
196 @rp.original_picture_id = 0
\r
197 @rp.should_not be_valid
\r
200 context 'artist_nameを検証するとき' do
\r
202 @rp.artist_name = 'a'
\r
203 @rp.should be_valid
\r
206 @rp.artist_name = nil
\r
207 @rp.should_not be_valid
\r
210 context 'classnameを検証するとき' do
\r
212 @rp.classname = 'a'*50
\r
213 @rp.should be_valid
\r
217 @rp.should_not be_valid
\r
219 it '51文字以上なら失敗する' do
\r
220 @rp.classname = 'a'*51
\r
221 @rp.should_not be_valid
\r
224 context 'creditを検証するとき' do
\r
227 @rp.should be_valid
\r
230 context 'settingsを検証するとき' do
\r
233 @rp.should be_valid
\r
236 context 'picture_idを検証するとき' do
\r
238 @rp.picture_id = @p.id
\r
239 @rp.should be_valid
\r
242 @rp.picture_id = nil
\r
243 @rp.should_not be_valid
\r
245 it '数値でなければ失敗する' do
\r
246 @rp.picture_id = 'a'
\r
247 @rp.should_not be_valid
\r
249 it '存在する実素材でなければ失敗する' do
\r
251 @rp.should_not be_valid
\r
256 describe 'データ補充に於いて' do
\r
262 describe '単体取得に於いて' do
\r
264 @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
\r
265 @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id
\r
266 @rp = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :original_picture_id => @op.id, :license_id => @license.id, :picture_id => @p.id
\r
269 pic = ResourcePicture.show @rp.id
\r
272 context '関連テーブルオプションがないとき' do
\r
273 it 'ライセンスと絵師を含んでいる' do
\r
274 r = ResourcePicture.show_include_opt
\r
275 r.should eq [:license, :artist]
\r
278 context '関連テーブルオプションで原画を含ませたとき' do
\r
279 it 'ライセンスと絵師と原画データを含んでいる' do
\r
280 r = ResourcePicture.show_include_opt(:include => :original_picture)
\r
281 r.should eq [:license, :artist, :original_picture]
\r
284 context '存在しない素材を開こうとしたとき' do
\r
285 it '404RecordNotFound例外を返す' do
\r
287 pic = ResourcePicture.show 0
\r
288 }.should raise_error(ActiveRecord::RecordNotFound)
\r
292 describe 'json単体出力オプションに於いて' do
\r
293 it 'includeキーがライセンスと絵師を含んでいる' do
\r
294 r = ResourcePicture.show_json_include_opt
\r
295 r[:include].should eq [:license, :artist]
\r
298 describe '一覧取得に於いて' do
\r
300 @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
\r
301 @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id
\r
302 @rp = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :original_picture_id => @op.id, :license_id => @license.id, :picture_id => @p.id
\r
304 context 'page補正について' do
\r
305 it '文字列から数値に変換される' do
\r
306 ResourcePicture.page('8').should eq 8
\r
308 it 'nilの場合は1になる' do
\r
309 ResourcePicture.page().should eq 1
\r
311 it '0以下の場合は1になる' do
\r
312 ResourcePicture.page('0').should eq 1
\r
315 context 'page_size補正について' do
\r
316 it '文字列から数値に変換される' do
\r
317 ResourcePicture.page_size('7').should eq 7
\r
319 it 'nilの場合はResourcePicture.default_page_sizeになる' do
\r
320 ResourcePicture.page_size().should eq ResourcePicture.default_page_size
\r
322 it '0以下の場合はResourcePicture.default_page_sizeになる' do
\r
323 ResourcePicture.page_size('0').should eq ResourcePicture.default_page_size
\r
325 it 'ResourcePicture.max_page_sizeを超えた場合はResourcePicture.max_page_sizeになる' do
\r
326 ResourcePicture.page_size('1000').should eq ResourcePicture.max_page_size
\r
330 pic = ResourcePicture.list
\r
331 pic.should eq [@rp]
\r
334 nop = FactoryGirl.create :original_picture, :artist_id => @artist.id
\r
335 nrp = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => nop.id, :picture_id => @p.id, :updated_at => Time.now + 100
\r
336 pic = ResourcePicture.list
\r
337 pic.should eq [nrp, @rp]
\r
339 context 'DBに5件あって1ページの件数を2件に変えたとして' do
\r
341 nop2 = FactoryGirl.create :original_picture, :artist_id => @artist.id
\r
342 @nrp2 = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => nop2.id, :picture_id => @p.id, :updated_at => Time.now + 100
\r
343 nop3 = FactoryGirl.create :original_picture, :artist_id => @artist.id
\r
344 @nrp3 = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => nop3.id, :picture_id => @p.id, :updated_at => Time.now + 200
\r
345 nop4 = FactoryGirl.create :original_picture, :artist_id => @artist.id
\r
346 @nrp4 = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => nop4.id, :picture_id => @p.id, :updated_at => Time.now + 300
\r
347 nop5 = FactoryGirl.create :original_picture, :artist_id => @artist.id
\r
348 @nrp5 = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => nop5.id, :picture_id => @p.id, :updated_at => Time.now + 400
\r
349 ResourcePicture.stub(:default_page_size).and_return(2)
\r
352 pic = ResourcePicture.list
\r
353 pic.should have(2).items
\r
355 it 'page=1なら末尾2件を返す' do
\r
357 pic = ResourcePicture.list({}, 1)
\r
358 pic.should eq [@nrp5, @nrp4]
\r
360 it 'page=2なら中間2件を返す' do
\r
361 pic = ResourcePicture.list({}, 2)
\r
362 pic.should eq [@nrp3, @nrp2]
\r
364 it 'page=3なら先頭1件を返す' do
\r
365 pic = ResourcePicture.list({}, 3)
\r
366 pic.should eq [@rp]
\r
370 describe 'json一覧出力オプションに於いて' do
\r
371 it 'includeキーがライセンスと絵師を含んでいる' do
\r
372 r = ResourcePicture.list_json_opt
\r
373 r[:include].should eq [:license, :artist]
\r
377 describe 'サイズの調整に於いて' do
\r
381 it 'サイズに収まるときはそのまま使う' do
\r
382 ResourcePicture.fix_size_both(64, 64, 64, 64).should eq [64, 64]
\r
385 ResourcePicture.fix_size_both(64, 64, 32, 32).should eq [32, 32]
\r
387 it '縦長のときは縦に合わせて縮小' do
\r
388 ResourcePicture.fix_size_both(64, 64, 64, 128).should eq [32, 64]
\r
390 it '横長のときは横に合わせて縮小' do
\r
391 ResourcePicture.fix_size_both(64, 64, 128, 64).should eq [64, 32]
\r
395 describe 'フォーマット変換対象判定に於いて' do
\r
397 @rp = FactoryGirl.build :resource_picture,
\r
398 :artist_id => @artist.id, :license_id => @license.id
\r
400 context '変換するケース' do
\r
401 it '画像フォーマットがpngかつライセンスの変換禁止フラグが無効のときTrue' do
\r
402 ResourcePicture.any_instance.stub(:flag_gif_convert).with(any_args).and_return(0)
\r
404 @rp.to_gif?.should be_true
\r
407 context '変換しないケース' do
\r
408 it '画像フォーマットがでない' do
\r
409 ResourcePicture.any_instance.stub(:flag_gif_convert).with(any_args).and_return(0)
\r
411 @rp.to_gif?.should be_false
\r
414 ResourcePicture.any_instance.stub(:flag_gif_convert).with(any_args).and_return(-1)
\r
416 @rp.to_gif?.should be_false
\r
421 describe 'Gifフォーマット変換に於いて' do
\r
423 Magick::Image.stub(:from_blob).with(any_args).and_return([Mgk.new])
\r
425 context 'つつがなく終わるとき' do
\r
426 it 'Mgkオブジェクトが返る' do
\r
427 mgk = ResourcePicture.png_to_gif('mgkbin')
\r
428 mgk.is_a?(Mgk).should be_true
\r
430 it 'Mgkオブジェクトはgif変換されている' do
\r
431 #スタブばかりで変換できないので代入されているかでチェックする
\r
432 Mgk.any_instance.should_receive(:format=).with('gif').exactly(1)
\r
433 ResourcePicture.png_to_gif('mgkbin')
\r
436 context 'RMagick変換が失敗したとき' do
\r
438 Magick::Image.should_receive(:from_blob).with(any_args).and_raise('StandardError')
\r
441 res = ResourcePicture.png_to_gif('mgkbin')
\r
442 res.should be_false
\r
447 describe '原画からのコピーデータセットに於いて' do
\r
449 @op = FactoryGirl.create :original_picture, :ext => 'jpeg', :width => 264, :height => 265, :filesize => 266,
\r
450 :artist_id => @artist.id
\r
451 @rp = FactoryGirl.build :resource_picture,
\r
452 :artist_id => @artist.id
\r
454 it '原画オブジェクトから属性を取り出して対象素材にセットしている' do
\r
455 res = @rp.copy_data(@op)
\r
456 @rp.ext.should eq 'jpeg'
\r
457 @rp.width.should eq 264
\r
458 @rp.height.should eq 265
\r
459 @rp.filesize.should eq 266
\r
460 @rp.md5.should eq @op.md5
\r
461 @rp.artist_id.should eq @artist.id
\r
465 describe 'RMagick変換に於いて' do
\r
467 @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
\r
468 @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id
\r
469 attr = {:original_picture_id => @op.id, :license_id => @license.id, :artist_name => 'tester', :credit => '{}', :settings => {}.to_json.to_s}
\r
470 @rp = FactoryGirl.build :resource_picture, attr
\r
472 context 'つつがなく終わるとき' do
\r
473 it '画像データをオブジェクト化している' do
\r
474 Magick::Image.stub(:from_blob).and_return(['mgkobj'])
\r
475 @rp.data_to_mgk('mgkbin').should eq 'mgkobj'
\r
478 context '失敗するとき' do
\r
480 Magick::Image.should_receive(:from_blob).with(any_args).and_raise('StandardError')
\r
481 @rp.data_to_mgk('mgkbin').should be_false
\r
483 it '全体エラーがセットされている' do
\r
484 Magick::Image.should_receive(:from_blob).with(any_args).and_raise('StandardError')
\r
486 @rp.data_to_mgk('mgkbin')
\r
487 }.should change(@rp.errors[:base], :count)
\r
491 describe '画像オブジェクトの取得に於いて' do
\r
493 @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
\r
494 @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id
\r
495 attr = {:original_picture_id => @op.id, :license_id => @license.id, :artist_name => 'tester', :credit => '{}', :settings => {}.to_json.to_s}
\r
496 @rp = FactoryGirl.build :resource_picture, attr
\r
498 context '事前チェック' do
\r
500 OriginalPicture.any_instance.stub(:restore).with(any_args).and_return('data')
\r
501 ResourcePicture.any_instance.stub(:data_to_mgk).with(any_args).and_return(Mgk.new)
\r
503 it '原画から画像データを取得している' do
\r
504 OriginalPicture.any_instance.should_receive(:restore).with(any_args).exactly(1)
\r
507 it '素材に原画データのRMagick変換を依頼している' do
\r
508 ResourcePicture.any_instance.should_receive(:data_to_mgk).with(any_args).exactly(1)
\r
512 context 'つつがなく終わるとき' do
\r
514 #素材に原画データのRMagick変換を依頼し、画像オブジェクトを返す。
\r
517 OriginalPicture.any_instance.stub(:restore).with(any_args).and_return('data')
\r
518 ResourcePicture.any_instance.stub(:data_to_mgk).with(any_args).and_return(@mgk)
\r
520 it '画像オブジェクトを返す' do
\r
527 describe '新規実素材の取得に於いて' do
\r
529 @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
\r
530 @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id
\r
531 attr = {:original_picture_id => @op.id, :license_id => @license.id, :artist_name => 'tester',
\r
532 :credit => '{}', :settings => {:reverse => 1, :gif_convert => 1}.to_json.to_s}
\r
533 @rp = FactoryGirl.build :resource_picture, attr
\r
536 context '事前チェック' do
\r
538 Picture.any_instance.stub(:copy_data).with(any_args).and_return(true)
\r
539 Picture.any_instance.stub(:store).with(any_args).and_return(true)
\r
541 it '実素材に原画からのコピーデータを依頼している' do
\r
542 Picture.any_instance.should_receive(:copy_data).with(any_args).exactly(1)
\r
543 res = @rp.new_picture @mgk
\r
546 Picture.any_instance.should_receive(:store).with(any_args).exactly(1)
\r
547 res = @rp.new_picture @mgk
\r
550 context 'つつがなく終わるとき' do
\r
551 it '実素材に原画からのコピーデータをセットしている' do
\r
552 res = @rp.new_picture @mgk
\r
553 @rp.ext.should eq @op.ext
\r
554 @rp.width.should eq @op.width
\r
555 @rp.height.should eq @op.height
\r
556 @rp.filesize.should eq @op.filesize
\r
557 @rp.artist_id.should eq @op.artist_id
\r
558 @rp.original_picture_id.should eq @op.id
\r
561 res = @rp.new_picture @mgk
\r
562 res.is_a?(Picture).should be_true
\r
567 describe '作成・更新に於いて' do
\r
569 @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
\r
570 @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id
\r
571 attr = {:original_picture_id => @op.id, :license_id => @license.id, :artist_name => 'tester', :classname => 'StandardLicense', :credit => '{}', :settings => {}.to_json.to_s}
\r
572 @rp = FactoryGirl.build :resource_picture, attr
\r
574 context '事前チェック' do
\r
576 #すべての処理が正常パターンで通過すれば、一番深い分岐まで通る。
\r
577 #それで外部のメソッド呼び出しだけに着目してテストする。
\r
578 @mgk = Mgk.new #一回目の本画像保存は与えたオブジェクトを使って保存する。
\r
579 ResourcePicture.any_instance.stub(:op_mgk).with(any_args).and_return(@mgk)
\r
580 ResourcePicture.any_instance.stub(:new_picture).with(any_args).and_return(@p)
\r
581 ResourcePicture.any_instance.stub(:store_picture_with_gif).with(any_args).and_return(true)
\r
583 it '素材に原画からのコピーデータをセットしている' do
\r
584 ResourcePicture.any_instance.stub(:copy_data).with(any_args).and_return(true)
\r
585 ResourcePicture.any_instance.should_receive(:copy_data).with(any_args).exactly(1)
\r
588 it '画像オブジェクトの取得を依頼している' do
\r
589 ResourcePicture.any_instance.should_receive(:op_mgk).with(any_args).exactly(1)
\r
592 it '新規実素材の取得を依頼している' do
\r
593 ResourcePicture.any_instance.should_receive(:new_picture).with(any_args).exactly(1)
\r
597 ResourcePicture.any_instance.should_receive(:save).with(any_args).exactly(1)
\r
600 it '画像ファイルの作成・更新機能で画像を保存している' do
\r
601 ResourcePicture.any_instance.should_receive(:store_picture_with_gif).with(@mgk).exactly(1)
\r
605 context 'つつがなく終わるとき' do
\r
607 #すべての処理を正常パターンで通過させ、保存機能をチェックする。
\r
608 @mgk = Mgk.new #一回目の本画像保存は与えたオブジェクトを使って保存する。
\r
609 ResourcePicture.any_instance.stub(:op_mgk).with(any_args).and_return(@mgk)
\r
610 ResourcePicture.any_instance.stub(:new_picture).with(any_args).and_return(@p)
\r
611 ResourcePicture.any_instance.stub(:store_picture_with_gif).with(any_args).and_return(true)
\r
617 it '実素材idをセットしている' do
\r
619 @rp.picture_id.should_not be_nil
\r
621 it '実素材idから最新画像idを取得してセットしている' do
\r
623 @rp.picture_id.should eq @p.id
\r
628 }.should change ResourcePicture, :count
\r
631 #以下から例外ケース。処理先頭から失敗させていく
\r
632 context '画像オブジェクトの取得に失敗したとき' do
\r
634 ResourcePicture.any_instance.stub(:op_mgk).with(any_args).and_return(false)
\r
638 res.should be_false
\r
642 @rp.should be_a_new ResourcePicture
\r
645 context '新規実素材の取得に失敗したとき' do
\r
647 @mgk = Mgk.new #一回目の本画像保存は与えたオブジェクトを使って保存する。
\r
648 ResourcePicture.any_instance.stub(:op_mgk).with(any_args).and_return(@mgk)
\r
649 ResourcePicture.any_instance.stub(:new_picture).with(any_args).and_return(@p)
\r
650 Picture.any_instance.stub(:valid?).with(any_args).and_return(false)
\r
654 res.should be_false
\r
658 @rp.should be_a_new ResourcePicture
\r
661 context '自身の保存に失敗したとき' do
\r
663 @mgk = Mgk.new #一回目の本画像保存は与えたオブジェクトを使って保存する。
\r
664 ResourcePicture.any_instance.stub(:op_mgk).with(any_args).and_return(@mgk)
\r
665 ResourcePicture.any_instance.stub(:new_picture).with(any_args).and_return(@p)
\r
666 ResourcePicture.any_instance.stub(:save).with(any_args).and_return(false)
\r
670 res.should be_false
\r
674 @rp.should be_a_new ResourcePicture
\r
676 it '処理を中断してロールバックする' do
\r
679 }.should_not change Picture, :count
\r
684 describe '画像ファイルの作成・更新に於いて' do
\r
686 @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
\r
687 @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id
\r
688 attr = {:original_picture_id => @op.id, :license_id => @license.id, :artist_name => 'tester', :credit => '{}', :settings => {}.to_json.to_s}
\r
689 @rp = FactoryGirl.build :resource_picture, attr
\r
691 context '事前チェック' do
\r
693 #すべての処理が正常パターンで通過すれば、一番深い分岐まで通る。
\r
694 #それで外部のメソッド呼び出しだけに着目してテストする。
\r
695 @mgk = Mgk.new #一回目の本画像保存は与えたオブジェクトを使って保存する。
\r
696 ResourcePicture.any_instance.stub(:store_picture).with(any_args).and_return(true)
\r
697 ResourcePicture.any_instance.stub(:to_gif?).with(any_args).and_return(true)
\r
698 class GifMgk < Mgk ; end #store_pictureは二回呼び出される。区別をつけるために
\r
699 @gifmgk = GifMgk.new #パラメータを二種類用意する。
\r
700 ResourcePicture.stub(:png_to_gif).with(any_args).and_return(@gifmgk)
\r
702 it '画像ファイルの作成・更新機能で画像を保存している' do
\r
703 ResourcePicture.any_instance.stub(:filename).with(any_args).and_return('1.png')
\r
704 ResourcePicture.any_instance.should_receive(:store_picture).with(@mgk, '1.png').exactly(1)
\r
705 res = @rp.store_picture_with_gif @mgk
\r
707 it '自身にフォーマット変換対象かを問い合わせている' do
\r
708 ResourcePicture.any_instance.should_receive(:to_gif?).with(any_args).exactly(1)
\r
709 res = @rp.store_picture_with_gif @mgk
\r
711 it '自身にGifフォーマット変換を依頼している' do
\r
712 ResourcePicture.should_receive(:png_to_gif).with(any_args).exactly(1)
\r
713 res = @rp.store_picture_with_gif @mgk
\r
715 it '画像ファイルの作成・更新機能でgif画像を保存している' do
\r
716 #二回目の保存はgif変換の結果を渡す。
\r
717 ResourcePicture.any_instance.stub(:gifname).with(any_args).and_return('1.gif')
\r
718 ResourcePicture.any_instance.should_receive(:store_picture).with(@gifmgk, '1.gif').exactly(1)
\r
719 res = @rp.store_picture_with_gif @mgk
\r
722 context 'つつがなく終わるとき' do
\r
724 #すべての処理を正常パターンで通過させ、保存機能をチェックする。
\r
725 @mgk = Mgk.new #一回目の本画像保存は与えたオブジェクトを使って保存する。
\r
726 ResourcePicture.any_instance.stub(:store_picture).with(any_args).and_return(true)
\r
727 ResourcePicture.any_instance.stub(:to_gif?).with(any_args).and_return(true)
\r
728 class GifMgk < Mgk ; end #store_pictureは二回呼び出される。区別をつけるために
\r
729 @gifmgk = GifMgk.new #パラメータを二種類用意する。
\r
730 ResourcePicture.stub(:png_to_gif).with(any_args).and_return(@gifmgk)
\r
733 res = @rp.store_picture_with_gif @mgk
\r
737 context 'gif変換なしで、つつがなく終わるとき' do
\r
739 #すべての処理を正常パターンで通過させ、保存機能をチェックする。
\r
740 @mgk = Mgk.new #一回目の本画像保存は与えたオブジェクトを使って保存する。
\r
741 ResourcePicture.any_instance.stub(:store_picture).with(any_args).and_return(true)
\r
742 ResourcePicture.any_instance.stub(:to_gif?).with(any_args).and_return(false)
\r
745 res = @rp.store_picture_with_gif @mgk
\r
748 it 'gif保存は呼ばれていない' do
\r
749 ResourcePicture.any_instance.should_receive(:store_picture).with(any_args).exactly(1)
\r
750 res = @rp.store_picture_with_gif @mgk
\r
753 #以下から例外ケース。処理先頭から失敗させていく
\r
754 context '画像の保存に失敗したとき' do
\r
756 @mgk = Mgk.new #一回目の本画像保存は与えたオブジェクトを使って保存する。
\r
757 ResourcePicture.any_instance.stub(:store_picture).with(any_args).and_return(false)
\r
760 res = @rp.store_picture_with_gif @mgk
\r
761 res.should be_false
\r
763 it 'gif変換判定は呼ばれていない' do
\r
764 ResourcePicture.any_instance.should_not_receive(:to_gif?).with(any_args)
\r
765 res = @rp.store_picture_with_gif @mgk
\r
768 context 'gif変換に失敗したとき' do
\r
770 @mgk = Mgk.new #一回目の本画像保存は与えたオブジェクトを使って保存する。
\r
771 ResourcePicture.any_instance.stub(:store_picture).with(any_args).and_return(true)
\r
772 ResourcePicture.any_instance.stub(:to_gif?).with(any_args).and_return(true)
\r
773 ResourcePicture.stub(:png_to_gif).with(any_args).and_return(false)
\r
776 res = @rp.store_picture_with_gif @mgk
\r
777 res.should be_false
\r
779 it 'gif画像の保存は呼ばれていない' do
\r
780 #本画像の保存があるので、一度は呼ばれる
\r
781 ResourcePicture.any_instance.should_receive(:store_picture).with(any_args).exactly(1)
\r
782 res = @rp.store_picture_with_gif @mgk
\r
785 context 'gif画像の保存に失敗したとき' do
\r
787 ResourcePicture.any_instance.stub(:filename).with(any_args).and_return('1.png')
\r
788 ResourcePicture.any_instance.stub(:gifname).with(any_args).and_return('1.gif')
\r
790 ResourcePicture.any_instance.stub(:store_picture).with(@mgk, '1.png').and_return(true)
\r
791 ResourcePicture.any_instance.stub(:to_gif?).with(any_args).and_return(true)
\r
792 class GifMgk < Mgk ; end #store_pictureは二回呼び出される。区別をつけるために
\r
793 @gifmgk = GifMgk.new #パラメータを二種類用意する。
\r
794 ResourcePicture.stub(:png_to_gif).with(any_args).and_return(@gifmgk)
\r
795 ResourcePicture.any_instance.stub(:store_picture).with(@gifmgk, '1.gif').and_return(false)
\r
798 res = @rp.store_picture_with_gif @mgk
\r
799 res.should be_false
\r
804 describe '画像ファイルの作成・更新に於いて' do
\r
805 #PictureIo経由で画像を保存するための機能。ファイル名に自身のidを使うので事前に自身の保存が必要。
\r
807 @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
\r
808 @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id
\r
809 attr = {:original_picture_id => @op.id, :license_id => @license.id, :artist_name => 'tester', :credit => '{}', :settings => {}.to_json.to_s}
\r
810 @rp = FactoryGirl.build :resource_picture, attr
\r
813 context '事前チェック' do
\r
815 #すべての処理を正常パターンで通過させ、保存機能をチェックする。
\r
816 PictureIO.resource_picture_io.stub(:put).with(any_args).and_return(true)
\r
817 ResourcePicture.any_instance.stub(:flag_thumbnail).with(any_args).and_return(1)
\r
818 ResourcePicture.any_instance.stub(:thumbnail).with(any_args).and_return('tmbdata')
\r
820 it 'サムネイル化が1回呼ばれる' do
\r
821 ResourcePicture.any_instance.should_receive(:thumbnail).with(any_args).exactly(1)
\r
822 res = @rp.store_picture(Mgk.new, '1.gif')
\r
824 it '画像ファイルの保存が2回呼ばれる' do
\r
825 PictureIO.resource_picture_io.should_receive(:put).with(any_args).exactly(2)
\r
826 res = @rp.store_picture(Mgk.new, '1.gif')
\r
828 it '画像ファイルのベースへのサムネイル保存が1回呼ばれる' do
\r
829 PictureIO.resource_picture_io.should_receive(:put).with('tmbdata', '1.gif').exactly(1)
\r
830 res = @rp.store_picture(Mgk.new, '1.gif')
\r
832 it '画像ファイルのfullへの保存が1回呼ばれる' do
\r
833 PictureIO.resource_picture_io.should_receive(:put).with('data', '1.gif', 'full').exactly(1)
\r
834 res = @rp.store_picture(Mgk.new, '1.gif')
\r
837 context 'つつがなく終わるとき' do
\r
839 #すべての処理を正常パターンで通過させ、保存機能をチェックする。
\r
840 PictureIO.resource_picture_io.stub(:put).with(any_args).and_return(true)
\r
841 ResourcePicture.any_instance.stub(:flag_thumbnail).with(any_args).and_return(1)
\r
842 ResourcePicture.any_instance.stub(:thumbnail).with(any_args).and_return('tmbdata')
\r
845 res = @rp.store_picture(Mgk.new, '1.gif')
\r
851 ResourcePicture.any_instance.stub(:flag_thumbnail).with(any_args).and_return(1)
\r
852 ResourcePicture.any_instance.stub(:thumbnail).with(any_args).and_return('tmbdata')
\r
853 PictureIO.resource_picture_io.stub(:put).with(any_args).and_return(false)
\r
855 it '画像の保存に失敗したならFalseを返す' do
\r
856 res = @rp.store_picture(Mgk.new, '1.gif')
\r
857 res.should be_false
\r
863 describe 'サムネイル変換に於いて' do
\r
864 #サムネイル化した画像データを返すが、スタブをおくので、リサイズと画像データ化を試みるかをチェックする
\r
866 ResourcePicture.stub(:resize).with(any_args).and_return(Mgk.new)
\r
867 @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
\r
868 @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id
\r
869 attr = {:original_picture_id => @op.id, :license_id => @license.id, :artist_name => 'tester', :credit => '{}', :settings => {}.to_json.to_s}
\r
870 @rp = FactoryGirl.build :resource_picture, attr
\r
872 it 'サムネイルサイズに縮小した画像データを返す' do
\r
873 ResourcePicture.should_receive(:resize).with(any_args).exactly(1)
\r
874 @rp.thumbnail(Mgk.new)
\r
878 @rp.thumbnail(Mgk.new).should eq 'data'
\r
882 describe 'クレジットデータに於いて' do
\r
884 @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
\r
885 @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id
\r
886 attr = {:original_picture_id => @op.id, :license_id => @license.id, :artist_name => 'tester', :credit => '{"system_picture_id": 2}', :settings => {}.to_json.to_s}
\r
887 @rp = FactoryGirl.build :resource_picture, attr
\r
889 it 'system_picture_idが入っている' do
\r
890 res = @rp.credit_data
\r
891 res["system_picture_id"].should eq 2
\r