From 8b3a05dd57656213d92bf5523f9c4ae863d1e3e6 Mon Sep 17 00:00:00 2001 From: yasushiito Date: Thu, 28 Jun 2012 08:19:14 +0900 Subject: [PATCH] t#28860:add sys pic replace --- .../plugins/pettan_importer/lib/pettan_importer.rb | 10 +++++ vendor/plugins/pettan_importer/test/import_spec.rb | 52 ++++++++++++++++++++++ 2 files changed, 62 insertions(+) diff --git a/vendor/plugins/pettan_importer/lib/pettan_importer.rb b/vendor/plugins/pettan_importer/lib/pettan_importer.rb index 808cf7a3..befe7225 100644 --- a/vendor/plugins/pettan_importer/lib/pettan_importer.rb +++ b/vendor/plugins/pettan_importer/lib/pettan_importer.rb @@ -16,6 +16,16 @@ module ActiveRecord end end + def replace_system_picture attr + d = attr["system_picture"] + return nil if d.blank? + sp = SystemPicture.store Base64.decode64(d.to_s) + return false unless sp + attr["system_picture_id"] = sp.id + attr.delete "system_picture" + true + end + def modify_object(name, attr, key = 'name') c = 'find_by_' + key.to_s r = self.__send__ c, name diff --git a/vendor/plugins/pettan_importer/test/import_spec.rb b/vendor/plugins/pettan_importer/test/import_spec.rb index 1dd7a849..c13f6e44 100644 --- a/vendor/plugins/pettan_importer/test/import_spec.rb +++ b/vendor/plugins/pettan_importer/test/import_spec.rb @@ -14,6 +14,58 @@ describe Import do @f = File.expand_path(File.dirname(__FILE__) + '/import.json') end + describe 'システム画像置換に於いて' do + before do + @attr = {"system_picture" => 'DAt'} + @sp = Factory :system_picture + end + context 'つつがなく終わるとき' do + 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) + 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 + 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 + end + it '値はシステム画像オブジェクトのidとしている' do + SystemPicture.stub(:store).with(any_args).and_return(@sp) + 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) + r = Import.replace_system_picture(@attr) + r.should be_true + 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 + end + it 'システム画像の保存が失敗(False)ならFalseを返す' do + SystemPicture.stub(:store).with(any_args).and_return(false) + r = Import.replace_system_picture(@attr) + r.should be_false + end + end + end describe '対象オブジェクトの更新準備に於いて' do context 'つつがなく終わるとき' do before do -- 2.11.0