OSDN Git Service

t#29400:update:itr2
[pettanr/pettanr.git] / spec / models / license_spec.rb
1 # -*- encoding: utf-8 -*-
2 #ライセンス
3 require 'spec_helper'
4
5 describe License do
6   before do
7   end
8   describe '検証に於いて' do
9     before do
10       @sp = FactoryGirl.create :system_picture
11       @lg = FactoryGirl.create :license_group
12       @l = FactoryGirl.build :license, :license_group_id => @lg.id, :system_picture_id => @sp.id
13     end
14     
15     context 'オーソドックスなデータのとき' do
16       it '下限データが通る' do
17         @l.name = 'a'
18         @l.caption = 'a'
19         @l.url = 'http://test.jp/'
20         @l.should be_valid
21       end
22       it '上限データが通る' do
23         @l.name = 'a'*50
24         @l.caption = 'a'*30
25         @l.url = 'http://test.jp/aaaaa' + 'a' * 180
26         @l.should be_valid
27       end
28     end
29     
30     context 'license_group_idを検証するとき' do
31       it 'nullなら失敗する' do
32         @l.license_group_id = ''
33         @l.should_not be_valid
34       end
35       it '数値でなければ失敗する' do\r
36         @l.license_group_id = 'a'\r
37         @l.should_not be_valid\r
38       end\r
39       it '存在するライセンスグループでなければ失敗する' do\r
40         @l.license_group_id = 0\r
41         @l.should_not be_valid\r
42       end\r
43     end
44     context 'nameを検証するとき' do
45       it 'nullなら失敗する' do
46         @l.name = ''
47         @l.should_not be_valid
48       end
49       it '51文字以上なら失敗する' do
50         @l.name = 'a'*51
51         @l.should_not be_valid
52       end
53       it '重複していたら失敗する' do
54         lc = FactoryGirl.create :license
55         @l.should_not be_valid
56       end
57     end
58     context 'captionを検証するとき' do
59       it 'nullなら失敗する' do
60         @l.caption = ''
61         @l.should_not be_valid
62       end
63       it '31文字以上なら失敗する' do
64         @l.caption = 'a'*31
65         @l.should_not be_valid
66       end
67     end
68     context 'urlを検証するとき' do
69       it 'nullなら失敗する' do
70         @l.url = ''
71         @l.should_not be_valid
72       end
73       it '201文字以上なら失敗する' do
74         @l.url = 'http://test.jp/aaaaa' + 'a' * 181
75         @l.should_not be_valid
76       end
77       it 'url形式でないなら失敗する' do
78         @l.url = 'a'*200\r
79         @l.should_not be_valid\r
80       end
81     end
82     context 'system_picture_idを検証するとき' do
83       it 'nullなら失敗する' do
84         @l.system_picture_id = ''
85         @l.should_not be_valid
86       end
87       it '数値でなければ失敗する' do\r
88         @l.system_picture_id = 'a'\r
89         @l.should_not be_valid\r
90       end\r
91       it '存在するシステム画像でなければ失敗する' do\r
92         @l.system_picture_id = 0\r
93         @l.should_not be_valid\r
94       end\r
95     end
96   end
97   
98   describe 'デフォルト値補充に於いて' do
99     it 'defined' do
100       @sp = FactoryGirl.create :system_picture
101       @lg = FactoryGirl.create :license_group
102       @l = FactoryGirl.build :license, :name => 'peta2.0', :license_group_id => @lg.id, :system_picture_id => @sp.id
103       @l.supply_default
104     end
105   end
106   
107   describe '上書き補充に於いて' do
108     it 'defined' do
109       @sp = FactoryGirl.create :system_picture
110       @lg = FactoryGirl.create :license_group
111       @l = FactoryGirl.build :license, :name => 'peta2.0', :license_group_id => @lg.id, :system_picture_id => @sp.id
112       @l.overwrite
113     end
114   end
115   
116   describe '閲覧許可に於いて' do
117     #ライセンスは作家作成する前から存在するので、閲覧制限の意味がない
118   end
119   
120   describe '一覧取得に於いて' do
121     before do
122       @sp = FactoryGirl.create :system_picture
123       @lg = FactoryGirl.create :license_group
124       @l = FactoryGirl.create :license, :name => 'peta2.0', :license_group_id => @lg.id, :system_picture_id => @sp.id
125       @lg2 = FactoryGirl.create :license_group, :name => 'pubdm'
126     end
127     context 'つつがなく終わるとき' do\r
128       it '一覧取得オプションを利用している' do\r
129         License.stub(:list_opt).with(any_args).and_return({})\r
130         License.should_receive(:list_opt).with(any_args).exactly(1)\r
131         r = License.list
132       end\r
133     end\r
134     it 'リストを返す' do
135       l = License.list 
136       l.should eq [@l]
137     end
138     it '名前順で並んでいる' do
139       @l2 = FactoryGirl.create :license, :name => 'peta3.0', :url => 'http://pe.ta/3.0', :license_group_id => @lg.id, :system_picture_id => @sp.id
140       @l3 = FactoryGirl.create :license, :name => 'pd1.0', :url => 'http://pb.dm/1.0', :license_group_id => @lg2.id, :system_picture_id => @sp.id
141       l = License.list
142       l.should eq [@l3, @l, @l2]
143     end
144   end
145   describe 'list関連テーブルプションに於いて' do
146     it 'includeキーを含んでいる' do
147       r = License.list_opt
148       r.has_key?(:include).should be_true
149     end
150     it '1つの項目を含んでいる' do
151       r = License.list_opt[:include]
152       r.should have(1).items
153     end
154     it 'ライセンスグループを含んでいる' do
155       r = License.list_opt[:include]
156       r.has_key?(:license_group).should be_true
157     end
158   end
159   describe 'json一覧出力オプションに於いて' do
160     it 'includeキーを含んでいる' do
161       r = License.list_json_opt
162       r.has_key?(:include).should be_true
163     end
164     it '1つの項目を含んでいる' do
165       r = License.list_json_opt[:include]
166       r.should have(1).items
167     end
168     it 'ライセンスグループを含んでいる' do
169       r = License.list_json_opt[:include]
170       r.has_key?(:license_group).should be_true
171     end
172   end
173   
174   describe '単体取得に於いて' do
175     before do
176       @sp = FactoryGirl.create :system_picture
177       @lg = FactoryGirl.create :license_group
178       @l = FactoryGirl.create :license, :license_group_id => @lg.id, :system_picture_id => @sp.id
179     end
180     context 'つつがなく終わるとき' do\r
181       it '単体取得オプションを利用している' do\r
182         License.stub(:show_opt).with(any_args).and_return({})\r
183         License.should_receive(:show_opt).with(any_args).exactly(1)\r
184         r = License.show @l.id
185       end\r
186     end\r
187     it '指定のライセンスを返す' do
188       l = License.show @l.id
189       l.should eq @l
190     end
191     context '存在しないライセンスを開こうとしたとき' do\r
192       it '404RecordNotFound例外を返す' do\r
193         lambda{\r
194           License.show 110\r
195         }.should raise_error(ActiveRecord::RecordNotFound)\r
196       end\r
197     end\r
198   end
199   describe '単体取得オプションに於いて' do
200     it 'includeキーを含んでいる' do
201       r = License.show_opt
202       r.has_key?(:include).should be_true
203     end
204     it '1つの項目を含んでいる' do
205       r = License.show_opt[:include]
206       r.should have(1).items
207     end
208     it 'ライセンスグループを含んでいる' do
209       r = License.show_opt[:include]
210       r.has_key?(:license_group).should be_true
211     end
212   end
213   describe 'json単体出力オプションに於いて' do
214     it 'includeキーを含んでいる' do
215       r = License.show_json_opt
216       r.has_key?(:include).should be_true
217     end
218     it '1つの項目を含んでいる' do
219       r = License.show_json_opt[:include]
220       r.should have(1).items
221     end
222     it 'ライセンスグループを含んでいる' do
223       r = License.show_json_opt[:include]
224       r.has_key?(:license_group).should be_true
225     end
226   end
227   
228   describe '更新に於いて' do
229     before do
230       @lg = FactoryGirl.create :license_group
231       @f = Rails.root + 'spec/json/license_group.json'
232       @t = File.open(@f, 'r').read
233       @j = JSON.parse @t
234       @n = @j.keys.first
235       @a = @j.values.first
236       @attr = @a["licenses_attributes"]
237       @ln = @attr.keys.first
238       @la = @attr.values.first
239       @la["license_group_id"] = @lg.id
240     end
241     context 'つつがなく終わるとき' do
242       it 'システム画像置換を依頼する' do
243         License.stub(:replace_system_picture).with(any_args).and_return(true)
244         License.should_receive(:replace_system_picture).with(any_args).exactly(1)
245         License.stub(:modify_object).with(any_args).and_return(License.new)
246         License.any_instance.stub(:save).with(any_args).and_return(true)
247         r = License.store(@ln, @la)
248       end
249       it 'データ更新準備を依頼する' do
250         License.stub(:replace_system_picture).with(any_args).and_return(true)
251         License.stub(:modify_object).with(any_args).and_return(License.new)
252         License.should_receive(:modify_object).with(any_args).exactly(1)
253         License.any_instance.stub(:save).with(any_args).and_return(true)
254         r = License.store(@ln, @la)
255       end
256       it '保存を依頼する' do
257         License.stub(:replace_system_picture).with(any_args).and_return(true)
258         License.stub(:modify_object).with(any_args).and_return(License.new)
259         License.any_instance.stub(:save).with(any_args).and_return(true)
260         License.any_instance.should_receive(:save).with(any_args).exactly(1)
261         r = License.store(@ln, @la)
262       end
263       it 'オブジェクトを返す' do
264         r = License.store(@ln, @la)
265         r.is_a?(License).should be_true
266         r.name.should eq @ln
267         r.url.should eq @la["url"]
268       end
269       it 'ライセンスが作成されている' do
270         lambda {
271           r = License.store(@ln, @la)
272         }.should change License, :count
273       end
274       it 'システム画像が作成されている' do
275         lambda {
276           r = License.store(@ln, @la)
277         }.should change SystemPicture, :count
278       end
279     end
280     context 'システム画像置換が失敗するとき' do
281       before do
282         License.stub(:replace_system_picture).with(any_args).and_return(false)
283         License.stub(:modify_object).with(any_args).and_return(License.new)
284       end
285       it '全体エラーメッセージがセットされている' do
286         r = License.store(@ln, @la)
287         r.errors[:base].should_not be_blank
288       end
289       it 'ライセンスが作成されていない' do
290         lambda {
291           r = License.store(@ln, @la)
292         }.should_not change License, :count
293       end
294     end
295   end
296   
297   describe '複数の更新に於いて' do
298     before do
299       @lg = FactoryGirl.create :license_group
300       @fs = Rails.root + 'spec/json/license_groups.json'
301       @ts = File.open(@fs, 'r').read
302       @js = JSON.parse @ts
303       @n = @js.keys.last
304       @a = @js.values.last
305       @attr = @a["licenses_attributes"]
306     end
307     context '2件データでつつがなく終わるとき' do
308       it '更新を2回依頼する' do
309         License.stub(:store).with(any_args).and_return(License.new)
310         License.should_receive(:store).with(any_args).exactly(2)
311         License.any_instance.stub(:valid?).with(any_args).and_return(true)
312         r = License.stores(@attr, @lg.id)
313       end
314       it '失敗件数0を返す' do
315         License.stub(:store).with(any_args).and_return(License.new)
316         License.any_instance.stub(:valid?).with(any_args).and_return(true)
317         r = License.stores(@attr, @lg.id)
318         r.should eq 0
319       end
320     end
321     context '2件データで失敗するとき' do
322       it '更新を2回依頼する' do
323         License.stub(:store).with(any_args).and_return(License.new)
324         License.should_receive(:store).with(any_args).exactly(2)
325         License.any_instance.stub(:valid?).with(any_args).and_return(false)
326         r = License.stores(@attr, @lg.id)
327       end
328       it '失敗件数2を返す' do
329         License.stub(:store).with(any_args).and_return(License.new)
330         License.any_instance.stub(:valid?).with(any_args).and_return(false)
331         r = License.stores(@attr, @lg.id)
332         r.should eq 2
333       end
334     end
335     context 'attrsがnilなどのHashでないとき' do
336       it '処理にかけず0を返す' do
337         r = License.stores(nil, @lg.id)
338         r.should eq 0
339       end
340     end
341   end
342   
343 end