@jes = JSON.parse @tes
@f = File.expand_path(File.dirname(__FILE__) + '/import.json')
end
-
- describe 'システム画像置換に於いて' do
+
+ describe 'システム画像インポートに於いて' do
before do
- @attr = {"system_picture" => 'DAt'}
+ @attr = {"text" => 'PictureData'}
@sp = FactoryGirl.create :system_picture
+ @imager = ImagerTest.load "abc\ndef\nghi"
end
- context 'つつがなく終わるとき' do
+ context '事前チェック' do
+ it 'カラム値の"text"キーの値をBase64展開している' do
+ Base64.stub(:decode64).with('PictureData').and_return('DecodedPictureData')
+ Base64.should_receive(:decode64).with('PictureData').exactly(1)
+ r = Import.import_system_picture(@attr)
+ end
+ it '画像データで画像ライブラリをロードを依頼している' do
+ Base64.stub(:decode64).with('PictureData').and_return('DecodedPictureData')
+ PettanImager.stub(:load).with('DecodedPictureData').and_return(@imager)
+ PettanImager.should_receive(:load).with('DecodedPictureData').exactly(1)
+ r = Import.import_system_picture(@attr)
+ end
it 'システム画像に保存を依頼している' do
- SystemPicture.stub(:store).with(any_args).and_return(@sp)
- SystemPicture.should_receive(:store).with(any_args).exactly(1)
- r = Import.replace_system_picture(@attr)
+ Base64.stub(:decode64).with('PictureData').and_return('DecodedPictureData')
+ PettanImager.stub(:load).with('DecodedPictureData').and_return(@imager)
+ SystemPicture.stub(:store).with(@imager).and_return(@sp)
+ SystemPicture.should_receive(:store).with(@imager).exactly(1)
+ r = Import.import_system_picture(@attr)
end
- it 'system_pictureキーを削除している' do
- SystemPicture.stub(:store).with(any_args).and_return(@sp)
- r = Import.replace_system_picture(@attr)
- @attr.has_key?("system_picture").should be_false
+ end
+ context 'つつがなく終わるとき' do
+ before do
+ Base64.stub(:decode64).with('PictureData').and_return('DecodedPictureData')
+ PettanImager.stub(:load).with('DecodedPictureData').and_return(@imager)
+ SystemPicture.stub(:store).with(@imager).and_return(@sp)
end
- it 'system_picture_idキーを追加している' do
- SystemPicture.stub(:store).with(any_args).and_return(@sp)
- r = Import.replace_system_picture(@attr)
- @attr.has_key?("system_picture_id").should be_true
+ it 'システム画像オブジェクトのidを返す' do
+ r = Import.import_system_picture(@attr)
+ r.should eq @sp.id
+ end
+ end
+ context '例外ケース' do
+ it 'カラム値に画像データがなかったならFalseを返す' do
+ @attr = {}
+ Base64.stub(:decode64).with('PictureData').and_return('DecodedPictureData')
+ r = Import.import_system_picture(@attr)
+ r.should be_false
end
- it '値はシステム画像オブジェクトのidとしている' do
- SystemPicture.stub(:store).with(any_args).and_return(@sp)
+ it '画像ライブラリのロードが失敗ならFalseを返す' do
+ @attr = {"system_picture" => ''}
+ Base64.stub(:decode64).with('PictureData').and_return('DecodedPictureData')
+ PettanImager.stub(:load).with('DecodedPictureData').and_return(nil)
+ r = Import.import_system_picture(@attr)
+ r.should be_false
+ end
+ it 'システム画像の保存が失敗(False)ならFalseを返す' do
+ Base64.stub(:decode64).with('PictureData').and_return('DecodedPictureData')
+ PettanImager.stub(:load).with('DecodedPictureData').and_return(@imager)
+ SystemPicture.stub(:store).with(@imager).and_return(false)
+ r = Import.import_system_picture(@attr)
+ r.should be_false
+ end
+ end
+ end
+
+ describe 'システム画像置換に於いて' do
+ before do
+ @sd = {"text" => 'PictureData'}
+ @attr = {"hoge" => {"system_picture_id" => @sd, "other" => 'no replace'}}
+ @sp = FactoryGirl.create :system_picture
+ @imager = ImagerTest.load "abc\ndef\nghi"
+ PettanImager.stub(:load).with(any_args).and_return(@imager)
+ end
+ context '事前チェック' do
+ before do
+ end
+ it 'システム画像インポートを依頼している' do
+ Import.stub(:import_system_picture).with(@sd).and_return(@sp.id)
+ Import.should_receive(:import_system_picture).with(@sd).exactly(1)
r = Import.replace_system_picture(@attr)
- @attr["system_picture_id"].should eq @sp.id
end
- it 'trueを返す' do
- SystemPicture.stub(:store).with(any_args).and_return(@sp)
+ it 'システム画像置換を依頼している' do
+#再帰だからできない?
+# Import.stub(:replace_system_picture).with(@attr['hoge']).and_return({"system_picture_id" => @sp.id})
+# Import.should_receive(:replace_system_picture).with(@attr['hoge']).exactly(1)
+# r = Import.replace_system_picture(@attr)
+ end
+ end
+ context 'つつがなく終わるとき' do
+ before do
+ Import.stub(:import_system_picture).with(@sd).and_return(@sp.id)
+ end
+ it 'カラム値を置換済カラム値として返す' do
r = Import.replace_system_picture(@attr)
- r.should be_true
+ r['hoge']['system_picture_id'].should eq @sp.id
end
end
context '例外ケース' do
- it 'カラム値にsystem_pictureキーがなければnilを返す' do
- @a = {}
- SystemPicture.stub(:store).with(any_args).and_return(@sp)
- r = Import.replace_system_picture(@a)
- r.should be_nil
- end
- it 'カラム値のsystem_pictureキーの値が空ならばnilを返す' do
- @a = {"system_picture" => ''}
- SystemPicture.stub(:store).with(any_args).and_return(@sp)
- r = Import.replace_system_picture(@a)
- r.should be_nil
+ it 'システム画像のインポートが失敗(False)ならFalseを返す' do
+ Import.stub(:import_system_picture).with(any_args).and_return(false)
+ r = Import.replace_system_picture(@attr)
+ r.should be_false
end
- it 'システム画像の保存が失敗(False)ならFalseを返す' do
- SystemPicture.stub(:store).with(any_args).and_return(false)
+ it 'システム画像置換が失敗(False)ならFalseを返す' do
+ Import.stub(:import_system_picture).with(any_args).and_return(@sp.id)
+ Import.stub(:replace_system_picture).with(any_args).and_return(false)
r = Import.replace_system_picture(@attr)
r.should be_false
end
end
it '新規オブジェクトを準備して返す' do
r = Import.modify_object(@n, @a, :b)
- r.should be_a_new Import
+ r.new_record?.should be_true
end
it 'オブジェクトが渡したデータで書き換えられている' do
r = Import.modify_object(@n, @a, :b)
it '該当行を返す' do
r = Import.modify_object(@n, {:a => 3}, :b)
r.is_a?(Import).should be_true
- r.should_not be_a_new Import
+ r.new_record?.should be_false
r[:b].should eq @n
end
it 'オブジェクトが渡したデータで書き換えられている' do