OSDN Git Service

ead28463b304a41b536b0012feb3ae78ace88c2e
[pettanr/pettanr.git] / spec / models / system_picture_spec.rb
1 # -*- encoding: utf-8 -*-
2 require 'spec_helper'
3 #システム画像
4 describe SystemPicture do
5   before do
6     @admin = FactoryGirl.create :admin
7   end
8   
9   describe '検証に於いて' do
10     before do
11     end
12     
13     it 'オーソドックスなデータなら通る' do
14       @sp = FactoryGirl.build :system_picture
15       @sp.should be_valid
16     end
17     
18     context 'extを検証するとき' do
19       before do
20         @sp = FactoryGirl.build :system_picture
21       end
22       it 'テストデータの確認' do
23         @sp.ext = 'jpeg'
24         @sp.should be_valid
25       end
26       it 'nullなら失敗する' do
27         @sp.ext = ''
28         @sp.should_not be_valid
29       end
30       it '5文字以上なら失敗する' do
31         @sp.ext = 'a'*5
32         @sp.should_not be_valid
33       end
34       it 'png,gif,jpeg以外なら失敗する' do
35         @sp.ext = 'bmp'
36         @sp.should_not be_valid
37       end
38     end
39     context 'widthを検証するとき' do
40       before do
41         @sp = FactoryGirl.build :system_picture
42       end
43       it 'テストデータの確認' do
44         @sp.width = 1
45         @sp.should be_valid
46       end
47       it 'nullなら失敗する' do
48         @sp.width = nil
49         @sp.should_not be_valid
50       end
51       it '数値でなければ失敗する' do
52         @sp.width = 'a'
53         @sp.should_not be_valid
54       end
55       it '0なら失敗する' do
56         @sp.width = '0'
57         @sp.should_not be_valid
58       end
59       it '負でも失敗する' do
60         @sp.width = -1
61         @sp.should_not be_valid
62       end
63     end
64     context 'heightを検証するとき' do
65       before do
66         @sp = FactoryGirl.build :system_picture
67       end
68       it 'テストデータの確認' do
69         @sp.height = 1
70         @sp.should be_valid
71       end
72       it 'nullなら失敗する' do
73         @sp.height = nil
74         @sp.should_not be_valid
75       end
76       it '数値でなければ失敗する' do
77         @sp.height = 'a'
78         @sp.should_not be_valid
79       end
80       it '0なら失敗する' do
81         @sp.height = '0'
82         @sp.should_not be_valid
83       end
84       it '負でも失敗する' do
85         @sp.height = -1
86         @sp.should_not be_valid
87       end
88     end
89     context 'filesizeを検証するとき' do
90       before do
91         @sp = FactoryGirl.build :system_picture
92       end
93       it 'テストデータの確認' do
94         @sp.filesize = 1
95         @sp.should be_valid
96       end
97       it 'nullなら失敗する' do
98         @sp.filesize = nil
99         @sp.should_not be_valid
100       end
101       it '数値でなければ失敗する' do
102         @sp.filesize = 'a'
103         @sp.should_not be_valid
104       end
105       it '負なら失敗する' do
106         @sp.filesize = '-1'
107         @sp.should_not be_valid
108       end
109       it '2MB以上なら失敗する' do
110         @sp.filesize = 2000000+1
111         @sp.should_not be_valid
112       end
113     end
114     context 'md5を検証するとき' do
115       before do
116         @sp = FactoryGirl.build :system_picture
117       end
118       it 'テストデータの確認' do
119         @sp.md5 = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'
120         @sp.should be_valid
121       end
122       it 'nullなら失敗する' do
123         @sp.md5 = ''
124         @sp.should_not be_valid
125       end
126       it '32文字以上なら失敗する' do
127         @sp.md5 = 'a'*33
128         @sp.should_not be_valid
129       end
130     end
131   end
132   
133   describe 'データ補充に於いて' do
134     before do
135     end
136     
137   end
138   
139   describe '作成・更新に於いて' do
140     before do
141       @sp = FactoryGirl.build :system_picture
142       #RMagickのスタブをおいておく
143       class Mgk ; class Image ; end ; end
144       @filesize = 76543
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')
151     end
152     context '事前チェック' do
153       before do
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)
159       end
160       it 'RMagick変換を依頼している' do
161         SystemPicture.any_instance.should_receive(:data_to_mgk).exactly(1)
162         @sp.store 'bindata'
163       end
164       it '自身に属性をセットしている' do
165         SystemPicture.any_instance.should_receive(:attributes=).exactly(1)
166         @sp.store 'bindata'
167       end
168       it '自身が保存されている' do
169         SystemPicture.any_instance.should_receive(:save).exactly(1)
170         @sp.store 'bindata'
171       end
172       it 'PictureIoに画像データの保存を依頼している' do
173         PictureIO::LocalPicture.any_instance.should_receive(:put).with(any_args).exactly(1)
174         @sp.store 'bindata'
175       end
176     end
177     context 'つつがなく終わるとき' do
178       before 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)
183       end
184       it '自身に属性をセットしている' do
185         lambda {
186           @sp.store 'bindata'
187         }.should change @sp, :filesize
188       end
189       it 'システム画像モデルが作成されている' do
190         lambda {
191           @sp.store 'bindata'
192         }.should change SystemPicture, :count
193       end
194       it 'システム画像が保存されている' do
195         @sp.store 'bindata'
196         SystemPicture.find(@sp).should_not be_nil
197       end
198       it 'Trueを返す' do
199         @sp.store('bindata').should eq true
200       end
201     end
202     #以下から例外ケース。処理先頭から失敗させていく
203     context 'RMagick変換が失敗したとき' do
204       before do
205         SystemPicture.any_instance.stub(:data_to_mgk).with(any_args).and_return(false)
206       end
207       it 'falseを返す' do
208         @sp.store('bindata').should be_false
209       end
210       it '自身の保存は呼ばれていない' do
211         SystemPicture.any_instance.should_not_receive(:save)
212       end
213     end
214     context '自身の保存に失敗したとき' do
215       before 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)
218       end
219       it 'falseを返す' do
220         @sp.store('bindata').should be_false
221       end
222       it '更新されていない' do
223         @sp.store('bindata')
224         @sp.should be_a_new SystemPicture
225       end
226       it '原画の保存は呼ばれていない' do
227         PictureIO::LocalPicture.any_instance.should_not_receive(:put)
228       end
229     end
230   end
231   
232   describe '置換に於いて' do
233     before do
234       @sp = FactoryGirl.create :system_picture
235     end
236     context '新規のとき' do
237       before do
238         SystemPicture.stub(:find_by_md5).with(any_args).and_return(nil)
239         SystemPicture.any_instance.stub(:store).with(any_args).and_return(true)
240       end
241       it '新規オブジェクト生成している' do
242         r = SystemPicture.store 'bindata'
243         r.should be_a_new SystemPicture
244       end
245       it '作成依頼している' do
246         SystemPicture.any_instance.should_receive(:store).with('bindata').exactly(1)
247         SystemPicture.store 'bindata'
248       end
249       it '保存された行を返す' do
250         r = SystemPicture.store 'bindata'
251         r.should_not eq @sp
252       end
253     end
254     context '既存のとき' do
255       before do
256         SystemPicture.stub(:find_by_md5).with(any_args).and_return(@sp)
257         SystemPicture.any_instance.stub(:store).with(any_args).and_return(true)
258       end
259       it '新規オブジェクト生成していない' do
260         SystemPicture.should_receive(:new).with(any_args).exactly(0)
261         SystemPicture.store 'bindata'
262       end
263       it '作成依頼している' do
264         SystemPicture.any_instance.should_receive(:store).with('bindata').exactly(1)
265         SystemPicture.store 'bindata'
266       end
267       it '保存された行を返す' do
268         r = SystemPicture.store 'bindata'
269         r.should eq @sp
270       end
271     end
272     context '保存失敗のとき' do
273       before do
274         SystemPicture.stub(:find_by_md5).with(any_args).and_return(@sp)
275         SystemPicture.any_instance.stub(:store).with(any_args).and_return(false)
276       end
277       it 'nilを返す' do
278         r = SystemPicture.store 'bindata'
279         r.should eq nil
280       end
281     end
282   end
283 end