fcoury-mongomapper 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (42) hide show
  1. data/.gitignore +7 -0
  2. data/History +30 -0
  3. data/LICENSE +20 -0
  4. data/README.rdoc +39 -0
  5. data/Rakefile +71 -0
  6. data/VERSION +1 -0
  7. data/lib/mongomapper.rb +70 -0
  8. data/lib/mongomapper/associations.rb +69 -0
  9. data/lib/mongomapper/associations/array_proxy.rb +6 -0
  10. data/lib/mongomapper/associations/base.rb +54 -0
  11. data/lib/mongomapper/associations/belongs_to_proxy.rb +26 -0
  12. data/lib/mongomapper/associations/has_many_embedded_proxy.rb +19 -0
  13. data/lib/mongomapper/associations/has_many_proxy.rb +29 -0
  14. data/lib/mongomapper/associations/polymorphic_belongs_to_proxy.rb +31 -0
  15. data/lib/mongomapper/associations/proxy.rb +66 -0
  16. data/lib/mongomapper/callbacks.rb +106 -0
  17. data/lib/mongomapper/document.rb +276 -0
  18. data/lib/mongomapper/document_rails_compatibility.rb +13 -0
  19. data/lib/mongomapper/embedded_document.rb +248 -0
  20. data/lib/mongomapper/embedded_document_rails_compatibility.rb +22 -0
  21. data/lib/mongomapper/finder_options.rb +81 -0
  22. data/lib/mongomapper/key.rb +82 -0
  23. data/lib/mongomapper/observing.rb +50 -0
  24. data/lib/mongomapper/save_with_validation.rb +19 -0
  25. data/lib/mongomapper/serialization.rb +55 -0
  26. data/lib/mongomapper/serializers/json_serializer.rb +77 -0
  27. data/lib/mongomapper/validations.rb +47 -0
  28. data/mongomapper.gemspec +105 -0
  29. data/test/serializers/test_json_serializer.rb +104 -0
  30. data/test/test_associations.rb +444 -0
  31. data/test/test_callbacks.rb +84 -0
  32. data/test/test_document.rb +1002 -0
  33. data/test/test_embedded_document.rb +253 -0
  34. data/test/test_finder_options.rb +148 -0
  35. data/test/test_helper.rb +62 -0
  36. data/test/test_key.rb +200 -0
  37. data/test/test_mongomapper.rb +28 -0
  38. data/test/test_observing.rb +101 -0
  39. data/test/test_rails_compatibility.rb +73 -0
  40. data/test/test_serializations.rb +54 -0
  41. data/test/test_validations.rb +409 -0
  42. metadata +155 -0
