OSDN Git Service

t#29620:json output include child
[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 '一覧取得オプションに於いて' 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     before do
141       @sp = FactoryGirl.create :system_picture
142       @lg = FactoryGirl.create :license_group
143       @license = FactoryGirl.create :license, :license_group_id => @lg.id, :system_picture_id => @sp.id
144       @user = FactoryGirl.create( :user_yas)
145       @author = @user.author
146       @artist = FactoryGirl.create :artist_yas, :author_id => @author.id
147       @op = FactoryGirl.create :original_picture, :artist_id => @artist.id\r
148       @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id\r
149       @rp = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => @op.id, :picture_id => @p.id\r
150       @sbt = FactoryGirl.create :speech_balloon_template\r
151       @comic = FactoryGirl.create :comic, :author_id => @author.id, :visible => 1
152       @panel = FactoryGirl.create :panel, :author_id => @author.id, :publish => 1
153       @story = FactoryGirl.create :story, :author_id => @author.id, :comic_id => @comic.id, :panel_id => @panel.id
154     end
155     it 'ライセンスを含んでいる' do
156       r = LicenseGroup.list.to_json LicenseGroup.list_json_opt
157       j = JSON.parse r
158       i = j.first
159       i.has_key?('licenses').should be_true
160     end
161   end
162   
163   describe '単体取得に於いて' do
164     before do
165       @lg = FactoryGirl.create :license_group
166     end
167     context 'つつがなく終わるとき' do\r
168       it '単体取得オプションを利用している' do\r
169         LicenseGroup.stub(:show_opt).with(any_args).and_return({})\r
170         LicenseGroup.should_receive(:show_opt).with(any_args).exactly(1)\r
171         r = LicenseGroup.show @lg.id
172       end\r
173     end\r
174     it '指定のコマを返す' do
175       l = LicenseGroup.show @lg.id
176       l.should eq @lg
177     end
178     context '存在しないライセンスグループを開こうとしたとき' do\r
179       it '404RecordNotFound例外を返す' do\r
180         lambda{\r
181           LicenseGroup.show 110\r
182         }.should raise_error(ActiveRecord::RecordNotFound)\r
183       end\r
184     end\r
185   end
186   describe '単体取得オプションに於いて' do
187     it 'includeキーを含んでいる' do
188       r = LicenseGroup.show_opt
189       r.has_key?(:include).should be_true
190     end
191     it '1つの項目を含んでいる' do
192       r = LicenseGroup.show_opt[:include]
193       r.should have(1).items
194     end
195     it 'ライセンスを含んでいる' do
196       r = LicenseGroup.show_opt[:include]
197       r.has_key?(:licenses).should be_true
198     end
199   end
200   describe 'json単体出力オプションに於いて' do
201     before do
202       @sp = FactoryGirl.create :system_picture
203       @lg = FactoryGirl.create :license_group
204       @license = FactoryGirl.create :license, :license_group_id => @lg.id, :system_picture_id => @sp.id
205       @user = FactoryGirl.create( :user_yas)
206       @author = @user.author
207       @artist = FactoryGirl.create :artist_yas, :author_id => @author.id
208       @op = FactoryGirl.create :original_picture, :artist_id => @artist.id\r
209       @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id\r
210       @rp = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => @op.id, :picture_id => @p.id\r
211       @sbt = FactoryGirl.create :speech_balloon_template\r
212       @comic = FactoryGirl.create :comic, :author_id => @author.id, :visible => 1
213       @panel = FactoryGirl.create :panel, :author_id => @author.id, :publish => 1
214       @story = FactoryGirl.create :story, :author_id => @author.id, :comic_id => @comic.id, :panel_id => @panel.id
215     end
216     it 'ライセンスを含んでいる' do
217       r = LicenseGroup.show(@lg.id).to_json LicenseGroup.show_json_opt
218       j = JSON.parse r
219       i = j
220       i.has_key?('licenses').should be_true
221     end
222   end
223   
224   describe '更新に於いて' do
225     before do
226       @n = @j.keys.first
227       @a = @j.values.first
228       @imager = ImagerTest.load("abc\ndef\nghi")
229       PettanImager.stub(:load).with(any_args).and_return(@imager)
230     end
231     context 'つつがなく終わるとき' do
232       it 'データ更新準備を依頼する' do
233         LicenseGroup.stub(:modify_object).with(any_args).and_return(LicenseGroup.new)
234         LicenseGroup.should_receive(:modify_object).with(any_args).exactly(1)
235         License.stub(:stores).with(any_args).and_return(0)
236         LicenseGroup.any_instance.stub(:save).with(any_args).and_return(true)
237         r = LicenseGroup.store(@n, @a)
238       end
239       it 'ライセンスに複数の更新を依頼する' do
240         LicenseGroup.stub(:modify_object).with(any_args).and_return(LicenseGroup.new)
241         License.stub(:stores).with(any_args).and_return(0)
242         License.should_receive(:stores).with(any_args).exactly(1)
243         LicenseGroup.any_instance.stub(:save).with(any_args).and_return(true)
244         r = LicenseGroup.store(@n, @a)
245       end
246       it '保存を依頼する' do
247         LicenseGroup.stub(:modify_object).with(any_args).and_return(LicenseGroup.new)
248         License.stub(:stores).with(any_args).and_return(0)
249         LicenseGroup.any_instance.stub(:save).with(any_args).and_return(true)
250         LicenseGroup.any_instance.should_receive(:save).with(any_args).exactly(1)
251         r = LicenseGroup.store(@n, @a)
252       end
253       it 'オブジェクトを返す' do
254         r = LicenseGroup.store(@n, @a)
255         r.is_a?(LicenseGroup).should be_true
256         r.name.should eq @n
257         r.url.should eq @a["url"]
258       end
259       it 'カラム値からlicenses_attributesが削除されている' do
260         @a["licenses_attributes"].should_not be_nil
261         r = LicenseGroup.store(@n, @a)
262         @a["licenses_attributes"].should be_nil
263       end
264     end
265     context 'ライセンス複数更新が失敗するとき' do
266       before do
267         LicenseGroup.stub(:modify_object).with(any_args).and_return(LicenseGroup.new)
268         License.stub(:stores).with(any_args).and_return(1)
269         LicenseGroup.any_instance.stub(:save).with(any_args).and_return(true)
270       end
271       it '全体エラーメッセージがセットされている' do
272         r = LicenseGroup.store(@n, @a)
273         r.errors[:base].should_not be_blank
274       end
275       it 'ライセンスが作成されていない' do
276         lambda {
277           r = LicenseGroup.store(@n, @a)
278         }.should_not change License, :count
279       end
280       it 'ライセンスグループが作成されていない' do
281         lambda {
282           r = LicenseGroup.store(@n, @a)
283         }.should_not change LicenseGroup, :count
284       end
285     end
286   end
287   
288   describe 'インポートに於いて' do
289     before do
290     end
291     context 'つつがなく終わるとき' do
292       it 'ファイルインポートを依頼する' do
293         LicenseGroup.should_receive(:import_file).with(any_args).exactly(1)
294         LicenseGroup.stub(:import_file).with(any_args).and_return([])
295         LicenseGroup.import(@f)
296       end
297       it 'ライセンスグループ更新を一回依頼する' do
298         LicenseGroup.stub(:store).with(any_args).and_return(LicenseGroup.new)
299         LicenseGroup.should_receive(:store).with(any_args).exactly(1)
300         LicenseGroup.import(@f)
301       end
302       it 'ライセンスグループが追加される' do
303         lambda {
304           LicenseGroup.import(@f)
305         }.should change LicenseGroup, :count
306       end
307       it 'ライセンスが追加される' do
308         lambda {
309           LicenseGroup.import(@f)
310         }.should change License, :count
311       end
312       it '[]を返す' do
313         r = LicenseGroup.import(@f)
314         r.should eq []
315       end
316     end
317     context '複数データがつつがなく終わるとき' do
318       it 'ライセンスグループ更新を二回依頼する' do
319         LicenseGroup.stub(:store).with(any_args).and_return(LicenseGroup.new)
320         LicenseGroup.should_receive(:store).with(any_args).exactly(2)
321         LicenseGroup.import(@fs)
322       end
323       it 'ライセンスグループが二個追加される' do
324         lambda {
325           r = LicenseGroup.import(@fs)
326         }.should change(LicenseGroup, :count).by 2
327       end
328       it 'ライセンスが追加される' do
329         lambda {
330           r = LicenseGroup.import(@fs)
331         }.should change(License, :count)
332       end
333       it '[]を返す' do
334         r = LicenseGroup.import(@fs)
335         r.should eq []
336       end
337     end
338     context 'ライセンスグループ作成に失敗したとき' do
339       before do
340         LicenseGroup.any_instance.stub(:save).with(any_args).and_return(false)
341         LicenseGroup.any_instance.stub(:valid?).with(any_args).and_return(false)
342       end
343       it 'ライセンスグループの数に変化がない' do
344         lambda {
345           LicenseGroup.import(@f)
346         }.should_not change LicenseGroup, :count
347       end
348       it '配列を返す' do
349         r = LicenseGroup.import(@f)
350         r.is_a?(Array).should be_true
351       end
352       it '配列の中身は一件' do
353         r = LicenseGroup.import(@f)
354         r.should have(1).items
355       end
356       it 'ライセンスグループオブジェクトが入っている' do
357         r = LicenseGroup.import(@f)
358         r.first.is_a?(LicenseGroup).should be_true
359       end
360     end
361     context '複数のライセンスグループ作成に失敗したとき' do
362       #三件中、二件の失敗、一件を成功させ、成功データは戻り値に含まないことを確認する
363       it 'ライセンスグループの数に変化がない' do
364         lambda {
365           LicenseGroup.import(@fes)
366         }.should_not change LicenseGroup, :count
367       end
368       it '途中で保存に失敗しても全件更新依頼する' do
369         LicenseGroup.stub(:store).with(any_args).and_return(LicenseGroup.new)
370         LicenseGroup.should_receive(:store).with(any_args).exactly(3)
371         LicenseGroup.import(@fes)
372       end
373       it '配列を返す' do
374         r = LicenseGroup.import(@fes)
375         r.is_a?(Array).should be_true
376       end
377       it '配列の中身は2件' do
378         r = LicenseGroup.import(@fes)
379         r.should have(2).items
380       end
381       it '配列の中身は失敗したライセンスグループオブジェクトが入っている' do
382         r = LicenseGroup.import(@fes)
383         r[0].is_a?(LicenseGroup).should be_true
384         r[0]["name"].should eq 'UnknownUrl'
385         r[1].is_a?(LicenseGroup).should be_true
386         r[1]["name"].should eq 'UnknownClassname'
387       end
388     end
389   end
390   
391 end