OSDN Git Service

ea5a8d9d005eb0458213bed35452c2fa0cc85427
[pettanr/pettanr.git] / spec / models / license_spec.rb
1 # -*- encoding: utf-8 -*-
2 #ライセンス
3 require 'spec_helper'
4
5 describe License do
6   before do
7   end
8   describe '検証に於いて' do
9     before do
10       @l = Factory.build :license
11     end
12     
13     it 'オーソドックスなデータなら通る' do
14       @l.should be_valid
15     end
16     
17     context 'nameを検証するとき' do
18       it 'nullなら失敗する' do
19         @l.name = ''
20         @l.should_not be_valid
21       end
22       it '51文字以上なら失敗する' do
23         @l.name = 'a'*51
24         @l.should_not be_valid
25       end
26     end
27     context 'urlを検証するとき' do
28       it 'nullなら失敗する' do
29         @l.url = ''
30         @l.should_not be_valid
31       end
32       it '201文字以上なら失敗する' do
33         @l.url = 'a'*201
34         @l.should_not be_valid
35       end
36       it '重複していたら失敗する' do
37         lc = Factory :license
38         @l.should_not be_valid
39       end
40       it 'url形式でなら失敗する' do
41         @l.url = ''
42         pending
43       end
44     end
45   end
46   
47   describe '対象ライセンスの取得に於いて' do
48     before do
49       @lc = Factory :license
50     end
51     context 'urlが一致するライセンスがないとき' do
52       it '新規ライセンスを準備して返す' do
53         cl = Factory.build(:common_license, :url => 'http://domain.no')
54         r = License.update_license cl
55         r.should be_a_new License
56       end
57     end
58     context 'urlが一致するライセンスがあるとき' do
59       it '該当ライセンスを返す' do
60         r = License.update_license @lc
61         r.is_a?(License).should be_true
62         r.should_not be_a_new License
63         r.url.should eq @lc.url
64         r.name.should eq @lc.name
65       end
66     end
67   end
68   
69   #作成が
70   describe '単体取得に於いて' do
71     before do
72       @lcl = Factory :license
73       @cl = Factory :common_license, :license_id => @lcl.id
74       @lol = Factory :license, :url => 'http://test.ptn/10'
75       @ol = Factory :original_license, :license_id => @lol.id, :url => 'http://test.ptn/10'
76     end
77     it '指定のコマを返す' do
78       l = License.show @lcl.id
79       l.should eq @lcl
80     end
81   end
82   describe '関連テーブルプションに於いて' do
83     context 'オプションがないとき' do
84       it 'コモンライセンスとオリジナルライセンスを含んでいる' do
85         r = License.show_include_opt
86         r.should eq [:common_license, :original_license]
87       end
88     end
89     context 'オプションで原画を含ませたとき' do
90       it 'コモンライセンスとオリジナルライセンスと原画データを含んでいる' do
91         r = License.show_include_opt(:include => :original_picture)
92         r.should eq [:common_license, :original_license, :original_picture]
93       end
94     end
95   end
96   describe 'json単体出力オプションに於いて' do
97     it 'includeキーを含んでいる' do
98       r = License.show_json_include_opt
99       r.has_key?(:include).should be_true
100     end
101     it '2つの項目を含んでいる' do
102       r = License.show_json_include_opt[:include]
103       r.should have(2).items
104     end
105     it 'コモンライセンスを含んでいる' do
106       r = License.show_json_include_opt[:include]
107       r.has_key?(:common_license).should be_true
108     end
109     it 'オリジナルライセンスを含んでいる' do
110       r = License.show_json_include_opt[:include]
111       r.has_key?(:original_license).should be_true
112     end
113   end
114   describe '一覧取得に於いて' do
115     before do
116       @lcl = Factory :license, :name => 'peta2.0'
117       @cl = Factory :common_license, :license_id => @lcl.id
118     end
119     context 'page補正について' do
120       it '文字列から数値に変換される' do
121         License.page('8').should eq 8
122       end
123       it 'nilの場合は1になる' do
124         License.page().should eq 1
125       end
126       it '0以下の場合は1になる' do
127         License.page('0').should eq 1
128       end
129     end
130     context 'page_size補正について' do
131       it '文字列から数値に変換される' do
132         License.page_size('7').should eq 7
133       end
134       it 'nilの場合はLicense.default_page_sizeになる' do
135         License.page_size().should eq License.default_page_size
136       end
137       it '0以下の場合はLicense.default_page_sizeになる' do
138         License.page_size('0').should eq License.default_page_size
139       end
140       it 'License.max_page_sizeを超えた場合はLicense.max_page_sizeになる' do
141         License.page_size('1000').should eq License.max_page_size
142       end
143     end
144     it 'リストを返す' do
145       pl = License.list
146       pl.should eq [@lcl]
147     end
148     it '名前順で並んでいる' do
149       @lol = Factory :license, :name => 'peta1.0', :url => 'http://test.ptn/10'
150       @ol = Factory :original_license, :license_id => @lol.id, :name => 'peta1.0', :url => 'http://test.ptn/10'
151       l = License.list
152       l.should eq [@lol, @lcl]
153     end
154     context 'DBに5件あって1ページの件数を2件に変えたとして' do
155       before do
156         @lol2 = Factory :license, :name => 'peta2.1', :url => 'http://test.ptn/21'
157         @ol2 = Factory :original_license, :license_id => @lol2.id, :name => 'peta2.1', :url => 'http://test.ptn/21'
158         @lol3 = Factory :license, :name => 'peta2.2', :url => 'http://test.ptn/22'
159         @ol3 = Factory :original_license, :license_id => @lol3.id, :name => 'peta2.2', :url => 'http://test.ptn/22'
160         @lol4 = Factory :license, :name => 'peta2.3', :url => 'http://test.ptn/23'
161         @ol4 = Factory :original_license, :license_id => @lol4.id, :name => 'peta2.3', :url => 'http://test.ptn/23'
162         @lol5 = Factory :license, :name => 'peta2.4', :url => 'http://test.ptn/24'
163         @ol5 = Factory :original_license, :license_id => @lol5.id, :name => 'peta2.4', :url => 'http://test.ptn/24'
164         License.stub(:default_page_size).and_return(2)
165       end
166       it '通常は2件を返す' do
167         l = License.list
168         l.should have(2).items 
169       end
170       it 'page=1なら末尾2件を返す' do
171         #名前順で並んでいる
172         l = License.list( {}, 1)
173         l.should eq [@lcl, @lol2]
174       end
175       it 'page=2なら中間2件を返す' do
176         l = License.list({}, 2)
177         l.should eq [@lol3, @lol4]
178       end
179       it 'page=3なら先頭1件を返す' do
180         l = License.list({}, 3)
181         l.should eq [@lol5]
182       end
183     end
184   end
185   describe 'list関連テーブルプションに於いて' do
186     it 'includeキーを含んでいる' do
187       r = License.list_opt
188       r.has_key?(:include).should be_true
189     end
190     it '2つの項目を含んでいる' do
191       r = License.list_opt[:include]
192       r.should have(2).items
193     end
194     it 'コモンライセンスを含んでいる' do
195       r = License.list_opt[:include]
196       r.has_key?(:common_license).should be_true
197     end
198     it 'オリジナルライセンスを含んでいる' do
199       r = License.list_opt[:include]
200       r.has_key?(:original_license).should be_true
201     end
202   end
203   describe 'json一覧出力オプションに於いて' do
204     it 'includeキーを含んでいる' do
205       r = License.list_json_opt
206       r.has_key?(:include).should be_true
207     end
208     it '2つの項目を含んでいる' do
209       r = License.list_json_opt[:include]
210       r.should have(2).items
211     end
212     it 'コモンライセンスを含んでいる' do
213       r = License.list_json_opt[:include]
214       r.has_key?(:common_license).should be_true
215     end
216     it 'オリジナルライセンスを含んでいる' do
217       r = License.list_json_opt[:include]
218       r.has_key?(:original_license).should be_true
219     end
220   end
221   
222 end