forked from Automattic/mongoose
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdocument.populate.test.js
506 lines (440 loc) · 14.1 KB
/
document.populate.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
/**
* Test dependencies.
*/
var start = require('./common')
, assert = require('assert')
, mongoose = start.mongoose
, utils = require('../lib/utils')
, random = utils.random
, Schema = mongoose.Schema
, ObjectId = Schema.ObjectId
, Document = require('../lib/document')
, DocObjectId = mongoose.Types.ObjectId
/**
* Setup.
*/
/**
* Test Document constructor.
*/
function TestDocument () {
Document.apply(this, arguments);
};
/**
* Inherits from Document.
*/
TestDocument.prototype.__proto__ = Document.prototype;
/**
* Set a dummy schema to simulate compilation.
*/
var em = new Schema({ title: String, body: String });
em.virtual('works').get(function () {
return 'em virtual works'
});
var schema = new Schema({
test : String
, oids : [ObjectId]
, numbers : [Number]
, nested : {
age : Number
, cool : ObjectId
, deep : { x: String }
, path : String
, setr : String
}
, nested2 : {
nested: String
, yup : {
nested : Boolean
, yup : String
, age : Number
}
}
, em: [em]
, date: Date
});
TestDocument.prototype.$__setSchema(schema);
/**
* User schema.
*/
var User = new Schema({
name : String
, email : String
, gender : { type: String, enum: ['male', 'female'], default: 'male' }
, age : { type: Number, default: 21 }
, blogposts : [{ type: ObjectId, ref: 'doc.populate.b' }]
}, { collection: 'doc.populate.us' });
/**
* Comment subdocument schema.
*/
var Comment = new Schema({
asers : [{ type: ObjectId, ref: 'doc.populate.u' }]
, _creator : { type: ObjectId, ref: 'doc.populate.u' }
, content : String
});
/**
* Blog post schema.
*/
var BlogPost = new Schema({
_creator : { type: ObjectId, ref: 'doc.populate.u' }
, title : String
, comments : [Comment]
, fans : [{ type: ObjectId, ref: 'doc.populate.u' }]
});
var posts = 'blogposts_' + random()
, users = 'users_' + random();
mongoose.model('doc.populate.b', BlogPost);
mongoose.model('doc.populate.u', User);
mongoose.model('doc.populate.u2', User);
describe('document.populate', function(){
var db, B, User;
var user1, user2, post, _id;
before(function(done){
db = start();
B = db.model('doc.populate.b');
User = db.model('doc.populate.u');
_id = new mongoose.Types.ObjectId;
User.create({
name : 'Phoenix'
, email : 'phx@az.com'
, blogposts: [_id]
}, {
name : 'Newark'
, email : 'ewr@nj.com'
, blogposts: [_id]
}, function (err, u1, u2) {
assert.ifError(err);
user1 = u1;
user2 = u2;
B.create({
title : 'the how and why'
, _creator : user1
, fans: [user1, user2]
, comments: [{ _creator: user2, content: 'user2' }, { _creator: user1, content: 'user1' }]
}, function (err, p) {
assert.ifError(err);
post = p
done();
});
});
});
after(function(done){
db.close(done);
})
describe('argument processing', function(){
describe('duplicates', function(){
it('are removed', function(done){
B.findById(post, function (err, post) {
assert.ifError(err);
post.populate('_creator');
assert.equal(1, Object.keys(post.$__.populate).length);
assert.ok('_creator' in post.$__.populate);
post.populate('_creator');
assert.equal(1, Object.keys(post.$__.populate).length);
assert.ok('_creator' in post.$__.populate);
post.populate('_creator fans');
assert.equal(2, Object.keys(post.$__.populate).length);
assert.ok('_creator' in post.$__.populate);
assert.ok('fans' in post.$__.populate);
post.populate({ path: '_creator' });
assert.equal(2, Object.keys(post.$__.populate).length);
assert.ok('_creator' in post.$__.populate);
assert.ok('fans' in post.$__.populate);
done();
})
})
it('overwrite previous', function(done){
B.findById(post, function (err, post) {
assert.ifError(err);
post.populate('_creator');
assert.equal(1, Object.keys(post.$__.populate).length);
assert.equal(undefined, post.$__.populate._creator.select);
post.populate({ path: '_creator', select: 'name' });
assert.equal(1, Object.keys(post.$__.populate).length);
assert.ok('_creator' in post.$__.populate);
assert.equal('name', post.$__.populate._creator.select);
done();
})
})
})
})
describe('options', function(){
it('resets populate options after execution', function(done){
B.findById(post, function (err, post) {
var creator_id = post._creator;
post.populate('_creator', function (err, post_) {
assert.ifError(err);
assert.ok(!post.$__.populate);
assert.ok(post._creator);
assert.equal(String(creator_id), String(post._creator._id));
done();
});
});
})
it('are not modified when no arguments are passed', function(done){
var d = new TestDocument();
var o = utils.clone(d.options);
assert.deepEqual(o, d.populate().options);
done();
})
})
describe('populating two paths', function(){
it('with space delmited string works', function(done){
B.findById(post, function (err, post) {
var creator_id = post._creator;
var alt_id = post.fans[1];
post.populate('_creator fans', function (err, post_) {
assert.ifError(err);
assert.ok(post._creator);
assert.equal(String(creator_id), String(post._creator._id));
assert.equal(String(creator_id), String(post.fans[0]._id));
assert.equal(String(alt_id), String(post.fans[1]._id));
done();
});
});
})
})
it('works with just a callback', function(done){
B.findById(post, function (err, post) {
var creator_id = post._creator;
var alt_id = post.fans[1];
post.populate('_creator').populate(function (err, post_) {
assert.ifError(err);
assert.ok(post._creator);
assert.equal(String(creator_id), String(post._creator._id));
assert.equal(String(alt_id), String(post.fans[1]));
done();
});
})
})
it('populating using space delimited paths with options', function(done){
B.findById(post, function (err, post) {
var param = {};
param.select = '-email';
param.options = { sort: 'name' }
param.path = '_creator fans'; // 2 paths
var creator_id = post._creator;
var alt_id = post.fans[1];
post.populate(param, function (err, post) {
assert.ifError(err);
assert.equal(2, post.fans.length);
assert.equal(String(creator_id), String(post._creator._id));
assert.equal(String(creator_id), String(post.fans[1]._id));
assert.equal(String(alt_id), String(post.fans[0]._id));
assert.ok(!post.fans[0].email);
assert.ok(!post.fans[1].email);
assert.ok(!post.fans[0].isInit('email'));
assert.ok(!post.fans[1].isInit('email'));
done();
});
});
});
it('using multiple populate calls', function(done){
B.findById(post, function (err, post) {
var creator_id = post._creator;
var alt_id = post.fans[1];
var param = {};
param.select = '-email';
param.options = { sort: 'name' }
param.path = '_creator';
post.populate(param);
param.path = 'fans';
post.populate(param, function (err, post) {
assert.ifError(err);
assert.equal(2, post.fans.length);
assert.equal(String(creator_id), String(post._creator._id));
assert.equal(String(creator_id), String(post.fans[1]._id));
assert.equal(String(alt_id), String(post.fans[0]._id));
assert.ok(!post.fans[0].email);
assert.ok(!post.fans[1].email);
assert.ok(!post.fans[0].isInit('email'));
assert.ok(!post.fans[1].isInit('email'));
done();
});
});
});
it('with custom model selection', function(done){
B.findById(post, function (err, post) {
var param = {};
param.select = '-email';
param.options = { sort: 'name' }
param.path = '_creator fans';
param.model = 'doc.populate.u2';
var creator_id = post._creator;
var alt_id = post.fans[1];
post.populate(param, function (err, post) {
assert.ifError(err);
assert.equal(2, post.fans.length);
assert.equal(String(creator_id), String(post._creator._id));
assert.equal(String(creator_id), String(post.fans[1]._id));
assert.equal(String(alt_id), String(post.fans[0]._id));
assert.ok(!post.fans[0].email);
assert.ok(!post.fans[1].email);
assert.ok(!post.fans[0].isInit('email'));
assert.ok(!post.fans[1].isInit('email'));
done();
});
});
});
it('a property not in schema', function(done){
B.findById(post, function (err, post) {
assert.ifError(err);
post.populate('idontexist', function (err) {
assert.ifError(err);
// stuff an ad-hoc value in
post.setValue('idontexist', user1._id);
// populate the non-schema value by passing an explicit model
post.populate({ path: 'idontexist', model: 'doc.populate.u' }, function (err, post) {
assert.ifError(err);
assert.ok(post);
assert.equal(post.get('idontexist')._id, user1._id.toString());
assert.equal(post.get('idontexist').name, 'Phoenix');
done();
});
});
});
})
it('of empty array', function(done){
B.findById(post, function (err, post) {
post.fans = []
post.populate('fans', function (err, post) {
assert.ifError(err);
done();
});
});
})
it('of array of null/undefined', function(done){
B.findById(post, function (err, post) {
post.fans = [null, undefined]
post.populate('fans', function (err, post) {
assert.ifError(err);
done();
});
});
})
it('of null property', function(done){
B.findById(post, function (err, post) {
post._creator = null;
post.populate('_creator', function (err, post) {
assert.ifError(err);
done();
});
});
})
it('String _ids', function(done){
var db = start();
var UserSchema = new Schema({
_id: String
, name: String
})
var NoteSchema = new Schema({
author: { type: String, ref: 'UserWithStringId' }
, body: String
})
var User = db.model('UserWithStringId', UserSchema, random())
var Note = db.model('NoteWithStringId', NoteSchema, random())
var alice = new User({_id: 'alice', name: "Alice In Wonderland"})
alice.save(function (err) {
assert.ifError(err);
var note = new Note({ author: 'alice', body: "Buy Milk" });
note.populate('author', function (err, note_) {
db.close();
assert.ifError(err);
assert.ok(note.author);
assert.equal('alice', note.author._id);
assert.equal(note.author.name, 'Alice In Wonderland');
done();
});
})
})
it('Buffer _ids', function(done){
var db = start();
var UserSchema = new Schema({
_id: Buffer
, name: String
})
var NoteSchema = new Schema({
author: { type: Buffer, ref: 'UserWithBufferId' }
, body: String
})
var User = db.model('UserWithBufferId', UserSchema, random())
var Note = db.model('NoteWithBufferId', NoteSchema, random())
var alice = new User({_id: new mongoose.Types.Buffer('YWxpY2U=', 'base64'), name: "Alice"})
alice.save(function (err) {
assert.ifError(err);
var note = new Note({author: 'alice', body: "Buy Milk"});
note.save(function (err) {
assert.ifError(err);
Note.findById(note.id, function (err, note) {
assert.ifError(err);
assert.equal('alice', note.author);
note.populate('author', function (err, note) {
db.close();
assert.ifError(err);
assert.equal(note.body,'Buy Milk');
assert.ok(note.author);
assert.equal(note.author.name,'Alice');
done();
});
});
});
})
})
it('Number _ids', function(done){
var db = start();
var UserSchema = new Schema({
_id: Number
, name: String
})
var NoteSchema = new Schema({
author: { type: Number, ref: 'UserWithNumberId' }
, body: String
})
var User = db.model('UserWithNumberId', UserSchema, random())
var Note = db.model('NoteWithNumberId', NoteSchema, random())
var alice = new User({_id: 2359, name: "Alice"})
alice.save(function (err) {
assert.ifError(err);
var note = new Note({author: 2359, body: "Buy Milk"});
note.populate('author').populate(function (err, note) {
db.close();
assert.ifError(err);
assert.ok(note.author);
assert.equal(2359, note.author._id);
assert.equal(note.author.name,'Alice');
done();
});
})
})
describe('sub-level properties', function(){
it('with string arg', function(done){
B.findById(post, function (err, post) {
var id0 = post.comments[0]._creator;
var id1 = post.comments[1]._creator;
post.populate('comments._creator', function (err, post) {
assert.ifError(err);
assert.equal(2, post.comments.length);
assert.equal(id0, post.comments[0]._creator.id);
assert.equal(id1, post.comments[1]._creator.id);
done();
})
})
})
})
describe('of new document', function(){
it('should save just the populated _id (gh-1442)', function(done){
var b = new B({ _creator: user1 });
b.populate('_creator', function (err, b) {
if (err) return done(err);
assert.equal('Phoenix', b._creator.name);
b.save(function (err) {
assert.ifError(err);
B.collection.findOne({ _id: b._id }, function (err, b) {
assert.ifError(err);
assert.equal(b._creator, String(user1._id));
done();
})
})
})
})
})
});