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.
Files changed (44) hide show
  1. data/README.rdoc +38 -38
  2. data/Rakefile +87 -73
  3. data/VERSION +1 -1
  4. data/lib/mongomapper.rb +67 -71
  5. data/lib/mongomapper/associations.rb +86 -84
  6. data/lib/mongomapper/associations/belongs_to_polymorphic_proxy.rb +34 -34
  7. data/lib/mongomapper/associations/many_embedded_proxy.rb +67 -17
  8. data/lib/mongomapper/associations/proxy.rb +74 -73
  9. data/lib/mongomapper/document.rb +342 -348
  10. data/lib/mongomapper/embedded_document.rb +354 -274
  11. data/lib/mongomapper/finder_options.rb +84 -84
  12. data/lib/mongomapper/key.rb +32 -76
  13. data/lib/mongomapper/rails_compatibility/document.rb +14 -14
  14. data/lib/mongomapper/rails_compatibility/embedded_document.rb +26 -24
  15. data/lib/mongomapper/support.rb +156 -29
  16. data/lib/mongomapper/validations.rb +69 -47
  17. data/test/custom_matchers.rb +48 -0
  18. data/test/functional/associations/test_belongs_to_polymorphic_proxy.rb +53 -56
  19. data/test/functional/associations/test_belongs_to_proxy.rb +48 -49
  20. data/test/functional/associations/test_many_documents_as_proxy.rb +208 -253
  21. data/test/functional/associations/test_many_embedded_polymorphic_proxy.rb +130 -130
  22. data/test/functional/associations/test_many_embedded_proxy.rb +168 -106
  23. data/test/functional/associations/test_many_polymorphic_proxy.rb +261 -262
  24. data/test/functional/test_binary.rb +21 -0
  25. data/test/functional/test_document.rb +946 -952
  26. data/test/functional/test_embedded_document.rb +98 -0
  27. data/test/functional/test_pagination.rb +87 -80
  28. data/test/functional/test_rails_compatibility.rb +29 -29
  29. data/test/functional/test_validations.rb +262 -172
  30. data/test/models.rb +169 -169
  31. data/test/test_helper.rb +28 -66
  32. data/test/unit/serializers/test_json_serializer.rb +193 -193
  33. data/test/unit/test_document.rb +161 -123
  34. data/test/unit/test_embedded_document.rb +643 -547
  35. data/test/unit/test_finder_options.rb +183 -183
  36. data/test/unit/test_key.rb +175 -247
  37. data/test/unit/test_rails_compatibility.rb +38 -33
  38. data/test/unit/test_serializations.rb +52 -52
  39. data/test/unit/test_support.rb +268 -0
  40. data/test/unit/test_time_zones.rb +40 -0
  41. data/test/unit/test_validations.rb +499 -258
  42. metadata +22 -12
  43. data/History +0 -76
  44. 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
