OSDN Git Service

Merge branch 'v06' of git.sourceforge.jp:/gitroot/pettanr/pettanr into v06
[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     SpeechBalloonTemplate.delete_all
8     #テストデータを用意してね
9     @f = Rails.root + 'spec/json/license_group.json'
10     @t = File.open(@f, 'r').read
11     @j = JSON.parse @t
12     @fs = Rails.root + 'spec/json/license_groups.json'
13     @fes = Rails.root + 'spec/json/invalid_license_groups.json'
14   end
15   describe '検証に於いて' do
16     before do
17       @lg = FactoryGirl.build :license_group
18     end
19     
20     context 'オーソドックスなデータのとき' do
21       it '下限データが通る' do
22         @lg.name = 'a'
23         @lg.classname = 'a'
24         @lg.caption = 'a'
25         @lg.url = 'http://test.jp/'
26         @lg.should be_valid
27       end
28       it '上限データが通る' do
29         @lg.name = 'a'*50
30         @lg.classname = 'a'*50
31         @lg.caption = 'a'*30
32         @lg.url = 'http://test.jp/aaaaa' + 'a' * 180
33         @lg.should be_valid
34       end
35     end
36     
37     context 'nameを検証するとき' do
38       it 'nullなら失敗する' do
39         @lg.name = ''
40         @lg.should_not be_valid
41       end
42       it '51文字以上なら失敗する' do
43         @lg.name = 'a'*51
44         @lg.should_not be_valid
45       end
46       it '重複していたら失敗する' do
47         l = FactoryGirl.create :license_group
48         @lg.should_not be_valid
49       end
50     end
51     context 'classnameを検証するとき' do
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       it 'nullなら失敗する' do
63         @lg.caption = ''
64         @lg.should_not be_valid
65       end
66       it '51文字以上なら失敗する' do
67         @lg.caption = 'a'*51
68         @lg.should_not be_valid
69       end
70     end
71     context 'urlを検証するとき' do
72       it 'nullなら失敗する' do
73         @lg.url = ''
74         @lg.should_not be_valid
75       end
76       it '201文字以上なら失敗する' do
77         @lg.url = 'a'*201
78         @lg.should_not be_valid
79       end
80       it 'url形式でないなら失敗する' do
81         @lg.url = 'aaaaaaa'
82         @lg.should_not be_valid
83       end
84     end
85   end
86   
87   describe '文字コード検証に於いて' do
88     before do
89       @lg = FactoryGirl.build :license_group
90     end
91     
92     context 'nameを検証するとき' do
93       it 'Shift JISなら失敗する' do
94         @lg.name = "\x83G\x83r\x83]\x83D"
95         lambda{
96           @lg.valid_encode
97         }.should raise_error(Pettanr::BadRequest)
98       end
99     end
100     
101     context 'classnameを検証するとき' do
102       it 'Shift JISなら失敗する' do
103         @lg.classname = "\x83G\x83r\x83]\x83D"
104         lambda{
105           @lg.valid_encode
106         }.should raise_error(Pettanr::BadRequest)
107       end
108     end
109     
110     context 'captionを検証するとき' do
111       it 'Shift JISなら失敗する' do
112         @lg.caption = "\x83G\x83r\x83]\x83D"
113         lambda{
114           @lg.valid_encode
115         }.should raise_error(Pettanr::BadRequest)
116       end
117     end
118     
119     context 'urlを検証するとき' do
120       it 'Shift JISなら失敗する' do
121         @lg.url = "\x83G\x83r\x83]\x83D"
122         lambda{
123           @lg.valid_encode
124         }.should raise_error(Pettanr::BadRequest)
125       end
126     end
127   end
128   
129   describe 'デフォルト値補充に於いて' do
130     it 'defined' do
131       @lg = FactoryGirl.build :license_group, :name => "1"
132       @lg.supply_default
133     end
134   end
135   
136   describe '上書き補充に於いて' do
137     it 'defined' do
138       @lg = FactoryGirl.build :license_group, :name => "1"
139       @lg.overwrite
140     end
141   end
142   
143   describe '閲覧許可に於いて' do
144     #ライセンスグループは作家作成する前から存在するので、閲覧制限の意味がない
145   end
146   
147   describe '一覧取得に於いて' do
148     before do
149       @lg = FactoryGirl.create :license_group, :name => "1"
150     end
151     context 'つつがなく終わるとき' do
152       it '一覧取得オプションを利用している' do
153         LicenseGroup.stub(:list_opt).with(any_args).and_return({})
154         LicenseGroup.should_receive(:list_opt).with(any_args).exactly(1)
155         r = LicenseGroup.list
156       end
157     end
158     it 'リストを返す' do
159       l = LicenseGroup.list
160       l.should eq [@lg]
161     end
162     it '名前順で並んでいる' do
163       @lg2 = FactoryGirl.create :license_group, :name => "5", :url => 'http://test.ptn/10'
164       l = LicenseGroup.list
165       l.should eq [@lg, @lg2]
166     end
167   end
168   describe '一覧取得オプションに於いて' do
169     it 'includeキーを含んでいる' do
170       r = LicenseGroup.list_opt
171       r.has_key?(:include).should be_true
172     end
173     it '1つの項目を含んでいる' do
174       r = LicenseGroup.list_opt[:include]
175       r.should have(1).items
176     end
177     it 'ライセンスを含んでいる' do
178       r = LicenseGroup.list_opt[:include]
179       r.has_key?(:licenses).should be_true
180     end
181   end
182   describe 'json一覧出力オプションに於いて' do
183     before do
184       @sp = FactoryGirl.create :system_picture
185       @lg = FactoryGirl.create :license_group
186       @license = FactoryGirl.create :license, :license_group_id => @lg.id, :system_picture_id => @sp.id
187       @user = FactoryGirl.create( :user_yas)
188       @author = FactoryGirl.create :author, :user_id => @user.id
189       @artist = FactoryGirl.create :artist_yas, :author_id => @author.id
190       @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
191       @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id
192       @rp = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => @op.id, :picture_id => @p.id
193       @sbt = FactoryGirl.create :speech_balloon_template
194       @scroll = FactoryGirl.create :scroll, :author_id => @author.id, :visible => 1
195       @panel = FactoryGirl.create :panel, :author_id => @author.id, :publish => 1
196       @scroll_panel = FactoryGirl.create :scroll_panel, :author_id => @author.id, :scroll_id => @scroll.id, :panel_id => @panel.id
197     end
198     it 'ライセンスを含んでいる' do
199       r = LicenseGroup.list.to_json LicenseGroup.list_json_opt
200       j = JSON.parse r
201       i = j.first
202       i.has_key?('licenses').should be_true
203     end
204   end
205   
206   describe '単体取得に於いて' do
207     before do
208       @lg = FactoryGirl.create :license_group
209     end
210     context 'つつがなく終わるとき' do
211       it '単体取得オプションを利用している' do
212         LicenseGroup.stub(:show_opt).with(any_args).and_return({})
213         LicenseGroup.should_receive(:show_opt).with(any_args).exactly(1)
214         r = LicenseGroup.show @lg.id
215       end
216     end
217     it '指定のコマを返す' do
218       l = LicenseGroup.show @lg.id
219       l.should eq @lg
220     end
221     context '存在しないライセンスグループを開こうとしたとき' do
222       it '404RecordNotFound例外を返す' do
223         lambda{
224           LicenseGroup.show 110
225         }.should raise_error(ActiveRecord::RecordNotFound)
226       end
227     end
228   end
229   describe '単体取得オプションに於いて' do
230     it 'includeキーを含んでいる' do
231       r = LicenseGroup.show_opt
232       r.has_key?(:include).should be_true
233     end
234     it '1つの項目を含んでいる' do
235       r = LicenseGroup.show_opt[:include]
236       r.should have(1).items
237     end
238     it 'ライセンスを含んでいる' do
239       r = LicenseGroup.show_opt[:include]
240       r.has_key?(:licenses).should be_true
241     end
242   end
243   describe 'json単体出力オプションに於いて' do
244     before do
245       @sp = FactoryGirl.create :system_picture
246       @lg = FactoryGirl.create :license_group
247       @license = FactoryGirl.create :license, :license_group_id => @lg.id, :system_picture_id => @sp.id
248       @user = FactoryGirl.create( :user_yas)
249       @author = FactoryGirl.create :author, :user_id => @user.id
250       @artist = FactoryGirl.create :artist_yas, :author_id => @author.id
251       @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
252       @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id
253       @rp = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => @op.id, :picture_id => @p.id
254       @sbt = FactoryGirl.create :speech_balloon_template
255       @scroll = FactoryGirl.create :scroll, :author_id => @author.id, :visible => 1
256       @panel = FactoryGirl.create :panel, :author_id => @author.id, :publish => 1
257       @scroll_panel = FactoryGirl.create :scroll_panel, :author_id => @author.id, :scroll_id => @scroll.id, :panel_id => @panel.id
258     end
259     it 'ライセンスを含んでいる' do
260       r = LicenseGroup.show(@lg.id).to_json LicenseGroup.show_json_opt
261       j = JSON.parse r
262       i = j
263       i.has_key?('licenses').should be_true
264     end
265   end
266   
267   describe '更新に於いて' do
268     before do
269       @n = @j.keys.first
270       @a = @j.values.first
271       @imager = ImagerTest.load("abc\ndef\nghi")
272       PettanImager.stub(:load).with(any_args).and_return(@imager)
273     end
274     context 'つつがなく終わるとき' do
275       it 'データ更新準備を依頼する' do
276         LicenseGroup.stub(:modify_object).with(any_args).and_return(LicenseGroup.new)
277         LicenseGroup.should_receive(:modify_object).with(any_args).exactly(1)
278         License.stub(:stores).with(any_args).and_return(0)
279         LicenseGroup.any_instance.stub(:save).with(any_args).and_return(true)
280         r = LicenseGroup.store(@n, @a)
281       end
282       it 'ライセンスに複数の更新を依頼する' do
283         LicenseGroup.stub(:modify_object).with(any_args).and_return(LicenseGroup.new)
284         License.stub(:stores).with(any_args).and_return(0)
285         License.should_receive(:stores).with(any_args).exactly(1)
286         LicenseGroup.any_instance.stub(:save).with(any_args).and_return(true)
287         r = LicenseGroup.store(@n, @a)
288       end
289       it '保存を依頼する' do
290         LicenseGroup.stub(:modify_object).with(any_args).and_return(LicenseGroup.new)
291         License.stub(:stores).with(any_args).and_return(0)
292         LicenseGroup.any_instance.stub(:save).with(any_args).and_return(true)
293         LicenseGroup.any_instance.should_receive(:save).with(any_args).exactly(1)
294         r = LicenseGroup.store(@n, @a)
295       end
296       it 'オブジェクトを返す' do
297         r = LicenseGroup.store(@n, @a)
298         r.is_a?(LicenseGroup).should be_true
299         r.name.should eq @n
300         r.url.should eq @a["url"]
301       end
302       it 'カラム値からlicenses_attributesが削除されている' do
303         @a["licenses_attributes"].should_not be_nil
304         r = LicenseGroup.store(@n, @a)
305         @a["licenses_attributes"].should be_nil
306       end
307     end
308     context 'ライセンス複数更新が失敗するとき' do
309       before do
310         LicenseGroup.stub(:modify_object).with(any_args).and_return(LicenseGroup.new)
311         License.stub(:stores).with(any_args).and_return(1)
312         LicenseGroup.any_instance.stub(:save).with(any_args).and_return(true)
313       end
314       it '全体エラーメッセージがセットされている' do
315         r = LicenseGroup.store(@n, @a)
316         r.errors[:base].should_not be_blank
317       end
318       it 'ライセンスが作成されていない' do
319         lambda {
320           r = LicenseGroup.store(@n, @a)
321         }.should_not change License, :count
322       end
323       it 'ライセンスグループが作成されていない' do
324         lambda {
325           r = LicenseGroup.store(@n, @a)
326         }.should_not change LicenseGroup, :count
327       end
328     end
329   end
330   
331   describe 'インポートに於いて' do
332     before do
333     end
334     context 'つつがなく終わるとき' do
335       it 'ファイルインポートを依頼する' do
336         LicenseGroup.should_receive(:import_file).with(any_args).exactly(1)
337         LicenseGroup.stub(:import_file).with(any_args).and_return([])
338         LicenseGroup.import(@f)
339       end
340       it 'ライセンスグループ更新を一回依頼する' do
341         LicenseGroup.stub(:store).with(any_args).and_return(LicenseGroup.new)
342         LicenseGroup.should_receive(:store).with(any_args).exactly(1)
343         LicenseGroup.import(@f)
344       end
345       it 'ライセンスグループが追加される' do
346         lambda {
347           LicenseGroup.import(@f)
348         }.should change LicenseGroup, :count
349       end
350       it 'ライセンスが追加される' do
351         lambda {
352           LicenseGroup.import(@f)
353         }.should change License, :count
354       end
355       it '[]を返す' do
356         r = LicenseGroup.import(@f)
357         r.should eq []
358       end
359     end
360     context '複数データがつつがなく終わるとき' do
361       it 'ライセンスグループ更新を二回依頼する' do
362         LicenseGroup.stub(:store).with(any_args).and_return(LicenseGroup.new)
363         LicenseGroup.should_receive(:store).with(any_args).exactly(2)
364         LicenseGroup.import(@fs)
365       end
366       it 'ライセンスグループが二個追加される' do
367         lambda {
368           r = LicenseGroup.import(@fs)
369         }.should change(LicenseGroup, :count).by 2
370       end
371       it 'ライセンスが追加される' do
372         lambda {
373           r = LicenseGroup.import(@fs)
374         }.should change(License, :count)
375       end
376       it '[]を返す' do
377         r = LicenseGroup.import(@fs)
378         r.should eq []
379       end
380     end
381     context 'ライセンスグループ作成に失敗したとき' do
382       before do
383         LicenseGroup.any_instance.stub(:save).with(any_args).and_return(false)
384         LicenseGroup.any_instance.stub(:valid?).with(any_args).and_return(false)
385       end
386       it 'ライセンスグループの数に変化がない' do
387         lambda {
388           LicenseGroup.import(@f)
389         }.should_not change LicenseGroup, :count
390       end
391       it '配列を返す' do
392         r = LicenseGroup.import(@f)
393         r.is_a?(Array).should be_true
394       end
395       it '配列の中身は一件' do
396         r = LicenseGroup.import(@f)
397         r.should have(1).items
398       end
399       it 'ライセンスグループオブジェクトが入っている' do
400         r = LicenseGroup.import(@f)
401         r.first.is_a?(LicenseGroup).should be_true
402       end
403     end
404     context '複数のライセンスグループ作成に失敗したとき' do
405       #三件中、二件の失敗、一件を成功させ、成功データは戻り値に含まないことを確認する
406       it 'ライセンスグループの数に変化がない' do
407         lambda {
408           LicenseGroup.import(@fes)
409         }.should_not change LicenseGroup, :count
410       end
411       it '途中で保存に失敗しても全件更新依頼する' do
412         LicenseGroup.stub(:store).with(any_args).and_return(LicenseGroup.new)
413         LicenseGroup.should_receive(:store).with(any_args).exactly(3)
414         LicenseGroup.import(@fes)
415       end
416       it '配列を返す' do
417         r = LicenseGroup.import(@fes)
418         r.is_a?(Array).should be_true
419       end
420       it '配列の中身は2件' do
421         r = LicenseGroup.import(@fes)
422         r.should have(2).items
423       end
424       it '配列の中身は失敗したライセンスグループオブジェクトが入っている' do
425         r = LicenseGroup.import(@fes)
426         r[0].is_a?(LicenseGroup).should be_true
427         r[0]["name"].should eq 'UnknownUrl'
428         r[1].is_a?(LicenseGroup).should be_true
429         r[1]["name"].should eq 'UnknownClassname'
430       end
431     end
432   end
433   
434 end