OSDN Git Service

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