OSDN Git Service

t#29400:update artist, author:itr1
[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     FactoryGirl.create :admin
8     @user = FactoryGirl.create( :user_yas)
9     @author = @user.author
10     @other_user = FactoryGirl.create( :user_yas)\r
11     @other_author = @other_user.author\r
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\r
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     it '自分の絵師を見るときは許可する' do\r
89       Artist.any_instance.stub(:own?).and_return(true)\r
90       @artist.visible?(@author).should == true
91     end\r
92     it '他人の絵師でも許可する' do\r
93       Artist.any_instance.stub(:own?).and_return(false)\r
94       @artist.visible?(@author).should == true
95     end\r
96   end
97   
98   describe '一覧取得に於いて' do
99     before do
100       @artist = FactoryGirl.create :artist, :author_id => @author.id
101     end
102     context 'page補正について' do
103       it '文字列から数値に変換される' do
104         Artist.page('8').should eq 8
105       end
106       it 'nilの場合は1になる' do
107         Artist.page().should eq 1
108       end
109       it '0以下の場合は1になる' do
110         Artist.page('0').should eq 1
111       end
112     end
113     context 'page_size補正について' do
114       it '文字列から数値に変換される' do
115         Artist.page_size('7').should eq 7
116       end
117       it 'nilの場合はArtist.default_page_sizeになる' do
118         Artist.page_size().should eq Artist.default_page_size
119       end
120       it '0以下の場合はArtist.default_page_sizeになる' do
121         Artist.page_size('0').should eq Artist.default_page_size
122       end
123       it 'Artist.max_page_sizeを超えた場合はArtist.max_page_sizeになる' do
124         Artist.page_size('1000').should eq Artist.max_page_size
125       end
126     end
127     context 'つつがなく終わるとき' do\r
128       it '一覧取得オプションを利用している' do\r
129         Artist.stub(:list_opt).with(any_args).and_return({})\r
130         Artist.should_receive(:list_opt).with(any_args).exactly(1)\r
131         r = Artist.list
132       end\r
133     end\r
134     it 'リストを返す' do
135       r = Artist.list
136       r.should eq [@artist]
137     end
138     it '作成時系列で並んでいる' do
139       n = FactoryGirl.create :artist, :author_id => @author.id, :name => 'artist', :created_at => Time.now + 100
140       l = Artist.list
141       l.should eq [n, @artist]
142     end
143     context 'DBに5件あって1ページの件数を2件に変えたとして' do
144       before do
145         @artist2 = FactoryGirl.create :artist, :author_id => @author.id, :name => 'artist2', :created_at => Time.now + 100
146         @artist3 = FactoryGirl.create :artist, :author_id => @author.id, :name => 'artist3', :created_at => Time.now + 200
147         @artist4 = FactoryGirl.create :artist, :author_id => @author.id, :name => 'artist4', :created_at => Time.now + 300
148         @artist5 = FactoryGirl.create :artist, :author_id => @author.id, :name => 'artist5', :created_at => Time.now + 400
149         Artist.stub(:default_page_size).and_return(2)
150       end
151       it '通常は2件を返す' do
152         r = Artist.list
153         r.should have(2).items 
154       end
155       it 'page=1なら末尾2件を返す' do
156         #時系列で並んでいる
157         r = Artist.list(1)
158         r.should eq [@artist5, @artist4]
159       end
160       it 'page=2なら中間2件を返す' do
161         r = Artist.list(2)
162         r.should eq [@artist3, @artist2]
163       end
164       it 'page=3なら先頭1件を返す' do
165         r = Artist.list(3)
166         r.should eq [@artist]
167       end
168     end
169     context 'DBに5件あって1ページの件数を0件に変えたとして' do
170       before do
171         @artist2 = FactoryGirl.create :artist, :author_id => @author.id, :name => 'artist2', :created_at => Time.now + 100
172         @artist3 = FactoryGirl.create :artist, :author_id => @author.id, :name => 'artist3', :created_at => Time.now + 200
173         @artist4 = FactoryGirl.create :artist, :author_id => @author.id, :name => 'artist4', :created_at => Time.now + 300
174         @artist5 = FactoryGirl.create :artist, :author_id => @author.id, :name => 'artist5', :created_at => Time.now + 400
175         Artist.stub(:default_page_size).and_return(0)
176       end
177       it '通常は全件(5件)を返す' do
178         r = Artist.list
179         r.should have(5).items 
180       end
181     end
182   end
183   describe '一覧取得オプションに於いて' do
184     it 'includeキーを含んでいる' do
185       r = Artist.list_opt
186       r.has_key?(:include).should be_true
187     end
188     it '4つの項目を含んでいる' do
189       r = Artist.list_opt[:include]
190       r.should have(4).items
191     end
192     it '作家を含んでいる' do
193       r = Artist.list_opt[:include]
194       r.has_key?(:author).should be_true
195     end
196     it '原画を含んでいる' do
197       r = Artist.list_opt[:include]
198       r.has_key?(:original_pictures).should be_true
199     end
200     it '実素材を含んでいる' do
201       r = Artist.list_opt[:include]
202       r.has_key?(:pictures).should be_true
203     end
204     it '素材を含んでいる' do
205       r = Artist.list_opt[:include]
206       r.has_key?(:resource_pictures).should be_true
207     end
208   end
209   describe 'json一覧出力オプションに於いて' do
210     it 'includeキーを含んでいる' do
211       r = Artist.list_json_opt
212       r.has_key?(:include).should be_true
213     end
214     it '4つの項目を含んでいる' do
215       r = Artist.list_json_opt[:include]
216       r.should have(4).items
217     end
218     it '作家を含んでいる' do
219       r = Artist.list_json_opt[:include]
220       r.has_key?(:author).should be_true
221     end
222     it '原画を含んでいる' do
223       r = Artist.list_json_opt[:include]
224       r.has_key?(:original_pictures).should be_true
225     end
226     it '実素材を含んでいる' do
227       r = Artist.list_json_opt[:include]
228       r.has_key?(:pictures).should be_true
229     end
230     it '素材を含んでいる' do
231       r = Artist.list_json_opt[:include]
232       r.has_key?(:resource_pictures).should be_true
233     end
234   end
235   
236   describe '単体取得に於いて' do
237     before do
238       @artist = FactoryGirl.create :artist, :author_id => @author.id
239     end
240     context 'つつがなく終わるとき' do\r
241       it '単体取得オプションを利用している' do\r
242         Artist.stub(:show_opt).with(any_args).and_return({})\r
243         Artist.should_receive(:show_opt).with(any_args).exactly(1)\r
244         r = Artist.show @artist.id, @author
245       end\r
246       it '閲覧許可を問い合わせている' do\r
247         Artist.any_instance.stub(:visible?).with(any_args).and_return(true)\r
248         Artist.any_instance.should_receive(:visible?).with(any_args).exactly(1)\r
249         r = Artist.show @artist.id, @author
250       end\r
251     end\r
252     it '指定の絵師を返す' do
253       a = Artist.show @artist.id, @author
254       a.should eq @artist
255     end
256     context '閲覧許可が出なかったとき' do\r
257       it '403Forbidden例外を返す' do\r
258         Artist.any_instance.stub(:visible?).and_return(false)\r
259         lambda{\r
260           Artist.show @artist.id, @author\r
261         }.should raise_error(ActiveRecord::Forbidden)\r
262       end\r
263     end\r
264     context '存在しない絵師を開こうとしたとき' do\r
265       it '404RecordNotFound例外を返す' do\r
266         lambda{\r
267           Artist.show 110, @author\r
268         }.should raise_error(ActiveRecord::RecordNotFound)\r
269       end\r
270     end\r
271   end
272   describe '編集取得に於いて' do
273     before do
274       @artist = FactoryGirl.create :artist, :author_id => @author.id
275     end
276     context 'つつがなく終わるとき' do\r
277       it '単体取得オプションを利用している' do\r
278         Artist.stub(:show_opt).with(any_args).and_return({})\r
279         Artist.should_receive(:show_opt).with(any_args).exactly(1)\r
280         r = Artist.edit @artist.id, @author
281       end\r
282       it '所持判定を問い合わせている' do\r
283         Artist.any_instance.stub(:own?).with(any_args).and_return(true)\r
284         Artist.any_instance.should_receive(:own?).with(any_args).exactly(1)\r
285         r = Artist.edit @artist.id, @author
286       end\r
287     end\r
288     it '指定の絵師を返す' do
289       Artist.any_instance.stub(:own?).and_return(true)
290       r = Artist.edit @artist.id, @author.id
291       r.should eq @artist
292     end
293     context '他人の絵師を開こうとしたとき' do
294       it '403Forbidden例外を返す' do
295         Artist.any_instance.stub(:own?).and_return(false)
296         lambda{
297           Artist.edit @artist.id, @author
298         }.should raise_error(ActiveRecord::Forbidden)
299       end
300     end
301     context '存在しない絵師を開こうとしたとき' do
302       it '404RecordNotFound例外を返す' do
303         lambda{
304           Artist.edit 110, @author
305         }.should raise_error(ActiveRecord::RecordNotFound)
306       end
307     end
308   end
309   describe '単体取得オプションに於いて' do
310     it 'includeキーを含んでいる' do
311       r = Artist.show_opt
312       r.has_key?(:include).should be_true
313     end
314     it '4つの項目を含んでいる' do
315       r = Artist.show_opt[:include]
316       r.should have(4).items
317     end
318     it '作家を含んでいる' do
319       r = Artist.show_opt[:include]
320       r.has_key?(:author).should be_true
321     end
322     it '原画を含んでいる' do
323       r = Artist.show_opt[:include]
324       r.has_key?(:original_pictures).should be_true
325     end
326     it '実素材を含んでいる' do
327       r = Artist.show_opt[:include]
328       r.has_key?(:pictures).should be_true
329     end
330     it '素材を含んでいる' do
331       r = Artist.show_opt[:include]
332       r.has_key?(:resource_pictures).should be_true
333     end
334   end
335   describe 'json単体出力オプションに於いて' do
336     it 'includeキーを含んでいる' do
337       r = Artist.show_json_opt
338       r.has_key?(:include).should be_true
339     end
340     it '4つの項目を含んでいる' do
341       r = Artist.show_json_opt[:include]
342       r.should have(4).items
343     end
344     it '作家を含んでいる' do
345       r = Artist.show_json_opt[:include]
346       r.has_key?(:author).should be_true
347     end
348     it '原画を含んでいる' do
349       r = Artist.show_json_opt[:include]
350       r.has_key?(:original_pictures).should be_true
351     end
352     it '実素材を含んでいる' do
353       r = Artist.show_json_opt[:include]
354       r.has_key?(:pictures).should be_true
355     end
356     it '素材を含んでいる' do
357       r = Artist.show_json_opt[:include]
358       r.has_key?(:resource_pictures).should be_true
359     end
360   end
361 end