thorsson-mongo_mapper 0.8.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (134) hide show
  1. data/LICENSE +20 -0
  2. data/README.rdoc +27 -0
  3. data/bin/mmconsole +60 -0
  4. data/examples/keys.rb +40 -0
  5. data/examples/modifiers/set.rb +25 -0
  6. data/examples/plugins.rb +41 -0
  7. data/examples/querying.rb +35 -0
  8. data/examples/scopes.rb +52 -0
  9. data/lib/mongo_mapper.rb +79 -0
  10. data/lib/mongo_mapper/connection.rb +83 -0
  11. data/lib/mongo_mapper/document.rb +41 -0
  12. data/lib/mongo_mapper/embedded_document.rb +31 -0
  13. data/lib/mongo_mapper/exceptions.rb +27 -0
  14. data/lib/mongo_mapper/extensions/array.rb +19 -0
  15. data/lib/mongo_mapper/extensions/binary.rb +22 -0
  16. data/lib/mongo_mapper/extensions/boolean.rb +44 -0
  17. data/lib/mongo_mapper/extensions/date.rb +25 -0
  18. data/lib/mongo_mapper/extensions/float.rb +14 -0
  19. data/lib/mongo_mapper/extensions/hash.rb +14 -0
  20. data/lib/mongo_mapper/extensions/integer.rb +19 -0
  21. data/lib/mongo_mapper/extensions/kernel.rb +9 -0
  22. data/lib/mongo_mapper/extensions/nil_class.rb +18 -0
  23. data/lib/mongo_mapper/extensions/object.rb +27 -0
  24. data/lib/mongo_mapper/extensions/object_id.rb +30 -0
  25. data/lib/mongo_mapper/extensions/set.rb +20 -0
  26. data/lib/mongo_mapper/extensions/string.rb +18 -0
  27. data/lib/mongo_mapper/extensions/time.rb +29 -0
  28. data/lib/mongo_mapper/plugins.rb +16 -0
  29. data/lib/mongo_mapper/plugins/accessible.rb +44 -0
  30. data/lib/mongo_mapper/plugins/associations.rb +97 -0
  31. data/lib/mongo_mapper/plugins/associations/base.rb +124 -0
  32. data/lib/mongo_mapper/plugins/associations/belongs_to_polymorphic_proxy.rb +29 -0
  33. data/lib/mongo_mapper/plugins/associations/belongs_to_proxy.rb +24 -0
  34. data/lib/mongo_mapper/plugins/associations/collection.rb +22 -0
  35. data/lib/mongo_mapper/plugins/associations/embedded_collection.rb +40 -0
  36. data/lib/mongo_mapper/plugins/associations/in_array_proxy.rb +127 -0
  37. data/lib/mongo_mapper/plugins/associations/many_documents_as_proxy.rb +28 -0
  38. data/lib/mongo_mapper/plugins/associations/many_documents_proxy.rb +110 -0
  39. data/lib/mongo_mapper/plugins/associations/many_embedded_polymorphic_proxy.rb +32 -0
  40. data/lib/mongo_mapper/plugins/associations/many_embedded_proxy.rb +24 -0
  41. data/lib/mongo_mapper/plugins/associations/many_polymorphic_proxy.rb +14 -0
  42. data/lib/mongo_mapper/plugins/associations/one_embedded_proxy.rb +40 -0
  43. data/lib/mongo_mapper/plugins/associations/one_proxy.rb +68 -0
  44. data/lib/mongo_mapper/plugins/associations/proxy.rb +126 -0
  45. data/lib/mongo_mapper/plugins/caching.rb +21 -0
  46. data/lib/mongo_mapper/plugins/callbacks.rb +241 -0
  47. data/lib/mongo_mapper/plugins/clone.rb +19 -0
  48. data/lib/mongo_mapper/plugins/descendants.rb +17 -0
  49. data/lib/mongo_mapper/plugins/dirty.rb +120 -0
  50. data/lib/mongo_mapper/plugins/document.rb +41 -0
  51. data/lib/mongo_mapper/plugins/dynamic_querying.rb +43 -0
  52. data/lib/mongo_mapper/plugins/dynamic_querying/dynamic_finder.rb +44 -0
  53. data/lib/mongo_mapper/plugins/embedded_document.rb +49 -0
  54. data/lib/mongo_mapper/plugins/equality.rb +17 -0
  55. data/lib/mongo_mapper/plugins/identity_map.rb +128 -0
  56. data/lib/mongo_mapper/plugins/indexes.rb +12 -0
  57. data/lib/mongo_mapper/plugins/inspect.rb +15 -0
  58. data/lib/mongo_mapper/plugins/keys.rb +309 -0
  59. data/lib/mongo_mapper/plugins/keys/key.rb +55 -0
  60. data/lib/mongo_mapper/plugins/logger.rb +18 -0
  61. data/lib/mongo_mapper/plugins/modifiers.rb +112 -0
  62. data/lib/mongo_mapper/plugins/pagination.rb +14 -0
  63. data/lib/mongo_mapper/plugins/persistence.rb +69 -0
  64. data/lib/mongo_mapper/plugins/protected.rb +53 -0
  65. data/lib/mongo_mapper/plugins/querying.rb +176 -0
  66. data/lib/mongo_mapper/plugins/querying/decorator.rb +46 -0
  67. data/lib/mongo_mapper/plugins/querying/plucky_methods.rb +15 -0
  68. data/lib/mongo_mapper/plugins/rails.rb +58 -0
  69. data/lib/mongo_mapper/plugins/safe.rb +28 -0
  70. data/lib/mongo_mapper/plugins/sci.rb +32 -0
  71. data/lib/mongo_mapper/plugins/scopes.rb +21 -0
  72. data/lib/mongo_mapper/plugins/serialization.rb +76 -0
  73. data/lib/mongo_mapper/plugins/timestamps.rb +22 -0
  74. data/lib/mongo_mapper/plugins/userstamps.rb +15 -0
  75. data/lib/mongo_mapper/plugins/validations.rb +50 -0
  76. data/lib/mongo_mapper/support/descendant_appends.rb +45 -0
  77. data/lib/mongo_mapper/version.rb +4 -0
  78. data/test/_NOTE_ON_TESTING +1 -0
  79. data/test/functional/associations/test_belongs_to_polymorphic_proxy.rb +63 -0
  80. data/test/functional/associations/test_belongs_to_proxy.rb +93 -0
  81. data/test/functional/associations/test_in_array_proxy.rb +319 -0
  82. data/test/functional/associations/test_many_documents_as_proxy.rb +229 -0
  83. data/test/functional/associations/test_many_documents_proxy.rb +575 -0
  84. data/test/functional/associations/test_many_embedded_polymorphic_proxy.rb +176 -0
  85. data/test/functional/associations/test_many_embedded_proxy.rb +256 -0
  86. data/test/functional/associations/test_many_polymorphic_proxy.rb +302 -0
  87. data/test/functional/associations/test_one_embedded_proxy.rb +67 -0
  88. data/test/functional/associations/test_one_proxy.rb +182 -0
  89. data/test/functional/test_accessible.rb +168 -0
  90. data/test/functional/test_associations.rb +44 -0
  91. data/test/functional/test_binary.rb +27 -0
  92. data/test/functional/test_caching.rb +76 -0
  93. data/test/functional/test_callbacks.rb +151 -0
  94. data/test/functional/test_dirty.rb +163 -0
  95. data/test/functional/test_document.rb +253 -0
  96. data/test/functional/test_dynamic_querying.rb +75 -0
  97. data/test/functional/test_embedded_document.rb +210 -0
  98. data/test/functional/test_identity_map.rb +506 -0
  99. data/test/functional/test_indexes.rb +42 -0
  100. data/test/functional/test_logger.rb +20 -0
  101. data/test/functional/test_modifiers.rb +416 -0
  102. data/test/functional/test_pagination.rb +91 -0
  103. data/test/functional/test_protected.rb +175 -0
  104. data/test/functional/test_querying.rb +873 -0
  105. data/test/functional/test_safe.rb +76 -0
  106. data/test/functional/test_sci.rb +230 -0
  107. data/test/functional/test_scopes.rb +171 -0
  108. data/test/functional/test_string_id_compatibility.rb +67 -0
  109. data/test/functional/test_timestamps.rb +62 -0
  110. data/test/functional/test_userstamps.rb +27 -0
  111. data/test/functional/test_validations.rb +342 -0
  112. data/test/models.rb +233 -0
  113. data/test/test_active_model_lint.rb +13 -0
  114. data/test/test_helper.rb +104 -0
  115. data/test/unit/associations/test_base.rb +212 -0
  116. data/test/unit/associations/test_proxy.rb +105 -0
  117. data/test/unit/serializers/test_json_serializer.rb +202 -0
  118. data/test/unit/test_clone.rb +69 -0
  119. data/test/unit/test_descendant_appends.rb +71 -0
  120. data/test/unit/test_document.rb +213 -0
  121. data/test/unit/test_dynamic_finder.rb +125 -0
  122. data/test/unit/test_embedded_document.rb +644 -0
  123. data/test/unit/test_extensions.rb +380 -0
  124. data/test/unit/test_key.rb +185 -0
  125. data/test/unit/test_keys.rb +55 -0
  126. data/test/unit/test_mongo_mapper.rb +110 -0
  127. data/test/unit/test_pagination.rb +11 -0
  128. data/test/unit/test_plugins.rb +50 -0
  129. data/test/unit/test_rails.rb +181 -0
  130. data/test/unit/test_rails_compatibility.rb +52 -0
  131. data/test/unit/test_serialization.rb +51 -0
  132. data/test/unit/test_time_zones.rb +39 -0
  133. data/test/unit/test_validations.rb +544 -0
  134. metadata +316 -0
