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'
18 it 'オーソドックスなデータなら通る' do
19 @lg = Factory.build :license_group
23 context 'nameを検証するとき' do
25 @lg = Factory.build :license_group
33 @lg.should_not be_valid
37 @lg.should_not be_valid
40 l = Factory :license_group
41 @lg.should_not be_valid
44 context 'classnameを検証するとき' do
46 @lg = Factory.build :license_group
49 @lg.classname = 'a'*50
54 @lg.should_not be_valid
57 @lg.classname = 'a'*51
58 @lg.should_not be_valid
61 context 'captionを検証するとき' do
63 @lg = Factory.build :license_group
71 @lg.should_not be_valid
75 @lg.should_not be_valid
78 context 'urlを検証するとき' do
80 @lg = Factory.build :license_group
83 @lg.url = 'http://test.com/'
88 @lg.should_not be_valid
92 @lg.should_not be_valid
94 it 'url形式でないなら失敗する' do
96 @lg.should_not be_valid
106 context 'つつがなく終わるとき' do
108 LicenseGroup.stub(:modify_object).with(any_args).and_return(LicenseGroup.new)
109 LicenseGroup.should_receive(:modify_object).with(any_args).exactly(1)
110 License.stub(:stores).with(any_args).and_return(0)
111 LicenseGroup.any_instance.stub(:save).with(any_args).and_return(true)
112 r = LicenseGroup.store(@n, @a)
114 it 'ライセンスに複数の更新を依頼する' do
115 LicenseGroup.stub(:modify_object).with(any_args).and_return(LicenseGroup.new)
116 License.stub(:stores).with(any_args).and_return(0)
117 License.should_receive(:stores).with(any_args).exactly(1)
118 LicenseGroup.any_instance.stub(:save).with(any_args).and_return(true)
119 r = LicenseGroup.store(@n, @a)
122 LicenseGroup.stub(:modify_object).with(any_args).and_return(LicenseGroup.new)
123 License.stub(:stores).with(any_args).and_return(0)
124 LicenseGroup.any_instance.stub(:save).with(any_args).and_return(true)
125 LicenseGroup.any_instance.should_receive(:save).with(any_args).exactly(1)
126 r = LicenseGroup.store(@n, @a)
129 r = LicenseGroup.store(@n, @a)
130 r.is_a?(LicenseGroup).should be_true
132 r.url.should eq @a["url"]
134 it 'カラム値からlicenses_attributesが削除されている' do
135 @a["licenses_attributes"].should_not be_nil
136 r = LicenseGroup.store(@n, @a)
137 @a["licenses_attributes"].should be_nil
140 context 'ライセンス複数更新が失敗するとき' do
142 LicenseGroup.stub(:modify_object).with(any_args).and_return(LicenseGroup.new)
143 License.stub(:stores).with(any_args).and_return(1)
144 LicenseGroup.any_instance.stub(:save).with(any_args).and_return(true)
146 it '全体エラーメッセージがセットされている' do
147 r = LicenseGroup.store(@n, @a)
148 r.errors[:base].should_not be_blank
150 it 'ライセンスが作成されていない' do
152 r = LicenseGroup.store(@n, @a)
153 }.should_not change License, :count
155 it 'ライセンスグループが作成されていない' do
157 r = LicenseGroup.store(@n, @a)
158 }.should_not change LicenseGroup, :count
163 describe 'インポートに於いて' do
166 context 'つつがなく終わるとき' do
167 it 'ファイルインポートを依頼する' do
168 LicenseGroup.should_receive(:import_file).with(any_args).exactly(1)
169 LicenseGroup.stub(:import_file).with(any_args).and_return([])
170 LicenseGroup.import(@f)
172 it 'ライセンスグループ更新を一回依頼する' do
173 LicenseGroup.stub(:store).with(any_args).and_return(LicenseGroup.new)
174 LicenseGroup.should_receive(:store).with(any_args).exactly(1)
175 LicenseGroup.import(@f)
177 it 'ライセンスグループが追加される' do
179 LicenseGroup.import(@f)
180 }.should change LicenseGroup, :count
184 LicenseGroup.import(@f)
185 }.should change License, :count
188 r = LicenseGroup.import(@f)
192 context '複数データがつつがなく終わるとき' do
193 it 'ライセンスグループ更新を二回依頼する' do
194 LicenseGroup.stub(:store).with(any_args).and_return(LicenseGroup.new)
195 LicenseGroup.should_receive(:store).with(any_args).exactly(2)
196 LicenseGroup.import(@fs)
198 it 'ライセンスグループが二個追加される' do
200 r = LicenseGroup.import(@fs)
201 }.should change(LicenseGroup, :count).by 2
205 r = LicenseGroup.import(@fs)
206 }.should change(License, :count)
209 r = LicenseGroup.import(@fs)
213 context 'ライセンスグループ作成に失敗したとき' do
215 LicenseGroup.any_instance.stub(:save).with(any_args).and_return(false)
216 LicenseGroup.any_instance.stub(:valid?).with(any_args).and_return(false)
218 it 'ライセンスグループの数に変化がない' do
220 LicenseGroup.import(@f)
221 }.should_not change LicenseGroup, :count
224 r = LicenseGroup.import(@f)
225 r.is_a?(Array).should be_true
228 r = LicenseGroup.import(@f)
229 r.should have(1).items
231 it 'ライセンスグループオブジェクトが入っている' do
232 r = LicenseGroup.import(@f)
233 r.first.is_a?(LicenseGroup).should be_true
236 context '複数のライセンスグループ作成に失敗したとき' do
237 #三件中、二件の失敗、一件を成功させ、成功データは戻り値に含まないことを確認する
238 it 'ライセンスグループの数に変化がない' do
240 LicenseGroup.import(@fes)
241 }.should_not change LicenseGroup, :count
243 it '途中で保存に失敗しても全件更新依頼する' do
244 LicenseGroup.stub(:store).with(any_args).and_return(LicenseGroup.new)
245 LicenseGroup.should_receive(:store).with(any_args).exactly(3)
246 LicenseGroup.import(@fes)
249 r = LicenseGroup.import(@fes)
250 r.is_a?(Array).should be_true
253 r = LicenseGroup.import(@fes)
254 r.should have(2).items
256 it '配列の中身は失敗したライセンスグループオブジェクトが入っている' do
257 r = LicenseGroup.import(@fes)
258 r[0].is_a?(LicenseGroup).should be_true
259 r[0]["name"].should eq 'UnknownUrl'
260 r[1].is_a?(LicenseGroup).should be_true
261 r[1]["name"].should eq 'UnknownClassname'
266 describe '単体取得に於いて' do
268 @lg = Factory :license_group
271 l = LicenseGroup.show @lg.id
275 describe '関連テーブルプションに於いて' do
276 context 'オプションがないとき' do
278 r = LicenseGroup.show_include_opt
279 r.has_key?(:licenses).should be_true
283 describe 'json単体出力オプションに於いて' do
284 it 'includeキーを含んでいる' do
285 r = LicenseGroup.show_json_include_opt
286 r.has_key?(:include).should be_true
289 r = LicenseGroup.show_json_include_opt[:include]
290 r.should have(1).items
293 r = LicenseGroup.show_json_include_opt[:include]
294 r.has_key?(:licenses).should be_true
297 describe '一覧取得に於いて' do
299 @lg = Factory :license_group, :name => "1"
302 l = LicenseGroup.list
306 @lg2 = Factory :license_group, :name => "5", :url => 'http://test.ptn/10'
307 l = LicenseGroup.list
308 l.should eq [@lg, @lg2]
311 describe 'list関連テーブルプションに於いて' do
312 it 'includeキーを含んでいる' do
313 r = LicenseGroup.list_opt
314 r.has_key?(:include).should be_true
317 r = LicenseGroup.list_opt[:include]
318 r.should have(1).items
321 r = LicenseGroup.list_opt[:include]
322 r.has_key?(:licenses).should be_true
325 describe 'json一覧出力オプションに於いて' do
326 it 'includeキーを含んでいる' do
327 r = LicenseGroup.list_json_opt
328 r.has_key?(:include).should be_true
331 r = LicenseGroup.list_json_opt[:include]
332 r.should have(1).items
335 r = LicenseGroup.list_json_opt[:include]
336 r.has_key?(:licenses).should be_true