OSDN Git Service

45af3809a086003ae28b671a2aba62ae19d76968
[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.find_by_md5(@p.md5)
439       res.is_a?(Array).should be_true
440     end
441     it 'md5が違えば含まない' do
442       res = Picture.find_by_md5(@p.md5)
443       res.include?(@p2).should be_false
444     end
445     it '更新日時順' do
446       @p3 = FactoryGirl.create :picture, :artist_id => @artist.id, :license_id => @license.id, :revision => 1,
447         :original_picture_id => @op2.id, :md5 => 'C' * 32
448       @p4 = FactoryGirl.create :picture, :artist_id => @artist.id, :license_id => @license.id, :revision => 1,
449         :original_picture_id => @op3.id, :md5 => @p3.md5, :updated_at => Time.now + 100
450       res = Picture.find_by_md5(@p3.md5)
451       res.should eq [@p4, @p3]
452     end
453   end
454   
455   describe 'md5重複リストに於いて' do
456     before do
457       @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
458       @p = FactoryGirl.create :picture, :artist_id => @artist.id, :license_id => @license.id, :revision => 0,
459         :original_picture_id => @op.id, :md5 => 'a' * 32
460       @rp = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :original_picture_id => @op.id, :license_id => @license.id, :picture_id => @p.id
461       @op2 = FactoryGirl.create :original_picture, :artist_id => @artist.id
462       @p2 = FactoryGirl.create :picture, :artist_id => @artist.id, :license_id => @license.id, :revision => 0,
463         :original_picture_id => @op2.id, :md5 => 'b' * 32
464       @op3 = FactoryGirl.create :original_picture, :artist_id => @artist.id
465     end
466     it 'リストを返す' do
467       res = Picture.list_by_md5(@p.md5, @p.original_picture_id)
468       res.is_a?(Array).should be_true
469     end
470     it 'md5が違えば含まない' do
471       res = Picture.list_by_md5(@p.md5, @p.original_picture_id)
472       res.include?(@p2).should be_false
473     end
474     it '同一原画は含まない' do
475       res = Picture.list_by_md5(@p.md5, @p.original_picture_id)
476       res.empty?.should be_true
477     end
478     it '同一原画は旧版でも含まない' do
479       @p3 = FactoryGirl.create :picture, :artist_id => @artist.id, :license_id => @license.id, :revision => 1,
480         :original_picture_id => @op.id, :md5 => 'a' * 32
481       res = Picture.list_by_md5(@p.md5, @p.original_picture_id)
482       res.empty?.should be_true
483     end
484     it '他所の原画なら含む' do
485       @p3 = FactoryGirl.create :picture, :artist_id => @artist.id, :license_id => @license.id, :revision => 1,
486         :original_picture_id => @op2.id, :md5 => 'a' * 32
487       res = Picture.list_by_md5(@p.md5, @p.original_picture_id)
488       res.should eq [@p3]
489     end
490     it '他所の原画でもmd5が違えば含まない' do
491       @p3 = FactoryGirl.create :picture, :artist_id => @artist.id, :license_id => @license.id, :revision => 1,
492         :original_picture_id => @op2.id, :md5 => 'c' * 32
493       res = Picture.list_by_md5(@p.md5, @p.original_picture_id)
494       res.empty?.should be_true
495     end
496     it '更新日時順' do
497       @p3 = FactoryGirl.create :picture, :artist_id => @artist.id, :license_id => @license.id, :revision => 1,
498         :original_picture_id => @op2.id, :md5 => 'a' * 32
499       @p4 = FactoryGirl.create :picture, :artist_id => @artist.id, :license_id => @license.id, :revision => 1,
500         :original_picture_id => @op3.id, :md5 => 'a' * 32, :updated_at => Time.now + 100
501       res = Picture.list_by_md5(@p.md5, @p.original_picture_id)
502       res.should eq [@p4, @p3]
503     end
504   end
505   
506   describe 'md5重複判定に於いて' do
507     before do
508       @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
509       @op2 = FactoryGirl.create :original_picture, :artist_id => @artist.id
510       @p = FactoryGirl.create :picture, :artist_id => @artist.id, :original_picture_id => @op.id, :license_id => @license.id, :revision => 0
511       @rp = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :original_picture_id => @op.id, :license_id => @license.id, :picture_id => @p.id
512     end
513     context '同一原画以外に同じ値があるとき' do
514       before do
515         @p2 = FactoryGirl.create :picture, :artist_id => @artist.id, :original_picture_id => @op2.id, :license_id => @license.id
516       end
517       it 'trueを返す' do
518         res = Picture.exist_by_md5(@p.md5, @p.original_picture_id)
519         res.should be_true
520       end
521     end
522     context '同一原画以外に同じ値がないとき' do
523       it 'falseを返す' do
524         res = Picture.exist_by_md5(@p.md5, @p.original_picture_id)
525         res.should be_false
526       end
527     end
528     context '同一原画に同じ値があるとき' do
529       before do
530         @p2 = FactoryGirl.create :picture, :artist_id => @artist.id, :original_picture_id => @op.id, :license_id => @license.id, :revision => 1
531       end
532       it 'falseを返す' do
533         res = Picture.exist_by_md5(@p.md5, @p.original_picture_id)
534         res.should be_false
535       end
536     end
537   end
538   
539   describe '単体取得に於いて' do
540     before do
541       @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id\r
542     end
543     context 'つつがなく終わるとき' do
544       it '閲覧許可を問い合わせている' do
545         Picture.any_instance.stub(:visible?).with(any_args).and_return(true)
546         Picture.any_instance.should_receive(:visible?).with(any_args).exactly(1)
547         r = Picture.show @p.id, @author
548       end
549     end
550     it '指定の実素材を返す' do
551       r = Picture.show @p.id, @author
552       r.should eq @p
553     end
554     context '他人の実素材を開こうとしたとき' do
555       it '403Forbidden例外を返す' do
556         Picture.any_instance.stub(:visible?).and_return(false)
557         lambda{
558           r = Picture.show @p.id, @other_author
559         }.should raise_error(ActiveRecord::Forbidden)
560       end
561     end
562     context '存在しない実素材を開こうとしたとき' do
563       it '404RecordNotFound例外を返す' do
564         lambda{
565           r = Picture.show 0, @author
566         }.should raise_error(ActiveRecord::RecordNotFound)
567       end
568     end
569   end
570   
571   describe '作成に於いて' do
572     before do
573       @imager = ImagerTest.load "abc\ndef\nghi"
574       #原画ファイル削除だけは必ず成功するものとしておく
575       PictureIO::LocalPicture.any_instance.stub(:delete).with(any_args).and_return(true)
576     end
577     context '事前チェック' do
578       before do
579         #すべての処理が正常パターンで通過すれば、一番深い分岐まで通る。
580         #それで外部のメソッド呼び出しだけに着目してテストする。
581         Picture.any_instance.stub(:save).with(any_args).and_return(true)
582         Picture.any_instance.stub(:store_picture).with(any_args).and_return(true)
583         Picture.any_instance.stub(:to_gif?).with(any_args).and_return(true)
584         @p = FactoryGirl.build :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id, :ext => 'png'\r
585       end
586       it '自身を保存している' do
587         Picture.any_instance.should_receive(:save).with(any_args).exactly(1)
588         r = @p.store(@imager)
589       end
590       it '画像ファイルの作成機能で画像を保存している' do
591         Picture.any_instance.stub(:filename).with(any_args).and_return('1.png')
592         #二回目の保存はgif変換の結果を渡す。
593         Picture.any_instance.should_receive(:store_picture).with(any_args).exactly(2)
594         r = @p.store(@imager)
595       end
596       it '自身にgifフォーマット変換対象かを問い合わせている' do
597         Picture.any_instance.should_receive(:to_gif?).with(any_args).exactly(1)
598         r = @p.store(@imager)
599       end
600     end
601     context 'つつがなく終わるとき' do
602       before do
603         #すべての処理を正常パターンで通過させ、保存機能をチェックする。
604         Picture.any_instance.stub(:store_picture).with(any_args).and_return(true)
605         Picture.any_instance.stub(:to_gif?).with(any_args).and_return(true)
606         @p = FactoryGirl.build :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id, :ext => 'png'\r
607       end
608       it 'Trueを返す' do
609         r = @p.store(@imager)
610         r.should be_true
611       end
612       it '自身が保存されている' do
613         lambda {
614           r = @p.store(@imager)
615         }.should change Picture, :count
616       end
617     end
618     context 'gif変換なしで、つつがなく終わるとき' do
619       before do
620         #すべての処理を正常パターンで通過させ、保存機能をチェックする。
621         Picture.any_instance.stub(:store_picture).with(any_args).and_return(true)
622         Picture.any_instance.stub(:to_gif?).with(any_args).and_return(false)
623         @p = FactoryGirl.build :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id, :ext => 'png'\r
624       end
625       it 'Trueを返す' do
626         r = @p.store(@imager)
627         r.should be_true
628       end
629       it 'gif保存は呼ばれていない' do
630         #二回目の画像作成が呼び出されないで1回こっきりならgif保存は呼ばれていないんだろう。
631         Picture.any_instance.should_receive(:store_picture).with(any_args).exactly(1)
632         r = @p.store(@imager)
633       end
634     end
635     #以下から例外ケース。処理先頭から失敗させていく
636     context '自身の保存に失敗したとき' do
637       before do
638         Picture.any_instance.stub(:save).with(any_args).and_return(false)
639         @p = FactoryGirl.build :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id, :ext => 'png'\r
640       end
641       it 'Falseを返す' do
642         r = @p.store(@imager)
643         r.should be_false
644       end
645       it '更新されていない' do
646         r = @p.store(@imager)
647         @p.should be_a_new Picture
648       end
649     end
650     context '画像の保存に失敗したとき' do
651       before do
652         Picture.any_instance.stub(:store_picture).with(any_args).and_return(false)
653         @p = FactoryGirl.build :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id, :ext => 'png'\r
654       end
655       it 'Falseを返す' do
656         r = @p.store(@imager)
657         r.should be_false
658       end
659       it 'gif変換判定は呼ばれていない' do
660         Picture.any_instance.should_not_receive(:to_gif?).with(any_args).exactly(0)
661         r = @p.store(@imager)
662       end
663     end
664     context 'gif変換に失敗したとき' do
665       before do
666         Picture.any_instance.stub(:store_picture).with(any_args).and_return(true)
667         Picture.any_instance.stub(:to_gif?).with(any_args).and_return(true)
668         ImagerTest.stub(:load).with(any_args).and_return(false)
669         @p = FactoryGirl.build :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id, :ext => 'png'\r
670       end
671       it 'Falseを返す' do
672         r = @p.store(@imager)
673         r.should be_false
674       end
675       it 'gif画像の保存は呼ばれていない' do
676         #本画像の保存があるので、一度は呼ばれる
677         Picture.any_instance.should_receive(:store_picture).with(any_args).exactly(1)
678         r = @p.store(@imager)
679       end
680     end
681     context 'gif画像の保存に失敗したとき' do
682       before do
683         @gifimager = @imager.to_gif
684         ImagerTest.any_instance.stub(:to_gif).with(any_args).and_return(@gifimager)
685         Picture.any_instance.stub(:filename).with(any_args).and_return('1.png')
686         Picture.any_instance.stub(:gifname).with(any_args).and_return('1.gif')
687         Picture.any_instance.stub(:store_picture).with(@imager, '1.png').and_return(true)
688         Picture.any_instance.stub(:to_gif?).with(any_args).and_return(true)
689         Picture.any_instance.stub(:store_picture).with(@gifimager, '1.gif').and_return(false)
690         @p = FactoryGirl.build :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id, :ext => 'png'\r
691       end
692       it 'Falseを返す' do
693         r = @p.store(@imager)
694         r.should be_false
695       end
696     end
697   end
698   
699   describe '画像ファイルの作成に於いて' do
700     #PictureIo経由で画像を保存するための機能。ファイル名に自身のidを使うので事前に自身の保存が必要。
701     before do
702       @imager = ImagerTest.load "abc\ndef\nghi"
703       #原画ファイル削除だけは必ず成功するものとしておく
704       PictureIO::LocalPicture.any_instance.stub(:delete).with(any_args).and_return(true)
705       
706       Picture.any_instance.stub(:subdirs).with(any_args).and_return(['', 'v', 'h', 'vh'])
707       @p = FactoryGirl.build :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id, :ext => 'png'\r
708     end
709     context '事前チェック' do
710       before do
711         #すべての処理を正常パターンで通過させ、保存機能をチェックする。
712         PictureIO.picture_io.stub(:put).with(any_args).and_return(true)
713       end
714       it '画像ファイルの保存が4回呼ばれる' do
715         PictureIO.picture_io.should_receive(:put).with(any_args).exactly(4)
716         r = @p.store_picture(@imager, '1.png')
717       end
718       it '画像ファイルのベースへの保存が1回呼ばれる' do
719         PictureIO.picture_io.should_receive(:put).with("abc\ndef\nghi", '1.png', '').exactly(1)
720         r = @p.store_picture(@imager, '1.png')
721       end
722       it '画像ファイルの垂直反転への保存が1回呼ばれる' do
723         PictureIO.picture_io.should_receive(:put).with("cba\nfed\nihg", '1.png', 'v').exactly(1)
724         r = @p.store_picture(@imager, '1.png')
725       end
726       it '画像ファイルの水平反転への保存が1回呼ばれる' do
727         PictureIO.picture_io.should_receive(:put).with("ghi\ndef\nabc", '1.png', 'h').exactly(1)
728         r = @p.store_picture(@imager, '1.png')
729       end
730       it '画像ファイルの垂直水平反転への保存が1回呼ばれる' do
731         PictureIO.picture_io.should_receive(:put).with("ihg\nfed\ncba", '1.png', 'vh').exactly(1)
732         r = @p.store_picture(@imager, '1.png')
733       end
734     end
735     context 'つつがなく終わるとき' do
736       before do
737         #すべての処理を正常パターンで通過させ、保存機能をチェックする。
738         PictureIO.picture_io.stub(:put).with(any_args).and_return(true)
739       end
740       it 'Trueを返す' do
741         r = @p.store_picture(@imager, '1.png')
742         r.should be_true
743       end
744     end
745     context '例外ケース' do
746       before do
747         PictureIO.picture_io.stub(:put).with(any_args).and_return(false)
748       end
749       it 'Falseを返す' do
750         r = @p.store_picture(@imager, '1.png')
751         r.should be_false
752       end
753     end
754     
755   end
756   
757   describe 'フラグ展開に於いて' do
758     before do
759       @p = FactoryGirl.build :picture, :original_picture_id => @op.id, :license_id => @license.id,
760         :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}"
761     end
762     context 'json展開チェック' do
763       it '展開してなければ展開して@flagsに保管する' do
764         @p.flags.should_not be_nil
765       end
766       it '展開できなければ{}' do
767         @p.settings += '}'
768         @p.flags.is_a?(Hash).should be_true
769         @p.flags.empty?.should be_true
770       end
771     end
772     context 'openについて' do
773       it '@flag_openに保管する' do
774         @p.flag_open.should_not be_nil
775       end
776       it '本当にフラグHashからじゃなく、変数から取得してる?' do
777         @p.flag_open
778         @p.flags = nil
779         @p.flag_open.should_not be_nil
780       end
781       it '1を返す' do
782         @p.flag_open.should eq 1
783       end
784     end
785     context 'commercialについて' do
786       it '@flag_commercialに保管する' do
787         @p.flag_commercial.should_not be_nil
788       end
789       it '本当にフラグHashからじゃなく、変数から取得してる?' do
790         @p.flag_commercial
791         @p.flags = nil
792         @p.flag_commercial.should_not be_nil
793       end
794       it '2を返す' do
795         @p.flag_commercial.should eq 2
796       end
797     end
798     context 'officialについて' do
799       it '@flag_officialに保管する' do
800         @p.flag_official.should_not be_nil
801       end
802       it '本当にフラグHashからじゃなく、変数から取得してる?' do
803         @p.flag_official
804         @p.flags = nil
805         @p.flag_official.should_not be_nil
806       end
807       it 'を返す' do
808         @p.flag_official.should eq 3
809       end
810     end
811     context 'attributionについて' do
812       it '@flag_attributionに保管する' do
813         @p.flag_attribution.should_not be_nil
814       end
815       it '本当にフラグHashからじゃなく、変数から取得してる?' do
816         @p.flag_attribution
817         @p.flags = nil
818         @p.flag_attribution.should_not be_nil
819       end
820       it '4を返す' do
821         @p.flag_attribution.should eq 4
822       end
823     end
824     context 'deriveについて' do
825       it '@flag_deriveに保管する' do
826         @p.flag_derive.should_not be_nil
827       end
828       it '本当にフラグHashからじゃなく、変数から取得してる?' do
829         @p.flag_derive
830         @p.flags = nil
831         @p.flag_derive.should_not be_nil
832       end
833       it '5を返す' do
834         @p.flag_derive.should eq 5
835       end
836     end
837     context 'thumbnailについて' do
838       it '@flag_thumbnailに保管する' do
839         @p.flag_thumbnail.should_not be_nil
840       end
841       it '本当にフラグHashからじゃなく、変数から取得してる?' do
842         @p.flag_thumbnail
843         @p.flags = nil
844         @p.flag_thumbnail.should_not be_nil
845       end
846       it '6を返す' do
847         @p.flag_thumbnail.should eq 6
848       end
849     end
850     context 'gif_convertについて' do
851       it '@flag_gif_convertに保管する' do
852         @p.flag_gif_convert.should_not be_nil
853       end
854       it '本当にフラグHashからじゃなく、変数から取得してる?' do
855         @p.flag_gif_convert
856         @p.flags = nil
857         @p.flag_gif_convert.should_not be_nil
858       end
859       it '7を返す' do
860         @p.flag_gif_convert.should eq 7
861       end
862     end
863     context 'reverseについて' do
864       it '@flag_reverseに保管する' do
865         @p.flag_reverse.should_not be_nil
866       end
867       it '本当にフラグHashからじゃなく、変数から取得してる?' do
868         @p.flag_reverse
869         @p.flags = nil
870         @p.flag_reverse.should_not be_nil
871       end
872       it '8を返す' do
873         @p.flag_reverse.should eq 8
874       end
875     end
876     context 'resizeについて' do
877       it '@flag_resizeに保管する' do
878         @p.flag_resize.should_not be_nil
879       end
880       it '本当にフラグHashからじゃなく、変数から取得してる?' do
881         @p.flag_resize
882         @p.flags = nil
883         @p.flag_resize.should_not be_nil
884       end
885       it '9を返す' do
886         @p.flag_resize.should eq 9
887       end
888     end
889     context 'sync_vhについて' do
890       it '@flag_sync_vhに保管する' do
891         @p.flag_sync_vh.should_not be_nil
892       end
893       it '本当にフラグHashからじゃなく、変数から取得してる?' do
894         @p.flag_sync_vh
895         @p.flags = nil
896         @p.flag_sync_vh.should_not be_nil
897       end
898       it '10を返す' do
899         @p.flag_sync_vh.should eq 10
900       end
901     end
902     context 'overlapについて' do
903       it '@flag_overlapに保管する' do
904         @p.flag_overlap.should_not be_nil
905       end
906       it '本当にフラグHashからじゃなく、変数から取得してる?' do
907         @p.flag_overlap
908         @p.flags = nil
909         @p.flag_overlap.should_not be_nil
910       end
911       it '11を返す' do
912         @p.flag_overlap.should eq 11
913       end
914     end
915   end
916
917   describe 'クレジットデータに於いて' do
918     before do
919       @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
920       @p = FactoryGirl.build :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id, :credit => '{"system_picture_id": 2}'
921     end
922     it 'system_picture_idが入っている' do
923       res = @p.credit_data
924       res["system_picture_id"].should eq 2
925     end
926   end
927   
928 end