OSDN Git Service

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