remarkable_activerecord 3.1.9 → 3.1.10

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,87 +0,0 @@
1
- require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
-
3
- describe 'validate_inclusion_of' do
4
- include ModelBuilder
5
-
6
- # Defines a model, create a validation and returns a raw matcher
7
- def define_and_validate(options={})
8
- @model = define_model :product, :title => :string, :size => :string do
9
- validates_inclusion_of :title, :size, options
10
- end
11
-
12
- validate_inclusion_of(:title, :size)
13
- end
14
-
15
- describe 'messages' do
16
- before(:each){ @matcher = define_and_validate(:in => 2..10) }
17
-
18
- it 'should contain a description' do
19
- @matcher.in(2..10)
20
- @matcher.description.should == 'ensure inclusion of title and size in 2..10'
21
-
22
- @matcher = validate_inclusion_of(:title, :size).in('X', 'Y', 'Z')
23
- @matcher.description.should == 'ensure inclusion of title and size in "X", "Y", and "Z"'
24
- end
25
-
26
- it 'should set is_valid? message' do
27
- @matcher.in(1..10).matches?(@model)
28
- @matcher.failure_message.should == 'Expected Product to be valid when title is set to 1'
29
- end
30
-
31
- it 'should set is_invalid? message' do
32
- @matcher.in(3..10).matches?(@model)
33
- @matcher.failure_message.should == 'Expected Product to be invalid when title is set to 2'
34
- end
35
-
36
- it 'should set allow_nil? message' do
37
- @matcher.allow_nil.matches?(@model)
38
- @matcher.failure_message.should == 'Expected Product to allow nil values for title'
39
- end
40
-
41
- it 'should set allow_blank? message' do
42
- @matcher.allow_blank.matches?(@model)
43
- @matcher.failure_message.should == 'Expected Product to allow blank values for title'
44
- end
45
- end
46
-
47
- describe 'matchers' do
48
- it { should define_and_validate(:in => ['X', 'Y', 'Z']).in('X', 'Y', 'Z') }
49
- it { should_not define_and_validate(:in => ['X', 'Y', 'Z']).in('X', 'Y') }
50
- it { should_not define_and_validate(:in => ['X', 'Y', 'Z']).in('A') }
51
-
52
- it { should define_and_validate(:in => [true, false]).in(true, false) }
53
- it { should_not define_and_validate(:in => [true, false]).in('A') }
54
-
55
- it { should define_and_validate(:in => 2..3).in(2..3) }
56
- it { should define_and_validate(:in => 2..20).in(2..20) }
57
- it { should_not define_and_validate(:in => 2..20).in(1..20) }
58
- it { should_not define_and_validate(:in => 2..20).in(3..20) }
59
- it { should_not define_and_validate(:in => 2..20).in(2..19) }
60
- it { should_not define_and_validate(:in => 2..20).in(2..21) }
61
-
62
- it { should define_and_validate(:in => ['X', 'Y', 'Z'], :message => 'not valid').in('X', 'Y', 'Z').message('not valid') }
63
- it { should_not define_and_validate(:in => ['X', 'Y', 'Z'], :message => 'not valid').in('X', 'Y', 'Z').message('valid') }
64
- end
65
-
66
- describe 'macros' do
67
- describe 'with array' do
68
- before(:each){ define_and_validate(:in => ['X', 'Y', 'Z']) }
69
-
70
- should_validate_inclusion_of :title, :in => ['X', 'Y', 'Z']
71
- should_validate_inclusion_of :title, :size, :in => ['X', 'Y', 'Z']
72
- should_not_validate_inclusion_of :title, :in => ['X', 'Y']
73
- should_not_validate_inclusion_of :title, :size, :in => ['A']
74
- end
75
-
76
- describe 'with range' do
77
- before(:each){ define_and_validate(:in => 2..20) }
78
-
79
- should_validate_inclusion_of :title, :in => 2..20
80
- should_not_validate_inclusion_of :title, :in => 1..20
81
- should_not_validate_inclusion_of :title, :in => 3..20
82
- should_not_validate_inclusion_of :title, :in => 2..19
83
- should_not_validate_inclusion_of :title, :in => 2..21
84
- end
85
- end
86
- end
87
-
@@ -1,218 +0,0 @@
1
- require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
-
3
- describe 'validate_length_of' do
4
- include ModelBuilder
5
-
6
- # Defines a model, create a validation and returns a raw matcher
7
- def define_and_validate(options={})
8
- options = options.merge(:within => 3..5) if options.slice(:in, :within, :maximum, :minimum, :is).empty?
9
-
10
- @model = define_model :product, :size => :string, :category => :string do
11
- validates_length_of :size, options
12
- end
13
-
14
- validate_length_of(:size)
15
- end
16
-
17
- describe 'messages' do
18
- before(:each){ @matcher = define_and_validate }
19
-
20
- it 'should contain a description' do
21
- @matcher.within(3..5)
22
- @matcher.description.should == 'ensure length of size is within 3..5 characters'
23
-
24
- @matcher.within(nil).in(3..5)
25
- @matcher.description.should == 'ensure length of size is within 3..5 characters'
26
-
27
- @matcher.in(nil).is(3)
28
- @matcher.description.should == 'ensure length of size is equal to 3 characters'
29
-
30
- @matcher.is(nil).maximum(5)
31
- @matcher.description.should == 'ensure length of size is maximum 5 characters'
32
-
33
- @matcher.maximum(nil).minimum(3)
34
- @matcher.description.should == 'ensure length of size is minimum 3 characters'
35
-
36
- @matcher.allow_nil(false)
37
- @matcher.description.should == 'ensure length of size is minimum 3 characters and not allowing nil values'
38
-
39
- @matcher.allow_blank
40
- @matcher.description.should == 'ensure length of size is minimum 3 characters, not allowing nil values, and allowing blank values'
41
- end
42
-
43
- it 'should set less_than_min_length? message' do
44
- @matcher.within(4..5).matches?(@model)
45
- @matcher.failure_message.should == 'Expected Product to be invalid when size length is less than 4 characters'
46
- end
47
-
48
- it 'should set exactly_min_length? message' do
49
- @matcher.should_receive(:less_than_min_length?).and_return(true)
50
- @matcher.within(2..5).matches?(@model)
51
- @matcher.failure_message.should == 'Expected Product to be valid when size length is 2 characters'
52
- end
53
-
54
- it 'should set more_than_max_length? message' do
55
- @matcher.within(3..4).matches?(@model)
56
- @matcher.failure_message.should == 'Expected Product to be invalid when size length is more than 4 characters'
57
- end
58
-
59
- it 'should set exactly_max_length? message' do
60
- @matcher.should_receive(:more_than_max_length?).and_return(true)
61
- @matcher.within(3..6).matches?(@model)
62
- @matcher.failure_message.should == 'Expected Product to be valid when size length is 6 characters'
63
- end
64
-
65
- it 'should set allow_blank? message' do
66
- @matcher.within(3..5).allow_blank.matches?(@model)
67
- @matcher.failure_message.should == 'Expected Product to allow blank values for size'
68
- end
69
-
70
- it 'should set allow_nil? message' do
71
- @matcher.within(3..5).allow_nil.matches?(@model)
72
- @matcher.failure_message.should == 'Expected Product to allow nil values for size'
73
- end
74
- end
75
-
76
- describe 'matcher' do
77
- # Wrap specs without options. Usually a couple specs.
78
- describe 'without options' do
79
- before(:each){ define_and_validate }
80
-
81
- it { should validate_length_of(:size, :within => 3..5) }
82
- it { should_not validate_length_of(:category, :within => 3..5) }
83
- end
84
-
85
- describe "with message option" do
86
-
87
- if RAILS_VERSION =~ /^2.3/
88
- it { should define_and_validate(:message => 'not valid').within(3..5).message('not valid') }
89
- it { should_not define_and_validate(:message => 'not valid').within(3..5).message('valid') }
90
- else
91
- it { should define_and_validate(:too_short => 'not valid', :too_long => 'not valid').within(3..5).message('not valid') }
92
- it { should_not define_and_validate(:too_short => 'not valid', :too_long => 'not valid').within(3..5).message('valid') }
93
- end
94
-
95
- it { should define_and_validate(:is => 4, :message => 'not valid').is(4).message('not valid') }
96
- it { should_not define_and_validate(:is => 4, :message => 'not valid').is(4).message('valid') }
97
- end
98
-
99
- describe "with too_short option" do
100
- it { should define_and_validate(:too_short => 'not valid').within(3..5).too_short('not valid') }
101
- it { should_not define_and_validate(:too_short => 'not valid').within(3..5).too_short('valid') }
102
- end
103
-
104
- describe "with too_long option" do
105
- it { should define_and_validate(:too_long => 'not valid').within(3..5).too_long('not valid') }
106
- it { should_not define_and_validate(:too_long => 'not valid').within(3..5).too_long('valid') }
107
- end
108
-
109
- describe "with wrong_length option" do
110
- it { should define_and_validate(:is => 4, :wrong_length => 'not valid').is(4).wrong_length('not valid') }
111
- it { should_not define_and_validate(:is => 4, :wrong_length => 'not valid').is(4).wrong_length('valid') }
112
- end
113
-
114
- describe "with within option" do
115
- it { should define_and_validate(:within => 3..5).within(3..5) }
116
- it { should_not define_and_validate(:within => 3..5).within(2..5) }
117
- it { should_not define_and_validate(:within => 3..5).within(4..5) }
118
- it { should_not define_and_validate(:within => 3..5).within(3..4) }
119
- it { should_not define_and_validate(:within => 3..5).within(3..6) }
120
- end
121
-
122
- describe "with in option" do
123
- it { should define_and_validate(:in => 3..5).within(3..5) }
124
- it { should_not define_and_validate(:in => 3..5).within(2..5) }
125
- it { should_not define_and_validate(:in => 3..5).within(4..5) }
126
- it { should_not define_and_validate(:in => 3..5).within(3..4) }
127
- it { should_not define_and_validate(:in => 3..5).within(3..6) }
128
- end
129
-
130
- describe "with minimum option" do
131
- it { should define_and_validate(:minimum => 3).minimum(3) }
132
- it { should_not define_and_validate(:minimum => 3).minimum(2) }
133
- it { should_not define_and_validate(:minimum => 3).minimum(4) }
134
- end
135
-
136
- describe "with maximum option" do
137
- it { should define_and_validate(:maximum => 3).maximum(3) }
138
- it { should_not define_and_validate(:maximum => 3).maximum(2) }
139
- it { should_not define_and_validate(:maximum => 3).maximum(4) }
140
- end
141
-
142
- describe "with is option" do
143
- it { should define_and_validate(:is => 3).is(3) }
144
- it { should_not define_and_validate(:is => 3).is(2) }
145
- it { should_not define_and_validate(:is => 3).is(4) }
146
- end
147
-
148
- describe "with token and separator options" do
149
- describe "and words as tokens" do
150
- before(:each) do
151
- @matcher = define_and_validate(:within => 3..5, :tokenizer => lambda { |str| str.scan(/\w+/) })
152
- end
153
-
154
- it { should @matcher.within(3..5).token("word").separator(" ") }
155
- it { should_not @matcher.within(2..5).token("word").separator(" ") }
156
- it { should_not @matcher.within(4..5).token("word").separator(" ") }
157
- it { should_not @matcher.within(3..4).token("word").separator(" ") }
158
- it { should_not @matcher.within(3..6).token("word").separator(" ") }
159
- it { should_not @matcher.within(3..5).token("word").separator("") }
160
- it { should_not @matcher.within(3..5).token("word").separator(" a ") }
161
- end
162
-
163
- describe "and digits as tokens" do
164
- before(:each) do
165
- @matcher = define_and_validate(:within => 3..5, :tokenizer => lambda { |str| str.scan(/\d+/) })
166
- end
167
-
168
- it { should @matcher.within(3..5).token("1").separator(" ") }
169
- it { should_not @matcher.within(3..5).token("a").separator("") }
170
- end
171
- end
172
-
173
- describe "with with kind of" do
174
- def define_and_validate(options)
175
- define_model :variant, :product_id => :integer
176
-
177
- @model = define_model :product do
178
- has_many :variants
179
- validates_length_of :variants, options
180
- end
181
-
182
- validate_length_of(:variants)
183
- end
184
-
185
- it { should define_and_validate(:within => 3..6).within(3..6).with_kind_of(Variant) }
186
- it { should_not define_and_validate(:within => 2..6).within(3..6).with_kind_of(Variant) }
187
- it { should_not define_and_validate(:within => 3..7).within(3..6).with_kind_of(Variant) }
188
-
189
- it "should raise association type mismatch if with_kind_of does not match" do
190
- lambda {
191
- should_not define_and_validate(:within => 3..6).within(3..6).with_kind_of(Product)
192
- }.should raise_error(ActiveRecord::AssociationTypeMismatch)
193
- end
194
- end
195
-
196
- # Those are macros to test optionals which accept only boolean values
197
- create_optional_boolean_specs(:allow_nil, self)
198
- create_optional_boolean_specs(:allow_blank, self)
199
- end
200
-
201
- # In macros we include just a few tests to assure that everything works properly
202
- describe 'macros' do
203
- before(:each) { define_and_validate }
204
-
205
- should_validate_length_of :size, :in => 3..5
206
- should_validate_length_of :size, :within => 3..5
207
-
208
- should_not_validate_length_of :size, :within => 2..5
209
- should_not_validate_length_of :size, :within => 4..5
210
- should_not_validate_length_of :size, :within => 3..4
211
- should_not_validate_length_of :size, :within => 3..6
212
- should_not_validate_length_of :category, :in => 3..5
213
-
214
- should_validate_length_of :size do |m|
215
- m.in = 3..5
216
- end
217
- end
218
- end
@@ -1,186 +0,0 @@
1
- require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
-
3
- describe 'validate_numericality_of' do
4
- include ModelBuilder
5
-
6
- # Defines a model, create a validation and returns a raw matcher
7
- def define_and_validate(options={})
8
- @model = define_model :product, :price => :integer, :size => :integer do
9
- validates_numericality_of :price, options
10
- end
11
-
12
- validate_numericality_of(:price)
13
- end
14
-
15
- describe 'messages' do
16
- before(:each){ @matcher = define_and_validate }
17
-
18
- # To tests descriptions, we don't need to use mocks neither stubs.
19
- # We just need to define a matcher.
20
- #
21
- it 'should contain a description' do
22
- matcher = validate_numericality_of(:age)
23
- matcher.description.should == 'ensure numericality of age'
24
-
25
- matcher.allow_nil(false)
26
- matcher.description.should == 'ensure numericality of age not allowing nil values'
27
-
28
- matcher.allow_blank
29
- matcher.description.should == 'ensure numericality of age not allowing nil values and allowing blank values'
30
-
31
- matcher = validate_numericality_of(:age, :only_integer => true)
32
- matcher.description.should == 'ensure numericality of age allowing only integer values'
33
-
34
- matcher = validate_numericality_of(:age, :even => true)
35
- matcher.description.should == 'ensure numericality of age allowing only even values'
36
-
37
- matcher = validate_numericality_of(:age, :odd => true)
38
- matcher.description.should == 'ensure numericality of age allowing only odd values'
39
-
40
- matcher = validate_numericality_of(:age, :equal_to => 10)
41
- matcher.description.should == 'ensure numericality of age is equal to 10'
42
-
43
- matcher = validate_numericality_of(:age, :less_than_or_equal_to => 10)
44
- matcher.description.should == 'ensure numericality of age is less than or equal to 10'
45
-
46
- matcher = validate_numericality_of(:age, :greater_than_or_equal_to => 10)
47
- matcher.description.should == 'ensure numericality of age is greater than or equal to 10'
48
-
49
- matcher = validate_numericality_of(:age, :less_than => 10)
50
- matcher.description.should == 'ensure numericality of age is less than 10'
51
-
52
- matcher = validate_numericality_of(:age, :greater_than => 10)
53
- matcher.description.should == 'ensure numericality of age is greater than 10'
54
-
55
- matcher = validate_numericality_of(:age, :greater_than => 10, :less_than => 20)
56
- matcher.description.should == 'ensure numericality of age is greater than 10 and is less than 20'
57
- end
58
-
59
- it 'should set only_numeric_values? message' do
60
- @matcher.should_receive(:only_numeric_values?).and_return(false)
61
- @matcher.matches?(@model)
62
- @matcher.failure_message.should == 'Expected Product to allow only numeric values for price'
63
- @matcher.negative_failure_message.should == 'Did not expect Product to allow only numeric values for price'
64
- end
65
-
66
- it 'should set only_integer_values? message' do
67
- @matcher.should_receive(:only_integer?).and_return([false, { :not => '' }])
68
- @matcher.matches?(@model)
69
- @matcher.failure_message.should == 'Expected Product to allow only integer values for price'
70
- end
71
-
72
- it 'should set only_odd_values? message' do
73
- @matcher.should_receive(:only_odd?).and_return(false)
74
- @matcher.matches?(@model)
75
- @matcher.failure_message.should == 'Expected Product to allow only odd values for price'
76
- end
77
-
78
- it 'should set only_even_values? message' do
79
- @matcher.should_receive(:only_even?).and_return(false)
80
- @matcher.matches?(@model)
81
- @matcher.failure_message.should == 'Expected Product to allow only even values for price'
82
- end
83
-
84
- it 'should set equals_to? message' do
85
- @matcher.should_receive(:equals_to?).and_return([false, { :count => 10 }])
86
- @matcher.matches?(@model)
87
- @matcher.failure_message.should == 'Expected Product to be valid only when price is equal to 10'
88
- end
89
-
90
- it 'should set less_than_minimum? message' do
91
- @matcher.should_receive(:less_than_minimum?).and_return([false, { :count => 10 }])
92
- @matcher.matches?(@model)
93
- @matcher.failure_message.should == 'Expected Product to be invalid when price is less than 10'
94
- end
95
-
96
- it 'should set more_than_maximum? message' do
97
- @matcher.should_receive(:more_than_maximum?).and_return([false, { :count => 10 }])
98
- @matcher.matches?(@model)
99
- @matcher.failure_message.should == 'Expected Product to be invalid when price is greater than 10'
100
- end
101
- end
102
-
103
- describe 'matcher' do
104
- # Wrap specs without options. Usually a couple specs.
105
- describe 'without options' do
106
- before(:each){ define_and_validate }
107
-
108
- it { should validate_numericality_of(:price) }
109
- it { should_not validate_numericality_of(:size) }
110
- end
111
-
112
- # Wrap each option inside a describe group.
113
- describe 'with equal_to option' do
114
- it { should define_and_validate(:equal_to => 100).equal_to(100) }
115
- it { should_not define_and_validate(:equal_to => 100).equal_to(99) }
116
- it { should_not define_and_validate(:equal_to => 100).equal_to(101) }
117
- end
118
-
119
- describe 'with less_than option' do
120
- it { should define_and_validate(:less_than => 100).less_than(100) }
121
- it { should_not define_and_validate(:less_than => 100).less_than(99) }
122
- it { should_not define_and_validate(:less_than => 100).less_than(101) }
123
- end
124
-
125
- describe 'with greater_than option' do
126
- it { should define_and_validate(:greater_than => 100).greater_than(100) }
127
- it { should_not define_and_validate(:greater_than => 100).greater_than(99) }
128
- it { should_not define_and_validate(:greater_than => 100).greater_than(101) }
129
- end
130
-
131
- describe 'with less_than_or_equal_to option' do
132
- it { should define_and_validate(:less_than_or_equal_to => 100).less_than_or_equal_to(100) }
133
- it { should_not define_and_validate(:less_than_or_equal_to => 100).less_than_or_equal_to(99) }
134
- it { should_not define_and_validate(:less_than_or_equal_to => 100).less_than_or_equal_to(101) }
135
- end
136
-
137
- describe 'with greater_than_or_equal_to option' do
138
- it { should define_and_validate(:greater_than_or_equal_to => 100).greater_than_or_equal_to(100) }
139
- it { should_not define_and_validate(:greater_than_or_equal_to => 100).greater_than_or_equal_to(99) }
140
- it { should_not define_and_validate(:greater_than_or_equal_to => 100).greater_than_or_equal_to(101) }
141
- end
142
-
143
- describe "with even option" do
144
- it { should define_and_validate(:even => true).even }
145
- it { should_not define_and_validate.even(true) }
146
- end
147
-
148
- describe "with odd option" do
149
- it { should define_and_validate(:odd => true).odd }
150
- it { should_not define_and_validate.odd(true) }
151
- end
152
-
153
- describe "with several options" do
154
- it { should define_and_validate(:less_than => 100, :greater_than => 10).less_than(100).greater_than(10) }
155
- it { should define_and_validate(:less_than => 100, :message => 'not valid').less_than(100).message('not valid') }
156
- it { should define_and_validate(:less_than_or_equal_to => 100, :greater_than => 1).less_than_or_equal_to(100).greater_than(1) }
157
- end
158
-
159
- # A macro to spec messages
160
- create_message_specs(self)
161
-
162
- # Those are macros to test optionals which accept only boolean values
163
- create_optional_boolean_specs(:allow_nil, self)
164
- create_optional_boolean_specs(:allow_blank, self)
165
- create_optional_boolean_specs(:only_integer, self)
166
- end
167
-
168
- # In macros we include just a few tests to assure that everything works properly
169
- describe 'macros' do
170
- before(:each) { define_and_validate(:less_than => 100000, :greater_than => 9999) }
171
-
172
- should_validate_numericality_of :price
173
- should_validate_numericality_of :price, :less_than => 100000
174
- should_validate_numericality_of :price, :greater_than => 9999
175
- should_validate_numericality_of :price, :less_than => 100000, :greater_than => 999
176
-
177
- should_not_validate_numericality_of :size
178
- should_not_validate_numericality_of :price, :less_than => 55555
179
- should_not_validate_numericality_of :price, :greater_than => 55555
180
-
181
- should_validate_numericality_of :price do |m|
182
- m.less_than 100000
183
- m.greater_than 999
184
- end
185
- end
186
- end