1 # -*- encoding: utf-8 -*-
5 describe LicenseGroup do
8 @f = Rails.root + 'spec/json/license_group.json'
9 @t = File.open(@f, 'r').read
11 @fs = Rails.root + 'spec/json/license_groups.json'
12 @fes = Rails.root + 'spec/json/invalid_license_groups.json'
16 @lg = FactoryGirl.build :license_group
19 context 'オーソドックスなデータのとき' do
24 @lg.url = 'http://test.jp/'
29 @lg.classname = 'a'*50
31 @lg.url = 'http://test.jp/aaaaa' + 'a' * 180
36 context 'nameを検証するとき' do
39 @lg.should_not be_valid
43 @lg.should_not be_valid
46 l = FactoryGirl.create :license_group
47 @lg.should_not be_valid
50 context 'classnameを検証するとき' do
53 @lg.should_not be_valid
56 @lg.classname = 'a'*51
57 @lg.should_not be_valid
60 context 'captionを検証するとき' do
63 @lg.should_not be_valid
67 @lg.should_not be_valid
70 context 'urlを検証するとき' do
73 @lg.should_not be_valid
77 @lg.should_not be_valid
79 it 'url形式でないなら失敗する' do
81 @lg.should_not be_valid
86 describe 'デフォルト値補充に於いて' do
88 @lg = FactoryGirl.build :license_group, :name => "1"
93 describe '上書き補充に於いて' do
95 @lg = FactoryGirl.build :license_group, :name => "1"
100 describe '閲覧許可に於いて' do
101 #ライセンスグループは作家作成する前から存在するので、閲覧制限の意味がない
104 describe '一覧取得に於いて' do
106 @lg = FactoryGirl.create :license_group, :name => "1"
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
116 l = LicenseGroup.list
120 @lg2 = FactoryGirl.create :license_group, :name => "5", :url => 'http://test.ptn/10'
121 l = LicenseGroup.list
122 l.should eq [@lg, @lg2]
125 describe '一覧取得オプションに於いて' do
126 it 'includeキーを含んでいる' do
127 r = LicenseGroup.list_opt
128 r.has_key?(:include).should be_true
131 r = LicenseGroup.list_opt[:include]
132 r.should have(1).items
135 r = LicenseGroup.list_opt[:include]
136 r.has_key?(:licenses).should be_true
139 describe 'json一覧出力オプションに於いて' 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
156 r = LicenseGroup.list.to_json LicenseGroup.list_json_opt
159 i.has_key?('licenses').should be_true
163 describe '単体取得に於いて' do
165 @lg = FactoryGirl.create :license_group
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
175 l = LicenseGroup.show @lg.id
178 context '存在しないライセンスグループを開こうとしたとき' do
\r
179 it '404RecordNotFound例外を返す' do
\r
181 LicenseGroup.show 110
\r
182 }.should raise_error(ActiveRecord::RecordNotFound)
\r
186 describe '単体取得オプションに於いて' do
187 it 'includeキーを含んでいる' do
188 r = LicenseGroup.show_opt
189 r.has_key?(:include).should be_true
192 r = LicenseGroup.show_opt[:include]
193 r.should have(1).items
196 r = LicenseGroup.show_opt[:include]
197 r.has_key?(:licenses).should be_true
200 describe 'json単体出力オプションに於いて' 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
217 r = LicenseGroup.show(@lg.id).to_json LicenseGroup.show_json_opt
220 i.has_key?('licenses').should be_true
228 @imager = ImagerTest.load("abc\ndef\nghi")
229 PettanImager.stub(:load).with(any_args).and_return(@imager)
231 context 'つつがなく終わるとき' 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)
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)
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)
254 r = LicenseGroup.store(@n, @a)
255 r.is_a?(LicenseGroup).should be_true
257 r.url.should eq @a["url"]
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
265 context 'ライセンス複数更新が失敗するとき' 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)
271 it '全体エラーメッセージがセットされている' do
272 r = LicenseGroup.store(@n, @a)
273 r.errors[:base].should_not be_blank
275 it 'ライセンスが作成されていない' do
277 r = LicenseGroup.store(@n, @a)
278 }.should_not change License, :count
280 it 'ライセンスグループが作成されていない' do
282 r = LicenseGroup.store(@n, @a)
283 }.should_not change LicenseGroup, :count
288 describe 'インポートに於いて' do
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)
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)
302 it 'ライセンスグループが追加される' do
304 LicenseGroup.import(@f)
305 }.should change LicenseGroup, :count
309 LicenseGroup.import(@f)
310 }.should change License, :count
313 r = LicenseGroup.import(@f)
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)
323 it 'ライセンスグループが二個追加される' do
325 r = LicenseGroup.import(@fs)
326 }.should change(LicenseGroup, :count).by 2
330 r = LicenseGroup.import(@fs)
331 }.should change(License, :count)
334 r = LicenseGroup.import(@fs)
338 context 'ライセンスグループ作成に失敗したとき' 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)
343 it 'ライセンスグループの数に変化がない' do
345 LicenseGroup.import(@f)
346 }.should_not change LicenseGroup, :count
349 r = LicenseGroup.import(@f)
350 r.is_a?(Array).should be_true
353 r = LicenseGroup.import(@f)
354 r.should have(1).items
356 it 'ライセンスグループオブジェクトが入っている' do
357 r = LicenseGroup.import(@f)
358 r.first.is_a?(LicenseGroup).should be_true
361 context '複数のライセンスグループ作成に失敗したとき' do
362 #三件中、二件の失敗、一件を成功させ、成功データは戻り値に含まないことを確認する
363 it 'ライセンスグループの数に変化がない' do
365 LicenseGroup.import(@fes)
366 }.should_not change LicenseGroup, :count
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)
374 r = LicenseGroup.import(@fes)
375 r.is_a?(Array).should be_true
378 r = LicenseGroup.import(@fes)
379 r.should have(2).items
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'