OSDN Git Service

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