From: yasushiito Date: Mon, 25 Jun 2012 09:34:17 +0000 (+0900) Subject: tid=28816 X-Git-Url: http://git.osdn.net/view?a=commitdiff_plain;h=c6131e584c4b9620ed55abdc110a19bebaaf2cbb;p=pettanr%2Fpettanr.git tid=28816 --- diff --git a/vendor/plugins/pettan_importer/lib/pettan_importer.rb b/vendor/plugins/pettan_importer/lib/pettan_importer.rb index 931ebb36..808cf7a3 100644 --- a/vendor/plugins/pettan_importer/lib/pettan_importer.rb +++ b/vendor/plugins/pettan_importer/lib/pettan_importer.rb @@ -16,6 +16,18 @@ module ActiveRecord end end + def modify_object(name, attr, key = 'name') + c = 'find_by_' + key.to_s + r = self.__send__ c, name + if r + r.attributes = attr + else + r = self.new attr + r[key] = name if r[key].blank? + end + r + end + def import_text(data, &blk) d = JSON.parse_no_except(data) return false unless d diff --git a/vendor/plugins/pettan_importer/test/import_spec.rb b/vendor/plugins/pettan_importer/test/import_spec.rb index 62a4aae9..1dd7a849 100644 --- a/vendor/plugins/pettan_importer/test/import_spec.rb +++ b/vendor/plugins/pettan_importer/test/import_spec.rb @@ -5,15 +5,76 @@ require File.expand_path(File.dirname(__FILE__) + '/import') describe Import do before do - @t = '{"1": {"a": 1, "b": "Z"}}' + @t = '{"Z": {"a": 1, "b": "Z"}}' @j = JSON.parse @t - @ts = '{"1": {"a": 1, "b": "Z"}, "2": {"a": 2, "b": "Z"}}' + @ts = '{"Z": {"a": 1, "b": "Z"}, "X": {"a": 2, "b": "X"}}' @js = JSON.parse @ts - @tes = '{"1": {"a": 0}, "2": {"a": 0}, "3": {"a": 2, "b": "Z"}}' + @tes = '{"1": {"a": 0}, "2": {"a": 0}, "Z": {"a": 2, "b": "Z"}}' @jes = JSON.parse @tes @f = File.expand_path(File.dirname(__FILE__) + '/import.json') end + describe '対象オブジェクトの更新準備に於いて' do + context 'つつがなく終わるとき' do + before do + @n = @j.keys.first + @a = @j.values.first + end + it 'キーカラムをnameで補充している' do + Import.stub(:find_by_name).with(any_args).and_return(nil) + Import.should_receive(:__send__).with('find_by_name', @n).exactly(1) + r = Import.modify_object(@n, @a) + end + it 'オブジェクト取得を依頼している' do + Import.stub(:find_by_b).with(any_args).and_return(nil) + Import.should_receive(:__send__).with('find_by_b', @n).exactly(1) + r = Import.modify_object(@n, @a, :b) + end + it 'カラム書き換えを依頼している' do + Import.create! @a + Import.any_instance.should_receive(:attributes=).with(any_args).exactly(1) + r = Import.modify_object(@n, @a, :b) + end + it 'オブジェクトを返す' do + Import.create! @a + r = Import.modify_object(@n, @a, :b) + r.is_a?(Import).should be_true + end + end + context 'キー値が一致する行がないとき' do + before do + @n = @j.keys.first + @a = @j.values.first + end + it '新規オブジェクトを準備して返す' do + r = Import.modify_object(@n, @a, :b) + r.should be_a_new Import + end + it 'オブジェクトが渡したデータで書き換えられている' do + r = Import.modify_object(@n, @a, :b) + r["a"].should eq 1 + r["b"].should eq "Z" + end + end + context 'キー値が一致する行があるとき' do + before do + @n = @j.keys.first + @a = @j.values.first + Import.create!(@a) + end + 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[:b].should eq @n + end + it 'オブジェクトが渡したデータで書き換えられている' do + r = Import.modify_object(@n, {:a => 3}, :b) + r[:a].should eq 3 + end + end + end + describe '繰り返し処理に於いて' do before do end @@ -30,7 +91,7 @@ describe Import do Import.each_import @j do |n, i| r << n end - r.first.should eq "1" + r.first.should eq "Z" end it '一回処理' do r = []