- setup do
6
- @document = Class.new do
7
- include MongoMapper::Document
8
- end
9
- end
10
-
11
- context "Validating acceptance of" do
12
- should "work with validates_acceptance_of macro" do
13
- @document.key :terms, String
14
- @document.validates_acceptance_of :terms
15
- doc = @document.new(:terms => '')
16
- doc.should have_error_on(:terms)
17
- doc.terms = '1'
18
- doc.should_not have_error_on(:terms)
19
- end
20
- end
21
-
22
- context "validating confirmation of" do
23
- should "work with validates_confirmation_of macro" do
24
- @document.key :password, String
25
- @document.validates_confirmation_of :password
26
- doc = @document.new
27
- doc.password = 'foobar'
28
- doc.should have_error_on(:password)
29
- doc.password_confirmation = 'foobar'
30
- doc.should_not have_error_on(:password)
31
- end
32
- end
33
-
34
- context "validating format of" do
35
- should "work with validates_format_of macro" do
36
- @document.key :name, String
37
- @document.validates_format_of :name, :with => /.+/
38
- doc = @document.new
39
- doc.should have_error_on(:name)
40
- doc.name = 'John'
41
- doc.should_not have_error_on(:name)
42
- end
43
-
44
- should "work with :format shorcut key" do
45
- @document.key :name, String, :format => /.+/
46
- doc = @document.new
47
- doc.should have_error_on(:name)
48
- doc.name = 'John'
49
- doc.should_not have_error_on(:name)
50
- end
51
- end
52
-
53
- context "validating length of" do
54
- should "work with validates_length_of macro" do
55
- @document.key :name, String
56
- @document.validates_length_of :name, :minimum => 5
57
- doc = @document.new
58
- doc.should have_error_on(:name)
59
- end
60
-
61
- context "with :length => integer shortcut" do
62
- should "set maximum of integer provided" do
63
- @document.key :name, String, :length => 5
64
- doc = @document.new
65
- doc.name = '123456'
66
- doc.should have_error_on(:name)
67
- doc.name = '12345'
68
- doc.should_not have_error_on(:name)
69
- end
70
- end
71
-
72
- context "with :length => range shortcut" do
73
- setup do
74
- @document.key :name, String, :length => 5..7
75
- end
76
-
77
- should "set minimum of range min" do
78
- doc = @document.new
79
- doc.should have_error_on(:name)
80
- doc.name = '123456'
81
- doc.should_not have_error_on(:name)
82
- end
83
-
84
- should "set maximum of range max" do
85
- doc = @document.new
86
- doc.should have_error_on(:name)
87
- doc.name = '12345678'
88
- doc.should have_error_on(:name)
89
- doc.name = '123456'
90
- doc.should_not have_error_on(:name)
91
- end
92
- end
93
-
94
- context "with :length => hash shortcut" do
95
- should "pass options through" do
96
- @document.key :name, String, :length => {:minimum => 2}
97
- doc = @document.new
98
- doc.should have_error_on(:name)
99
- doc.name = '12'
100
- doc.should_not have_error_on(:name)
101
- end
102
- end
103
- end # validates_length_of
104
-
105
- context "Validating numericality of" do
106
- should "work with validates_numericality_of macro" do
107
- @document.key :age, Integer
108
- @document.validates_numericality_of :age
109
- doc = @document.new
110
- doc.age = 'String'
111
- doc.should have_error_on(:age)
112
- doc.age = 23
113
- doc.should_not have_error_on(:age)
114
- end
115
-
116
- context "with :numeric shortcut" do
117
- should "work with integer or float" do
118
- @document.key :weight, Float, :numeric => true
119
- doc = @document.new
120
- doc.weight = 'String'
121
- doc.should have_error_on(:weight)
122
- doc.weight = 23.0
123
- doc.should_not have_error_on(:weight)
124
- doc.weight = 23
125
- doc.should_not have_error_on(:weight)
126
- end
127
- end
128
-
129
- context "with :numeric shortcut on Integer key" do
130
- should "only work with integers" do
131
- @document.key :age, Integer, :numeric => true
132
- doc = @document.new
133
- doc.age = 'String'
134
- doc.should have_error_on(:age)
135
- doc.age = 23.1
136
- doc.should have_error_on(:age)
137
- doc.age = 23
138
- doc.should_not have_error_on(:age)
139
- end
140
- end
141
- end # numericality of
142
-
143
- context "validating presence of" do
144
- should "work with validates_presence_of macro" do
145
- @document.key :name, String
146
- @document.validates_presence_of :name
147
- doc = @document.new
148
- doc.should have_error_on(:name)
149
- end
150
-
151
- should "work with :required shortcut on key definition" do
152
- @document.key :name, String, :required => true
153
- doc = @document.new
154
- doc.should have_error_on(:name)
155
- end
156
- end
157
-
158
- context "validating exclusion of" do
159
- should "throw error if enumerator not provided" do
160
- @document.key :action, String
161
- lambda {
162
- @document.validates_exclusion_of :action
163
- }.should raise_error(ArgumentError)
164
- end
165
-
166
- should "work with validates_exclusion_of macro" do
167
- @document.key :action, String
168
- @document.validates_exclusion_of :action, :within => %w(kick run)
169
-
170
- doc = @document.new
171
- doc.should_not have_error_on(:action)
172
-
173
- doc.action = 'fart'
174
- doc.should_not have_error_on(:action)
175
-
176
- doc.action = 'kick'
177
- doc.should have_error_on(:action, 'is reserved')
178
- end
179
-
180
- should "not have error if allow nil is true and value is nil" do
181
- @document.key :action, String
182
- @document.validates_exclusion_of :action, :within => %w(kick run), :allow_nil => true
183
-
184
- doc = @document.new
185
- doc.should_not have_error_on(:action)
186
- end
187
-
188
- should "not have error if allow blank is true and value is blank" do
189
- @document.key :action, String
190
- @document.validates_exclusion_of :action, :within => %w(kick run), :allow_nil => true
191
-
192
- doc = @document.new(:action => '')
193
- doc.should_not have_error_on(:action)
194
- end
195
- end
196
-
197
- context "validating inclusion of" do
198
- should "throw error if enumerator not provided" do
199
- @document.key :action, String
200
- lambda {
201
- @document.validates_inclusion_of :action
202
- }.should raise_error(ArgumentError)
203
- end
204
-
205
- should "work with validates_inclusion_of macro" do
206
- @document.key :action, String
207
- @document.validates_inclusion_of :action, :within => %w(kick run)
208
-
209
- doc = @document.new
210
- doc.should have_error_on(:action, 'is not in the list')
211
-
212
- doc.action = 'fart'
213
- doc.should have_error_on(:action, 'is not in the list')
214
-
215
- doc.action = 'kick'
216
- doc.should_not have_error_on(:action)
217
- end
218
-
219
- should "not have error if allow nil is true and value is nil" do
220
- @document.key :action, String
221
- @document.validates_inclusion_of :action, :within => %w(kick run), :allow_nil => true
222
-
223
- doc = @document.new
224
- doc.should_not have_error_on(:action)
225
- end
226
-
227
- should "not have error if allow blank is true and value is blank" do
228
- @document.key :action, String
229
- @document.validates_inclusion_of :action, :within => %w(kick run), :allow_blank => true
230
-
231
- doc = @document.new(:action => '')
232
- doc.should_not have_error_on(:action)
233
- end
234
- end
235
- end # Validations
236
-
237
- context "Adding validation errors" do
238
- setup do
239
- @document = Class.new do
240
- include MongoMapper::Document
241
- key :action, String
242
- def action_present
243
- errors.add(:action, 'is invalid') if action.blank?
244
- end
245
- end
246
- end
247
-
248
- should "work with validate callback" do
249
- @document.validate :action_present
250
-
251
- doc = @document.new
252
- doc.action = nil
253
- doc.should have_error_on(:action)
254
-
255
- doc.action = 'kick'
256
- doc.should_not have_error_on(:action)
257
- end
258
- end
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