OSDN Git Service

39fda16bdb4c8d6c0ba355bf78792675368d9fb9
[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     #コモンライセンスとオリジナルライセンスをまたいだUrl重複チェックはここでやるよりない
68     #コモンライセンスが新規オブジェクトなのにライセンスが取得できるのは、
69     #そのUrlがオリジナルライセンスから登録されているということ
70     context '新規でユニークチェックするとき' do
71       it 'ライセンスの全体エラーに重複メッセージを入れて返す' do
72         cl = Factory.build(:common_license, :url => 'http://domain.no')
73         License.stub(:find_by_url).with(any_args).and_return(@lc)
74         r = License.update_license cl
75         r.errors[:base].should_not be_empty
76       end
77     end
78   end
79   
80   #作成が
81   describe '単体取得に於いて' do
82     before do
83       @lcl = Factory :license
84       @cl = Factory :common_license, :license_id => @lcl.id
85       @lol = Factory :license, :url => 'http://test.ptn/10'
86       @ol = Factory :original_license, :license_id => @lol.id, :url => 'http://test.ptn/10'
87     end
88     it '指定のコマを返す' do
89       l = License.show @lcl.id
90       l.should eq @lcl
91     end
92   end
93   describe '関連テーブルプションに於いて' do
94     context 'オプションがないとき' do
95       it 'コモンライセンスとオリジナルライセンスを含んでいる' do
96         r = License.show_include_opt
97         r.should eq [:common_license, :original_license]
98       end
99     end
100     context 'オプションで原画を含ませたとき' do
101       it 'コモンライセンスとオリジナルライセンスと原画データを含んでいる' do
102         r = License.show_include_opt(:include => :original_picture)
103         r.should eq [:common_license, :original_license, :original_picture]
104       end
105     end
106   end
107   describe 'json単体出力オプションに於いて' do
108     it 'includeキーを含んでいる' do
109       r = License.show_json_include_opt
110       r.has_key?(:include).should be_true
111     end
112     it '2つの項目を含んでいる' do
113       r = License.show_json_include_opt[:include]
114       r.should have(2).items
115     end
116     it 'コモンライセンスを含んでいる' do
117       r = License.show_json_include_opt[:include]
118       r.has_key?(:common_license).should be_true
119     end
120     it 'オリジナルライセンスを含んでいる' do
121       r = License.show_json_include_opt[:include]
122       r.has_key?(:original_license).should be_true
123     end
124   end
125   describe '一覧取得に於いて' do
126     before do
127       @lcl = Factory :license, :name => 'peta2.0'
128       @cl = Factory :common_license, :license_id => @lcl.id
129     end
130     context 'page補正について' do
131       it '文字列から数値に変換される' do
132         License.page('8').should eq 8
133       end
134       it 'nilの場合は1になる' do
135         License.page().should eq 1
136       end
137       it '0以下の場合は1になる' do
138         License.page('0').should eq 1
139       end
140     end
141     context 'page_size補正について' do
142       it '文字列から数値に変換される' do
143         License.page_size('7').should eq 7
144       end
145       it 'nilの場合はLicense.default_page_sizeになる' do
146         License.page_size().should eq License.default_page_size
147       end
148       it '0以下の場合はLicense.default_page_sizeになる' do
149         License.page_size('0').should eq License.default_page_size
150       end
151       it 'License.max_page_sizeを超えた場合はLicense.max_page_sizeになる' do
152         License.page_size('1000').should eq License.max_page_size
153       end
154     end
155     it 'リストを返す' do
156       pl = License.list
157       pl.should eq [@lcl]
158     end
159     it '名前順で並んでいる' do
160       @lol = Factory :license, :name => 'peta1.0', :url => 'http://test.ptn/10'
161       @ol = Factory :original_license, :license_id => @lol.id, :name => 'peta1.0', :url => 'http://test.ptn/10'
162       l = License.list
163       l.should eq [@lol, @lcl]
164     end
165     context 'DBに5件あって1ページの件数を2件に変えたとして' do
166       before do
167         @lol2 = Factory :license, :name => 'peta2.1', :url => 'http://test.ptn/21'
168         @ol2 = Factory :original_license, :license_id => @lol2.id, :name => 'peta2.1', :url => 'http://test.ptn/21'
169         @lol3 = Factory :license, :name => 'peta2.2', :url => 'http://test.ptn/22'
170         @ol3 = Factory :original_license, :license_id => @lol3.id, :name => 'peta2.2', :url => 'http://test.ptn/22'
171         @lol4 = Factory :license, :name => 'peta2.3', :url => 'http://test.ptn/23'
172         @ol4 = Factory :original_license, :license_id => @lol4.id, :name => 'peta2.3', :url => 'http://test.ptn/23'
173         @lol5 = Factory :license, :name => 'peta2.4', :url => 'http://test.ptn/24'
174         @ol5 = Factory :original_license, :license_id => @lol5.id, :name => 'peta2.4', :url => 'http://test.ptn/24'
175         License.stub(:default_page_size).and_return(2)
176       end
177       it '通常は2件を返す' do
178         l = License.list
179         l.should have(2).items 
180       end
181       it 'page=1なら末尾2件を返す' do
182         #名前順で並んでいる
183         l = License.list( {}, 1)
184         l.should eq [@lcl, @lol2]
185       end
186       it 'page=2なら中間2件を返す' do
187         l = License.list({}, 2)
188         l.should eq [@lol3, @lol4]
189       end
190       it 'page=3なら先頭1件を返す' do
191         l = License.list({}, 3)
192         l.should eq [@lol5]
193       end
194     end
195   end
196   describe 'list関連テーブルプションに於いて' do
197     it 'includeキーを含んでいる' do
198       r = License.list_opt
199       r.has_key?(:include).should be_true
200     end
201     it '2つの項目を含んでいる' do
202       r = License.list_opt[:include]
203       r.should have(2).items
204     end
205     it 'コモンライセンスを含んでいる' do
206       r = License.list_opt[:include]
207       r.has_key?(:common_license).should be_true
208     end
209     it 'オリジナルライセンスを含んでいる' do
210       r = License.list_opt[:include]
211       r.has_key?(:original_license).should be_true
212     end
213   end
214   describe 'json一覧出力オプションに於いて' do
215     it 'includeキーを含んでいる' do
216       r = License.list_json_opt
217       r.has_key?(:include).should be_true
218     end
219     it '2つの項目を含んでいる' do
220       r = License.list_json_opt[:include]
221       r.should have(2).items
222     end
223     it 'コモンライセンスを含んでいる' do
224       r = License.list_json_opt[:include]
225       r.has_key?(:common_license).should be_true
226     end
227     it 'オリジナルライセンスを含んでいる' do
228       r = License.list_json_opt[:include]
229       r.has_key?(:original_license).should be_true
230     end
231   end
232   
233 end