OSDN Git Service

Merge branch 'v04' of git.sourceforge.jp:/gitroot/pettanr/pettanr into v04
[pettanr/pettanr.git] / spec / models / author_spec.rb
1 # -*- encoding: utf-8 -*-
2 #作家
3 require 'spec_helper'
4
5 describe Author do
6   before do
7     FactoryGirl.create :admin
8     @user = FactoryGirl.create( :user_yas)
9     @author = @user.author
10   end
11
12   describe '検証に於いて' do
13     before do
14     end
15     
16     context 'オーソドックスなデータのとき' do
17       it '下限データが通る' do
18         @author.name = 'a'
19         @author.should be_valid
20       end
21       it '上限データが通る' do
22         @author.name = 'a'*30
23         @author.should be_valid
24       end
25     end
26     
27     context 'nameを検証するとき' do
28       it 'nullなら失敗する' do
29         @author.name = nil
30         @author.should_not be_valid
31       end
32       it '30文字以上なら失敗する' do
33         @author.name = 'a'*31
34         @author.should_not be_valid
35       end
36     end
37     context 'user_idを検証するとき' do
38       it 'nullなら失敗する' do
39         @author.user_id = nil
40         @author.should_not be_valid
41       end
42       it '数値でなければ失敗する' do
43         @author.user_id = 'a'
44         @author.should_not be_valid
45       end
46       it '存在するアカウントでなければ失敗する' do
47         @author.user_id = 0
48         @author.should_not be_valid
49       end
50     end
51   end
52   
53   describe 'デフォルト値補充に於いて' do
54     it '名前がno nameになっている' do
55       @author = FactoryGirl.build :author, :name => nil
56       @author.supply_default
57       @author.name.should eq 'no name'
58     end
59   end
60   
61   describe '上書き補充に於いて' do
62   end
63   
64   describe '所持判定に於いて' do
65     before do
66       @other_user = FactoryGirl.create :user_yas
67 #アカウントを作ると連動して作家ができる      @other_author = FactoryGirl.create :author_yas, :user_id => @other_user.id\r
68     end
69     it '作家自身ならyes' do
70       @author.own?(@author).should == true
71     end
72     it '作家自身でなければno' do
73       @author.own?(@other_author).should == false
74     end
75     it '作家が不明ならno' do
76       @author.own?(nil).should == false
77     end
78   end
79   
80   describe '閲覧許可に於いて' do
81     before do
82     end
83     it '許可する' do\r
84       @author.visible?(@author).should == true
85     end\r
86   end
87   
88   describe '絵師作家判定に於いて' do
89     before do
90     end
91     context 'Trueのケース' do
92       it 'リンクされた絵師がある' do
93         artist = FactoryGirl.create :artist, :author_id => @author.id
94         @author.artist?.should eq true
95       end
96     end
97     context 'Falseのケース' do
98       it 'リンクされた絵師がない' do
99         @author.artist?.should eq false
100       end
101     end
102   end
103   
104   describe '一覧取得に於いて' do
105     before do
106     end
107     context 'page補正について' do
108       it '文字列から数値に変換される' do
109         Author.page('8').should eq 8
110       end
111       it 'nilの場合は1になる' do
112         Author.page().should eq 1
113       end
114       it '0以下の場合は1になる' do
115         Author.page('0').should eq 1
116       end
117     end
118     context 'page_size補正について' do
119       it '文字列から数値に変換される' do
120         Author.page_size('7').should eq 7
121       end
122       it 'nilの場合はAuthor.default_page_sizeになる' do
123         Author.page_size().should eq Author.default_page_size
124       end
125       it '0以下の場合はAuthor.default_page_sizeになる' do
126         Author.page_size('0').should eq Author.default_page_size
127       end
128       it 'Author.max_page_sizeを超えた場合はAuthor.max_page_sizeになる' do
129         Author.page_size('1000').should eq Author.max_page_size
130       end
131     end
132     context 'つつがなく終わるとき' do\r
133       it '一覧取得オプションを利用している' do\r
134         Author.stub(:list_opt).with(any_args).and_return({})\r
135         Author.should_receive(:list_opt).with(any_args).exactly(1)\r
136         r = Author.list
137       end\r
138     end\r
139     it 'リストを返す' do
140       r = Author.list
141       r.should eq [@author]
142     end
143     it '作成時系列で並んでいる' do
144       @other_user = FactoryGirl.create :user_yas
145       #アカウントを作ると連動して作家ができる
146       n = @other_user.author
147       n.created_at = Time.now + 100
148       n.save
149       l = Author.list
150       l.should eq [n, @author]
151     end
152     context 'DBに5件あって1ページの件数を2件に変えたとして' do
153       before do
154         @other_user2 = FactoryGirl.create :user_yas
155         @author2 = @other_user2.author
156         @author2.created_at = Time.now + 100
157         @author2.save
158         @other_user3 = FactoryGirl.create :user_yas
159         @author3 = @other_user3.author
160         @author3.created_at = Time.now + 200
161         @author3.save
162         @other_user4 = FactoryGirl.create :user_yas
163         @author4 = @other_user4.author
164         @author4.created_at = Time.now + 300
165         @author4.save
166         @other_user5 = FactoryGirl.create :user_yas
167         @author5 = @other_user5.author
168         @author5.created_at = Time.now + 400
169         @author5.save
170         Author.stub(:default_page_size).and_return(2)
171       end
172       it '通常は2件を返す' do
173         r = Author.list
174         r.should have(2).items 
175       end
176       it 'page=1なら末尾2件を返す' do
177         #時系列で並んでいる
178         r = Author.list(1)
179         r.should eq [@author5, @author4]
180       end
181       it 'page=2なら中間2件を返す' do
182         r = Author.list(2)
183         r.should eq [@author3, @author2]
184       end
185       it 'page=3なら先頭1件を返す' do
186         r = Author.list(3)
187         r.should eq [@author]
188       end
189     end
190     context 'DBに5件あって1ページの件数を0件に変えたとして' do
191       before do
192         @other_user2 = FactoryGirl.create :user_yas
193         @author2 = @other_user2.author
194         @author2.created_at = Time.now + 100
195         @author2.save
196         @other_user3 = FactoryGirl.create :user_yas
197         @author3 = @other_user3.author
198         @author3.created_at = Time.now + 200
199         @author3.save
200         @other_user4 = FactoryGirl.create :user_yas
201         @author4 = @other_user4.author
202         @author4.created_at = Time.now + 300
203         @author4.save
204         @other_user5 = FactoryGirl.create :user_yas
205         @author5 = @other_user5.author
206         @author5.created_at = Time.now + 400
207         @author5.save
208         Author.stub(:default_page_size).and_return(0)
209       end
210       it '通常は全件(5件)を返す' do
211         r = Author.list
212         r.should have(5).items 
213       end
214     end
215   end
216   describe '一覧取得オプションに於いて' do
217     it 'includeキーを含んでいる' do
218       r = Author.list_opt
219       r.has_key?(:include).should be_true
220     end
221     it '1つの項目を含んでいる' do
222       r = Author.list_opt[:include]
223       r.should have(1).items
224     end
225     it '絵師を含んでいる' do
226       r = Author.list_opt[:include]
227       r.has_key?(:artist).should be_true
228     end
229   end
230   describe 'json一覧出力オプションに於いて' do
231     it 'includeキーを含んでいる' do
232       r = Author.list_json_opt
233       r.has_key?(:include).should be_true
234     end
235     it '1つの項目を含んでいる' do
236       r = Author.list_json_opt[:include]
237       r.should have(1).items
238     end
239     it '絵師を含んでいる' do
240       r = Author.list_json_opt[:include]
241       r.has_key?(:artist).should be_true
242     end
243   end
244   
245   describe '単体取得に於いて' do
246     before do
247     end
248     context 'つつがなく終わるとき' do\r
249       it '単体取得オプションを利用している' do\r
250         Author.stub(:show_opt).with(any_args).and_return({})\r
251         Author.should_receive(:show_opt).with(any_args).exactly(1)\r
252         r = Author.show @author.id, @author
253       end\r
254       it '閲覧許可を問い合わせている' do\r
255         Author.any_instance.stub(:visible?).with(any_args).and_return(true)\r
256         Author.any_instance.should_receive(:visible?).with(any_args).exactly(1)\r
257         r = Author.show @author.id, @author
258       end\r
259     end\r
260     it '指定の作家を返す' do
261       r = Author.show @author.id, @author
262       r.should eq @author
263     end
264     context '閲覧許可が出なかったとき' do\r
265       it '403Forbidden例外を返す' do\r
266         Author.any_instance.stub(:visible?).and_return(false)\r
267         lambda{\r
268           Author.show @author.id, @author\r
269         }.should raise_error(ActiveRecord::Forbidden)\r
270       end\r
271     end\r
272     context '存在しない作家を開こうとしたとき' do\r
273       it '404RecordNotFound例外を返す' do\r
274         lambda{\r
275           Author.show 110, @author\r
276         }.should raise_error(ActiveRecord::RecordNotFound)\r
277       end\r
278     end\r
279   end
280   describe '編集取得に於いて' do
281     before do
282     end
283     context 'つつがなく終わるとき' do\r
284       it '単体取得オプションを利用している' do\r
285         Author.stub(:show_opt).with(any_args).and_return({})\r
286         Author.should_receive(:show_opt).with(any_args).exactly(1)\r
287         r = Author.edit @author.id, @author
288       end\r
289       it '所持判定を問い合わせている' do\r
290         Author.any_instance.stub(:own?).with(any_args).and_return(true)\r
291         Author.any_instance.should_receive(:own?).with(any_args).exactly(1)\r
292         r = Author.edit @author.id, @author
293       end\r
294     end\r
295     it '指定の作家を返す' do
296       Author.any_instance.stub(:own?).and_return(true)
297       r = Author.edit @author.id, @author.id
298       r.should eq @author
299     end
300     context '他人の作家を開こうとしたとき' do
301       it '403Forbidden例外を返す' do
302         Author.any_instance.stub(:own?).and_return(false)
303         lambda{
304           Author.edit @author.id, @author
305         }.should raise_error(ActiveRecord::Forbidden)
306       end
307     end
308     context '存在しない作家を開こうとしたとき' do
309       it '404RecordNotFound例外を返す' do
310         lambda{
311           Author.edit 110, @author
312         }.should raise_error(ActiveRecord::RecordNotFound)
313       end
314     end
315   end
316   describe '単体取得オプションに於いて' do
317     it 'includeキーを含んでいる' do
318       r = Author.show_opt
319       r.has_key?(:include).should be_true
320     end
321     it '1つの項目を含んでいる' do
322       r = Author.show_opt[:include]
323       r.should have(1).items
324     end
325     it '絵師を含んでいる' do
326       r = Author.show_opt[:include]
327       r.has_key?(:artist).should be_true
328     end
329   end
330   describe 'json単体出力オプションに於いて' do
331     it 'includeキーを含んでいる' do
332       r = Author.show_json_opt
333       r.has_key?(:include).should be_true
334     end
335     it '1つの項目を含んでいる' do
336       r = Author.show_json_opt[:include]
337       r.should have(1).items
338     end
339     it '絵師を含んでいる' do
340       r = Author.show_json_opt[:include]
341       r.has_key?(:artist).should be_true
342     end
343   end
344   
345   describe 'マイリストページ制御パラメータに於いて' do
346     before do
347     end
348     context 'コミックpage_size補正について' do
349       it '文字列から数値に変換される' do
350         Author.comic_page_size('7').should eq 7
351       end
352       it 'nilの場合はAuthor.default_comic_page_sizeになる' do
353         Author.comic_page_size().should eq Author.default_comic_page_size
354       end
355       it '0以下の場合はAuthor.default_comic_page_sizeになる' do
356         Author.comic_page_size('0').should eq Author.default_comic_page_size
357       end
358       it 'Author.comic_max_page_sizeを超えた場合はAuthor.comic_max_page_sizeになる' do
359         Author.comic_page_size('1000').should eq Author.comic_max_page_size
360       end
361     end
362     context 'ストーリーpage_size補正について' do
363       it '文字列から数値に変換される' do
364         Author.story_page_size('7').should eq 7
365       end
366       it 'nilの場合はAuthor.default_story_page_sizeになる' do
367         Author.story_page_size().should eq Author.default_story_page_size
368       end
369       it '0以下の場合はAuthor.default_story_page_sizeになる' do
370         Author.story_page_size('0').should eq Author.default_story_page_size
371       end
372       it 'Author.story_max_page_sizeを超えた場合はAuthor.story_max_page_sizeになる' do
373         Author.story_page_size('1000').should eq Author.story_max_page_size
374       end
375     end
376     context 'コマ絵page_size補正について' do
377       it '文字列から数値に変換される' do
378         Author.panel_page_size('7').should eq 7
379       end
380       it 'nilの場合はAuthor.default_panel_page_sizeになる' do
381         Author.panel_page_size().should eq Author.default_panel_page_size
382       end
383       it '0以下の場合はAuthor.default_panel_page_sizeになる' do
384         Author.panel_page_size('0').should eq Author.default_panel_page_size
385       end
386       it 'Author.panel_max_page_sizeを超えた場合はAuthor.panel_max_page_sizeになる' do
387         Author.panel_page_size('1000').should eq Author.panel_max_page_size
388       end
389     end
390     context '景色素材page_size補正について' do
391       it '文字列から数値に変換される' do
392         Author.panel_picture_page_size('7').should eq 7
393       end
394       it 'nilの場合はAuthor.default_panel_picture_page_sizeになる' do
395         Author.panel_picture_page_size().should eq Author.default_panel_picture_page_size
396       end
397       it '0以下の場合はAuthor.default_panel_picture_page_sizeになる' do
398         Author.panel_picture_page_size('0').should eq Author.default_panel_picture_page_size
399       end
400       it 'Author.panel_picture_max_page_sizeを超えた場合はAuthor.panel_picture_max_page_sizeになる' do
401         Author.panel_picture_page_size('1000').should eq Author.panel_picture_max_page_size
402       end
403     end
404     context '景色カラーpage_size補正について' do
405       it '文字列から数値に変換される' do
406         Author.ground_picture_page_size('7').should eq 7
407       end
408       it 'nilの場合はAuthor.default_ground_picture_page_sizeになる' do
409         Author.ground_picture_page_size().should eq Author.default_ground_picture_page_size
410       end
411       it '0以下の場合はAuthor.default_ground_picture_page_sizeになる' do
412         Author.ground_picture_page_size('0').should eq Author.default_ground_picture_page_size
413       end
414       it 'Author.ground_picture_max_page_sizeを超えた場合はAuthor.ground_picture_max_page_sizeになる' do
415         Author.ground_picture_page_size('1000').should eq Author.ground_picture_max_page_size
416       end
417     end
418     context '景色カラーコードpage_size補正について' do
419       it '文字列から数値に変換される' do
420         Author.ground_color_page_size('7').should eq 7
421       end
422       it 'nilの場合はAuthor.default_ground_color_page_sizeになる' do
423         Author.ground_color_page_size().should eq Author.default_ground_color_page_size
424       end
425       it '0以下の場合はAuthor.default_ground_color_page_sizeになる' do
426         Author.ground_color_page_size('0').should eq Author.default_ground_color_page_size
427       end
428       it 'Author.ground_color_max_page_sizeを超えた場合はAuthor.ground_color_max_page_sizeになる' do
429         Author.ground_color_page_size('1000').should eq Author.ground_color_max_page_size
430       end
431     end
432   end
433 end