jnunemaker-mongomapper 0.3.1 → 0.3.2
Sign up to get free protection for your applications and to get access to all the features.
- data/History +11 -0
- data/VERSION +1 -1
- data/lib/mongomapper/associations/base.rb +5 -2
- data/lib/mongomapper/associations/belongs_to_polymorphic_proxy.rb +1 -2
- data/lib/mongomapper/associations/belongs_to_proxy.rb +3 -3
- data/lib/mongomapper/associations/many_documents_proxy.rb +85 -0
- data/lib/mongomapper/associations/many_embedded_polymorphic_proxy.rb +2 -3
- data/lib/mongomapper/associations/many_embedded_proxy.rb +2 -4
- data/lib/mongomapper/associations/many_polymorphic_proxy.rb +11 -0
- data/lib/mongomapper/associations/many_proxy.rb +1 -50
- data/lib/mongomapper/associations/proxy.rb +1 -1
- data/lib/mongomapper/document.rb +5 -12
- data/lib/mongomapper/embedded_document.rb +21 -2
- data/lib/mongomapper/key.rb +2 -1
- data/lib/mongomapper/serializers/json_serializer.rb +15 -0
- data/lib/mongomapper.rb +17 -10
- data/mongomapper.gemspec +17 -4
- data/test/functional/associations/test_belongs_to_polymorphic_proxy.rb +39 -0
- data/test/functional/associations/test_belongs_to_proxy.rb +35 -0
- data/test/functional/associations/test_many_embedded_polymorphic_proxy.rb +131 -0
- data/test/functional/associations/test_many_embedded_proxy.rb +106 -0
- data/test/functional/associations/test_many_polymorphic_proxy.rb +259 -0
- data/test/functional/associations/test_many_proxy.rb +236 -0
- data/test/functional/test_associations.rb +22 -467
- data/test/functional/test_document.rb +76 -19
- data/test/functional/test_pagination.rb +2 -3
- data/test/models.rb +16 -0
- data/test/test_helper.rb +1 -1
- data/test/unit/serializers/test_json_serializer.rb +69 -16
- data/test/unit/test_association_base.rb +5 -0
- data/test/unit/test_document.rb +14 -4
- data/test/unit/test_embedded_document.rb +46 -21
- data/test/unit/test_key.rb +5 -0
- data/test/unit/test_mongo_id.rb +2 -2
- data/test/unit/test_rails_compatibility.rb +3 -3
- metadata +16 -3
- data/lib/mongomapper/associations/array_proxy.rb +0 -6
@@ -6,480 +6,35 @@ class AssociationsTest < Test::Unit::TestCase
|
|
6
6
|
clear_all_collections
|
7
7
|
end
|
8
8
|
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
"transports" => [
|
14
|
-
{"_type" => "TrModels::Ambulance", "license_plate" => "GGG123", "icu" => true},
|
15
|
-
{"_type" => "TrModels::Car", "license_plate" => "ABC123", "model" => "VW Golf", "year" => 2001},
|
16
|
-
{"_type" => "TrModels::Car", "license_plate" => "DEF123", "model" => "Honda Accord", "year" => 2008},
|
17
|
-
]
|
18
|
-
}
|
19
|
-
|
20
|
-
fleet = TrModels::Fleet.new(fleet_attributes)
|
21
|
-
fleet.transports.size.should == 3
|
22
|
-
fleet.transports[0].class.should == TrModels::Ambulance
|
23
|
-
fleet.transports[0].license_plate.should == "GGG123"
|
24
|
-
fleet.transports[0].icu.should be_true
|
25
|
-
fleet.transports[1].class.should == TrModels::Car
|
26
|
-
fleet.transports[1].license_plate.should == "ABC123"
|
27
|
-
fleet.transports[1].model.should == "VW Golf"
|
28
|
-
fleet.transports[1].year.should == 2001
|
29
|
-
fleet.transports[2].class.should == TrModels::Car
|
30
|
-
fleet.transports[2].license_plate.should == "DEF123"
|
31
|
-
fleet.transports[2].model.should == "Honda Accord"
|
32
|
-
fleet.transports[2].year.should == 2008
|
33
|
-
fleet.save.should be_true
|
34
|
-
|
35
|
-
from_db = TrModels::Fleet.find(fleet.id)
|
36
|
-
from_db.transports.size.should == 3
|
37
|
-
from_db.transports[0].license_plate.should == "GGG123"
|
38
|
-
from_db.transports[0].icu.should be_true
|
39
|
-
from_db.transports[1].license_plate.should == "ABC123"
|
40
|
-
from_db.transports[1].model.should == "VW Golf"
|
41
|
-
from_db.transports[1].year.should == 2001
|
42
|
-
from_db.transports[2].license_plate.should == "DEF123"
|
43
|
-
from_db.transports[2].model.should == "Honda Accord"
|
44
|
-
from_db.transports[2].year.should == 2008
|
9
|
+
should "allow changing class names" do
|
10
|
+
class AwesomeUser
|
11
|
+
include MongoMapper::Document
|
12
|
+
many :posts, :class_name => 'AssociationsTest::AwesomePost', :foreign_key => :creator_id
|
45
13
|
end
|
46
14
|
|
47
|
-
|
48
|
-
|
49
|
-
|
15
|
+
class AwesomeTag
|
16
|
+
include MongoMapper::EmbeddedDocument
|
17
|
+
key :name, String
|
18
|
+
belongs_to :post, :class_name => 'AssociationsTest::AwesomeUser'
|
50
19
|
end
|
51
20
|
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
fleet.transports.size.should == 2
|
21
|
+
class AwesomePost
|
22
|
+
include MongoMapper::Document
|
23
|
+
belongs_to :creator, :class_name => 'AssociationsTest::AwesomeUser'
|
24
|
+
many :tags, :class_name => 'AssociationsTest::AwesomeTag', :foreign_key => :post_id
|
57
25
|
end
|
58
26
|
|
59
|
-
|
60
|
-
|
61
|
-
fleet.transports = [TrModels::Car.new("license_plate" => "DCU2013", "model" => "Honda Civic")]
|
62
|
-
fleet.save.should be_true
|
27
|
+
AwesomeUser.collection.clear
|
28
|
+
AwesomePost.collection.clear
|
63
29
|
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
should
|
70
|
-
fleet = TrModels::Fleet.new
|
71
|
-
fleet.transports = [
|
72
|
-
TrModels::Car.new("license_plate" => "ABC1223", "model" => "Honda Civic", "year" => 2003),
|
73
|
-
TrModels::Bus.new("license_plate" => "XYZ9090", "max_passengers" => 51),
|
74
|
-
TrModels::Ambulance.new("license_plate" => "HDD3030", "icu" => true)
|
75
|
-
]
|
76
|
-
fleet.save.should be_true
|
77
|
-
|
78
|
-
from_db = TrModels::Fleet.find(fleet.id)
|
79
|
-
from_db.transports.size.should == 3
|
80
|
-
from_db.transports[0].license_plate.should == "ABC1223"
|
81
|
-
from_db.transports[0].model.should == "Honda Civic"
|
82
|
-
from_db.transports[0].year.should == 2003
|
83
|
-
from_db.transports[1].license_plate.should == "XYZ9090"
|
84
|
-
from_db.transports[1].max_passengers.should == 51
|
85
|
-
from_db.transports[2].license_plate.should == "HDD3030"
|
86
|
-
from_db.transports[2].icu.should == true
|
87
|
-
end
|
88
|
-
end
|
89
|
-
|
90
|
-
context "Polymorphic Many Embedded" do
|
91
|
-
should "default reader to empty array" do
|
92
|
-
catalog = Catalog.new
|
93
|
-
catalog.medias.should == []
|
94
|
-
end
|
95
|
-
|
96
|
-
should "allow adding to association like it was an array" do
|
97
|
-
catalog = Catalog.new
|
98
|
-
catalog.medias << Video.new
|
99
|
-
catalog.medias.push Video.new
|
100
|
-
catalog.medias.size.should == 2
|
101
|
-
end
|
102
|
-
|
103
|
-
should "store the association" do
|
104
|
-
catalog = Catalog.new
|
105
|
-
catalog.medias = [Video.new("file" => "video.mpg", "length" => 3600)]
|
106
|
-
catalog.save.should be_true
|
107
|
-
|
108
|
-
from_db = Catalog.find(catalog.id)
|
109
|
-
from_db.medias.size.should == 1
|
110
|
-
from_db.medias[0].file.should == "video.mpg"
|
111
|
-
end
|
112
|
-
|
113
|
-
should "store different associations" do
|
114
|
-
catalog = Catalog.new
|
115
|
-
catalog.medias = [
|
116
|
-
Video.new("file" => "video.mpg", "length" => 3600),
|
117
|
-
Music.new("file" => "music.mp3", "bitrate" => "128kbps"),
|
118
|
-
Image.new("file" => "image.png", "width" => 800, "height" => 600)
|
119
|
-
]
|
120
|
-
catalog.save.should be_true
|
121
|
-
|
122
|
-
from_db = Catalog.find(catalog.id)
|
123
|
-
from_db.medias.size.should == 3
|
124
|
-
from_db.medias[0].file.should == "video.mpg"
|
125
|
-
from_db.medias[0].length.should == 3600
|
126
|
-
from_db.medias[1].file.should == "music.mp3"
|
127
|
-
from_db.medias[1].bitrate.should == "128kbps"
|
128
|
-
from_db.medias[2].file.should == "image.png"
|
129
|
-
from_db.medias[2].width.should == 800
|
130
|
-
from_db.medias[2].height.should == 600
|
131
|
-
end
|
132
|
-
end
|
133
|
-
|
134
|
-
context "Polymorphic Belongs To" do
|
135
|
-
should "default to nil" do
|
136
|
-
status = Status.new
|
137
|
-
status.target.should be_nil
|
138
|
-
end
|
139
|
-
|
140
|
-
should "store the association" do
|
141
|
-
status = Status.new
|
142
|
-
project = Project.new(:name => "mongomapper")
|
143
|
-
status.target = project
|
144
|
-
status.save.should be_true
|
145
|
-
|
146
|
-
from_db = Status.find(status.id)
|
147
|
-
from_db.target.should_not be_nil
|
148
|
-
from_db.target_id.should == project.id
|
149
|
-
from_db.target_type.should == "Project"
|
150
|
-
from_db.target.name.should == "mongomapper"
|
151
|
-
end
|
152
|
-
|
153
|
-
should "unset the association" do
|
154
|
-
status = Status.new
|
155
|
-
project = Project.new(:name => "mongomapper")
|
156
|
-
status.target = project
|
157
|
-
status.save.should be_true
|
158
|
-
|
159
|
-
from_db = Status.find(status.id)
|
160
|
-
from_db.target = nil
|
161
|
-
from_db.target_type.should be_nil
|
162
|
-
from_db.target_id.should be_nil
|
163
|
-
from_db.target.should be_nil
|
164
|
-
end
|
165
|
-
end
|
166
|
-
|
167
|
-
context "Belongs To" do
|
168
|
-
should "default to nil" do
|
169
|
-
status = Status.new
|
170
|
-
status.project.should be_nil
|
171
|
-
end
|
172
|
-
|
173
|
-
should "store the association" do
|
174
|
-
status = Status.new
|
175
|
-
project = Project.new(:name => "mongomapper")
|
176
|
-
status.project = project
|
177
|
-
status.save.should be_true
|
178
|
-
|
179
|
-
from_db = Status.find(status.id)
|
180
|
-
from_db.project.should_not be_nil
|
181
|
-
from_db.project.name.should == "mongomapper"
|
182
|
-
end
|
30
|
+
user = AwesomeUser.create
|
31
|
+
tag1 = AwesomeTag.new(:name => 'awesome')
|
32
|
+
tag2 = AwesomeTag.new(:name => 'grand')
|
33
|
+
post1 = AwesomePost.create(:creator => user, :tags => [tag1])
|
34
|
+
post2 = AwesomePost.create(:creator => user, :tags => [tag2])
|
35
|
+
user.posts.should == [post1, post2]
|
183
36
|
|
184
|
-
|
185
|
-
|
186
|
-
project = Project.new(:name => "mongomapper")
|
187
|
-
status.project = project
|
188
|
-
status.save.should be_true
|
189
|
-
|
190
|
-
from_db = Status.find(status.id)
|
191
|
-
from_db.project = nil
|
192
|
-
from_db.project.should be_nil
|
193
|
-
end
|
194
|
-
end
|
195
|
-
|
196
|
-
context "Many documents" do
|
197
|
-
should "default reader to empty array" do
|
198
|
-
project = Project.new
|
199
|
-
project.statuses.should == []
|
200
|
-
end
|
201
|
-
|
202
|
-
should "allow adding to association like it was an array" do
|
203
|
-
project = Project.new
|
204
|
-
project.statuses << Status.new
|
205
|
-
project.statuses.push Status.new
|
206
|
-
project.statuses.size.should == 2
|
207
|
-
end
|
208
|
-
|
209
|
-
should "store the association" do
|
210
|
-
project = Project.new
|
211
|
-
project.statuses = [Status.new("name" => "ready")]
|
212
|
-
project.save.should be_true
|
213
|
-
|
214
|
-
from_db = Project.find(project.id)
|
215
|
-
from_db.statuses.size.should == 1
|
216
|
-
from_db.statuses[0].name.should == "ready"
|
217
|
-
end
|
218
|
-
|
219
|
-
context "Finding scoped to association" do
|
220
|
-
setup do
|
221
|
-
@project1 = Project.new(:name => 'Project 1')
|
222
|
-
@brand_new = Status.create(:name => 'New')
|
223
|
-
@complete = Status.create(:name => 'Complete')
|
224
|
-
@project1.statuses = [@brand_new, @complete]
|
225
|
-
@project1.save
|
226
|
-
|
227
|
-
@project2 = Project.create(:name => 'Project 2')
|
228
|
-
@in_progress = Status.create(:name => 'In Progress')
|
229
|
-
@archived = Status.create(:name => 'Archived')
|
230
|
-
@another_complete = Status.create(:name => 'Complete')
|
231
|
-
@project2.statuses = [@in_progress, @archived, @another_complete]
|
232
|
-
@project2.save
|
233
|
-
end
|
234
|
-
|
235
|
-
context "with :all" do
|
236
|
-
should "work" do
|
237
|
-
@project1.statuses.find(:all).should == [@brand_new, @complete]
|
238
|
-
end
|
239
|
-
|
240
|
-
should "work with conditions" do
|
241
|
-
statuses = @project1.statuses.find(:all, :conditions => {'name' => 'Complete'})
|
242
|
-
statuses.should == [@complete]
|
243
|
-
end
|
244
|
-
|
245
|
-
should "work with order" do
|
246
|
-
statuses = @project1.statuses.find(:all, :order => 'name asc')
|
247
|
-
statuses.should == [@complete, @brand_new]
|
248
|
-
end
|
249
|
-
end
|
250
|
-
|
251
|
-
context "with #all" do
|
252
|
-
should "work" do
|
253
|
-
@project1.statuses.all.should == [@brand_new, @complete]
|
254
|
-
end
|
255
|
-
|
256
|
-
should "work with conditions" do
|
257
|
-
statuses = @project1.statuses.all(:conditions => {'name' => 'Complete'})
|
258
|
-
statuses.should == [@complete]
|
259
|
-
end
|
260
|
-
|
261
|
-
should "work with order" do
|
262
|
-
statuses = @project1.statuses.all(:order => 'name asc')
|
263
|
-
statuses.should == [@complete, @brand_new]
|
264
|
-
end
|
265
|
-
end
|
266
|
-
|
267
|
-
context "with :first" do
|
268
|
-
should "work" do
|
269
|
-
@project1.statuses.find(:first).should == @brand_new
|
270
|
-
end
|
271
|
-
|
272
|
-
should "work with conditions" do
|
273
|
-
status = @project1.statuses.find(:first, :conditions => {:name => 'Complete'})
|
274
|
-
status.should == @complete
|
275
|
-
end
|
276
|
-
end
|
277
|
-
|
278
|
-
context "with #first" do
|
279
|
-
should "work" do
|
280
|
-
@project1.statuses.first.should == @brand_new
|
281
|
-
end
|
282
|
-
|
283
|
-
should "work with conditions" do
|
284
|
-
status = @project1.statuses.first(:conditions => {:name => 'Complete'})
|
285
|
-
status.should == @complete
|
286
|
-
end
|
287
|
-
end
|
288
|
-
|
289
|
-
context "with :last" do
|
290
|
-
should "work" do
|
291
|
-
@project1.statuses.find(:last).should == @complete
|
292
|
-
end
|
293
|
-
|
294
|
-
should "work with conditions" do
|
295
|
-
status = @project1.statuses.find(:last, :conditions => {:name => 'New'})
|
296
|
-
status.should == @brand_new
|
297
|
-
end
|
298
|
-
end
|
299
|
-
|
300
|
-
context "with #last" do
|
301
|
-
should "work" do
|
302
|
-
@project1.statuses.last.should == @complete
|
303
|
-
end
|
304
|
-
|
305
|
-
should "work with conditions" do
|
306
|
-
status = @project1.statuses.last(:conditions => {:name => 'New'})
|
307
|
-
status.should == @brand_new
|
308
|
-
end
|
309
|
-
end
|
310
|
-
|
311
|
-
context "with one id" do
|
312
|
-
should "work for id in association" do
|
313
|
-
@project1.statuses.find(@complete.id).should == @complete
|
314
|
-
end
|
315
|
-
|
316
|
-
should "not work for id not in association" do
|
317
|
-
lambda {
|
318
|
-
@project1.statuses.find(@archived.id)
|
319
|
-
}.should raise_error(MongoMapper::DocumentNotFound)
|
320
|
-
end
|
321
|
-
end
|
322
|
-
|
323
|
-
context "with multiple ids" do
|
324
|
-
should "work for ids in association" do
|
325
|
-
statuses = @project1.statuses.find(@brand_new.id, @complete.id)
|
326
|
-
statuses.should == [@brand_new, @complete]
|
327
|
-
end
|
328
|
-
|
329
|
-
should "not work for ids not in association" do
|
330
|
-
lambda {
|
331
|
-
@project1.statuses.find(@brand_new.id, @complete.id, @archived.id)
|
332
|
-
}.should raise_error(MongoMapper::DocumentNotFound)
|
333
|
-
end
|
334
|
-
end
|
335
|
-
|
336
|
-
context "with #paginate" do
|
337
|
-
setup do
|
338
|
-
@statuses = @project2.statuses.paginate(:per_page => 2, :page => 1)
|
339
|
-
end
|
340
|
-
|
341
|
-
should "return total pages" do
|
342
|
-
@statuses.total_pages.should == 2
|
343
|
-
end
|
344
|
-
|
345
|
-
should "return total entries" do
|
346
|
-
@statuses.total_entries.should == 3
|
347
|
-
end
|
348
|
-
|
349
|
-
should "return the subject" do
|
350
|
-
@statuses.should == [@in_progress, @archived]
|
351
|
-
end
|
352
|
-
end
|
353
|
-
end
|
354
|
-
end
|
355
|
-
|
356
|
-
context "Many embedded documents" do
|
357
|
-
should "allow adding to association like it was an array" do
|
358
|
-
project = Project.new
|
359
|
-
project.addresses << Address.new
|
360
|
-
project.addresses.push Address.new
|
361
|
-
project.addresses.size.should == 2
|
362
|
-
end
|
363
|
-
|
364
|
-
should "be embedded in document on save" do
|
365
|
-
sb = Address.new(:city => 'South Bend', :state => 'IN')
|
366
|
-
chi = Address.new(:city => 'Chicago', :state => 'IL')
|
367
|
-
project = Project.new
|
368
|
-
project.addresses << sb
|
369
|
-
project.addresses << chi
|
370
|
-
project.save
|
371
|
-
|
372
|
-
from_db = Project.find(project.id)
|
373
|
-
from_db.addresses.size.should == 2
|
374
|
-
from_db.addresses[0].should == sb
|
375
|
-
from_db.addresses[1].should == chi
|
376
|
-
end
|
377
|
-
|
378
|
-
should "allow embedding arbitrarily deep" do
|
379
|
-
@document = Class.new do
|
380
|
-
include MongoMapper::Document
|
381
|
-
key :person, Person
|
382
|
-
end
|
383
|
-
@document.collection.clear
|
384
|
-
|
385
|
-
meg = Person.new(:name => "Meg")
|
386
|
-
meg.child = Person.new(:name => "Steve")
|
387
|
-
meg.child.child = Person.new(:name => "Linda")
|
388
|
-
|
389
|
-
doc = @document.new(:person => meg)
|
390
|
-
doc.save
|
391
|
-
|
392
|
-
from_db = @document.find(doc.id)
|
393
|
-
from_db.person.name.should == 'Meg'
|
394
|
-
from_db.person.child.name.should == 'Steve'
|
395
|
-
from_db.person.child.child.name.should == 'Linda'
|
396
|
-
end
|
397
|
-
|
398
|
-
should "allow assignment of 'many' embedded documents using a hash" do
|
399
|
-
person_attributes = {
|
400
|
-
"name" => "Mr. Pet Lover",
|
401
|
-
"pets" => [
|
402
|
-
{"name" => "Jimmy", "species" => "Cocker Spainel"},
|
403
|
-
{"name" => "Sasha", "species" => "Siberian Husky"},
|
404
|
-
]
|
405
|
-
}
|
406
|
-
|
407
|
-
pet_lover = RealPerson.new(person_attributes)
|
408
|
-
pet_lover.name.should == "Mr. Pet Lover"
|
409
|
-
pet_lover.pets[0].name.should == "Jimmy"
|
410
|
-
pet_lover.pets[0].species.should == "Cocker Spainel"
|
411
|
-
pet_lover.pets[1].name.should == "Sasha"
|
412
|
-
pet_lover.pets[1].species.should == "Siberian Husky"
|
413
|
-
pet_lover.save.should be_true
|
414
|
-
|
415
|
-
from_db = RealPerson.find(pet_lover.id)
|
416
|
-
from_db.name.should == "Mr. Pet Lover"
|
417
|
-
from_db.pets[0].name.should == "Jimmy"
|
418
|
-
from_db.pets[0].species.should == "Cocker Spainel"
|
419
|
-
from_db.pets[1].name.should == "Sasha"
|
420
|
-
from_db.pets[1].species.should == "Siberian Husky"
|
421
|
-
end
|
422
|
-
|
423
|
-
should "allow saving embedded documents in 'many' embedded documents" do
|
424
|
-
@document = Class.new do
|
425
|
-
include MongoMapper::Document
|
426
|
-
many :people
|
427
|
-
end
|
428
|
-
@document.collection.clear
|
429
|
-
|
430
|
-
meg = Person.new(:name => "Meg")
|
431
|
-
sparky = Pet.new(:name => "Sparky", :species => "Dog")
|
432
|
-
koda = Pet.new(:name => "Koda", :species => "Dog")
|
433
|
-
|
434
|
-
doc = @document.new
|
435
|
-
|
436
|
-
meg.pets << sparky
|
437
|
-
meg.pets << koda
|
438
|
-
|
439
|
-
doc.people << meg
|
440
|
-
doc.save
|
441
|
-
|
442
|
-
from_db = @document.find(doc.id)
|
443
|
-
from_db.people.first.name.should == "Meg"
|
444
|
-
from_db.people.first.pets.should_not == []
|
445
|
-
from_db.people.first.pets.first.name.should == "Sparky"
|
446
|
-
from_db.people.first.pets.first.species.should == "Dog"
|
447
|
-
from_db.people.first.pets[1].name.should == "Koda"
|
448
|
-
from_db.people.first.pets[1].species.should == "Dog"
|
449
|
-
end
|
450
|
-
end
|
451
|
-
|
452
|
-
context "Changing association class names" do
|
453
|
-
should "work for many and belongs to" do
|
454
|
-
class AwesomeUser
|
455
|
-
include MongoMapper::Document
|
456
|
-
many :posts, :class_name => 'AssociationsTest::AwesomePost', :foreign_key => :creator_id
|
457
|
-
end
|
458
|
-
|
459
|
-
class AwesomeTag
|
460
|
-
include MongoMapper::EmbeddedDocument
|
461
|
-
key :name, String
|
462
|
-
belongs_to :post, :class_name => 'AssociationsTest::AwesomeUser'
|
463
|
-
end
|
464
|
-
|
465
|
-
class AwesomePost
|
466
|
-
include MongoMapper::Document
|
467
|
-
belongs_to :creator, :class_name => 'AssociationsTest::AwesomeUser'
|
468
|
-
many :tags, :class_name => 'AssociationsTest::AwesomeTag', :foreign_key => :post_id
|
469
|
-
end
|
470
|
-
|
471
|
-
AwesomeUser.collection.clear
|
472
|
-
AwesomePost.collection.clear
|
473
|
-
|
474
|
-
user = AwesomeUser.create
|
475
|
-
tag1 = AwesomeTag.new(:name => 'awesome')
|
476
|
-
tag2 = AwesomeTag.new(:name => 'grand')
|
477
|
-
post1 = AwesomePost.create(:creator => user, :tags => [tag1])
|
478
|
-
post2 = AwesomePost.create(:creator => user, :tags => [tag2])
|
479
|
-
user.posts.should == [post1, post2]
|
480
|
-
|
481
|
-
post1_from_db = AwesomePost.find(post1.id)
|
482
|
-
post1_from_db.tags.should == [tag1]
|
483
|
-
end
|
37
|
+
post1_from_db = AwesomePost.find(post1.id)
|
38
|
+
post1_from_db.tags.should == [tag1]
|
484
39
|
end
|
485
40
|
end
|
@@ -1,10 +1,5 @@
|
|
1
1
|
require 'test_helper'
|
2
|
-
|
3
|
-
class Address
|
4
|
-
include MongoMapper::EmbeddedDocument
|
5
|
-
key :city, String
|
6
|
-
key :state, String
|
7
|
-
end
|
2
|
+
require 'models'
|
8
3
|
|
9
4
|
class DocumentTest < Test::Unit::TestCase
|
10
5
|
def setup
|
@@ -25,11 +20,43 @@ class DocumentTest < Test::Unit::TestCase
|
|
25
20
|
setup do
|
26
21
|
@document.key :tags, Array
|
27
22
|
end
|
28
|
-
|
29
|
-
should "
|
23
|
+
|
24
|
+
should "give correct default" do
|
30
25
|
doc = @document.new
|
31
26
|
doc.tags.should == []
|
27
|
+
end
|
28
|
+
|
29
|
+
should "work with assignment" do
|
30
|
+
doc = @document.new
|
32
31
|
doc.tags = %w(foo bar)
|
32
|
+
doc.tags.should == %w(foo bar)
|
33
|
+
end
|
34
|
+
|
35
|
+
should "work with assignment after saving" do
|
36
|
+
doc = @document.new
|
37
|
+
doc.tags = %w(foo bar)
|
38
|
+
doc.save
|
39
|
+
doc.tags.should == %w(foo bar)
|
40
|
+
@document.find(doc.id).tags.should == %w(foo bar)
|
41
|
+
end
|
42
|
+
|
43
|
+
should "work with assignment then <<" do
|
44
|
+
doc = @document.new
|
45
|
+
doc.tags = []
|
46
|
+
doc.tags << "foo"
|
47
|
+
doc.tags.should == ["foo"]
|
48
|
+
end
|
49
|
+
|
50
|
+
should "work with <<" do
|
51
|
+
doc = @document.new
|
52
|
+
doc.tags << "foo"
|
53
|
+
doc.tags.should == ["foo"]
|
54
|
+
end
|
55
|
+
|
56
|
+
should_eventually "work with << then save" do
|
57
|
+
doc = @document.new
|
58
|
+
doc.tags << "foo"
|
59
|
+
doc.tags << "bar"
|
33
60
|
doc.save
|
34
61
|
doc.tags.should == %w(foo bar)
|
35
62
|
@document.find(doc.id).tags.should == %w(foo bar)
|
@@ -40,12 +67,31 @@ class DocumentTest < Test::Unit::TestCase
|
|
40
67
|
setup do
|
41
68
|
@document.key :foo, Hash
|
42
69
|
end
|
43
|
-
|
70
|
+
|
71
|
+
should "give correct default" do
|
72
|
+
doc = @document.new
|
73
|
+
doc.foo.should == {}
|
74
|
+
end
|
75
|
+
|
76
|
+
should "work with []=" do
|
77
|
+
doc = @document.new
|
78
|
+
doc.foo["quux"] = "bar"
|
79
|
+
doc.foo["quux"].should == "bar"
|
80
|
+
doc.foo.should == { "quux" => "bar" }
|
81
|
+
end
|
82
|
+
|
44
83
|
should "work with indifferent access" do
|
84
|
+
doc = @document.new
|
85
|
+
doc.foo = {:baz => 'bar'}
|
86
|
+
doc.foo[:baz].should == 'bar'
|
87
|
+
doc.foo['baz'].should == 'bar'
|
88
|
+
end
|
89
|
+
|
90
|
+
should "work with indifferent access after save" do
|
45
91
|
doc = @document.new
|
46
92
|
doc.foo = {:baz => 'bar'}
|
47
93
|
doc.save
|
48
|
-
|
94
|
+
|
49
95
|
doc = @document.find(doc.id)
|
50
96
|
doc.foo[:baz].should == 'bar'
|
51
97
|
doc.foo['baz'].should == 'bar'
|
@@ -109,7 +155,6 @@ class DocumentTest < Test::Unit::TestCase
|
|
109
155
|
end
|
110
156
|
end
|
111
157
|
|
112
|
-
|
113
158
|
context "Creating multiple documents" do
|
114
159
|
setup do
|
115
160
|
@doc_instances = @document.create([
|
@@ -208,7 +253,7 @@ class DocumentTest < Test::Unit::TestCase
|
|
208
253
|
end
|
209
254
|
|
210
255
|
should "raise error if id is illegal" do
|
211
|
-
lambda { @document.find(1) }.should raise_error(MongoMapper::
|
256
|
+
lambda { @document.find(1) }.should raise_error(MongoMapper::IllegalID)
|
212
257
|
end
|
213
258
|
end
|
214
259
|
|
@@ -224,7 +269,7 @@ class DocumentTest < Test::Unit::TestCase
|
|
224
269
|
|
225
270
|
context "with :all" do
|
226
271
|
should "find all documents" do
|
227
|
-
@document.find(:all).should == [@doc1, @
|
272
|
+
@document.find(:all, :order => 'first_name').should == [@doc1, @doc3, @doc2]
|
228
273
|
end
|
229
274
|
|
230
275
|
should "be able to add conditions" do
|
@@ -234,20 +279,20 @@ class DocumentTest < Test::Unit::TestCase
|
|
234
279
|
|
235
280
|
context "with #all" do
|
236
281
|
should "find all documents based on criteria" do
|
237
|
-
@document.all.should == [@doc1, @
|
282
|
+
@document.all(:order => 'first_name').should == [@doc1, @doc3, @doc2]
|
238
283
|
@document.all(:conditions => {:last_name => 'Nunemaker'}).should == [@doc1, @doc3]
|
239
284
|
end
|
240
285
|
end
|
241
286
|
|
242
287
|
context "with :first" do
|
243
288
|
should "find first document" do
|
244
|
-
@document.find(:first).should == @doc1
|
289
|
+
@document.find(:first, :order => 'first_name').should == @doc1
|
245
290
|
end
|
246
291
|
end
|
247
292
|
|
248
293
|
context "with #first" do
|
249
294
|
should "find first document based on criteria" do
|
250
|
-
@document.first.should == @doc1
|
295
|
+
@document.first(:order => 'first_name').should == @doc1
|
251
296
|
@document.first(:conditions => {:age => 28}).should == @doc2
|
252
297
|
end
|
253
298
|
end
|
@@ -569,12 +614,12 @@ class DocumentTest < Test::Unit::TestCase
|
|
569
614
|
@document.count.should == 1
|
570
615
|
end
|
571
616
|
|
572
|
-
|
617
|
+
should "update attributes" do
|
573
618
|
@doc.first_name.should == 'Johnny'
|
574
619
|
@doc.age.should == 30
|
575
620
|
end
|
576
621
|
|
577
|
-
|
622
|
+
should "update attributes in the database" do
|
578
623
|
from_db = @document.find(@doc.id)
|
579
624
|
from_db.first_name.should == 'Johnny'
|
580
625
|
from_db.age.should == 30
|
@@ -623,7 +668,7 @@ class DocumentTest < Test::Unit::TestCase
|
|
623
668
|
doc.updated_at.should_not be(nil)
|
624
669
|
end
|
625
670
|
|
626
|
-
should "set updated_at on update but leave created_at alone" do
|
671
|
+
should "set updated_at on field update but leave created_at alone" do
|
627
672
|
doc = @document.create(:first_name => 'John', :age => 27)
|
628
673
|
old_created_at = doc.created_at
|
629
674
|
old_updated_at = doc.updated_at
|
@@ -632,5 +677,17 @@ class DocumentTest < Test::Unit::TestCase
|
|
632
677
|
doc.created_at.should == old_created_at
|
633
678
|
doc.updated_at.should_not == old_updated_at
|
634
679
|
end
|
680
|
+
|
681
|
+
should "set updated_at on document update but leave created_at alone" do
|
682
|
+
doc = @document.create(:first_name => 'John', :age => 27)
|
683
|
+
old_created_at = doc.created_at
|
684
|
+
old_updated_at = doc.updated_at
|
685
|
+
sleep 1 # this annoys me
|
686
|
+
@document.update(doc._id, { :first_name => 'Johnny' })
|
687
|
+
|
688
|
+
from_db = @document.find(doc.id)
|
689
|
+
from_db.created_at.to_i.should == old_created_at.to_i
|
690
|
+
from_db.updated_at.to_i.should_not == old_updated_at.to_i
|
691
|
+
end
|
635
692
|
end
|
636
693
|
end
|
@@ -30,10 +30,9 @@ class PaginationTest < Test::Unit::TestCase
|
|
30
30
|
end
|
31
31
|
|
32
32
|
should "return the items" do
|
33
|
-
result = @document.paginate(:per_page => 2, :page => 1)
|
33
|
+
result = @document.paginate(:per_page => 2, :page => 1, :order => 'first_name')
|
34
34
|
result.size.should == 2
|
35
|
-
result.
|
36
|
-
result.should == [@doc1, @doc2]
|
35
|
+
result.should == [@doc1, @doc3]
|
37
36
|
end
|
38
37
|
|
39
38
|
should "accept conditions" do
|