OSDN Git Service

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