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.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(@t) {|name, attr| Import.etest ; Import.new(attr) }
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(@t) {|name, attr| Import.new(attr)}.should eq []
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(@ts) {|name, attr| Import.etest ; Import.new(attr)}.should eq []
87 Import.import(@ts) {|name, attr| Import.new(attr)}.should eq []
91 context 'Json解析に失敗したとき' do
93 JSON.stub(:parse_no_except).with(any_args).and_return(false)
96 Import.import(@t){|name, attr| Import.new(attr)}.should be_false
99 context '作成に失敗したとき' do
101 Import.any_instance.stub(:valid?).with(any_args).and_return(false)
104 r = Import.import(@t){|name, attr| Import.new(attr)}
105 r.is_a?(Array).should be_true
108 r = Import.import(@t){|name, attr| Import.new(attr)}
109 r.should have(1).items
112 r = Import.import(@t){|name, attr| Import.new(attr)}
113 r.first.is_a?(Import).should be_true
116 context '複数の作成に失敗したとき' do
117 #三件中、二件の失敗、一件を成功させ、成功データは戻り値に含まないことを確認する
118 it '途中で保存に失敗しても全件更新依頼する' do
119 Import.import(@tes){|name, attr| Import.create(attr)}
122 r = Import.import(@tes){|name, attr| Import.create(attr)}
123 r.is_a?(Array).should be_true
126 r = Import.import(@tes){|name, attr| Import.create(attr)}
127 r.should have(2).items
129 it '配列の中身は失敗したオブジェクトが入っている' do
130 r = Import.import(@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
139 describe 'インポートエラーの表示に於いて' do
142 describe 'ファイル取り込みに於いて' do
144 Import.stub(:import).with(any_args).and_return(true)
146 context 'つつがなく終わるとき' do
148 Import.stub(:import).with(any_args).and_return(true)
150 it 'ファイルを開いてテキストを読む' do
151 File.should_receive(:open).with(@f, 'r').exactly(1)
152 Import.import_file(@f)
154 it 'テキスト取り込みを依頼する' do
155 Import.should_receive(:import).with(any_args).exactly(1)
156 Import.import_file(@f)
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) }
166 Import.import_file(@f).should be_true
169 context 'ファイルが開けないとき' do
171 File.stub(:open).with(any_args).and_raise('StandardError')
173 it 'ファイルエラーのメッセージを出力する' do
177 Import.import_file(@f).should be_false
180 #失敗したときは、失敗したライセンスが配列で返る
181 context 'テキスト取り込みが失敗したとき' do
183 Import.stub(:import).with(any_args).and_return(false)
185 it '各コモンライセンスのエラーメッセージを出力する' do
189 Import.import_file(@f).should be_false