OSDN Git Service

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