OSDN Git Service

c73ec3b02378f0b0c6e95507db197eba0c9d8d40
[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     end
22     
23     it 'オーソドックスなデータなら通る' do
24       @comic = FactoryGirl.build :comic, :author_id => @author.id
25       @comic.should be_valid
26     end
27     
28     context 'titleを検証するとき' do
29       it 'nullなら失敗する' do
30         @comic = FactoryGirl.build :comic, :author_id => @author.id, :title => nil
31         @comic.should_not be_valid
32       end
33       it '100文字以上なら失敗する' do
34         @comic = FactoryGirl.build :comic, :author_id => @author.id, :title => 'a'*101
35         @comic.should_not be_valid
36       end
37     end
38     context 'visibleを検証するとき' do
39       it 'nullなら失敗する' do
40         @comic = FactoryGirl.build :comic, :author_id => @author.id, :visible => nil
41         @comic.should_not be_valid
42       end
43       it '負なら失敗する' do
44         @comic = FactoryGirl.build :comic, :author_id => @author.id, :visible => -1
45         @comic.should_not be_valid
46       end
47       it '4以上なら失敗する' do
48         @comic = FactoryGirl.build :comic, :author_id => @author.id, :visible => 4
49         @comic.should_not be_valid
50       end
51     end
52   end
53   
54   describe 'データ補充に於いて' do
55     before do
56       @comic = Comic.new
57     end
58     
59     context '初期値を補充するとき' do
60       it '空なら0が補充される' do
61         @comic.supply_default
62         @comic.visible.should == 0
63       end
64       it 'visibleが空でないなら変化なし' do
65         @comic.visible = 1
66         lambda{@comic.supply_default}.should_not change(@comic, :visible)
67       end
68     end
69   end
70   
71   describe '作者判定に於いて' do
72     before do
73     end
74     it '自作のコミックならyes' do
75       comic = FactoryGirl.create :comic, :author_id => @author.id
76       comic.own?(@author).should == true
77     end
78     it '他人のコミックならno' do
79       comic = FactoryGirl.create :comic, :author_id => 0
80       comic.own?(@author).should == false
81     end
82     it '作家が不明ならno' do
83       comic = FactoryGirl.create :comic, :author_id => @author.id
84       comic.own?(nil).should == false
85     end
86   end
87   describe '閲覧許可に於いて' do
88     before do
89     end
90     it '自作の公開コミックを見るときは許可する' do
91       Comic.any_instance.stub(:own?).and_return(true)
92       comic = FactoryGirl.create :comic, :author_id => @author.id, :visible => 3
93       comic.visible?(@author).should == true
94     end
95     it '自作のコミックは非公開でも許可する' do
96       Comic.any_instance.stub(:own?).and_return(true)
97       comic = FactoryGirl.create :comic, :author_id => @author.id, :visible => 0
98       comic.visible?(@author).should == true
99     end
100     it '他人のコミックでも公開コミックなら許可する' do
101       Comic.any_instance.stub(:own?).and_return(false)
102       comic = FactoryGirl.create :comic, :author_id => 0, :visible => 3
103       comic.visible?(@author).should == true
104     end
105     it '他人のコミックで非公開コミックなら許可しない' do
106       Comic.any_instance.stub(:own?).and_return(false)
107       comic = FactoryGirl.create :comic, :author_id => 0, :visible => 0
108       comic.visible?(@author).should == false
109     end
110   end
111   describe '単体取得に於いて' do
112     before do
113       @comic = FactoryGirl.create :comic, :author_id => @author.id
114     end
115     it '指定のコミックを返す' do
116       c = Comic.show @comic.id, @author.id
117       c.should eq @comic
118     end
119   end
120   describe '編集取得に於いて' do
121     before do
122       @comic = FactoryGirl.create :comic, :author_id => @author.id
123     end
124     it '指定のコミックを返す' do
125       Comic.any_instance.stub(:own?).and_return(true)
126       c = Comic.edit @comic.id, @author.id
127       c.should eq @comic
128     end
129     context '他人のコミックを開こうとしたとき' do
130       it '403Forbidden例外を返す' do
131         Comic.any_instance.stub(:own?).and_return(false)
132         lambda{
133           Comic.edit @comic.id, @author
134         }.should raise_error(ActiveRecord::Forbidden)
135       end
136     end
137     context '存在しないコミックを開こうとしたとき' do
138       it '404RecordNotFound例外を返す' do
139         lambda{
140           Comic.edit 110, @author
141         }.should raise_error(ActiveRecord::RecordNotFound)
142       end
143     end
144   end
145   describe '関連テーブルプションに於いて' do
146     context 'オプションがないとき' do
147       it '2つの項目を含んでいる' do
148         r = Comic.show_include_opt
149         r.should have(2).items
150       end
151       it '作家を含んでいる' do
152         r = Comic.show_include_opt
153         r.has_key?(:author).should be_true
154       end
155       it 'ストーリーを含んでいる' do
156         r = Comic.show_include_opt
157         r.has_key?(:stories).should be_true
158       end
159         it 'ストーリーはコマを含んでいる' do
160           r = Comic.show_include_opt
161           r[:stories].has_key?(:panel).should be_true
162         end
163     end
164   end
165   describe 'json単体出力オプションに於いて' do
166     it 'includeキーを含んでいる' do
167       r = Comic.show_json_include_opt
168       r.has_key?(:include).should be_true
169     end
170     it '2つの項目を含んでいる' do
171       r = Comic.show_json_include_opt[:include]
172       r.should have(2).items
173     end
174     it '作家を含んでいる' do
175       r = Comic.show_json_include_opt[:include]
176       r.has_key?(:author).should be_true
177     end
178     it 'ストーリーを含んでいる' do
179       r = Comic.show_json_include_opt[:include]
180       r.has_key?(:stories).should be_true
181     end
182       it 'ストーリーはコマを含んでいる' do
183         r = Comic.show_json_include_opt[:include]
184         r[:stories].has_key?(:panel).should be_true
185       end
186   end
187   describe '一覧取得に於いて' do
188     before do
189       @comic = FactoryGirl.create :comic, :author_id => @author.id
190     end
191     context 'page補正について' do
192       it '文字列から数値に変換される' do
193         Comic.page('8').should eq 8
194       end
195       it 'nilの場合は1になる' do
196         Comic.page().should eq 1
197       end
198       it '0以下の場合は1になる' do
199         Comic.page('0').should eq 1
200       end
201     end
202     context 'page_size補正について' do
203       it '文字列から数値に変換される' do
204         Comic.page_size('7').should eq 7
205       end
206       it 'nilの場合はComic.default_page_sizeになる' do
207         Comic.page_size().should eq Comic.default_page_size
208       end
209       it '0以下の場合はComic.default_page_sizeになる' do
210         Comic.page_size('0').should eq Comic.default_page_size
211       end
212       it 'Comic.max_page_sizeを超えた場合はComic.max_page_sizeになる' do
213         Comic.page_size('1000').should eq Comic.max_page_size
214       end
215     end
216     it 'リストを返す' do
217       c = Comic.list
218       c.should eq [@comic]
219     end
220     it '非公開コミックは(自分のコミックであっても)含んでいない' do
221       FactoryGirl.create :comic, :author_id => @author.id, :visible => 0
222       c = Comic.list
223       c.should eq [@comic]
224     end
225     it '時系列で並んでいる' do
226       v = FactoryGirl.create :comic, :author_id => @author.id, :updated_at => Time.now + 100
227       c = Comic.list
228       c.should eq [v, @comic]
229     end
230     context 'DBに5件あって1ページの件数を2件に変えたとして' do
231       before do
232         @comic2 = FactoryGirl.create :comic, :author_id => @author.id, :updated_at => Time.now + 100
233         @comic3 = FactoryGirl.create :comic, :author_id => @author.id, :updated_at => Time.now + 200
234         @comic4 = FactoryGirl.create :comic, :author_id => @author.id, :updated_at => Time.now + 300
235         @comic5 = FactoryGirl.create :comic, :author_id => @author.id, :updated_at => Time.now + 400
236         Comic.stub(:default_page_size).and_return(2)
237       end
238       it '通常は2件を返す' do
239         c = Comic.list
240         c.should have(2).items 
241       end
242       it 'page=1なら末尾2件を返す' do
243         #時系列で並んでいる
244         c = Comic.list({}, 1)
245         c.should eq [@comic5, @comic4]
246       end
247       it 'page=2なら中間2件を返す' do
248         c = Comic.list({}, 2)
249         c.should eq [@comic3, @comic2]
250       end
251       it 'page=3なら先頭1件を返す' do
252         c = Comic.list({}, 3)
253         c.should eq [@comic]
254       end
255     end
256   end
257   describe 'list関連テーブルプションに於いて' do
258     it 'includeキーを含んでいる' do
259       r = Comic.list_opt
260       r.has_key?(:include).should be_true
261     end
262     it '2つの項目を含んでいる' do
263       r = Comic.list_opt[:include]
264       r.should have(2).items
265     end
266     it 'ストーリーを含んでいる' do
267       r = Comic.list_opt[:include]
268       r.has_key?(:stories).should be_true
269     end
270       it 'ストーリーはコマを含んでいる' do
271         r = Comic.list_opt[:include]
272         r[:stories].has_key?(:panel).should be_true
273       end
274     it '作家を含んでいる' do
275       r = Comic.list_opt[:include]
276       r.has_key?(:author).should be_true
277     end
278   end
279   describe 'json一覧出力オプションに於いて' do
280     it 'includeキーを含んでいる' do
281       r = Comic.list_json_opt
282       r.has_key?(:include).should be_true
283     end
284     it '2つの項目を含んでいる' do
285       r = Comic.list_json_opt[:include]
286       r.should have(2).items
287     end
288     it 'ストーリーを含んでいる' do
289       r = Comic.list_json_opt[:include]
290       r.has_key?(:stories).should be_true
291     end
292       it 'ストーリーはコマを含んでいる' do
293         r = Comic.list_json_opt[:include]
294         r[:stories].has_key?(:panel).should be_true
295       end
296     it '作家を含んでいる' do
297       r = Comic.list_json_opt[:include]
298       r.has_key?(:author).should be_true
299     end
300   end
301   
302   describe '自分のコミック一覧取得に於いて' do
303     before do
304       @comic = FactoryGirl.create :comic, :author_id => @author.id
305     end
306     it 'リストを返す' do
307       c = Comic.mylist @author
308       c.should eq [@comic]
309     end
310     it '時系列で並んでいる' do
311       nc = FactoryGirl.create :comic, :author_id => @author.id, :updated_at => Time.now + 100
312       cl = Comic.mylist @author
313       cl.should eq [nc, @comic]
314     end
315     it '他人のコミックは公開でも含まない' do
316       nc = FactoryGirl.create :comic, :author_id => @other_author.id, :visible => 1
317       cl = Comic.mylist @author
318       cl.should eq [@comic]
319     end
320     it '自分のコミックは非公開でも含んでいる' do
321       nc = FactoryGirl.create :comic, :author_id => @author.id, :visible => 0, :updated_at => Time.now + 100
322       cl = Comic.mylist @author
323       cl.should eq [nc, @comic]
324     end
325   end
326   
327 end