X-Git-Url: http://git.osdn.net/view?a=blobdiff_plain;f=spec%2Fmodels%2Fresource_picture_spec.rb;h=cccf4c47bc4d47061602ed6d70ae6489601bc5c1;hb=2bcff4ea2e955de6c21e19239313f00ac323f410;hp=28a3430cfe990d6b1c56caace4f1d7b59cb1b10e;hpb=efd817c2dbd6c7b98352306dccdb5f272ca5c67c;p=pettanr%2Fpettanr.git diff --git a/spec/models/resource_picture_spec.rb b/spec/models/resource_picture_spec.rb index 28a3430c..cccf4c47 100644 --- a/spec/models/resource_picture_spec.rb +++ b/spec/models/resource_picture_spec.rb @@ -1,732 +1,1357 @@ # -*- encoding: utf-8 -*- -require 'spec_helper' - -describe ResourcePicture do - before do - Factory :admin - @user = Factory( :user_yas) - @author = @user.author - @artist = Factory :artist_yas, :author_id => @author.id - @other_user = Factory( :user_yas) - @other_author = @other_user.author - @other_artist = Factory :artist_yas, :author_id => @other_author.id - @sp = Factory :system_picture - @lg = Factory :license_group - @license = Factory :license, :license_group_id => @lg.id, :system_picture_id => @sp.id - @original_picture = Factory :original_picture, :artist_id => @artist.id, :license_id => @license.id - - class Mgk ; class Image ; end ; end - Mgk::Image.stub(:from_blob).with(any_args).and_return([Mgk.new]) - Mgk.any_instance.stub(:format).with(any_args).and_return('png') - Mgk.any_instance.stub(:format=).with(any_args) - Mgk.any_instance.stub(:rows).with(any_args).and_return(200) - Mgk.any_instance.stub(:columns).with(any_args).and_return(100) - Mgk.any_instance.stub(:filesize).with(any_args).and_return(20000) - Mgk.any_instance.stub(:to_blob).with(any_args).and_return('data') - #原画ファイル削除だけは必ず成功するものとしておく - PictureIO::LocalPicture.any_instance.stub(:delete).with(any_args).and_return(true) +#素材 +require 'spec_helper' + +describe ResourcePicture do + before do + @admin = FactoryGirl.create :admin + @user = FactoryGirl.create( :user_yas) + @author = FactoryGirl.create :author, :user_id => @user.id + @artist = FactoryGirl.create :artist_yas, :author_id => @author.id + @other_user = FactoryGirl.create( :user_yas) + @other_author = FactoryGirl.create :author, :user_id => @other_user.id + @other_artist = FactoryGirl.create :artist_yas, :author_id => @other_author.id + @sp = FactoryGirl.create :system_picture + @lg = FactoryGirl.create :license_group + @license = FactoryGirl.create :license, :license_group_id => @lg.id, :system_picture_id => @sp.id + @original_picture = FactoryGirl.create :original_picture, :artist_id => @artist.id + + #原画ファイル削除だけは必ず成功するものとしておく + PictureIO::LocalPicture.any_instance.stub(:delete).with(any_args).and_return(true) + Picture.any_instance.stub(:subdirs).with(any_args).and_return(['', 'v', 'h', 'vh']) + Picture.any_instance.stub(:h).with(any_args).and_return('data') + Picture.any_instance.stub(:v).with(any_args).and_return('data') + Picture.any_instance.stub(:vh).with(any_args).and_return('data') + end + + describe '検証に於いて' do + before do + @p = FactoryGirl.create :picture, :original_picture_id => @original_picture.id, :license_id => @license.id, :artist_id => @artist.id + @rp = FactoryGirl.build :resource_picture, :artist_id => @artist.id, :original_picture_id => @original_picture.id, :license_id => @license.id, :picture_id => @p.id + end + + context 'オーソドックスなデータのとき' do + it '下限データが通る' do + @rp.ext = 'png' #リストにない拡張子は通らないし + @rp.width = 1 + @rp.height = 1 + @rp.filesize = 1 + @rp.md5 = 'a'*32 + @rp.classname = 'a'*1 + @rp.should be_valid + end + it '上限データが通る' do + @rp.ext = 'jpeg' + @rp.width = 99999 + @rp.height = 99999 + @rp.filesize = 2000000 + @rp.md5 = 'a'*32 + @rp.classname = 'a'*50 + @rp.should be_valid + end + end + + context 'extを検証するとき' do + it 'nullなら失敗する' do + @rp.ext = '' + @rp.should_not be_valid + end + it '5文字以上なら失敗する' do + @rp.ext = 'a'*5 + @rp.should_not be_valid + end + it 'png,gif,jpeg以外なら失敗する' do + @rp.ext = 'bmp' + @rp.should_not be_valid + end + end + context 'widthを検証するとき' do + it 'nullなら失敗する' do + @rp.width = nil + @rp.should_not be_valid + end + it '数値でなければ失敗する' do + @rp.width = 'a' + @rp.should_not be_valid + end + it '0なら失敗する' do + @rp.width = '0' + @rp.should_not be_valid + end + it '負でも失敗する' do + @rp.width = -1 + @rp.should_not be_valid + end + end + context 'heightを検証するとき' do + it 'nullなら失敗する' do + @rp.height = nil + @rp.should_not be_valid + end + it '数値でなければ失敗する' do + @rp.height = 'a' + @rp.should_not be_valid + end + it '0なら失敗する' do + @rp.height = '0' + @rp.should_not be_valid + end + it '負でも失敗する' do + @rp.height = -1 + @rp.should_not be_valid + end + end + context 'filesizeを検証するとき' do + it 'nullなら失敗する' do + @rp.filesize = nil + @rp.should_not be_valid + end + it '数値でなければ失敗する' do + @rp.filesize = 'a' + @rp.should_not be_valid + end + it '負なら失敗する' do + @rp.filesize = '-1' + @rp.should_not be_valid + end + it '2MB以上なら失敗する' do + @rp.filesize = 2000000+1 + @rp.should_not be_valid + end + end + context 'artist_idを検証するとき' do + it 'nullなら失敗する' do + @rp.artist_id = nil + @rp.should_not be_valid + end + it '数値でなければ失敗する' do + @rp.artist_id = 'a' + @rp.should_not be_valid + end + it '存在する絵師でなければ失敗する' do + @rp.artist_id = 0 + @rp.should_not be_valid + end + end + context 'md5を検証するとき' do + it 'nullなら失敗する' do + @rp.md5 = '' + @rp.should_not be_valid + end + it '31文字なら失敗する' do + @rp.md5 = 'a'*31 + @rp.should_not be_valid + end + it '32文字以上なら失敗する' do + @rp.md5 = 'a'*33 + @rp.should_not be_valid + end + end + context 'license_idを検証するとき' do + it 'nullなら失敗する' do + @rp.license_id = nil + @rp.should_not be_valid + end + it '数値でなければ失敗する' do + @rp.license_id = 'a' + @rp.should_not be_valid + end + it '存在する絵師でなければ失敗する' do + @rp.license_id = 0 + @rp.should_not be_valid + end + end + context 'original_picture_idを検証するとき' do + it 'nullなら失敗する' do + @rp.original_picture_id = nil + @rp.should_not be_valid + end + it '数値でなければ失敗する' do + @rp.original_picture_id = 'a' + @rp.should_not be_valid + end + it '存在する原画でなければ失敗する' do + @rp.original_picture_id = 0 + @rp.should_not be_valid + end + end + context 'artist_nameを検証するとき' do + it 'nullなら失敗する' do + @rp.artist_name = nil + @rp.should_not be_valid + end + end + context 'classnameを検証するとき' do + it 'nullなら失敗する' do + @rp.classname = '' + @rp.should_not be_valid + end + it '51文字以上なら失敗する' do + @rp.classname = 'a'*51 + @rp.should_not be_valid + end + end + context 'creditを検証するとき' do + end + context 'settingsを検証するとき' do + end + context 'picture_idを検証するとき' do + it 'nullなら失敗する' do + @rp.picture_id = nil + @rp.should_not be_valid + end + it '数値でなければ失敗する' do + @rp.picture_id = 'a' + @rp.should_not be_valid + end + it '存在する実素材でなければ失敗する' do + @rp.picture_id = 0 + @rp.should_not be_valid + end + end + end + + describe '文字コード検証に於いて' do + before do + @p = FactoryGirl.create :picture, :original_picture_id => @original_picture.id, :license_id => @license.id, :artist_id => @artist.id + @rp = FactoryGirl.build :resource_picture, :artist_id => @artist.id, :original_picture_id => @original_picture.id, :license_id => @license.id, :picture_id => @p.id + end + + context 'artist_nameを検証するとき' do + it 'Shift JISなら失敗する' do + @rp.artist_name = "\x83G\x83r\x83]\x83D" + lambda{ + @rp.valid_encode + }.should raise_error(Pettanr::BadRequest) + end + end + + context 'classnameを検証するとき' do + it 'Shift JISなら失敗する' do + @rp.classname = "\x83G\x83r\x83]\x83D" + lambda{ + @rp.valid_encode + }.should raise_error(Pettanr::BadRequest) + end + end + + context 'creditを検証するとき' do + it 'Shift JISなら失敗する' do + @rp.credit = "\x83G\x83r\x83]\x83D" + lambda{ + @rp.valid_encode + }.should raise_error(Pettanr::BadRequest) + end + end + + context 'settingsを検証するとき' do + it 'Shift JISなら失敗する' do + @rp.settings = "\x83G\x83r\x83]\x83D" + lambda{ + @rp.valid_encode + }.should raise_error(Pettanr::BadRequest) + end + end + + end + + describe 'デフォルト値補充に於いて' do + it 'defined' do + @p = FactoryGirl.create :picture, :original_picture_id => @original_picture.id, :license_id => @license.id, :artist_id => @artist.id + @rp = FactoryGirl.build :resource_picture, :artist_id => @artist.id, :original_picture_id => @original_picture.id, :license_id => @license.id, :picture_id => @p.id + @rp.supply_default + end + end + + describe '上書き補充に於いて' do + before do + @original_picture.attributes = {:ext => 'gif', :width => 267, :height => 268, :filesize => 269, + :artist_id => @artist.id } + @rp = FactoryGirl.build :resource_picture, :original_picture_id => @original_picture.id + end + it 'width, height, ext, filesize, md5, original_picture_id, artist_idが設定されている' do + @rp.overwrite @original_picture + @rp.width.should eq 267 + @rp.height.should eq 268 + @rp.ext.should eq 'gif' + @rp.filesize.should eq 269 + @rp.md5.should eq @rp.md5 + @rp.original_picture_id.should eq @original_picture.id + @rp.artist_id.should eq @artist.id + end + end + + describe '所持判定に於いて' do + before do + @p = FactoryGirl.create :picture, :original_picture_id => @original_picture.id, :license_id => @license.id, :artist_id => @artist.id + @rp = FactoryGirl.build :resource_picture, :artist_id => @artist.id, :original_picture_id => @original_picture.id, :license_id => @license.id, :picture_id => @p.id + end + context '事前チェックする' do + it '自身にロールリストからの絵師取得を依頼している' do + ResourcePicture.should_receive(:get_artist_from_roles).with(any_args).exactly(1) + r = @rp.own?([@author]) + end + end + context 'ロール内絵師が取得できるとき' do + before do + end + it 'ロール内絵師のidが自身の絵師idと一致するなら許可する' do + ResourcePicture.stub(:get_artist_from_roles).with(any_args).and_return(@artist) + r = @rp.own?([@author]) + r.should be_true + end + it 'ロール内絵師のidが自身の絵師idと一致しないならno' do + ResourcePicture.stub(:get_artist_from_roles).with(any_args).and_return(@other_artist) + @rp.own?(@other_artist).should be_false + end + end + context 'ロール内絵師が取得できないとき' do + before do + ResourcePicture.stub(:get_artist_from_roles).with(any_args).and_return(nil) + end + it 'Falseを返す' do + r = @rp.own?([@author]) + r.should be_false + end + end + end + + describe '閲覧許可に於いて' do + before do + @p = FactoryGirl.create :picture, :original_picture_id => @original_picture.id, :license_id => @license.id, :artist_id => @artist.id + @rp = FactoryGirl.build :resource_picture, :artist_id => @artist.id, :original_picture_id => @original_picture.id, :license_id => @license.id, :picture_id => @p.id + end + context 'オープンモードのとき' do + before do + MagicNumber['run_mode'] = 0 + end + it '自身にゲスト用ロールチェックを問い合わせしている' do + ResourcePicture.any_instance.stub(:guest_role_check).and_return(true) + ResourcePicture.any_instance.should_receive(:guest_role_check).with(any_args).exactly(1) + r = @rp.visible?([@author]) + end + it 'ゲスト用ロールチェックが失敗したとき、falseを返す' do + ResourcePicture.any_instance.stub(:guest_role_check).and_return(false) + r = @rp.visible?([@author]) + r.should be_false + end + end + context 'クローズドモードのとき' do + before do + MagicNumber['run_mode'] = 1 + end + it '自身に素材読者用ロールチェックを問い合わせしている' do + ResourcePicture.any_instance.stub(:resource_reader_role_check).and_return(true) + ResourcePicture.any_instance.should_receive(:resource_reader_role_check).with(any_args).exactly(1) + r = @rp.visible?([@author]) + end + it '素材読者用ロールチェックが失敗したとき、falseを返す' do + ResourcePicture.any_instance.stub(:resource_reader_role_check).and_return(false) + r = @rp.visible?([@author]) + r.should be_false + end + end + context 'つつがなく終わるとき' do + before do + MagicNumber['run_mode'] = 1 + ResourcePicture.any_instance.stub(:resource_reader_role_check).and_return(true) + end + it '許可する' do + r = @rp.visible?([@author]) + r.should be_true + end + end + end + + describe 'ファイル名に於いて' do + before do + @p = FactoryGirl.create :picture, :original_picture_id => @original_picture.id, :license_id => @license.id, :artist_id => @artist.id + @rp = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :original_picture_id => @original_picture.id, :license_id => @license.id, :picture_id => @p.id + end + it 'id+拡張子のフォーマットで返す' do + r = @rp.filename + r.should eq "#{@rp.id}.png" + end + end + + describe 'MimeTypeに於いて' do + before do + @p = FactoryGirl.create :picture, :original_picture_id => @original_picture.id, :license_id => @license.id, :artist_id => @artist.id + @rp = FactoryGirl.build :resource_picture, :artist_id => @artist.id, :original_picture_id => @original_picture.id, :license_id => @license.id, :picture_id => @p.id + end + it 'image/拡張子のフォーマットで返す' do + r = @rp.mime_type + r.should eq "image/png" + end + end + + describe 'ファイルのurlに於いて' do + before do + @p = FactoryGirl.create :picture, :original_picture_id => @original_picture.id, :license_id => @license.id, :artist_id => @artist.id + @rp = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :original_picture_id => @original_picture.id, :license_id => @license.id, :picture_id => @p.id + ResourcePicture.any_instance.stub(:filename).and_return('3.gif') + end + it 'ファイル名取得を依頼している' do + ResourcePicture.any_instance.should_receive(:filename).exactly(1) + @rp.url + end + it '/resource_pictures/3.gifのフォーマットで返す' do + r = @rp.url + r.should eq "/resource_pictures/3.gif" + end + end + + describe '一覧取得に於いて' do + before do + @op = FactoryGirl.create :original_picture, :artist_id => @artist.id + @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id + @rp = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :original_picture_id => @op.id, :license_id => @license.id, :picture_id => @p.id + end + context 'page補正について' do + it '文字列から数値に変換される' do + ResourcePicture.page('8').should eq 8 + end + it 'nilの場合は1になる' do + ResourcePicture.page().should eq 1 + end + it '0以下の場合は1になる' do + ResourcePicture.page('0').should eq 1 + end + end + context 'page_size補正について' do + it '文字列から数値に変換される' do + ResourcePicture.page_size('7').should eq 7 + end + it 'nilの場合はResourcePicture.default_page_sizeになる' do + ResourcePicture.page_size().should eq ResourcePicture.default_page_size + end + it '0以下の場合はResourcePicture.default_page_sizeになる' do + ResourcePicture.page_size('0').should eq ResourcePicture.default_page_size + end + it 'ResourcePicture.max_page_sizeを超えた場合はResourcePicture.max_page_sizeになる' do + ResourcePicture.page_size('1000').should eq ResourcePicture.max_page_size + end + end + it 'リストを返す' do + r = ResourcePicture.list + r.should eq [@rp] + end + it '時系列で並んでいる' do + nop = FactoryGirl.create :original_picture, :artist_id => @artist.id + 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 = ResourcePicture.list + r.should eq [nrp, @rp] + end + context 'DBに5件あって1ページの件数を2件に変えたとして' do + before do + nop2 = FactoryGirl.create :original_picture, :artist_id => @artist.id + @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 + nop3 = FactoryGirl.create :original_picture, :artist_id => @artist.id + @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 + nop4 = FactoryGirl.create :original_picture, :artist_id => @artist.id + @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 + nop5 = FactoryGirl.create :original_picture, :artist_id => @artist.id + @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 + ResourcePicture.stub(:default_page_size).and_return(2) + end + it '通常は2件を返す' do + r = ResourcePicture.list + r.should have(2).items + end + it 'page=1なら末尾2件を返す' do + #時系列で並んでいる + r = ResourcePicture.list(1) + r.should eq [@nrp5, @nrp4] + end + it 'page=2なら中間2件を返す' do + r = ResourcePicture.list(2) + r.should eq [@nrp3, @nrp2] + end + it 'page=3なら先頭1件を返す' do + r = ResourcePicture.list(3) + r.should eq [@rp] + end + end end - - describe '検証に於いて' do + + describe '一覧ページ制御に於いて' do before do - end - - it 'オーソドックスなデータなら通る' do - @rp = Factory.build :resource_picture, :artist_id => @artist.id, :original_picture_id => @original_picture.id, :license_id => @license.id - @rp.should be_valid - end - - context 'extを検証するとき' do - before do - @rp = Factory.build :resource_picture, - :artist_id => @artist.id, :original_picture_id => @original_picture.id, :license_id => @license.id - end - it 'テストデータの確認' do - @rp.ext = 'jpeg' - @rp.should be_valid - end - it 'nullなら失敗する' do - @rp.ext = '' - @rp.should_not be_valid - end - it '5文字以上なら失敗する' do - @rp.ext = 'a'*5 - @rp.should_not be_valid - end - it 'png,gif,jpeg以外なら失敗する' do - @rp.ext = 'bmp' - @rp.should_not be_valid - end - end - context 'widthを検証するとき' do - before do - @rp = Factory.build :resource_picture, - :artist_id => @artist.id, :original_picture_id => @original_picture.id, :license_id => @license.id - end - it 'テストデータの確認' do - @rp.width = 1 - @rp.should be_valid - end - it 'nullなら失敗する' do - @rp.width = nil - @rp.should_not be_valid - end - it '数値でなければ失敗する' do - @rp.width = 'a' - @rp.should_not be_valid - end - it '0なら失敗する' do - @rp.width = '0' - @rp.should_not be_valid - end - it '負でも失敗する' do - @rp.width = -1 - @rp.should_not be_valid - end - end - context 'heightを検証するとき' do - before do - @rp = Factory.build :resource_picture, - :artist_id => @artist.id, :original_picture_id => @original_picture.id, :license_id => @license.id - end - it 'テストデータの確認' do - @rp.height = '1' - @rp.should be_valid - end - it 'nullなら失敗する' do - @rp.height = nil - @rp.should_not be_valid - end - it '数値でなければ失敗する' do - @rp.height = 'a' - @rp.should_not be_valid - end - it '0なら失敗する' do - @rp.height = '0' - @rp.should_not be_valid - end - it '負でも失敗する' do - @rp.height = -1 - @rp.should_not be_valid - end - end - context 'filesizeを検証するとき' do - before do - @rp = Factory.build :resource_picture, - :artist_id => @artist.id, :original_picture_id => @original_picture.id, :license_id => @license.id - end - it 'テストデータの確認' do - @rp.filesize = '1' - @rp.should be_valid - end - it 'nullなら失敗する' do - @rp.filesize = nil - @rp.should_not be_valid - end - it '数値でなければ失敗する' do - @rp.filesize = 'a' - @rp.should_not be_valid - end - it '負なら失敗する' do - @rp.filesize = '-1' - @rp.should_not be_valid - end - it '2MB以上なら失敗する' do - @rp.filesize = 2000000+1 - @rp.should_not be_valid - end - end - context 'artist_idを検証するとき' do - before do - @rp = Factory.build :resource_picture, - :original_picture_id => @original_picture.id, :license_id => @license.id - end - it 'テストデータの確認' do - @rp.artist_id = @artist.id - @rp.should be_valid - end - it 'nullなら失敗する' do - @rp.artist_id = nil - @rp.should_not be_valid - end - it '数値でなければ失敗する' do - @rp.artist_id = 'a' - @rp.should_not be_valid - end - it '存在する絵師でなければ失敗する' do - @rp.artist_id = 0 - @rp.should_not be_valid - end - end - context 'license_idを検証するとき' do - before do - @rp = Factory.build :resource_picture, - :artist_id => @artist.id, :original_picture_id => @original_picture.id - end - it 'テストデータの確認' do - @rp.license_id = @license.id - @rp.should be_valid - end - it 'nullなら失敗する' do - @rp.license_id = nil - @rp.should_not be_valid - end - it '数値でなければ失敗する' do - @rp.license_id = 'a' - @rp.should_not be_valid - end - it '存在する絵師でなければ失敗する' do - @rp.license_id = 0 - @rp.should_not be_valid - end - end - context 'original_picture_idを検証するとき' do - before do - @rp = Factory.build :resource_picture, - :artist_id => @artist.id, :license_id => @license.id - end - it 'テストデータの確認' do - @rp.original_picture_id = @original_picture.id - @rp.should be_valid - end - it 'nullなら失敗する' do - @rp.original_picture_id = nil - @rp.should_not be_valid - end - it '数値でなければ失敗する' do - @rp.original_picture_id = 'a' - @rp.should_not be_valid - end - it '存在する原画でなければ失敗する' do - @rp.original_picture_id = 0 - @rp.should_not be_valid - end - end - end - - describe 'データ補充に於いて' do - before do - end - - end + ResourcePicture.stub(:count).with(any_args).and_return(100) + end + it 'ページ制御を返す' do + r = ResourcePicture.list_paginate + r.is_a?(Kaminari::PaginatableArray).should be_true + end + it 'ページ件数10のとき、3ページ目のオフセットは20から始まる' do + r = ResourcePicture.list_paginate 3, 10 + r.limit_value.should eq 10 + r.offset_value.should eq 20 + end + end + + describe '自分の素材一覧取得に於いて' do + before do + @op = FactoryGirl.create :original_picture, :artist_id => @artist.id + @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id + @rp = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => @op.id, :picture_id => @p.id + end + context 'つつがなく終わるとき' do + it '一覧取得オプションを利用している' do + ResourcePicture.stub(:list_opt).with(any_args).and_return({}) + ResourcePicture.should_receive(:list_opt).with(any_args).exactly(1) + r = ResourcePicture.mylist @artist + end + end + it 'リストを返す' do + c = ResourcePicture.mylist @artist + c.should eq [@rp] + end + it '時系列で並んでいる' do + nop = FactoryGirl.create :original_picture, :artist_id => @artist.id + 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 + cl = ResourcePicture.mylist @artist + cl.should eq [nrp, @rp] + end + it '他人の素材は含まない' do + nop = FactoryGirl.create :original_picture, :artist_id => @other_artist.id + nrp = FactoryGirl.create :resource_picture, :artist_id => @other_artist.id, :license_id => @license.id, :original_picture_id => nop.id, :picture_id => @p.id, :updated_at => Time.now + 100 + cl = ResourcePicture.mylist @artist + cl.should eq [@rp] + end + context 'DBに5件あって1ページの件数を2件に変えたとして' do + before do + @op2 = FactoryGirl.create :original_picture, :artist_id => @artist.id + @rp2 = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => @op2.id, :picture_id => @p.id, :updated_at => Time.now + 100 + @op3 = FactoryGirl.create :original_picture, :artist_id => @artist.id + @rp3 = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => @op3.id, :picture_id => @p.id, :updated_at => Time.now + 200 + @op4 = FactoryGirl.create :original_picture, :artist_id => @artist.id + @rp4 = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => @op4.id, :picture_id => @p.id, :updated_at => Time.now + 300 + @op5 = FactoryGirl.create :original_picture, :artist_id => @artist.id + @rp5 = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => @op5.id, :picture_id => @p.id, :updated_at => Time.now + 400 + end + it '通常は2件を返す' do + c = ResourcePicture.mylist @artist, 1, 2 + c.should have(2).items + end + it 'page=1なら末尾2件を返す' do + #時系列で並んでいる + c = ResourcePicture.mylist(@artist, 1, 2) + c.should eq [@rp5, @rp4] + end + it 'page=2なら中間2件を返す' do + c = ResourcePicture.mylist(@artist, 2, 2) + c.should eq [@rp3, @rp2] + end + it 'page=3なら先頭1件を返す' do + c = ResourcePicture.mylist(@artist, 3, 2) + c.should eq [@rp] + end + end + end + + describe '自分の素材一覧ページ制御に於いて' do + before do + ResourcePicture.stub(:count).with(any_args).and_return(100) + end + it 'ページ制御を返す' do + r = ResourcePicture.mylist_paginate @artist + r.is_a?(Kaminari::PaginatableArray).should be_true + end + it '自分の素材一覧の取得条件を利用している' do + ResourcePicture.stub(:mylist_where).with(any_args).and_return('') + ResourcePicture.should_receive(:mylist_where).with(any_args).exactly(1) + r = ResourcePicture.mylist_paginate @artist + end + it 'ページ件数10のとき、3ページ目のオフセットは20から始まる' do + r = ResourcePicture.mylist_paginate @artist, 3, 10 + r.limit_value.should eq 10 + r.offset_value.should eq 20 + end + end + + describe '他作家の素材一覧取得に於いて' do + before do + @op = FactoryGirl.create :original_picture, :artist_id => @artist.id + @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id + @rp = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => @op.id, :picture_id => @p.id + @other_op = FactoryGirl.create :original_picture, :artist_id => @other_artist.id + @other_p = FactoryGirl.create :picture, :original_picture_id => @other_op.id, :license_id => @license.id, :artist_id => @other_artist.id + @other_rp = FactoryGirl.create :resource_picture, :artist_id => @other_artist.id, :license_id => @license.id, :original_picture_id => @other_op.id, :picture_id => @other_p.id + end + context 'つつがなく終わるとき' do + it '一覧取得オプションを利用している' do + ResourcePicture.stub(:list_opt).with(any_args).and_return({}) + ResourcePicture.should_receive(:list_opt).with(any_args).exactly(1) + r = ResourcePicture.himlist @other_artist + end + end + it '指定した作家のリストを返す' do + r = ResourcePicture.himlist @other_artist + r.should eq [@other_rp] + end + it '時系列で並んでいる' do + nop = FactoryGirl.create :original_picture, :artist_id => @other_artist.id + nrp = FactoryGirl.create :resource_picture, :artist_id => @other_artist.id, :license_id => @license.id, :original_picture_id => nop.id, :picture_id => @other_p.id, :updated_at => Time.now + 100 + r = ResourcePicture.himlist @other_artist + r.should eq [nrp, @other_rp] + end + context 'DBに5件あって1ページの件数を2件に変えたとして' do + before do + @op2 = FactoryGirl.create :original_picture, :artist_id => @other_artist.id + @rp2 = FactoryGirl.create :resource_picture, :artist_id => @other_artist.id, :license_id => @license.id, :original_picture_id => @op2.id, :picture_id => @p.id, :updated_at => Time.now + 100 + @op3 = FactoryGirl.create :original_picture, :artist_id => @other_artist.id + @rp3 = FactoryGirl.create :resource_picture, :artist_id => @other_artist.id, :license_id => @license.id, :original_picture_id => @op3.id, :picture_id => @p.id, :updated_at => Time.now + 200 + @op4 = FactoryGirl.create :original_picture, :artist_id => @other_artist.id + @rp4 = FactoryGirl.create :resource_picture, :artist_id => @other_artist.id, :license_id => @license.id, :original_picture_id => @op4.id, :picture_id => @p.id, :updated_at => Time.now + 300 + @op5 = FactoryGirl.create :original_picture, :artist_id => @other_artist.id + @rp5 = FactoryGirl.create :resource_picture, :artist_id => @other_artist.id, :license_id => @license.id, :original_picture_id => @op5.id, :picture_id => @p.id, :updated_at => Time.now + 400 + end + it '通常は2件を返す' do + r = ResourcePicture.himlist @other_artist, 1, 2 + r.should have(2).items + end + it 'page=1なら末尾2件を返す' do + #時系列で並んでいる + r = ResourcePicture.himlist(@other_artist, 1, 2) + r.should eq [@rp5, @rp4] + end + it 'page=2なら中間2件を返す' do + r = ResourcePicture.himlist(@other_artist, 2, 2) + r.should eq [@rp3, @rp2] + end + it 'page=3なら先頭1件を返す' do + r = ResourcePicture.himlist(@other_artist, 3, 2) + r.should eq [@other_rp] + end + end + end + + describe '他作家の素材一覧ページ制御に於いて' do + before do + ResourcePicture.stub(:count).with(any_args).and_return(100) + end + it 'ページ制御を返す' do + r = ResourcePicture.himlist_paginate @artist + r.is_a?(Kaminari::PaginatableArray).should be_true + end + it '自分の素材一覧の取得条件を利用している' do + ResourcePicture.stub(:himlist_where).with(any_args).and_return('') + ResourcePicture.should_receive(:himlist_where).with(any_args).exactly(1) + r = ResourcePicture.himlist_paginate @artist + end + it 'ページ件数10のとき、3ページ目のオフセットは20から始まる' do + r = ResourcePicture.himlist_paginate @artist, 3, 10 + r.limit_value.should eq 10 + r.offset_value.should eq 20 + end + end + + describe '一覧取得オプションに於いて' do + it '3つの項目を含んでいる' do + r = ResourcePicture.list_opt + r.should have(3).items + end + it 'ライセンスを含んでいる' do + r = ResourcePicture.list_opt + r.has_key?(:license).should be_true + end + it '絵師を含んでいる' do + r = ResourcePicture.list_opt + r.has_key?(:artist).should be_true + end + it '実素材を含んでいる' do + r = ResourcePicture.list_opt + r.has_key?(:picture).should be_true + end + end + describe 'json一覧出力オプションに於いて' do + before do + @op = FactoryGirl.create :original_picture, :artist_id => @artist.id + @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id + @rp = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => @op.id, :picture_id => @p.id + @sbt = FactoryGirl.create :speech_balloon_template + @comic = FactoryGirl.create :comic, :author_id => @author.id, :visible => 1 + @panel = FactoryGirl.create :panel, :author_id => @author.id, :publish => 1 + @story = FactoryGirl.create :story, :author_id => @author.id, :comic_id => @comic.id, :panel_id => @panel.id + end + it 'ライセンスを含んでいる' do + r = ResourcePicture.list.to_json ResourcePicture.list_json_opt + j = JSON.parse r + i = j.first + i.has_key?('license').should be_true + end + it '絵師を含んでいる' do + r = ResourcePicture.list.to_json ResourcePicture.list_json_opt + j = JSON.parse r + i = j.first + i.has_key?('artist').should be_true + end + it '実素材を含んでいる' do + r = ResourcePicture.list.to_json ResourcePicture.list_json_opt + j = JSON.parse r + i = j.first + i.has_key?('picture').should be_true + end + end - describe '単体取得に於いて' do - before do - @op = Factory :original_picture, :artist_id => @artist.id, :license_id => @license.id - @rp = Factory :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => @op.id - end - it '指定の素材を返す' do - pic = ResourcePicture.show @rp.id - pic.should eq @rp - end - context '関連テーブルオプションがないとき' do - it 'ライセンスと絵師を含んでいる' do - r = ResourcePicture.show_include_opt - r.should eq [:license, :artist] - end - end - context '関連テーブルオプションで原画を含ませたとき' do - it 'ライセンスと絵師と原画データを含んでいる' do - r = ResourcePicture.show_include_opt(:include => :original_picture) - r.should eq [:license, :artist, :original_picture] - end - end - context '存在しない素材を開こうとしたとき' do - it '404RecordNotFound例外を返す' do - lambda{ - pic = ResourcePicture.show 0 - }.should raise_error(ActiveRecord::RecordNotFound) - end - end - end - describe 'json単体出力オプションに於いて' do - it 'includeキーがライセンスと絵師を含んでいる' do - r = ResourcePicture.show_json_include_opt - r[:include].should eq [:license, :artist] - end - end - describe '一覧取得に於いて' do - before do - @op = Factory :original_picture, :artist_id => @artist.id, :license_id => @license.id - @rp = Factory :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => @op.id - end - context 'page補正について' do - it '文字列から数値に変換される' do - ResourcePicture.page('8').should eq 8 - end - it 'nilの場合は1になる' do - ResourcePicture.page().should eq 1 - end - it '0以下の場合は1になる' do - ResourcePicture.page('0').should eq 1 - end - end - context 'page_size補正について' do - it '文字列から数値に変換される' do - ResourcePicture.page_size('7').should eq 7 - end - it 'nilの場合はResourcePicture.default_page_sizeになる' do - ResourcePicture.page_size().should eq ResourcePicture.default_page_size - end - it '0以下の場合はResourcePicture.default_page_sizeになる' do - ResourcePicture.page_size('0').should eq ResourcePicture.default_page_size - end - it 'ResourcePicture.max_page_sizeを超えた場合はResourcePicture.max_page_sizeになる' do - ResourcePicture.page_size('1000').should eq ResourcePicture.max_page_size - end - end - it 'リストを返す' do - pic = ResourcePicture.list - pic.should eq [@rp] - end - it '時系列で並んでいる' do - nop = Factory :original_picture, :artist_id => @artist.id, :license_id => @license.id - nrp = Factory :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => nop.id, :updated_at => Time.now + 100 - pic = ResourcePicture.list - pic.should eq [nrp, @rp] - end - context 'DBに5件あって1ページの件数を2件に変えたとして' do - before do - nop2 = Factory :original_picture, :artist_id => @artist.id, :license_id => @license.id - @nrp2 = Factory :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => nop2.id, :updated_at => Time.now + 100 - nop3 = Factory :original_picture, :artist_id => @artist.id, :license_id => @license.id - @nrp3 = Factory :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => nop3.id, :updated_at => Time.now + 200 - nop4 = Factory :original_picture, :artist_id => @artist.id, :license_id => @license.id - @nrp4 = Factory :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => nop4.id, :updated_at => Time.now + 300 - nop5 = Factory :original_picture, :artist_id => @artist.id, :license_id => @license.id - @nrp5 = Factory :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => nop5.id, :updated_at => Time.now + 400 - ResourcePicture.stub(:default_page_size).and_return(2) - end - it '通常は2件を返す' do - pic = ResourcePicture.list - pic.should have(2).items - end - it 'page=1なら末尾2件を返す' do - #時系列で並んでいる - pic = ResourcePicture.list({}, 1) - pic.should eq [@nrp5, @nrp4] - end - it 'page=2なら中間2件を返す' do - pic = ResourcePicture.list({}, 2) - pic.should eq [@nrp3, @nrp2] - end - it 'page=3なら先頭1件を返す' do - pic = ResourcePicture.list({}, 3) - pic.should eq [@rp] - end - end - end - describe 'json一覧出力オプションに於いて' do - it 'includeキーがライセンスと絵師を含んでいる' do - r = ResourcePicture.list_json_opt - r[:include].should eq [:license, :artist] - end + describe '単体取得に於いて' do + before do + @op = FactoryGirl.create :original_picture, :artist_id => @artist.id + @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id + @rp = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :original_picture_id => @op.id, :license_id => @license.id, :picture_id => @p.id + end + context 'つつがなく終わるとき' do + it '単体取得オプションを利用している' do + ResourcePicture.stub(:show_opt).with(any_args).and_return({}) + ResourcePicture.should_receive(:show_opt).with(any_args).exactly(1) + r = ResourcePicture.show @rp.id, @author + end + it '閲覧許可を問い合わせている' do + ResourcePicture.any_instance.stub(:visible?).with(any_args).and_return(true) + ResourcePicture.any_instance.should_receive(:visible?).with(any_args).exactly(1) + r = ResourcePicture.show @rp.id, @author + end + end + it '指定の素材を返す' do + r = ResourcePicture.show @rp.id, @author + r.should eq @rp + end + context '他人の素材を開こうとしたとき' do + it '403Forbidden例外を返す' do + ResourcePicture.any_instance.stub(:visible?).and_return(false) + lambda{ + r = ResourcePicture.show @rp.id, @other_author + }.should raise_error(ActiveRecord::Forbidden) + end + end + context '存在しない素材を開こうとしたとき' do + it '404RecordNotFound例外を返す' do + lambda{ + r = ResourcePicture.show 0, @author + }.should raise_error(ActiveRecord::RecordNotFound) + end + end + end + describe '単体取得オプションに於いて' do + it 'includeキーを含んでいる' do + r = ResourcePicture.show_opt + r.has_key?(:include).should be_true + end + it '3つの項目を含んでいる' do + r = ResourcePicture.show_opt[:include] + r.should have(3).items + end + it 'ライセンスを含んでいる' do + r = ResourcePicture.show_opt[:include] + r.has_key?(:license).should be_true + end + it '絵師を含んでいる' do + r = ResourcePicture.show_opt[:include] + r.has_key?(:artist).should be_true + end + it '実素材を含んでいる' do + r = ResourcePicture.show_opt[:include] + r.has_key?(:picture).should be_true + end + end + describe 'json単体出力オプションに於いて' do + before do + @op = FactoryGirl.create :original_picture, :artist_id => @artist.id + @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id + @rp = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => @op.id, :picture_id => @p.id + @sbt = FactoryGirl.create :speech_balloon_template + @comic = FactoryGirl.create :comic, :author_id => @author.id, :visible => 1 + @panel = FactoryGirl.create :panel, :author_id => @author.id, :publish => 1 + @story = FactoryGirl.create :story, :author_id => @author.id, :comic_id => @comic.id, :panel_id => @panel.id + end + it 'ライセンスを含んでいる' do + r = ResourcePicture.show(@rp.id, @author).to_json ResourcePicture.show_json_opt + j = JSON.parse r + i = j + i.has_key?('license').should be_true + end + it '絵師を含んでいる' do + r = ResourcePicture.show(@rp.id, @author).to_json ResourcePicture.show_json_opt + j = JSON.parse r + i = j + i.has_key?('artist').should be_true + end + it '実素材を含んでいる' do + r = ResourcePicture.show(@rp.id, @author).to_json ResourcePicture.show_json_opt + j = JSON.parse r + i = j + i.has_key?('picture').should be_true + end end - - describe 'サイズの調整に於いて' do - before do - end - - it 'サイズに収まるときはそのまま使う' do - ResourcePicture.fix_size_both(64, 64, 64, 64).should eq [64, 64] - end - it '小さくても拡大しない' do - ResourcePicture.fix_size_both(64, 64, 32, 32).should eq [32, 32] - end - it '縦長のときは縦に合わせて縮小' do - ResourcePicture.fix_size_both(64, 64, 64, 128).should eq [32, 64] - end - it '横長のときは横に合わせて縮小' do - ResourcePicture.fix_size_both(64, 64, 128, 64).should eq [64, 32] - end - end - describe 'フォーマット変換対象判定に於いて' do - before do - @rp = Factory.build :resource_picture, - :artist_id => @artist.id, :license_id => @license.id - end + describe 'フォーマット変換対象判定に於いて' do + before do + @rp = FactoryGirl.build :resource_picture, + :artist_id => @artist.id, :license_id => @license.id + end context '変換するケース' do it '画像フォーマットがpngかつライセンスの変換禁止フラグが無効のときTrue' do - License.any_instance.stub(:no_convert).with(any_args).and_return(0) + ResourcePicture.any_instance.stub(:flag_gif_convert).with(any_args).and_return(0) @rp.ext = 'png' - @rp.to_gif?.should be_true - end - end - context '変換しないケース' do + @rp.to_gif?.should be_true + end + end + context '変換しないケース' do it '画像フォーマットがでない' do - License.any_instance.stub(:no_convert).with(any_args).and_return(0) + ResourcePicture.any_instance.stub(:flag_gif_convert).with(any_args).and_return(0) @rp.ext = 'gif' - @rp.to_gif?.should be_false - end - it 'ライセンスの変換禁止フラグが無効' do - License.any_instance.stub(:no_convert).with(any_args).and_return(1) + @rp.to_gif?.should be_false + end + it '変換禁止フラグが無効' do + ResourcePicture.any_instance.stub(:flag_gif_convert).with(any_args).and_return(-1) @rp.ext = 'png' - @rp.to_gif?.should be_false - end - end - end - - describe 'Gifフォーマット変換に於いて' do - before do - Magick::Image.stub(:from_blob).with(any_args).and_return([Mgk.new]) - end - context 'つつがなく終わるとき' do - it 'Mgkオブジェクトが返る' do - mgk = ResourcePicture.png_to_gif('mgkbin') - mgk.is_a?(Mgk).should be_true - end - it 'Mgkオブジェクトはgif変換されている' do - #スタブばかりで変換できないので代入されているかでチェックする - Mgk.any_instance.should_receive(:format=).with('gif').exactly(1) - ResourcePicture.png_to_gif('mgkbin') - end - end - context 'RMagick変換が失敗したとき' do - before do - Magick::Image.should_receive(:from_blob).with(any_args).and_raise('StandardError') - end - it 'falseを返す' do - res = ResourcePicture.png_to_gif('mgkbin') - res.should be_false - end - end - end - - describe '対象素材の取得に於いて' do + @rp.to_gif?.should be_false + end + end + end + + describe '新規実素材の取得に於いて' do + before do + @op = FactoryGirl.create :original_picture, :artist_id => @artist.id + @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id + attr = {:original_picture_id => @op.id, :license_id => @license.id, :artist_name => 'tester', + :credit => '{}', :settings => {:reverse => 1, :gif_convert => 1}.to_json.to_s} + @rp = FactoryGirl.build :resource_picture, attr + @imager = ImagerTest.load "abc\ndef\nghi" + end + context '事前チェック' do + before do + Picture.any_instance.stub(:store).with(any_args).and_return(true) + end + it '実素材モデルにデフォルト値補充を依頼している' do + Picture.any_instance.should_receive(:supply_default).with(any_args).exactly(1) + r = @rp.new_picture @imager + end + it '実素材モデルに上書き補充を依頼している' do + Picture.any_instance.should_receive(:overwrite).with(any_args).exactly(1) + r = @rp.new_picture @imager + end + it '実素材を保存している' do + Picture.any_instance.should_receive(:store).with(any_args).exactly(1) + r = @rp.new_picture @imager + end + end + context 'つつがなく終わるとき' do + it '保存された実素材を返す' do + r = @rp.new_picture @imager + r.is_a?(Picture).should be_true + end + end + #以下から例外ケース。処理先頭から失敗させていく + context '実素材の保存に失敗したとき' do + before do + Picture.any_instance.stub(:store).with(any_args).and_return(false) + end + it 'Falseを返す' do + r = @rp.new_picture @imager + r.should be_false + end + it '自身の全体エラーメッセージにその旨をセットしている' do + r = @rp.new_picture @imager + @rp.errors[:base].should_not be_empty + end + end + end + + describe '作成・更新に於いて' do + before do + @op = FactoryGirl.create :original_picture, :artist_id => @artist.id + @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id + attr = {:original_picture_id => @op.id, :picture_id => nil, :license_id => @license.id, :artist_name => 'tester', :classname => 'StandardLicense', :credit => '{}', :settings => {}.to_json.to_s} + @rp = FactoryGirl.build :resource_picture, attr + @imager = ImagerTest.load "abc\ndef\nghi" + end + context '事前チェック' do + before do + #すべての処理が正常パターンで通過すれば、一番深い分岐まで通る。 + #それで外部のメソッド呼び出しだけに着目してテストする。 + OriginalPicture.any_instance.stub(:save).and_return(true) + ResourcePicture.any_instance.stub(:new_picture).with(any_args).and_return(@p) + ResourcePicture.any_instance.stub(:store_picture_with_gif).with(any_args).and_return(true) + end + it '原画に保存を依頼している' do + OriginalPicture.any_instance.should_receive(:save).exactly(1) + r = @rp.store @imager + end + it '新規実素材の取得を依頼している' do + ResourcePicture.any_instance.should_receive(:new_picture).with(any_args).exactly(1) + r = @rp.store @imager + end + it '自身を保存している' do + ResourcePicture.any_instance.should_receive(:save).with(any_args).exactly(1) + r = @rp.store @imager + end + it 'gif付き画像ファイルの作成・更新機能で画像を保存している' do + ResourcePicture.any_instance.should_receive(:store_picture_with_gif).with(@imager).exactly(1) + r = @rp.store @imager + end + end + context 'つつがなく終わるとき' do + before do + #すべての処理を正常パターンで通過させ、保存機能をチェックする。 + ResourcePicture.any_instance.stub(:new_picture).with(any_args).and_return(@p) + ResourcePicture.any_instance.stub(:store_picture_with_gif).with(@imager).and_return(true) + end + it 'Trueを返す' do + r = @rp.store @imager + r.should be_true + end + it '原画の停止日時をクリアする' do + r = @rp.store @imager + @rp.original_picture.stopped_at.should be_nil + end + it '原画の公開日時に現在時刻をセットする' do + lambda { + r = @rp.store @imager + }.should change(@rp.original_picture, :published_at) + end + it '実素材idから最新画像idを取得してセットしている' do + r = @rp.store @imager + @rp.picture_id.should eq @p.id + end + it '自身が保存されている' do + lambda { + r = @rp.store @imager + }.should change ResourcePicture, :count + end + end + #以下から例外ケース。処理先頭から失敗させていく + context '原画の保存に失敗したとき' do + before do + OriginalPicture.any_instance.stub(:save).and_return(false) + end + it 'Falseを返す' do + r = @rp.unpublish + r.should be_false + end + it 'ロールバックしている' do + lambda { + r = @rp.unpublish + }.should_not change(ResourcePicture, :count) + end + end + context '画像オブジェクトの取得に失敗したとき' do + before do + @imager = false + end + it 'Falseを返す' do + r = @rp.store @imager + r.should be_false + end + it '更新されていない' do + r = @rp.store @imager + @rp.should be_a_new ResourcePicture + end + it '処理を中断してロールバックする' do + lambda { + r = @rp.store @imager + }.should_not change Picture, :count + end + end + context '新規実素材の取得に失敗したとき' do + before do + ResourcePicture.any_instance.stub(:new_picture).with(any_args).and_return(false) + end + it 'Falseを返す' do + r = @rp.store @imager + r.should be_false + end + it '更新されていない' do + r = @rp.store @imager + @rp.should be_a_new ResourcePicture + end + it '処理を中断してロールバックする' do + lambda { + r = @rp.store @imager + }.should_not change Picture, :count + end + end + context '自身の保存に失敗したとき' do + before do + ResourcePicture.any_instance.stub(:new_picture).with(any_args).and_return(@p) + ResourcePicture.any_instance.stub(:save).with(any_args).and_return(false) + end + it 'Falseを返す' do + r = @rp.store @imager + r.should be_false + end + it '更新されていない' do + r = @rp.store @imager + @rp.should be_a_new ResourcePicture + end + it '処理を中断してロールバックする' do + lambda { + r = @rp.store @imager + }.should_not change Picture, :count + end + end + context 'gif付き画像ファイルの作成・更新機能に失敗したとき' do + before do + ResourcePicture.any_instance.stub(:new_picture).with(any_args).and_return(@p) + ResourcePicture.any_instance.stub(:store_picture_with_gif).with(any_args).and_return(false) + end + it 'Falseを返す' do + r = @rp.store @imager + r.should be_false + end + it '更新されていない' do + r = @rp.store @imager + @rp.should be_a_new ResourcePicture + end + it '処理を中断してロールバックする' do + lambda { + r = @rp.store @imager + }.should_not change Picture, :count + end + end + end + + describe 'gif付き画像ファイルの作成・更新に於いて' do before do - @op = Factory.build :original_picture, :ext => 'jpeg', :width => 264, :height => 265, :filesize => 266, - :artist_id => @artist.id, :license_id => @license.id - @rp = Factory.build :resource_picture, - :artist_id => @artist.id, :license_id => @license.id - end - context '原画オブジェクトが素材を持っている(更新ケース)' do - before do - OriginalPicture.any_instance.stub(:resource_picture).with(any_args).and_return(@rp) - end - it 'それを対象素材として返す' do - res = ResourcePicture.update_picture(@op) - res.should eq @rp - end - end - context '持っていない(新規作成ケース)' do - before do - OriginalPicture.any_instance.stub(:resource_picture).with(any_args).and_return(nil) - end - it '新規素材モデルを用意し、それを対象素材として返す' do - res = ResourcePicture.update_picture(@op) - res.should be_a_new ResourcePicture - end - it '原画オブジェクトから属性を取り出して対象素材にセットしている' do - res = ResourcePicture.update_picture(@op) - res.ext.should eq 'jpeg' - res.width.should eq 264 - res.height.should eq 265 - res.filesize.should eq 266 - res.artist_id.should eq @artist.id - res.license_id.should eq @license.id - end - - end - end - - describe '作成・更新に於いて' do - before do - @op = Factory :original_picture, :artist_id => @artist.id, :license_id => @license.id - @rp = Factory.build :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => @op.id - end - context '事前チェック' do - before do - #すべての処理が正常パターンで通過すれば、一番深い分岐まで通る。 - #それで外部のメソッド呼び出しだけに着目してテストする。 - ResourcePicture.any_instance.stub(:save).with(any_args).and_return(true) + @op = FactoryGirl.create :original_picture, :artist_id => @artist.id + @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id + attr = {:original_picture_id => @op.id, :license_id => @license.id, :artist_name => 'tester', :credit => '{}', :settings => {}.to_json.to_s} + @rp = FactoryGirl.build :resource_picture, attr + @imager = ImagerTest.load "abc\ndef\nghi" + end + context '事前チェック' do + before do + #すべての処理が正常パターンで通過すれば、一番深い分岐まで通る。 + #それで外部のメソッド呼び出しだけに着目してテストする。 ResourcePicture.any_instance.stub(:store_picture).with(any_args).and_return(true) - ResourcePicture.stub(:to_gif?).with(any_args).and_return(true) - class GifMgk < Mgk ; end #store_pictureは二回呼び出される。区別をつけるために - @gifmgk = GifMgk.new #パラメータを二種類用意する。 - ResourcePicture.stub(:png_to_gif).with(any_args).and_return(@gifmgk) - end - it '自身を保存している' do - ResourcePicture.any_instance.should_receive(:save).with(any_args).exactly(1) - res = @rp.store(Mgk.new) + ResourcePicture.any_instance.stub(:to_gif?).with(any_args).and_return(true) + ResourcePicture.any_instance.stub(:filename).with(any_args).and_return('1.png') + ResourcePicture.any_instance.stub(:gifname).with(any_args).and_return('1.gif') end it '画像ファイルの作成・更新機能で画像を保存している' do - mgk = Mgk.new #一回目の本画像保存は与えたオブジェクトを使って保存する。 - ResourcePicture.any_instance.should_receive(:store_picture).with(mgk).exactly(1) - res = @rp.store(mgk) - end - it '自身にフォーマット変換対象かを問い合わせている' do - ResourcePicture.any_instance.should_receive(:to_gif?).with(any_args).exactly(1) - res = @rp.store(Mgk.new) - end - it '自身にGifフォーマット変換を依頼している' do - ResourcePicture.should_receive(:png_to_gif).with(any_args).exactly(1) - res = @rp.store(Mgk.new) - end - it '画像ファイルの作成・更新機能でgif画像を保存している' do - #二回目の保存はgif変換の結果を渡す。 - ResourcePicture.any_instance.should_receive(:store_picture).with(@gifmgk).exactly(1) - res = @rp.store(Mgk.new) - end - end - context 'つつがなく終わるとき' do - before do - #すべての処理を正常パターンで通過させ、保存機能をチェックする。 -# ResourcePicture.any_instance.stub(:save).with(any_args).and_return(true) + #二回目の保存はgif変換の結果を渡す。 + ResourcePicture.any_instance.should_receive(:store_picture).with(any_args).exactly(2) + r = @rp.store_picture_with_gif @imager + end + it '自身にフォーマット変換対象かを問い合わせている' do + ResourcePicture.any_instance.should_receive(:to_gif?).with(any_args).exactly(1) + r = @rp.store_picture_with_gif @imager + end + it '画像ライブラリにGifフォーマット変換を依頼している' do + ImagerTest.any_instance.should_receive(:to_gif).with(any_args).exactly(1) + r = @rp.store_picture_with_gif @imager + end + end + context 'つつがなく終わるとき' do + before do + #すべての処理を正常パターンで通過させ、保存機能をチェックする。 ResourcePicture.any_instance.stub(:store_picture).with(any_args).and_return(true) ResourcePicture.any_instance.stub(:to_gif?).with(any_args).and_return(true) - class GifMgk < Mgk ; end #store_pictureは二回呼び出される。区別をつけるために - @gifmgk = GifMgk.new #パラメータを二種類用意する。 - ResourcePicture.stub(:png_to_gif).with(any_args).and_return(@gifmgk) - end - it 'Trueを返す' do - res = @rp.store(Mgk.new) - res.should be_true + ResourcePicture.any_instance.stub(:filename).with(any_args).and_return('1.png') + ResourcePicture.any_instance.stub(:gifname).with(any_args).and_return('1.gif') end - it '自身が保存されている' do - lambda { - res = @rp.store(Mgk.new) - }.should change ResourcePicture, :count + it 'Trueを返す' do + r = @rp.store_picture_with_gif @imager + r.should be_true end - end - context 'gif変換なしで、つつがなく終わるとき' do - before do - #すべての処理を正常パターンで通過させ、保存機能をチェックする。 - ResourcePicture.any_instance.stub(:save).with(any_args).and_return(true) + end + context 'gif変換なしで、つつがなく終わるとき' do + before do + #すべての処理を正常パターンで通過させ、保存機能をチェックする。 ResourcePicture.any_instance.stub(:store_picture).with(any_args).and_return(true) ResourcePicture.any_instance.stub(:to_gif?).with(any_args).and_return(false) - end - it 'Trueを返す' do - res = @rp.store(Mgk.new) - res.should be_true + ResourcePicture.any_instance.stub(:filename).with(any_args).and_return('1.png') + ResourcePicture.any_instance.stub(:gifname).with(any_args).and_return('1.gif') end - it 'gif保存は呼ばれていない' do - ResourcePicture.any_instance.should_receive(:store_picture).with(any_args).exactly(1) - res = @rp.store(Mgk.new) + it 'Trueを返す' do + r = @rp.store_picture_with_gif @imager + r.should be_true end - end - #以下から例外ケース。処理先頭から失敗させていく - context '自身の保存に失敗したとき' do - before do - ResourcePicture.any_instance.stub(:save).with(any_args).and_return(false) - end - it 'Falseを返す' do - res = @rp.store(Mgk.new) - res.should be_false + it 'gif保存は呼ばれていない' do + ResourcePicture.any_instance.should_receive(:store_picture).with(any_args).exactly(1) + r = @rp.store_picture_with_gif @imager end - it '更新されていない' do - @rp.store(Mgk.new) - @rp.should be_a_new ResourcePicture - end - end - context '画像の保存に失敗したとき' do - before do - ResourcePicture.any_instance.stub(:save).with(any_args).and_return(true) + end + #以下から例外ケース。処理先頭から失敗させていく + context '画像の保存に失敗したとき' do + before do ResourcePicture.any_instance.stub(:store_picture).with(any_args).and_return(false) - end - it 'Falseを返す' do - res = @rp.store(Mgk.new) - res.should be_false + end + it 'Falseを返す' do + r = @rp.store_picture_with_gif @imager + r.should be_false + end + it '全体エラーメッセージがセットされている' do + lambda { + r = @rp.store_picture_with_gif @imager + }.should change(@rp.errors[:base], :count) end it 'gif変換判定は呼ばれていない' do - ResourcePicture.any_instance.should_not_receive(:to_gif?).with(any_args) - res = @rp.store(Mgk.new) + ResourcePicture.any_instance.should_not_receive(:to_gif?).with(any_args) + r = @rp.store_picture_with_gif @imager end - end - context 'gif変換に失敗したとき' do - before do - ResourcePicture.any_instance.stub(:save).with(any_args).and_return(true) + end + context 'gif変換に失敗したとき' do + before do ResourcePicture.any_instance.stub(:store_picture).with(any_args).and_return(true) ResourcePicture.any_instance.stub(:to_gif?).with(any_args).and_return(true) - ResourcePicture.stub(:png_to_gif).with(any_args).and_return(false) - end - it 'Falseを返す' do - res = @rp.store(Mgk.new) - res.should be_false + ImagerTest.any_instance.stub(:to_gif).with(any_args).and_return(false) + end + it 'Falseを返す' do + r = @rp.store_picture_with_gif @imager + r.should be_false + end + it '全体エラーメッセージがセットされている' do + lambda { + r = @rp.store_picture_with_gif @imager + }.should change(@rp.errors[:base], :count) end it 'gif画像の保存は呼ばれていない' do #本画像の保存があるので、一度は呼ばれる - ResourcePicture.any_instance.should_receive(:store_picture).with(any_args).exactly(1) - res = @rp.store(Mgk.new) + ResourcePicture.any_instance.should_receive(:store_picture).with(any_args).exactly(1) + r = @rp.store_picture_with_gif @imager end - end - context 'gif画像の保存に失敗したとき' do + end + context 'gif画像の保存に失敗したとき' do before do - @mgk = Mgk.new - ResourcePicture.any_instance.stub(:save).with(any_args).and_return(true) - ResourcePicture.any_instance.stub(:store_picture).with(@mgk).and_return(true) + ResourcePicture.any_instance.stub(:store_picture).with(@imager, '1.png').and_return(true) ResourcePicture.any_instance.stub(:to_gif?).with(any_args).and_return(true) - class GifMgk < Mgk ; end #store_pictureは二回呼び出される。区別をつけるために - @gifmgk = GifMgk.new #パラメータを二種類用意する。 - ResourcePicture.stub(:png_to_gif).with(any_args).and_return(@gifmgk) - ResourcePicture.any_instance.stub(:store_picture).with(@gifmgk).and_return(false) - end - it 'Falseを返す' do - res = @rp.store(@mgk) - res.should be_false - end - end + @gifimager = @imager.to_gif + ImagerTest.any_instance.stub(:to_gif).with(any_args).and_return(@gifimager) + ResourcePicture.any_instance.stub(:store_picture).with(@gifimager, '1.gif').and_return(false) + ResourcePicture.any_instance.stub(:filename).with(any_args).and_return('1.png') + ResourcePicture.any_instance.stub(:gifname).with(any_args).and_return('1.gif') + end + it 'Falseを返す' do + r = @rp.store_picture_with_gif @imager + r.should be_false + end + it '全体エラーメッセージがセットされている' do + lambda { + r = @rp.store_picture_with_gif @imager + }.should change(@rp.errors[:base], :count) + end + end end - + describe '画像ファイルの作成・更新に於いて' do #PictureIo経由で画像を保存するための機能。ファイル名に自身のidを使うので事前に自身の保存が必要。 - before do - PictureIO.resource_picture_io.class.stub(:subdirs).with(any_args).and_return(['', 'v', 'h', 'vh', 'thumbnail']) - ResourcePicture.any_instance.stub(:h).with(any_args).and_return('data') - ResourcePicture.any_instance.stub(:v).with(any_args).and_return('data') - ResourcePicture.any_instance.stub(:vh).with(any_args).and_return('data') - ResourcePicture.any_instance.stub(:thumbnail).with(any_args).and_return('data') - @op = Factory :original_picture, :artist_id => @artist.id, :license_id => @license.id - @rp = Factory :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => @op.id - end - context '事前チェック' do - before do - #すべての処理を正常パターンで通過させ、保存機能をチェックする。 - PictureIO.resource_picture_io.stub(:put).with(any_args).and_return(true) - end - it '画像ファイルの保存が5回呼ばれる' do - PictureIO.resource_picture_io.should_receive(:put).with(any_args).exactly(5) - res = @rp.store_picture(Mgk.new) - end - it '画像ファイルのベースへの保存が1回呼ばれる' do - PictureIO.resource_picture_io.should_receive(:put).with('data', @rp.filename, '').exactly(1) - res = @rp.store_picture(Mgk.new) - end - it '画像ファイルの垂直反転への保存が1回呼ばれる' do - PictureIO.resource_picture_io.should_receive(:put).with('data', @rp.filename, 'v').exactly(1) - res = @rp.store_picture(Mgk.new) - end - it '画像ファイルの水平反転への保存が1回呼ばれる' do - PictureIO.resource_picture_io.should_receive(:put).with('data', @rp.filename, 'h').exactly(1) - res = @rp.store_picture(Mgk.new) - end - it '画像ファイルの垂直水平反転への保存が1回呼ばれる' do - PictureIO.resource_picture_io.should_receive(:put).with('data', @rp.filename, 'vh').exactly(1) - res = @rp.store_picture(Mgk.new) - end - it '画像ファイルのサムネイルへの保存が1回呼ばれる' do - PictureIO.resource_picture_io.should_receive(:put).with('data', @rp.filename, 'thumbnail').exactly(1) - res = @rp.store_picture(Mgk.new) - end - it '垂直反転が1回呼ばれる' do - ResourcePicture.any_instance.should_receive(:v).with(any_args).exactly(1) - res = @rp.store_picture(Mgk.new) - end - it '水平反転が1回呼ばれる' do - ResourcePicture.any_instance.should_receive(:h).with(any_args).exactly(1) - res = @rp.store_picture(Mgk.new) - end - it '垂直水平反転が1回呼ばれる' do - ResourcePicture.any_instance.should_receive(:vh).with(any_args).exactly(1) - res = @rp.store_picture(Mgk.new) + before do + @op = FactoryGirl.create :original_picture, :artist_id => @artist.id + @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id + attr = {:original_picture_id => @op.id, :license_id => @license.id, :artist_name => 'tester', :credit => '{}', :settings => {}.to_json.to_s} + @rp = FactoryGirl.build :resource_picture, attr + @rp.overwrite @op + @imager = ImagerTest.load "abc\ndef\nghi" + end + context '事前チェック' do + before do + #すべての処理を正常パターンで通過させ、保存機能をチェックする。 + PictureIO.resource_picture_io.stub(:put).with(any_args).and_return(true) + ResourcePicture.any_instance.stub(:flag_thumbnail).with(any_args).and_return(1) end it 'サムネイル化が1回呼ばれる' do - ResourcePicture.any_instance.should_receive(:thumbnail).with(any_args).exactly(1) - res = @rp.store_picture(Mgk.new) + ImagerTest.any_instance.should_receive(:to_thumbnail).with(any_args).exactly(1) + r = @rp.store_picture(@imager, '1.png') + end + it '画像ファイルの保存が2回呼ばれる' do + PictureIO.resource_picture_io.should_receive(:put).with(any_args).exactly(2) + r = @rp.store_picture(@imager, '1.gif') end - end - context 'つつがなく終わるとき' do - before do + it '画像ファイルのベースへのサムネイル保存が1回呼ばれる' do + PictureIO.resource_picture_io.should_receive(:put).with(@imager.to_thumbnail.binary, '1.gif').exactly(1) + r = @rp.store_picture(@imager, '1.gif') + end + it '画像ファイルのfullへの保存が1回呼ばれる' do + PictureIO.resource_picture_io.should_receive(:put).with(@imager.binary, '1.gif', 'full').exactly(1) + r = @rp.store_picture(@imager, '1.gif') + end + end + context 'つつがなく終わるとき' do + before do #すべての処理を正常パターンで通過させ、保存機能をチェックする。 - PictureIO.resource_picture_io.stub(:put).with(any_args).and_return(true) - end - it 'Trueを返す' do - res = @rp.store_picture(Mgk.new) - res.should be_true - end - end - context '例外ケース' do - before do - PictureIO.resource_picture_io.stub(:put).with(any_args).and_return(false) - end - it 'Falseを返す' do - res = @rp.store_picture(Mgk.new) - res.should be_false - end - end - - end - -describe 'サムネイル変換に於いて' do - #サムネイル化した画像データを返すが、スタブをおくので、リサイズと画像データ化を試みるかをチェックする - before do - ResourcePicture.stub(:resize).with(any_args).and_return(Mgk.new) - @rp = Factory :resource_picture, :artist_id => @artist.id, :license_id => @license.id - end - it 'サムネイルサイズに縮小した画像データを返す' do - ResourcePicture.should_receive(:resize).with(any_args).exactly(1) - @rp.thumbnail(Mgk.new) - end - it 'データが返る' do - #全体スタブより - @rp.thumbnail(Mgk.new).should eq 'data' - end - end - - describe '垂直反転変換に於いて' do - #垂直反転した画像データを返すが、スタブをおくので、反転と画像データ化を試みるかをチェックする - before do - Mgk.any_instance.stub(:flip).with(any_args).and_return(Mgk.new) - @rp = Factory :resource_picture, :artist_id => @artist.id, :license_id => @license.id - end - it '垂直反転する' do - Mgk.any_instance.should_receive(:flip).exactly(1) - @rp.v(Mgk.new) - end - it '画像データ化する' do - Mgk.any_instance.should_receive(:to_blob).exactly(1) - @rp.v(Mgk.new) - end - it 'データが返る' do - #全体スタブより - @rp.v(Mgk.new).should eq 'data' - end - end - - describe '水平反転変換に於いて' do - #水平反転した画像データを返すが、スタブをおくので、反転と画像データ化を試みるかをチェックする - before do - Mgk.any_instance.stub(:flop).with(any_args).and_return(Mgk.new) - @rp = Factory :resource_picture, :artist_id => @artist.id, :license_id => @license.id - end - it '水平反転する' do - Mgk.any_instance.should_receive(:flop).exactly(1) - @rp.h(Mgk.new) - end - it '画像データ化する' do - Mgk.any_instance.should_receive(:to_blob).exactly(1) - @rp.h(Mgk.new) - end - it 'データが返る' do - #全体スタブより - @rp.h(Mgk.new).should eq 'data' - end - end - - describe '垂直水平反転変換に於いて' do - #垂直水平反転した画像データを返すが、スタブをおくので、反転と画像データ化を試みるかをチェックする - before do - Mgk.any_instance.stub(:flip).with(any_args).and_return(Mgk.new) - Mgk.any_instance.stub(:flop).with(any_args).and_return(Mgk.new) - @rp = Factory :resource_picture, :artist_id => @artist.id, :license_id => @license.id - end - it '垂直反転する' do - Mgk.any_instance.should_receive(:flip).exactly(1) - @rp.vh(Mgk.new) - end - it '水平反転する' do - Mgk.any_instance.should_receive(:flop).exactly(1) - @rp.vh(Mgk.new) - end - it '画像データ化する' do - Mgk.any_instance.should_receive(:to_blob).exactly(1) - @rp.vh(Mgk.new) - end - it 'データが返る' do - #全体スタブより - @rp.vh(Mgk.new).should eq 'data' - end - end - -end + PictureIO.resource_picture_io.stub(:put).with(any_args).and_return(true) + ResourcePicture.any_instance.stub(:flag_thumbnail).with(any_args).and_return(1) + end + it 'Trueを返す' do + r = @rp.store_picture(@imager, '1.gif') + r.should be_true + end + end + #以下から例外ケース。処理先頭から失敗させていく + context 'サムネイル化に失敗したとき' do + before do + ResourcePicture.any_instance.stub(:flag_thumbnail).with(any_args).and_return(1) + ImagerTest.any_instance.stub(:to_thumbnail).with(any_args).and_return(false) + end + it 'Falseを返す' do + r = @rp.store_picture(@imager, '1.gif') + r.should be_false + end + it '画像ファイルの保存は呼ばれていない' do + PictureIO.resource_picture_io.should_not_receive(:put).with(any_args) + r = @rp.store_picture(@imager, '1.gif') + end + end + context 'サムネイル画像の保存に失敗したとき' do + before do + ResourcePicture.any_instance.stub(:flag_thumbnail).with(any_args).and_return(1) + @tmbimager = @imager.to_thumbnail + ImagerTest.any_instance.stub(:to_thumbnail).with(any_args).and_return(@tmbimager) + PictureIO.resource_picture_io.stub(:put).with(@tmbimager.binary, '1.gif').and_raise(PictureIO::Error) + end + it 'Falseを返す' do + r = @rp.store_picture(@imager, '1.gif') + r.should be_false + end + it '画像ファイルの保存は呼ばれていない' do + PictureIO.resource_picture_io.should_receive(:put).with(any_args).exactly(1) + r = @rp.store_picture(@imager, '1.gif') + end + end + context '画像の保存に失敗したとき' do + before do + ResourcePicture.any_instance.stub(:flag_thumbnail).with(any_args).and_return(1) + @tmbimager = @imager.to_thumbnail + ImagerTest.any_instance.stub(:to_thumbnail).with(any_args).and_return(@tmbimager) + PictureIO.resource_picture_io.stub(:put).with(@tmbimager.binary, '1.gif').and_return(true) + PictureIO.resource_picture_io.stub(:put).with(@imager.binary, '1.gif', 'full').and_raise(PictureIO::Error) + end + it 'Falseを返す' do + r = @rp.store_picture(@imager, '1.gif') + r.should be_false + end + end + + end + + describe '公開停止に於いて' do + before do + @op = FactoryGirl.create :original_picture, :artist_id => @artist.id + @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id + @rp = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => @op.id, :picture_id => @p.id + PictureIO.resource_picture_io.stub(:exist?).with(@rp.filename).and_return(true) + PictureIO.resource_picture_io.stub(:exist?).with(@rp.filename, 'full').and_return(true) + end + context '事前チェックしておく' do + before do + OriginalPicture.any_instance.stub(:save).and_return(true) + ResourcePicture.any_instance.stub(:destroy).and_return(true) + PictureIO.resource_picture_io.stub(:delete).with(@rp.filename).and_return(true) + PictureIO.resource_picture_io.stub(:delete).with(@rp.filename, 'full').and_return(true) + end + it '原画に保存を依頼している' do + OriginalPicture.any_instance.should_receive(:save).exactly(1) + r = @rp.unpublish + end + it '素材モデルに削除を依頼している' do + ResourcePicture.any_instance.should_receive(:destroy).exactly(1) + r = @rp.unpublish + end + it '保管庫にサムネイルの画像データ削除を依頼している' do + PictureIO.resource_picture_io.should_receive(:delete).with(@rp.filename).exactly(1) + r = @rp.unpublish + end + it '保管庫にフルサイズの画像データ削除を依頼している' do + PictureIO.resource_picture_io.should_receive(:delete).with(@rp.filename, 'full').exactly(1) + r = @rp.unpublish + end + end + context 'つつがなく終わるとき' do + it '自身を削除する' do + lambda { + r = @rp.unpublish + }.should change(ResourcePicture, :count).by(-1) + lambda { + r = ResourcePicture.find @rp.id + }.should raise_error + end + it '原画の公開日時をクリアする' do + r = @rp.store @imager + @rp.original_picture.published_at.should be_nil + end + it '原画の停止日時に現在時刻をセットする' do + lambda { + r = @rp.unpublish + }.should change(@rp.original_picture, :stopped_at) + end + it 'Trueを返す' do + r = @rp.unpublish + r.should be_true + end + end + context '原画の保存に失敗したとき' do + before do + OriginalPicture.any_instance.stub(:save).and_return(false) + end + it 'Falseを返す' do + r = @rp.unpublish + r.should be_false + end + it 'ロールバックしている' do + lambda { + r = @rp.unpublish + }.should_not change(ResourcePicture, :count) + end + end + context '自身の削除に失敗したとき' do + before do + ResourcePicture.any_instance.stub(:destroy).with(any_args).and_return(false) + end + it 'Falseを返す' do + r = @rp.unpublish + r.should be_false + end + it 'ロールバックしている' do + lambda { + r = @rp.unpublish + }.should_not change(ResourcePicture, :count) + end + end + context 'サムネイル画像の削除に失敗したとき' do + before do + PictureIO.resource_picture_io.stub(:delete).with(@rp.filename).and_raise(PictureIO::Error) + PictureIO.resource_picture_io.stub(:delete).with(@rp.filename, 'full').and_return(true) + end + it 'Falseを返す' do + r = @rp.unpublish + r.should be_false + end + it 'ロールバックしている' do + lambda { + r = @rp.unpublish + }.should_not change(ResourcePicture, :count) + end + end + context 'フルサイズ画像の削除に失敗したとき' do + before do + PictureIO.resource_picture_io.stub(:delete).with(@rp.filename).and_return(true) + PictureIO.resource_picture_io.stub(:delete).with(@rp.filename, 'full').and_raise(PictureIO::Error) + end + it 'Falseを返す' do + r = @rp.unpublish + r.should be_false + end + it 'ロールバックしている' do + lambda { + r = @rp.unpublish + }.should_not change(ResourcePicture, :count) + end + end + end + + describe 'クレジットデータに於いて' do + before do + @op = FactoryGirl.create :original_picture, :artist_id => @artist.id + @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id + attr = {:original_picture_id => @op.id, :license_id => @license.id, :artist_name => 'tester', :credit => '{"system_picture_id": 2}', :settings => {}.to_json.to_s} + @rp = FactoryGirl.build :resource_picture, attr + end + it 'system_picture_idが入っている' do + res = @rp.credit_data + res["system_picture_id"].should eq 2 + end + end + +end