durran-mongoid 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +5 -0
- data/MIT_LICENSE +20 -0
- data/README.textile +134 -0
- data/Rakefile +49 -0
- data/VERSION +1 -0
- data/lib/mongoid.rb +77 -0
- data/lib/mongoid/associations/association_factory.rb +21 -0
- data/lib/mongoid/associations/belongs_to_association.rb +23 -0
- data/lib/mongoid/associations/has_many_association.rb +25 -0
- data/lib/mongoid/associations/has_one_association.rb +26 -0
- data/lib/mongoid/document.rb +202 -0
- data/lib/mongoid/extensions.rb +7 -0
- data/lib/mongoid/extensions/array/conversions.rb +13 -0
- data/lib/mongoid/extensions/object/conversions.rb +13 -0
- data/lib/mongoid/paginator.rb +22 -0
- data/mongoid.gemspec +79 -0
- data/spec/integration/mongoid/document_spec.rb +95 -0
- data/spec/spec.opts +5 -0
- data/spec/spec_helper.rb +40 -0
- data/spec/unit/mongoid/associations/association_factory_spec.rb +48 -0
- data/spec/unit/mongoid/associations/belongs_to_association_spec.rb +35 -0
- data/spec/unit/mongoid/associations/has_many_association_spec.rb +105 -0
- data/spec/unit/mongoid/associations/has_one_association_spec.rb +35 -0
- data/spec/unit/mongoid/document_spec.rb +559 -0
- data/spec/unit/mongoid/extensions/array/conversions_spec.rb +14 -0
- data/spec/unit/mongoid/extensions/object/conversions_spec.rb +13 -0
- data/spec/unit/mongoid/paginator_spec.rb +74 -0
- metadata +108 -0
@@ -0,0 +1,35 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), "/../../../spec_helper.rb")
|
2
|
+
|
3
|
+
describe Mongoid::Associations::HasOneAssociation do
|
4
|
+
|
5
|
+
before do
|
6
|
+
@attributes = { :name => { :first_name => "Drexel" } }
|
7
|
+
@document = stub(:attributes => @attributes)
|
8
|
+
end
|
9
|
+
|
10
|
+
describe "#method_missing" do
|
11
|
+
|
12
|
+
before do
|
13
|
+
@association = Mongoid::Associations::HasOneAssociation.new(:name, @document)
|
14
|
+
end
|
15
|
+
|
16
|
+
context "when getting values" do
|
17
|
+
|
18
|
+
it "delegates to the document" do
|
19
|
+
@association.first_name.should == "Drexel"
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
23
|
+
|
24
|
+
context "when setting values" do
|
25
|
+
|
26
|
+
it "delegates to the document" do
|
27
|
+
@association.first_name = "Test"
|
28
|
+
@association.first_name.should == "Test"
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
34
|
+
|
35
|
+
end
|
@@ -0,0 +1,559 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), "/../../spec_helper.rb")
|
2
|
+
|
3
|
+
describe Mongoid::Document do
|
4
|
+
|
5
|
+
before do
|
6
|
+
@collection = mock
|
7
|
+
@database = stub(:collection => @collection)
|
8
|
+
Mongoid.stubs(:database).returns(@database)
|
9
|
+
end
|
10
|
+
|
11
|
+
after do
|
12
|
+
Person.instance_variable_set(:@collection, nil)
|
13
|
+
end
|
14
|
+
|
15
|
+
describe "#belongs_to" do
|
16
|
+
|
17
|
+
it "adds a new Association to the collection" do
|
18
|
+
address = Address.new
|
19
|
+
address.person.should_not be_nil
|
20
|
+
end
|
21
|
+
|
22
|
+
it "creates a reader for the association" do
|
23
|
+
address = Address.new
|
24
|
+
address.should respond_to(:person)
|
25
|
+
end
|
26
|
+
|
27
|
+
it "creates a writer for the association" do
|
28
|
+
address = Address.new
|
29
|
+
address.should respond_to(:person=)
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
33
|
+
|
34
|
+
describe "#collection" do
|
35
|
+
|
36
|
+
it "sets the collection name to the class pluralized" do
|
37
|
+
@database.expects(:collection).with("people").returns(@collection)
|
38
|
+
Person.collection
|
39
|
+
end
|
40
|
+
|
41
|
+
end
|
42
|
+
|
43
|
+
describe "#create" do
|
44
|
+
|
45
|
+
context "with no attributes" do
|
46
|
+
|
47
|
+
it "creates a new saved document" do
|
48
|
+
@collection.expects(:save).with({})
|
49
|
+
person = Person.create
|
50
|
+
person.should_not be_nil
|
51
|
+
end
|
52
|
+
|
53
|
+
end
|
54
|
+
|
55
|
+
context "with attributes" do
|
56
|
+
|
57
|
+
it "creates a new saved document" do
|
58
|
+
@collection.expects(:save).with({:test => "test"})
|
59
|
+
person = Person.create(:test => "test")
|
60
|
+
person.should_not be_nil
|
61
|
+
end
|
62
|
+
|
63
|
+
end
|
64
|
+
|
65
|
+
end
|
66
|
+
|
67
|
+
describe "#destroy" do
|
68
|
+
|
69
|
+
context "when the Document is remove from the database" do
|
70
|
+
|
71
|
+
it "returns nil" do
|
72
|
+
id = Mongo::ObjectID.new
|
73
|
+
@collection.expects(:remove).with(:_id => id)
|
74
|
+
person = Person.new(:_id => id)
|
75
|
+
person.destroy.should be_nil
|
76
|
+
end
|
77
|
+
|
78
|
+
end
|
79
|
+
|
80
|
+
end
|
81
|
+
|
82
|
+
describe "#fields" do
|
83
|
+
|
84
|
+
before do
|
85
|
+
Person.fields([:testing])
|
86
|
+
end
|
87
|
+
|
88
|
+
it "adds a reader for the fields defined" do
|
89
|
+
@person = Person.new(:testing => "Test")
|
90
|
+
@person.testing.should == "Test"
|
91
|
+
end
|
92
|
+
|
93
|
+
it "adds a writer for the fields defined" do
|
94
|
+
@person = Person.new(:testing => "Test")
|
95
|
+
@person.testing = "Testy"
|
96
|
+
@person.testing.should == "Testy"
|
97
|
+
end
|
98
|
+
|
99
|
+
end
|
100
|
+
|
101
|
+
describe "#find" do
|
102
|
+
|
103
|
+
before do
|
104
|
+
@attributes = { :document_class => "Person" }
|
105
|
+
end
|
106
|
+
|
107
|
+
context "when an id is passed in" do
|
108
|
+
|
109
|
+
before do
|
110
|
+
@id = Mongo::ObjectID.new
|
111
|
+
end
|
112
|
+
|
113
|
+
it "delegates to find_first" do
|
114
|
+
@collection.expects(:find_one).with(Mongo::ObjectID.from_string(@id.to_s)).returns(@attributes)
|
115
|
+
Person.find(@id.to_s)
|
116
|
+
end
|
117
|
+
|
118
|
+
end
|
119
|
+
|
120
|
+
context "when finding first" do
|
121
|
+
|
122
|
+
it "delegates to find_first" do
|
123
|
+
@collection.expects(:find_one).with(:test => "Test").returns(@attributes)
|
124
|
+
Person.find(:first, :test => "Test")
|
125
|
+
end
|
126
|
+
|
127
|
+
end
|
128
|
+
|
129
|
+
context "when finding all" do
|
130
|
+
|
131
|
+
before do
|
132
|
+
@cursor = mock
|
133
|
+
@people = []
|
134
|
+
end
|
135
|
+
|
136
|
+
it "delegates to find_all" do
|
137
|
+
@collection.expects(:find).with(:test => "Test").returns(@cursor)
|
138
|
+
@cursor.expects(:collect).returns(@people)
|
139
|
+
Person.find(:all, :test => "Test")
|
140
|
+
end
|
141
|
+
|
142
|
+
end
|
143
|
+
|
144
|
+
end
|
145
|
+
|
146
|
+
describe "#find_first" do
|
147
|
+
|
148
|
+
before do
|
149
|
+
@attributes = { :document_class => "Person" }
|
150
|
+
end
|
151
|
+
|
152
|
+
context "when a selector is provided" do
|
153
|
+
|
154
|
+
it "finds the first document from the collection and instantiates it" do
|
155
|
+
@collection.expects(:find_one).with(:test => "Test").returns(@attributes)
|
156
|
+
Person.find_first(:test => "Test").attributes.should == @attributes
|
157
|
+
end
|
158
|
+
|
159
|
+
end
|
160
|
+
|
161
|
+
context "when a selector is not provided" do
|
162
|
+
|
163
|
+
it "finds the first document from the collection and instantiates it" do
|
164
|
+
@collection.expects(:find_one).with(nil).returns(@attributes)
|
165
|
+
Person.find_first.attributes.should == @attributes
|
166
|
+
end
|
167
|
+
|
168
|
+
end
|
169
|
+
|
170
|
+
end
|
171
|
+
|
172
|
+
describe "#find_all" do
|
173
|
+
|
174
|
+
before do
|
175
|
+
@cursor = mock
|
176
|
+
@people = []
|
177
|
+
end
|
178
|
+
|
179
|
+
context "when a selector is provided" do
|
180
|
+
|
181
|
+
it "finds from the collection and instantiate objects for each returned" do
|
182
|
+
@collection.expects(:find).with(:test => "Test").returns(@cursor)
|
183
|
+
@cursor.expects(:collect).returns(@people)
|
184
|
+
Person.find_all(:test => "Test")
|
185
|
+
end
|
186
|
+
|
187
|
+
end
|
188
|
+
|
189
|
+
context "when a selector is not provided" do
|
190
|
+
|
191
|
+
it "finds from the collection and instantiate objects for each returned" do
|
192
|
+
@collection.expects(:find).with(nil).returns(@cursor)
|
193
|
+
@cursor.expects(:collect).returns(@people)
|
194
|
+
Person.find_all
|
195
|
+
end
|
196
|
+
|
197
|
+
end
|
198
|
+
|
199
|
+
end
|
200
|
+
|
201
|
+
describe "#group_by" do
|
202
|
+
|
203
|
+
before do
|
204
|
+
@reduce = "function(obj, prev) { prev.group.push(obj); }"
|
205
|
+
end
|
206
|
+
|
207
|
+
it "returns documents grouped by the supplied fields" do
|
208
|
+
results = [{ "title" => "Sir", "group" => [{ "title" => "Sir", "age" => 30 }] }]
|
209
|
+
@collection.expects(:group).with([:title], {}, { :group => [] }, @reduce).returns(results)
|
210
|
+
grouped = Person.group_by([:title], {})
|
211
|
+
people = grouped.first["group"]
|
212
|
+
people.first.should be_a_kind_of(Person)
|
213
|
+
end
|
214
|
+
|
215
|
+
end
|
216
|
+
|
217
|
+
describe "#has_many" do
|
218
|
+
|
219
|
+
it "adds a new Association to the collection" do
|
220
|
+
person = Person.new
|
221
|
+
person.addresses.should_not be_nil
|
222
|
+
end
|
223
|
+
|
224
|
+
it "creates a reader for the association" do
|
225
|
+
person = Person.new
|
226
|
+
person.should respond_to(:addresses)
|
227
|
+
end
|
228
|
+
|
229
|
+
it "creates a writer for the association" do
|
230
|
+
person = Person.new
|
231
|
+
person.should respond_to(:addresses=)
|
232
|
+
end
|
233
|
+
|
234
|
+
context "when setting the association directly" do
|
235
|
+
|
236
|
+
before do
|
237
|
+
@attributes = { :title => "Sir",
|
238
|
+
:addresses => [
|
239
|
+
{ :street => "Street 1", :document_class => "Address" },
|
240
|
+
{ :street => "Street 2", :document_class => "Address" } ] }
|
241
|
+
@person = Person.new(@attributes)
|
242
|
+
end
|
243
|
+
|
244
|
+
it "sets the attributes for the association" do
|
245
|
+
address = Address.new(:street => "New Street", :document_class => "Address")
|
246
|
+
@person.addresses = [address]
|
247
|
+
@person.addresses.first.street.should == "New Street"
|
248
|
+
end
|
249
|
+
|
250
|
+
end
|
251
|
+
|
252
|
+
end
|
253
|
+
|
254
|
+
describe "#has_one" do
|
255
|
+
|
256
|
+
it "adds a new Association to the collection" do
|
257
|
+
person = Person.new
|
258
|
+
person.name.should_not be_nil
|
259
|
+
end
|
260
|
+
|
261
|
+
it "creates a reader for the association" do
|
262
|
+
person = Person.new
|
263
|
+
person.should respond_to(:name)
|
264
|
+
end
|
265
|
+
|
266
|
+
it "creates a writer for the association" do
|
267
|
+
person = Person.new
|
268
|
+
person.should respond_to(:name=)
|
269
|
+
end
|
270
|
+
|
271
|
+
context "when setting the association directly" do
|
272
|
+
|
273
|
+
before do
|
274
|
+
@attributes = { :title => "Sir",
|
275
|
+
:name => { :first_name => "Test" } }
|
276
|
+
@person = Person.new(@attributes)
|
277
|
+
end
|
278
|
+
|
279
|
+
it "sets the attributes for the association" do
|
280
|
+
name = Name.new(:first_name => "New Name", :document_class => "Name")
|
281
|
+
@person.name = name
|
282
|
+
@person.name.first_name.should == "New Name"
|
283
|
+
end
|
284
|
+
|
285
|
+
end
|
286
|
+
|
287
|
+
end
|
288
|
+
|
289
|
+
describe "#index" do
|
290
|
+
|
291
|
+
context "when unique options are not provided" do
|
292
|
+
|
293
|
+
it "delegates to collection with unique => false" do
|
294
|
+
@collection.expects(:create_index).with(:title, :unique => false)
|
295
|
+
Person.index :title
|
296
|
+
end
|
297
|
+
|
298
|
+
end
|
299
|
+
|
300
|
+
context "when unique option is provided" do
|
301
|
+
|
302
|
+
it "delegates to collection with unique option" do
|
303
|
+
@collection.expects(:create_index).with(:title, :unique => true)
|
304
|
+
Person.index :title, :unique => true
|
305
|
+
end
|
306
|
+
|
307
|
+
end
|
308
|
+
|
309
|
+
end
|
310
|
+
|
311
|
+
describe "#new" do
|
312
|
+
|
313
|
+
context "with no attributes" do
|
314
|
+
|
315
|
+
it "does not set any attributes" do
|
316
|
+
person = Person.new
|
317
|
+
person.attributes.empty?.should be_true
|
318
|
+
end
|
319
|
+
|
320
|
+
end
|
321
|
+
|
322
|
+
context "with attributes" do
|
323
|
+
|
324
|
+
before do
|
325
|
+
@attributes = { :test => "test" }
|
326
|
+
end
|
327
|
+
|
328
|
+
it "sets the arributes hash on the object" do
|
329
|
+
person = Person.new(@attributes)
|
330
|
+
person.attributes.should == @attributes
|
331
|
+
end
|
332
|
+
|
333
|
+
end
|
334
|
+
|
335
|
+
end
|
336
|
+
|
337
|
+
describe "#new_record?" do
|
338
|
+
|
339
|
+
context "when the object has been saved" do
|
340
|
+
|
341
|
+
before do
|
342
|
+
@person = Person.new(:_id => "1")
|
343
|
+
end
|
344
|
+
|
345
|
+
it "returns false" do
|
346
|
+
@person.new_record?.should be_false
|
347
|
+
end
|
348
|
+
|
349
|
+
end
|
350
|
+
|
351
|
+
context "when the object has not been saved" do
|
352
|
+
|
353
|
+
before do
|
354
|
+
@person = Person.new
|
355
|
+
end
|
356
|
+
|
357
|
+
it "returns true" do
|
358
|
+
@person.new_record?.should be_true
|
359
|
+
end
|
360
|
+
|
361
|
+
end
|
362
|
+
|
363
|
+
end
|
364
|
+
|
365
|
+
describe "#paginate" do
|
366
|
+
|
367
|
+
context "when pagination parameters are passed" do
|
368
|
+
|
369
|
+
it "delegates offset and limit to find_all" do
|
370
|
+
@collection.expects(:find).with({ :test => "Test" }, {:limit => 20, :offset => 20}).returns([])
|
371
|
+
Person.paginate({ :test => "Test" }, { :page => 2, :per_page => 20 })
|
372
|
+
end
|
373
|
+
|
374
|
+
end
|
375
|
+
|
376
|
+
context "when pagination parameters are not passed" do
|
377
|
+
|
378
|
+
it "passes the default offset and limit to find_all" do
|
379
|
+
@collection.expects(:find).with({ :test => "Test" }, {:limit => 20, :offset => 0}).returns([])
|
380
|
+
Person.paginate({ :test => "Test" })
|
381
|
+
end
|
382
|
+
|
383
|
+
end
|
384
|
+
|
385
|
+
end
|
386
|
+
|
387
|
+
describe "#parent" do
|
388
|
+
|
389
|
+
before do
|
390
|
+
@attributes = { :title => "Sir",
|
391
|
+
:addresses => [
|
392
|
+
{ :street => "Street 1", :document_class => "Address" },
|
393
|
+
{ :street => "Street 2", :document_class => "Address" } ] }
|
394
|
+
@person = Person.new(@attributes)
|
395
|
+
end
|
396
|
+
|
397
|
+
context "when document is embedded" do
|
398
|
+
|
399
|
+
it "returns the parent document" do
|
400
|
+
@person.addresses.first.parent.should == @person
|
401
|
+
end
|
402
|
+
|
403
|
+
end
|
404
|
+
|
405
|
+
context "when document is root" do
|
406
|
+
|
407
|
+
it "returns nil" do
|
408
|
+
@person.parent.should be_nil
|
409
|
+
end
|
410
|
+
|
411
|
+
end
|
412
|
+
|
413
|
+
end
|
414
|
+
|
415
|
+
describe "#save" do
|
416
|
+
|
417
|
+
context "when the document is the root" do
|
418
|
+
|
419
|
+
before do
|
420
|
+
@attributes = { :test => "test" }
|
421
|
+
@person = Person.new(@attributes)
|
422
|
+
end
|
423
|
+
|
424
|
+
it "persists the object to the MongoDB collection" do
|
425
|
+
@collection.expects(:save).with(@person.attributes)
|
426
|
+
@person.save.should be_true
|
427
|
+
end
|
428
|
+
|
429
|
+
end
|
430
|
+
|
431
|
+
context "when the document is embedded" do
|
432
|
+
|
433
|
+
before do
|
434
|
+
@attributes = { :title => "Sir",
|
435
|
+
:addresses => [
|
436
|
+
{ :street => "Street 1", :document_class => "Address" },
|
437
|
+
{ :street => "Street 2", :document_class => "Address" } ] }
|
438
|
+
@person = Person.new(@attributes)
|
439
|
+
end
|
440
|
+
|
441
|
+
it "saves the root document" do
|
442
|
+
@collection.expects(:save).with(@person.attributes)
|
443
|
+
@person.addresses.first.save
|
444
|
+
end
|
445
|
+
|
446
|
+
end
|
447
|
+
|
448
|
+
end
|
449
|
+
|
450
|
+
describe "#to_param" do
|
451
|
+
|
452
|
+
it "returns the id" do
|
453
|
+
id = Mongo::ObjectID.new
|
454
|
+
Person.new(:_id => id).to_param.should == id.to_s
|
455
|
+
end
|
456
|
+
|
457
|
+
end
|
458
|
+
|
459
|
+
describe "#update_attributes" do
|
460
|
+
|
461
|
+
context "when attributes are provided" do
|
462
|
+
|
463
|
+
it "saves and returns true" do
|
464
|
+
person = Person.new
|
465
|
+
person.expects(:save).returns(true)
|
466
|
+
person.update_attributes(:test => "Test").should be_true
|
467
|
+
end
|
468
|
+
|
469
|
+
end
|
470
|
+
|
471
|
+
end
|
472
|
+
|
473
|
+
context "validations" do
|
474
|
+
|
475
|
+
after do
|
476
|
+
Person.validations.clear
|
477
|
+
end
|
478
|
+
|
479
|
+
describe "#validates_acceptance_of" do
|
480
|
+
|
481
|
+
it "adds the acceptance validation" do
|
482
|
+
Person.class_eval do
|
483
|
+
validates_acceptance_of :terms
|
484
|
+
end
|
485
|
+
Person.validations.first.should be_a_kind_of(Validatable::ValidatesAcceptanceOf)
|
486
|
+
end
|
487
|
+
|
488
|
+
end
|
489
|
+
|
490
|
+
describe "#validates_associated" do
|
491
|
+
|
492
|
+
it "adds the associated validation" do
|
493
|
+
Person.class_eval do
|
494
|
+
validates_associated :name
|
495
|
+
end
|
496
|
+
Person.validations.first.should be_a_kind_of(Validatable::ValidatesAssociated)
|
497
|
+
end
|
498
|
+
|
499
|
+
end
|
500
|
+
|
501
|
+
describe "#validates_format_of" do
|
502
|
+
|
503
|
+
it "adds the format validation" do
|
504
|
+
Person.class_eval do
|
505
|
+
validates_format_of :title, :with => /[A-Za-z]/
|
506
|
+
end
|
507
|
+
Person.validations.first.should be_a_kind_of(Validatable::ValidatesFormatOf)
|
508
|
+
end
|
509
|
+
|
510
|
+
end
|
511
|
+
|
512
|
+
describe "#validates_length_of" do
|
513
|
+
|
514
|
+
it "adds the length validation" do
|
515
|
+
Person.class_eval do
|
516
|
+
validates_length_of :title, :minimum => 10
|
517
|
+
end
|
518
|
+
Person.validations.first.should be_a_kind_of(Validatable::ValidatesLengthOf)
|
519
|
+
end
|
520
|
+
|
521
|
+
end
|
522
|
+
|
523
|
+
describe "#validates_numericality_of" do
|
524
|
+
|
525
|
+
it "adds the numericality validation" do
|
526
|
+
Person.class_eval do
|
527
|
+
validates_numericality_of :age
|
528
|
+
end
|
529
|
+
Person.validations.first.should be_a_kind_of(Validatable::ValidatesNumericalityOf)
|
530
|
+
end
|
531
|
+
|
532
|
+
end
|
533
|
+
|
534
|
+
describe "#validates_presence_of" do
|
535
|
+
|
536
|
+
it "adds the presence validation" do
|
537
|
+
Person.class_eval do
|
538
|
+
validates_presence_of :title
|
539
|
+
end
|
540
|
+
Person.validations.first.should be_a_kind_of(Validatable::ValidatesPresenceOf)
|
541
|
+
end
|
542
|
+
|
543
|
+
end
|
544
|
+
|
545
|
+
describe "#validates_true_for" do
|
546
|
+
|
547
|
+
it "adds the true validation" do
|
548
|
+
Person.class_eval do
|
549
|
+
validates_true_for :title, :logic => lambda { title == "Esquire" }
|
550
|
+
end
|
551
|
+
Person.validations.first.should be_a_kind_of(Validatable::ValidatesTrueFor)
|
552
|
+
end
|
553
|
+
|
554
|
+
end
|
555
|
+
|
556
|
+
end
|
557
|
+
|
558
|
+
|
559
|
+
end
|