OSDN Git Service

t#29400:update:itr2
[pettanr/pettanr.git] / spec / models / color_spec.rb
1 # -*- encoding: utf-8 -*-
2 require 'spec_helper'
3 #色マスター
4
5 describe Color do
6   before do
7     @f = Rails.root + 'spec/json/color.json'
8     @fs = Rails.root + 'spec/json/colors.json'
9     FactoryGirl.create :admin
10     @user = FactoryGirl.create( :user_yas)
11     @author = @user.author
12     @artist = FactoryGirl.create :artist_yas, :author_id => @author.id
13     @other_user = FactoryGirl.create( :user_yas)
14     @other_author = @other_user.author
15     @other_artist = FactoryGirl.create :artist_yas, :author_id => @other_author.id
16     @sp = FactoryGirl.create :system_picture
17     @lg = FactoryGirl.create :license_group
18     @license = FactoryGirl.create :license, :license_group_id => @lg.id, :system_picture_id => @sp.id
19   end
20   
21   describe '検証に於いて' do
22     before do
23       @c = FactoryGirl.build :color
24     end
25     
26     context 'オーソドックスなデータのとき' do
27       it '下限データが通る' do
28         @c.name = 'a'
29         @c.code = 0
30         @c.t = 0
31         @c.should be_valid
32       end
33       it '上限データが通る' do
34         @author.name = 'a'*30
35         @c.code = 0xffffff
36         @c.t = 99999
37         @c.should be_valid
38       end
39     end
40     
41     context 'nameを検証するとき' do
42       it 'nullなら失敗する' do
43         @c.name = ''
44         @c.should_not be_valid
45       end
46       it '51文字以上なら失敗する' do
47         @c.name = 'a'*51
48         @c.should_not be_valid
49       end
50       it '重複していたら失敗する' do
51         l = FactoryGirl.create :color
52         @c.should_not be_valid
53       end
54     end
55     context 'codeを検証するとき' do
56       it 'nullなら失敗する' do
57         @c.code = nil
58         @c.should_not be_valid
59       end
60       it '数値でなければ失敗する' do
61         @c.code = 'a'
62         @c.should_not be_valid
63       end
64       it '負なら失敗する' do
65         @c.code = -1
66         @c.should_not be_valid
67       end
68       it '24bit colorでなければ失敗する' do
69         @c.code = 0x1000000
70         @c.should_not be_valid
71       end
72     end
73     context 'tを検証するとき' do
74       it '数値でなければ失敗する' do
75         @c.t = 'a'
76         @c.should_not be_valid
77       end
78       it '0なら通る' do
79         @c.t = '0'
80         @c.should be_valid
81       end
82       it '負なら失敗する' do
83         @c.t = -1
84         @c.should_not be_valid
85       end
86     end
87   end
88   
89   describe 'デフォルト値補充に於いて' do
90     it '名前がno nameになっている' do
91       @c = FactoryGirl.build :color, :name => nil
92       @c.supply_default
93       @c.name.should eq 'no name'
94     end
95   end
96   
97   describe '上書き補充に於いて' do
98     it 'defined' do
99       @c = FactoryGirl.build :color
100       @c.overwrite
101     end
102   end
103   
104   describe '上書き補充に於いて' do
105   end
106   
107   describe '閲覧許可に於いて' do
108     before do
109       @c = FactoryGirl.build :color
110     end
111     it '許可する' do\r
112       @c.visible?(@author).should == true
113     end\r
114   end
115   
116   describe '一覧取得に於いて' do
117     before do
118       @c = FactoryGirl.create :color
119     end
120     context 'page補正について' do
121       it '文字列から数値に変換される' do
122         Color.page('8').should eq 8
123       end
124       it 'nilの場合は1になる' do
125         Color.page().should eq 1
126       end
127       it '0以下の場合は1になる' do
128         Color.page('0').should eq 1
129       end
130     end
131     context 'page_size補正について' do
132       it '文字列から数値に変換される' do
133         Color.page_size('7').should eq 7
134       end
135       it 'nilの場合はColor.default_page_sizeになる' do
136         Color.page_size().should eq Color.default_page_size
137       end
138       it '0以下の場合はColor.default_page_sizeになる' do
139         Color.page_size('0').should eq Color.default_page_size
140       end
141       it 'Color.max_page_sizeを超えた場合はColor.max_page_sizeになる' do
142         Color.page_size('1000').should eq Color.max_page_size
143       end
144     end
145     context 'つつがなく終わるとき' do\r
146       it '一覧取得オプションを利用している' do\r
147         Color.stub(:list_opt).with(any_args).and_return({})\r
148         Color.should_receive(:list_opt).with(any_args).exactly(1)\r
149         r = Color.list
150       end\r
151     end\r
152     it 'リストを返す' do
153       r = Color.list
154       r.should eq [@c]
155     end
156     it 'tで並んでいる' do
157       n = FactoryGirl.create :color, :name => 'new color', :t => 1
158       l = Color.list
159       l.should eq [@c, n]
160     end
161     context 'DBに5件あって1ページの件数を2件に変えたとして' do
162       before do
163         @color2 = FactoryGirl.create :color, :name => 'new color a', :t => 1
164         @color3 = FactoryGirl.create :color, :name => 'new color b', :t => 2
165         @color4 = FactoryGirl.create :color, :name => 'new color c', :t => 3
166         @color5 = FactoryGirl.create :color, :name => 'new color d', :t => 4
167         Color.stub(:default_page_size).and_return(2)
168       end
169       it '通常は2件を返す' do
170         r = Color.list
171         r.should have(2).items 
172       end
173       it 'page=1なら末尾2件を返す' do
174         #時系列で並んでいる
175         r = Color.list(1)
176         r.should eq [@c, @color2]
177       end
178       it 'page=2なら中間2件を返す' do
179         r = Color.list(2)
180         r.should eq [@color3, @color4]
181       end
182       it 'page=3なら先頭1件を返す' do
183         r = Color.list(3)
184         r.should eq [@color5]
185       end
186     end
187     context 'DBに5件あって1ページの件数を0件に変えたとして' do
188       before do
189         @color2 = FactoryGirl.create :color, :name => 'new color a', :t => 1
190         @color3 = FactoryGirl.create :color, :name => 'new color b', :t => 2
191         @color4 = FactoryGirl.create :color, :name => 'new color c', :t => 3
192         @color5 = FactoryGirl.create :color, :name => 'new color d', :t => 4
193         Color.stub(:default_page_size).and_return(0)
194       end
195       it '通常は全件(5件)を返す' do
196         r = Color.list
197         r.should have(5).items 
198       end
199     end
200   end
201   describe '一覧取得オプションに於いて' do
202     it '空のHashを返す' do
203       r = Color.list_opt
204       r.is_a?(Hash).should be_true
205       r.should be_empty
206     end
207   end
208   describe 'json一覧出力オプションに於いて' do
209     it '空のHashを返す' do
210       r = Color.list_json_opt
211       r.is_a?(Hash).should be_true
212       r.should be_empty
213     end
214   end
215   
216   describe '単体取得に於いて' do
217     before do
218       @c = FactoryGirl.create :color
219     end
220     context 'つつがなく終わるとき' do\r
221       it '単体取得オプションを利用している' do\r
222         Color.stub(:show_opt).with(any_args).and_return({})\r
223         Color.should_receive(:show_opt).with(any_args).exactly(1)\r
224         r = Color.show @c.id, @author
225       end\r
226       it '閲覧許可を問い合わせている' do\r
227         Color.any_instance.stub(:visible?).with(any_args).and_return(true)\r
228         Color.any_instance.should_receive(:visible?).with(any_args).exactly(1)\r
229         r = Color.show @c.id, @author
230       end\r
231     end\r
232     it '指定の色を返す' do
233       r = Color.show @author.id, @author
234       r.should eq @c
235     end
236     context '閲覧許可が出なかったとき' do\r
237       it '403Forbidden例外を返す' do\r
238         Color.any_instance.stub(:visible?).and_return(false)\r
239         lambda{\r
240           Color.show @c.id, @author\r
241         }.should raise_error(ActiveRecord::Forbidden)\r
242       end\r
243     end\r
244     context '存在しない色を開こうとしたとき' do\r
245       it '404RecordNotFound例外を返す' do\r
246         lambda{\r
247           Color.show 110, @author\r
248         }.should raise_error(ActiveRecord::RecordNotFound)\r
249       end\r
250     end\r
251   end
252   
253   describe '単体取得オプションに於いて' do
254     it '空のHashを返す' do
255       r = Color.show_opt
256       r.is_a?(Hash).should be_true
257       r.should be_empty
258     end
259   end
260   describe 'json単体出力オプションに於いて' do
261     it '空のHashを返す' do
262       r = Color.show_json_opt
263       r.is_a?(Hash).should be_true
264       r.should be_empty
265     end
266   end
267   
268   describe 'インポートに於いて' do
269     before do
270     end
271     context 'つつがなく終わるとき' do
272       it 'ファイルインポートを依頼する' do
273         Color.should_receive(:import_file).with(any_args).exactly(1)
274         Color.stub(:import_file).with(any_args).and_return([])
275         Color.import(@f)
276       end
277       it '色更新を一回依頼する' do
278         Color.stub(:store).with(any_args).and_return(Color.new)
279         Color.should_receive(:store).with(any_args).exactly(1)
280         Color.import(@f)
281       end
282       it '色が追加される' do
283         lambda {
284           Color.import(@f)
285         }.should change Color, :count
286       end
287       it '[]を返す' do
288         r = Color.import(@f)
289         r.should eq []
290       end
291     end
292     context '複数データがつつがなく終わるとき' do
293       it '色更新を二回依頼する' do
294         Color.stub(:store).with(any_args).and_return(Color.new)
295         Color.should_receive(:store).with(any_args).exactly(2)
296         Color.import(@fs)
297       end
298       it '色が二個追加される' do
299         lambda {
300           r = Color.import(@fs)
301         }.should change(Color, :count).by 2
302       end
303       it '[]を返す' do
304         r = Color.import(@fs)
305         r.should eq []
306       end
307     end
308   end
309   
310 end