OSDN Git Service

a6ca057a82b2f1b78e283bb881e1b17062dcd0a2
[pettanr/pettanr.git] / spec / models / license_group_spec.rb
1 # -*- encoding: utf-8 -*-
2 #ライセンスグループ
3 require 'spec_helper'
4
5 describe LicenseGroup do
6   before do
7     #テストデータを用意してね
8     @f = Rails.root + 'spec/json/license_group.json'
9     @t = File.open(@f, 'r').read
10     @j = JSON.parse @t
11     @fs = Rails.root + 'spec/json/license_groups.json'
12     @fes = Rails.root + 'spec/json/invalid_license_groups.json'
13   end
14   describe '検証に於いて' do
15     before do
16       @lg = FactoryGirl.build :license_group
17     end
18     
19     context 'オーソドックスなデータのとき' do
20       it '下限データが通る' do
21         @lg.name = 'a'
22         @lg.classname = 'a'
23         @lg.caption = 'a'
24         @lg.url = 'http://test.jp/'
25         @lg.should be_valid
26       end
27       it '上限データが通る' do
28         @lg.name = 'a'*50
29         @lg.classname = 'a'*50
30         @lg.caption = 'a'*30
31         @lg.url = 'http://test.jp/aaaaa' + 'a' * 180
32         @lg.should be_valid
33       end
34     end
35     
36     context 'nameを検証するとき' do
37       it 'nullなら失敗する' do
38         @lg.name = ''
39         @lg.should_not be_valid
40       end
41       it '51文字以上なら失敗する' do
42         @lg.name = 'a'*51
43         @lg.should_not be_valid
44       end
45       it '重複していたら失敗する' do
46         l = FactoryGirl.create :license_group
47         @lg.should_not be_valid
48       end
49     end
50     context 'classnameを検証するとき' do
51       it 'nullなら失敗する' do
52         @lg.classname = ''
53         @lg.should_not be_valid
54       end
55       it '51文字以上なら失敗する' do
56         @lg.classname = 'a'*51
57         @lg.should_not be_valid
58       end
59     end
60     context 'captionを検証するとき' do
61       it 'nullなら失敗する' do
62         @lg.caption = ''
63         @lg.should_not be_valid
64       end
65       it '51文字以上なら失敗する' do
66         @lg.caption = 'a'*51
67         @lg.should_not be_valid
68       end
69     end
70     context 'urlを検証するとき' do
71       it 'nullなら失敗する' do
72         @lg.url = ''
73         @lg.should_not be_valid
74       end
75       it '201文字以上なら失敗する' do
76         @lg.url = 'a'*201
77         @lg.should_not be_valid
78       end
79       it 'url形式でないなら失敗する' do
80         @lg.url = 'aaaaaaa'
81         @lg.should_not be_valid
82       end
83     end
84   end
85   
86   describe 'デフォルト値補充に於いて' do
87     it 'defined' do
88       @lg = FactoryGirl.build :license_group, :name => "1"
89       @lg.supply_default
90     end
91   end
92   
93   describe '上書き補充に於いて' do
94     it 'defined' do
95       @lg = FactoryGirl.build :license_group, :name => "1"
96       @lg.overwrite
97     end
98   end
99   
100   describe '閲覧許可に於いて' do
101     #ライセンスグループは作家作成する前から存在するので、閲覧制限の意味がない
102   end
103   
104   describe '一覧取得に於いて' do
105     before do
106       @lg = FactoryGirl.create :license_group, :name => "1"
107     end
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
113       end\r
114     end\r
115     it 'リストを返す' do
116       l = LicenseGroup.list
117       l.should eq [@lg]
118     end
119     it '名前順で並んでいる' do
120       @lg2 = FactoryGirl.create :license_group, :name => "5", :url => 'http://test.ptn/10'
121       l = LicenseGroup.list
122       l.should eq [@lg, @lg2]
123     end
124   end
125   describe 'list関連テーブルプションに於いて' do
126     it 'includeキーを含んでいる' do
127       r = LicenseGroup.list_opt
128       r.has_key?(:include).should be_true
129     end
130     it '1つの項目を含んでいる' do
131       r = LicenseGroup.list_opt[:include]
132       r.should have(1).items
133     end
134     it 'ライセンスを含んでいる' do
135       r = LicenseGroup.list_opt[:include]
136       r.has_key?(:licenses).should be_true
137     end
138   end
139   describe 'json一覧出力オプションに於いて' do
140     it 'includeキーを含んでいる' do
141       r = LicenseGroup.list_json_opt
142       r.has_key?(:include).should be_true
143     end
144     it '1つの項目を含んでいる' do
145       r = LicenseGroup.list_json_opt[:include]
146       r.should have(1).items
147     end
148     it 'ライセンスを含んでいる' do
149       r = LicenseGroup.list_json_opt[:include]
150       r.has_key?(:licenses).should be_true
151     end
152   end
153   
154   describe '単体取得に於いて' do
155     before do
156       @lg = FactoryGirl.create :license_group
157     end
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
163       end\r
164     end\r
165     it '指定のコマを返す' do
166       l = LicenseGroup.show @lg.id
167       l.should eq @lg
168     end
169     context '存在しないライセンスグループを開こうとしたとき' do\r
170       it '404RecordNotFound例外を返す' do\r
171         lambda{\r
172           LicenseGroup.show 110\r
173         }.should raise_error(ActiveRecord::RecordNotFound)\r
174       end\r
175     end\r
176   end
177   describe '単体出力オプションに於いて' do
178     it 'includeキーを含んでいる' do
179       r = LicenseGroup.show_opt
180       r.has_key?(:include).should be_true
181     end
182     it '1つの項目を含んでいる' do
183       r = LicenseGroup.show_opt[:include]
184       r.should have(1).items
185     end
186     it 'ライセンスを含んでいる' do
187       r = LicenseGroup.show_opt[:include]
188       r.has_key?(:licenses).should be_true
189     end
190   end
191   describe 'json単体出力オプションに於いて' do
192     it 'includeキーを含んでいる' do
193       r = LicenseGroup.show_json_opt
194       r.has_key?(:include).should be_true
195     end
196     it '1つの項目を含んでいる' do
197       r = LicenseGroup.show_json_opt[:include]
198       r.should have(1).items
199     end
200     it 'ライセンスを含んでいる' do
201       r = LicenseGroup.show_json_opt[:include]
202       r.has_key?(:licenses).should be_true
203     end
204   end
205   
206   describe '更新に於いて' do
207     before do
208       @n = @j.keys.first
209       @a = @j.values.first
210     end
211     context 'つつがなく終わるとき' do
212       it 'データ更新準備を依頼する' 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)
218       end
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)
225       end
226       it '保存を依頼する' do
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)
232       end
233       it 'オブジェクトを返す' do
234         r = LicenseGroup.store(@n, @a)
235         r.is_a?(LicenseGroup).should be_true
236         r.name.should eq @n
237         r.url.should eq @a["url"]
238       end
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
243       end
244     end
245     context 'ライセンス複数更新が失敗するとき' do
246       before 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)
250       end
251       it '全体エラーメッセージがセットされている' do
252         r = LicenseGroup.store(@n, @a)
253         r.errors[:base].should_not be_blank
254       end
255       it 'ライセンスが作成されていない' do
256         lambda {
257           r = LicenseGroup.store(@n, @a)
258         }.should_not change License, :count
259       end
260       it 'ライセンスグループが作成されていない' do
261         lambda {
262           r = LicenseGroup.store(@n, @a)
263         }.should_not change LicenseGroup, :count
264       end
265     end
266   end
267   
268   describe 'インポートに於いて' do
269     before do
270     end
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)
276       end
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)
281       end
282       it 'ライセンスグループが追加される' do
283         lambda {
284           LicenseGroup.import(@f)
285         }.should change LicenseGroup, :count
286       end
287       it 'ライセンスが追加される' do
288         lambda {
289           LicenseGroup.import(@f)
290         }.should change License, :count
291       end
292       it '[]を返す' do
293         r = LicenseGroup.import(@f)
294         r.should eq []
295       end
296     end
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)
302       end
303       it 'ライセンスグループが二個追加される' do
304         lambda {
305           r = LicenseGroup.import(@fs)
306         }.should change(LicenseGroup, :count).by 2
307       end
308       it 'ライセンスが追加される' do
309         lambda {
310           r = LicenseGroup.import(@fs)
311         }.should change(License, :count)
312       end
313       it '[]を返す' do
314         r = LicenseGroup.import(@fs)
315         r.should eq []
316       end
317     end
318     context 'ライセンスグループ作成に失敗したとき' do
319       before 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)
322       end
323       it 'ライセンスグループの数に変化がない' do
324         lambda {
325           LicenseGroup.import(@f)
326         }.should_not change LicenseGroup, :count
327       end
328       it '配列を返す' do
329         r = LicenseGroup.import(@f)
330         r.is_a?(Array).should be_true
331       end
332       it '配列の中身は一件' do
333         r = LicenseGroup.import(@f)
334         r.should have(1).items
335       end
336       it 'ライセンスグループオブジェクトが入っている' do
337         r = LicenseGroup.import(@f)
338         r.first.is_a?(LicenseGroup).should be_true
339       end
340     end
341     context '複数のライセンスグループ作成に失敗したとき' do
342       #三件中、二件の失敗、一件を成功させ、成功データは戻り値に含まないことを確認する
343       it 'ライセンスグループの数に変化がない' do
344         lambda {
345           LicenseGroup.import(@fes)
346         }.should_not change LicenseGroup, :count
347       end
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)
352       end
353       it '配列を返す' do
354         r = LicenseGroup.import(@fes)
355         r.is_a?(Array).should be_true
356       end
357       it '配列の中身は2件' do
358         r = LicenseGroup.import(@fes)
359         r.should have(2).items
360       end
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'
367       end
368     end
369   end
370   
371 end