OSDN Git Service

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