OSDN Git Service

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