OSDN Git Service

t#29400:itr3?
[pettanr/pettanr.git] / spec / models / resource_picture_spec.rb
1 # -*- encoding: utf-8 -*-\r
2 #素材\r
3 require 'spec_helper'\r
4 \r
5 describe ResourcePicture do\r
6   before do\r
7     FactoryGirl.create :admin\r
8     @user = FactoryGirl.create( :user_yas)\r
9     @author = @user.author\r
10     @artist = FactoryGirl.create :artist_yas, :author_id => @author.id\r
11     @other_user = FactoryGirl.create( :user_yas)\r
12     @other_author = @other_user.author\r
13     @other_artist = FactoryGirl.create :artist_yas, :author_id => @other_author.id\r
14     @sp = FactoryGirl.create :system_picture\r
15     @lg = FactoryGirl.create :license_group\r
16     @license = FactoryGirl.create :license, :license_group_id => @lg.id, :system_picture_id => @sp.id\r
17     @original_picture = FactoryGirl.create :original_picture, :artist_id => @artist.id\r
18     \r
19     #原画ファイル削除だけは必ず成功するものとしておく\r
20     PictureIO::LocalPicture.any_instance.stub(:delete).with(any_args).and_return(true)\r
21     Picture.any_instance.stub(:subdirs).with(any_args).and_return(['', 'v', 'h', 'vh'])\r
22     Picture.any_instance.stub(:h).with(any_args).and_return('data')\r
23     Picture.any_instance.stub(:v).with(any_args).and_return('data')\r
24     Picture.any_instance.stub(:vh).with(any_args).and_return('data')\r
25   end\r
26   \r
27   describe '検証に於いて' do\r
28     before do\r
29       @p = FactoryGirl.create :picture, :original_picture_id => @original_picture.id, :license_id => @license.id, :artist_id => @artist.id\r
30       @rp = FactoryGirl.build :resource_picture, :artist_id => @artist.id, :original_picture_id => @original_picture.id, :license_id => @license.id, :picture_id => @p.id\r
31     end\r
32     \r
33     context 'オーソドックスなデータのとき' do
34       it '下限データが通る' do
35         @rp.ext = 'png' #リストにない拡張子は通らないし
36         @rp.width = 1
37         @rp.height = 1
38         @rp.filesize = 1
39         @rp.md5 = 'a'*32
40         @rp.classname = 'a'*1
41         @rp.should be_valid
42       end
43       it '上限データが通る' do
44         @rp.ext = 'jpeg'
45         @rp.width = 99999
46         @rp.height = 99999
47         @rp.filesize = 2000000
48         @rp.md5 = 'a'*32
49         @rp.classname = 'a'*50
50         @rp.should be_valid
51       end
52     end
53     
54     context 'extを検証するとき' do\r
55       it 'nullなら失敗する' do\r
56         @rp.ext = ''\r
57         @rp.should_not be_valid\r
58       end\r
59       it '5文字以上なら失敗する' do\r
60         @rp.ext = 'a'*5\r
61         @rp.should_not be_valid\r
62       end\r
63       it 'png,gif,jpeg以外なら失敗する' do\r
64         @rp.ext = 'bmp'\r
65         @rp.should_not be_valid\r
66       end\r
67     end\r
68     context 'widthを検証するとき' do\r
69       it 'nullなら失敗する' do\r
70         @rp.width = nil\r
71         @rp.should_not be_valid\r
72       end\r
73       it '数値でなければ失敗する' do\r
74         @rp.width = 'a'\r
75         @rp.should_not be_valid\r
76       end\r
77       it '0なら失敗する' do\r
78         @rp.width = '0'\r
79         @rp.should_not be_valid\r
80       end\r
81       it '負でも失敗する' do\r
82         @rp.width = -1\r
83         @rp.should_not be_valid\r
84       end\r
85     end\r
86     context 'heightを検証するとき' do\r
87       it 'nullなら失敗する' do\r
88         @rp.height = nil\r
89         @rp.should_not be_valid\r
90       end\r
91       it '数値でなければ失敗する' do\r
92         @rp.height = 'a'\r
93         @rp.should_not be_valid\r
94       end\r
95       it '0なら失敗する' do\r
96         @rp.height = '0'\r
97         @rp.should_not be_valid\r
98       end\r
99       it '負でも失敗する' do\r
100         @rp.height = -1\r
101         @rp.should_not be_valid\r
102       end\r
103     end\r
104     context 'filesizeを検証するとき' do\r
105       it 'nullなら失敗する' do\r
106         @rp.filesize = nil\r
107         @rp.should_not be_valid\r
108       end\r
109       it '数値でなければ失敗する' do\r
110         @rp.filesize = 'a'\r
111         @rp.should_not be_valid\r
112       end\r
113       it '負なら失敗する' do\r
114         @rp.filesize = '-1'\r
115         @rp.should_not be_valid\r
116       end\r
117       it '2MB以上なら失敗する' do\r
118         @rp.filesize = 2000000+1\r
119         @rp.should_not be_valid\r
120       end\r
121     end\r
122     context 'artist_idを検証するとき' do\r
123       it 'nullなら失敗する' do\r
124         @rp.artist_id = nil\r
125         @rp.should_not be_valid\r
126       end\r
127       it '数値でなければ失敗する' do\r
128         @rp.artist_id = 'a'\r
129         @rp.should_not be_valid\r
130       end\r
131       it '存在する絵師でなければ失敗する' do\r
132         @rp.artist_id = 0\r
133         @rp.should_not be_valid\r
134       end\r
135     end\r
136     context 'md5を検証するとき' do\r
137       it 'nullなら失敗する' do\r
138         @rp.md5 = ''\r
139         @rp.should_not be_valid\r
140       end\r
141       it '31文字なら失敗する' do
142         @rp.md5 = 'a'*31
143         @rp.should_not be_valid
144       end
145       it '32文字以上なら失敗する' do\r
146         @rp.md5 = 'a'*33\r
147         @rp.should_not be_valid\r
148       end\r
149     end\r
150     context 'license_idを検証するとき' do\r
151       it 'nullなら失敗する' do\r
152         @rp.license_id = nil\r
153         @rp.should_not be_valid\r
154       end\r
155       it '数値でなければ失敗する' do\r
156         @rp.license_id = 'a'\r
157         @rp.should_not be_valid\r
158       end\r
159       it '存在する絵師でなければ失敗する' do\r
160         @rp.license_id = 0\r
161         @rp.should_not be_valid\r
162       end\r
163     end\r
164     context 'original_picture_idを検証するとき' do\r
165       it 'nullなら失敗する' do\r
166         @rp.original_picture_id = nil\r
167         @rp.should_not be_valid\r
168       end\r
169       it '数値でなければ失敗する' do\r
170         @rp.original_picture_id = 'a'\r
171         @rp.should_not be_valid\r
172       end\r
173       it '存在する原画でなければ失敗する' do\r
174         @rp.original_picture_id = 0\r
175         @rp.should_not be_valid\r
176       end\r
177     end\r
178     context 'artist_nameを検証するとき' do\r
179       it 'nullなら失敗する' do\r
180         @rp.artist_name = nil\r
181         @rp.should_not be_valid\r
182       end\r
183     end\r
184     context 'classnameを検証するとき' do\r
185       it 'nullなら失敗する' do\r
186         @rp.classname = ''\r
187         @rp.should_not be_valid\r
188       end\r
189       it '51文字以上なら失敗する' do\r
190         @rp.classname = 'a'*51\r
191         @rp.should_not be_valid\r
192       end\r
193     end\r
194     context 'creditを検証するとき' do\r
195     end\r
196     context 'settingsを検証するとき' do\r
197     end\r
198     context 'picture_idを検証するとき' do\r
199       it 'nullなら失敗する' do\r
200         @rp.picture_id = nil\r
201         @rp.should_not be_valid\r
202       end\r
203       it '数値でなければ失敗する' do\r
204         @rp.picture_id = 'a'\r
205         @rp.should_not be_valid\r
206       end\r
207       it '存在する実素材でなければ失敗する' do\r
208         @rp.picture_id = 0\r
209         @rp.should_not be_valid\r
210       end\r
211     end\r
212   end\r
213   \r
214   describe 'デフォルト値補充に於いて' do
215     it 'defined' do
216       @p = FactoryGirl.create :picture, :original_picture_id => @original_picture.id, :license_id => @license.id, :artist_id => @artist.id\r
217       @rp = FactoryGirl.build :resource_picture, :artist_id => @artist.id, :original_picture_id => @original_picture.id, :license_id => @license.id, :picture_id => @p.id\r
218       @rp.supply_default
219     end
220   end
221   
222   describe '上書き補充に於いて' do
223     before do
224       @original_picture.attributes = {:ext => 'gif', :width => 267, :height => 268, :filesize => 269, 
225         :artist_id => @artist.id }
226       @rp = FactoryGirl.build :resource_picture, :original_picture_id => @original_picture.id
227     end
228     it 'width, height, ext, filesize, md5, original_picture_id, artist_idが設定されている' do
229       @rp.overwrite @original_picture
230       @rp.width.should eq 267
231       @rp.height.should eq 268
232       @rp.ext.should eq 'gif'
233       @rp.filesize.should eq 269
234       @rp.md5.should eq @rp.md5
235       @rp.original_picture_id.should eq @original_picture.id
236       @rp.artist_id.should eq @artist.id
237     end
238   end
239   
240   describe '所持判定に於いて' do
241     before do
242       @p = FactoryGirl.create :picture, :original_picture_id => @original_picture.id, :license_id => @license.id, :artist_id => @artist.id\r
243       @rp = FactoryGirl.build :resource_picture, :artist_id => @artist.id, :original_picture_id => @original_picture.id, :license_id => @license.id, :picture_id => @p.id\r
244     end
245     it '素材を更新することはないので、Falseを返す' do
246       @rp.own?(@author).should == false
247     end
248   end
249   
250   describe '閲覧許可に於いて' do
251     before do
252       @p = FactoryGirl.create :picture, :original_picture_id => @original_picture.id, :license_id => @license.id, :artist_id => @artist.id\r
253       @rp = FactoryGirl.build :resource_picture, :artist_id => @artist.id, :original_picture_id => @original_picture.id, :license_id => @license.id, :picture_id => @p.id\r
254     end
255     it '必ず許可となる' do\r
256       r = @rp.visible?(@author)
257       r.should == true
258     end\r
259   end
260   
261   describe 'ファイル名に於いて' do
262     before do
263       @p = FactoryGirl.create :picture, :original_picture_id => @original_picture.id, :license_id => @license.id, :artist_id => @artist.id\r
264       @rp = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :original_picture_id => @original_picture.id, :license_id => @license.id, :picture_id => @p.id\r
265     end
266     it 'id+拡張子のフォーマットで返す' do
267       r = @rp.filename
268       r.should eq "#{@rp.id}.png"
269     end
270   end
271   
272   describe 'MimeTypeに於いて' do
273     before do
274       @p = FactoryGirl.create :picture, :original_picture_id => @original_picture.id, :license_id => @license.id, :artist_id => @artist.id\r
275       @rp = FactoryGirl.build :resource_picture, :artist_id => @artist.id, :original_picture_id => @original_picture.id, :license_id => @license.id, :picture_id => @p.id\r
276     end
277     it 'image/拡張子のフォーマットで返す' do
278       r = @rp.mime_type
279       r.should eq "image/png"
280     end
281   end
282   
283   describe 'ファイルのurlに於いて' do
284     before do
285       @p = FactoryGirl.create :picture, :original_picture_id => @original_picture.id, :license_id => @license.id, :artist_id => @artist.id\r
286       @rp = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :original_picture_id => @original_picture.id, :license_id => @license.id, :picture_id => @p.id\r
287       ResourcePicture.any_instance.stub(:filename).and_return('3.gif')
288     end
289     it 'ファイル名取得を依頼している' do
290       ResourcePicture.any_instance.should_receive(:filename).exactly(1)
291       @rp.url
292     end
293     it '/resource_pictures/3.gifのフォーマットで返す' do
294       r = @rp.url
295       r.should eq "/resource_pictures/3.gif"
296     end
297   end
298   
299   describe '一覧取得に於いて' do\r
300     before do\r
301       @op = FactoryGirl.create :original_picture, :artist_id => @artist.id\r
302       @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id\r
303       @rp = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :original_picture_id => @op.id, :license_id => @license.id, :picture_id => @p.id\r
304     end\r
305     context 'page補正について' do\r
306       it '文字列から数値に変換される' do\r
307         ResourcePicture.page('8').should eq 8\r
308       end\r
309       it 'nilの場合は1になる' do\r
310         ResourcePicture.page().should eq 1\r
311       end\r
312       it '0以下の場合は1になる' do\r
313         ResourcePicture.page('0').should eq 1\r
314       end\r
315     end\r
316     context 'page_size補正について' do\r
317       it '文字列から数値に変換される' do\r
318         ResourcePicture.page_size('7').should eq 7\r
319       end\r
320       it 'nilの場合はResourcePicture.default_page_sizeになる' do\r
321         ResourcePicture.page_size().should eq ResourcePicture.default_page_size\r
322       end\r
323       it '0以下の場合はResourcePicture.default_page_sizeになる' do\r
324         ResourcePicture.page_size('0').should eq ResourcePicture.default_page_size\r
325       end\r
326       it 'ResourcePicture.max_page_sizeを超えた場合はResourcePicture.max_page_sizeになる' do\r
327         ResourcePicture.page_size('1000').should eq ResourcePicture.max_page_size\r
328       end\r
329     end\r
330     it 'リストを返す' do\r
331       r = ResourcePicture.list\r
332       r.should eq [@rp]\r
333     end\r
334     it '時系列で並んでいる' do\r
335       nop = FactoryGirl.create :original_picture, :artist_id => @artist.id\r
336       nrp = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => nop.id, :picture_id => @p.id, :updated_at => Time.now + 100\r
337       r = ResourcePicture.list\r
338       r.should eq [nrp, @rp]\r
339     end\r
340     context 'DBに5件あって1ページの件数を2件に変えたとして' do\r
341       before do\r
342         nop2 = FactoryGirl.create :original_picture, :artist_id => @artist.id\r
343         @nrp2 = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => nop2.id, :picture_id => @p.id, :updated_at => Time.now + 100\r
344         nop3 = FactoryGirl.create :original_picture, :artist_id => @artist.id\r
345         @nrp3 = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => nop3.id, :picture_id => @p.id, :updated_at => Time.now + 200\r
346         nop4 = FactoryGirl.create :original_picture, :artist_id => @artist.id\r
347         @nrp4 = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => nop4.id, :picture_id => @p.id, :updated_at => Time.now + 300\r
348         nop5 = FactoryGirl.create :original_picture, :artist_id => @artist.id\r
349         @nrp5 = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => nop5.id, :picture_id => @p.id, :updated_at => Time.now + 400\r
350         ResourcePicture.stub(:default_page_size).and_return(2)\r
351       end\r
352       it '通常は2件を返す' do\r
353         r = ResourcePicture.list\r
354         r.should have(2).items \r
355       end\r
356       it 'page=1なら末尾2件を返す' do\r
357         #時系列で並んでいる\r
358         r = ResourcePicture.list(1)\r
359         r.should eq [@nrp5, @nrp4]\r
360       end\r
361       it 'page=2なら中間2件を返す' do\r
362         r = ResourcePicture.list(2)\r
363         r.should eq [@nrp3, @nrp2]\r
364       end\r
365       it 'page=3なら先頭1件を返す' do\r
366         r = ResourcePicture.list(3)\r
367         r.should eq [@rp]\r
368       end\r
369     end\r
370     context 'DBに5件あって1ページの件数を2件に変えたとして' do
371       before do
372         nop2 = FactoryGirl.create :original_picture, :artist_id => @artist.id\r
373         @nrp2 = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => nop2.id, :picture_id => @p.id, :updated_at => Time.now + 100\r
374         nop3 = FactoryGirl.create :original_picture, :artist_id => @artist.id\r
375         @nrp3 = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => nop3.id, :picture_id => @p.id, :updated_at => Time.now + 200\r
376         nop4 = FactoryGirl.create :original_picture, :artist_id => @artist.id\r
377         @nrp4 = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => nop4.id, :picture_id => @p.id, :updated_at => Time.now + 300\r
378         nop5 = FactoryGirl.create :original_picture, :artist_id => @artist.id\r
379         @nrp5 = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => nop5.id, :picture_id => @p.id, :updated_at => Time.now + 400\r
380         ResourcePicture.stub(:default_page_size).and_return(2)\r
381       end
382       it '件数0は全件(5件)を返す' do
383         r = ResourcePicture.list 5, 0
384         r.should have(5).items 
385       end
386     end
387   end\r
388   describe '一覧出力オプションに於いて' do\r
389     it 'includeキーを含んでいる' do
390       r = ResourcePicture.list_opt
391       r.has_key?(:include).should be_true
392     end
393     it '3つの項目を含んでいる' do
394       r = ResourcePicture.list_opt[:include]
395       r.should have(3).items
396     end
397     it 'ライセンスを含んでいる' do
398       r = ResourcePicture.list_opt[:include]
399       r.has_key?(:license).should be_true
400     end
401     it '絵師を含んでいる' do
402       r = ResourcePicture.list_opt[:include]
403       r.has_key?(:artist).should be_true
404     end
405     it '実素材を含んでいる' do
406       r = ResourcePicture.list_opt[:include]
407       r.has_key?(:picture).should be_true
408     end
409   end\r
410   describe 'json一覧出力オプションに於いて' do\r
411     it 'includeキーを含んでいる' do
412       r = ResourcePicture.list_json_opt
413       r.has_key?(:include).should be_true
414     end
415     it '3つの項目を含んでいる' do
416       r = ResourcePicture.list_json_opt[:include]
417       r.should have(3).items
418     end
419     it 'ライセンスを含んでいる' do
420       r = ResourcePicture.list_json_opt[:include]
421       r.has_key?(:license).should be_true
422     end
423     it '絵師を含んでいる' do
424       r = ResourcePicture.list_json_opt[:include]
425       r.has_key?(:artist).should be_true
426     end
427     it '実素材を含んでいる' do
428       r = ResourcePicture.list_json_opt[:include]
429       r.has_key?(:picture).should be_true
430     end
431   end\r
432   \r
433   describe '単体取得に於いて' do\r
434     before do\r
435       @op = FactoryGirl.create :original_picture, :artist_id => @artist.id\r
436       @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id\r
437       @rp = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :original_picture_id => @op.id, :license_id => @license.id, :picture_id => @p.id\r
438     end\r
439     context 'つつがなく終わるとき' do
440       it '単体取得オプションを利用している' do
441         ResourcePicture.stub(:show_opt).with(any_args).and_return({})
442         ResourcePicture.should_receive(:show_opt).with(any_args).exactly(1)
443         r = ResourcePicture.show @rp.id, @author
444       end
445       it '閲覧許可を問い合わせている' do
446         ResourcePicture.any_instance.stub(:visible?).with(any_args).and_return(true)
447         ResourcePicture.any_instance.should_receive(:visible?).with(any_args).exactly(1)
448         r = ResourcePicture.show @rp.id, @author
449       end
450     end
451     it '指定の素材を返す' do\r
452       r = ResourcePicture.show @rp.id, @author\r
453       r.should eq @rp\r
454     end\r
455     context '他人の素材を開こうとしたとき' do
456       it '403Forbidden例外を返す' do
457         ResourcePicture.any_instance.stub(:visible?).and_return(false)
458         lambda{
459           r = ResourcePicture.show @rp.id, @other_author
460         }.should raise_error(ActiveRecord::Forbidden)
461       end
462     end
463     context '存在しない素材を開こうとしたとき' do\r
464       it '404RecordNotFound例外を返す' do\r
465         lambda{\r
466           r = ResourcePicture.show 0, @author\r
467         }.should raise_error(ActiveRecord::RecordNotFound)\r
468       end\r
469     end\r
470   end\r
471   describe '単体出力オプションに於いて' do\r
472     it 'includeキーを含んでいる' do
473       r = ResourcePicture.show_opt
474       r.has_key?(:include).should be_true
475     end
476     it '3つの項目を含んでいる' do
477       r = ResourcePicture.show_opt[:include]
478       r.should have(3).items
479     end
480     it 'ライセンスを含んでいる' do
481       r = ResourcePicture.show_opt[:include]
482       r.has_key?(:license).should be_true
483     end
484     it '絵師を含んでいる' do
485       r = ResourcePicture.show_opt[:include]
486       r.has_key?(:artist).should be_true
487     end
488     it '実素材を含んでいる' do
489       r = ResourcePicture.show_opt[:include]
490       r.has_key?(:picture).should be_true
491     end
492   end\r
493   describe 'json単体出力オプションに於いて' do\r
494     it 'includeキーを含んでいる' do
495       r = ResourcePicture.show_json_opt
496       r.has_key?(:include).should be_true
497     end
498     it '3つの項目を含んでいる' do
499       r = ResourcePicture.show_json_opt[:include]
500       r.should have(3).items
501     end
502     it 'ライセンスを含んでいる' do
503       r = ResourcePicture.show_json_opt[:include]
504       r.has_key?(:license).should be_true
505     end
506     it '絵師を含んでいる' do
507       r = ResourcePicture.show_json_opt[:include]
508       r.has_key?(:artist).should be_true
509     end
510     it '実素材を含んでいる' do
511       r = ResourcePicture.show_json_opt[:include]
512       r.has_key?(:picture).should be_true
513     end
514   end\r
515   \r
516   describe 'フォーマット変換対象判定に於いて' do\r
517     before do\r
518       @rp = FactoryGirl.build :resource_picture, \r
519         :artist_id => @artist.id, :license_id => @license.id\r
520     end\r
521     context '変換するケース' do\r
522       it '画像フォーマットがpngかつライセンスの変換禁止フラグが無効のときTrue' do\r
523         ResourcePicture.any_instance.stub(:flag_gif_convert).with(any_args).and_return(0)\r
524         @rp.ext = 'png'\r
525         @rp.to_gif?.should be_true\r
526       end\r
527     end\r
528     context '変換しないケース' do\r
529       it '画像フォーマットがでない' do\r
530         ResourcePicture.any_instance.stub(:flag_gif_convert).with(any_args).and_return(0)\r
531         @rp.ext = 'gif'\r
532         @rp.to_gif?.should be_false\r
533       end\r
534       it '変換禁止フラグが無効' do\r
535         ResourcePicture.any_instance.stub(:flag_gif_convert).with(any_args).and_return(-1)\r
536         @rp.ext = 'png'\r
537         @rp.to_gif?.should be_false\r
538       end\r
539     end\r
540   end\r
541   \r
542   describe '新規実素材の取得に於いて' do\r
543     before do\r
544       @op = FactoryGirl.create :original_picture, :artist_id => @artist.id\r
545       @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id\r
546       attr = {:original_picture_id => @op.id, :license_id => @license.id, :artist_name => 'tester', \r
547         :credit => '{}', :settings => {:reverse => 1, :gif_convert => 1}.to_json.to_s}\r
548       @rp = FactoryGirl.build :resource_picture, attr\r
549       @imager = ImagerTest.load "abc\ndef\nghi"
550     end\r
551     context '事前チェック' do\r
552       before do\r
553         Picture.any_instance.stub(:store).with(any_args).and_return(true)\r
554       end\r
555       it '実素材モデルにデフォルト値補充を依頼している' do\r
556         Picture.any_instance.should_receive(:supply_default).with(any_args).exactly(1)\r
557         r = @rp.new_picture @imager\r
558       end\r
559       it '実素材モデルに上書き補充を依頼している' do\r
560         Picture.any_instance.should_receive(:overwrite).with(any_args).exactly(1)\r
561         r = @rp.new_picture @imager\r
562       end\r
563       it '実素材を保存している' do\r
564         Picture.any_instance.should_receive(:store).with(any_args).exactly(1)\r
565         r = @rp.new_picture @imager\r
566       end\r
567     end\r
568     context 'つつがなく終わるとき' do\r
569       it '保存された実素材を返す' do\r
570         r = @rp.new_picture @imager\r
571         r.is_a?(Picture).should be_true\r
572       end\r
573     end\r
574     #以下から例外ケース。処理先頭から失敗させていく\r
575     context '実素材の保存に失敗したとき' do\r
576       before do\r
577         Picture.any_instance.stub(:store).with(any_args).and_return(false)\r
578       end\r
579       it 'Falseを返す' do\r
580         r = @rp.new_picture @imager\r
581         r.should be_false\r
582       end\r
583       it '自身の全体エラーメッセージにその旨をセットしている' do\r
584         r = @rp.new_picture @imager\r
585         @rp.errors[:base].should_not be_empty\r
586       end\r
587     end\r
588   end\r
589   \r
590   describe '作成・更新に於いて' do\r
591     before do\r
592       @op = FactoryGirl.create :original_picture, :artist_id => @artist.id\r
593       @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id\r
594       attr = {:original_picture_id => @op.id, :picture_id => nil, :license_id => @license.id, :artist_name => 'tester', :classname => 'StandardLicense', :credit => '{}', :settings => {}.to_json.to_s}\r
595       @rp = FactoryGirl.build :resource_picture, attr\r
596       @imager = ImagerTest.load "abc\ndef\nghi"
597     end\r
598     context '事前チェック' do\r
599       before do\r
600         #すべての処理が正常パターンで通過すれば、一番深い分岐まで通る。\r
601         #それで外部のメソッド呼び出しだけに着目してテストする。\r
602         ResourcePicture.any_instance.stub(:new_picture).with(any_args).and_return(@p)\r
603         ResourcePicture.any_instance.stub(:store_picture_with_gif).with(any_args).and_return(true)\r
604       end\r
605       it '新規実素材の取得を依頼している' do\r
606         ResourcePicture.any_instance.should_receive(:new_picture).with(any_args).exactly(1)\r
607         r = @rp.store @imager\r
608       end\r
609       it '自身を保存している' do\r
610         ResourcePicture.any_instance.should_receive(:save).with(any_args).exactly(1)\r
611         r = @rp.store @imager\r
612       end\r
613       it 'gif付き画像ファイルの作成・更新機能で画像を保存している' do\r
614         ResourcePicture.any_instance.should_receive(:store_picture_with_gif).with(@imager).exactly(1)\r
615         r = @rp.store @imager\r
616       end\r
617     end\r
618     context 'つつがなく終わるとき' do\r
619       before do\r
620         #すべての処理を正常パターンで通過させ、保存機能をチェックする。\r
621         ResourcePicture.any_instance.stub(:new_picture).with(any_args).and_return(@p)\r
622         ResourcePicture.any_instance.stub(:store_picture_with_gif).with(@imager).and_return(true)\r
623       end\r
624       it 'Trueを返す' do\r
625         r = @rp.store @imager\r
626         r.should be_true\r
627       end\r
628       it '実素材idから最新画像idを取得してセットしている' do\r
629         r = @rp.store @imager\r
630         @rp.picture_id.should eq @p.id\r
631       end\r
632       it '自身が保存されている' do\r
633         lambda {\r
634           r = @rp.store @imager\r
635         }.should change ResourcePicture, :count\r
636       end\r
637     end\r
638     #以下から例外ケース。処理先頭から失敗させていく\r
639     context '画像オブジェクトの取得に失敗したとき' do\r
640       before do\r
641         @imager = false\r
642       end\r
643       it 'Falseを返す' do\r
644         r = @rp.store @imager\r
645         r.should be_false\r
646       end\r
647       it '更新されていない' do\r
648         r = @rp.store @imager\r
649         @rp.should be_a_new ResourcePicture\r
650       end\r
651       it '処理を中断してロールバックする' do\r
652         lambda {\r
653           r = @rp.store @imager\r
654         }.should_not change Picture, :count\r
655       end\r
656     end\r
657     context '新規実素材の取得に失敗したとき' do\r
658       before do\r
659         ResourcePicture.any_instance.stub(:new_picture).with(any_args).and_return(false)\r
660       end\r
661       it 'Falseを返す' do\r
662         r = @rp.store @imager\r
663         r.should be_false\r
664       end\r
665       it '更新されていない' do\r
666         r = @rp.store @imager\r
667         @rp.should be_a_new ResourcePicture\r
668       end\r
669       it '処理を中断してロールバックする' do\r
670         lambda {\r
671           r = @rp.store @imager\r
672         }.should_not change Picture, :count\r
673       end\r
674     end\r
675     context '自身の保存に失敗したとき' do\r
676       before do\r
677         ResourcePicture.any_instance.stub(:new_picture).with(any_args).and_return(@p)\r
678         ResourcePicture.any_instance.stub(:save).with(any_args).and_return(false)\r
679       end\r
680       it 'Falseを返す' do\r
681         r = @rp.store @imager\r
682         r.should be_false\r
683       end\r
684       it '更新されていない' do\r
685         r = @rp.store @imager\r
686         @rp.should be_a_new ResourcePicture\r
687       end\r
688       it '処理を中断してロールバックする' do\r
689         lambda {\r
690           r = @rp.store @imager\r
691         }.should_not change Picture, :count\r
692       end\r
693     end\r
694     context 'gif付き画像ファイルの作成・更新機能に失敗したとき' do\r
695       before do\r
696         ResourcePicture.any_instance.stub(:new_picture).with(any_args).and_return(@p)\r
697         ResourcePicture.any_instance.stub(:store_picture_with_gif).with(any_args).and_return(false)\r
698       end\r
699       it 'Falseを返す' do\r
700         r = @rp.store @imager\r
701         r.should be_false\r
702       end\r
703       it '更新されていない' do\r
704         r = @rp.store @imager\r
705         @rp.should be_a_new ResourcePicture\r
706       end\r
707       it '処理を中断してロールバックする' do\r
708         lambda {\r
709           r = @rp.store @imager\r
710         }.should_not change Picture, :count\r
711       end\r
712     end\r
713   end\r
714   \r
715   describe 'gif付き画像ファイルの作成・更新に於いて' do\r
716     before do\r
717       @op = FactoryGirl.create :original_picture, :artist_id => @artist.id\r
718       @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id\r
719       attr = {:original_picture_id => @op.id, :license_id => @license.id, :artist_name => 'tester', :credit => '{}', :settings => {}.to_json.to_s}\r
720       @rp = FactoryGirl.build :resource_picture, attr\r
721       @imager = ImagerTest.load "abc\ndef\nghi"
722     end\r
723     context '事前チェック' do\r
724       before do\r
725         #すべての処理が正常パターンで通過すれば、一番深い分岐まで通る。\r
726         #それで外部のメソッド呼び出しだけに着目してテストする。\r
727         ResourcePicture.any_instance.stub(:store_picture).with(any_args).and_return(true)\r
728         ResourcePicture.any_instance.stub(:to_gif?).with(any_args).and_return(true)\r
729         ResourcePicture.any_instance.stub(:filename).with(any_args).and_return('1.png')\r
730         ResourcePicture.any_instance.stub(:gifname).with(any_args).and_return('1.gif')\r
731       end\r
732       it '画像ファイルの作成・更新機能で画像を保存している' do\r
733         #二回目の保存はgif変換の結果を渡す。\r
734         ResourcePicture.any_instance.should_receive(:store_picture).with(any_args).exactly(2)\r
735         r = @rp.store_picture_with_gif @imager\r
736       end\r
737       it '自身にフォーマット変換対象かを問い合わせている' do\r
738         ResourcePicture.any_instance.should_receive(:to_gif?).with(any_args).exactly(1)\r
739         r = @rp.store_picture_with_gif @imager\r
740       end\r
741       it '画像ライブラリにGifフォーマット変換を依頼している' do\r
742         ImagerTest.any_instance.should_receive(:to_gif).with(any_args).exactly(1)\r
743         r = @rp.store_picture_with_gif @imager\r
744       end\r
745     end\r
746     context 'つつがなく終わるとき' do\r
747       before do\r
748         #すべての処理を正常パターンで通過させ、保存機能をチェックする。\r
749         ResourcePicture.any_instance.stub(:store_picture).with(any_args).and_return(true)\r
750         ResourcePicture.any_instance.stub(:to_gif?).with(any_args).and_return(true)\r
751         ResourcePicture.any_instance.stub(:filename).with(any_args).and_return('1.png')\r
752         ResourcePicture.any_instance.stub(:gifname).with(any_args).and_return('1.gif')\r
753       end\r
754       it 'Trueを返す' do\r
755         r = @rp.store_picture_with_gif @imager\r
756         r.should be_true\r
757       end\r
758     end\r
759     context 'gif変換なしで、つつがなく終わるとき' do\r
760       before do\r
761         #すべての処理を正常パターンで通過させ、保存機能をチェックする。\r
762         ResourcePicture.any_instance.stub(:store_picture).with(any_args).and_return(true)\r
763         ResourcePicture.any_instance.stub(:to_gif?).with(any_args).and_return(false)\r
764         ResourcePicture.any_instance.stub(:filename).with(any_args).and_return('1.png')\r
765         ResourcePicture.any_instance.stub(:gifname).with(any_args).and_return('1.gif')\r
766       end\r
767       it 'Trueを返す' do\r
768         r = @rp.store_picture_with_gif @imager\r
769         r.should be_true\r
770       end\r
771       it 'gif保存は呼ばれていない' do\r
772         ResourcePicture.any_instance.should_receive(:store_picture).with(any_args).exactly(1)\r
773         r = @rp.store_picture_with_gif @imager\r
774       end\r
775     end\r
776     #以下から例外ケース。処理先頭から失敗させていく\r
777     context '画像の保存に失敗したとき' do\r
778       before do\r
779         ResourcePicture.any_instance.stub(:store_picture).with(any_args).and_return(false)\r
780       end\r
781       it 'Falseを返す' do\r
782         r = @rp.store_picture_with_gif @imager\r
783         r.should be_false\r
784       end\r
785       it 'gif変換判定は呼ばれていない' do\r
786         ResourcePicture.any_instance.should_not_receive(:to_gif?).with(any_args)\r
787         r = @rp.store_picture_with_gif @imager\r
788       end\r
789     end\r
790     context 'gif変換に失敗したとき' do\r
791       before do\r
792         ResourcePicture.any_instance.stub(:store_picture).with(any_args).and_return(true)\r
793         ResourcePicture.any_instance.stub(:to_gif?).with(any_args).and_return(true)\r
794         ImagerTest.any_instance.stub(:to_gif).with(any_args).and_return(false)\r
795       end\r
796       it 'Falseを返す' do\r
797         r = @rp.store_picture_with_gif @imager\r
798         r.should be_false\r
799       end\r
800       it 'gif画像の保存は呼ばれていない' do\r
801         #本画像の保存があるので、一度は呼ばれる\r
802         ResourcePicture.any_instance.should_receive(:store_picture).with(any_args).exactly(1)\r
803         r = @rp.store_picture_with_gif @imager\r
804       end\r
805     end\r
806     context 'gif画像の保存に失敗したとき' do\r
807       before do\r
808         ResourcePicture.any_instance.stub(:store_picture).with(@imager, '1.png').and_return(true)\r
809         ResourcePicture.any_instance.stub(:to_gif?).with(any_args).and_return(true)\r
810         @gifimager = @imager.to_gif\r
811         ImagerTest.any_instance.stub(:to_gif).with(any_args).and_return(@gifimager)\r
812         ResourcePicture.any_instance.stub(:store_picture).with(@gifimager, '1.gif').and_return(false)\r
813         ResourcePicture.any_instance.stub(:filename).with(any_args).and_return('1.png')\r
814         ResourcePicture.any_instance.stub(:gifname).with(any_args).and_return('1.gif')\r
815       end\r
816       it 'Falseを返す' do\r
817         r = @rp.store_picture_with_gif @imager\r
818         r.should be_false\r
819       end\r
820     end\r
821   end\r
822   \r
823   describe '画像ファイルの作成・更新に於いて' do\r
824     #PictureIo経由で画像を保存するための機能。ファイル名に自身のidを使うので事前に自身の保存が必要。\r
825     before do\r
826       @op = FactoryGirl.create :original_picture, :artist_id => @artist.id\r
827       @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id\r
828       attr = {:original_picture_id => @op.id, :license_id => @license.id, :artist_name => 'tester', :credit => '{}', :settings => {}.to_json.to_s}\r
829       @rp = FactoryGirl.build :resource_picture, attr\r
830       @rp.overwrite @op\r
831       @imager = ImagerTest.load "abc\ndef\nghi"
832     end\r
833     context '事前チェック' do\r
834       before do\r
835         #すべての処理を正常パターンで通過させ、保存機能をチェックする。\r
836         PictureIO.resource_picture_io.stub(:put).with(any_args).and_return(true)\r
837         ResourcePicture.any_instance.stub(:flag_thumbnail).with(any_args).and_return(1)\r
838       end\r
839       it 'サムネイル化が1回呼ばれる' do\r
840         ImagerTest.any_instance.should_receive(:to_thumbnail).with(any_args).exactly(1)\r
841         r = @rp.store_picture(@imager, '1.png')\r
842       end\r
843       it '画像ファイルの保存が2回呼ばれる' do\r
844         PictureIO.resource_picture_io.should_receive(:put).with(any_args).exactly(2)\r
845         r = @rp.store_picture(@imager, '1.gif')\r
846       end\r
847       it '画像ファイルのベースへのサムネイル保存が1回呼ばれる' do\r
848         PictureIO.resource_picture_io.should_receive(:put).with(@imager.to_thumbnail.binary, '1.gif').exactly(1)\r
849         r = @rp.store_picture(@imager, '1.gif')\r
850       end\r
851       it '画像ファイルのfullへの保存が1回呼ばれる' do\r
852         PictureIO.resource_picture_io.should_receive(:put).with(@imager.binary, '1.gif', 'full').exactly(1)\r
853         r = @rp.store_picture(@imager, '1.gif')\r
854       end\r
855     end\r
856     context 'つつがなく終わるとき' do\r
857       before do\r
858         #すべての処理を正常パターンで通過させ、保存機能をチェックする。\r
859         PictureIO.resource_picture_io.stub(:put).with(any_args).and_return(true)\r
860         ResourcePicture.any_instance.stub(:flag_thumbnail).with(any_args).and_return(1)\r
861       end\r
862       it 'Trueを返す' do\r
863         r = @rp.store_picture(@imager, '1.gif')\r
864         r.should be_true\r
865       end\r
866     end\r
867     #以下から例外ケース。処理先頭から失敗させていく\r
868     context 'サムネイル化に失敗したとき' do\r
869       before do\r
870         ResourcePicture.any_instance.stub(:flag_thumbnail).with(any_args).and_return(1)\r
871         ImagerTest.any_instance.stub(:to_thumbnail).with(any_args).and_return(false)\r
872       end\r
873       it 'Falseを返す' do\r
874         r = @rp.store_picture(@imager, '1.gif')\r
875         r.should be_false\r
876       end\r
877       it '画像ファイルの保存は呼ばれていない' do\r
878         PictureIO.resource_picture_io.should_not_receive(:put).with(any_args)\r
879         r = @rp.store_picture(@imager, '1.gif')\r
880       end\r
881     end\r
882     context 'サムネイル画像の保存に失敗したとき' do\r
883       before do\r
884         ResourcePicture.any_instance.stub(:flag_thumbnail).with(any_args).and_return(1)\r
885         @tmbimager = @imager.to_thumbnail\r
886         ImagerTest.any_instance.stub(:to_thumbnail).with(any_args).and_return(@tmbimager)\r
887         PictureIO.resource_picture_io.stub(:put).with(@tmbimager.binary, '1.gif').and_return(false)\r
888       end\r
889       it 'Falseを返す' do\r
890         r = @rp.store_picture(@imager, '1.gif')\r
891         r.should be_false\r
892       end\r
893       it '画像ファイルの保存は呼ばれていない' do\r
894         PictureIO.resource_picture_io.should_receive(:put).with(any_args).exactly(1)\r
895         r = @rp.store_picture(@imager, '1.gif')\r
896       end\r
897     end\r
898     context '画像の保存に失敗したとき' do\r
899       before do\r
900         ResourcePicture.any_instance.stub(:flag_thumbnail).with(any_args).and_return(1)\r
901         @tmbimager = @imager.to_thumbnail\r
902         ImagerTest.any_instance.stub(:to_thumbnail).with(any_args).and_return(@tmbimager)\r
903         PictureIO.resource_picture_io.stub(:put).with(@tmbimager.binary, '1.gif').and_return(true)\r
904         PictureIO.resource_picture_io.stub(:put).with(@imager.binary, '1.gif', 'full').and_return(false)\r
905       end\r
906       it 'Falseを返す' do\r
907         r = @rp.store_picture(@imager, '1.gif')\r
908         r.should be_false\r
909       end\r
910     end\r
911     \r
912   end\r
913   \r
914   describe 'クレジットデータに於いて' do\r
915     before do\r
916       @op = FactoryGirl.create :original_picture, :artist_id => @artist.id\r
917       @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id\r
918       attr = {:original_picture_id => @op.id, :license_id => @license.id, :artist_name => 'tester', :credit => '{"system_picture_id": 2}', :settings => {}.to_json.to_s}\r
919       @rp = FactoryGirl.build :resource_picture, attr\r
920     end\r
921     it 'system_picture_idが入っている' do\r
922       res = @rp.credit_data\r
923       res["system_picture_id"].should eq 2\r
924     end\r
925   end\r
926   \r
927 end\r
928 \r