1 # -*- encoding: utf-8 -*-
4 describe SystemPicture do
6 @admin = FactoryGirl.create :admin
13 it 'オーソドックスなデータなら通る' do
14 @sp = FactoryGirl.build :system_picture
18 context 'extを検証するとき' do
20 @sp = FactoryGirl.build :system_picture
28 @sp.should_not be_valid
32 @sp.should_not be_valid
34 it 'png,gif,jpeg以外なら失敗する' do
36 @sp.should_not be_valid
39 context 'widthを検証するとき' do
41 @sp = FactoryGirl.build :system_picture
49 @sp.should_not be_valid
53 @sp.should_not be_valid
57 @sp.should_not be_valid
61 @sp.should_not be_valid
64 context 'heightを検証するとき' do
66 @sp = FactoryGirl.build :system_picture
74 @sp.should_not be_valid
78 @sp.should_not be_valid
82 @sp.should_not be_valid
86 @sp.should_not be_valid
89 context 'filesizeを検証するとき' do
91 @sp = FactoryGirl.build :system_picture
99 @sp.should_not be_valid
103 @sp.should_not be_valid
107 @sp.should_not be_valid
110 @sp.filesize = 2000000+1
111 @sp.should_not be_valid
114 context 'md5を検証するとき' do
116 @sp = FactoryGirl.build :system_picture
119 @sp.md5 = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'
124 @sp.should_not be_valid
128 @sp.should_not be_valid
133 describe 'データ補充に於いて' do
139 describe '作成・更新に於いて' do
141 @sp = FactoryGirl.build :system_picture
143 class Mgk ; class Image ; end ; end
145 Mgk::Image.stub(:from_blob).with(any_args).and_return([Mgk.new])
146 Mgk.any_instance.stub(:format).with(any_args).and_return('png')
147 Mgk.any_instance.stub(:rows).with(any_args).and_return(200)
148 Mgk.any_instance.stub(:columns).with(any_args).and_return(100)
149 Mgk.any_instance.stub(:filesize).with(any_args).and_return(@filesize)
150 Mgk.any_instance.stub(:to_blob).with(any_args).and_return('data')
154 #すべての処理が正常パターンで通過すれば、一番深い分岐まで通る。
155 #それで外部のメソッド呼び出しだけに着目してテストする。
156 SystemPicture.any_instance.stub(:data_to_mgk).with(any_args).and_return(Mgk.new)
157 SystemPicture.any_instance.stub(:save).with(any_args).and_return(true)
158 PictureIO::LocalPicture.any_instance.stub(:put).with(any_args).and_return(true)
160 it 'RMagick変換を依頼している' do
161 SystemPicture.any_instance.should_receive(:data_to_mgk).exactly(1)
164 it '自身に属性をセットしている' do
165 SystemPicture.any_instance.should_receive(:attributes=).exactly(1)
169 SystemPicture.any_instance.should_receive(:save).exactly(1)
172 it 'PictureIoに画像データの保存を依頼している' do
173 PictureIO::LocalPicture.any_instance.should_receive(:put).with(any_args).exactly(1)
177 context 'つつがなく終わるとき' do
179 #すべての処理を正常パターンで通過させ、保存機能をチェックする。
180 SystemPicture.any_instance.stub(:data_to_mgk).with(any_args).and_return(Mgk.new)
181 # SystemPicture.any_instance.stub(:save).with(any_args).and_return(true)
182 PictureIO::LocalPicture.any_instance.stub(:put).with(any_args).and_return(true)
184 it '自身に属性をセットしている' do
187 }.should change @sp, :filesize
189 it 'システム画像モデルが作成されている' do
192 }.should change SystemPicture, :count
194 it 'システム画像が保存されている' do
196 SystemPicture.find(@sp).should_not be_nil
199 @sp.store('bindata').should eq true
202 #以下から例外ケース。処理先頭から失敗させていく
203 context 'RMagick変換が失敗したとき' do
205 SystemPicture.any_instance.stub(:data_to_mgk).with(any_args).and_return(false)
208 @sp.store('bindata').should be_false
210 it '自身の保存は呼ばれていない' do
211 SystemPicture.any_instance.should_not_receive(:save)
214 context '自身の保存に失敗したとき' do
216 SystemPicture.any_instance.stub(:data_to_mgk).with(any_args).and_return(Mgk.new)
217 SystemPicture.any_instance.stub(:save).with(any_args).and_return(false)
220 @sp.store('bindata').should be_false
224 @sp.should be_a_new SystemPicture
226 it '原画の保存は呼ばれていない' do
227 PictureIO::LocalPicture.any_instance.should_not_receive(:put)
234 @sp = FactoryGirl.create :system_picture
238 SystemPicture.stub(:find_by_md5).with(any_args).and_return(nil)
239 SystemPicture.any_instance.stub(:store).with(any_args).and_return(true)
241 it '新規オブジェクト生成している' do
242 r = SystemPicture.store 'bindata'
243 r.should be_a_new SystemPicture
246 SystemPicture.any_instance.should_receive(:store).with('bindata').exactly(1)
247 SystemPicture.store 'bindata'
250 r = SystemPicture.store 'bindata'
256 SystemPicture.stub(:find_by_md5).with(any_args).and_return(@sp)
257 SystemPicture.any_instance.stub(:store).with(any_args).and_return(true)
259 it '新規オブジェクト生成していない' do
260 SystemPicture.should_receive(:new).with(any_args).exactly(0)
261 SystemPicture.store 'bindata'
264 SystemPicture.any_instance.should_receive(:store).with('bindata').exactly(1)
265 SystemPicture.store 'bindata'
268 r = SystemPicture.store 'bindata'
274 SystemPicture.stub(:find_by_md5).with(any_args).and_return(@sp)
275 SystemPicture.any_instance.stub(:store).with(any_args).and_return(false)
278 r = SystemPicture.store 'bindata'