From: yasushiito Date: Thu, 21 Jun 2012 09:18:53 +0000 (+0900) Subject: import:change method name. retake test X-Git-Url: http://git.osdn.net/view?p=pettanr%2Fpettanr.git;a=commitdiff_plain;h=0c2ed3aeef2e4a62a95834763103cd69f50c632e;hp=65674a6fd94127b4a7b4bf69d46a0a01154f14f8 import:change method name. retake test --- diff --git a/vendor/plugins/pettan_importer/lib/pettan_importer.rb b/vendor/plugins/pettan_importer/lib/pettan_importer.rb index 2a3e378d..931ebb36 100644 --- a/vendor/plugins/pettan_importer/lib/pettan_importer.rb +++ b/vendor/plugins/pettan_importer/lib/pettan_importer.rb @@ -16,7 +16,7 @@ module ActiveRecord end end - def import(data, &blk) + def import_text(data, &blk) d = JSON.parse_no_except(data) return false unless d res = [] @@ -30,14 +30,14 @@ module ActiveRecord res end - def import_file(filename) + def import_file(filename, &blk) t = nil begin t = File.open(filename, 'r').read rescue return false end - self.import t + self.import_text t, &blk end end diff --git a/vendor/plugins/pettan_importer/test/import.json b/vendor/plugins/pettan_importer/test/import.json index 4ca50573..bdd519e3 100644 --- a/vendor/plugins/pettan_importer/test/import.json +++ b/vendor/plugins/pettan_importer/test/import.json @@ -1,3 +1,6 @@ { - "a": 1 + "1": { + "a": 1, + "b": "x" + } } diff --git a/vendor/plugins/pettan_importer/test/import_spec.rb b/vendor/plugins/pettan_importer/test/import_spec.rb index b3700754..2513bbb6 100644 --- a/vendor/plugins/pettan_importer/test/import_spec.rb +++ b/vendor/plugins/pettan_importer/test/import_spec.rb @@ -60,19 +60,19 @@ describe Import do it 'Json解析を依頼する' do JSON.should_receive(:parse_no_except).with(any_args).exactly(1) JSON.stub(:parse_no_except).with(any_args).and_return(@j) - Import.import(@t) {|name, attr| Import.new(attr)} + Import.import_text(@t) {|name, attr| Import.new(attr)} end it '更新を一回依頼する' do JSON.stub(:parse_no_except).with(any_args).and_return(@j) Import.any_instance.stub(:valid?).with(any_args).and_return(true) #newでスタブを作るとインスタンス生成ができないので、テスト用スタブで Import.should_receive(:etest).with(any_args).exactly(1) - Import.import(@t) {|name, attr| Import.etest ; Import.new(attr) } + Import.import_text(@t) {|name, attr| Import.etest ; Import.new(attr) } end it '[]を返す' do JSON.stub(:parse_no_except).with(any_args).and_return(@j) Import.any_instance.stub(:valid?).with(any_args).and_return(true) - Import.import(@t) {|name, attr| Import.new(attr)}.should eq [] + Import.import_text(@t) {|name, attr| Import.new(attr)}.should eq [] end end context '複数データがつつがなく終わるとき' do @@ -81,10 +81,10 @@ describe Import do Import.any_instance.stub(:valid?).with(any_args).and_return(true) #newでスタブを作るとインスタンス生成ができないので、テスト用スタブで Import.should_receive(:etest).with(any_args).exactly(2) - Import.import(@ts) {|name, attr| Import.etest ; Import.new(attr)}.should eq [] + Import.import_text(@ts) {|name, attr| Import.etest ; Import.new(attr)}.should eq [] end it '[]を返す' do - Import.import(@ts) {|name, attr| Import.new(attr)}.should eq [] + Import.import_text(@ts) {|name, attr| Import.new(attr)}.should eq [] end end #例外ケース @@ -93,7 +93,7 @@ describe Import do JSON.stub(:parse_no_except).with(any_args).and_return(false) end it 'Falseを返す' do - Import.import(@t){|name, attr| Import.new(attr)}.should be_false + Import.import_text(@t){|name, attr| Import.new(attr)}.should be_false end end context '作成に失敗したとき' do @@ -101,33 +101,33 @@ describe Import do Import.any_instance.stub(:valid?).with(any_args).and_return(false) end it '配列を返す' do - r = Import.import(@t){|name, attr| Import.new(attr)} + r = Import.import_text(@t){|name, attr| Import.new(attr)} r.is_a?(Array).should be_true end it '配列の中身は一件' do - r = Import.import(@t){|name, attr| Import.new(attr)} + r = Import.import_text(@t){|name, attr| Import.new(attr)} r.should have(1).items end it 'オブジェクトが入っている' do - r = Import.import(@t){|name, attr| Import.new(attr)} + r = Import.import_text(@t){|name, attr| Import.new(attr)} r.first.is_a?(Import).should be_true end end context '複数の作成に失敗したとき' do #三件中、二件の失敗、一件を成功させ、成功データは戻り値に含まないことを確認する it '途中で保存に失敗しても全件更新依頼する' do - Import.import(@tes){|name, attr| Import.create(attr)} + Import.import_text(@tes){|name, attr| Import.create(attr)} end it '配列を返す' do - r = Import.import(@tes){|name, attr| Import.create(attr)} + r = Import.import_text(@tes){|name, attr| Import.create(attr)} r.is_a?(Array).should be_true end it '配列の中身は2件' do - r = Import.import(@tes){|name, attr| Import.create(attr)} + r = Import.import_text(@tes){|name, attr| Import.create(attr)} r.should have(2).items end it '配列の中身は失敗したオブジェクトが入っている' do - r = Import.import(@tes){|name, attr| Import.create(attr)} + r = Import.import_text(@tes){|name, attr| Import.create(attr)} r[0].is_a?(Import).should be_true r[0]["a"].should eq 0 r[1].is_a?(Import).should be_true @@ -149,15 +149,21 @@ describe Import do end it 'ファイルを開いてテキストを読む' do File.should_receive(:open).with(@f, 'r').exactly(1) - Import.import_file(@f) + Import.import_file(@f){|name, attr| Import.new(attr) } end it 'テキスト取り込みを依頼する' do - Import.should_receive(:import).with(any_args).exactly(1) - Import.import_file(@f) + Import.should_receive(:import_text).with(any_args).exactly(1) + Import.import_file(@f){|name, attr| Import.new(attr) } + end + it 'ブロックがあるとき更新を一回依頼する' do + Import.any_instance.stub(:valid?).with(any_args).and_return(true) + #newでスタブを作るとインスタンス生成ができないので、テスト用スタブで + Import.should_receive(:etest).with(any_args).exactly(1) + Import.import_file(@f) {|name, attr| Import.etest ; Import.new(attr) } end #テキスト取り込み成功でTrueが返る it 'Trueを返す' do - Import.import_file(@f).should be_true + Import.import_file(@f){|name, attr| Import.new(attr) }.should be_true end end context 'ファイルが開けないとき' do @@ -168,19 +174,19 @@ describe Import do pending end it 'Falseを返す' do - Import.import_file(@f).should be_false + Import.import_file(@f){|name, attr| Import.new(attr) }.should be_false end end #失敗したときは、失敗したライセンスが配列で返る context 'テキスト取り込みが失敗したとき' do before do - Import.stub(:import).with(any_args).and_return(false) + Import.stub(:import_text).with(any_args).and_return(false) end it '各コモンライセンスのエラーメッセージを出力する' do pending end it 'Falseを返す' do - Import.import_file(@f).should be_false + Import.import_file(@f) {|name, attr| Import.new(attr) }.should be_false end end end