1 # -*- encoding: utf-8 -*-
5 describe LicenseGroup do
8 @f = Rails.root + 'spec/json/license_group.json'
9 @t = File.open(@f, 'r').read
11 @fs = Rails.root + 'spec/json/license_groups.json'
12 @fes = Rails.root + 'spec/json/invalid_license_groups.json'
16 @lg = FactoryGirl.build :license_group
19 context 'オーソドックスなデータのとき' do
24 @lg.url = 'http://test.jp/'
29 @lg.classname = 'a'*50
31 @lg.url = 'http://test.jp/aaaaa' + 'a' * 180
36 context 'nameを検証するとき' do
39 @lg.should_not be_valid
43 @lg.should_not be_valid
46 l = FactoryGirl.create :license_group
47 @lg.should_not be_valid
50 context 'classnameを検証するとき' do
53 @lg.should_not be_valid
56 @lg.classname = 'a'*51
57 @lg.should_not be_valid
60 context 'captionを検証するとき' do
63 @lg.should_not be_valid
67 @lg.should_not be_valid
70 context 'urlを検証するとき' do
73 @lg.should_not be_valid
77 @lg.should_not be_valid
79 it 'url形式でないなら失敗する' do
81 @lg.should_not be_valid
86 describe 'デフォルト値補充に於いて' do
88 @lg = FactoryGirl.build :license_group, :name => "1"
93 describe '上書き補充に於いて' do
95 @lg = FactoryGirl.build :license_group, :name => "1"
100 describe '閲覧許可に於いて' do
101 #ライセンスグループは作家作成する前から存在するので、閲覧制限の意味がない
104 describe '一覧取得に於いて' do
106 @lg = FactoryGirl.create :license_group, :name => "1"
108 context 'つつがなく終わるとき' do
\r
109 it '一覧取得オプションを利用している' do
\r
110 LicenseGroup.stub(:list_opt).with(any_args).and_return({})
\r
111 LicenseGroup.should_receive(:list_opt).with(any_args).exactly(1)
\r
112 r = LicenseGroup.list
116 l = LicenseGroup.list
120 @lg2 = FactoryGirl.create :license_group, :name => "5", :url => 'http://test.ptn/10'
121 l = LicenseGroup.list
122 l.should eq [@lg, @lg2]
125 describe 'list関連テーブルプションに於いて' do
126 it 'includeキーを含んでいる' do
127 r = LicenseGroup.list_opt
128 r.has_key?(:include).should be_true
131 r = LicenseGroup.list_opt[:include]
132 r.should have(1).items
135 r = LicenseGroup.list_opt[:include]
136 r.has_key?(:licenses).should be_true
139 describe 'json一覧出力オプションに於いて' do
140 it 'includeキーを含んでいる' do
141 r = LicenseGroup.list_json_opt
142 r.has_key?(:include).should be_true
145 r = LicenseGroup.list_json_opt[:include]
146 r.should have(1).items
149 r = LicenseGroup.list_json_opt[:include]
150 r.has_key?(:licenses).should be_true
154 describe '単体取得に於いて' do
156 @lg = FactoryGirl.create :license_group
158 context 'つつがなく終わるとき' do
\r
159 it '単体取得オプションを利用している' do
\r
160 LicenseGroup.stub(:show_opt).with(any_args).and_return({})
\r
161 LicenseGroup.should_receive(:show_opt).with(any_args).exactly(1)
\r
162 r = LicenseGroup.show @lg.id
166 l = LicenseGroup.show @lg.id
169 context '存在しないライセンスグループを開こうとしたとき' do
\r
170 it '404RecordNotFound例外を返す' do
\r
172 LicenseGroup.show 110
\r
173 }.should raise_error(ActiveRecord::RecordNotFound)
\r
177 describe '単体出力オプションに於いて' do
178 it 'includeキーを含んでいる' do
179 r = LicenseGroup.show_opt
180 r.has_key?(:include).should be_true
183 r = LicenseGroup.show_opt[:include]
184 r.should have(1).items
187 r = LicenseGroup.show_opt[:include]
188 r.has_key?(:licenses).should be_true
191 describe 'json単体出力オプションに於いて' do
192 it 'includeキーを含んでいる' do
193 r = LicenseGroup.show_json_opt
194 r.has_key?(:include).should be_true
197 r = LicenseGroup.show_json_opt[:include]
198 r.should have(1).items
201 r = LicenseGroup.show_json_opt[:include]
202 r.has_key?(:licenses).should be_true
211 context 'つつがなく終わるとき' do
213 LicenseGroup.stub(:modify_object).with(any_args).and_return(LicenseGroup.new)
214 LicenseGroup.should_receive(:modify_object).with(any_args).exactly(1)
215 License.stub(:stores).with(any_args).and_return(0)
216 LicenseGroup.any_instance.stub(:save).with(any_args).and_return(true)
217 r = LicenseGroup.store(@n, @a)
219 it 'ライセンスに複数の更新を依頼する' do
220 LicenseGroup.stub(:modify_object).with(any_args).and_return(LicenseGroup.new)
221 License.stub(:stores).with(any_args).and_return(0)
222 License.should_receive(:stores).with(any_args).exactly(1)
223 LicenseGroup.any_instance.stub(:save).with(any_args).and_return(true)
224 r = LicenseGroup.store(@n, @a)
227 LicenseGroup.stub(:modify_object).with(any_args).and_return(LicenseGroup.new)
228 License.stub(:stores).with(any_args).and_return(0)
229 LicenseGroup.any_instance.stub(:save).with(any_args).and_return(true)
230 LicenseGroup.any_instance.should_receive(:save).with(any_args).exactly(1)
231 r = LicenseGroup.store(@n, @a)
234 r = LicenseGroup.store(@n, @a)
235 r.is_a?(LicenseGroup).should be_true
237 r.url.should eq @a["url"]
239 it 'カラム値からlicenses_attributesが削除されている' do
240 @a["licenses_attributes"].should_not be_nil
241 r = LicenseGroup.store(@n, @a)
242 @a["licenses_attributes"].should be_nil
245 context 'ライセンス複数更新が失敗するとき' do
247 LicenseGroup.stub(:modify_object).with(any_args).and_return(LicenseGroup.new)
248 License.stub(:stores).with(any_args).and_return(1)
249 LicenseGroup.any_instance.stub(:save).with(any_args).and_return(true)
251 it '全体エラーメッセージがセットされている' do
252 r = LicenseGroup.store(@n, @a)
253 r.errors[:base].should_not be_blank
255 it 'ライセンスが作成されていない' do
257 r = LicenseGroup.store(@n, @a)
258 }.should_not change License, :count
260 it 'ライセンスグループが作成されていない' do
262 r = LicenseGroup.store(@n, @a)
263 }.should_not change LicenseGroup, :count
268 describe 'インポートに於いて' do
271 context 'つつがなく終わるとき' do
272 it 'ファイルインポートを依頼する' do
273 LicenseGroup.should_receive(:import_file).with(any_args).exactly(1)
274 LicenseGroup.stub(:import_file).with(any_args).and_return([])
275 LicenseGroup.import(@f)
277 it 'ライセンスグループ更新を一回依頼する' do
278 LicenseGroup.stub(:store).with(any_args).and_return(LicenseGroup.new)
279 LicenseGroup.should_receive(:store).with(any_args).exactly(1)
280 LicenseGroup.import(@f)
282 it 'ライセンスグループが追加される' do
284 LicenseGroup.import(@f)
285 }.should change LicenseGroup, :count
289 LicenseGroup.import(@f)
290 }.should change License, :count
293 r = LicenseGroup.import(@f)
297 context '複数データがつつがなく終わるとき' do
298 it 'ライセンスグループ更新を二回依頼する' do
299 LicenseGroup.stub(:store).with(any_args).and_return(LicenseGroup.new)
300 LicenseGroup.should_receive(:store).with(any_args).exactly(2)
301 LicenseGroup.import(@fs)
303 it 'ライセンスグループが二個追加される' do
305 r = LicenseGroup.import(@fs)
306 }.should change(LicenseGroup, :count).by 2
310 r = LicenseGroup.import(@fs)
311 }.should change(License, :count)
314 r = LicenseGroup.import(@fs)
318 context 'ライセンスグループ作成に失敗したとき' do
320 LicenseGroup.any_instance.stub(:save).with(any_args).and_return(false)
321 LicenseGroup.any_instance.stub(:valid?).with(any_args).and_return(false)
323 it 'ライセンスグループの数に変化がない' do
325 LicenseGroup.import(@f)
326 }.should_not change LicenseGroup, :count
329 r = LicenseGroup.import(@f)
330 r.is_a?(Array).should be_true
333 r = LicenseGroup.import(@f)
334 r.should have(1).items
336 it 'ライセンスグループオブジェクトが入っている' do
337 r = LicenseGroup.import(@f)
338 r.first.is_a?(LicenseGroup).should be_true
341 context '複数のライセンスグループ作成に失敗したとき' do
342 #三件中、二件の失敗、一件を成功させ、成功データは戻り値に含まないことを確認する
343 it 'ライセンスグループの数に変化がない' do
345 LicenseGroup.import(@fes)
346 }.should_not change LicenseGroup, :count
348 it '途中で保存に失敗しても全件更新依頼する' do
349 LicenseGroup.stub(:store).with(any_args).and_return(LicenseGroup.new)
350 LicenseGroup.should_receive(:store).with(any_args).exactly(3)
351 LicenseGroup.import(@fes)
354 r = LicenseGroup.import(@fes)
355 r.is_a?(Array).should be_true
358 r = LicenseGroup.import(@fes)
359 r.should have(2).items
361 it '配列の中身は失敗したライセンスグループオブジェクトが入っている' do
362 r = LicenseGroup.import(@fes)
363 r[0].is_a?(LicenseGroup).should be_true
364 r[0]["name"].should eq 'UnknownUrl'
365 r[1].is_a?(LicenseGroup).should be_true
366 r[1]["name"].should eq 'UnknownClassname'