OSDN Git Service

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