OSDN Git Service

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