OSDN Git Service

add importer
[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')\r
4 #インポート処理
5
6 describe Import do
7   before do
8     @t = '{"a": 1, "b": "Z"}'
9     @j = JSON.parse @t
10     @ts = '[{"a": 1, "b": "Z"}, {"a": 2, "b": "Z"}]'
11     @js = JSON.parse @ts
12     @tes = '[{"a": 0}, {"a": 0}, {"a": 2, "b": "Z"}]'
13     @jes = JSON.parse @tes
14     @f = File.expand_path(File.dirname(__FILE__) + '/import.json')
15   end
16
17   describe '繰り返し処理に於いて' do
18     before do
19     end
20     context '単体データを渡されたとき' do
21       it '一回処理' do
22         r = []
23         Import.each_import @j do |i|
24           r << i
25         end
26         r.size.should eq 1
27       end
28     end
29     context '複数を渡されたとき' do
30       it '二回処理' do
31         r = []
32         Import.each_import @js do |i|
33           r << i
34         end
35         r.size.should eq 2
36       end
37     end
38   end
39   
40   describe 'テキスト取り込みに於いて' do
41     #成功でTrue、パース失敗でFalse、失敗は保存エラーのモデルを配列で返す
42     before do
43     end
44     context 'つつがなく終わるとき' do
45       it 'Json解析を依頼する' do
46         JSON.should_receive(:parse_no_except).with(any_args).exactly(1)
47         JSON.stub(:parse_no_except).with(any_args).and_return(@j)
48         Import.import(@t) {|attr| Import.new(attr)}
49       end
50       it '繰り返し処理を依頼する' do
51         JSON.stub(:parse_no_except).with(any_args).and_return(@j)
52         Import.should_receive(:each_import).with(any_args).exactly(1)
53         Import.import(@t) {|attr| Import.new(attr)}
54       end
55       it '更新を一回依頼する' do
56         JSON.stub(:parse_no_except).with(any_args).and_return(@j)
57         Import.any_instance.stub(:valid?).with(any_args).and_return(true)
58         #newでスタブを作るとインスタンス生成ができないので、テスト用スタブで
59         Import.should_receive(:etest).with(any_args).exactly(1)
60         Import.import(@t) {|attr| Import.etest ; Import.new(attr) }
61       end
62       it '[]を返す' do
63         JSON.stub(:parse_no_except).with(any_args).and_return(@j)
64         Import.any_instance.stub(:valid?).with(any_args).and_return(true)
65         Import.import(@t) {|attr| Import.new(attr)}.should eq []
66       end
67     end
68     context '複数データがつつがなく終わるとき' do
69       it 'ライセンス更新を二回依頼する' do
70         JSON.stub(:parse_no_except).with(any_args).and_return(@js)
71         Import.any_instance.stub(:valid?).with(any_args).and_return(true)
72         #newでスタブを作るとインスタンス生成ができないので、テスト用スタブで
73         Import.should_receive(:etest).with(any_args).exactly(2)
74         Import.import(@ts) {|attr| Import.etest ; Import.new(attr)}.should eq []
75       end
76       it '[]を返す' do
77         Import.import(@ts) {|attr| Import.new(attr)}.should eq []
78       end
79     end
80     #例外ケース
81     context 'Json解析に失敗したとき' do
82       before do
83         JSON.stub(:parse_no_except).with(any_args).and_return(false)
84       end
85       it 'Falseを返す' do
86         Import.import(@t){|attr| Import.new(attr)}.should be_false
87       end
88     end
89     context '作成に失敗したとき' do
90       before do
91         Import.any_instance.stub(:valid?).with(any_args).and_return(false)
92       end
93       it '配列を返す' do
94         r = Import.import(@t){|attr| Import.new(attr)}
95         r.is_a?(Array).should be_true
96       end
97       it '配列の中身は一件' do
98         r = Import.import(@t){|attr| Import.new(attr)}
99         r.should have(1).items
100       end
101       it 'オブジェクトが入っている' do
102         r = Import.import(@t){|attr| Import.new(attr)}
103         r.first.is_a?(Import).should be_true
104       end
105     end
106     context '複数の作成に失敗したとき' do
107       #三件中、二件の失敗、一件を成功させ、成功データは戻り値に含まないことを確認する
108       it '途中で保存に失敗しても全件更新依頼する' do
109         Import.import(@tes){|attr| Import.create(attr)}
110       end
111       it '配列を返す' do
112         r = Import.import(@tes){|attr| Import.create(attr)}
113         r.is_a?(Array).should be_true
114       end
115       it '配列の中身は2件' do
116         r = Import.import(@tes){|attr| Import.create(attr)}
117         r.should have(2).items
118       end
119       it '配列の中身は失敗したオブジェクトが入っている' do
120         r = Import.import(@tes){|attr| Import.create(attr)}
121         r[0].is_a?(Import).should be_true
122         r[0]["a"].should eq 0
123         r[1].is_a?(Import).should be_true
124         r[1]["a"].should eq 0
125       end
126     end
127   end
128   
129   describe 'インポートエラーの表示に於いて' do
130   end
131   
132   describe 'ファイル取り込みに於いて' do
133     before do
134       Import.stub(:import).with(any_args).and_return(true)
135     end
136     context 'つつがなく終わるとき' do
137       before do
138         Import.stub(:import).with(any_args).and_return(true)
139       end
140       it 'ファイルを開いてテキストを読む' do
141         File.should_receive(:open).with(@f, 'r').exactly(1)
142         Import.import_file(@f)
143       end
144       it 'テキスト取り込みを依頼する' do
145         Import.should_receive(:import).with(any_args).exactly(1)
146         Import.import_file(@f)
147       end
148       #テキスト取り込み成功でTrueが返る
149       it 'Trueを返す' do
150         Import.import_file(@f).should be_true
151       end
152     end
153     context 'ファイルが開けないとき' do
154       before do
155         File.stub(:open).with(any_args).and_raise('StandardError')
156       end
157       it 'ファイルエラーのメッセージを出力する' do
158         pending
159       end
160       it 'Falseを返す' do
161         Import.import_file(@f).should be_false
162       end
163     end
164     #失敗したときは、失敗したライセンスが配列で返る
165     context 'テキスト取り込みが失敗したとき' do
166       before do
167         Import.stub(:import).with(any_args).and_return(false)
168       end
169       it '各コモンライセンスのエラーメッセージを出力する' do
170         pending
171       end
172       it 'Falseを返す' do
173         Import.import_file(@f).should be_false
174       end
175     end
176   end
177   
178 end
179