OSDN Git Service

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