OSDN Git Service

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