mongo_mapper 0.5.8 → 0.6.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.
- data/Rakefile +4 -4
- data/VERSION +1 -1
- data/bin/mmconsole +10 -5
- data/lib/mongo_mapper.rb +28 -5
- data/lib/mongo_mapper/associations.rb +113 -12
- data/lib/mongo_mapper/associations/base.rb +24 -9
- 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 +7 -2
- data/lib/mongo_mapper/associations/many_embedded_proxy.rb +22 -36
- data/lib/mongo_mapper/associations/proxy.rb +11 -6
- data/lib/mongo_mapper/document.rb +37 -21
- data/lib/mongo_mapper/embedded_document.rb +32 -18
- data/lib/mongo_mapper/finder_options.rb +19 -12
- 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/support.rb +18 -46
- data/lib/mongo_mapper/types.rb +64 -0
- data/lib/mongo_mapper/validations.rb +13 -43
- data/mongo_mapper.gemspec +13 -10
- data/test/functional/associations/test_belongs_to_polymorphic_proxy.rb +10 -10
- data/test/functional/associations/test_belongs_to_proxy.rb +29 -30
- 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 +69 -74
- 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 +76 -69
- data/test/functional/test_embedded_document.rb +15 -14
- data/test/functional/test_pagination.rb +9 -1
- data/test/functional/test_string_id_compatibility.rb +72 -0
- data/test/functional/test_validations.rb +56 -7
- data/test/models.rb +7 -7
- data/test/test_helper.rb +2 -5
- data/test/unit/test_association_base.rb +6 -1
- data/test/unit/test_document.rb +22 -13
- 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 +9 -6
- data/test/unit/test_mongomapper.rb +0 -28
@@ -17,13 +17,6 @@ class ManyEmbeddedProxyTest < Test::Unit::TestCase
|
|
17
17
|
project.addresses.push Address.new
|
18
18
|
project.addresses.size.should == 2
|
19
19
|
end
|
20
|
-
|
21
|
-
should "allow finding :all embedded documents" do
|
22
|
-
project = Project.new
|
23
|
-
project.addresses << Address.new
|
24
|
-
project.addresses << Address.new
|
25
|
-
project.save
|
26
|
-
end
|
27
20
|
|
28
21
|
should "be embedded in document on save" do
|
29
22
|
sb = Address.new(:city => 'South Bend', :state => 'IN')
|
@@ -33,10 +26,10 @@ class ManyEmbeddedProxyTest < Test::Unit::TestCase
|
|
33
26
|
project.addresses << chi
|
34
27
|
project.save
|
35
28
|
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
29
|
+
project = project.reload
|
30
|
+
project.addresses.size.should == 2
|
31
|
+
project.addresses[0].should == sb
|
32
|
+
project.addresses[1].should == chi
|
40
33
|
end
|
41
34
|
|
42
35
|
should "allow embedding arbitrarily deep" do
|
@@ -54,10 +47,10 @@ class ManyEmbeddedProxyTest < Test::Unit::TestCase
|
|
54
47
|
doc = @document.new(:person => meg)
|
55
48
|
doc.save
|
56
49
|
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
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'
|
61
54
|
end
|
62
55
|
|
63
56
|
should "allow assignment of 'many' embedded documents using a hash" do
|
@@ -77,12 +70,12 @@ class ManyEmbeddedProxyTest < Test::Unit::TestCase
|
|
77
70
|
pet_lover.pets[1].species.should == "Siberian Husky"
|
78
71
|
pet_lover.save.should be_true
|
79
72
|
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
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"
|
86
79
|
end
|
87
80
|
|
88
81
|
context "embedding many embedded documents" do
|
@@ -108,13 +101,13 @@ class ManyEmbeddedProxyTest < Test::Unit::TestCase
|
|
108
101
|
doc.people << meg
|
109
102
|
doc.save
|
110
103
|
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
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"
|
118
111
|
end
|
119
112
|
|
120
113
|
should "create a reference to the root document for all embedded documents before save" do
|
@@ -130,70 +123,72 @@ class ManyEmbeddedProxyTest < Test::Unit::TestCase
|
|
130
123
|
doc.people.first.pets.first._root_document.should == doc
|
131
124
|
end
|
132
125
|
|
133
|
-
should "create properly-named reference to parent document when building off association proxy" do
|
134
|
-
person = RealPerson.new
|
135
|
-
pet = person.pets.build
|
136
|
-
person.should == pet.real_person
|
137
|
-
end
|
138
|
-
|
139
|
-
|
140
126
|
should "create a reference to the root document for all embedded documents" do
|
141
|
-
meg = Person.new(:name => "Meg")
|
142
127
|
sparky = Pet.new(:name => "Sparky", :species => "Dog")
|
143
|
-
|
128
|
+
meg = Person.new(:name => "Meg", :pets => [sparky])
|
144
129
|
doc = @document.new
|
145
|
-
|
146
|
-
meg.pets << sparky
|
147
|
-
|
148
130
|
doc.people << meg
|
149
131
|
doc.save
|
150
132
|
|
151
|
-
|
152
|
-
|
153
|
-
|
133
|
+
doc = doc.reload
|
134
|
+
doc.people.first._root_document.should == doc
|
135
|
+
doc.people.first.pets.first._root_document.should == doc
|
154
136
|
end
|
155
137
|
end
|
156
138
|
|
157
|
-
should "allow
|
158
|
-
meg = Person.new(:name => "Meg")
|
159
|
-
sparky = Pet.new(:name => "Sparky", :species => "Dog")
|
160
|
-
|
161
|
-
meg.pets << sparky
|
162
|
-
|
163
|
-
meg.pets.find(:all).should include(sparky)
|
164
|
-
end
|
165
|
-
|
166
|
-
should "allow retrieval via find(id)" do
|
167
|
-
meg = Person.new(:name => "Meg")
|
139
|
+
should "allow finding by id" do
|
168
140
|
sparky = Pet.new(:name => "Sparky", :species => "Dog")
|
169
|
-
|
170
|
-
meg.pets
|
171
|
-
|
172
|
-
meg.pets.find(sparky.id).should == sparky
|
141
|
+
meg = Person.new(:name => "Meg", :pets => [sparky])
|
142
|
+
meg.pets.find(sparky._id).should == sparky
|
173
143
|
end
|
174
144
|
|
175
145
|
context "extending the association" do
|
146
|
+
setup do
|
147
|
+
@address_class = Class.new do
|
148
|
+
include MongoMapper::EmbeddedDocument
|
149
|
+
key :address, String
|
150
|
+
key :city, String
|
151
|
+
key :state, String
|
152
|
+
key :zip, Integer
|
153
|
+
end
|
154
|
+
|
155
|
+
@project_class = Class.new do
|
156
|
+
include MongoMapper::Document
|
157
|
+
key :name, String
|
158
|
+
end
|
159
|
+
|
160
|
+
@project_class.collection.remove
|
161
|
+
end
|
162
|
+
|
176
163
|
should "work using a block passed to many" do
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
164
|
+
@project_class.many :addresses, :class => @address_class do
|
165
|
+
def find_all_by_state(state)
|
166
|
+
find_all { |a| a.state == state }
|
167
|
+
end
|
168
|
+
end
|
169
|
+
|
170
|
+
addr1 = @address_class.new(:address => "Gate-3 Lankershim Blvd.", :city => "Universal City", :state => "CA", :zip => "91608")
|
171
|
+
addr2 = @address_class.new(:address => "3000 W. Alameda Ave.", :city => "Burbank", :state => "CA", :zip => "91523")
|
172
|
+
addr3 = @address_class.new(:address => "111 Some Ln", :city => "Nashville", :state => "TN", :zip => "37211")
|
173
|
+
project = @project_class.create(:name => "Some Project", :addresses => [addr1, addr2, addr3])
|
174
|
+
|
183
175
|
project.addresses.find_all_by_state("CA").should == [addr1, addr2]
|
184
176
|
end
|
185
177
|
|
186
178
|
should "work using many's :extend option" do
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
179
|
+
module FindByCity
|
180
|
+
def find_by_city(city)
|
181
|
+
find_all { |a| a.city == city }
|
182
|
+
end
|
183
|
+
end
|
184
|
+
@project_class.many :addresses, :class => @address_class, :extend => FindByCity
|
185
|
+
|
186
|
+
addr1 = @address_class.new(:address => "Gate-3 Lankershim Blvd.", :city => "Universal City", :state => "CA", :zip => "91608")
|
187
|
+
addr2 = @address_class.new(:address => "3000 W. Alameda Ave.", :city => "Burbank", :state => "CA", :zip => "91523")
|
188
|
+
addr3 = @address_class.new(:address => "111 Some Ln", :city => "Nashville", :state => "TN", :zip => "37211")
|
189
|
+
project = @project_class.create(:name => "Some Project", :addresses => [addr1, addr2, addr3])
|
190
|
+
|
191
|
+
project.addresses.find_by_city('Burbank').should == [addr2]
|
197
192
|
end
|
198
193
|
end
|
199
194
|
end
|
@@ -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
|
|
@@ -288,33 +290,41 @@ class DocumentTest < Test::Unit::TestCase
|
|
288
290
|
@doc3 = @document.create({:first_name => 'Steph', :last_name => 'Nunemaker', :age => '26'})
|
289
291
|
end
|
290
292
|
|
291
|
-
should "
|
292
|
-
|
293
|
+
should "return nil if nothing provided for find" do
|
294
|
+
@document.find.should be_nil
|
295
|
+
end
|
296
|
+
|
297
|
+
should "raise document not found if nothing provided for find!" do
|
298
|
+
lambda { @document.find! }.should raise_error(MongoMapper::DocumentNotFound)
|
293
299
|
end
|
294
300
|
|
295
301
|
context "with a single id" do
|
296
302
|
should "work" do
|
297
|
-
@document.find(@doc1.
|
303
|
+
@document.find(@doc1._id).should == @doc1
|
304
|
+
end
|
305
|
+
|
306
|
+
should "return nil if document not found with find" do
|
307
|
+
@document.find(123).should be_nil
|
298
308
|
end
|
299
309
|
|
300
|
-
should "raise error if document not found" do
|
310
|
+
should "raise error if document not found with find!" do
|
301
311
|
lambda {
|
302
|
-
@document.find(123)
|
312
|
+
@document.find!(123)
|
303
313
|
}.should raise_error(MongoMapper::DocumentNotFound)
|
304
314
|
end
|
305
315
|
end
|
306
316
|
|
307
317
|
context "with multiple id's" do
|
308
318
|
should "work as arguments" do
|
309
|
-
@document.find(@doc1.
|
319
|
+
@document.find(@doc1._id, @doc2._id).should == [@doc1, @doc2]
|
310
320
|
end
|
311
321
|
|
312
322
|
should "work as array" do
|
313
|
-
@document.find([@doc1.
|
323
|
+
@document.find([@doc1._id, @doc2._id]).should == [@doc1, @doc2]
|
314
324
|
end
|
315
325
|
|
316
326
|
should "return array if array only has one element" do
|
317
|
-
@document.find([@doc1.
|
327
|
+
@document.find([@doc1._id]).should == [@doc1]
|
318
328
|
end
|
319
329
|
end
|
320
330
|
|
@@ -434,8 +444,8 @@ class DocumentTest < Test::Unit::TestCase
|
|
434
444
|
end
|
435
445
|
|
436
446
|
should "be able to find by id" do
|
437
|
-
@document.find_by_id(@doc1.
|
438
|
-
@document.find_by_id(@doc2.
|
447
|
+
@document.find_by_id(@doc1._id).should == @doc1
|
448
|
+
@document.find_by_id(@doc2._id).should == @doc2
|
439
449
|
end
|
440
450
|
|
441
451
|
should "return nil if document not found" do
|
@@ -447,7 +457,7 @@ class DocumentTest < Test::Unit::TestCase
|
|
447
457
|
setup do
|
448
458
|
@doc1 = @document.create({:first_name => 'John', :last_name => 'Nunemaker', :age => '27'})
|
449
459
|
@doc2 = @document.create({:first_name => 'Steve', :last_name => 'Smith', :age => '28'})
|
450
|
-
@document.delete(@doc1.
|
460
|
+
@document.delete(@doc1._id)
|
451
461
|
end
|
452
462
|
|
453
463
|
should "remove document from collection" do
|
@@ -455,7 +465,7 @@ class DocumentTest < Test::Unit::TestCase
|
|
455
465
|
end
|
456
466
|
|
457
467
|
should "not remove other documents" do
|
458
|
-
@document.find(@doc2.
|
468
|
+
@document.find(@doc2._id).should_not be(nil)
|
459
469
|
end
|
460
470
|
end
|
461
471
|
|
@@ -464,7 +474,7 @@ class DocumentTest < Test::Unit::TestCase
|
|
464
474
|
@doc1 = @document.create({:first_name => 'John', :last_name => 'Nunemaker', :age => '27'})
|
465
475
|
@doc2 = @document.create({:first_name => 'Steve', :last_name => 'Smith', :age => '28'})
|
466
476
|
@doc3 = @document.create({:first_name => 'Steph', :last_name => 'Nunemaker', :age => '26'})
|
467
|
-
@document.delete(@doc1.
|
477
|
+
@document.delete(@doc1._id, @doc2._id)
|
468
478
|
|
469
479
|
@document.count.should == 1
|
470
480
|
end
|
@@ -473,7 +483,7 @@ class DocumentTest < Test::Unit::TestCase
|
|
473
483
|
@doc1 = @document.create({:first_name => 'John', :last_name => 'Nunemaker', :age => '27'})
|
474
484
|
@doc2 = @document.create({:first_name => 'Steve', :last_name => 'Smith', :age => '28'})
|
475
485
|
@doc3 = @document.create({:first_name => 'Steph', :last_name => 'Nunemaker', :age => '26'})
|
476
|
-
@document.delete([@doc1.
|
486
|
+
@document.delete([@doc1._id, @doc2._id])
|
477
487
|
|
478
488
|
@document.count.should == 1
|
479
489
|
end
|
@@ -506,7 +516,7 @@ class DocumentTest < Test::Unit::TestCase
|
|
506
516
|
setup do
|
507
517
|
@doc1 = @document.create({:first_name => 'John', :last_name => 'Nunemaker', :age => '27'})
|
508
518
|
@doc2 = @document.create({:first_name => 'Steve', :last_name => 'Smith', :age => '28'})
|
509
|
-
@document.destroy(@doc1.
|
519
|
+
@document.destroy(@doc1._id)
|
510
520
|
end
|
511
521
|
|
512
522
|
should "remove document from collection" do
|
@@ -514,7 +524,7 @@ class DocumentTest < Test::Unit::TestCase
|
|
514
524
|
end
|
515
525
|
|
516
526
|
should "not remove other documents" do
|
517
|
-
@document.find(@doc2.
|
527
|
+
@document.find(@doc2._id).should_not be(nil)
|
518
528
|
end
|
519
529
|
end
|
520
530
|
|
@@ -523,8 +533,8 @@ class DocumentTest < Test::Unit::TestCase
|
|
523
533
|
@doc1 = @document.create({:first_name => 'John', :last_name => 'Nunemaker', :age => '27'})
|
524
534
|
@doc2 = @document.create({:first_name => 'Steve', :last_name => 'Smith', :age => '28'})
|
525
535
|
@doc3 = @document.create({:first_name => 'Steph', :last_name => 'Nunemaker', :age => '26'})
|
526
|
-
@document.destroy(@doc1.
|
527
|
-
|
536
|
+
@document.destroy(@doc1._id, @doc2._id)
|
537
|
+
|
528
538
|
@document.count.should == 1
|
529
539
|
end
|
530
540
|
|
@@ -532,7 +542,7 @@ class DocumentTest < Test::Unit::TestCase
|
|
532
542
|
@doc1 = @document.create({:first_name => 'John', :last_name => 'Nunemaker', :age => '27'})
|
533
543
|
@doc2 = @document.create({:first_name => 'Steve', :last_name => 'Smith', :age => '28'})
|
534
544
|
@doc3 = @document.create({:first_name => 'Steph', :last_name => 'Nunemaker', :age => '26'})
|
535
|
-
@document.destroy([@doc1.
|
545
|
+
@document.destroy([@doc1._id, @doc2._id])
|
536
546
|
|
537
547
|
@document.count.should == 1
|
538
548
|
end
|
@@ -586,7 +596,7 @@ class DocumentTest < Test::Unit::TestCase
|
|
586
596
|
context "many" do
|
587
597
|
context "=> destroy" do
|
588
598
|
setup do
|
589
|
-
Property.key :thing_id,
|
599
|
+
Property.key :thing_id, ObjectId
|
590
600
|
Property.belongs_to :thing, :dependent => :destroy
|
591
601
|
Thing.many :properties, :dependent => :destroy
|
592
602
|
|
@@ -609,7 +619,7 @@ class DocumentTest < Test::Unit::TestCase
|
|
609
619
|
|
610
620
|
context "=> delete_all" do
|
611
621
|
setup do
|
612
|
-
Property.key :thing_id,
|
622
|
+
Property.key :thing_id, ObjectId
|
613
623
|
Property.belongs_to :thing
|
614
624
|
Thing.has_many :properties, :dependent => :delete_all
|
615
625
|
|
@@ -632,7 +642,7 @@ class DocumentTest < Test::Unit::TestCase
|
|
632
642
|
|
633
643
|
context "=> nullify" do
|
634
644
|
setup do
|
635
|
-
Property.key :thing_id,
|
645
|
+
Property.key :thing_id, ObjectId
|
636
646
|
Property.belongs_to :thing
|
637
647
|
Thing.has_many :properties, :dependent => :nullify
|
638
648
|
|
@@ -657,7 +667,7 @@ class DocumentTest < Test::Unit::TestCase
|
|
657
667
|
context "belongs_to" do
|
658
668
|
context "=> destroy" do
|
659
669
|
setup do
|
660
|
-
Property.key :thing_id,
|
670
|
+
Property.key :thing_id, ObjectId
|
661
671
|
Property.belongs_to :thing, :dependent => :destroy
|
662
672
|
Thing.has_many :properties
|
663
673
|
|
@@ -777,25 +787,24 @@ class DocumentTest < Test::Unit::TestCase
|
|
777
787
|
end
|
778
788
|
|
779
789
|
should "update attributes in the database" do
|
780
|
-
|
781
|
-
|
782
|
-
|
783
|
-
|
790
|
+
doc = @doc.reload
|
791
|
+
doc.should == @doc
|
792
|
+
doc.first_name.should == 'John'
|
793
|
+
doc.age.should == 27
|
784
794
|
end
|
785
795
|
|
786
796
|
should "allow to add custom attributes to the document" do
|
787
797
|
@doc = @document.new(:first_name => 'David', :age => '26', :gender => 'male', :tags => [1, "2"])
|
788
798
|
@doc.save
|
789
|
-
|
790
|
-
|
791
|
-
|
799
|
+
doc = @doc.reload
|
800
|
+
doc.gender.should == 'male'
|
801
|
+
doc.tags.should == [1, "2"]
|
792
802
|
end
|
793
803
|
|
794
804
|
should "allow to use custom methods to assign properties" do
|
795
|
-
person = RealPerson.new(:realname =>
|
805
|
+
person = RealPerson.new(:realname => 'David')
|
796
806
|
person.save
|
797
|
-
|
798
|
-
from_db.name.should == "David"
|
807
|
+
person.reload.name.should == 'David'
|
799
808
|
end
|
800
809
|
|
801
810
|
context "with key of type date" do
|
@@ -825,17 +834,16 @@ class DocumentTest < Test::Unit::TestCase
|
|
825
834
|
end
|
826
835
|
|
827
836
|
should "update attributes in the database" do
|
828
|
-
|
829
|
-
|
830
|
-
|
837
|
+
doc = @doc.reload
|
838
|
+
doc.first_name.should == 'Johnny'
|
839
|
+
doc.age.should == 30
|
831
840
|
end
|
832
841
|
|
833
842
|
should "allow updating custom attributes" do
|
834
843
|
@doc = @document.new(:first_name => 'David', :age => '26', :gender => 'male')
|
835
844
|
@doc.gender = 'Male'
|
836
845
|
@doc.save
|
837
|
-
|
838
|
-
from_db.gender.should == 'Male'
|
846
|
+
@doc.reload.gender.should == 'Male'
|
839
847
|
end
|
840
848
|
end
|
841
849
|
|
@@ -860,16 +868,15 @@ class DocumentTest < Test::Unit::TestCase
|
|
860
868
|
end
|
861
869
|
|
862
870
|
should "update attributes in the database" do
|
863
|
-
|
864
|
-
|
865
|
-
|
866
|
-
|
871
|
+
doc = @doc.reload
|
872
|
+
doc.should == @doc
|
873
|
+
doc.first_name.should == 'Johnny'
|
874
|
+
doc.age.should == 30
|
867
875
|
end
|
868
876
|
|
869
877
|
should "allow updating custom attributes" do
|
870
878
|
@doc.update_attributes(:gender => 'mALe')
|
871
|
-
|
872
|
-
from_db.gender.should == 'mALe'
|
879
|
+
@doc.reload.gender.should == 'mALe'
|
873
880
|
end
|
874
881
|
end
|
875
882
|
|
@@ -889,9 +896,9 @@ class DocumentTest < Test::Unit::TestCase
|
|
889
896
|
end
|
890
897
|
|
891
898
|
should "update attributes in the database" do
|
892
|
-
|
893
|
-
|
894
|
-
|
899
|
+
doc = @doc.reload
|
900
|
+
doc.first_name.should == 'Johnny'
|
901
|
+
doc.age.should == 30
|
895
902
|
end
|
896
903
|
end
|
897
904
|
|
@@ -1023,14 +1030,14 @@ class DocumentTest < Test::Unit::TestCase
|
|
1023
1030
|
steph = DocDaughter.create(:name => 'Steph')
|
1024
1031
|
|
1025
1032
|
lambda {
|
1026
|
-
DocSon.find(steph.
|
1033
|
+
DocSon.find!(steph._id)
|
1027
1034
|
}.should raise_error(MongoMapper::DocumentNotFound)
|
1028
1035
|
end
|
1029
1036
|
|
1030
1037
|
should "not raise error for find with parent" do
|
1031
1038
|
john = DocSon.create(:name => 'John')
|
1032
1039
|
|
1033
|
-
DocParent.find(john.
|
1040
|
+
DocParent.find!(john._id).should == john
|
1034
1041
|
end
|
1035
1042
|
|
1036
1043
|
should "count scoped to class" do
|
@@ -1065,7 +1072,7 @@ class DocumentTest < Test::Unit::TestCase
|
|
1065
1072
|
steph = DocDaughter.create(:name => 'Steph')
|
1066
1073
|
|
1067
1074
|
lambda {
|
1068
|
-
DocSon.destroy(steph.
|
1075
|
+
DocSon.destroy(steph._id)
|
1069
1076
|
}.should raise_error(MongoMapper::DocumentNotFound)
|
1070
1077
|
end
|
1071
1078
|
|
@@ -1074,7 +1081,7 @@ class DocumentTest < Test::Unit::TestCase
|
|
1074
1081
|
steph = DocDaughter.create(:name => 'Steph')
|
1075
1082
|
|
1076
1083
|
lambda {
|
1077
|
-
DocSon.delete(steph.
|
1084
|
+
DocSon.delete(steph._id)
|
1078
1085
|
}.should_not change { DocParent.count }
|
1079
1086
|
end
|
1080
1087
|
|
@@ -1134,9 +1141,9 @@ class DocumentTest < Test::Unit::TestCase
|
|
1134
1141
|
@document.update(doc._id, { :first_name => 'Johnny' })
|
1135
1142
|
end
|
1136
1143
|
|
1137
|
-
|
1138
|
-
|
1139
|
-
|
1144
|
+
doc = doc.reload
|
1145
|
+
doc.created_at.should == old_created_at
|
1146
|
+
doc.updated_at.should_not == old_updated_at
|
1140
1147
|
end
|
1141
1148
|
end
|
1142
1149
|
|
@@ -1166,7 +1173,7 @@ class DocumentTest < Test::Unit::TestCase
|
|
1166
1173
|
context "reload" do
|
1167
1174
|
setup do
|
1168
1175
|
@doc_instance_1 = @document.create({:first_name => 'Ryan', :last_name => 'Koopmans', :age => '37'})
|
1169
|
-
@doc_instance_2 = @document.update(@doc_instance_1.
|
1176
|
+
@doc_instance_2 = @document.update(@doc_instance_1._id, {:age => '39'})
|
1170
1177
|
end
|
1171
1178
|
|
1172
1179
|
should "load fresh information from the database" do
|