OSDN Git Service

t#29400:itr3?
[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       @imager = ImagerTest.load("abc\ndef\nghi")
211       PettanImager.stub(:load).with(any_args).and_return(@imager)
212     end
213     context 'つつがなく終わるとき' do
214       it 'データ更新準備を依頼する' 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)
220       end
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)
227       end
228       it '保存を依頼する' do
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)
234       end
235       it 'オブジェクトを返す' do
236         r = LicenseGroup.store(@n, @a)
237         r.is_a?(LicenseGroup).should be_true
238         r.name.should eq @n
239         r.url.should eq @a["url"]
240       end
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
245       end
246     end
247     context 'ライセンス複数更新が失敗するとき' do
248       before 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)
252       end
253       it '全体エラーメッセージがセットされている' do
254         r = LicenseGroup.store(@n, @a)
255         r.errors[:base].should_not be_blank
256       end
257       it 'ライセンスが作成されていない' do
258         lambda {
259           r = LicenseGroup.store(@n, @a)
260         }.should_not change License, :count
261       end
262       it 'ライセンスグループが作成されていない' do
263         lambda {
264           r = LicenseGroup.store(@n, @a)
265         }.should_not change LicenseGroup, :count
266       end
267     end
268   end
269   
270   describe 'インポートに於いて' do
271     before do
272     end
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)
278       end
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)
283       end
284       it 'ライセンスグループが追加される' do
285         lambda {
286           LicenseGroup.import(@f)
287         }.should change LicenseGroup, :count
288       end
289       it 'ライセンスが追加される' do
290         lambda {
291           LicenseGroup.import(@f)
292         }.should change License, :count
293       end
294       it '[]を返す' do
295         r = LicenseGroup.import(@f)
296         r.should eq []
297       end
298     end
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)
304       end
305       it 'ライセンスグループが二個追加される' do
306         lambda {
307           r = LicenseGroup.import(@fs)
308         }.should change(LicenseGroup, :count).by 2
309       end
310       it 'ライセンスが追加される' do
311         lambda {
312           r = LicenseGroup.import(@fs)
313         }.should change(License, :count)
314       end
315       it '[]を返す' do
316         r = LicenseGroup.import(@fs)
317         r.should eq []
318       end
319     end
320     context 'ライセンスグループ作成に失敗したとき' do
321       before 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)
324       end
325       it 'ライセンスグループの数に変化がない' do
326         lambda {
327           LicenseGroup.import(@f)
328         }.should_not change LicenseGroup, :count
329       end
330       it '配列を返す' do
331         r = LicenseGroup.import(@f)
332         r.is_a?(Array).should be_true
333       end
334       it '配列の中身は一件' do
335         r = LicenseGroup.import(@f)
336         r.should have(1).items
337       end
338       it 'ライセンスグループオブジェクトが入っている' do
339         r = LicenseGroup.import(@f)
340         r.first.is_a?(LicenseGroup).should be_true
341       end
342     end
343     context '複数のライセンスグループ作成に失敗したとき' do
344       #三件中、二件の失敗、一件を成功させ、成功データは戻り値に含まないことを確認する
345       it 'ライセンスグループの数に変化がない' do
346         lambda {
347           LicenseGroup.import(@fes)
348         }.should_not change LicenseGroup, :count
349       end
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)
354       end
355       it '配列を返す' do
356         r = LicenseGroup.import(@fes)
357         r.is_a?(Array).should be_true
358       end
359       it '配列の中身は2件' do
360         r = LicenseGroup.import(@fes)
361         r.should have(2).items
362       end
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'
369       end
370     end
371   end
372   
373 end