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
210 @imager = ImagerTest.load("abc\ndef\nghi")
211 PettanImager.stub(:load).with(any_args).and_return(@imager)
213 context 'つつがなく終わるとき' do
215 LicenseGroup.stub(:modify_object).with(any_args).and_return(LicenseGroup.new)
216 LicenseGroup.should_receive(:modify_object).with(any_args).exactly(1)
217 License.stub(:stores).with(any_args).and_return(0)
218 LicenseGroup.any_instance.stub(:save).with(any_args).and_return(true)
219 r = LicenseGroup.store(@n, @a)
221 it 'ライセンスに複数の更新を依頼する' do
222 LicenseGroup.stub(:modify_object).with(any_args).and_return(LicenseGroup.new)
223 License.stub(:stores).with(any_args).and_return(0)
224 License.should_receive(:stores).with(any_args).exactly(1)
225 LicenseGroup.any_instance.stub(:save).with(any_args).and_return(true)
226 r = LicenseGroup.store(@n, @a)
229 LicenseGroup.stub(:modify_object).with(any_args).and_return(LicenseGroup.new)
230 License.stub(:stores).with(any_args).and_return(0)
231 LicenseGroup.any_instance.stub(:save).with(any_args).and_return(true)
232 LicenseGroup.any_instance.should_receive(:save).with(any_args).exactly(1)
233 r = LicenseGroup.store(@n, @a)
236 r = LicenseGroup.store(@n, @a)
237 r.is_a?(LicenseGroup).should be_true
239 r.url.should eq @a["url"]
241 it 'カラム値からlicenses_attributesが削除されている' do
242 @a["licenses_attributes"].should_not be_nil
243 r = LicenseGroup.store(@n, @a)
244 @a["licenses_attributes"].should be_nil
247 context 'ライセンス複数更新が失敗するとき' do
249 LicenseGroup.stub(:modify_object).with(any_args).and_return(LicenseGroup.new)
250 License.stub(:stores).with(any_args).and_return(1)
251 LicenseGroup.any_instance.stub(:save).with(any_args).and_return(true)
253 it '全体エラーメッセージがセットされている' do
254 r = LicenseGroup.store(@n, @a)
255 r.errors[:base].should_not be_blank
257 it 'ライセンスが作成されていない' do
259 r = LicenseGroup.store(@n, @a)
260 }.should_not change License, :count
262 it 'ライセンスグループが作成されていない' do
264 r = LicenseGroup.store(@n, @a)
265 }.should_not change LicenseGroup, :count
270 describe 'インポートに於いて' do
273 context 'つつがなく終わるとき' do
274 it 'ファイルインポートを依頼する' do
275 LicenseGroup.should_receive(:import_file).with(any_args).exactly(1)
276 LicenseGroup.stub(:import_file).with(any_args).and_return([])
277 LicenseGroup.import(@f)
279 it 'ライセンスグループ更新を一回依頼する' do
280 LicenseGroup.stub(:store).with(any_args).and_return(LicenseGroup.new)
281 LicenseGroup.should_receive(:store).with(any_args).exactly(1)
282 LicenseGroup.import(@f)
284 it 'ライセンスグループが追加される' do
286 LicenseGroup.import(@f)
287 }.should change LicenseGroup, :count
291 LicenseGroup.import(@f)
292 }.should change License, :count
295 r = LicenseGroup.import(@f)
299 context '複数データがつつがなく終わるとき' do
300 it 'ライセンスグループ更新を二回依頼する' do
301 LicenseGroup.stub(:store).with(any_args).and_return(LicenseGroup.new)
302 LicenseGroup.should_receive(:store).with(any_args).exactly(2)
303 LicenseGroup.import(@fs)
305 it 'ライセンスグループが二個追加される' do
307 r = LicenseGroup.import(@fs)
308 }.should change(LicenseGroup, :count).by 2
312 r = LicenseGroup.import(@fs)
313 }.should change(License, :count)
316 r = LicenseGroup.import(@fs)
320 context 'ライセンスグループ作成に失敗したとき' do
322 LicenseGroup.any_instance.stub(:save).with(any_args).and_return(false)
323 LicenseGroup.any_instance.stub(:valid?).with(any_args).and_return(false)
325 it 'ライセンスグループの数に変化がない' do
327 LicenseGroup.import(@f)
328 }.should_not change LicenseGroup, :count
331 r = LicenseGroup.import(@f)
332 r.is_a?(Array).should be_true
335 r = LicenseGroup.import(@f)
336 r.should have(1).items
338 it 'ライセンスグループオブジェクトが入っている' do
339 r = LicenseGroup.import(@f)
340 r.first.is_a?(LicenseGroup).should be_true
343 context '複数のライセンスグループ作成に失敗したとき' do
344 #三件中、二件の失敗、一件を成功させ、成功データは戻り値に含まないことを確認する
345 it 'ライセンスグループの数に変化がない' do
347 LicenseGroup.import(@fes)
348 }.should_not change LicenseGroup, :count
350 it '途中で保存に失敗しても全件更新依頼する' do
351 LicenseGroup.stub(:store).with(any_args).and_return(LicenseGroup.new)
352 LicenseGroup.should_receive(:store).with(any_args).exactly(3)
353 LicenseGroup.import(@fes)
356 r = LicenseGroup.import(@fes)
357 r.is_a?(Array).should be_true
360 r = LicenseGroup.import(@fes)
361 r.should have(2).items
363 it '配列の中身は失敗したライセンスグループオブジェクトが入っている' do
364 r = LicenseGroup.import(@fes)
365 r[0].is_a?(LicenseGroup).should be_true
366 r[0]["name"].should eq 'UnknownUrl'
367 r[1].is_a?(LicenseGroup).should be_true
368 r[1]["name"].should eq 'UnknownClassname'