@@ -0,0 +1,51 @@
1
+ require 'test_helper'
2
+
3
+ class SerializationTest < Test::Unit::TestCase
4
+ def setup
5
+ @document = EDoc do
6
+ key :name, String
7
+ key :age, Integer
8
+ key :awesome, Boolean
9
+ key :preferences, Hash
10
+ key :created_at, Time
11
+ end
12
+
13
+ @instance = @document.new(
14
+ :name => 'John Doe',
15
+ :age => 25,
16
+ :awesome => true,
17
+ :preferences => {:language => 'Ruby'},
18
+ :created_at => Time.now.change(:usec => 0)
19
+ )
20
+ end
21
+
22
+ [:json].each do |format|
23
+ context format do
24
+ should "be reversable" do
25
+ serialized = @instance.send("to_#{format}")
26
+ unserialized = @document.send("from_#{format}", serialized)
27
+
28
+ assert_equal @instance, unserialized
29
+ end
30
+
31
+ should "allow attribute only filtering" do
32
+ serialized = @instance.send("to_#{format}", :only => [ :age, :name ])
33
+ unserialized = @document.send("from_#{format}", serialized)
34
+
35
+ assert_equal @instance.name, unserialized.name
36
+ assert_equal @instance.age, unserialized.age
37
+ assert ! unserialized.awesome
38
+ assert_nil unserialized.created_at
39
+ end
40
+
41
+ should "allow attribute except filtering" do
42
+ serialized = @instance.send("to_#{format}", :except => [ :age, :name ])
43
+ unserialized = @document.send("from_#{format}", serialized)
44
+
45
+ assert_nil unserialized.name
46
+ assert_nil unserialized.age
47
+ assert_equal @instance.awesome, unserialized.awesome
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,39 @@
1
+ require 'test_helper'
2
+
3
+ class TimeZonesTest < Test::Unit::TestCase
4
+ context "An instance of an embedded document" do
5
+ setup do
6
+ @document = EDoc do
7
+ key :name, String
8
+ key :created_at, Time
9
+ end
10
+ end
11
+
12
+ should "work without Time.zone" do
13
+ Time.zone = nil
14
+
15
+ doc = @document.new(:created_at => "2009-08-15 14:00:00")
16
+ doc.created_at.should == Time.local(2009, 8, 15, 14, 0, 0).utc
17
+ end
18
+
19
+ should "work with Time.zone set to the (default) UTC" do
20
+ Time.zone = 'UTC'
21
+
22
+ doc = @document.new(:created_at => "2009-08-15 14:00:00")
23
+ doc.created_at.is_a?(ActiveSupport::TimeWithZone).should be_true
24
+ doc.created_at.should == Time.utc(2009, 8, 15, 14)
25
+
26
+ Time.zone = nil
27
+ end
28
+
29
+ should "work with timezones that are not UTC" do
30
+ Time.zone = 'Hawaii'
31
+
32
+ doc = @document.new(:created_at => "2009-08-15 14:00:00")
33
+ doc.created_at.is_a?(ActiveSupport::TimeWithZone).should be_true
34
+ doc.created_at.should == Time.utc(2009, 8, 16)
35
+
36
+ Time.zone = nil
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,544 @@
1
+ require 'test_helper'
2
+
3
+ class ValidationsTest < Test::Unit::TestCase
4
+ context "Validations" do
5
+ context "on a Document" do
6
+ setup do
7
+ @document = Doc()
8
+ end
9
+
10
+ context "Validating acceptance of" do
11
+ should "work with validates_acceptance_of macro" do
12
+ @document.key :terms, String
13
+ @document.validates_acceptance_of :terms
14
+ doc = @document.new(:terms => '')
15
+ doc.should have_error_on(:terms)
16
+ doc.terms = '1'
17
+ doc.should_not have_error_on(:terms)
18
+ end
19
+ end
20
+
21
+ context "validating confirmation of" do
22
+ should "work with validates_confirmation_of macro" do
23
+ @document.key :password, String
24
+ @document.validates_confirmation_of :password
25
+ doc = @document.new
26
+ doc.password = 'foobar'
27
+ doc.should have_error_on(:password)
28
+ doc.password_confirmation = 'foobar'
29
+ doc.should_not have_error_on(:password)
30
+ end
31
+ end
32
+
33
+ context "validating format of" do
34
+ should "work with validates_format_of macro" do
35
+ @document.key :name, String
36
+ @document.validates_format_of :name, :with => /.+/
37
+ doc = @document.new
38
+ doc.should have_error_on(:name)
39
+ doc.name = 'John'
40
+ doc.should_not have_error_on(:name)
41
+ end
42
+
43
+ should "work with :format shorcut key" do
44
+ @document.key :name, String, :format => /.+/
45
+ doc = @document.new
46
+ doc.should have_error_on(:name)
47
+ doc.name = 'John'
48
+ doc.should_not have_error_on(:name)
49
+ end
50
+ end
51
+
52
+ context "validating length of" do
53
+ should "work with validates_length_of macro" do
54
+ @document.key :name, String
55
+ @document.validates_length_of :name, :minimum => 5
56
+ doc = @document.new
57
+ doc.should have_error_on(:name)
58
+ end
59
+
60
+ context "with :length => integer shortcut" do
61
+ should "set maximum of integer provided" do
62
+ @document.key :name, String, :length => 5
63
+ doc = @document.new
64
+ doc.name = '123456'
65
+ doc.should have_error_on(:name)
66
+ doc.name = '12345'
67
+ doc.should_not have_error_on(:name)
68
+ end
69
+ end
70
+
71
+ context "with :length => range shortcut" do
72
+ setup do
73
+ @document.key :name, String, :length => 5..7
74
+ end
75
+
76
+ should "set minimum of range min" do
77
+ doc = @document.new
78
+ doc.should have_error_on(:name)
79
+ doc.name = '123456'
80
+ doc.should_not have_error_on(:name)
81
+ end
82
+
83
+ should "set maximum of range max" do
84
+ doc = @document.new
85
+ doc.should have_error_on(:name)
86
+ doc.name = '12345678'
87
+ doc.should have_error_on(:name)
88
+ doc.name = '123456'
89
+ doc.should_not have_error_on(:name)
90
+ end
91
+ end
92
+
93
+ context "with :length => hash shortcut" do
94
+ should "pass options through" do
95
+ @document.key :name, String, :length => {:minimum => 2}
96
+ doc = @document.new
97
+ doc.should have_error_on(:name)
98
+ doc.name = '12'
99
+ doc.should_not have_error_on(:name)
100
+ end
101
+ end
102
+ end # validates_length_of
103
+
104
+ context "Validating numericality of" do
105
+ should "work with validates_numericality_of macro" do
106
+ @document.key :age, Integer
107
+ @document.validates_numericality_of :age
108
+ doc = @document.new
109
+ doc.age = 'String'
110
+ doc.should have_error_on(:age)
111
+ doc.age = 23
112
+ doc.should_not have_error_on(:age)
113
+ end
114
+
115
+ context "with :numeric shortcut" do
116
+ should "work with integer or float" do
117
+ @document.key :weight, Float, :numeric => true
118
+ doc = @document.new
119
+ doc.weight = 'String'
120
+ doc.should have_error_on(:weight)
121
+ doc.weight = 23.0
122
+ doc.should_not have_error_on(:weight)
123
+ doc.weight = 23
124
+ doc.should_not have_error_on(:weight)
125
+ end
126
+ end
127
+
128
+ context "with :numeric shortcut on Integer key" do
129
+ should "only work with integers" do
130
+ @document.key :age, Integer, :numeric => true
131
+ doc = @document.new
132
+ doc.age = 'String'
133
+ doc.should have_error_on(:age)
134
+ doc.age = 23.1
135
+ doc.should have_error_on(:age)
136
+ doc.age = 23
137
+ doc.should_not have_error_on(:age)
138
+ end
139
+ end
140
+ end # numericality of
141
+
142
+ context "validating presence of" do
143
+ should "work with validates_presence_of macro" do
144
+ @document.key :name, String
145
+ @document.validates_presence_of :name
146
+ doc = @document.new
147
+ doc.should have_error_on(:name)
148
+ end
149
+
150
+ should "work with :required shortcut on key definition" do
151
+ @document.key :name, String, :required => true
152
+ doc = @document.new
153
+ doc.should have_error_on(:name)
154
+ end
155
+ end
156
+
157
+ context "validating exclusion of" do
158
+ should "throw error if enumerator not provided" do
159
+ @document.key :action, String
160
+ lambda {
161
+ @document.validates_exclusion_of :action
162
+ }.should raise_error(ArgumentError)
163
+ end
164
+
165
+ should "work with validates_exclusion_of macro" do
166
+ @document.key :action, String
167
+ @document.validates_exclusion_of :action, :within => %w(kick run)
168
+
169
+ doc = @document.new
170
+ doc.should_not have_error_on(:action)
171
+
172
+ doc.action = 'fart'
173
+ doc.should_not have_error_on(:action)
174
+
175
+ doc.action = 'kick'
176
+ doc.should have_error_on(:action, 'is reserved')
177
+ end
178
+
179
+ should "work with :not_in shortcut on key definition" do
180
+ @document.key :action, String, :not_in => %w(kick run)
181
+
182
+ doc = @document.new
183
+ doc.should_not have_error_on(:action)
184
+
185
+ doc.action = 'fart'
186
+ doc.should_not have_error_on(:action)
187
+
188
+ doc.action = 'kick'
189
+ doc.should have_error_on(:action, 'is reserved')
190
+ end
191
+
192
+ should "not have error if allow nil is true and value is nil" do
193
+ @document.key :action, String
194
+ @document.validates_exclusion_of :action, :within => %w(kick run), :allow_nil => true
195
+
196
+ doc = @document.new
197
+ doc.should_not have_error_on(:action)
198
+ end
199
+
200
+ should "not have error if allow blank is true and value is blank" do
201
+ @document.key :action, String
202
+ @document.validates_exclusion_of :action, :within => %w(kick run), :allow_nil => true
203
+
204
+ doc = @document.new(:action => '')
205
+ doc.should_not have_error_on(:action)
206
+ end
207
+ end
208
+
209
+ context "validating inclusion of" do
210
+ should "throw error if enumerator not provided" do
211
+ @document.key :action, String
212
+ lambda {
213
+ @document.validates_inclusion_of :action
214
+ }.should raise_error(ArgumentError)
215
+ end
216
+
217
+ should "work with validates_inclusion_of macro" do
218
+ @document.key :action, String
219
+ @document.validates_inclusion_of :action, :within => %w(kick run)
220
+
221
+ doc = @document.new
222
+ doc.should have_error_on(:action, 'is not in the list')
223
+
224
+ doc.action = 'fart'
225
+ doc.should have_error_on(:action, 'is not in the list')
226
+
227
+ doc.action = 'kick'
228
+ doc.should_not have_error_on(:action)
229
+ end
230
+
231
+ should "work with :in shortcut on key definition" do
232
+ @document.key :action, String, :in => %w(kick run)
233
+
234
+ doc = @document.new
235
+ doc.should have_error_on(:action, 'is not in the list')
236
+
237
+ doc.action = 'fart'
238
+ doc.should have_error_on(:action, 'is not in the list')
239
+
240
+ doc.action = 'kick'
241
+ doc.should_not have_error_on(:action)
242
+ end
243
+
244
+ should "not have error if allow nil is true and value is nil" do
245
+ @document.key :action, String
246
+ @document.validates_inclusion_of :action, :within => %w(kick run), :allow_nil => true
247
+
248
+ doc = @document.new
249
+ doc.should_not have_error_on(:action)
250
+ end
251
+
252
+ should "not have error if allow blank is true and value is blank" do
253
+ @document.key :action, String
254
+ @document.validates_inclusion_of :action, :within => %w(kick run), :allow_blank => true
255
+
256
+ doc = @document.new(:action => '')
257
+ doc.should_not have_error_on(:action)
258
+ end
259
+ end
260
+
261
+ end # End on a Document
262
+
263
+ context "On an EmbeddedDocument" do
264
+ setup do
265
+ @embedded_doc = EDoc()
266
+ end
267
+
268
+ context "Validating acceptance of" do
269
+ should "work with validates_acceptance_of macro" do
270
+ @embedded_doc.key :terms, String
271
+ @embedded_doc.validates_acceptance_of :terms
272
+ doc = @embedded_doc.new(:terms => '')
273
+ doc.should have_error_on(:terms)
274
+ doc.terms = '1'
275
+ doc.should_not have_error_on(:terms)
276
+ end
277
+ end
278
+
279
+ context "validating confirmation of" do
280
+ should "work with validates_confirmation_of macro" do
281
+ @embedded_doc.key :password, String
282
+ @embedded_doc.validates_confirmation_of :password
283
+ doc = @embedded_doc.new
284
+ doc.password = 'foobar'
285
+ doc.should have_error_on(:password)
286
+ doc.password_confirmation = 'foobar'
287
+ doc.should_not have_error_on(:password)
288
+ end
289
+ end
290
+
291
+ context "validating format of" do
292
+ should "work with validates_format_of macro" do
293
+ @embedded_doc.key :name, String
294
+ @embedded_doc.validates_format_of :name, :with => /.+/
295
+ doc = @embedded_doc.new
296
+ doc.should have_error_on(:name)
297
+ doc.name = 'John'
298
+ doc.should_not have_error_on(:name)
299
+ end
300
+
301
+ should "work with :format shorcut key" do
302
+ @embedded_doc.key :name, String, :format => /.+/
303
+ doc = @embedded_doc.new
304
+ doc.should have_error_on(:name)
305
+ doc.name = 'John'
306
+ doc.should_not have_error_on(:name)
307
+ end
308
+ end
309
+
310
+ context "validating length of" do
311
+ should "work with validates_length_of macro" do
312
+ @embedded_doc.key :name, String
313
+ @embedded_doc.validates_length_of :name, :minimum => 5
314
+ doc = @embedded_doc.new
315
+ doc.should have_error_on(:name)
316
+ end
317
+
318
+ context "with :length => integer shortcut" do
319
+ should "set maximum of integer provided" do
320
+ @embedded_doc.key :name, String, :length => 5
321
+ doc = @embedded_doc.new
322
+ doc.name = '123456'
323
+ doc.should have_error_on(:name)
324
+ doc.name = '12345'
325
+ doc.should_not have_error_on(:name)
326
+ end
327
+ end
328
+
329
+ context "with :length => range shortcut" do
330
+ setup do
331
+ @embedded_doc.key :name, String, :length => 5..7
332
+ end
333
+
334
+ should "set minimum of range min" do
335
+ doc = @embedded_doc.new
336
+ doc.should have_error_on(:name)
337
+ doc.name = '123456'
338
+ doc.should_not have_error_on(:name)
339
+ end
340
+
341
+ should "set maximum of range max" do
342
+ doc = @embedded_doc.new
343
+ doc.should have_error_on(:name)
344
+ doc.name = '12345678'
345
+ doc.should have_error_on(:name)
346
+ doc.name = '123456'
347
+ doc.should_not have_error_on(:name)
348
+ end
349
+ end
350
+
351
+ context "with :length => hash shortcut" do
352
+ should "pass options through" do
353
+ @embedded_doc.key :name, String, :length => {:minimum => 2}
354
+ doc = @embedded_doc.new
355
+ doc.should have_error_on(:name)
356
+ doc.name = '12'
357
+ doc.should_not have_error_on(:name)
358
+ end
359
+ end
360
+ end # validates_length_of
361
+
362
+ context "Validating numericality of" do
363
+ should "work with validates_numericality_of macro" do
364
+ @embedded_doc.key :age, Integer
365
+ @embedded_doc.validates_numericality_of :age
366
+ doc = @embedded_doc.new
367
+ doc.age = 'String'
368
+ doc.should have_error_on(:age)
369
+ doc.age = 23
370
+ doc.should_not have_error_on(:age)
371
+ end
372
+
373
+ context "with :numeric shortcut" do
374
+ should "work with integer or float" do
375
+ @embedded_doc.key :weight, Float, :numeric => true
376
+ doc = @embedded_doc.new
377
+ doc.weight = 'String'
378
+ doc.should have_error_on(:weight)
379
+ doc.weight = 23.0
380
+ doc.should_not have_error_on(:weight)
381
+ doc.weight = 23
382
+ doc.should_not have_error_on(:weight)
383
+ end
384
+ end
385
+
386
+ context "with :numeric shortcut on Integer key" do
387
+ should "only work with integers" do
388
+ @embedded_doc.key :age, Integer, :numeric => true
389
+ doc = @embedded_doc.new
390
+ doc.age = 'String'
391
+ doc.should have_error_on(:age)
392
+ doc.age = 23.1
393
+ doc.should have_error_on(:age)
394
+ doc.age = 23
395
+ doc.should_not have_error_on(:age)
396
+ end
397
+ end
398
+ end # numericality of
399
+
400
+ context "validating presence of" do
401
+ should "work with validates_presence_of macro" do
402
+ @embedded_doc.key :name, String
403
+ @embedded_doc.validates_presence_of :name
404
+ doc = @embedded_doc.new
405
+ doc.should have_error_on(:name)
406
+ end
407
+
408
+ should "work with :required shortcut on key definition" do
409
+ @embedded_doc.key :name, String, :required => true
410
+ doc = @embedded_doc.new
411
+ doc.should have_error_on(:name)
412
+ end
413
+ end
414
+
415
+ context "validating exclusion of" do
416
+ should "throw error if enumerator not provided" do
417
+ @embedded_doc.key :action, String
418
+ lambda {
419
+ @embedded_doc.validates_exclusion_of :action
420
+ }.should raise_error(ArgumentError)
421
+ end
422
+
423
+ should "work with validates_exclusion_of macro" do
424
+ @embedded_doc.key :action, String
425
+ @embedded_doc.validates_exclusion_of :action, :within => %w(kick run)
426
+
427
+ doc = @embedded_doc.new
428
+ doc.should_not have_error_on(:action)
429
+
430
+ doc.action = 'fart'
431
+ doc.should_not have_error_on(:action)
432
+
433
+ doc.action = 'kick'
434
+ doc.should have_error_on(:action, 'is reserved')
435
+ end
436
+
437
+ should "work with :not_in shortcut on key definition" do
438
+ @embedded_doc.key :action, String, :not_in => %w(kick run)
439
+
440
+ doc = @embedded_doc.new
441
+ doc.should_not have_error_on(:action)
442
+
443
+ doc.action = 'fart'
444
+ doc.should_not have_error_on(:action)
445
+
446
+ doc.action = 'kick'
447
+ doc.should have_error_on(:action, 'is reserved')
448
+ end
449
+
450
+ should "not have error if allow nil is true and value is nil" do
451
+ @embedded_doc.key :action, String
452
+ @embedded_doc.validates_exclusion_of :action, :within => %w(kick run), :allow_nil => true
453
+
454
+ doc = @embedded_doc.new
455
+ doc.should_not have_error_on(:action)
456
+ end
457
+
458
+ should "not have error if allow blank is true and value is blank" do
459
+ @embedded_doc.key :action, String
460
+ @embedded_doc.validates_exclusion_of :action, :within => %w(kick run), :allow_nil => true
461
+
462
+ doc = @embedded_doc.new(:action => '')
463
+ doc.should_not have_error_on(:action)
464
+ end
465
+ end
466
+
467
+ context "validating inclusion of" do
468
+ should "throw error if enumerator not provided" do
469
+ @embedded_doc.key :action, String
470
+ lambda {
471
+ @embedded_doc.validates_inclusion_of :action
472
+ }.should raise_error(ArgumentError)
473
+ end
474
+
475
+ should "work with validates_inclusion_of macro" do
476
+ @embedded_doc.key :action, String
477
+ @embedded_doc.validates_inclusion_of :action, :within => %w(kick run)
478
+
479
+ doc = @embedded_doc.new
480
+ doc.should have_error_on(:action, 'is not in the list')
481
+
482
+ doc.action = 'fart'
483
+ doc.should have_error_on(:action, 'is not in the list')
484
+
485
+ doc.action = 'kick'
486
+ doc.should_not have_error_on(:action)
487
+ end
488
+
489
+ should "work with :in shortcut on key definition" do
490
+ @embedded_doc.key :action, String, :in => %w(kick run)
491
+
492
+ doc = @embedded_doc.new
493
+ doc.should have_error_on(:action, 'is not in the list')
494
+
495
+ doc.action = 'fart'
496
+ doc.should have_error_on(:action, 'is not in the list')
497
+
498
+ doc.action = 'kick'
499
+ doc.should_not have_error_on(:action)
500
+ end
501
+
502
+ should "not have error if allow nil is true and value is nil" do
503
+ @embedded_doc.key :action, String
504
+ @embedded_doc.validates_inclusion_of :action, :within => %w(kick run), :allow_nil => true
505
+
506
+ doc = @embedded_doc.new
507
+ doc.should_not have_error_on(:action)
508
+ end
509
+
510
+ should "not have error if allow blank is true and value is blank" do
511
+ @embedded_doc.key :action, String
512
+ @embedded_doc.validates_inclusion_of :action, :within => %w(kick run), :allow_blank => true
513
+
514
+ doc = @embedded_doc.new(:action => '')
515
+ doc.should_not have_error_on(:action)
516
+ end
517
+ end
518
+
519
+ end # End on an EmbeddedDocument
520
+
521
+ end # Validations
522
+
523
+ context "Adding validation errors" do
524
+ setup do
525
+ @document = Doc do
526
+ key :action, String
527
+ def action_present
528
+ errors.add(:action, 'is invalid') if action.blank?
529
+ end
530
+ end
531
+ end
532
+
533
+ should "work with validate callback" do
534
+ @document.validate :action_present
535
+
536
+ doc = @document.new
537
+ doc.action = nil
538
+ doc.should have_error_on(:action)
539
+
540
+ doc.action = 'kick'
541
+ doc.should_not have_error_on(:action)
542
+ end
543
+ end
544
+ end