OSDN Git Service

t#29510:
[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(2)
209       end
210       it '通常は全件(5件)を返す' do
211         r = Author.list 5, 0
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     before do
232       @artist = FactoryGirl.create :artist_yas, :author_id => @author.id
233     end
234     it '絵師を含んでいる' do
235       r = Author.list.to_json Author.list_json_opt
236       j = JSON.parse r
237       i = j.first
238       i.has_key?('artist').should be_true
239     end
240   end
241   
242   describe '単体取得に於いて' do
243     before do
244     end
245     context 'つつがなく終わるとき' do\r
246       it '単体取得オプションを利用している' do\r
247         Author.stub(:show_opt).with(any_args).and_return({})\r
248         Author.should_receive(:show_opt).with(any_args).exactly(1)\r
249         r = Author.show @author.id, @author
250       end\r
251       it '閲覧許可を問い合わせている' do\r
252         Author.any_instance.stub(:visible?).with(any_args).and_return(true)\r
253         Author.any_instance.should_receive(:visible?).with(any_args).exactly(1)\r
254         r = Author.show @author.id, @author
255       end\r
256     end\r
257     it '指定の作家を返す' do
258       r = Author.show @author.id, @author
259       r.should eq @author
260     end
261     context '閲覧許可が出なかったとき' do\r
262       it '403Forbidden例外を返す' do\r
263         Author.any_instance.stub(:visible?).and_return(false)\r
264         lambda{\r
265           Author.show @author.id, @author\r
266         }.should raise_error(ActiveRecord::Forbidden)\r
267       end\r
268     end\r
269     context '存在しない作家を開こうとしたとき' do\r
270       it '404RecordNotFound例外を返す' do\r
271         lambda{\r
272           Author.show 110, @author\r
273         }.should raise_error(ActiveRecord::RecordNotFound)\r
274       end\r
275     end\r
276   end
277   describe '編集取得に於いて' do
278     before do
279     end
280     context 'つつがなく終わるとき' do\r
281       it '単体取得オプションを利用している' do\r
282         Author.stub(:show_opt).with(any_args).and_return({})\r
283         Author.should_receive(:show_opt).with(any_args).exactly(1)\r
284         r = Author.edit @author.id, @author
285       end\r
286       it '所持判定を問い合わせている' do\r
287         Author.any_instance.stub(:own?).with(any_args).and_return(true)\r
288         Author.any_instance.should_receive(:own?).with(any_args).exactly(1)\r
289         r = Author.edit @author.id, @author
290       end\r
291     end\r
292     it '指定の作家を返す' do
293       Author.any_instance.stub(:own?).and_return(true)
294       r = Author.edit @author.id, @author.id
295       r.should eq @author
296     end
297     context '他人の作家を開こうとしたとき' do
298       it '403Forbidden例外を返す' do
299         Author.any_instance.stub(:own?).and_return(false)
300         lambda{
301           Author.edit @author.id, @author
302         }.should raise_error(ActiveRecord::Forbidden)
303       end
304     end
305     context '存在しない作家を開こうとしたとき' do
306       it '404RecordNotFound例外を返す' do
307         lambda{
308           Author.edit 110, @author
309         }.should raise_error(ActiveRecord::RecordNotFound)
310       end
311     end
312   end
313   describe '単体取得オプションに於いて' do
314     it 'includeキーを含んでいる' do
315       r = Author.show_opt
316       r.has_key?(:include).should be_true
317     end
318     it '1つの項目を含んでいる' do
319       r = Author.show_opt[:include]
320       r.should have(1).items
321     end
322     it '絵師を含んでいる' do
323       r = Author.show_opt[:include]
324       r.has_key?(:artist).should be_true
325     end
326   end
327   describe 'json単体出力オプションに於いて' do
328     before do
329       @artist = FactoryGirl.create :artist_yas, :author_id => @author.id
330     end
331     it '絵師を含んでいる' do
332       r = Author.show(@author.id, @author).to_json Author.show_json_opt
333       j = JSON.parse r
334       i = j
335       i.has_key?('artist').should be_true
336     end
337   end
338   
339   describe 'マイリストページ制御パラメータに於いて' do
340     before do
341     end
342     context 'コミックpage_size補正について' do
343       it '文字列から数値に変換される' do
344         Author.comic_page_size('7').should eq 7
345       end
346       it 'nilの場合はAuthor.default_comic_page_sizeになる' do
347         Author.comic_page_size().should eq Author.default_comic_page_size
348       end
349       it '0以下の場合はAuthor.default_comic_page_sizeになる' do
350         Author.comic_page_size('0').should eq Author.default_comic_page_size
351       end
352       it 'Author.comic_max_page_sizeを超えた場合はAuthor.comic_max_page_sizeになる' do
353         Author.comic_page_size('1000').should eq Author.comic_max_page_size
354       end
355     end
356     context 'ストーリーpage_size補正について' do
357       it '文字列から数値に変換される' do
358         Author.story_page_size('7').should eq 7
359       end
360       it 'nilの場合はAuthor.default_story_page_sizeになる' do
361         Author.story_page_size().should eq Author.default_story_page_size
362       end
363       it '0以下の場合はAuthor.default_story_page_sizeになる' do
364         Author.story_page_size('0').should eq Author.default_story_page_size
365       end
366       it 'Author.story_max_page_sizeを超えた場合はAuthor.story_max_page_sizeになる' do
367         Author.story_page_size('1000').should eq Author.story_max_page_size
368       end
369     end
370     context 'コマ絵page_size補正について' do
371       it '文字列から数値に変換される' do
372         Author.panel_page_size('7').should eq 7
373       end
374       it 'nilの場合はAuthor.default_panel_page_sizeになる' do
375         Author.panel_page_size().should eq Author.default_panel_page_size
376       end
377       it '0以下の場合はAuthor.default_panel_page_sizeになる' do
378         Author.panel_page_size('0').should eq Author.default_panel_page_size
379       end
380       it 'Author.panel_max_page_sizeを超えた場合はAuthor.panel_max_page_sizeになる' do
381         Author.panel_page_size('1000').should eq Author.panel_max_page_size
382       end
383     end
384     context '景色素材page_size補正について' do
385       it '文字列から数値に変換される' do
386         Author.panel_picture_page_size('7').should eq 7
387       end
388       it 'nilの場合はAuthor.default_panel_picture_page_sizeになる' do
389         Author.panel_picture_page_size().should eq Author.default_panel_picture_page_size
390       end
391       it '0以下の場合はAuthor.default_panel_picture_page_sizeになる' do
392         Author.panel_picture_page_size('0').should eq Author.default_panel_picture_page_size
393       end
394       it 'Author.panel_picture_max_page_sizeを超えた場合はAuthor.panel_picture_max_page_sizeになる' do
395         Author.panel_picture_page_size('1000').should eq Author.panel_picture_max_page_size
396       end
397     end
398     context '景色カラーpage_size補正について' do
399       it '文字列から数値に変換される' do
400         Author.ground_picture_page_size('7').should eq 7
401       end
402       it 'nilの場合はAuthor.default_ground_picture_page_sizeになる' do
403         Author.ground_picture_page_size().should eq Author.default_ground_picture_page_size
404       end
405       it '0以下の場合はAuthor.default_ground_picture_page_sizeになる' do
406         Author.ground_picture_page_size('0').should eq Author.default_ground_picture_page_size
407       end
408       it 'Author.ground_picture_max_page_sizeを超えた場合はAuthor.ground_picture_max_page_sizeになる' do
409         Author.ground_picture_page_size('1000').should eq Author.ground_picture_max_page_size
410       end
411     end
412     context '景色カラーコードpage_size補正について' do
413       it '文字列から数値に変換される' do
414         Author.ground_color_page_size('7').should eq 7
415       end
416       it 'nilの場合はAuthor.default_ground_color_page_sizeになる' do
417         Author.ground_color_page_size().should eq Author.default_ground_color_page_size
418       end
419       it '0以下の場合はAuthor.default_ground_color_page_sizeになる' do
420         Author.ground_color_page_size('0').should eq Author.default_ground_color_page_size
421       end
422       it 'Author.ground_color_max_page_sizeを超えた場合はAuthor.ground_color_max_page_sizeになる' do
423         Author.ground_color_page_size('1000').should eq Author.ground_color_max_page_size
424       end
425     end
426   end
427 end