OSDN Git Service

a3a167f4f590ea6c763ab0bebb9ca6df1dfaeaae
[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     Factory :admin
8     @user = Factory( :user_yas)
9     @author = @user.author
10     @license = Factory :license
11   end
12
13   describe '検証に於いて' do
14     before do
15     end
16     
17     it 'オーソドックスなデータなら通る' do
18       @artist = Factory.build :artist, :author_id => @author.id, :default_license_id => @license.id
19       @artist.should be_valid
20     end
21     
22     context 'nameを検証するとき' do
23       it 'nullなら失敗する' do
24         @artist = Factory.build :artist, :author_id => @author.id, :default_license_id => @license.id, :name => nil
25         @artist.should_not be_valid
26       end
27       it '30文字以上なら失敗する' do
28         @artist = Factory.build :artist, :author_id => @author.id, :default_license_id => @license.id, :name => 'a'*31
29         @artist.should_not be_valid
30       end
31     end
32   end
33   
34   describe '自動補充に於いて' do
35     it '名前がno nameになっている' do
36       @artist = Factory.build :artist, :name => nil
37       @artist.save
38       @artist.name.should eq 'no name'
39     end
40   end
41   
42   describe '単体取得に於いて' do
43     before do
44       @artist = Factory :artist, :author_id => @author.id, :default_license_id => @license.id
45     end
46     it '指定の絵師を返す' do
47       a = Artist.show @artist.id
48       a.should eq @artist
49     end
50     context '関連テーブルオプションがないとき' do
51       it '作家データだけを含んでいる' do
52         r = Artist.show_include_opt
53         r.should eq [:author]
54       end
55     end
56     context '関連テーブルオプションで素材を含ませたとき' do
57       it '作家データと素材データを含んでいる' do
58         r = Artist.show_include_opt(:include => :resource_pictures)
59         r.should eq [:author, :resource_pictures]
60       end
61     end
62   end
63   describe '一覧取得に於いて' do
64     before do
65       @artist = Factory :artist, :author_id => @author.id, :default_license_id => @license.id
66     end
67     context 'page補正について' do
68       it '文字列から数値に変換される' do
69         Artist.page('8').should eq 8
70       end
71       it 'nilの場合は1になる' do
72         Artist.page().should eq 1
73       end
74       it '0以下の場合は1になる' do
75         Artist.page('0').should eq 1
76       end
77     end
78     context 'page_size補正について' do
79       it '文字列から数値に変換される' do
80         Artist.page_size('7').should eq 7
81       end
82       it 'nilの場合はArtist.default_page_sizeになる' do
83         Artist.page_size().should eq Artist.default_page_size
84       end
85       it '0以下の場合はArtist.default_page_sizeになる' do
86         Artist.page_size('0').should eq Artist.default_page_size
87       end
88       it 'Artist.max_page_sizeを超えた場合はArtist.max_page_sizeになる' do
89         Artist.page_size('1000').should eq Artist.max_page_size
90       end
91     end
92     it 'リストを返す' do
93       c = Artist.list
94       c.should eq [@artist]
95     end
96     it '時系列で並んでいる' do
97       n = Factory :artist, :author_id => @author.id, :name => 'artist'
98       l = Artist.list
99       l.should eq [n, @artist]
100     end
101     context 'DBに5件あって1ページの件数を2件に変えたとして' do
102       before do
103         @artist2 = Factory :artist, :author_id => @author.id, :name => 'artist2'
104         @artist3 = Factory :artist, :author_id => @author.id, :name => 'artist3'
105         @artist4 = Factory :artist, :author_id => @author.id, :name => 'artist4'
106         @artist5 = Factory :artist, :author_id => @author.id, :name => 'artist5'
107         Artist.stub(:default_page_size).and_return(2)
108       end
109       it '通常は2件を返す' do
110         c = Artist.list
111         c.should have(2).items 
112       end
113       it 'page=1なら末尾2件を返す' do
114         #時系列で並んでいる
115         c = Artist.list({}, 1)
116         c.should eq [@artist5, @artist4]
117       end
118       it 'page=2なら中間2件を返す' do
119         c = Artist.list({}, 2)
120         c.should eq [@artist3, @artist2]
121       end
122       it 'page=3なら先頭1件を返す' do
123         c = Artist.list({}, 3)
124         c.should eq [@artist]
125       end
126     end
127   end
128 end