-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
model.findOneAndReplace.test.js
460 lines (371 loc) · 12.4 KB
/
model.findOneAndReplace.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
'use strict';
/**
* Test dependencies.
*/
const start = require('./common');
const assert = require('assert');
const co = require('co');
const random = require('../lib/utils').random;
const mongoose = start.mongoose;
const Schema = mongoose.Schema;
const ObjectId = Schema.Types.ObjectId;
const DocumentObjectId = mongoose.Types.ObjectId;
describe('model: findOneAndReplace:', function() {
let Comments;
let BlogPost;
let modelname;
let collection;
let strictSchema;
let db;
before(function() {
Comments = new Schema;
Comments.add({
title: String,
date: Date,
body: String,
comments: [Comments]
});
BlogPost = new Schema({
title: String,
author: String,
slug: String,
date: Date,
meta: {
date: Date,
visitors: Number
},
published: Boolean,
mixed: {},
numbers: [Number],
owners: [ObjectId],
comments: [Comments]
});
BlogPost.virtual('titleWithAuthor')
.get(function() {
return this.get('title') + ' by ' + this.get('author');
})
.set(function(val) {
const split = val.split(' by ');
this.set('title', split[0]);
this.set('author', split[1]);
});
BlogPost.method('cool', function() {
return this;
});
BlogPost.static('woot', function() {
return this;
});
modelname = 'ReplaceOneBlogPost';
mongoose.model(modelname, BlogPost);
collection = 'replaceoneblogposts';
strictSchema = new Schema({name: String}, {strict: true});
mongoose.model('ReplaceOneStrictSchema', strictSchema);
db = start();
});
after(function(done) {
db.close(done);
});
it('returns the original document', function() {
const M = db.model(modelname, collection);
const title = 'remove muah';
const post = new M({title: title});
return co(function*() {
yield post.save();
const doc = yield M.findOneAndReplace({title: title});
assert.equal(post.id, doc.id);
});
});
it('options/conditions/doc are merged when no callback is passed', function(done) {
const M = db.model(modelname, collection);
const now = new Date;
let query;
// Model.findOneAndReplace
query = M.findOneAndReplace({author: 'aaron'}, {}, {select: 'author'});
assert.equal(query._fields.author, 1);
assert.equal(query._conditions.author, 'aaron');
query = M.findOneAndReplace({author: 'aaron'});
assert.equal(query._fields, undefined);
assert.equal(query._conditions.author, 'aaron');
query = M.findOneAndReplace();
assert.equal(query.options.new, undefined);
assert.equal(query._fields, undefined);
assert.equal(query._conditions.author, undefined);
// Query.findOneAndReplace
query = M.where('author', 'aaron').findOneAndReplace({date: now});
assert.equal(query._fields, undefined);
assert.equal(query._conditions.date, now);
assert.equal(query._conditions.author, 'aaron');
query = M.find().findOneAndReplace({author: 'aaron'}, {}, {select: 'author'});
assert.equal(query._fields.author, 1);
assert.equal(query._conditions.author, 'aaron');
query = M.find().findOneAndReplace();
assert.equal(query._fields, undefined);
assert.equal(query._conditions.author, undefined);
done();
});
it('executes when a callback is passed', function(done) {
const M = db.model(modelname, collection + random());
let pending = 5;
M.findOneAndReplace({name: 'aaron1'}, {select: 'name'}, cb);
M.findOneAndReplace({name: 'aaron1'}, cb);
M.where().findOneAndReplace({name: 'aaron1'}, {select: 'name'}, cb);
M.where().findOneAndReplace({name: 'aaron1'}, cb);
M.where('name', 'aaron1').findOneAndReplace(cb);
function cb(err, doc) {
assert.ifError(err);
assert.equal(doc, null); // no previously existing doc
if (--pending) return;
done();
}
});
it('executed with only a callback throws', function(done) {
const M = db.model(modelname, collection);
let err;
try {
M.findOneAndReplace(function() {});
} catch (e) {
err = e;
}
assert.ok(/First argument must not be a function/.test(err));
done();
});
it('executed with only a callback throws', function(done) {
const M = db.model(modelname, collection);
let err;
try {
M.findByIdAndDelete(function() {});
} catch (e) {
err = e;
}
assert.ok(/First argument must not be a function/.test(err));
done();
});
it('executes when a callback is passed', function(done) {
const M = db.model(modelname, collection + random());
const _id = new DocumentObjectId;
let pending = 2;
M.findByIdAndDelete(_id, {select: 'name'}, cb);
M.findByIdAndDelete(_id, cb);
function cb(err, doc) {
assert.ifError(err);
assert.equal(doc, null); // no previously existing doc
if (--pending) return;
done();
}
});
it('returns the original document', function(done) {
const M = db.model(modelname, collection);
const title = 'remove muah pleez';
const post = new M({title: title});
post.save(function(err) {
assert.ifError(err);
M.findByIdAndDelete(post.id, function(err, doc) {
assert.ifError(err);
assert.equal(post.id, doc.id);
M.findById(post.id, function(err, gone) {
assert.ifError(err);
assert.equal(gone, null);
done();
});
});
});
});
it('options/conditions/doc are merged when no callback is passed', function(done) {
const M = db.model(modelname, collection);
const _id = new DocumentObjectId();
let query;
// Model.findByIdAndDelete
query = M.findByIdAndDelete(_id, {select: 'author'});
assert.equal(query._fields.author, 1);
assert.equal(query._conditions._id.toString(), _id.toString());
query = M.findByIdAndDelete(_id.toString());
assert.equal(query._fields, undefined);
assert.equal(query._conditions._id, _id.toString());
query = M.findByIdAndDelete();
assert.equal(query.options.new, undefined);
assert.equal(query._fields, undefined);
assert.equal(query._conditions._id, undefined);
done();
});
it('supports v3 select string syntax', function(done) {
const M = db.model(modelname, collection);
const query = M.findOneAndReplace({}, {}, {select: 'author -title'});
assert.strictEqual(1, query._fields.author);
assert.strictEqual(0, query._fields.title);
done();
});
it('supports v3 select object syntax', function(done) {
const M = db.model(modelname, collection);
const query = M.findOneAndReplace({}, {}, {select: {author: 1, title: 0}});
assert.strictEqual(1, query._fields.author);
assert.strictEqual(0, query._fields.title);
done();
});
it('supports v3 sort string syntax', function(done) {
const M = db.model(modelname, collection);
const query = M.findOneAndReplace({}, {}, {sort: 'author -title'});
assert.equal(Object.keys(query.options.sort).length, 2);
assert.equal(query.options.sort.author, 1);
assert.equal(query.options.sort.title, -1);
done();
});
it('supports v3 sort object syntax', function(done) {
const M = db.model(modelname, collection);
const query = M.findOneAndReplace({}, {}, {sort: {author: 1, title: -1}});
assert.equal(Object.keys(query.options.sort).length, 2);
assert.equal(query.options.sort.author, 1);
assert.equal(query.options.sort.title, -1);
done();
});
it('supports population (gh-1395)', function(done) {
const M = db.model('A', {name: String});
const N = db.model('B', {a: {type: Schema.ObjectId, ref: 'A'}, i: Number});
M.create({name: 'i am an A'}, function(err, a) {
if (err) return done(err);
N.create({a: a._id, i: 10}, function(err, b) {
if (err) return done(err);
N.findOneAndReplace({_id: b._id}, {a: a._id})
.populate('a')
.exec(function(err, doc) {
if (err) return done(err);
assert.ok(doc);
assert.ok(doc.a);
assert.equal(doc.a.name, 'i am an A');
done();
});
});
});
});
it('only calls setters once (gh-6203)', function() {
return co(function*() {
const calls = [];
const userSchema = new mongoose.Schema({
name: String,
foo: {
type: String,
set: function(val) {
calls.push(val);
return val + val;
}
}
});
const Model = db.model('gh6203', userSchema);
yield Model.findOneAndReplace({ foo: '123' }, { name: 'bar' });
assert.deepEqual(calls, ['123']);
});
});
describe('middleware', function() {
it('works', function(done) {
const s = new Schema({
topping: {type: String, default: 'bacon'},
base: String
});
let preCount = 0;
s.pre('findOneAndReplace', function() {
++preCount;
});
let postCount = 0;
s.post('findOneAndReplace', function() {
++postCount;
});
const Breakfast = db.model('gh-439', s);
const breakfast = new Breakfast({
base: 'eggs'
});
breakfast.save(function(error) {
assert.ifError(error);
Breakfast.findOneAndReplace(
{base: 'eggs'},
{},
function(error, breakfast) {
assert.ifError(error);
assert.equal(breakfast.base, 'eggs');
assert.equal(preCount, 1);
assert.equal(postCount, 1);
done();
});
});
});
it('works with exec() (gh-439)', function(done) {
const s = new Schema({
topping: {type: String, default: 'bacon'},
base: String
});
let preCount = 0;
s.pre('findOneAndReplace', function() {
++preCount;
});
let postCount = 0;
s.post('findOneAndReplace', function() {
++postCount;
});
const Breakfast = db.model('Breakfast', s);
const breakfast = new Breakfast({
base: 'eggs'
});
breakfast.save(function(error) {
assert.ifError(error);
Breakfast.
findOneAndReplace({base: 'eggs'}, {}).
exec(function(error, breakfast) {
assert.ifError(error);
assert.equal(breakfast.base, 'eggs');
assert.equal(preCount, 1);
assert.equal(postCount, 1);
done();
});
});
});
});
it('works (gh-7654)', function() {
const schema = new Schema({ name: String, age: Number });
const Model = db.model('gh7654', schema);
return co(function*() {
yield Model.findOneAndReplace({}, { name: 'Jean-Luc Picard', age: 59 }, { upsert: true });
const doc = yield Model.findOne();
assert.equal(doc.name, 'Jean-Luc Picard');
const err = yield Model.findOneAndReplace({}, { age: 'not a number' }, {}).
then(() => null, err => err);
assert.ok(err);
assert.ok(err.errors['age'].message.indexOf('not a number') !== -1,
err.errors['age'].message);
});
});
it('schema-level projection (gh-7654)', function() {
const schema = new Schema({ name: String, age: { type: Number, select: false } });
const Model = db.model('gh7654_0', schema);
return co(function*() {
const doc = yield Model.findOneAndReplace({}, { name: 'Jean-Luc Picard', age: 59 }, {
upsert: true,
returnOriginal: false
});
assert.ok(!doc.age);
});
});
it('supports `new` in addition to `returnOriginal` (gh-7846)', function() {
const schema = new Schema({ name: String, age: Number });
const Model = db.model('gh7846', schema);
return co(function*() {
const doc = yield Model.findOneAndReplace({}, { name: 'Jean-Luc Picard', age: 59 }, {
upsert: true,
new: true
});
assert.equal(doc.age, 59);
});
});
it('orFail() (gh-8030)', function() {
const schema = Schema({ name: String, age: Number });
const Model = db.model('gh8030', schema);
return co(function*() {
let err = yield Model.findOneAndReplace({}, { name: 'test' }).orFail().
then(() => assert.ok(false), err => err);
assert.ok(err);
assert.equal(err.name, 'DocumentNotFoundError');
yield Model.create({ name: 'test' });
err = yield Model.findOneAndReplace({ name: 'test' }, { name: 'test2' }).
orFail().
then(() => null, err => err);
assert.ifError(err);
});
});
});