@@ -0,0 +1,409 @@
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 uniqueness of" do
159
+ setup do
160
+ @document.key :name, String
161
+ @document.validates_uniqueness_of :name
162
+ end
163
+
164
+ should "not fail if object is new" do
165
+ doc = @document.new
166
+ doc.should_not have_error_on(:name)
167
+ end
168
+
169
+ should "allow to update an object" do
170
+ doc = @document.new("name" => "joe")
171
+ doc.save
172
+
173
+ @document \
174
+ .stubs(:find) \
175
+ .with(:first, :conditions => {:name => 'joe'}, :limit => 1) \
176
+ .returns(doc)
177
+
178
+ doc.name = "joe"
179
+ doc.valid?.should be_true
180
+ doc.should_not have_error_on(:name)
181
+ end
182
+
183
+ should "fail if object name is not unique" do
184
+ doc = @document.new("name" => "joe")
185
+ doc.save.should be_true
186
+
187
+ @document \
188
+ .stubs(:find) \
189
+ .with(:first, :conditions => {:name => 'joe'}, :limit => 1) \
190
+ .returns(doc)
191
+
192
+ doc2 = @document.new("name" => "joe")
193
+ doc2.should have_error_on(:name)
194
+ end
195
+ end
196
+
197
+ context "validates uniqueness of with :unique shortcut" do
198
+ should "work" do
199
+ @document.key :name, String, :unique => true
200
+
201
+ doc = @document.create(:name => 'John')
202
+ doc.should_not have_error_on(:name)
203
+
204
+ @document \
205
+ .stubs(:find) \
206
+ .with(:first, :conditions => {:name => 'John'}, :limit => 1) \
207
+ .returns(doc)
208
+
209
+ second_john = @document.create(:name => 'John')
210
+ second_john.should have_error_on(:name, 'has already been taken')
211
+ end
212
+ end
213
+
214
+ context "validating exclusion of" do
215
+ should "throw error if enumerator not provided" do
216
+ @document.key :action, String
217
+ lambda {
218
+ @document.validates_exclusion_of :action
219
+ }.should raise_error(ArgumentError)
220
+ end
221
+
222
+ should "work with validates_exclusion_of macro" do
223
+ @document.key :action, String
224
+ @document.validates_exclusion_of :action, :within => %w(kick run)
225
+
226
+ doc = @document.new
227
+ doc.should_not have_error_on(:action)
228
+
229
+ doc.action = 'fart'
230
+ doc.should_not have_error_on(:action)
231
+
232
+ doc.action = 'kick'
233
+ doc.should have_error_on(:action, 'is reserved')
234
+ end
235
+
236
+ should "not have error if allow nil is true and value is nil" do
237
+ @document.key :action, String
238
+ @document.validates_exclusion_of :action, :within => %w(kick run), :allow_nil => true
239
+
240
+ doc = @document.new
241
+ doc.should_not have_error_on(:action)
242
+ end
243
+
244
+ should "not have error if allow blank is true and value is blank" do
245
+ @document.key :action, String
246
+ @document.validates_exclusion_of :action, :within => %w(kick run), :allow_nil => true
247
+
248
+ doc = @document.new(:action => '')
249
+ doc.should_not have_error_on(:action)
250
+ end
251
+ end
252
+
253
+ context "validating inclusion of" do
254
+ should "throw error if enumerator not provided" do
255
+ @document.key :action, String
256
+ lambda {
257
+ @document.validates_inclusion_of :action
258
+ }.should raise_error(ArgumentError)
259
+ end
260
+
261
+ should "work with validates_inclusion_of macro" do
262
+ @document.key :action, String
263
+ @document.validates_inclusion_of :action, :within => %w(kick run)
264
+
265
+ doc = @document.new
266
+ doc.should have_error_on(:action, 'is not in the list')
267
+
268
+ doc.action = 'fart'
269
+ doc.should have_error_on(:action, 'is not in the list')
270
+
271
+ doc.action = 'kick'
272
+ doc.should_not have_error_on(:action)
273
+ end
274
+
275
+ should "not have error if allow nil is true and value is nil" do
276
+ @document.key :action, String
277
+ @document.validates_inclusion_of :action, :within => %w(kick run), :allow_nil => true
278
+
279
+ doc = @document.new
280
+ doc.should_not have_error_on(:action)
281
+ end
282
+
283
+ should "not have error if allow blank is true and value is blank" do
284
+ @document.key :action, String
285
+ @document.validates_inclusion_of :action, :within => %w(kick run), :allow_blank => true
286
+
287
+ doc = @document.new(:action => '')
288
+ doc.should_not have_error_on(:action)
289
+ end
290
+ end
291
+ end # Validations
292
+
293
+ context "Saving a new document that is invalid" do
294
+ setup do
295
+ @document = Class.new do
296
+ include MongoMapper::Document
297
+ key :name, String, :required => true
298
+ end
299
+
300
+ @document.collection.clear
301
+ end
302
+
303
+ should "not insert document" do
304
+ doc = @document.new
305
+ doc.save
306
+ @document.count.should == 0
307
+ end
308
+
309
+ should "populate document's errors" do
310
+ doc = @document.new
311
+ doc.errors.size.should == 0
312
+ doc.save
313
+ doc.errors.full_messages.should == ["Name can't be empty"]
314
+ end
315
+ end
316
+
317
+ context "Saving a document that is invalid (destructive)" do
318
+ setup do
319
+ @document = Class.new do
320
+ include MongoMapper::Document
321
+ key :name, String, :required => true
322
+ end
323
+
324
+ @document.collection.clear
325
+ end
326
+
327
+ should "raise error" do
328
+ doc = @document.new
329
+ lambda { doc.save! }.should raise_error(MongoMapper::DocumentNotValid)
330
+ end
331
+ end
332
+
333
+ context "Saving an existing document that is invalid" do
334
+ setup do
335
+ @document = Class.new do
336
+ include MongoMapper::Document
337
+ key :name, String, :required => true
338
+ end
339
+
340
+ @document.collection.clear
341
+ @doc = @document.create(:name => 'John Nunemaker')
342
+ end
343
+
344
+ should "not update document" do
345
+ @doc.name = nil
346
+ @doc.save
347
+ @document.find(@doc.id).name.should == 'John Nunemaker'
348
+ end
349
+
350
+ should "populate document's errors" do
351
+ @doc.name = nil
352
+ @doc.save
353
+ @doc.errors.full_messages.should == ["Name can't be empty"]
354
+ end
355
+ end
356
+
357
+ context "Adding validation errors" do
358
+ setup do
359
+ @document = Class.new do
360
+ include MongoMapper::Document
361
+ key :action, String
362
+ def action_present
363
+ errors.add(:action, 'is invalid') if action.blank?
364
+ end
365
+ end
366
+ end
367
+
368
+ should "work with validate callback" do
369
+ @document.validate :action_present
370
+
371
+ doc = @document.new
372
+ doc.action = nil
373
+ doc.should have_error_on(:action)
374
+
375
+ doc.action = 'kick'
376
+ doc.should_not have_error_on(:action)
377
+ end
378
+
379
+ should "work with validate_on_create callback" do
380
+ @document.validate_on_create :action_present
381
+
382
+ doc = @document.new
383
+ doc.action = nil
384
+ doc.should have_error_on(:action)
385
+
386
+ doc.action = 'kick'
387
+ doc.should_not have_error_on(:action)
388
+ doc.save
389
+
390
+ doc.action = nil
391
+ doc.should_not have_error_on(:action)
392
+ end
393
+
394
+ should "work with validate_on_update callback" do
395
+ @document.validate_on_update :action_present
396
+
397
+ doc = @document.new
398
+ doc.action = nil
399
+ doc.should_not have_error_on(:action)
400
+ doc.save
401
+
402
+ doc.action = nil
403
+ doc.should have_error_on(:action)
404
+
405
+ doc.action = 'kick'
406
+ doc.should_not have_error_on(:action)
407
+ end
408
+ end
409
+ end
metadata ADDED
@@ -0,0 +1,155 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fcoury-mongomapper
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ platform: ruby
6
+ authors:
7
+ - John Nunemaker
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-07-07 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: activesupport
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0"
24
+ version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: mongodb-mongo
27
+ type: :runtime
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "="
32
+ - !ruby/object:Gem::Version
33
+ version: "0.9"
34
+ version:
35
+ - !ruby/object:Gem::Dependency
36
+ name: jnunemaker-validatable
37
+ type: :runtime
38
+ version_requirement:
39
+ version_requirements: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - "="
42
+ - !ruby/object:Gem::Version
43
+ version: 1.7.1
44
+ version:
45
+ - !ruby/object:Gem::Dependency
46
+ name: mocha
47
+ type: :development
48
+ version_requirement:
49
+ version_requirements: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - "="
52
+ - !ruby/object:Gem::Version
53
+ version: 0.9.4
54
+ version:
55
+ - !ruby/object:Gem::Dependency
56
+ name: jnunemaker-matchy
57
+ type: :development
58
+ version_requirement:
59
+ version_requirements: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - "="
62
+ - !ruby/object:Gem::Version
63
+ version: 0.4.0
64
+ version:
65
+ description:
66
+ email: nunemaker@gmail.com
67
+ executables: []
68
+
69
+ extensions: []
70
+
71
+ extra_rdoc_files:
72
+ - LICENSE
73
+ - README.rdoc
74
+ files:
75
+ - .gitignore
76
+ - History
77
+ - LICENSE
78
+ - README.rdoc
79
+ - Rakefile
80
+ - VERSION
81
+ - lib/mongomapper.rb
82
+ - lib/mongomapper/associations.rb
83
+ - lib/mongomapper/associations/array_proxy.rb
84
+ - lib/mongomapper/associations/base.rb
85
+ - lib/mongomapper/associations/belongs_to_proxy.rb
86
+ - lib/mongomapper/associations/has_many_embedded_proxy.rb
87
+ - lib/mongomapper/associations/has_many_proxy.rb
88
+ - lib/mongomapper/associations/polymorphic_belongs_to_proxy.rb
89
+ - lib/mongomapper/associations/proxy.rb
90
+ - lib/mongomapper/callbacks.rb
91
+ - lib/mongomapper/document.rb
92
+ - lib/mongomapper/embedded_document.rb
93
+ - lib/mongomapper/finder_options.rb
94
+ - lib/mongomapper/key.rb
95
+ - lib/mongomapper/observing.rb
96
+ - lib/mongomapper/document_rails_compatibility.rb
97
+ - lib/mongomapper/embedded_document_rails_compatibility.rb
98
+ - lib/mongomapper/save_with_validation.rb
99
+ - lib/mongomapper/serialization.rb
100
+ - lib/mongomapper/serializers/json_serializer.rb
101
+ - lib/mongomapper/validations.rb
102
+ - mongomapper.gemspec
103
+ - test/serializers/test_json_serializer.rb
104
+ - test/test_associations.rb
105
+ - test/test_callbacks.rb
106
+ - test/test_document.rb
107
+ - test/test_embedded_document.rb
108
+ - test/test_finder_options.rb
109
+ - test/test_helper.rb
110
+ - test/test_key.rb
111
+ - test/test_mongomapper.rb
112
+ - test/test_observing.rb
113
+ - test/test_rails_compatibility.rb
114
+ - test/test_serializations.rb
115
+ - test/test_validations.rb
116
+ has_rdoc: true
117
+ homepage: http://github.com/jnunemaker/mongomapper
118
+ post_install_message:
119
+ rdoc_options:
120
+ - --charset=UTF-8
121
+ require_paths:
122
+ - lib
123
+ required_ruby_version: !ruby/object:Gem::Requirement
124
+ requirements:
125
+ - - ">="
126
+ - !ruby/object:Gem::Version
127
+ version: "0"
128
+ version:
129
+ required_rubygems_version: !ruby/object:Gem::Requirement
130
+ requirements:
131
+ - - ">="
132
+ - !ruby/object:Gem::Version
133
+ version: "0"
134
+ version:
135
+ requirements: []
136
+
137
+ rubyforge_project: mongomapper
138
+ rubygems_version: 1.2.0
139
+ signing_key:
140
+ specification_version: 2
141
+ summary: Awesome gem for modeling your domain and storing it in mongo
142
+ test_files:
143
+ - test/serializers/test_json_serializer.rb
144
+ - test/test_associations.rb
145
+ - test/test_callbacks.rb
146
+ - test/test_document.rb
147
+ - test/test_embedded_document.rb
148
+ - test/test_finder_options.rb
149
+ - test/test_helper.rb
150
+ - test/test_key.rb
151
+ - test/test_mongomapper.rb
152
+ - test/test_observing.rb
153
+ - test/test_rails_compatibility.rb
154
+ - test/test_serializations.rb
155
+ - test/test_validations.rb