OSDN Git Service

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