mongo_mapper-unstable 2009.11.8 → 2009.11.18
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.
- data/Rakefile +3 -3
- data/VERSION +1 -1
- data/bin/mmconsole +10 -5
- data/lib/mongo_mapper.rb +28 -5
- data/lib/mongo_mapper/associations/belongs_to_polymorphic_proxy.rb +1 -1
- data/lib/mongo_mapper/associations/belongs_to_proxy.rb +1 -1
- data/lib/mongo_mapper/associations/many_documents_as_proxy.rb +2 -2
- data/lib/mongo_mapper/associations/many_documents_proxy.rb +2 -2
- data/lib/mongo_mapper/associations/many_embedded_proxy.rb +2 -1
- data/lib/mongo_mapper/document.rb +3 -12
- data/lib/mongo_mapper/embedded_document.rb +37 -19
- data/lib/mongo_mapper/finder_options.rb +17 -11
- data/lib/mongo_mapper/rails_compatibility/document.rb +4 -0
- data/lib/mongo_mapper/rails_compatibility/embedded_document.rb +4 -0
- data/lib/mongo_mapper/serializers/json_serializer.rb +2 -2
- data/lib/mongo_mapper/support.rb +16 -44
- data/lib/mongo_mapper/types.rb +64 -0
- data/lib/mongo_mapper/validations.rb +1 -1
- data/mongo_mapper.gemspec +15 -12
- data/test/functional/associations/test_belongs_to_polymorphic_proxy.rb +10 -10
- data/test/functional/associations/test_belongs_to_proxy.rb +2 -1
- data/test/functional/associations/test_many_documents_as_proxy.rb +13 -12
- data/test/functional/associations/test_many_embedded_polymorphic_proxy.rb +34 -34
- data/test/functional/associations/test_many_embedded_proxy.rb +22 -22
- data/test/functional/associations/test_many_polymorphic_proxy.rb +10 -10
- data/test/functional/associations/test_many_proxy.rb +14 -15
- data/test/functional/test_associations.rb +4 -4
- data/test/functional/test_binary.rb +1 -1
- data/test/functional/test_dirty.rb +6 -6
- data/test/functional/test_document.rb +64 -65
- data/test/functional/test_embedded_document.rb +34 -14
- data/test/functional/test_string_id_compatibility.rb +72 -0
- data/test/functional/test_validations.rb +1 -1
- data/test/models.rb +24 -24
- data/test/unit/test_document.rb +7 -4
- data/test/unit/test_embedded_document.rb +47 -5
- data/test/unit/test_finder_options.rb +22 -3
- data/test/unit/test_mongo_mapper.rb +65 -0
- data/test/unit/test_rails_compatibility.rb +14 -0
- data/test/unit/test_support.rb +45 -0
- metadata +8 -5
- data/test/unit/test_mongomapper.rb +0 -28
@@ -26,10 +26,10 @@ class ManyEmbeddedProxyTest < Test::Unit::TestCase
|
|
26
26
|
project.addresses << chi
|
27
27
|
project.save
|
28
28
|
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
29
|
+
project = project.reload
|
30
|
+
project.addresses.size.should == 2
|
31
|
+
project.addresses[0].should == sb
|
32
|
+
project.addresses[1].should == chi
|
33
33
|
end
|
34
34
|
|
35
35
|
should "allow embedding arbitrarily deep" do
|
@@ -47,10 +47,10 @@ class ManyEmbeddedProxyTest < Test::Unit::TestCase
|
|
47
47
|
doc = @document.new(:person => meg)
|
48
48
|
doc.save
|
49
49
|
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
50
|
+
doc = doc.reload
|
51
|
+
doc.person.name.should == 'Meg'
|
52
|
+
doc.person.child.name.should == 'Steve'
|
53
|
+
doc.person.child.child.name.should == 'Linda'
|
54
54
|
end
|
55
55
|
|
56
56
|
should "allow assignment of 'many' embedded documents using a hash" do
|
@@ -70,12 +70,12 @@ class ManyEmbeddedProxyTest < Test::Unit::TestCase
|
|
70
70
|
pet_lover.pets[1].species.should == "Siberian Husky"
|
71
71
|
pet_lover.save.should be_true
|
72
72
|
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
73
|
+
pet_lover = pet_lover.reload
|
74
|
+
pet_lover.name.should == "Mr. Pet Lover"
|
75
|
+
pet_lover.pets[0].name.should == "Jimmy"
|
76
|
+
pet_lover.pets[0].species.should == "Cocker Spainel"
|
77
|
+
pet_lover.pets[1].name.should == "Sasha"
|
78
|
+
pet_lover.pets[1].species.should == "Siberian Husky"
|
79
79
|
end
|
80
80
|
|
81
81
|
context "embedding many embedded documents" do
|
@@ -101,13 +101,13 @@ class ManyEmbeddedProxyTest < Test::Unit::TestCase
|
|
101
101
|
doc.people << meg
|
102
102
|
doc.save
|
103
103
|
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
104
|
+
doc = doc.reload
|
105
|
+
doc.people.first.name.should == "Meg"
|
106
|
+
doc.people.first.pets.should_not == []
|
107
|
+
doc.people.first.pets.first.name.should == "Sparky"
|
108
|
+
doc.people.first.pets.first.species.should == "Dog"
|
109
|
+
doc.people.first.pets[1].name.should == "Koda"
|
110
|
+
doc.people.first.pets[1].species.should == "Dog"
|
111
111
|
end
|
112
112
|
|
113
113
|
should "create a reference to the root document for all embedded documents before save" do
|
@@ -139,7 +139,7 @@ class ManyEmbeddedProxyTest < Test::Unit::TestCase
|
|
139
139
|
should "allow finding by id" do
|
140
140
|
sparky = Pet.new(:name => "Sparky", :species => "Dog")
|
141
141
|
meg = Person.new(:name => "Meg", :pets => [sparky])
|
142
|
-
meg.pets.find(sparky.
|
142
|
+
meg.pets.find(sparky._id).should == sparky
|
143
143
|
end
|
144
144
|
|
145
145
|
context "extending the association" do
|
@@ -34,8 +34,8 @@ class ManyPolymorphicProxyTest < Test::Unit::TestCase
|
|
34
34
|
]
|
35
35
|
}.should change { Message.count }.by(3)
|
36
36
|
|
37
|
-
|
38
|
-
messages =
|
37
|
+
room = room.reload
|
38
|
+
messages = room.messages.all :order => "position"
|
39
39
|
messages.size.should == 3
|
40
40
|
messages[0].body.should == 'John entered room'
|
41
41
|
messages[1].body.should == 'Heyyyoooo!'
|
@@ -48,8 +48,8 @@ class ManyPolymorphicProxyTest < Test::Unit::TestCase
|
|
48
48
|
room.messages.push Exit.new(:body => 'John entered the room', :position => 2)
|
49
49
|
room.messages.concat Chat.new(:body => 'Holla!' , :position => 3)
|
50
50
|
|
51
|
-
|
52
|
-
messages =
|
51
|
+
room = room.reload
|
52
|
+
messages = room.messages.all :order => "position"
|
53
53
|
messages[0]._type.should == 'Enter'
|
54
54
|
messages[1]._type.should == 'Exit'
|
55
55
|
messages[2]._type.should == 'Chat'
|
@@ -59,7 +59,7 @@ class ManyPolymorphicProxyTest < Test::Unit::TestCase
|
|
59
59
|
should "assign foreign key" do
|
60
60
|
room = Room.create
|
61
61
|
message = room.messages.build
|
62
|
-
message.room_id.should == room.
|
62
|
+
message.room_id.should == room._id
|
63
63
|
end
|
64
64
|
|
65
65
|
should "assign _type" do
|
@@ -79,7 +79,7 @@ class ManyPolymorphicProxyTest < Test::Unit::TestCase
|
|
79
79
|
should "assign foreign key" do
|
80
80
|
room = Room.create
|
81
81
|
message = room.messages.create
|
82
|
-
message.room_id.should == room.
|
82
|
+
message.room_id.should == room._id
|
83
83
|
end
|
84
84
|
|
85
85
|
should "assign _type" do
|
@@ -254,12 +254,12 @@ class ManyPolymorphicProxyTest < Test::Unit::TestCase
|
|
254
254
|
|
255
255
|
context "with one id" do
|
256
256
|
should "work for id in association" do
|
257
|
-
@lounge.messages.find(@lm2.
|
257
|
+
@lounge.messages.find(@lm2._id).should == @lm2
|
258
258
|
end
|
259
259
|
|
260
260
|
should "not work for id not in association" do
|
261
261
|
lambda {
|
262
|
-
@lounge.messages.find!(@hm2.
|
262
|
+
@lounge.messages.find!(@hm2._id)
|
263
263
|
}.should raise_error(MongoMapper::DocumentNotFound)
|
264
264
|
end
|
265
265
|
end
|
@@ -280,13 +280,13 @@ class ManyPolymorphicProxyTest < Test::Unit::TestCase
|
|
280
280
|
|
281
281
|
context "with multiple ids" do
|
282
282
|
should "work for ids in association" do
|
283
|
-
messages = @lounge.messages.find(@lm1.
|
283
|
+
messages = @lounge.messages.find(@lm1._id, @lm2._id)
|
284
284
|
messages.should == [@lm1, @lm2]
|
285
285
|
end
|
286
286
|
|
287
287
|
should "not work for ids not in association" do
|
288
288
|
lambda {
|
289
|
-
@lounge.messages.find!(@lm1.
|
289
|
+
@lounge.messages.find!(@lm1._id, @lm2._id, @hm2._id)
|
290
290
|
}.should raise_error(MongoMapper::DocumentNotFound)
|
291
291
|
end
|
292
292
|
end
|
@@ -25,9 +25,9 @@ class ManyProxyTest < Test::Unit::TestCase
|
|
25
25
|
project.statuses = [Status.new("name" => "ready")]
|
26
26
|
project.save.should be_true
|
27
27
|
|
28
|
-
|
29
|
-
|
30
|
-
|
28
|
+
project = project.reload
|
29
|
+
project.statuses.size.should == 1
|
30
|
+
project.statuses[0].name.should == "ready"
|
31
31
|
end
|
32
32
|
|
33
33
|
should "correctly assign foreign key when using <<, push and concat" do
|
@@ -36,17 +36,17 @@ class ManyProxyTest < Test::Unit::TestCase
|
|
36
36
|
project.statuses.push Status.new(:name => 'push')
|
37
37
|
project.statuses.concat Status.new(:name => 'concat')
|
38
38
|
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
39
|
+
project = project.reload
|
40
|
+
project.statuses[0].project_id.should == project._id
|
41
|
+
project.statuses[1].project_id.should == project._id
|
42
|
+
project.statuses[2].project_id.should == project._id
|
43
43
|
end
|
44
44
|
|
45
45
|
context "build" do
|
46
46
|
should "assign foreign key" do
|
47
47
|
project = Project.create
|
48
48
|
status = project.statuses.build
|
49
|
-
status.project_id.should == project.
|
49
|
+
status.project_id.should == project._id
|
50
50
|
end
|
51
51
|
|
52
52
|
should "allow assigning attributes" do
|
@@ -60,7 +60,7 @@ class ManyProxyTest < Test::Unit::TestCase
|
|
60
60
|
should "assign foreign key" do
|
61
61
|
project = Project.create
|
62
62
|
status = project.statuses.create(:name => 'Foo!')
|
63
|
-
status.project_id.should == project.
|
63
|
+
status.project_id.should == project._id
|
64
64
|
end
|
65
65
|
|
66
66
|
should "save record" do
|
@@ -81,7 +81,7 @@ class ManyProxyTest < Test::Unit::TestCase
|
|
81
81
|
should "assign foreign key" do
|
82
82
|
project = Project.create
|
83
83
|
status = project.statuses.create!(:name => 'Foo!')
|
84
|
-
status.project_id.should == project.
|
84
|
+
status.project_id.should == project._id
|
85
85
|
end
|
86
86
|
|
87
87
|
should "save record" do
|
@@ -105,7 +105,6 @@ class ManyProxyTest < Test::Unit::TestCase
|
|
105
105
|
end
|
106
106
|
end
|
107
107
|
|
108
|
-
|
109
108
|
context "count" do
|
110
109
|
should "work scoped to association" do
|
111
110
|
project = Project.create
|
@@ -316,25 +315,25 @@ class ManyProxyTest < Test::Unit::TestCase
|
|
316
315
|
|
317
316
|
context "with one id" do
|
318
317
|
should "work for id in association" do
|
319
|
-
@project1.statuses.find(@complete.
|
318
|
+
@project1.statuses.find(@complete._id).should == @complete
|
320
319
|
end
|
321
320
|
|
322
321
|
should "not work for id not in association" do
|
323
322
|
lambda {
|
324
|
-
@project1.statuses.find!(@archived.
|
323
|
+
@project1.statuses.find!(@archived._id)
|
325
324
|
}.should raise_error(MongoMapper::DocumentNotFound)
|
326
325
|
end
|
327
326
|
end
|
328
327
|
|
329
328
|
context "with multiple ids" do
|
330
329
|
should "work for ids in association" do
|
331
|
-
statuses = @project1.statuses.find(@brand_new.
|
330
|
+
statuses = @project1.statuses.find(@brand_new._id, @complete._id)
|
332
331
|
statuses.should == [@brand_new, @complete]
|
333
332
|
end
|
334
333
|
|
335
334
|
should "not work for ids not in association" do
|
336
335
|
lambda {
|
337
|
-
@project1.statuses.find!(@brand_new.
|
336
|
+
@project1.statuses.find!(@brand_new._id, @complete._id, @archived._id)
|
338
337
|
}.should raise_error(MongoMapper::DocumentNotFound)
|
339
338
|
end
|
340
339
|
end
|
@@ -14,7 +14,7 @@ class AssociationsTest < Test::Unit::TestCase
|
|
14
14
|
include MongoMapper::EmbeddedDocument
|
15
15
|
|
16
16
|
key :name, String
|
17
|
-
key :post_id,
|
17
|
+
key :post_id, ObjectId
|
18
18
|
|
19
19
|
belongs_to :post, :class_name => 'AssociationsTest::AwesomeUser'
|
20
20
|
end
|
@@ -22,7 +22,7 @@ class AssociationsTest < Test::Unit::TestCase
|
|
22
22
|
class AwesomePost
|
23
23
|
include MongoMapper::Document
|
24
24
|
|
25
|
-
key :creator_id,
|
25
|
+
key :creator_id, ObjectId
|
26
26
|
|
27
27
|
belongs_to :creator, :class_name => 'AssociationsTest::AwesomeUser'
|
28
28
|
many :tags, :class_name => 'AssociationsTest::AwesomeTag', :foreign_key => :post_id
|
@@ -38,7 +38,7 @@ class AssociationsTest < Test::Unit::TestCase
|
|
38
38
|
post2 = AwesomePost.create(:creator => user, :tags => [tag2])
|
39
39
|
user.posts.should == [post1, post2]
|
40
40
|
|
41
|
-
|
42
|
-
|
41
|
+
post1 = post1.reload
|
42
|
+
post1.tags.should == [tag1]
|
43
43
|
end
|
44
44
|
end
|
@@ -55,17 +55,17 @@ class DirtyTest < Test::Unit::TestCase
|
|
55
55
|
should "not happen when loading from database" do
|
56
56
|
doc = @document.create(:phrase => 'Foo')
|
57
57
|
|
58
|
-
|
59
|
-
|
58
|
+
doc = doc.reload
|
59
|
+
doc.changed?.should be_false
|
60
60
|
end
|
61
61
|
|
62
62
|
should "happen if changed after loading from database" do
|
63
63
|
doc = @document.create(:phrase => 'Foo')
|
64
64
|
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
65
|
+
doc = doc.reload
|
66
|
+
doc.changed?.should be_false
|
67
|
+
doc.phrase = 'Bar'
|
68
|
+
doc.changed?.should be_true
|
69
69
|
end
|
70
70
|
end
|
71
71
|
|
@@ -17,6 +17,7 @@ class DocumentTest < Test::Unit::TestCase
|
|
17
17
|
|
18
18
|
context "Saving a document with a custom id" do
|
19
19
|
should "clear custom id flag when saved" do
|
20
|
+
@document.key :_id, String
|
20
21
|
doc = @document.new(:id => '1234')
|
21
22
|
doc.using_custom_id?.should be_true
|
22
23
|
doc.save.should be_true
|
@@ -37,7 +38,7 @@ class DocumentTest < Test::Unit::TestCase
|
|
37
38
|
|
38
39
|
context "Loading a document from the database with keys that are not defined" do
|
39
40
|
setup do
|
40
|
-
@id = Mongo::ObjectID.new
|
41
|
+
@id = Mongo::ObjectID.new
|
41
42
|
@document.collection.insert({
|
42
43
|
:_id => @id,
|
43
44
|
:first_name => 'John',
|
@@ -80,7 +81,7 @@ class DocumentTest < Test::Unit::TestCase
|
|
80
81
|
doc.tags = %w(foo bar)
|
81
82
|
doc.save
|
82
83
|
doc.tags.should == %w(foo bar)
|
83
|
-
|
84
|
+
doc.reload.tags.should == %w(foo bar)
|
84
85
|
end
|
85
86
|
|
86
87
|
should "work with assignment then <<" do
|
@@ -102,7 +103,7 @@ class DocumentTest < Test::Unit::TestCase
|
|
102
103
|
doc.tags << "bar"
|
103
104
|
doc.save
|
104
105
|
doc.tags.should == %w(foo bar)
|
105
|
-
|
106
|
+
doc.reload.tags.should == %w(foo bar)
|
106
107
|
end
|
107
108
|
end
|
108
109
|
|
@@ -135,7 +136,7 @@ class DocumentTest < Test::Unit::TestCase
|
|
135
136
|
doc.foo = {:baz => 'bar'}
|
136
137
|
doc.save
|
137
138
|
|
138
|
-
doc =
|
139
|
+
doc = doc.reload
|
139
140
|
doc.foo[:baz].should == 'bar'
|
140
141
|
doc.foo['baz'].should == 'bar'
|
141
142
|
end
|
@@ -156,12 +157,11 @@ class DocumentTest < Test::Unit::TestCase
|
|
156
157
|
doc = @document.new
|
157
158
|
doc.save
|
158
159
|
|
159
|
-
|
160
|
-
|
160
|
+
doc = doc.reload
|
161
|
+
doc.window.should == WindowSize.new(600, 480)
|
161
162
|
end
|
162
163
|
end
|
163
164
|
|
164
|
-
|
165
165
|
context "Creating a single document" do
|
166
166
|
setup do
|
167
167
|
@doc_instance = @document.create({:first_name => 'John', :last_name => 'Nunemaker', :age => '27'})
|
@@ -174,6 +174,8 @@ class DocumentTest < Test::Unit::TestCase
|
|
174
174
|
should "automatically set id" do
|
175
175
|
@doc_instance.id.should_not be_nil
|
176
176
|
@doc_instance.id.size.should == 24
|
177
|
+
@doc_instance.id.should be_instance_of(String)
|
178
|
+
@doc_instance._id.should be_instance_of(Mongo::ObjectID)
|
177
179
|
end
|
178
180
|
|
179
181
|
should "no longer be new?" do
|
@@ -226,7 +228,7 @@ class DocumentTest < Test::Unit::TestCase
|
|
226
228
|
context "Updating a document" do
|
227
229
|
setup do
|
228
230
|
doc = @document.create({:first_name => 'John', :last_name => 'Nunemaker', :age => '27'})
|
229
|
-
@doc_instance = @document.update(doc.
|
231
|
+
@doc_instance = @document.update(doc._id, {:age => 40})
|
230
232
|
end
|
231
233
|
|
232
234
|
should "update attributes provided" do
|
@@ -246,8 +248,8 @@ class DocumentTest < Test::Unit::TestCase
|
|
246
248
|
should "raise error when updating single doc if not provided id and attributes" do
|
247
249
|
doc = @document.create({:first_name => 'John', :last_name => 'Nunemaker', :age => '27'})
|
248
250
|
lambda { @document.update }.should raise_error(ArgumentError)
|
249
|
-
lambda { @document.update(doc.
|
250
|
-
lambda { @document.update(doc.
|
251
|
+
lambda { @document.update(doc._id) }.should raise_error(ArgumentError)
|
252
|
+
lambda { @document.update(doc._id, [1]) }.should raise_error(ArgumentError)
|
251
253
|
end
|
252
254
|
|
253
255
|
context "Updating multiple documents" do
|
@@ -256,8 +258,8 @@ class DocumentTest < Test::Unit::TestCase
|
|
256
258
|
@doc2 = @document.create({:first_name => 'Steve', :last_name => 'Smith', :age => '28'})
|
257
259
|
|
258
260
|
@doc_instances = @document.update({
|
259
|
-
@doc1.
|
260
|
-
@doc2.
|
261
|
+
@doc1._id => {:age => 30},
|
262
|
+
@doc2._id => {:age => 30},
|
261
263
|
})
|
262
264
|
end
|
263
265
|
|
@@ -272,8 +274,8 @@ class DocumentTest < Test::Unit::TestCase
|
|
272
274
|
end
|
273
275
|
|
274
276
|
should "update the documents" do
|
275
|
-
@document.find(@doc1.
|
276
|
-
@document.find(@doc2.
|
277
|
+
@document.find(@doc1._id).age.should == 30
|
278
|
+
@document.find(@doc2._id).age.should == 30
|
277
279
|
end
|
278
280
|
end
|
279
281
|
|
@@ -298,7 +300,7 @@ class DocumentTest < Test::Unit::TestCase
|
|
298
300
|
|
299
301
|
context "with a single id" do
|
300
302
|
should "work" do
|
301
|
-
@document.find(@doc1.
|
303
|
+
@document.find(@doc1._id).should == @doc1
|
302
304
|
end
|
303
305
|
|
304
306
|
should "return nil if document not found with find" do
|
@@ -314,15 +316,15 @@ class DocumentTest < Test::Unit::TestCase
|
|
314
316
|
|
315
317
|
context "with multiple id's" do
|
316
318
|
should "work as arguments" do
|
317
|
-
@document.find(@doc1.
|
319
|
+
@document.find(@doc1._id, @doc2._id).should == [@doc1, @doc2]
|
318
320
|
end
|
319
321
|
|
320
322
|
should "work as array" do
|
321
|
-
@document.find([@doc1.
|
323
|
+
@document.find([@doc1._id, @doc2._id]).should == [@doc1, @doc2]
|
322
324
|
end
|
323
325
|
|
324
326
|
should "return array if array only has one element" do
|
325
|
-
@document.find([@doc1.
|
327
|
+
@document.find([@doc1._id]).should == [@doc1]
|
326
328
|
end
|
327
329
|
end
|
328
330
|
|
@@ -442,8 +444,8 @@ class DocumentTest < Test::Unit::TestCase
|
|
442
444
|
end
|
443
445
|
|
444
446
|
should "be able to find by id" do
|
445
|
-
@document.find_by_id(@doc1.
|
446
|
-
@document.find_by_id(@doc2.
|
447
|
+
@document.find_by_id(@doc1._id).should == @doc1
|
448
|
+
@document.find_by_id(@doc2._id).should == @doc2
|
447
449
|
end
|
448
450
|
|
449
451
|
should "return nil if document not found" do
|
@@ -455,7 +457,7 @@ class DocumentTest < Test::Unit::TestCase
|
|
455
457
|
setup do
|
456
458
|
@doc1 = @document.create({:first_name => 'John', :last_name => 'Nunemaker', :age => '27'})
|
457
459
|
@doc2 = @document.create({:first_name => 'Steve', :last_name => 'Smith', :age => '28'})
|
458
|
-
@document.delete(@doc1.
|
460
|
+
@document.delete(@doc1._id)
|
459
461
|
end
|
460
462
|
|
461
463
|
should "remove document from collection" do
|
@@ -463,7 +465,7 @@ class DocumentTest < Test::Unit::TestCase
|
|
463
465
|
end
|
464
466
|
|
465
467
|
should "not remove other documents" do
|
466
|
-
@document.find(@doc2.
|
468
|
+
@document.find(@doc2._id).should_not be(nil)
|
467
469
|
end
|
468
470
|
end
|
469
471
|
|
@@ -472,7 +474,7 @@ class DocumentTest < Test::Unit::TestCase
|
|
472
474
|
@doc1 = @document.create({:first_name => 'John', :last_name => 'Nunemaker', :age => '27'})
|
473
475
|
@doc2 = @document.create({:first_name => 'Steve', :last_name => 'Smith', :age => '28'})
|
474
476
|
@doc3 = @document.create({:first_name => 'Steph', :last_name => 'Nunemaker', :age => '26'})
|
475
|
-
@document.delete(@doc1.
|
477
|
+
@document.delete(@doc1._id, @doc2._id)
|
476
478
|
|
477
479
|
@document.count.should == 1
|
478
480
|
end
|
@@ -481,7 +483,7 @@ class DocumentTest < Test::Unit::TestCase
|
|
481
483
|
@doc1 = @document.create({:first_name => 'John', :last_name => 'Nunemaker', :age => '27'})
|
482
484
|
@doc2 = @document.create({:first_name => 'Steve', :last_name => 'Smith', :age => '28'})
|
483
485
|
@doc3 = @document.create({:first_name => 'Steph', :last_name => 'Nunemaker', :age => '26'})
|
484
|
-
@document.delete([@doc1.
|
486
|
+
@document.delete([@doc1._id, @doc2._id])
|
485
487
|
|
486
488
|
@document.count.should == 1
|
487
489
|
end
|
@@ -514,7 +516,7 @@ class DocumentTest < Test::Unit::TestCase
|
|
514
516
|
setup do
|
515
517
|
@doc1 = @document.create({:first_name => 'John', :last_name => 'Nunemaker', :age => '27'})
|
516
518
|
@doc2 = @document.create({:first_name => 'Steve', :last_name => 'Smith', :age => '28'})
|
517
|
-
@document.destroy(@doc1.
|
519
|
+
@document.destroy(@doc1._id)
|
518
520
|
end
|
519
521
|
|
520
522
|
should "remove document from collection" do
|
@@ -522,7 +524,7 @@ class DocumentTest < Test::Unit::TestCase
|
|
522
524
|
end
|
523
525
|
|
524
526
|
should "not remove other documents" do
|
525
|
-
@document.find(@doc2.
|
527
|
+
@document.find(@doc2._id).should_not be(nil)
|
526
528
|
end
|
527
529
|
end
|
528
530
|
|
@@ -531,8 +533,8 @@ class DocumentTest < Test::Unit::TestCase
|
|
531
533
|
@doc1 = @document.create({:first_name => 'John', :last_name => 'Nunemaker', :age => '27'})
|
532
534
|
@doc2 = @document.create({:first_name => 'Steve', :last_name => 'Smith', :age => '28'})
|
533
535
|
@doc3 = @document.create({:first_name => 'Steph', :last_name => 'Nunemaker', :age => '26'})
|
534
|
-
@document.destroy(@doc1.
|
535
|
-
|
536
|
+
@document.destroy(@doc1._id, @doc2._id)
|
537
|
+
|
536
538
|
@document.count.should == 1
|
537
539
|
end
|
538
540
|
|
@@ -540,7 +542,7 @@ class DocumentTest < Test::Unit::TestCase
|
|
540
542
|
@doc1 = @document.create({:first_name => 'John', :last_name => 'Nunemaker', :age => '27'})
|
541
543
|
@doc2 = @document.create({:first_name => 'Steve', :last_name => 'Smith', :age => '28'})
|
542
544
|
@doc3 = @document.create({:first_name => 'Steph', :last_name => 'Nunemaker', :age => '26'})
|
543
|
-
@document.destroy([@doc1.
|
545
|
+
@document.destroy([@doc1._id, @doc2._id])
|
544
546
|
|
545
547
|
@document.count.should == 1
|
546
548
|
end
|
@@ -594,7 +596,7 @@ class DocumentTest < Test::Unit::TestCase
|
|
594
596
|
context "many" do
|
595
597
|
context "=> destroy" do
|
596
598
|
setup do
|
597
|
-
Property.key :thing_id,
|
599
|
+
Property.key :thing_id, ObjectId
|
598
600
|
Property.belongs_to :thing, :dependent => :destroy
|
599
601
|
Thing.many :properties, :dependent => :destroy
|
600
602
|
|
@@ -617,7 +619,7 @@ class DocumentTest < Test::Unit::TestCase
|
|
617
619
|
|
618
620
|
context "=> delete_all" do
|
619
621
|
setup do
|
620
|
-
Property.key :thing_id,
|
622
|
+
Property.key :thing_id, ObjectId
|
621
623
|
Property.belongs_to :thing
|
622
624
|
Thing.has_many :properties, :dependent => :delete_all
|
623
625
|
|
@@ -640,7 +642,7 @@ class DocumentTest < Test::Unit::TestCase
|
|
640
642
|
|
641
643
|
context "=> nullify" do
|
642
644
|
setup do
|
643
|
-
Property.key :thing_id,
|
645
|
+
Property.key :thing_id, ObjectId
|
644
646
|
Property.belongs_to :thing
|
645
647
|
Thing.has_many :properties, :dependent => :nullify
|
646
648
|
|
@@ -665,7 +667,7 @@ class DocumentTest < Test::Unit::TestCase
|
|
665
667
|
context "belongs_to" do
|
666
668
|
context "=> destroy" do
|
667
669
|
setup do
|
668
|
-
Property.key :thing_id,
|
670
|
+
Property.key :thing_id, ObjectId
|
669
671
|
Property.belongs_to :thing, :dependent => :destroy
|
670
672
|
Thing.has_many :properties
|
671
673
|
|
@@ -785,25 +787,24 @@ class DocumentTest < Test::Unit::TestCase
|
|
785
787
|
end
|
786
788
|
|
787
789
|
should "update attributes in the database" do
|
788
|
-
|
789
|
-
|
790
|
-
|
791
|
-
|
790
|
+
doc = @doc.reload
|
791
|
+
doc.should == @doc
|
792
|
+
doc.first_name.should == 'John'
|
793
|
+
doc.age.should == 27
|
792
794
|
end
|
793
795
|
|
794
796
|
should "allow to add custom attributes to the document" do
|
795
797
|
@doc = @document.new(:first_name => 'David', :age => '26', :gender => 'male', :tags => [1, "2"])
|
796
798
|
@doc.save
|
797
|
-
|
798
|
-
|
799
|
-
|
799
|
+
doc = @doc.reload
|
800
|
+
doc.gender.should == 'male'
|
801
|
+
doc.tags.should == [1, "2"]
|
800
802
|
end
|
801
803
|
|
802
804
|
should "allow to use custom methods to assign properties" do
|
803
|
-
person = RealPerson.new(:realname =>
|
805
|
+
person = RealPerson.new(:realname => 'David')
|
804
806
|
person.save
|
805
|
-
|
806
|
-
from_db.name.should == "David"
|
807
|
+
person.reload.name.should == 'David'
|
807
808
|
end
|
808
809
|
|
809
810
|
context "with key of type date" do
|
@@ -833,17 +834,16 @@ class DocumentTest < Test::Unit::TestCase
|
|
833
834
|
end
|
834
835
|
|
835
836
|
should "update attributes in the database" do
|
836
|
-
|
837
|
-
|
838
|
-
|
837
|
+
doc = @doc.reload
|
838
|
+
doc.first_name.should == 'Johnny'
|
839
|
+
doc.age.should == 30
|
839
840
|
end
|
840
841
|
|
841
842
|
should "allow updating custom attributes" do
|
842
843
|
@doc = @document.new(:first_name => 'David', :age => '26', :gender => 'male')
|
843
844
|
@doc.gender = 'Male'
|
844
845
|
@doc.save
|
845
|
-
|
846
|
-
from_db.gender.should == 'Male'
|
846
|
+
@doc.reload.gender.should == 'Male'
|
847
847
|
end
|
848
848
|
end
|
849
849
|
|
@@ -868,16 +868,15 @@ class DocumentTest < Test::Unit::TestCase
|
|
868
868
|
end
|
869
869
|
|
870
870
|
should "update attributes in the database" do
|
871
|
-
|
872
|
-
|
873
|
-
|
874
|
-
|
871
|
+
doc = @doc.reload
|
872
|
+
doc.should == @doc
|
873
|
+
doc.first_name.should == 'Johnny'
|
874
|
+
doc.age.should == 30
|
875
875
|
end
|
876
876
|
|
877
877
|
should "allow updating custom attributes" do
|
878
878
|
@doc.update_attributes(:gender => 'mALe')
|
879
|
-
|
880
|
-
from_db.gender.should == 'mALe'
|
879
|
+
@doc.reload.gender.should == 'mALe'
|
881
880
|
end
|
882
881
|
end
|
883
882
|
|
@@ -897,9 +896,9 @@ class DocumentTest < Test::Unit::TestCase
|
|
897
896
|
end
|
898
897
|
|
899
898
|
should "update attributes in the database" do
|
900
|
-
|
901
|
-
|
902
|
-
|
899
|
+
doc = @doc.reload
|
900
|
+
doc.first_name.should == 'Johnny'
|
901
|
+
doc.age.should == 30
|
903
902
|
end
|
904
903
|
end
|
905
904
|
|
@@ -1031,14 +1030,14 @@ class DocumentTest < Test::Unit::TestCase
|
|
1031
1030
|
steph = DocDaughter.create(:name => 'Steph')
|
1032
1031
|
|
1033
1032
|
lambda {
|
1034
|
-
DocSon.find!(steph.
|
1033
|
+
DocSon.find!(steph._id)
|
1035
1034
|
}.should raise_error(MongoMapper::DocumentNotFound)
|
1036
1035
|
end
|
1037
1036
|
|
1038
1037
|
should "not raise error for find with parent" do
|
1039
1038
|
john = DocSon.create(:name => 'John')
|
1040
1039
|
|
1041
|
-
DocParent.find!(john.
|
1040
|
+
DocParent.find!(john._id).should == john
|
1042
1041
|
end
|
1043
1042
|
|
1044
1043
|
should "count scoped to class" do
|
@@ -1073,7 +1072,7 @@ class DocumentTest < Test::Unit::TestCase
|
|
1073
1072
|
steph = DocDaughter.create(:name => 'Steph')
|
1074
1073
|
|
1075
1074
|
lambda {
|
1076
|
-
DocSon.destroy(steph.
|
1075
|
+
DocSon.destroy(steph._id)
|
1077
1076
|
}.should raise_error(MongoMapper::DocumentNotFound)
|
1078
1077
|
end
|
1079
1078
|
|
@@ -1082,7 +1081,7 @@ class DocumentTest < Test::Unit::TestCase
|
|
1082
1081
|
steph = DocDaughter.create(:name => 'Steph')
|
1083
1082
|
|
1084
1083
|
lambda {
|
1085
|
-
DocSon.delete(steph.
|
1084
|
+
DocSon.delete(steph._id)
|
1086
1085
|
}.should_not change { DocParent.count }
|
1087
1086
|
end
|
1088
1087
|
|
@@ -1142,9 +1141,9 @@ class DocumentTest < Test::Unit::TestCase
|
|
1142
1141
|
@document.update(doc._id, { :first_name => 'Johnny' })
|
1143
1142
|
end
|
1144
1143
|
|
1145
|
-
|
1146
|
-
|
1147
|
-
|
1144
|
+
doc = doc.reload
|
1145
|
+
doc.created_at.should == old_created_at
|
1146
|
+
doc.updated_at.should_not == old_updated_at
|
1148
1147
|
end
|
1149
1148
|
end
|
1150
1149
|
|
@@ -1174,7 +1173,7 @@ class DocumentTest < Test::Unit::TestCase
|
|
1174
1173
|
context "reload" do
|
1175
1174
|
setup do
|
1176
1175
|
@doc_instance_1 = @document.create({:first_name => 'Ryan', :last_name => 'Koopmans', :age => '37'})
|
1177
|
-
@doc_instance_2 = @document.update(@doc_instance_1.
|
1176
|
+
@doc_instance_2 = @document.update(@doc_instance_1._id, {:age => '39'})
|
1178
1177
|
end
|
1179
1178
|
|
1180
1179
|
should "load fresh information from the database" do
|