OSDN Git Service

Merge branch 'v05' of git.sourceforge.jp:/gitroot/pettanr/pettanr into v05
[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
36         @l.license_group_id = 'a'
37         @l.should_not be_valid
38       end
39       it '存在するライセンスグループでなければ失敗する' do
40         @l.license_group_id = 0
41         @l.should_not be_valid
42       end
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
79         @l.should_not be_valid
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
88         @l.system_picture_id = 'a'
89         @l.should_not be_valid
90       end
91       it '存在するシステム画像でなければ失敗する' do
92         @l.system_picture_id = 0
93         @l.should_not be_valid
94       end
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
128       it '一覧取得オプションを利用している' do
129         License.stub(:list_opt).with(any_args).and_return({})
130         License.should_receive(:list_opt).with(any_args).exactly(1)
131         r = License.list
132       end
133     end
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 '一覧取得オプションに於いて' 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     before do
161       @sp = FactoryGirl.create :system_picture
162       @lg = FactoryGirl.create :license_group
163       @l = FactoryGirl.create :license, :license_group_id => @lg.id, :system_picture_id => @sp.id
164     end
165     it 'ライセンスグループを含んでいる' do
166       r = License.list.to_json License.list_json_opt
167       j = JSON.parse r
168       i = j.first
169       i.has_key?('license_group').should be_true
170     end
171   end
172   
173   describe '単体取得に於いて' do
174     before do
175       @sp = FactoryGirl.create :system_picture
176       @lg = FactoryGirl.create :license_group
177       @l = FactoryGirl.create :license, :license_group_id => @lg.id, :system_picture_id => @sp.id
178     end
179     context 'つつがなく終わるとき' do
180       it '単体取得オプションを利用している' do
181         License.stub(:show_opt).with(any_args).and_return({})
182         License.should_receive(:show_opt).with(any_args).exactly(1)
183         r = License.show @l.id
184       end
185     end
186     it '指定のライセンスを返す' do
187       l = License.show @l.id
188       l.should eq @l
189     end
190     context '存在しないライセンスを開こうとしたとき' do
191       it '404RecordNotFound例外を返す' do
192         lambda{
193           License.show 110
194         }.should raise_error(ActiveRecord::RecordNotFound)
195       end
196     end
197   end
198   describe '単体取得オプションに於いて' do
199     it 'includeキーを含んでいる' do
200       r = License.show_opt
201       r.has_key?(:include).should be_true
202     end
203     it '1つの項目を含んでいる' do
204       r = License.show_opt[:include]
205       r.should have(1).items
206     end
207     it 'ライセンスグループを含んでいる' do
208       r = License.show_opt[:include]
209       r.has_key?(:license_group).should be_true
210     end
211   end
212   describe 'json単体出力オプションに於いて' do
213     before do
214       @sp = FactoryGirl.create :system_picture
215       @lg = FactoryGirl.create :license_group
216       @l = FactoryGirl.create :license, :license_group_id => @lg.id, :system_picture_id => @sp.id
217     end
218     it 'ライセンスグループを含んでいる' do
219       r = License.show(@l.id).to_json License.show_json_opt
220       j = JSON.parse r
221       i = j
222       i.has_key?('license_group').should be_true
223     end
224   end
225   
226   describe '管理名検索に於いて' do
227     before do
228       @sp = FactoryGirl.create :system_picture
229       @lg = FactoryGirl.create :license_group
230       @l = FactoryGirl.create :license, :name => 'peta2.0', :license_group_id => @lg.id, :system_picture_id => @sp.id
231       @lg2 = FactoryGirl.create :license_group, :name => 'pubdm'
232       @l2 = FactoryGirl.create :license, :name => 'pettan2.2b', :url => @l.url + 'l2', :license_group_id => @lg.id, :system_picture_id => @sp.id
233     end
234     context 'つつがなく終わるとき' do
235       it 'リストを返す' do
236         l = License.list_by_name @l2.name
237         l.should eq [@l2]
238       end
239     end
240   end
241   
242   describe '更新に於いて' do
243     before do
244       @lg = FactoryGirl.create :license_group
245       @f = Rails.root + 'spec/json/license_group.json'
246       @t = File.open(@f, 'r').read
247       @j = JSON.parse @t
248       @n = @j.keys.first
249       @a = @j.values.first
250       @attr = @a["licenses_attributes"]
251       @ln = @attr.keys.first
252       @la = @attr.values.first
253       @la["license_group_id"] = @lg.id
254     end
255     context 'つつがなく終わるとき' do
256       it 'システム画像置換を依頼する' do
257         License.stub(:replace_system_picture).with(any_args).and_return(true)
258         License.should_receive(:replace_system_picture).with(any_args).exactly(1)
259         License.stub(:modify_object).with(any_args).and_return(License.new)
260         License.any_instance.stub(:save).with(any_args).and_return(true)
261         r = License.store(@ln, @la)
262       end
263       it 'データ更新準備を依頼する' do
264         License.stub(:replace_system_picture).with(any_args).and_return(true)
265         License.stub(:modify_object).with(any_args).and_return(License.new)
266         License.should_receive(:modify_object).with(any_args).exactly(1)
267         License.any_instance.stub(:save).with(any_args).and_return(true)
268         r = License.store(@ln, @la)
269       end
270       it '保存を依頼する' do
271         License.stub(:replace_system_picture).with(any_args).and_return(true)
272         License.stub(:modify_object).with(any_args).and_return(License.new)
273         License.any_instance.stub(:save).with(any_args).and_return(true)
274         License.any_instance.should_receive(:save).with(any_args).exactly(1)
275         r = License.store(@ln, @la)
276       end
277       it 'オブジェクトを返す' do
278         r = License.store(@ln, @la)
279         r.is_a?(License).should be_true
280         r.name.should eq @ln
281         r.url.should eq @la["url"]
282         cp = JSON.parse r.credit_pictures
283         cp['a_picture_id'].should_not be_nil
284         cp['b_picture_id'].should_not be_nil
285       end
286       it 'ライセンスが作成されている' do
287         lambda {
288           r = License.store(@ln, @la)
289         }.should change License, :count
290       end
291       it 'システム画像が作成されている' do
292         lambda {
293           r = License.store(@ln, @la)
294         }.should change SystemPicture, :count
295       end
296     end
297     context 'システム画像置換が失敗するとき' do
298       before do
299         License.stub(:replace_system_picture).with(any_args).and_return(false)
300         License.stub(:modify_object).with(any_args).and_return(License.new)
301       end
302       it '全体エラーメッセージがセットされている' do
303         r = License.store(@ln, @la)
304         r.errors[:base].should_not be_blank
305       end
306       it 'ライセンスが作成されていない' do
307         lambda {
308           r = License.store(@ln, @la)
309         }.should_not change License, :count
310       end
311     end
312   end
313   
314   describe '複数の更新に於いて' do
315     before do
316       @lg = FactoryGirl.create :license_group
317       @fs = Rails.root + 'spec/json/license_groups.json'
318       @ts = File.open(@fs, 'r').read
319       @js = JSON.parse @ts
320       @n = @js.keys.last
321       @a = @js.values.last
322       @attr = @a["licenses_attributes"]
323     end
324     context '2件データでつつがなく終わるとき' do
325       it '更新を2回依頼する' do
326         License.stub(:store).with(any_args).and_return(License.new)
327         License.should_receive(:store).with(any_args).exactly(2)
328         License.any_instance.stub(:valid?).with(any_args).and_return(true)
329         r = License.stores(@attr, @lg.id)
330       end
331       it '失敗件数0を返す' do
332         License.stub(:store).with(any_args).and_return(License.new)
333         License.any_instance.stub(:valid?).with(any_args).and_return(true)
334         r = License.stores(@attr, @lg.id)
335         r.should eq 0
336       end
337     end
338     context '2件データで失敗するとき' do
339       it '更新を2回依頼する' do
340         License.stub(:store).with(any_args).and_return(License.new)
341         License.should_receive(:store).with(any_args).exactly(2)
342         License.any_instance.stub(:valid?).with(any_args).and_return(false)
343         r = License.stores(@attr, @lg.id)
344       end
345       it '失敗件数2を返す' do
346         License.stub(:store).with(any_args).and_return(License.new)
347         License.any_instance.stub(:valid?).with(any_args).and_return(false)
348         r = License.stores(@attr, @lg.id)
349         r.should eq 2
350       end
351     end
352     context 'attrsがnilなどのHashでないとき' do
353       it '処理にかけず0を返す' do
354         r = License.stores(nil, @lg.id)
355         r.should eq 0
356       end
357     end
358   end
359   
360   describe 'エクスポートに於いて' do
361     before do
362       @sp = FactoryGirl.create :system_picture
363       @lg = FactoryGirl.create :license_group
364       @l = FactoryGirl.create :license, :license_group_id => @lg.id, :system_picture_id => @sp.id
365       @l2 = FactoryGirl.create :license, :name => 'l2[old ls]', :url => @l.url+'2', :license_group_id => @lg.id, :system_picture_id => @sp.id, :updated_at => Time.now - 3000
366     end
367     it '開始日時が省略された場合はすべてのライセンスを返す' do
368       r = License.export 
369       r.should eq [@l, @l2]
370     end
371     it '開始日時以降に更新されたライセンスを返す' do
372       r = License.export @l.updated_at - 100
373       r.should eq [@l]
374     end
375   end
376   
377 end