OSDN Git Service

fix panel editor
[pettanr/pettanr.git] / vendor / plugins / pettan_importer / test / import_spec.rb
1 # -*- encoding: utf-8 -*-
2 require 'spec_helper'
3 require File.expand_path(File.dirname(__FILE__) + '/import')
4 #インポート処理
5
6 describe Import do
7   before do
8     @t = '{"Z": {"a": 1, "b": "Z"}}'
9     @j = JSON.parse @t
10     @ts = '{"Z": {"a": 1, "b": "Z"}, "X": {"a": 2, "b": "X"}}'
11     @js = JSON.parse @ts
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'
16   end
17   
18   describe 'システム画像インポートに於いて' do
19     before do
20       @attr = {"text" => 'PictureData'}
21       @sp = FactoryGirl.create :system_picture
22       @imager = ImagerTest.load "abc\ndef\nghi"
23     end
24     context '事前チェック' do
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)
29       end
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)
35       end
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)
42       end
43     end
44     context 'つつがなく終わるとき' do
45       before 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)
49       end
50       it 'システム画像オブジェクトのidを返す' do
51         r = Import.import_system_picture(@attr)
52         r.should eq @sp.id
53       end
54     end
55     context '例外ケース' do
56       it 'カラム値に画像データがなかったならFalseを返す' do
57         @attr = {}
58         Base64.stub(:decode64).with('PictureData').and_return('DecodedPictureData')
59         r = Import.import_system_picture(@attr)
60         r.should be_false
61       end
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)
67         r.should be_false
68       end
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)
74         r.should be_false
75       end
76     end
77   end
78   
79   describe 'システム画像置換に於いて' do
80     before 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)
86     end
87     context '事前チェック' do
88       before do
89       end
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)
94       end
95       it 'システム画像置換を依頼している' do
96 #再帰だからできない?
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)
100       end
101     end
102     context 'つつがなく終わるとき' do
103       before do
104         Import.stub(:import_system_picture).with(@sd).and_return(@sp.id)
105       end
106       it 'カラム値を置換済カラム値として返す' do
107         r = Import.replace_system_picture(@attr)
108         r['hoge']['system_picture_id'].should eq @sp.id
109       end
110     end
111     context '例外ケース' do
112       it 'システム画像のインポートが失敗(False)ならFalseを返す' do
113         Import.stub(:import_system_picture).with(any_args).and_return(false)
114         r = Import.replace_system_picture(@attr)
115         r.should be_false
116       end
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)
121         r.should be_false
122       end
123     end
124   end
125   describe '対象オブジェクトの更新準備に於いて' do
126     context 'つつがなく終わるとき' do
127       before do
128         @n = @j.keys.first
129         @a = @j.values.first
130       end
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)
135       end
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)
140       end
141       it 'カラム書き換えを依頼している' do
142         Import.create! @a
143         Import.any_instance.should_receive(:attributes=).with(any_args).exactly(1)
144         r = Import.modify_object(@n, @a, :b)
145       end
146       it 'オブジェクトを返す' do
147         Import.create! @a
148         r = Import.modify_object(@n, @a, :b)
149         r.is_a?(Import).should be_true
150       end
151     end
152     context 'キー値が一致する行がないとき' do
153       before do
154         @n = @j.keys.first
155         @a = @j.values.first
156       end
157       it '新規オブジェクトを準備して返す' do
158         r = Import.modify_object(@n, @a, :b)
159         r.new_record?.should be_true
160       end
161       it 'オブジェクトが渡したデータで書き換えられている' do
162         r = Import.modify_object(@n, @a, :b)
163         r["a"].should eq 1
164         r["b"].should eq "Z"
165       end
166     end
167     context 'キー値が一致する行があるとき' do
168       before do
169         @n = @j.keys.first
170         @a = @j.values.first
171         Import.create!(@a)
172       end
173       it '該当行を返す' 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
177         r[:b].should eq @n
178       end
179       it 'オブジェクトが渡したデータで書き換えられている' do
180         r = Import.modify_object(@n, {:a => 3}, :b)
181         r[:a].should eq 3
182       end
183     end
184   end
185   
186   describe '繰り返し処理に於いて' do
187     before do
188     end
189     context '単体データを渡されたとき' do
190       it '一回処理' do
191         r = []
192         Import.each_import @j do |n, i|
193           r << i
194         end
195         r.size.should eq 1
196       end
197       it '一回処理' do
198         r = []
199         Import.each_import @j do |n, i|
200           r << n
201         end
202         r.first.should eq "Z"
203       end
204       it '一回処理' do
205         r = []
206         Import.each_import @j do |n, i|
207           r << i
208         end
209         r.first["a"].should eq 1
210         r.first["b"].should eq "Z"
211       end
212     end
213     context '複数を渡されたとき' do
214       it '二回処理' do
215         r = []
216         Import.each_import @js do |n, i|
217           r << i
218         end
219         r.size.should eq 2
220       end
221     end
222   end
223   
224   describe 'リストからのインポートに於いて' do
225     #インポート失敗データ(オブジェクト)のすべてを配列で返す
226     #成功したときは空の配列を返す 
227     before do
228     end
229     context '事前チェック' do
230       it '更新を一回依頼する' do
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) }
235       end
236     end
237     context '複数データがつつがなく終わるとき' do
238       it '更新を二回依頼する' 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)}
243       end
244     end
245     context 'つつがなく終わるとき' do
246       it '[]を返す' do
247         r = Import.import_list(@js) {|name, attr| Import.new(attr)}
248         r.should eq []
249       end
250     end
251     #注意ケース
252     context '作成に失敗したとき' do
253       before do
254         Import.any_instance.stub(:valid?).with(any_args).and_return(false)
255       end
256       it '配列を返す' do
257         r = Import.import_list(@j){|name, attr| Import.new(attr)}
258         r.is_a?(Array).should be_true
259       end
260       it '配列の中身は一件' do
261         r = Import.import_list(@j){|name, attr| Import.new(attr)}
262         r.should have(1).items
263       end
264       it 'オブジェクトが入っている' do
265         r = Import.import_list(@j){|name, attr| Import.new(attr)}
266         r.first.is_a?(Import).should be_true
267       end
268     end
269     context '複数の作成に失敗したとき' do
270       #三件中、二件の失敗、一件を成功させ、成功データは戻り値に含まないことを確認する
271       it '行の数に変化がない' do
272         lambda {
273           Import.import_list(@jes){|name, attr| Import.create(attr)}
274         }.should_not change Import, :count
275       end
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)}
280       end
281       it '配列を返す' do
282         r = Import.import_list(@jes){|name, attr| Import.create(attr)}
283         r.is_a?(Array).should be_true
284       end
285       it '配列の中身は2件' do
286         r = Import.import_list(@jes){|name, attr| Import.create(attr)}
287         r.should have(2).items
288       end
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
295       end
296     end
297   end
298   
299   describe 'テキスト取り込みに於いて' do
300     #インポート失敗データ(オブジェクト)のすべてを配列で返す
301     #成功したときは空の配列を返す 
302     before do
303     end
304     context '事前チェック' do
305       it 'Json解析を依頼する' do
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)}
309       end
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) }
315       end
316     end
317     context 'つつがなく終わるとき' do
318       it '[]を返す' do
319         Import.import_text(@t) {|name, attr| Import.new(attr)}.should eq []
320       end
321     end
322     #例外ケース
323     context 'Json解析に失敗したとき' do
324       before do
325       end
326       it '処理を中断する' do
327         lambda {
328           Import.import_text(''){|name, attr| Import.new(attr)}
329         }.should raise_error
330       end
331     end
332   end
333   
334   describe 'ファイル取り込みに於いて' do
335     before do
336     end
337     context '事前チェック' do
338       before do
339       end
340       it 'テキスト取り込みを依頼する' do
341         Import.should_receive(:import_text).with(any_args).exactly(1)
342         Import.import_file(@f){|name, attr| Import.new(attr) }
343       end
344     end
345     context 'つつがなく終わるとき' do
346       it 'インポート結果を返す' 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
350         r.should be_empty
351       end
352     end
353     context 'ファイルが開けないとき' do
354       before do
355         File.stub(:open).with(any_args).and_raise('StandardError')
356       end
357       it '処理を中断する' do
358         lambda {
359           Import.import_file(@f){|name, attr| Import.new(attr)}
360         }.should raise_error
361       end
362     end
363   end
364   
365   describe 'Urlからのインポートに於いて' do
366     before do
367     end
368     context '事前チェック' do
369       before do
370       end
371       it 'テキスト取り込みを依頼する' do
372         Import.should_receive(:import_text).with(any_args).exactly(1)
373         Import.import_url(@u){|name, attr| Import.new(attr) }
374       end
375     end
376     context 'つつがなく終わるとき' do
377       it 'インポート結果を返す' 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
381         r.should be_empty
382       end
383     end
384     context 'ファイルが開けないとき' do
385       it '処理を中断する' do
386         lambda {
387           Import.import_url('http://localhost:3000/noroute'){|name, attr| Import.new(attr)}
388         }.should raise_error
389       end
390     end
391   end
392   
393   describe '文献からのインポートに於いて' do
394     before do
395       @urls = ['urla', 'urlb']
396     end
397     context '事前チェック' do
398       before do
399       end
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) }
404       end
405     end
406     #戻り
407     #各urlのインポート結果
408     #  Hash:キー値はurl、値はHash
409     #    キー値がexceptionのとき、値はHashで、location例外発生場所とmessage例外発生理由
410     #    キー値がvalidationsのとき、値は配列で、インポート失敗データ(オブジェクト)のすべて 
411     context 'つつがなく終わるとき' do
412       it 'インポート結果を返す' 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
425       end
426     end
427     context 'Urlからのインポートで何らかの例外が発生したとき' do
428       before do
429         Import.stub(:import_url).with(any_args).and_raise('StandardError')
430       end
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
439       end
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
448       end
449     end
450   end
451 end
452