OSDN Git Service

pass test license controller
[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     end
17     
18     it 'オーソドックスなデータなら通る' do
19       @lg = Factory.build :license_group
20       @lg.should be_valid
21     end
22     
23     context 'nameを検証するとき' do
24       before do
25         @lg = Factory.build :license_group
26       end
27       it 'テストデータの確認' do
28         @lg.name = 'a'*50
29         @lg.should be_valid
30       end
31       it 'nullなら失敗する' do
32         @lg.name = ''
33         @lg.should_not be_valid
34       end
35       it '51文字以上なら失敗する' do
36         @lg.name = 'a'*51
37         @lg.should_not be_valid
38       end
39       it '重複していたら失敗する' do
40         l = Factory :license_group
41         @lg.should_not be_valid
42       end
43     end
44     context 'classnameを検証するとき' do
45       before do
46         @lg = Factory.build :license_group
47       end
48       it 'テストデータの確認' do
49         @lg.classname = 'a'*50
50         @lg.should be_valid
51       end
52       it 'nullなら失敗する' do
53         @lg.classname = ''
54         @lg.should_not be_valid
55       end
56       it '51文字以上なら失敗する' do
57         @lg.classname = 'a'*51
58         @lg.should_not be_valid
59       end
60     end
61     context 'captionを検証するとき' do
62       before do
63         @lg = Factory.build :license_group
64       end
65       it 'テストデータの確認' do
66         @lg.caption = 'a'*30
67         @lg.should be_valid
68       end
69       it 'nullなら失敗する' do
70         @lg.caption = ''
71         @lg.should_not be_valid
72       end
73       it '51文字以上なら失敗する' do
74         @lg.caption = 'a'*51
75         @lg.should_not be_valid
76       end
77     end
78     context 'urlを検証するとき' do
79       before do
80         @lg = Factory.build :license_group
81       end
82       it 'テストデータの確認' do
83         @lg.url = 'http://test.com/'
84         @lg.should be_valid
85       end
86       it 'nullなら失敗する' do
87         @lg.url = ''
88         @lg.should_not be_valid
89       end
90       it '201文字以上なら失敗する' do
91         @lg.url = 'a'*201
92         @lg.should_not be_valid
93       end
94       it 'url形式でないなら失敗する' do
95         @lg.url = 'aaaaaaa'
96         @lg.should_not be_valid
97       end
98     end
99   end
100   
101   describe '更新に於いて' do
102     before do
103       @n = @j.keys.first
104       @a = @j.values.first
105     end
106     context 'つつがなく終わるとき' do
107       it 'データ更新準備を依頼する' 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)
113       end
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)
120       end
121       it '保存を依頼する' do
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)
127       end
128       it 'オブジェクトを返す' do
129         r = LicenseGroup.store(@n, @a)
130         r.is_a?(LicenseGroup).should be_true
131         r.name.should eq @n
132         r.url.should eq @a["url"]
133       end
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
138       end
139     end
140     context 'ライセンス複数更新が失敗するとき' do
141       before 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)
145       end
146       it '全体エラーメッセージがセットされている' do
147         r = LicenseGroup.store(@n, @a)
148         r.errors[:base].should_not be_blank
149       end
150       it 'ライセンスが作成されていない' do
151         lambda {
152           r = LicenseGroup.store(@n, @a)
153         }.should_not change License, :count
154       end
155       it 'ライセンスグループが作成されていない' do
156         lambda {
157           r = LicenseGroup.store(@n, @a)
158         }.should_not change LicenseGroup, :count
159       end
160     end
161   end
162   
163   describe 'インポートに於いて' do
164     before do
165     end
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)
171       end
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)
176       end
177       it 'ライセンスグループが追加される' do
178         lambda {
179           LicenseGroup.import(@f)
180         }.should change LicenseGroup, :count
181       end
182       it 'ライセンスが追加される' do
183         lambda {
184           LicenseGroup.import(@f)
185         }.should change License, :count
186       end
187       it '[]を返す' do
188         r = LicenseGroup.import(@f)
189         r.should eq []
190       end
191     end
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)
197       end
198       it 'ライセンスグループが二個追加される' do
199         lambda {
200           r = LicenseGroup.import(@fs)
201         }.should change(LicenseGroup, :count).by 2
202       end
203       it 'ライセンスが追加される' do
204         lambda {
205           r = LicenseGroup.import(@fs)
206         }.should change(License, :count)
207       end
208       it '[]を返す' do
209         r = LicenseGroup.import(@fs)
210         r.should eq []
211       end
212     end
213     context 'ライセンスグループ作成に失敗したとき' do
214       before 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)
217       end
218       it 'ライセンスグループの数に変化がない' do
219         lambda {
220           LicenseGroup.import(@f)
221         }.should_not change LicenseGroup, :count
222       end
223       it '配列を返す' do
224         r = LicenseGroup.import(@f)
225         r.is_a?(Array).should be_true
226       end
227       it '配列の中身は一件' do
228         r = LicenseGroup.import(@f)
229         r.should have(1).items
230       end
231       it 'ライセンスグループオブジェクトが入っている' do
232         r = LicenseGroup.import(@f)
233         r.first.is_a?(LicenseGroup).should be_true
234       end
235     end
236     context '複数のライセンスグループ作成に失敗したとき' do
237       #三件中、二件の失敗、一件を成功させ、成功データは戻り値に含まないことを確認する
238       it 'ライセンスグループの数に変化がない' do
239         lambda {
240           LicenseGroup.import(@fes)
241         }.should_not change LicenseGroup, :count
242       end
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)
247       end
248       it '配列を返す' do
249         r = LicenseGroup.import(@fes)
250         r.is_a?(Array).should be_true
251       end
252       it '配列の中身は2件' do
253         r = LicenseGroup.import(@fes)
254         r.should have(2).items
255       end
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'
262       end
263     end
264   end
265   
266   describe '単体取得に於いて' do
267     before do
268       @lg = Factory :license_group
269     end
270     it '指定のコマを返す' do
271       l = LicenseGroup.show @lg.id
272       l.should eq @lg
273     end
274   end
275   describe '関連テーブルプションに於いて' do
276     context 'オプションがないとき' do
277       it 'ライセンスを含んでいる' do
278         r = LicenseGroup.show_include_opt
279         r.has_key?(:licenses).should be_true
280       end
281     end
282   end
283   describe 'json単体出力オプションに於いて' do
284     it 'includeキーを含んでいる' do
285       r = LicenseGroup.show_json_include_opt
286       r.has_key?(:include).should be_true
287     end
288     it '1つの項目を含んでいる' do
289       r = LicenseGroup.show_json_include_opt[:include]
290       r.should have(1).items
291     end
292     it 'ライセンスを含んでいる' do
293       r = LicenseGroup.show_json_include_opt[:include]
294       r.has_key?(:licenses).should be_true
295     end
296   end
297   describe '一覧取得に於いて' do
298     before do
299       @lg = Factory :license_group, :name => "1"
300     end
301     it 'リストを返す' do
302       l = LicenseGroup.list
303       l.should eq [@lg]
304     end
305     it '名前順で並んでいる' do
306       @lg2 = Factory :license_group, :name => "5", :url => 'http://test.ptn/10'
307       l = LicenseGroup.list
308       l.should eq [@lg, @lg2]
309     end
310   end
311   describe 'list関連テーブルプションに於いて' do
312     it 'includeキーを含んでいる' do
313       r = LicenseGroup.list_opt
314       r.has_key?(:include).should be_true
315     end
316     it '1つの項目を含んでいる' do
317       r = LicenseGroup.list_opt[:include]
318       r.should have(1).items
319     end
320     it 'ライセンスを含んでいる' do
321       r = LicenseGroup.list_opt[:include]
322       r.has_key?(:licenses).should be_true
323     end
324   end
325   describe 'json一覧出力オプションに於いて' do
326     it 'includeキーを含んでいる' do
327       r = LicenseGroup.list_json_opt
328       r.has_key?(:include).should be_true
329     end
330     it '1つの項目を含んでいる' do
331       r = LicenseGroup.list_json_opt[:include]
332       r.should have(1).items
333     end
334     it 'ライセンスを含んでいる' do
335       r = LicenseGroup.list_json_opt[:include]
336       r.has_key?(:licenses).should be_true
337     end
338   end
339   
340 end