OSDN Git Service

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