OSDN Git Service

passed. resource picture model test
[pettanr/pettanr.git] / spec / models / original_picture_spec.rb
1 # -*- encoding: utf-8 -*-
2 require 'spec_helper'
3
4 describe OriginalPicture 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
10     @other_user = Factory( :user_yas)
11     @other_author = @other_user.author
12     @other_artist = Factory :artist_yas, :author_id => @other_author.id
13     @license = Factory :license
14   end
15   
16   describe '検証に於いて' do
17     before do
18     end
19     
20     it 'オーソドックスなデータなら通る' do
21       @op = Factory.build :original_picture, :artist_id => @artist.id, :license_id => @license.id
22       @op.should be_valid
23     end
24     
25     context 'extを検証するとき' do
26       before do
27         @op = Factory.build :original_picture, :artist_id => @artist.id, :license_id => @license.id
28       end
29       it 'テストデータの確認' do
30         @op.ext = 'jpeg'
31         @op.should be_valid
32       end
33       it 'nullなら失敗する' do
34         @op.ext = ''
35         @op.should_not be_valid
36       end
37       it '5文字以上なら失敗する' do
38         @op.ext = 'a'*5
39         @op.should_not be_valid
40       end
41       it 'png,gif,jpeg以外なら失敗する' do
42         @op.ext = 'bmp'
43         @op.should_not be_valid
44       end
45     end
46     context 'widthを検証するとき' do
47       before do
48         @op = Factory.build :original_picture, :artist_id => @artist.id, :license_id => @license.id
49       end
50       it 'テストデータの確認' do
51         @op.width = 1
52         @op.should be_valid
53       end
54       it 'nullなら失敗する' do
55         @op.width = nil
56         @op.should_not be_valid
57       end
58       it '数値でなければ失敗する' do
59         @op.width = 'a'
60         @op.should_not be_valid
61       end
62       it '0なら失敗する' do
63         @op.width = '0'
64         @op.should_not be_valid
65       end
66       it '負でも失敗する' do
67         @op.width = -1
68         @op.should_not be_valid
69       end
70     end
71     context 'heightを検証するとき' do
72       before do
73         @op = Factory.build :original_picture, :artist_id => @artist.id, :license_id => @license.id
74       end
75       it 'テストデータの確認' do
76         @op.height = 1
77         @op.should be_valid
78       end
79       it 'nullなら失敗する' do
80         @op.height = nil
81         @op.should_not be_valid
82       end
83       it '数値でなければ失敗する' do
84         @op.height = 'a'
85         @op.should_not be_valid
86       end
87       it '0なら失敗する' do
88         @op.height = '0'
89         @op.should_not be_valid
90       end
91       it '負でも失敗する' do
92         @op.height = -1
93         @op.should_not be_valid
94       end
95     end
96     context 'filesizeを検証するとき' do
97       before do
98         @op = Factory.build :original_picture, :artist_id => @artist.id, :license_id => @license.id
99       end
100       it 'テストデータの確認' do
101         @op.filesize = 1
102         @op.should be_valid
103       end
104       it 'nullなら失敗する' do
105         @op.filesize = nil
106         @op.should_not be_valid
107       end
108       it '数値でなければ失敗する' do
109         @op.filesize = 'a'
110         @op.should_not be_valid
111       end
112       it '負なら失敗する' do
113         @op.filesize = '-1'
114         @op.should_not be_valid
115       end
116       it '2MB以上なら失敗する' do
117         @op.filesize = 2000000+1
118         @op.should_not be_valid
119       end
120     end
121     context 'artist_idを検証するとき' do
122       before do
123         @op = Factory.build :original_picture, :license_id => @license.id
124       end
125       it 'テストデータの確認' do
126         @op.artist_id = @artist.id
127         @op.should be_valid
128       end
129       it 'nullなら失敗する' do
130         @op.artist_id = nil
131         @op.should_not be_valid
132       end
133       it '数値でなければ失敗する' do
134         @op.artist_id = 'a'
135         @op.should_not be_valid
136       end
137       it '存在する絵師でなければ失敗する' do
138         @op.artist_id = 0
139         @op.should_not be_valid
140       end
141     end
142     context 'license_idを検証するとき' do
143       before do
144         @op = Factory.build :original_picture, :artist_id => @artist.id
145       end
146       it 'テストデータの確認' do
147         @op.license_id = @license.id
148         @op.should be_valid
149       end
150       it 'nullなら失敗する' do
151         @op.license_id = nil
152         @op.should_not be_valid
153       end
154       it '数値でなければ失敗する' do
155         @op.license_id = 'a'
156         @op.should_not be_valid
157       end
158       it '存在するlicenseでなければ失敗する' do
159         @op.license_id = 0
160         @op.should_not be_valid
161       end
162     end
163   end
164   
165   describe 'データ補充に於いて' do
166     before do
167     end
168     
169   end
170   
171   describe '作者判定に於いて' do
172     before do
173     end
174     it '自作の原画ならyes' do
175       op = Factory :original_picture, :artist_id => @artist.id
176       op.own?(@author).should == true
177     end
178     it '他人のならno' do
179       op = Factory :original_picture, :artist_id => @other_artist.id
180       op.own?(@author).should == false
181     end
182     it '作家が不明ならno' do
183       op = Factory :original_picture, :artist_id => @artist.id
184       op.own?(nil).should == false
185     end
186     it '作家が絵師でないならno' do
187       other_user = Factory( :user_yas)
188       op = Factory :original_picture, :artist_id => @artist.id
189       op.own?(@other_author).should == false
190     end
191   end
192   describe '単体取得に於いて' do
193     before do
194       @op = Factory :original_picture, :artist_id => @artist.id
195     end
196     it '指定のコミックを返す' do
197       pic = OriginalPicture.show @op.id, @author
198       pic.should eq @op
199     end
200     context '関連テーブルオプションがないとき' do
201       it 'ライセンスデータだけを含んでいる' do
202         r = OriginalPicture.show_include_opt
203         r.should eq [:license]
204       end
205     end
206     context '関連テーブルオプションで絵師を含ませたとき' do
207       it 'ライセンスデータと作者データを含んでいる' do
208         r = OriginalPicture.show_include_opt(:include => :artist)
209         r.should eq [:license, :artist]
210       end
211     end
212   end
213   describe '一覧取得に於いて' do
214     before do
215       @op = Factory :original_picture, :artist_id => @artist.id
216     end
217     context 'page補正について' do
218       it '文字列から数値に変換される' do
219         OriginalPicture.page('8').should eq 8
220       end
221       it 'nilの場合は1になる' do
222         OriginalPicture.page().should eq 1
223       end
224       it '0以下の場合は1になる' do
225         OriginalPicture.page('0').should eq 1
226       end
227     end
228     context 'page_size補正について' do
229       it '文字列から数値に変換される' do
230         OriginalPicture.page_size('7').should eq 7
231       end
232       it 'nilの場合はOriginalPicture.default_page_sizeになる' do
233         OriginalPicture.page_size().should eq OriginalPicture.default_page_size
234       end
235       it '0以下の場合はOriginalPicture.default_page_sizeになる' do
236         OriginalPicture.page_size('0').should eq OriginalPicture.default_page_size
237       end
238       it 'OriginalPicture.max_page_sizeを超えた場合はOriginalPicture.max_page_sizeになる' do
239         OriginalPicture.page_size('1000').should eq OriginalPicture.max_page_size
240       end
241     end
242     it 'リストを返す' do
243       pic = OriginalPicture.list @artist.id
244       pic.should eq [@op]
245     end
246     it '他人の原画は含んでいない' do
247       Factory :original_picture, :artist_id => @other_artist.id
248       pic = OriginalPicture.list @artist.id
249       pic.should eq [@op]
250     end
251     it '時系列で並んでいる' do
252       newpic = Factory :original_picture, :artist_id => @artist.id
253       pic = OriginalPicture.list @artist.id
254       pic.should eq [newpic, @op]
255     end
256     context 'DBに5件あって1ページの件数を2件に変えたとして' do
257       before do
258         @op2 = Factory :original_picture, :artist_id => @artist.id
259         @op3 = Factory :original_picture, :artist_id => @artist.id
260         @op4 = Factory :original_picture, :artist_id => @artist.id
261         @op5 = Factory :original_picture, :artist_id => @artist.id
262         OriginalPicture.stub(:default_page_size).and_return(2)
263       end
264       it '通常は2件を返す' do
265         pic = OriginalPicture.list @artist.id
266         pic.should have(2).items 
267       end
268       it 'page=1なら末尾2件を返す' do
269         #時系列で並んでいる
270         pic = OriginalPicture.list( @artist.id, {}, 1)
271         pic.should eq [@op5, @op4]
272       end
273       it 'page=2なら中間2件を返す' do
274         pic = OriginalPicture.list( @artist.id, {}, 2)
275         pic.should eq [@op3, @op2]
276       end
277       it 'page=3なら先頭1件を返す' do
278         pic = OriginalPicture.list( @artist.id, {}, 3)
279         pic.should eq [@op]
280       end
281     end
282   end
283   describe 'RMagick変換に於いて' do
284     before do
285       @op = Factory.build :original_picture, :artist_id => @artist.id
286     end
287     context 'つつがなく終わるとき' do
288       it '画像データをオブジェクト化している' do
289         Magick::Image.stub(:from_blob).and_return(['mgkobj'])
290         @op.data_to_mgk('mgkbin').should eq 'mgkobj'
291       end
292     end
293     context '失敗するとき' do
294       it 'Falseを返す' do
295         Magick::Image.should_receive(:from_blob).with(any_args).and_raise('StandardError')
296         @op.data_to_mgk('mgkbin').should be_false
297       end
298       it '全体エラーがセットされている' do
299         Magick::Image.should_receive(:from_blob).with(any_args).and_raise('StandardError')
300         lambda {
301           @op.data_to_mgk('mgkbin')
302         }.should change(@op.errors[:base], :count)
303       end
304     end
305   end
306   describe '作成・更新に於いて' do
307     before do
308       @op = Factory.build :original_picture, :artist_id => @artist.id
309       #RMagickのスタブをおいておく
310       class Mgk ; class Image ; end ; end
311       @filesize = 76543
312       Mgk::Image.stub(:from_blob).with(any_args).and_return([Mgk.new])
313       Mgk.any_instance.stub(:format).with(any_args).and_return('png')
314       Mgk.any_instance.stub(:rows).with(any_args).and_return(200)
315       Mgk.any_instance.stub(:columns).with(any_args).and_return(100)
316       Mgk.any_instance.stub(:filesize).with(any_args).and_return(@filesize)
317       Mgk.any_instance.stub(:to_blob).with(any_args).and_return('data')
318       #原画ファイル削除だけは必ず成功するものとしておく
319       PictureIO::LocalPicture.any_instance.stub(:delete).with(any_args).and_return(true)
320       #素材取得は新規作成ケースでテストしておく
321       ResourcePicture.stub(:update_picture).with(any_args).and_return(
322         Factory.build :resource_picture, :artist_id => @artist.id
323       )
324     end
325     context '事前チェック' do
326       before do
327         #すべての処理が正常パターンで通過すれば、一番深い分岐まで通る。
328         #それで外部のメソッド呼び出しだけに着目してテストする。
329         OriginalPicture.any_instance.stub(:data_to_mgk).with(any_args).and_return(Mgk.new)
330         OriginalPicture.any_instance.stub(:save).with(any_args).and_return(true)\r
331         PictureIO::LocalPicture.any_instance.stub(:put).with(any_args).and_return(true)
332         ResourcePicture.any_instance.stub(:store).with(any_args).and_return(true)
333       end
334       it 'RMagick変換を依頼している' do
335         OriginalPicture.any_instance.should_receive(:data_to_mgk).exactly(1)
336         @op.store 'bindata', @artist, @license.id\r
337       end
338       it '自身に属性をセットしている' do
339         OriginalPicture.any_instance.should_receive(:attributes=).exactly(1)
340         @op.store 'bindata', @artist, @license.id\r
341       end
342       it '自身が保存されている' do
343         OriginalPicture.any_instance.should_receive(:save).exactly(1)\r
344         @op.store 'bindata', @artist, @license.id\r
345       end
346       it 'PictureIoに画像データの保存を依頼している' do
347         PictureIO::LocalPicture.any_instance.should_receive(:put).with(any_args).exactly(1)
348         @op.store 'bindata', @artist, @license.id\r
349       end
350       it '素材モデルに対象素材を問い合わせている' do
351         ResourcePicture.should_receive(:update_picture).with(any_args).exactly(1)
352         @op.store 'bindata', @artist, @license.id\r
353       end
354       it '対象素材オブジェクトに保存を依頼している' do
355         ResourcePicture.any_instance.should_receive(:store).with(any_args).exactly(1)
356         @op.store 'bindata', @artist, @license.id\r
357       end
358     end
359     context 'つつがなく終わるとき' do
360       before do
361         #すべての処理を正常パターンで通過させ、保存機能をチェックする。
362         OriginalPicture.any_instance.stub(:data_to_mgk).with(any_args).and_return(Mgk.new)
363 #        OriginalPicture.any_instance.stub(:save).with(any_args).and_return(true)\r
364         PictureIO::LocalPicture.any_instance.stub(:put).with(any_args).and_return(true)
365         ResourcePicture.any_instance.stub(:store).with(any_args).and_return(true)
366       end
367       it '自身に属性をセットしている' do
368         lambda {
369           @op.store 'bindata', @artist, @license.id\r
370         }.should change @op, :filesize
371       end
372       it '原画モデルが作成されている' do
373         lambda {
374           @op.store 'bindata', @artist, @license.id
375         }.should change OriginalPicture, :count
376       end
377       it '原画が保存されている' do
378         @op.store 'bindata', @artist, @license.id
379         OriginalPicture.find(@op).should_not be_nil\r
380       end
381       it 'Trueを返す' do
382         @op.store('bindata', @artist, @license.id).should eq true\r
383       end
384     end
385     #以下から例外ケース。処理先頭から失敗させていく
386     context 'RMagick変換が失敗したとき' do
387       before do
388         OriginalPicture.any_instance.stub(:data_to_mgk).with(any_args).and_return(false)
389       end
390       it 'falseを返す' do
391         @op.store('bindata', @artist, @license.id).should be_false\r
392       end
393       it '自身の保存は呼ばれていない' do
394         OriginalPicture.any_instance.should_not_receive(:save)\r
395       end
396     end
397     context '自身の保存に失敗したとき' do
398       before do
399         OriginalPicture.any_instance.stub(:data_to_mgk).with(any_args).and_return(Mgk.new)
400         OriginalPicture.any_instance.stub(:save).with(any_args).and_return(false)\r
401       end
402       it 'falseを返す' do
403         @op.store('bindata', @artist, @license.id).should be_false\r
404       end
405       it '更新されていない' do
406         @op.store('bindata', @artist, @license.id)
407         @op.should be_a_new OriginalPicture\r
408       end
409       it '原画の保存は呼ばれていない' do
410         PictureIO::LocalPicture.any_instance.should_not_receive(:put)\r
411       end
412     end
413     context '原画の保存に失敗したとき' do
414       before do
415         OriginalPicture.any_instance.stub(:data_to_mgk).with(any_args).and_return(Mgk.new)
416         OriginalPicture.any_instance.stub(:save).with(any_args).and_return(true)\r
417         PictureIO::LocalPicture.any_instance.stub(:put).with(any_args).and_return(false)
418       end
419       it 'falseを返す' do
420         @op.store('bindata', @artist, @license.id).should be_false\r
421       end
422       it '更新されていない' do
423         @op.store('bindata', @artist, @license.id)\r
424         @op.should be_a_new OriginalPicture\r
425       end
426       it '対象素材オブジェクトの保存は呼ばれていない' do
427         ResourcePicture.any_instance.should_not_receive(:save)\r
428         @op.store('bindata', @artist, @license.id)\r
429       end
430       it '全体エラーメッセージがセットされている' do
431         lambda {
432           @op.store('bindata', @artist, @license.id)\r
433         }.should change(@op.errors[:base], :count)
434       end
435     end
436     context '対象素材オブジェクトの保存に失敗したとき' do
437       before do
438         OriginalPicture.any_instance.stub(:data_to_mgk).with(any_args).and_return(Mgk.new)
439         OriginalPicture.any_instance.stub(:save).with(any_args).and_return(true)\r
440         PictureIO::LocalPicture.any_instance.stub(:put).with(any_args).and_return(true)
441         ResourcePicture.any_instance.stub(:store).with(any_args).and_return(false)
442       end
443       it 'falseを返す' do
444         @op.store('bindata', @artist, @license.id).should be_false\r
445       end
446       it '更新されていない' do
447         @op.store('bindata', @artist, @license.id)\r
448         @op.should be_a_new OriginalPicture\r
449       end
450       it '全体エラーメッセージがセットされている' do
451         lambda {
452           @op.store('bindata', @artist, @license.id)\r
453         }.should change(@op.errors[:base], :count)
454       end
455     end
456   end
457 end