OSDN Git Service

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