forked from Automattic/mongoose
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.populate.setting.test.js
326 lines (272 loc) · 11.3 KB
/
model.populate.setting.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
/* eslint no-dupe-keys: 1 */
/**
* Test dependencies.
*/
'use strict';
let start = require('./common'), assert = require('power-assert'), mongoose = start.mongoose, utils = require('../lib/utils'), random = utils.random, Schema = mongoose.Schema, DocObjectId = mongoose.Types.ObjectId, Buffer = require('safe-buffer').Buffer;
/**
* Setup.
*/
let posts = 'blogposts_' + random(),
users = 'users_' + random();
/**
* Tests.
*/
describe('model: populate:', function() {
this.timeout(process.env.TRAVIS ? 8000 : 4500);
describe('setting populated paths (gh-570)', function() {
const types = {
ObjectId: DocObjectId,
String: String,
Number: Number,
Buffer: Buffer
};
const construct = {};
construct.String = random;
construct.ObjectId = DocObjectId;
construct.Number = random;
construct.Buffer = function() {
return Buffer.from(random());
};
Object.keys(types).forEach(function(id) {
describe('should not cast to _id of type ' + id, function() {
let refuser;
let db;
let B, U;
let u1;
let b1, b2;
before(function(done) {
refuser = 'RefUser-' + id;
const bSchema = new Schema({
title: String,
fans: [{type: id, ref: refuser}],
adhoc: [{subdoc: id, subarray: [{things: [id]}]}],
_creator: {type: id, ref: refuser},
embed: [{
other: {type: id, ref: refuser},
array: [{type: id, ref: refuser}],
nested: [{subdoc: {type: id, ref: refuser}}]
}]
});
const uSchema = new Schema({
_id: id,
name: String,
email: String
});
db = start();
B = db.model('RefBlogPost-' + id, bSchema, posts + random());
U = db.model(refuser, uSchema, users + random());
U.create({
_id: construct[id](),
name: 'Fan 1',
email: 'fan1@learnboost.com'
}, {
_id: construct[id](),
name: 'Fan 2',
email: 'fan2@learnboost.com'
}, function(err, fan1, fan2) {
assert.ifError(err);
u1 = fan1;
B.create({
title: 'Woot',
fans: [fan1, fan2],
adhoc: [{subdoc: fan2, subarray: [{things: [fan1]}]}],
_creator: fan1,
embed: [{other: fan1, array: [fan1, fan2]}, {other: fan2, array: [fan2, fan1]}]
}, {
title: 'Woot2',
fans: [fan2, fan1],
adhoc: [{subdoc: fan1, subarray: [{things: [fan2]}]}],
_creator: fan2,
embed: [{other: fan2, array: [fan2, fan1]}, {other: fan1, array: [fan1, fan2]}]
}, function(err, post1, post2) {
assert.ifError(err);
b1 = post1;
b2 = post2;
done();
});
});
});
after(function(done) {
db.close(done);
});
function userLiteral(name) {
return {_id: construct[id](), name: name};
}
function user(name) {
return new U(userLiteral(name));
}
it('if a document', function(done) {
const query = B.findById(b1).
populate('fans _creator embed.other embed.array embed.nested.subdoc').
populate({path: 'adhoc.subdoc', model: refuser}).
populate({path: 'adhoc.subarray.things', model: refuser});
query.exec(function(err, doc) {
assert.ifError(err);
const user3 = user('user3');
doc.fans.push(user3);
assert.deepEqual(doc.fans[2].toObject(), user3.toObject());
const user4 = user('user4');
doc.fans.nonAtomicPush(user4);
assert.deepEqual(doc.fans[3].toObject(), user4.toObject());
const user5 = user('user5');
doc.fans.splice(2, 1, user5);
assert.deepEqual(doc.fans[2].toObject(), user5.toObject());
const user6 = user('user6');
doc.fans.unshift(user6);
assert.deepEqual(doc.fans[0].toObject(), user6.toObject());
const user7 = user('user7');
doc.fans.addToSet(user7);
assert.deepEqual(doc.fans[5].toObject(), user7.toObject());
doc.fans.forEach(function(doc) {
assert.ok(doc instanceof U);
});
const user8 = user('user8');
doc.fans.set(0, user8);
assert.deepEqual(doc.fans[0].toObject(), user8.toObject());
doc.fans.push(null);
assert.equal(doc.fans[6], null);
const _id = construct[id]();
doc.fans.addToSet(_id);
if (Buffer.isBuffer(_id)) {
assert.equal(doc.fans[7]._id.toString('utf8'), _id.toString('utf8'));
} else {
assert.equal(doc.fans[7]._id, String(_id));
}
assert.equal(doc._creator.email, u1.email);
doc._creator = null;
assert.equal(doc._creator, null);
const creator = user('creator');
doc._creator = creator;
assert.ok(doc._creator instanceof mongoose.Document);
assert.deepEqual(doc._creator.toObject(), creator.toObject());
// embedded with declared ref in schema
const user1a = user('user1a');
doc.embed[0].array.set(0, user1a);
assert.deepEqual(doc.embed[0].array[0].toObject(), user1a.toObject());
const user1b = user('user1b');
doc.embed[0].other = user1b;
assert.deepEqual(doc.embed[0].other.toObject(), user1b.toObject());
const user1c = user('user2c');
doc.embed[0].nested = [{subdoc: user1c}];
assert.deepEqual(doc.embed[0].nested[0].subdoc.toObject(), user1c.toObject());
// embedded without declared ref in schema
const user2a = user('user2a');
doc.adhoc[0].subdoc = user2a;
assert.deepEqual(doc.adhoc[0].subdoc.toObject(), user2a.toObject());
const user2b = user('user2b');
doc.adhoc[0].subarray[0].things.push(user2b);
assert.deepEqual(doc.adhoc[0].subarray[0].things[1].toObject(), user2b.toObject());
doc.save(function(err) {
assert.ifError(err);
B.findById(b1).exec(function(err, doc) {
// db is closed in after()
assert.ifError(err);
assert.equal(doc.fans.length, 8);
assert.equal(doc.fans[0], user8.id);
assert.equal(doc.fans[5], user7.id);
assert.equal(doc.fans[6], null);
assert.equal(doc.fans[7], String(_id));
assert.equal(String(doc._creator), creator._id);
assert.equal(doc.embed[0].array[0], user1a.id);
assert.equal(doc.embed[0].other, user1b.id);
assert.equal(doc.embed[0].nested[0].subdoc, user1c.id);
assert.equal(doc.adhoc[0].subdoc, user2a.id);
assert.equal(doc.adhoc[0].subarray[0].things[1], user2b.id);
done();
});
});
});
});
it('if an object', function(done) {
B.findById(b2)
.populate('fans _creator embed.other embed.array embed.nested.subdoc')
.populate({path: 'adhoc.subdoc', model: refuser})
.populate({path: 'adhoc.subarray.things', model: refuser})
.exec(function(err, doc) {
assert.ifError(err);
let name = 'fan1';
doc.fans.push(userLiteral(name));
assert.ok(doc.fans[2]._id);
assert.equal(doc.fans[2].name, name);
name = 'fan2';
doc.fans.nonAtomicPush(userLiteral(name));
assert.ok(doc.fans[3]._id);
assert.equal(doc.fans[3].name, name);
name = 'fan3';
doc.fans.splice(2, 1, userLiteral(name));
assert.ok(doc.fans[2]._id);
assert.equal(doc.fans[2].name, name);
name = 'fan4';
doc.fans.unshift(userLiteral(name));
assert.ok(doc.fans[0]._id);
assert.equal(doc.fans[0].name, name);
name = 'fan5';
doc.fans.addToSet(userLiteral(name));
assert.ok(doc.fans[5]._id);
assert.equal(doc.fans[5].name, name);
name = 'fan6';
doc.fans.set(0, userLiteral(name));
assert.ok(doc.fans[0]._id);
assert.equal(doc.fans[0].name, name);
doc.fans.forEach(function(doc) {
assert.ok(doc instanceof U);
});
name = 'creator';
const creator = userLiteral(name);
doc._creator = creator;
const creatorId = doc._creator._id;
assert.ok(creatorId);
assert.equal(doc._creator.name, name);
assert.ok(doc._creator instanceof U);
const fan2Id = doc.fans[2]._id;
const fan5Id = doc.fans[5]._id;
name = 'user1a';
const user1a = userLiteral(name);
doc.embed[0].array.set(0, user1a);
assert.equal(doc.embed[0].array[0].name, name);
const user1aId = doc.embed[0].array[0]._id;
name = 'user1b';
const user1b = userLiteral(name);
doc.embed[0].other = user1b;
assert.equal(doc.embed[0].other.name, name);
const user1bId = doc.embed[0].other._id;
name = 'user1c';
const user1c = userLiteral(name);
doc.embed[0].nested = [{subdoc: user1c}];
assert.equal(doc.embed[0].nested[0].subdoc.name, name);
const user1cId = doc.embed[0].nested[0].subdoc._id;
// embedded without declared ref in schema
name = 'user2a';
const user2a = userLiteral(name);
doc.adhoc[0].subdoc = user2a;
assert.equal(doc.adhoc[0].subdoc.name, name);
const user2aId = doc.adhoc[0].subdoc._id;
name = 'user2b';
const user2b = userLiteral(name);
doc.adhoc[0].subarray[0].things.push(user2b);
assert.deepEqual(name, doc.adhoc[0].subarray[0].things[1].name);
const user2bId = doc.adhoc[0].subarray[0].things[1]._id;
doc.save(function(err) {
assert.ifError(err);
B.findById(b2).exec(function(err, doc) {
// db is closed in after()
assert.ifError(err);
assert.equal(doc.fans.length, 6);
assert.equal(String(doc._creator), creatorId);
assert.equal(doc.fans[2], String(fan2Id));
assert.equal(doc.fans[5], String(fan5Id));
assert.equal(doc.embed[0].array[0], String(user1aId));
assert.equal(doc.embed[0].other, String(user1bId));
assert.equal(doc.embed[0].nested[0].subdoc, String(user1cId));
assert.equal(doc.adhoc[0].subdoc, String(user2aId));
assert.equal(doc.adhoc[0].subarray[0].things[1], String(user2bId));
done();
});
});
});
});
});
});
});
});