djsun-mongomapper 0.3.5.5 → 0.4.1.2
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +38 -38
- data/Rakefile +87 -73
- data/VERSION +1 -1
- data/lib/mongomapper.rb +67 -71
- data/lib/mongomapper/associations.rb +86 -84
- data/lib/mongomapper/associations/belongs_to_polymorphic_proxy.rb +34 -34
- data/lib/mongomapper/associations/many_embedded_proxy.rb +67 -17
- data/lib/mongomapper/associations/proxy.rb +74 -73
- data/lib/mongomapper/document.rb +342 -348
- data/lib/mongomapper/embedded_document.rb +354 -274
- data/lib/mongomapper/finder_options.rb +84 -84
- data/lib/mongomapper/key.rb +32 -76
- data/lib/mongomapper/rails_compatibility/document.rb +14 -14
- data/lib/mongomapper/rails_compatibility/embedded_document.rb +26 -24
- data/lib/mongomapper/support.rb +156 -29
- data/lib/mongomapper/validations.rb +69 -47
- data/test/custom_matchers.rb +48 -0
- data/test/functional/associations/test_belongs_to_polymorphic_proxy.rb +53 -56
- data/test/functional/associations/test_belongs_to_proxy.rb +48 -49
- data/test/functional/associations/test_many_documents_as_proxy.rb +208 -253
- data/test/functional/associations/test_many_embedded_polymorphic_proxy.rb +130 -130
- data/test/functional/associations/test_many_embedded_proxy.rb +168 -106
- data/test/functional/associations/test_many_polymorphic_proxy.rb +261 -262
- data/test/functional/test_binary.rb +21 -0
- data/test/functional/test_document.rb +946 -952
- data/test/functional/test_embedded_document.rb +98 -0
- data/test/functional/test_pagination.rb +87 -80
- data/test/functional/test_rails_compatibility.rb +29 -29
- data/test/functional/test_validations.rb +262 -172
- data/test/models.rb +169 -169
- data/test/test_helper.rb +28 -66
- data/test/unit/serializers/test_json_serializer.rb +193 -193
- data/test/unit/test_document.rb +161 -123
- data/test/unit/test_embedded_document.rb +643 -547
- data/test/unit/test_finder_options.rb +183 -183
- data/test/unit/test_key.rb +175 -247
- data/test/unit/test_rails_compatibility.rb +38 -33
- data/test/unit/test_serializations.rb +52 -52
- data/test/unit/test_support.rb +268 -0
- data/test/unit/test_time_zones.rb +40 -0
- data/test/unit/test_validations.rb +499 -258
- metadata +22 -12
- data/History +0 -76
- data/mongomapper.gemspec +0 -145
@@ -1,259 +1,500 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
|
3
|
-
class ValidationsTest < Test::Unit::TestCase
|
4
|
-
context "Validations" do
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
|
256
|
-
|
257
|
-
|
258
|
-
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class ValidationsTest < Test::Unit::TestCase
|
4
|
+
context "Validations" do
|
5
|
+
|
6
|
+
context "on a Document" do
|
7
|
+
|
8
|
+
setup do
|
9
|
+
@document = Class.new do
|
10
|
+
include MongoMapper::Document
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
context "Validating acceptance of" do
|
15
|
+
should "work with validates_acceptance_of macro" do
|
16
|
+
@document.key :terms, String
|
17
|
+
@document.validates_acceptance_of :terms
|
18
|
+
doc = @document.new(:terms => '')
|
19
|
+
doc.should have_error_on(:terms)
|
20
|
+
doc.terms = '1'
|
21
|
+
doc.should_not have_error_on(:terms)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
context "validating confirmation of" do
|
26
|
+
should "work with validates_confirmation_of macro" do
|
27
|
+
@document.key :password, String
|
28
|
+
@document.validates_confirmation_of :password
|
29
|
+
doc = @document.new
|
30
|
+
doc.password = 'foobar'
|
31
|
+
doc.should have_error_on(:password)
|
32
|
+
doc.password_confirmation = 'foobar'
|
33
|
+
doc.should_not have_error_on(:password)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
context "validating format of" do
|
38
|
+
should "work with validates_format_of macro" do
|
39
|
+
@document.key :name, String
|
40
|
+
@document.validates_format_of :name, :with => /.+/
|
41
|
+
doc = @document.new
|
42
|
+
doc.should have_error_on(:name)
|
43
|
+
doc.name = 'John'
|
44
|
+
doc.should_not have_error_on(:name)
|
45
|
+
end
|
46
|
+
|
47
|
+
should "work with :format shorcut key" do
|
48
|
+
@document.key :name, String, :format => /.+/
|
49
|
+
doc = @document.new
|
50
|
+
doc.should have_error_on(:name)
|
51
|
+
doc.name = 'John'
|
52
|
+
doc.should_not have_error_on(:name)
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
context "validating length of" do
|
57
|
+
should "work with validates_length_of macro" do
|
58
|
+
@document.key :name, String
|
59
|
+
@document.validates_length_of :name, :minimum => 5
|
60
|
+
doc = @document.new
|
61
|
+
doc.should have_error_on(:name)
|
62
|
+
end
|
63
|
+
|
64
|
+
context "with :length => integer shortcut" do
|
65
|
+
should "set maximum of integer provided" do
|
66
|
+
@document.key :name, String, :length => 5
|
67
|
+
doc = @document.new
|
68
|
+
doc.name = '123456'
|
69
|
+
doc.should have_error_on(:name)
|
70
|
+
doc.name = '12345'
|
71
|
+
doc.should_not have_error_on(:name)
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
context "with :length => range shortcut" do
|
76
|
+
setup do
|
77
|
+
@document.key :name, String, :length => 5..7
|
78
|
+
end
|
79
|
+
|
80
|
+
should "set minimum of range min" do
|
81
|
+
doc = @document.new
|
82
|
+
doc.should have_error_on(:name)
|
83
|
+
doc.name = '123456'
|
84
|
+
doc.should_not have_error_on(:name)
|
85
|
+
end
|
86
|
+
|
87
|
+
should "set maximum of range max" do
|
88
|
+
doc = @document.new
|
89
|
+
doc.should have_error_on(:name)
|
90
|
+
doc.name = '12345678'
|
91
|
+
doc.should have_error_on(:name)
|
92
|
+
doc.name = '123456'
|
93
|
+
doc.should_not have_error_on(:name)
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
context "with :length => hash shortcut" do
|
98
|
+
should "pass options through" do
|
99
|
+
@document.key :name, String, :length => {:minimum => 2}
|
100
|
+
doc = @document.new
|
101
|
+
doc.should have_error_on(:name)
|
102
|
+
doc.name = '12'
|
103
|
+
doc.should_not have_error_on(:name)
|
104
|
+
end
|
105
|
+
end
|
106
|
+
end # validates_length_of
|
107
|
+
|
108
|
+
context "Validating numericality of" do
|
109
|
+
should "work with validates_numericality_of macro" do
|
110
|
+
@document.key :age, Integer
|
111
|
+
@document.validates_numericality_of :age
|
112
|
+
doc = @document.new
|
113
|
+
doc.age = 'String'
|
114
|
+
doc.should have_error_on(:age)
|
115
|
+
doc.age = 23
|
116
|
+
doc.should_not have_error_on(:age)
|
117
|
+
end
|
118
|
+
|
119
|
+
context "with :numeric shortcut" do
|
120
|
+
should "work with integer or float" do
|
121
|
+
@document.key :weight, Float, :numeric => true
|
122
|
+
doc = @document.new
|
123
|
+
doc.weight = 'String'
|
124
|
+
doc.should have_error_on(:weight)
|
125
|
+
doc.weight = 23.0
|
126
|
+
doc.should_not have_error_on(:weight)
|
127
|
+
doc.weight = 23
|
128
|
+
doc.should_not have_error_on(:weight)
|
129
|
+
end
|
130
|
+
end
|
131
|
+
|
132
|
+
context "with :numeric shortcut on Integer key" do
|
133
|
+
should "only work with integers" do
|
134
|
+
@document.key :age, Integer, :numeric => true
|
135
|
+
doc = @document.new
|
136
|
+
doc.age = 'String'
|
137
|
+
doc.should have_error_on(:age)
|
138
|
+
doc.age = 23.1
|
139
|
+
doc.should have_error_on(:age)
|
140
|
+
doc.age = 23
|
141
|
+
doc.should_not have_error_on(:age)
|
142
|
+
end
|
143
|
+
end
|
144
|
+
end # numericality of
|
145
|
+
|
146
|
+
context "validating presence of" do
|
147
|
+
should "work with validates_presence_of macro" do
|
148
|
+
@document.key :name, String
|
149
|
+
@document.validates_presence_of :name
|
150
|
+
doc = @document.new
|
151
|
+
doc.should have_error_on(:name)
|
152
|
+
end
|
153
|
+
|
154
|
+
should "work with :required shortcut on key definition" do
|
155
|
+
@document.key :name, String, :required => true
|
156
|
+
doc = @document.new
|
157
|
+
doc.should have_error_on(:name)
|
158
|
+
end
|
159
|
+
end
|
160
|
+
|
161
|
+
context "validating exclusion of" do
|
162
|
+
should "throw error if enumerator not provided" do
|
163
|
+
@document.key :action, String
|
164
|
+
lambda {
|
165
|
+
@document.validates_exclusion_of :action
|
166
|
+
}.should raise_error(ArgumentError)
|
167
|
+
end
|
168
|
+
|
169
|
+
should "work with validates_exclusion_of macro" do
|
170
|
+
@document.key :action, String
|
171
|
+
@document.validates_exclusion_of :action, :within => %w(kick run)
|
172
|
+
|
173
|
+
doc = @document.new
|
174
|
+
doc.should_not have_error_on(:action)
|
175
|
+
|
176
|
+
doc.action = 'fart'
|
177
|
+
doc.should_not have_error_on(:action)
|
178
|
+
|
179
|
+
doc.action = 'kick'
|
180
|
+
doc.should have_error_on(:action, 'is reserved')
|
181
|
+
end
|
182
|
+
|
183
|
+
should "not have error if allow nil is true and value is nil" do
|
184
|
+
@document.key :action, String
|
185
|
+
@document.validates_exclusion_of :action, :within => %w(kick run), :allow_nil => true
|
186
|
+
|
187
|
+
doc = @document.new
|
188
|
+
doc.should_not have_error_on(:action)
|
189
|
+
end
|
190
|
+
|
191
|
+
should "not have error if allow blank is true and value is blank" do
|
192
|
+
@document.key :action, String
|
193
|
+
@document.validates_exclusion_of :action, :within => %w(kick run), :allow_nil => true
|
194
|
+
|
195
|
+
doc = @document.new(:action => '')
|
196
|
+
doc.should_not have_error_on(:action)
|
197
|
+
end
|
198
|
+
end
|
199
|
+
|
200
|
+
context "validating inclusion of" do
|
201
|
+
should "throw error if enumerator not provided" do
|
202
|
+
@document.key :action, String
|
203
|
+
lambda {
|
204
|
+
@document.validates_inclusion_of :action
|
205
|
+
}.should raise_error(ArgumentError)
|
206
|
+
end
|
207
|
+
|
208
|
+
should "work with validates_inclusion_of macro" do
|
209
|
+
@document.key :action, String
|
210
|
+
@document.validates_inclusion_of :action, :within => %w(kick run)
|
211
|
+
|
212
|
+
doc = @document.new
|
213
|
+
doc.should have_error_on(:action, 'is not in the list')
|
214
|
+
|
215
|
+
doc.action = 'fart'
|
216
|
+
doc.should have_error_on(:action, 'is not in the list')
|
217
|
+
|
218
|
+
doc.action = 'kick'
|
219
|
+
doc.should_not have_error_on(:action)
|
220
|
+
end
|
221
|
+
|
222
|
+
should "not have error if allow nil is true and value is nil" do
|
223
|
+
@document.key :action, String
|
224
|
+
@document.validates_inclusion_of :action, :within => %w(kick run), :allow_nil => true
|
225
|
+
|
226
|
+
doc = @document.new
|
227
|
+
doc.should_not have_error_on(:action)
|
228
|
+
end
|
229
|
+
|
230
|
+
should "not have error if allow blank is true and value is blank" do
|
231
|
+
@document.key :action, String
|
232
|
+
@document.validates_inclusion_of :action, :within => %w(kick run), :allow_blank => true
|
233
|
+
|
234
|
+
doc = @document.new(:action => '')
|
235
|
+
doc.should_not have_error_on(:action)
|
236
|
+
end
|
237
|
+
end
|
238
|
+
|
239
|
+
end # End on a Document
|
240
|
+
|
241
|
+
context "On an EmbeddedDocument" do
|
242
|
+
|
243
|
+
setup do
|
244
|
+
@embedded_doc = Class.new do
|
245
|
+
include MongoMapper::EmbeddedDocument
|
246
|
+
end
|
247
|
+
end
|
248
|
+
|
249
|
+
context "Validating acceptance of" do
|
250
|
+
should "work with validates_acceptance_of macro" do
|
251
|
+
@embedded_doc.key :terms, String
|
252
|
+
@embedded_doc.validates_acceptance_of :terms
|
253
|
+
doc = @embedded_doc.new(:terms => '')
|
254
|
+
doc.should have_error_on(:terms)
|
255
|
+
doc.terms = '1'
|
256
|
+
doc.should_not have_error_on(:terms)
|
257
|
+
end
|
258
|
+
end
|
259
|
+
|
260
|
+
context "validating confirmation of" do
|
261
|
+
should "work with validates_confirmation_of macro" do
|
262
|
+
@embedded_doc.key :password, String
|
263
|
+
@embedded_doc.validates_confirmation_of :password
|
264
|
+
doc = @embedded_doc.new
|
265
|
+
doc.password = 'foobar'
|
266
|
+
doc.should have_error_on(:password)
|
267
|
+
doc.password_confirmation = 'foobar'
|
268
|
+
doc.should_not have_error_on(:password)
|
269
|
+
end
|
270
|
+
end
|
271
|
+
|
272
|
+
context "validating format of" do
|
273
|
+
should "work with validates_format_of macro" do
|
274
|
+
@embedded_doc.key :name, String
|
275
|
+
@embedded_doc.validates_format_of :name, :with => /.+/
|
276
|
+
doc = @embedded_doc.new
|
277
|
+
doc.should have_error_on(:name)
|
278
|
+
doc.name = 'John'
|
279
|
+
doc.should_not have_error_on(:name)
|
280
|
+
end
|
281
|
+
|
282
|
+
should "work with :format shorcut key" do
|
283
|
+
@embedded_doc.key :name, String, :format => /.+/
|
284
|
+
doc = @embedded_doc.new
|
285
|
+
doc.should have_error_on(:name)
|
286
|
+
doc.name = 'John'
|
287
|
+
doc.should_not have_error_on(:name)
|
288
|
+
end
|
289
|
+
end
|
290
|
+
|
291
|
+
context "validating length of" do
|
292
|
+
should "work with validates_length_of macro" do
|
293
|
+
@embedded_doc.key :name, String
|
294
|
+
@embedded_doc.validates_length_of :name, :minimum => 5
|
295
|
+
doc = @embedded_doc.new
|
296
|
+
doc.should have_error_on(:name)
|
297
|
+
end
|
298
|
+
|
299
|
+
context "with :length => integer shortcut" do
|
300
|
+
should "set maximum of integer provided" do
|
301
|
+
@embedded_doc.key :name, String, :length => 5
|
302
|
+
doc = @embedded_doc.new
|
303
|
+
doc.name = '123456'
|
304
|
+
doc.should have_error_on(:name)
|
305
|
+
doc.name = '12345'
|
306
|
+
doc.should_not have_error_on(:name)
|
307
|
+
end
|
308
|
+
end
|
309
|
+
|
310
|
+
context "with :length => range shortcut" do
|
311
|
+
setup do
|
312
|
+
@embedded_doc.key :name, String, :length => 5..7
|
313
|
+
end
|
314
|
+
|
315
|
+
should "set minimum of range min" do
|
316
|
+
doc = @embedded_doc.new
|
317
|
+
doc.should have_error_on(:name)
|
318
|
+
doc.name = '123456'
|
319
|
+
doc.should_not have_error_on(:name)
|
320
|
+
end
|
321
|
+
|
322
|
+
should "set maximum of range max" do
|
323
|
+
doc = @embedded_doc.new
|
324
|
+
doc.should have_error_on(:name)
|
325
|
+
doc.name = '12345678'
|
326
|
+
doc.should have_error_on(:name)
|
327
|
+
doc.name = '123456'
|
328
|
+
doc.should_not have_error_on(:name)
|
329
|
+
end
|
330
|
+
end
|
331
|
+
|
332
|
+
context "with :length => hash shortcut" do
|
333
|
+
should "pass options through" do
|
334
|
+
@embedded_doc.key :name, String, :length => {:minimum => 2}
|
335
|
+
doc = @embedded_doc.new
|
336
|
+
doc.should have_error_on(:name)
|
337
|
+
doc.name = '12'
|
338
|
+
doc.should_not have_error_on(:name)
|
339
|
+
end
|
340
|
+
end
|
341
|
+
end # validates_length_of
|
342
|
+
|
343
|
+
context "Validating numericality of" do
|
344
|
+
should "work with validates_numericality_of macro" do
|
345
|
+
@embedded_doc.key :age, Integer
|
346
|
+
@embedded_doc.validates_numericality_of :age
|
347
|
+
doc = @embedded_doc.new
|
348
|
+
doc.age = 'String'
|
349
|
+
doc.should have_error_on(:age)
|
350
|
+
doc.age = 23
|
351
|
+
doc.should_not have_error_on(:age)
|
352
|
+
end
|
353
|
+
|
354
|
+
context "with :numeric shortcut" do
|
355
|
+
should "work with integer or float" do
|
356
|
+
@embedded_doc.key :weight, Float, :numeric => true
|
357
|
+
doc = @embedded_doc.new
|
358
|
+
doc.weight = 'String'
|
359
|
+
doc.should have_error_on(:weight)
|
360
|
+
doc.weight = 23.0
|
361
|
+
doc.should_not have_error_on(:weight)
|
362
|
+
doc.weight = 23
|
363
|
+
doc.should_not have_error_on(:weight)
|
364
|
+
end
|
365
|
+
end
|
366
|
+
|
367
|
+
context "with :numeric shortcut on Integer key" do
|
368
|
+
should "only work with integers" do
|
369
|
+
@embedded_doc.key :age, Integer, :numeric => true
|
370
|
+
doc = @embedded_doc.new
|
371
|
+
doc.age = 'String'
|
372
|
+
doc.should have_error_on(:age)
|
373
|
+
doc.age = 23.1
|
374
|
+
doc.should have_error_on(:age)
|
375
|
+
doc.age = 23
|
376
|
+
doc.should_not have_error_on(:age)
|
377
|
+
end
|
378
|
+
end
|
379
|
+
end # numericality of
|
380
|
+
|
381
|
+
context "validating presence of" do
|
382
|
+
should "work with validates_presence_of macro" do
|
383
|
+
@embedded_doc.key :name, String
|
384
|
+
@embedded_doc.validates_presence_of :name
|
385
|
+
doc = @embedded_doc.new
|
386
|
+
doc.should have_error_on(:name)
|
387
|
+
end
|
388
|
+
|
389
|
+
should "work with :required shortcut on key definition" do
|
390
|
+
@embedded_doc.key :name, String, :required => true
|
391
|
+
doc = @embedded_doc.new
|
392
|
+
doc.should have_error_on(:name)
|
393
|
+
end
|
394
|
+
end
|
395
|
+
|
396
|
+
context "validating exclusion of" do
|
397
|
+
should "throw error if enumerator not provided" do
|
398
|
+
@embedded_doc.key :action, String
|
399
|
+
lambda {
|
400
|
+
@embedded_doc.validates_exclusion_of :action
|
401
|
+
}.should raise_error(ArgumentError)
|
402
|
+
end
|
403
|
+
|
404
|
+
should "work with validates_exclusion_of macro" do
|
405
|
+
@embedded_doc.key :action, String
|
406
|
+
@embedded_doc.validates_exclusion_of :action, :within => %w(kick run)
|
407
|
+
|
408
|
+
doc = @embedded_doc.new
|
409
|
+
doc.should_not have_error_on(:action)
|
410
|
+
|
411
|
+
doc.action = 'fart'
|
412
|
+
doc.should_not have_error_on(:action)
|
413
|
+
|
414
|
+
doc.action = 'kick'
|
415
|
+
doc.should have_error_on(:action, 'is reserved')
|
416
|
+
end
|
417
|
+
|
418
|
+
should "not have error if allow nil is true and value is nil" do
|
419
|
+
@embedded_doc.key :action, String
|
420
|
+
@embedded_doc.validates_exclusion_of :action, :within => %w(kick run), :allow_nil => true
|
421
|
+
|
422
|
+
doc = @embedded_doc.new
|
423
|
+
doc.should_not have_error_on(:action)
|
424
|
+
end
|
425
|
+
|
426
|
+
should "not have error if allow blank is true and value is blank" do
|
427
|
+
@embedded_doc.key :action, String
|
428
|
+
@embedded_doc.validates_exclusion_of :action, :within => %w(kick run), :allow_nil => true
|
429
|
+
|
430
|
+
doc = @embedded_doc.new(:action => '')
|
431
|
+
doc.should_not have_error_on(:action)
|
432
|
+
end
|
433
|
+
end
|
434
|
+
|
435
|
+
context "validating inclusion of" do
|
436
|
+
should "throw error if enumerator not provided" do
|
437
|
+
@embedded_doc.key :action, String
|
438
|
+
lambda {
|
439
|
+
@embedded_doc.validates_inclusion_of :action
|
440
|
+
}.should raise_error(ArgumentError)
|
441
|
+
end
|
442
|
+
|
443
|
+
should "work with validates_inclusion_of macro" do
|
444
|
+
@embedded_doc.key :action, String
|
445
|
+
@embedded_doc.validates_inclusion_of :action, :within => %w(kick run)
|
446
|
+
|
447
|
+
doc = @embedded_doc.new
|
448
|
+
doc.should have_error_on(:action, 'is not in the list')
|
449
|
+
|
450
|
+
doc.action = 'fart'
|
451
|
+
doc.should have_error_on(:action, 'is not in the list')
|
452
|
+
|
453
|
+
doc.action = 'kick'
|
454
|
+
doc.should_not have_error_on(:action)
|
455
|
+
end
|
456
|
+
|
457
|
+
should "not have error if allow nil is true and value is nil" do
|
458
|
+
@embedded_doc.key :action, String
|
459
|
+
@embedded_doc.validates_inclusion_of :action, :within => %w(kick run), :allow_nil => true
|
460
|
+
|
461
|
+
doc = @embedded_doc.new
|
462
|
+
doc.should_not have_error_on(:action)
|
463
|
+
end
|
464
|
+
|
465
|
+
should "not have error if allow blank is true and value is blank" do
|
466
|
+
@embedded_doc.key :action, String
|
467
|
+
@embedded_doc.validates_inclusion_of :action, :within => %w(kick run), :allow_blank => true
|
468
|
+
|
469
|
+
doc = @embedded_doc.new(:action => '')
|
470
|
+
doc.should_not have_error_on(:action)
|
471
|
+
end
|
472
|
+
end
|
473
|
+
|
474
|
+
end # End on an EmbeddedDocument
|
475
|
+
|
476
|
+
end # Validations
|
477
|
+
|
478
|
+
context "Adding validation errors" do
|
479
|
+
setup do
|
480
|
+
@document = Class.new do
|
481
|
+
include MongoMapper::Document
|
482
|
+
key :action, String
|
483
|
+
def action_present
|
484
|
+
errors.add(:action, 'is invalid') if action.blank?
|
485
|
+
end
|
486
|
+
end
|
487
|
+
end
|
488
|
+
|
489
|
+
should "work with validate callback" do
|
490
|
+
@document.validate :action_present
|
491
|
+
|
492
|
+
doc = @document.new
|
493
|
+
doc.action = nil
|
494
|
+
doc.should have_error_on(:action)
|
495
|
+
|
496
|
+
doc.action = 'kick'
|
497
|
+
doc.should_not have_error_on(:action)
|
498
|
+
end
|
499
|
+
end
|
259
500
|
end
|