1 # -*- encoding: utf-8 -*-
4 describe SystemPicture do
6 @admin = Factory :admin
7 @license = Factory :license
14 it 'オーソドックスなデータなら通る' do
15 @sp = Factory.build :system_picture
19 context 'extを検証するとき' do
21 @sp = Factory.build :system_picture
29 @sp.should_not be_valid
33 @sp.should_not be_valid
35 it 'png,gif,jpeg以外なら失敗する' do
37 @sp.should_not be_valid
40 context 'widthを検証するとき' do
42 @sp = Factory.build :system_picture
50 @sp.should_not be_valid
54 @sp.should_not be_valid
58 @sp.should_not be_valid
62 @sp.should_not be_valid
65 context 'heightを検証するとき' do
67 @sp = Factory.build :system_picture
75 @sp.should_not be_valid
79 @sp.should_not be_valid
83 @sp.should_not be_valid
87 @sp.should_not be_valid
90 context 'filesizeを検証するとき' do
92 @sp = Factory.build :system_picture
100 @sp.should_not be_valid
104 @sp.should_not be_valid
108 @sp.should_not be_valid
111 @sp.filesize = 2000000+1
112 @sp.should_not be_valid
115 context 'md5を検証するとき' do
117 @sp = Factory.build :system_picture
120 @sp.md5 = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'
125 @sp.should_not be_valid
129 @sp.should_not be_valid
134 describe 'データ補充に於いて' do
140 describe '作成・更新に於いて' do
142 @sp = Factory.build :system_picture
144 class Mgk ; class Image ; end ; end
146 Mgk::Image.stub(:from_blob).with(any_args).and_return([Mgk.new])
147 Mgk.any_instance.stub(:format).with(any_args).and_return('png')
148 Mgk.any_instance.stub(:rows).with(any_args).and_return(200)
149 Mgk.any_instance.stub(:columns).with(any_args).and_return(100)
150 Mgk.any_instance.stub(:filesize).with(any_args).and_return(@filesize)
151 Mgk.any_instance.stub(:to_blob).with(any_args).and_return('data')
155 #すべての処理が正常パターンで通過すれば、一番深い分岐まで通る。
156 #それで外部のメソッド呼び出しだけに着目してテストする。
157 SystemPicture.any_instance.stub(:data_to_mgk).with(any_args).and_return(Mgk.new)
158 SystemPicture.any_instance.stub(:save).with(any_args).and_return(true)
159 PictureIO::LocalPicture.any_instance.stub(:put).with(any_args).and_return(true)
161 it 'RMagick変換を依頼している' do
162 SystemPicture.any_instance.should_receive(:data_to_mgk).exactly(1)
165 it '自身に属性をセットしている' do
166 SystemPicture.any_instance.should_receive(:attributes=).exactly(1)
170 SystemPicture.any_instance.should_receive(:save).exactly(1)
173 it 'PictureIoに画像データの保存を依頼している' do
174 PictureIO::LocalPicture.any_instance.should_receive(:put).with(any_args).exactly(1)
178 context 'つつがなく終わるとき' do
180 #すべての処理を正常パターンで通過させ、保存機能をチェックする。
181 SystemPicture.any_instance.stub(:data_to_mgk).with(any_args).and_return(Mgk.new)
182 # SystemPicture.any_instance.stub(:save).with(any_args).and_return(true)
183 PictureIO::LocalPicture.any_instance.stub(:put).with(any_args).and_return(true)
185 it '自身に属性をセットしている' do
188 }.should change @sp, :filesize
190 it 'システム画像モデルが作成されている' do
193 }.should change SystemPicture, :count
195 it 'システム画像が保存されている' do
197 SystemPicture.find(@sp).should_not be_nil
200 @sp.store('bindata').should eq true
203 #以下から例外ケース。処理先頭から失敗させていく
204 context 'RMagick変換が失敗したとき' do
206 SystemPicture.any_instance.stub(:data_to_mgk).with(any_args).and_return(false)
209 @sp.store('bindata').should be_false
211 it '自身の保存は呼ばれていない' do
212 SystemPicture.any_instance.should_not_receive(:save)
215 context '自身の保存に失敗したとき' do
217 SystemPicture.any_instance.stub(:data_to_mgk).with(any_args).and_return(Mgk.new)
218 SystemPicture.any_instance.stub(:save).with(any_args).and_return(false)
221 @sp.store('bindata').should be_false
225 @sp.should be_a_new SystemPicture
227 it '原画の保存は呼ばれていない' do
228 PictureIO::LocalPicture.any_instance.should_not_receive(:put)
235 @sp = Factory :system_picture
239 SystemPicture.stub(:find_by_md5).with(any_args).and_return(nil)
240 SystemPicture.any_instance.stub(:store).with(any_args).and_return(true)
242 it '新規オブジェクト生成している' do
243 r = SystemPicture.store 'bindata'
244 r.should be_a_new SystemPicture
247 SystemPicture.any_instance.should_receive(:store).with('bindata').exactly(1)
248 SystemPicture.store 'bindata'
251 r = SystemPicture.store 'bindata'
257 SystemPicture.stub(:find_by_md5).with(any_args).and_return(@sp)
258 SystemPicture.any_instance.stub(:store).with(any_args).and_return(true)
260 it '新規オブジェクト生成していない' do
261 SystemPicture.should_receive(:new).with(any_args).exactly(0)
262 SystemPicture.store 'bindata'
265 SystemPicture.any_instance.should_receive(:store).with('bindata').exactly(1)
266 SystemPicture.store 'bindata'
269 r = SystemPicture.store 'bindata'
275 SystemPicture.stub(:find_by_md5).with(any_args).and_return(@sp)
276 SystemPicture.any_instance.stub(:store).with(any_args).and_return(false)
279 r = SystemPicture.store 'bindata'