1 # -*- encoding: utf-8 -*-
3 require File.expand_path(File.dirname(__FILE__) + '/import')
\r
8 @t = '{"1": {"a": 1, "b": "Z"}}'
10 @ts = '{"1": {"a": 1, "b": "Z"}, "2": {"a": 2, "b": "Z"}}'
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')
17 describe '繰り返し処理に於いて' do
20 context '単体データを渡されたとき' do
23 Import.each_import @j do |n, i|
30 Import.each_import @j do |n, i|
37 Import.each_import @j do |n, i|
40 r.first["a"].should eq 1
41 r.first["b"].should eq "Z"
44 context '複数を渡されたとき' do
47 Import.each_import @js do |n, i|
55 describe 'テキスト取り込みに於いて' do
56 #成功でTrue、パース失敗でFalse、失敗は保存エラーのモデルを配列で返す
59 context 'つつがなく終わるとき' 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)}
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)}
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) }
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 []
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 []
92 Import.import(@ts) {|name, attr| Import.new(attr)}.should eq []
96 context 'Json解析に失敗したとき' do
98 JSON.stub(:parse_no_except).with(any_args).and_return(false)
101 Import.import(@t){|name, attr| Import.new(attr)}.should be_false
104 context '作成に失敗したとき' do
106 Import.any_instance.stub(:valid?).with(any_args).and_return(false)
109 r = Import.import(@t){|name, attr| Import.new(attr)}
110 r.is_a?(Array).should be_true
113 r = Import.import(@t){|name, attr| Import.new(attr)}
114 r.should have(1).items
117 r = Import.import(@t){|name, attr| Import.new(attr)}
118 r.first.is_a?(Import).should be_true
121 context '複数の作成に失敗したとき' do
122 #三件中、二件の失敗、一件を成功させ、成功データは戻り値に含まないことを確認する
123 it '途中で保存に失敗しても全件更新依頼する' do
124 Import.import(@tes){|name, attr| Import.create(attr)}
127 r = Import.import(@tes){|name, attr| Import.create(attr)}
128 r.is_a?(Array).should be_true
131 r = Import.import(@tes){|name, attr| Import.create(attr)}
132 r.should have(2).items
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
144 describe 'インポートエラーの表示に於いて' do
147 describe 'ファイル取り込みに於いて' do
149 Import.stub(:import).with(any_args).and_return(true)
151 context 'つつがなく終わるとき' do
153 Import.stub(:import).with(any_args).and_return(true)
155 it 'ファイルを開いてテキストを読む' do
156 File.should_receive(:open).with(@f, 'r').exactly(1)
157 Import.import_file(@f)
159 it 'テキスト取り込みを依頼する' do
160 Import.should_receive(:import).with(any_args).exactly(1)
161 Import.import_file(@f)
165 Import.import_file(@f).should be_true
168 context 'ファイルが開けないとき' do
170 File.stub(:open).with(any_args).and_raise('StandardError')
172 it 'ファイルエラーのメッセージを出力する' do
176 Import.import_file(@f).should be_false
179 #失敗したときは、失敗したライセンスが配列で返る
180 context 'テキスト取り込みが失敗したとき' do
182 Import.stub(:import).with(any_args).and_return(false)
184 it '各コモンライセンスのエラーメッセージを出力する' do
188 Import.import_file(@f).should be_false