OSDN Git Service

t#30286:artist separates outer artist
[pettanr/pettanr.git] / spec / models / artist_spec.rb
1 # -*- encoding: utf-8 -*-
2 #絵師
3 require 'spec_helper'
4
5 describe Artist do
6   before do
7     @admin = FactoryGirl.create :admin
8     @user = FactoryGirl.create( :user_yas)
9     @author = FactoryGirl.create :author, :user_id => @user.id
10     @other_user = FactoryGirl.create( :user_yas)
11     @other_author = FactoryGirl.create :author, :user_id => @other_user.id
12     @sp = FactoryGirl.create :system_picture
13     @lg = FactoryGirl.create :license_group
14     @license = FactoryGirl.create :license, :license_group_id => @lg.id, :system_picture_id => @sp.id
15   end
16
17   describe '検証に於いて' do
18     before do
19       @artist = FactoryGirl.build :artist, :author_id => @author.id
20     end
21     
22     context 'オーソドックスなデータのとき' do
23       it '下限データが通る' do
24         @artist.name = 'a'
25         @artist.should be_valid
26       end
27       it '上限データが通る' do
28         @artist.name = 'a'*30
29         @artist.should be_valid
30       end
31     end
32     
33     context 'nameを検証するとき' do
34       it 'nullなら失敗する' do
35         @artist.name = nil
36         @artist.should_not be_valid
37       end
38       it '30文字以上なら失敗する' do
39         @artist.name = 'a'*31
40         @artist.should_not be_valid
41       end
42     end
43     context 'author_idを検証するとき' do
44       #絵師は独立絵師があるので数値チェックだけ
45       it '数値でなければ失敗する' do
46         @artist.author_id = 'a'
47         @artist.should_not be_valid
48       end
49     end
50   end
51   
52   describe 'デフォルト値補充に於いて' do
53     it '名前がno nameになっている' do
54       @artist = FactoryGirl.build :artist, :name => nil
55       @artist.supply_default
56       @artist.name.should eq 'no name'
57     end
58   end
59   
60   describe '上書き補充に於いて' do
61     it '作家idが設定されている' do
62       @artist = FactoryGirl.build :artist
63       @artist.overwrite @author
64       @artist.author_id.should eq @author.id
65     end
66   end
67   
68   describe '所持判定に於いて' do
69     before do
70       @artist = FactoryGirl.create :artist, :author_id => @author.id
71       @other_artist = FactoryGirl.create :artist_yas, :author_id => @other_author.id
72     end
73     it '自分の絵師ならyes' do
74       @artist.own?(@author).should == true
75     end
76     it '他人の絵師ならno' do
77       @artist.own?(@other_author).should == false
78     end
79     it 'パラメータが作家でないならならno' do
80       @artist.own?(nil).should == false
81     end
82   end
83   
84   describe '閲覧許可に於いて' do
85     before do
86       @artist = FactoryGirl.create :artist, :author_id => @author.id
87     end
88     context '検査対象がnil(ゲスト)のとき' do
89       context 'クローズドモードのとき' do
90         before do
91           MagicNumber['run_mode'] = 1
92         end
93         it '不許可を返す。' do
94           r = @artist.visible?(nil)
95           r.should be_false
96         end
97       end
98       context 'オープンモードのとき' do
99         before do
100           MagicNumber['run_mode'] = 0
101         end
102         it '許可する' do
103           r = @artist.visible?(nil)
104           r.should be_true
105         end
106       end
107     end
108     context '検査対象が作家のとき' do
109       it '許可する' do
110         r = @artist.visible?(@author)
111         r.should == true
112       end
113     end
114     context '検査対象がそれ以外のとき' do
115       it '不許可を返す。' do
116         r = @artist.visible?(@admin)
117         r.should be_false
118       end
119     end
120   end
121   
122   describe '一覧取得に於いて' do
123     before do
124       @artist = FactoryGirl.create :artist, :author_id => @author.id
125     end
126     context 'page補正について' do
127       it '文字列から数値に変換される' do
128         Artist.page('8').should eq 8
129       end
130       it 'nilの場合は1になる' do
131         Artist.page().should eq 1
132       end
133       it '0以下の場合は1になる' do
134         Artist.page('0').should eq 1
135       end
136     end
137     context 'page_size補正について' do
138       it '文字列から数値に変換される' do
139         Artist.page_size('7').should eq 7
140       end
141       it 'nilの場合はArtist.default_page_sizeになる' do
142         Artist.page_size().should eq Artist.default_page_size
143       end
144       it '0以下の場合はArtist.default_page_sizeになる' do
145         Artist.page_size('0').should eq Artist.default_page_size
146       end
147       it 'Artist.max_page_sizeを超えた場合はArtist.max_page_sizeになる' do
148         Artist.page_size('1000').should eq Artist.max_page_size
149       end
150     end
151     context 'つつがなく終わるとき' do
152       it '一覧取得オプションを利用している' do
153         Artist.stub(:list_opt).with(any_args).and_return({})
154         Artist.should_receive(:list_opt).with(any_args).exactly(1)
155         r = Artist.list
156       end
157     end
158     it 'リストを返す' do
159       r = Artist.list
160       r.should eq [@artist]
161     end
162     it '作成時系列で並んでいる' do
163       n = FactoryGirl.create :artist, :author_id => @author.id, :name => 'artist', :created_at => Time.now + 100
164       l = Artist.list
165       l.should eq [n, @artist]
166     end
167     it 'ただし、内絵師(作家idが空)だけとする' do
168       n = FactoryGirl.create :artist, :author_id => nil
169       l = Artist.list
170       l.should eq [@artist]
171     end
172     context 'DBに5件あって1ページの件数を2件に変えたとして' do
173       before do
174         @artist2 = FactoryGirl.create :artist, :author_id => @author.id, :name => 'artist2', :created_at => Time.now + 100
175         @artist3 = FactoryGirl.create :artist, :author_id => @author.id, :name => 'artist3', :created_at => Time.now + 200
176         @artist4 = FactoryGirl.create :artist, :author_id => @author.id, :name => 'artist4', :created_at => Time.now + 300
177         @artist5 = FactoryGirl.create :artist, :author_id => @author.id, :name => 'artist5', :created_at => Time.now + 400
178         Artist.stub(:default_page_size).and_return(2)
179       end
180       it '通常は2件を返す' do
181         r = Artist.list
182         r.should have(2).items 
183       end
184       it 'page=1なら末尾2件を返す' do
185         #時系列で並んでいる
186         r = Artist.list(1)
187         r.should eq [@artist5, @artist4]
188       end
189       it 'page=2なら中間2件を返す' do
190         r = Artist.list(2)
191         r.should eq [@artist3, @artist2]
192       end
193       it 'page=3なら先頭1件を返す' do
194         r = Artist.list(3)
195         r.should eq [@artist]
196       end
197     end
198     context 'DBに5件あって1ページの件数を0件に変えたとして' do
199       before do
200         @artist2 = FactoryGirl.create :artist, :author_id => @author.id, :name => 'artist2', :created_at => Time.now + 100
201         @artist3 = FactoryGirl.create :artist, :author_id => @author.id, :name => 'artist3', :created_at => Time.now + 200
202         @artist4 = FactoryGirl.create :artist, :author_id => @author.id, :name => 'artist4', :created_at => Time.now + 300
203         @artist5 = FactoryGirl.create :artist, :author_id => @author.id, :name => 'artist5', :created_at => Time.now + 400
204         Artist.stub(:default_page_size).and_return(2)
205       end
206       it '通常は全件(5件)を返す' do
207         r = Artist.list 5, 0
208         r.should have(5).items 
209       end
210     end
211   end
212   describe '一覧取得オプションに於いて' do
213     it 'includeキーを含んでいる' do
214       r = Artist.list_opt
215       r.has_key?(:include).should be_true
216     end
217     it '1つの項目を含んでいる' do
218       r = Artist.list_opt[:include]
219       r.should have(1).items
220     end
221     it '作家を含んでいる' do
222       r = Artist.list_opt[:include]
223       r.has_key?(:author).should be_true
224     end
225   end
226   describe 'json一覧出力オプションに於いて' do
227     before do
228       @artist = FactoryGirl.create :artist, :author_id => @author.id
229     end
230     it '作家を含んでいる' do
231       r = Artist.list.to_json Artist.list_json_opt
232       j = JSON.parse r
233       i = j.first
234       i.has_key?('author').should be_true
235     end
236   end
237   
238   describe '単体取得に於いて' do
239     before do
240       @artist = FactoryGirl.create :artist, :author_id => @author.id
241     end
242     context 'つつがなく終わるとき' do
243       it '単体取得オプションを利用している' do
244         Artist.stub(:show_opt).with(any_args).and_return({})
245         Artist.should_receive(:show_opt).with(any_args).exactly(1)
246         r = Artist.show @artist.id, @author
247       end
248       it '閲覧許可を問い合わせている' do
249         Artist.any_instance.stub(:visible?).with(any_args).and_return(true)
250         Artist.any_instance.should_receive(:visible?).with(any_args).exactly(1)
251         r = Artist.show @artist.id, @author
252       end
253     end
254     it '指定の絵師を返す' do
255       a = Artist.show @artist.id, @author
256       a.should eq @artist
257     end
258     context '閲覧許可が出なかったとき' do
259       it '403Forbidden例外を返す' do
260         Artist.any_instance.stub(:visible?).and_return(false)
261         lambda{
262           Artist.show @artist.id, @author
263         }.should raise_error(ActiveRecord::Forbidden)
264       end
265     end
266     context '存在しない絵師を開こうとしたとき' do
267       it '404RecordNotFound例外を返す' do
268         lambda{
269           Artist.show 110, @author
270         }.should raise_error(ActiveRecord::RecordNotFound)
271       end
272     end
273   end
274   describe '編集取得に於いて' do
275     before do
276       @artist = FactoryGirl.create :artist, :author_id => @author.id
277     end
278     context 'つつがなく終わるとき' do
279       it '単体取得オプションを利用している' do
280         Artist.stub(:show_opt).with(any_args).and_return({})
281         Artist.should_receive(:show_opt).with(any_args).exactly(1)
282         r = Artist.edit @artist.id, @author
283       end
284       it '所持判定を問い合わせている' do
285         Artist.any_instance.stub(:own?).with(any_args).and_return(true)
286         Artist.any_instance.should_receive(:own?).with(any_args).exactly(1)
287         r = Artist.edit @artist.id, @author
288       end
289     end
290     it '指定の絵師を返す' do
291       Artist.any_instance.stub(:own?).and_return(true)
292       r = Artist.edit @artist.id, @author.id
293       r.should eq @artist
294     end
295     context '他人の絵師を開こうとしたとき' do
296       it '403Forbidden例外を返す' do
297         Artist.any_instance.stub(:own?).and_return(false)
298         lambda{
299           Artist.edit @artist.id, @author
300         }.should raise_error(ActiveRecord::Forbidden)
301       end
302     end
303     context '存在しない絵師を開こうとしたとき' do
304       it '404RecordNotFound例外を返す' do
305         lambda{
306           Artist.edit 110, @author
307         }.should raise_error(ActiveRecord::RecordNotFound)
308       end
309     end
310   end
311   describe '単体取得オプションに於いて' do
312     it 'includeキーを含んでいる' do
313       r = Artist.show_opt
314       r.has_key?(:include).should be_true
315     end
316     it '1つの項目を含んでいる' do
317       r = Artist.show_opt[:include]
318       r.should have(1).items
319     end
320   end
321   describe 'json単体出力オプションに於いて' do
322     before do
323       @artist = FactoryGirl.create :artist, :author_id => @author.id
324     end
325     it '作家を含んでいる' do
326       r = Artist.show(@artist.id, @author).to_json Artist.show_json_opt
327       j = JSON.parse r
328       i = j
329       i.has_key?('author').should be_true
330     end
331   end
332   
333   describe '有効絵師数に於いて' do
334     before do
335       @artist = FactoryGirl.create :artist, :author_id => @author.id
336       @artist = FactoryGirl.create :artist, :author_id => @other_author.id
337       @artist = FactoryGirl.create :artist, :author_id => nil
338     end
339     it '内絵師数を返す' do
340       r = Artist.visible_count
341       r.should eq 2
342     end
343   end
344
345 end