1 # -*- encoding: utf-8 -*-
3 require File.expand_path(File.dirname(__FILE__) + '/import')
8 @t = '{"Z": {"a": 1, "b": "Z"}}'
10 @ts = '{"Z": {"a": 1, "b": "Z"}, "X": {"a": 2, "b": "X"}}'
12 @tes = '{"1": {"a": 0}, "2": {"a": 0}, "Z": {"a": 2, "b": "Z"}}'
13 @jes = JSON.parse @tes
14 @f = File.expand_path(File.dirname(__FILE__) + '/import.json')
15 @u = 'http://localhost:3000/profile.json'
18 describe 'システム画像インポートに於いて' do
20 @attr = {"text" => 'PictureData'}
21 @sp = FactoryGirl.create :system_picture
22 @imager = ImagerTest.load "abc\ndef\nghi"
25 it 'カラム値の"text"キーの値をBase64展開している' do
26 Base64.stub(:decode64).with('PictureData').and_return('DecodedPictureData')
27 Base64.should_receive(:decode64).with('PictureData').exactly(1)
28 r = Import.import_system_picture(@attr)
30 it '画像データで画像ライブラリをロードを依頼している' do
31 Base64.stub(:decode64).with('PictureData').and_return('DecodedPictureData')
32 PettanImager.stub(:load).with('DecodedPictureData').and_return(@imager)
33 PettanImager.should_receive(:load).with('DecodedPictureData').exactly(1)
34 r = Import.import_system_picture(@attr)
36 it 'システム画像に保存を依頼している' do
37 Base64.stub(:decode64).with('PictureData').and_return('DecodedPictureData')
38 PettanImager.stub(:load).with('DecodedPictureData').and_return(@imager)
39 SystemPicture.stub(:store).with(@imager).and_return(@sp)
40 SystemPicture.should_receive(:store).with(@imager).exactly(1)
41 r = Import.import_system_picture(@attr)
44 context 'つつがなく終わるとき' do
46 Base64.stub(:decode64).with('PictureData').and_return('DecodedPictureData')
47 PettanImager.stub(:load).with('DecodedPictureData').and_return(@imager)
48 SystemPicture.stub(:store).with(@imager).and_return(@sp)
50 it 'システム画像オブジェクトのidを返す' do
51 r = Import.import_system_picture(@attr)
56 it 'カラム値に画像データがなかったならFalseを返す' do
58 Base64.stub(:decode64).with('PictureData').and_return('DecodedPictureData')
59 r = Import.import_system_picture(@attr)
62 it '画像ライブラリのロードが失敗ならFalseを返す' do
63 @attr = {"system_picture" => ''}
64 Base64.stub(:decode64).with('PictureData').and_return('DecodedPictureData')
65 PettanImager.stub(:load).with('DecodedPictureData').and_return(nil)
66 r = Import.import_system_picture(@attr)
69 it 'システム画像の保存が失敗(False)ならFalseを返す' do
70 Base64.stub(:decode64).with('PictureData').and_return('DecodedPictureData')
71 PettanImager.stub(:load).with('DecodedPictureData').and_return(@imager)
72 SystemPicture.stub(:store).with(@imager).and_return(false)
73 r = Import.import_system_picture(@attr)
79 describe 'システム画像置換に於いて' do
81 @sd = {"text" => 'PictureData'}
82 @attr = {"hoge" => {"system_picture_id" => @sd, "other" => 'no replace'}}
83 @sp = FactoryGirl.create :system_picture
84 @imager = ImagerTest.load "abc\ndef\nghi"
85 PettanImager.stub(:load).with(any_args).and_return(@imager)
90 it 'システム画像インポートを依頼している' do
91 Import.stub(:import_system_picture).with(@sd).and_return(@sp.id)
92 Import.should_receive(:import_system_picture).with(@sd).exactly(1)
93 r = Import.replace_system_picture(@attr)
95 it 'システム画像置換を依頼している' do
97 # Import.stub(:replace_system_picture).with(@attr['hoge']).and_return({"system_picture_id" => @sp.id})
98 # Import.should_receive(:replace_system_picture).with(@attr['hoge']).exactly(1)
99 # r = Import.replace_system_picture(@attr)
102 context 'つつがなく終わるとき' do
104 Import.stub(:import_system_picture).with(@sd).and_return(@sp.id)
106 it 'カラム値を置換済カラム値として返す' do
107 r = Import.replace_system_picture(@attr)
108 r['hoge']['system_picture_id'].should eq @sp.id
112 it 'システム画像のインポートが失敗(False)ならFalseを返す' do
113 Import.stub(:import_system_picture).with(any_args).and_return(false)
114 r = Import.replace_system_picture(@attr)
117 it 'システム画像置換が失敗(False)ならFalseを返す' do
118 Import.stub(:import_system_picture).with(any_args).and_return(@sp.id)
119 Import.stub(:replace_system_picture).with(any_args).and_return(false)
120 r = Import.replace_system_picture(@attr)
125 describe '対象オブジェクトの更新準備に於いて' do
126 context 'つつがなく終わるとき' do
131 it 'キーカラムをnameで補充している' do
132 Import.stub(:find_by_name).with(any_args).and_return(nil)
133 Import.should_receive(:__send__).with('find_by_name', @n).exactly(1)
134 r = Import.modify_object(@n, @a)
136 it 'オブジェクト取得を依頼している' do
137 Import.stub(:find_by_b).with(any_args).and_return(nil)
138 Import.should_receive(:__send__).with('find_by_b', @n).exactly(1)
139 r = Import.modify_object(@n, @a, :b)
141 it 'カラム書き換えを依頼している' do
143 Import.any_instance.should_receive(:attributes=).with(any_args).exactly(1)
144 r = Import.modify_object(@n, @a, :b)
148 r = Import.modify_object(@n, @a, :b)
149 r.is_a?(Import).should be_true
152 context 'キー値が一致する行がないとき' do
157 it '新規オブジェクトを準備して返す' do
158 r = Import.modify_object(@n, @a, :b)
159 r.new_record?.should be_true
161 it 'オブジェクトが渡したデータで書き換えられている' do
162 r = Import.modify_object(@n, @a, :b)
167 context 'キー値が一致する行があるとき' do
174 r = Import.modify_object(@n, {:a => 3}, :b)
175 r.is_a?(Import).should be_true
176 r.new_record?.should be_false
179 it 'オブジェクトが渡したデータで書き換えられている' do
180 r = Import.modify_object(@n, {:a => 3}, :b)
186 describe '繰り返し処理に於いて' do
189 context '単体データを渡されたとき' do
192 Import.each_import @j do |n, i|
199 Import.each_import @j do |n, i|
202 r.first.should eq "Z"
206 Import.each_import @j do |n, i|
209 r.first["a"].should eq 1
210 r.first["b"].should eq "Z"
213 context '複数を渡されたとき' do
216 Import.each_import @js do |n, i|
224 describe 'リストからのインポートに於いて' do
225 #インポート失敗データ(オブジェクト)のすべてを配列で返す
231 Import.any_instance.stub(:valid?).with(any_args).and_return(true)
232 #newでスタブを作るとインスタンス生成ができないので、テスト用スタブで
233 Import.should_receive(:etest).with(any_args).exactly(1)
234 Import.import_list(@j) {|name, attr| Import.etest ; Import.new(attr) }
237 context '複数データがつつがなく終わるとき' do
239 Import.any_instance.stub(:valid?).with(any_args).and_return(true)
240 #newでスタブを作るとインスタンス生成ができないので、テスト用スタブで
241 Import.should_receive(:etest).with(any_args).exactly(2)
242 Import.import_list(@js) {|name, attr| Import.etest ; Import.new(attr)}
245 context 'つつがなく終わるとき' do
247 r = Import.import_list(@js) {|name, attr| Import.new(attr)}
252 context '作成に失敗したとき' do
254 Import.any_instance.stub(:valid?).with(any_args).and_return(false)
257 r = Import.import_list(@j){|name, attr| Import.new(attr)}
258 r.is_a?(Array).should be_true
261 r = Import.import_list(@j){|name, attr| Import.new(attr)}
262 r.should have(1).items
265 r = Import.import_list(@j){|name, attr| Import.new(attr)}
266 r.first.is_a?(Import).should be_true
269 context '複数の作成に失敗したとき' do
270 #三件中、二件の失敗、一件を成功させ、成功データは戻り値に含まないことを確認する
273 Import.import_list(@jes){|name, attr| Import.create(attr)}
274 }.should_not change Import, :count
276 it '途中で保存に失敗しても全件更新依頼する' do
277 #newでスタブを作るとインスタンス生成ができないので、テスト用スタブで
278 Import.should_receive(:etest).with(any_args).exactly(3)
279 Import.import_list(@jes){|name, attr| Import.etest ; Import.create(attr)}
282 r = Import.import_list(@jes){|name, attr| Import.create(attr)}
283 r.is_a?(Array).should be_true
286 r = Import.import_list(@jes){|name, attr| Import.create(attr)}
287 r.should have(2).items
289 it '配列の中身は失敗したオブジェクトが入っている' do
290 r = Import.import_list(@jes){|name, attr| Import.create(attr)}
291 r[0].is_a?(Import).should be_true
292 r[0]["a"].should eq 0
293 r[1].is_a?(Import).should be_true
294 r[1]["a"].should eq 0
299 describe 'テキスト取り込みに於いて' do
300 #インポート失敗データ(オブジェクト)のすべてを配列で返す
306 JSON.stub(:parse).with(@t).and_return(@j)
307 JSON.should_receive(:parse).with(@t).exactly(1)
308 Import.import_text(@t) {|name, attr| Import.new(attr)}
310 it 'リストからのインポートを依頼する' do
311 JSON.stub(:parse).with(@t).and_return(@j)
312 Import.any_instance.stub(:import_list).with(@j).and_return([])
313 Import.should_receive(:import_list).with(@j).exactly(1)
314 Import.import_text(@t) {|name, attr| Import.new(attr) }
317 context 'つつがなく終わるとき' do
319 Import.import_text(@t) {|name, attr| Import.new(attr)}.should eq []
323 context 'Json解析に失敗したとき' do
328 Import.import_text(''){|name, attr| Import.new(attr)}
334 describe 'ファイル取り込みに於いて' do
340 it 'テキスト取り込みを依頼する' do
341 Import.should_receive(:import_text).with(any_args).exactly(1)
342 Import.import_file(@f){|name, attr| Import.new(attr) }
345 context 'つつがなく終わるとき' do
347 Import.stub(:import_text).with(any_args).and_return([])
348 r = Import.import_file(@f){|name, attr| Import.new(attr) }
349 r.is_a?(Array).should be_true
353 context 'ファイルが開けないとき' do
355 File.stub(:open).with(any_args).and_raise('StandardError')
359 Import.import_file(@f){|name, attr| Import.new(attr)}
365 describe 'Urlからのインポートに於いて' do
371 it 'テキスト取り込みを依頼する' do
372 Import.should_receive(:import_text).with(any_args).exactly(1)
373 Import.import_url(@u){|name, attr| Import.new(attr) }
376 context 'つつがなく終わるとき' do
378 Import.stub(:import_text).with(any_args).and_return([])
379 r = Import.import_url(@u){|name, attr| Import.new(attr) }
380 r.is_a?(Array).should be_true
384 context 'ファイルが開けないとき' do
387 Import.import_url('http://localhost:3000/noroute'){|name, attr| Import.new(attr)}
393 describe '文献からのインポートに於いて' do
395 @urls = ['urla', 'urlb']
400 it 'Urlからのインポートを依頼する' do
401 Import.stub(:import_url).with(any_args).and_return([])
402 Import.should_receive(:import_url).with(any_args).exactly(2)
403 Import.import_urls(@urls){|name, attr| Import.new(attr) }
408 # Hash:キー値はurl、値はHash
409 # キー値がexceptionのとき、値はHashで、location例外発生場所とmessage例外発生理由
410 # キー値がvalidationsのとき、値は配列で、インポート失敗データ(オブジェクト)のすべて
411 context 'つつがなく終わるとき' do
413 Import.stub(:import_url).with(any_args).and_return([])
414 r = Import.import_urls(@urls){|name, attr| Import.new(attr) }
415 r.is_a?(Hash).should be_true
416 r.should have(2).items
417 r['urla'].is_a?(Hash).should be_true
418 r['urla'].should have(1).items
419 r['urla'][:validations].is_a?(Array).should be_true
420 r['urla'][:validations].should be_empty
421 r['urlb'].is_a?(Hash).should be_true
422 r['urlb'].should have(1).items
423 r['urlb'][:validations].is_a?(Array).should be_true
424 r['urlb'][:validations].should be_empty
427 context 'Urlからのインポートで何らかの例外が発生したとき' do
429 Import.stub(:import_url).with(any_args).and_raise('StandardError')
431 it 'インポート結果Hashにexceptionをキーを追加する' do
432 r = Import.import_urls(@urls){|name, attr| Import.new(attr) }
433 r.is_a?(Hash).should be_true
434 r.should have(2).items
435 r['urla'].is_a?(Hash).should be_true
436 r['urla'].should have(1).items
437 r['urlb'].is_a?(Hash).should be_true
438 r['urlb'].should have(1).items
440 it '例外発生場所と例外発生理由を追加する' do
441 r = Import.import_urls(@urls){|name, attr| Import.new(attr) }
442 r['urla'][:exception].is_a?(Hash).should be_true
443 r['urla'][:exception][:location].should_not be_nil
444 r['urla'][:exception][:message].should_not be_nil
445 r['urlb'][:exception].is_a?(Hash).should be_true
446 r['urlb'][:exception][:location].should_not be_nil
447 r['urlb'][:exception][:message].should_not be_nil