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
Import.each_import @j do |n, i|
r << n
end
- r.first.should eq "1"
+ r.first.should eq "Z"
end
it '一回処理' do
r = []