Skip to content

Commit d1ffe7c

Browse files
committedAug 29, 2021
refactor more tests to async/await
1 parent 48badcd commit d1ffe7c

12 files changed

+324
-379
lines changed
 

‎test/docs/date.test.js

+18-20
Original file line numberDiff line numberDiff line change
@@ -86,27 +86,25 @@ describe('Date Tutorial', function() {
8686
let Episode;
8787
let db;
8888

89-
before(function() {
90-
return co(function*() {
91-
const episodeSchema = new mongoose.Schema({
92-
title: String,
93-
airedAt: {
94-
type: Date,
95-
// The dates of the first and last episodes of
96-
// Star Trek: The Next Generation
97-
min: '1987-09-28',
98-
max: '1994-05-23'
99-
}
100-
});
101-
db = yield start().asPromise();
102-
Episode = db.model('Episode', episodeSchema);
103-
104-
yield Episode.create([
105-
{ title: 'Encounter at Farpoint', airedAt: '1987-09-28' },
106-
{ title: 'The Last Outpost', airedAt: '1987-10-19' },
107-
{ title: 'Where No One Has Gone Before', airedAt: '1987-10-26' }
108-
]);
89+
before(async function() {
90+
const episodeSchema = new mongoose.Schema({
91+
title: String,
92+
airedAt: {
93+
type: Date,
94+
// The dates of the first and last episodes of
95+
// Star Trek: The Next Generation
96+
min: '1987-09-28',
97+
max: '1994-05-23'
98+
}
10999
});
100+
db = await start().asPromise();
101+
Episode = db.model('Episode', episodeSchema);
102+
103+
await Episode.create([
104+
{ title: 'Encounter at Farpoint', airedAt: '1987-09-28' },
105+
{ title: 'The Last Outpost', airedAt: '1987-10-19' },
106+
{ title: 'Where No One Has Gone Before', airedAt: '1987-10-26' }
107+
]);
110108
});
111109

112110
it('date queries', function() {

‎test/model.field.selection.test.js

+52-57
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
const start = require('./common');
88

99
const assert = require('assert');
10-
const co = require('co');
1110

1211
const mongoose = start.mongoose;
1312
const Schema = mongoose.Schema;
@@ -446,7 +445,7 @@ describe('model field selection', function() {
446445
});
447446
});
448447

449-
it('sets defaults correctly in child docs with projection (gh-7159)', function() {
448+
it('sets defaults correctly in child docs with projection (gh-7159)', async function() {
450449
const CalendarSchema = new Schema({
451450
dateFormat: {
452451
type: String,
@@ -476,85 +475,81 @@ describe('model field selection', function() {
476475
db.deleteModel(/BlogPost/);
477476
const BlogPost = db.model('BlogPost', BlogPostSchema);
478477

479-
return co(function*() {
480-
yield BlogPost.create({
481-
author: 'me',
482-
settings: {
483-
calendar: {
484-
dateFormat: '1234'
485-
}
478+
479+
await BlogPost.create({
480+
author: 'me',
481+
settings: {
482+
calendar: {
483+
dateFormat: '1234'
486484
}
487-
});
485+
}
486+
});
488487

489-
yield BlogPost.updateOne({}, { $unset: { 'settings.calendar.locale': 1 } });
488+
await BlogPost.updateOne({}, { $unset: { 'settings.calendar.locale': 1 } });
490489

491-
let doc = yield BlogPost.findOne();
492-
assert.strictEqual(doc.settings.calendar.locale, 'en-gb');
493-
assert.strictEqual(doc.settings.calendar.dateFormat, '1234');
490+
let doc = await BlogPost.findOne();
491+
assert.strictEqual(doc.settings.calendar.locale, 'en-gb');
492+
assert.strictEqual(doc.settings.calendar.dateFormat, '1234');
494493

495-
doc = yield BlogPost.findOne().select('settings author');
496-
assert.strictEqual(doc.settings.calendar.locale, 'en-gb');
497-
assert.strictEqual(doc.settings.calendar.dateFormat, '1234');
498-
});
494+
doc = await BlogPost.findOne().select('settings author');
495+
assert.strictEqual(doc.settings.calendar.locale, 'en-gb');
496+
assert.strictEqual(doc.settings.calendar.dateFormat, '1234');
499497
});
500498

501-
it('when `select: true` in schema, works with $elemMatch in projection', function() {
502-
return co(function*() {
499+
it('when `select: true` in schema, works with $elemMatch in projection', async function() {
503500

504-
const productSchema = new Schema({
505-
attributes: {
506-
select: true,
507-
type: [{ name: String, group: String }]
508-
}
509-
});
510501

511-
const Product = db.model('Product', productSchema);
502+
const productSchema = new Schema({
503+
attributes: {
504+
select: true,
505+
type: [{ name: String, group: String }]
506+
}
507+
});
512508

513-
const attributes = [
514-
{ name: 'a', group: 'alpha' },
515-
{ name: 'b', group: 'beta' }
516-
];
509+
const Product = db.model('Product', productSchema);
517510

518-
yield Product.create({ name: 'test', attributes });
511+
const attributes = [
512+
{ name: 'a', group: 'alpha' },
513+
{ name: 'b', group: 'beta' }
514+
];
519515

520-
const product = yield Product.findOne()
521-
.select({ attributes: { $elemMatch: { group: 'beta' } } });
516+
await Product.create({ name: 'test', attributes });
522517

523-
assert.equal(product.attributes[0].name, 'b');
524-
assert.equal(product.attributes[0].group, 'beta');
525-
assert.equal(product.attributes.length, 1);
526-
});
518+
const product = await Product.findOne()
519+
.select({ attributes: { $elemMatch: { group: 'beta' } } });
520+
521+
assert.equal(product.attributes[0].name, 'b');
522+
assert.equal(product.attributes[0].group, 'beta');
523+
assert.equal(product.attributes.length, 1);
527524
});
528525

529-
it('selection specified in query overwrites option in schema', function() {
530-
return co(function*() {
531-
const productSchema = new Schema({ name: { type: String, select: false } });
526+
it('selection specified in query overwrites option in schema', async function() {
532527

533-
const Product = db.model('Product', productSchema);
528+
const productSchema = new Schema({ name: { type: String, select: false } });
534529

530+
const Product = db.model('Product', productSchema);
535531

536-
yield Product.create({ name: 'Computer' });
537532

538-
const product = yield Product.findOne().select('name');
533+
await Product.create({ name: 'Computer' });
539534

540-
assert.equal(product.name, 'Computer');
541-
});
535+
const product = await Product.findOne().select('name');
536+
537+
assert.equal(product.name, 'Computer');
542538
});
543539

544-
it('selecting with `false` instead of `0` doesn\'t overwrite schema `select: false` (gh-8923)', function() {
545-
return co(function*() {
546-
const userSchema = new Schema({
547-
name: { type: String, select: false },
548-
age: { type: Number }
549-
});
540+
it('selecting with `false` instead of `0` doesn\'t overwrite schema `select: false` (gh-8923)', async function() {
550541

551-
const User = db.model('User', userSchema);
542+
const userSchema = new Schema({
543+
name: { type: String, select: false },
544+
age: { type: Number }
545+
});
552546

553-
yield User.create({ name: 'Hafez', age: 25 });
547+
const User = db.model('User', userSchema);
554548

555-
const user = yield User.findOne().select({ age: false });
549+
await User.create({ name: 'Hafez', age: 25 });
556550

557-
assert.ok(!user.name);
558-
});
551+
const user = await User.findOne().select({ age: false });
552+
553+
assert.ok(!user.name);
559554
});
560555
});

‎test/model.findOneAndDelete.test.js

+20-25
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
const start = require('./common');
88

99
const assert = require('assert');
10-
const co = require('co');
1110

1211
const mongoose = start.mongoose;
1312
const Schema = mongoose.Schema;
@@ -78,22 +77,20 @@ describe('model: findOneAndDelete:', function() {
7877
BlogPost = db.model('BlogPost', BlogPost);
7978
});
8079

81-
it('returns the original document', function() {
80+
it('returns the original document', async function() {
8281
const M = BlogPost;
8382
const title = 'remove muah';
8483

8584
const post = new M({ title: title });
8685

87-
return co(function*() {
88-
yield post.save();
86+
await post.save();
8987

90-
const doc = yield M.findOneAndDelete({ title: title });
88+
const doc = await M.findOneAndDelete({ title: title });
9189

92-
assert.equal(post.id, doc.id);
90+
assert.equal(post.id, doc.id);
9391

94-
const gone = yield M.findById(post.id);
95-
assert.equal(gone, null);
96-
});
92+
const gone = await M.findById(post.id);
93+
assert.equal(gone, null);
9794
});
9895

9996
it('options/conditions/doc are merged when no callback is passed', function(done) {
@@ -326,25 +323,23 @@ describe('model: findOneAndDelete:', function() {
326323
});
327324
});
328325

329-
it('only calls setters once (gh-6203)', function() {
330-
return co(function*() {
331-
const calls = [];
332-
const userSchema = new mongoose.Schema({
333-
name: String,
334-
foo: {
335-
type: String,
336-
set: function(val) {
337-
calls.push(val);
338-
return val + val;
339-
}
326+
it('only calls setters once (gh-6203)', async function() {
327+
const calls = [];
328+
const userSchema = new mongoose.Schema({
329+
name: String,
330+
foo: {
331+
type: String,
332+
set: function(val) {
333+
calls.push(val);
334+
return val + val;
340335
}
341-
});
342-
const Model = db.model('Test', userSchema);
336+
}
337+
});
338+
const Model = db.model('Test', userSchema);
343339

344-
yield Model.findOneAndDelete({ foo: '123' }, { name: 'bar' });
340+
await Model.findOneAndDelete({ foo: '123' }, { name: 'bar' });
345341

346-
assert.deepEqual(calls, ['123']);
347-
});
342+
assert.deepEqual(calls, ['123']);
348343
});
349344

350345
describe('middleware', function() {

‎test/model.findOneAndRemove.test.js

+22-26
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,13 @@
77
const start = require('./common');
88

99
const assert = require('assert');
10-
const co = require('co');
1110

1211
const mongoose = start.mongoose;
1312
const Schema = mongoose.Schema;
1413
const ObjectId = Schema.Types.ObjectId;
1514
const DocumentObjectId = mongoose.Types.ObjectId;
1615

17-
describe('model: findOneAndRemove:', function() {
16+
describe('model: findOneAndRemove:', async function() {
1817
let Comments;
1918
let BlogPost;
2019
let db;
@@ -78,22 +77,20 @@ describe('model: findOneAndRemove:', function() {
7877
BlogPost = db.model('BlogPost', BlogPost);
7978
});
8079

81-
it('returns the original document', function() {
80+
it('returns the original document', async function() {
8281
const M = BlogPost;
8382
const title = 'remove muah';
8483

8584
const post = new M({ title: title });
8685

87-
return co(function*() {
88-
yield post.save();
86+
await post.save();
8987

90-
const doc = yield M.findOneAndRemove({ title: title });
88+
const doc = await M.findOneAndRemove({ title: title });
9189

92-
assert.equal(post.id, doc.id);
90+
assert.equal(post.id, doc.id);
9391

94-
const gone = yield M.findById(post.id);
95-
assert.equal(gone, null);
96-
});
92+
const gone = await M.findById(post.id);
93+
assert.equal(gone, null);
9794
});
9895

9996
it('options/conditions/doc are merged when no callback is passed', function(done) {
@@ -326,25 +323,24 @@ describe('model: findOneAndRemove:', function() {
326323
});
327324
});
328325

329-
it('only calls setters once (gh-6203)', function() {
330-
return co(function*() {
331-
const calls = [];
332-
const userSchema = new mongoose.Schema({
333-
name: String,
334-
foo: {
335-
type: String,
336-
set: function(val) {
337-
calls.push(val);
338-
return val + val;
339-
}
326+
it('only calls setters once (gh-6203)', async function() {
327+
328+
const calls = [];
329+
const userSchema = new mongoose.Schema({
330+
name: String,
331+
foo: {
332+
type: String,
333+
set: function(val) {
334+
calls.push(val);
335+
return val + val;
340336
}
341-
});
342-
const Model = db.model('Test', userSchema);
337+
}
338+
});
339+
const Model = db.model('Test', userSchema);
343340

344-
yield Model.findOneAndRemove({ foo: '123' }, { name: 'bar' });
341+
await Model.findOneAndRemove({ foo: '123' }, { name: 'bar' });
345342

346-
assert.deepEqual(calls, ['123']);
347-
});
343+
assert.deepEqual(calls, ['123']);
348344
});
349345

350346
it('with orFail() (gh-9381)', function() {

‎test/model.middleware.test.js

+20-25
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
const start = require('./common');
88

99
const assert = require('assert');
10-
const co = require('co');
1110

1211
const mongoose = start.mongoose;
1312
const Schema = mongoose.Schema;
@@ -401,7 +400,7 @@ describe('model middleware', function() {
401400
});
402401
});
403402

404-
it('static hooks (gh-5982)', function() {
403+
it('static hooks (gh-5982)', async function() {
405404
const schema = new Schema({
406405
name: String
407406
});
@@ -424,18 +423,16 @@ describe('model middleware', function() {
424423

425424
const Model = db.model('Test', schema);
426425

427-
return co(function*() {
428-
yield Model.create({ name: 'foo' });
426+
await Model.create({ name: 'foo' });
429427

430-
const docs = yield Model.findByName('foo');
431-
assert.equal(docs.length, 1);
432-
assert.equal(docs[0].name, 'foo');
433-
assert.equal(preCalled, 1);
434-
assert.equal(postCalled, 1);
435-
});
428+
const docs = await Model.findByName('foo');
429+
assert.equal(docs.length, 1);
430+
assert.equal(docs[0].name, 'foo');
431+
assert.equal(preCalled, 1);
432+
assert.equal(postCalled, 1);
436433
});
437434

438-
it('deleteOne hooks (gh-7538)', function() {
435+
it('deleteOne hooks (gh-7538)', async function() {
439436
const schema = new Schema({
440437
name: String
441438
});
@@ -457,25 +454,23 @@ describe('model middleware', function() {
457454

458455
const Model = db.model('Test', schema);
459456

460-
return co(function*() {
461-
yield Model.create({ name: 'foo' });
457+
await Model.create({ name: 'foo' });
462458

463-
const doc = yield Model.findOne();
459+
const doc = await Model.findOne();
464460

465-
assert.equal(preCalled, 0);
466-
assert.equal(postCalled, 0);
461+
assert.equal(preCalled, 0);
462+
assert.equal(postCalled, 0);
467463

468-
yield doc.deleteOne();
464+
await doc.deleteOne();
469465

470-
assert.equal(queryPreCalled, 0);
471-
assert.equal(preCalled, 1);
472-
assert.equal(postCalled, 1);
466+
assert.equal(queryPreCalled, 0);
467+
assert.equal(preCalled, 1);
468+
assert.equal(postCalled, 1);
473469

474-
yield Model.deleteOne();
470+
await Model.deleteOne();
475471

476-
assert.equal(queryPreCalled, 1);
477-
assert.equal(preCalled, 1);
478-
assert.equal(postCalled, 1);
479-
});
472+
assert.equal(queryPreCalled, 1);
473+
assert.equal(preCalled, 1);
474+
assert.equal(postCalled, 1);
480475
});
481476
});

‎test/model.query.casting.test.js

+17-22
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
const start = require('./common');
88

99
const assert = require('assert');
10-
const co = require('co');
1110
const random = require('../lib/utils').random;
1211

1312
const mongoose = start.mongoose;
@@ -204,25 +203,23 @@ describe('model query casting', function() {
204203
});
205204
});
206205

207-
it('works with $type matching', function() {
206+
it('works with $type matching', async function() {
208207
const B = BlogPostB;
209208

210-
return co(function*() {
211-
yield B.deleteMany({});
209+
await B.deleteMany({});
212210

213-
yield B.collection.insertMany([{ title: 'test' }, { title: 1 }]);
211+
await B.collection.insertMany([{ title: 'test' }, { title: 1 }]);
214212

215-
const err = yield B.find({ title: { $type: { x: 1 } } }).then(() => null, err => err);
216-
assert.equal(err.message,
217-
'$type parameter must be number, string, or array of numbers and strings');
213+
const err = await B.find({ title: { $type: { x: 1 } } }).then(() => null, err => err);
214+
assert.equal(err.message,
215+
'$type parameter must be number, string, or array of numbers and strings');
218216

219-
let posts = yield B.find({ title: { $type: 2 } });
220-
assert.equal(posts.length, 1);
221-
assert.equal(posts[0].title, 'test');
217+
let posts = await B.find({ title: { $type: 2 } });
218+
assert.equal(posts.length, 1);
219+
assert.equal(posts[0].title, 'test');
222220

223-
posts = yield B.find({ title: { $type: ['string', 'number'] } });
224-
assert.equal(posts.length, 2);
225-
});
221+
posts = await B.find({ title: { $type: ['string', 'number'] } });
222+
assert.equal(posts.length, 2);
226223
});
227224

228225
it('works when finding Boolean with $in (gh-998)', function(done) {
@@ -909,20 +906,18 @@ describe('model query casting', function() {
909906
}
910907
});
911908

912-
it('casts $nor within $elemMatch (gh-9479)', function() {
909+
it('casts $nor within $elemMatch (gh-9479)', async function() {
913910
const Test = db.model('Test', Schema({
914911
arr: [{ x: Number, y: Number }]
915912
}));
916913

917-
return co(function*() {
918-
const _doc = yield Test.create({ arr: [{ x: 1 }, { y: 3 }, { x: 2 }] });
919-
920-
const doc = yield Test.findOne({
921-
arr: { $elemMatch: { $nor: [{ x: 1 }, { y: 3 }] } }
922-
});
914+
const _doc = await Test.create({ arr: [{ x: 1 }, { y: 3 }, { x: 2 }] });
923915

924-
assert.equal(_doc._id.toString(), doc._id.toString());
916+
const doc = await Test.findOne({
917+
arr: { $elemMatch: { $nor: [{ x: 1 }, { y: 3 }] } }
925918
});
919+
920+
assert.equal(_doc._id.toString(), doc._id.toString());
926921
});
927922
});
928923

‎test/model.querying.test.js

+114-128
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ const start = require('./common');
88

99
const Query = require('../lib/query');
1010
const assert = require('assert');
11-
const co = require('co');
1211
const random = require('../lib/utils').random;
1312
const util = require('./util');
1413

@@ -944,31 +943,29 @@ describe('model: querying:', function() {
944943
});
945944
});
946945

947-
it('where $exists', function() {
946+
it('where $exists', async function() {
948947
const ExistsSchema = new Schema({ a: Number, b: String });
949948
const Exists = db.model('Test', ExistsSchema);
950949

951-
return co(function*() {
952-
yield Exists.create({ a: 1 }, { b: 'hi' });
953-
let docs = yield Exists.find({ b: { $exists: true } });
954-
assert.equal(docs.length, 1);
955-
assert.equal(docs[0].b, 'hi');
956-
957-
docs = yield Exists.find({ b: { $exists: 'true' } });
958-
assert.equal(docs.length, 1);
959-
assert.equal(docs[0].b, 'hi');
960-
961-
let threw = false;
962-
try {
963-
yield Exists.find({ b: { $exists: 'foo' } });
964-
} catch (error) {
965-
threw = true;
966-
assert.equal(error.path, 'b');
967-
assert.equal(error.value, 'foo');
968-
assert.equal(error.name, 'CastError');
969-
}
970-
assert.ok(threw);
971-
});
950+
await Exists.create({ a: 1 }, { b: 'hi' });
951+
let docs = await Exists.find({ b: { $exists: true } });
952+
assert.equal(docs.length, 1);
953+
assert.equal(docs[0].b, 'hi');
954+
955+
docs = await Exists.find({ b: { $exists: 'true' } });
956+
assert.equal(docs.length, 1);
957+
assert.equal(docs[0].b, 'hi');
958+
959+
let threw = false;
960+
try {
961+
await Exists.find({ b: { $exists: 'foo' } });
962+
} catch (error) {
963+
threw = true;
964+
assert.equal(error.path, 'b');
965+
assert.equal(error.value, 'foo');
966+
assert.equal(error.name, 'CastError');
967+
}
968+
assert.ok(threw);
972969
});
973970

974971
it('works with $elemMatch (gh-1100)', function(done) {
@@ -1435,7 +1432,7 @@ describe('model: querying:', function() {
14351432
});
14361433
});
14371434

1438-
it('works when text search is called by a schema (gh-3824) (gh-6851)', function() {
1435+
it('works when text search is called by a schema (gh-3824) (gh-6851)', async function() {
14391436
if (!mongo26_or_greater) {
14401437
return this.skip();
14411438
}
@@ -1448,29 +1445,27 @@ describe('model: querying:', function() {
14481445

14491446
const Example = db.model('Test', exampleSchema);
14501447

1451-
return co(function*() {
1452-
yield Example.init(); // Wait for index build
1453-
// Should not error
1454-
yield Example.findOne({ $text: { $search: 'text search' } });
1448+
await Example.init(); // Wait for index build
1449+
// Should not error
1450+
await Example.findOne({ $text: { $search: 'text search' } });
14551451

1456-
yield Example.create({ name: '1234 ABCD', tag: 'test1' });
1457-
let doc = yield Example.findOne({
1458-
$text: {
1459-
$search: 1234 // Will be casted to a string
1460-
}
1461-
});
1462-
assert.ok(doc);
1463-
assert.equal(doc.tag, 'test1');
1452+
await Example.create({ name: '1234 ABCD', tag: 'test1' });
1453+
let doc = await Example.findOne({
1454+
$text: {
1455+
$search: 1234 // Will be casted to a string
1456+
}
1457+
});
1458+
assert.ok(doc);
1459+
assert.equal(doc.tag, 'test1');
14641460

1465-
doc = yield Example.findOne({
1466-
$text: {
1467-
$search: 'abcd',
1468-
$caseSensitive: 'no' // Casted to boolean
1469-
}
1470-
});
1471-
assert.ok(doc);
1472-
assert.equal(doc.tag, 'test1');
1461+
doc = await Example.findOne({
1462+
$text: {
1463+
$search: 'abcd',
1464+
$caseSensitive: 'no' // Casted to boolean
1465+
}
14731466
});
1467+
assert.ok(doc);
1468+
assert.equal(doc.tag, 'test1');
14741469
});
14751470
});
14761471
});
@@ -1838,7 +1833,7 @@ describe('model: querying:', function() {
18381833
});
18391834
});
18401835

1841-
it('with conditionals', function(done) {
1836+
it('with conditionals', async function() {
18421837
// $in $nin etc
18431838
const BufSchema = new Schema({ name: String, block: Buffer });
18441839
const Test = db.model('Test', BufSchema);
@@ -1848,79 +1843,72 @@ describe('model: querying:', function() {
18481843
const docC = { name: 'C', block: new MongooseBuffer('aGVsbG8gd29ybGQ=', 'base64') };
18491844
const docD = { name: 'D', block: new MongooseBuffer({ type: 'Buffer', data: [103, 104, 45, 54, 56, 54, 51] }) };
18501845

1851-
Test.create(docA, docB, docC, docD, function(err, a, b, c, d) {
1852-
if (err) return done(err);
1853-
1854-
co(function*() {
1855-
assert.equal(a.block.toString('utf8'), 'über');
1856-
assert.equal(b.block.toString('utf8'), 'buffer shtuffs are neat');
1857-
assert.equal(c.block.toString('utf8'), 'hello world');
1858-
assert.equal(d.block.toString('utf8'), 'gh-6863');
1859-
1860-
const testPromises = [
1861-
Test.find({ block: {
1862-
$in: [
1863-
[195, 188, 98, 101, 114],
1864-
'buffer shtuffs are neat',
1865-
Buffer.from('aGVsbG8gd29ybGQ=', 'base64'),
1866-
{ type: 'Buffer', data: [103, 104, 45, 54, 56, 54, 51] } // gh-6863
1867-
] } }).exec().then(tests => {
1868-
assert.ifError(err);
1869-
assert.equal(tests.length, 4);
1870-
}),
1871-
Test.find({ block: { $in: ['über', 'hello world'] } }).exec().then(tests => {
1872-
assert.equal(tests.length, 2);
1873-
}),
1874-
Test.find({ block: { $in: ['über'] } }).exec().then(tests => {
1875-
assert.equal(tests.length, 1);
1876-
assert.equal(tests[0].block.toString('utf8'), 'über');
1877-
}),
1878-
Test.find({ block: { $nin: ['über'] } }).exec().then(tests => {
1879-
assert.equal(tests.length, 3);
1880-
}),
1881-
Test.find({ block: {
1882-
$nin: [
1883-
[195, 188, 98, 101, 114],
1884-
Buffer.from('aGVsbG8gd29ybGQ=', 'base64'),
1885-
{ type: 'Buffer', data: [103, 104, 45, 54, 56, 54, 51] } // gh-6863
1886-
] } }).exec().then(tests => {
1887-
assert.ifError(err);
1888-
assert.equal(tests.length, 1);
1889-
assert.equal(tests[0].block.toString('utf8'), 'buffer shtuffs are neat');
1890-
}),
1891-
Test.find({ block: { $ne: 'über' } }).exec().then(tests => {
1892-
assert.equal(tests.length, 3);
1893-
}),
1894-
Test.find({ block: { $gt: 'über' } }).exec().then(tests => {
1895-
assert.equal(tests.length, 3);
1896-
}),
1897-
Test.find({ block: { $gte: 'über' } }).exec().then(tests => {
1898-
assert.equal(tests.length, 4);
1899-
}),
1900-
Test.find({ block: { $lt: Buffer.from('buffer shtuffs are neat') } }).exec().then(tests => {
1901-
assert.ifError(err);
1902-
assert.equal(tests.length, 3);
1903-
const ret = {};
1904-
ret[tests[0].block.toString('utf8')] = 1;
1905-
ret[tests[1].block.toString('utf8')] = 1;
1906-
ret[tests[2].block.toString('utf8')] = 1;
1907-
1908-
assert.ok(ret['über'] !== undefined);
1909-
}),
1910-
Test.find({ block: { $lte: 'buffer shtuffs are neat' } }).exec().then(tests => {
1911-
assert.equal(tests.length, 4);
1912-
}),
1913-
Test.find({ block: { $gt: { type: 'Buffer', data: [103, 104, 45, 54, 56, 54, 51] } } }).exec().then(tests => {
1914-
assert.equal(tests.length, 2);
1915-
})
1916-
];
1917-
1918-
// run all of the tests in parallel
1919-
yield testPromises;
1920-
yield Test.deleteOne({});
1921-
done();
1922-
}).catch(done);
1923-
});
1846+
const [a, b, c, d] = await Test.create([docA, docB, docC, docD]);
1847+
1848+
1849+
1850+
assert.equal(a.block.toString('utf8'), 'über');
1851+
assert.equal(b.block.toString('utf8'), 'buffer shtuffs are neat');
1852+
assert.equal(c.block.toString('utf8'), 'hello world');
1853+
assert.equal(d.block.toString('utf8'), 'gh-6863');
1854+
1855+
1856+
// run all of the tests in parallel
1857+
await Promise.all([
1858+
Test.find({ block: {
1859+
$in: [
1860+
[195, 188, 98, 101, 114],
1861+
'buffer shtuffs are neat',
1862+
Buffer.from('aGVsbG8gd29ybGQ=', 'base64'),
1863+
{ type: 'Buffer', data: [103, 104, 45, 54, 56, 54, 51] } // gh-6863
1864+
] } }).exec().then(tests => {
1865+
assert.equal(tests.length, 4);
1866+
}),
1867+
Test.find({ block: { $in: ['über', 'hello world'] } }).exec().then(tests => {
1868+
assert.equal(tests.length, 2);
1869+
}),
1870+
Test.find({ block: { $in: ['über'] } }).exec().then(tests => {
1871+
assert.equal(tests.length, 1);
1872+
assert.equal(tests[0].block.toString('utf8'), 'über');
1873+
}),
1874+
Test.find({ block: { $nin: ['über'] } }).exec().then(tests => {
1875+
assert.equal(tests.length, 3);
1876+
}),
1877+
Test.find({ block: {
1878+
$nin: [
1879+
[195, 188, 98, 101, 114],
1880+
Buffer.from('aGVsbG8gd29ybGQ=', 'base64'),
1881+
{ type: 'Buffer', data: [103, 104, 45, 54, 56, 54, 51] } // gh-6863
1882+
] } }).exec().then(tests => {
1883+
assert.equal(tests.length, 1);
1884+
assert.equal(tests[0].block.toString('utf8'), 'buffer shtuffs are neat');
1885+
}),
1886+
Test.find({ block: { $ne: 'über' } }).exec().then(tests => {
1887+
assert.equal(tests.length, 3);
1888+
}),
1889+
Test.find({ block: { $gt: 'über' } }).exec().then(tests => {
1890+
assert.equal(tests.length, 3);
1891+
}),
1892+
Test.find({ block: { $gte: 'über' } }).exec().then(tests => {
1893+
assert.equal(tests.length, 4);
1894+
}),
1895+
Test.find({ block: { $lt: Buffer.from('buffer shtuffs are neat') } }).exec().then(tests => {
1896+
assert.equal(tests.length, 3);
1897+
const ret = {};
1898+
ret[tests[0].block.toString('utf8')] = 1;
1899+
ret[tests[1].block.toString('utf8')] = 1;
1900+
ret[tests[2].block.toString('utf8')] = 1;
1901+
1902+
assert.ok(ret['über'] !== undefined);
1903+
}),
1904+
Test.find({ block: { $lte: 'buffer shtuffs are neat' } }).exec().then(tests => {
1905+
assert.equal(tests.length, 4);
1906+
}),
1907+
Test.find({ block: { $gt: { type: 'Buffer', data: [103, 104, 45, 54, 56, 54, 51] } } }).exec().then(tests => {
1908+
assert.equal(tests.length, 2);
1909+
})
1910+
]);
1911+
await Test.deleteOne({});
19241912
});
19251913

19261914
it('with previously existing null values in the db', function(done) {
@@ -2358,20 +2346,18 @@ describe('model: querying:', function() {
23582346
});
23592347
}
23602348
});
2361-
it('works with legacy 2dsphere pair in schema (gh-6937)', function() {
2349+
it('works with legacy 2dsphere pair in schema (gh-6937)', async function() {
23622350
if (!mongo24_or_greater) {
23632351
return it.skip();
23642352
}
23652353

2366-
return co(function*() {
2367-
const Model = db.model('Test', schema2dsphere);
2368-
yield Model.init();
2369-
const model = new Model();
2370-
model.loc = [1, 2];
2371-
yield model.save();
2372-
const result = yield Model.where('loc').near({ center: { type: 'Point', coordinates: [1, 2] }, maxDistance: 10 });
2373-
assert.equal(result.length, 1);
2374-
});
2354+
const Model = db.model('Test', schema2dsphere);
2355+
await Model.init();
2356+
const model = new Model();
2357+
model.loc = [1, 2];
2358+
await model.save();
2359+
const result = await Model.where('loc').near({ center: { type: 'Point', coordinates: [1, 2] }, maxDistance: 10 });
2360+
assert.equal(result.length, 1);
23752361
});
23762362
});
23772363
});

‎test/query.toconstructor.test.js

+6-8
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ const start = require('./common');
44

55
const Query = require('../lib/query');
66
const assert = require('assert');
7-
const co = require('co');
87

98
const mongoose = start.mongoose;
109
const Schema = mongoose.Schema;
@@ -174,7 +173,7 @@ describe('Query:', function() {
174173
});
175174
});
176175

177-
it('gets middleware from model (gh-6455)', function() {
176+
it('gets middleware from model (gh-6455)', async function() {
178177
let called = 0;
179178
const schema = new Schema({
180179
name: String
@@ -188,12 +187,11 @@ describe('Query:', function() {
188187
const test = new Test({ name: 'Romero' });
189188
const Q = Test.findOne({}).toConstructor();
190189

191-
return co(function*() {
192-
yield test.save();
193-
const doc = yield Q();
194-
assert.strictEqual(doc.name, 'Romero');
195-
assert.strictEqual(called, 1);
196-
});
190+
191+
await test.save();
192+
const doc = await Q();
193+
assert.strictEqual(doc.name, 'Romero');
194+
assert.strictEqual(called, 1);
197195
});
198196

199197
it('works with entries-style sort() syntax (gh-8159)', function() {

‎test/schema.select.test.js

+18-23
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
const start = require('./common');
88

99
const assert = require('assert');
10-
const co = require('co');
1110
const mongoose = start.mongoose;
1211
const Schema = mongoose.Schema;
1312

@@ -424,43 +423,39 @@ describe('schema select option', function() {
424423
});
425424
});
426425

427-
it('ignores if path does not have select in schema (gh-6785)', function() {
426+
it('ignores if path does not have select in schema (gh-6785)', async function() {
428427
const M = db.model('Test', new Schema({
429428
a: String,
430429
b: String
431430
}));
432431

433-
return co(function*() {
434-
yield M.create({ a: 'foo', b: 'bar' });
432+
await M.create({ a: 'foo', b: 'bar' });
435433

436-
const doc = yield M.findOne().select('+a');
437-
assert.equal(doc.a, 'foo');
438-
assert.equal(doc.b, 'bar');
439-
});
434+
const doc = await M.findOne().select('+a');
435+
assert.equal(doc.a, 'foo');
436+
assert.equal(doc.b, 'bar');
440437
});
441438

442-
it('omits if not in schema (gh-7017)', function() {
439+
it('omits if not in schema (gh-7017)', async function() {
443440
const M = db.model('Test', new Schema({
444441
a: { type: String, select: false },
445442
b: { type: String, select: false }
446443
}), 'Test');
447444

448-
return co(function*() {
449-
yield db.$initialConnection;
450-
yield db.collection('Test').insertOne({
451-
a: 'foo',
452-
b: 'bar',
453-
c: 'baz'
454-
});
445+
await db.$initialConnection;
446+
await db.collection('Test').insertOne({
447+
a: 'foo',
448+
b: 'bar',
449+
c: 'baz'
450+
});
455451

456-
const q = M.find({}).select('+c');
457-
const doc = yield q.then(res => res[0]);
458-
assert.deepEqual(q._fields, { a: 0, b: 0 });
452+
const q = M.find({}).select('+c');
453+
const doc = await q.then(res => res[0]);
454+
assert.deepEqual(q._fields, { a: 0, b: 0 });
459455

460-
assert.strictEqual(doc.a, void 0);
461-
assert.strictEqual(doc.b, void 0);
462-
assert.equal(doc.toObject().c, 'baz');
463-
});
456+
assert.strictEqual(doc.a, void 0);
457+
assert.strictEqual(doc.b, void 0);
458+
assert.equal(doc.toObject().c, 'baz');
464459
});
465460
});
466461

‎test/schema.test.js

+10-13
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ const Mixed = SchemaTypes.Mixed;
1717
const DocumentObjectId = mongoose.Types.ObjectId;
1818
const ReadPref = mongoose.mongo.ReadPreference;
1919
const vm = require('vm');
20-
const co = require('co');
2120
const applyPlugins = require('../lib/helpers/schema/applyPlugins');
2221

2322
/**
@@ -2387,28 +2386,26 @@ describe('schema', function() {
23872386
});
23882387

23892388
describe('gh-8849', function() {
2390-
it('treats `select: undefined` as not specifying `select` option', function() {
2389+
it('treats `select: undefined` as not specifying `select` option', async function() {
23912390
const userSchema = new Schema({ name: { type: String, select: undefined } });
23922391
const User = db.model('User', userSchema);
23932392

2394-
return co(function*() {
2395-
yield User.create({ name: 'Hafez' });
2396-
const user = yield User.findOne();
23972393

2398-
assert.equal(user.name, 'Hafez');
2399-
});
2394+
await User.create({ name: 'Hafez' });
2395+
const user = await User.findOne();
2396+
2397+
assert.equal(user.name, 'Hafez');
24002398
});
24012399

2402-
it('treats `select: null` as not specifying `select` option', function() {
2400+
it('treats `select: null` as not specifying `select` option', async function() {
24032401
const userSchema = new Schema({ name: { type: String, select: null } });
24042402
const User = db.model('User', userSchema);
24052403

2406-
return co(function*() {
2407-
yield User.create({ name: 'Hafez' });
2408-
const user = yield User.findOne();
24092404

2410-
assert.equal(user.name, 'Hafez');
2411-
});
2405+
await User.create({ name: 'Hafez' });
2406+
const user = await User.findOne();
2407+
2408+
assert.equal(user.name, 'Hafez');
24122409
});
24132410
});
24142411

‎test/types.array.test.js

+16-19
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
const start = require('./common');
88

99
const assert = require('assert');
10-
const co = require('co');
1110
const mongodb = require('mongodb');
1211
const mongoose = require('./common').mongoose;
1312

@@ -1196,27 +1195,25 @@ describe('types array', function() {
11961195
return Promise.resolve();
11971196
});
11981197

1199-
it('works with $addToSet and $push (gh-7479)', function() {
1200-
return co(function*() {
1201-
const schema = new Schema({
1202-
arr: [mongoose.Schema.Types.ObjectId]
1203-
});
1204-
const Model = db.model('Test', schema);
1205-
yield Model.create({ arr: [] });
1198+
it('works with $addToSet and $push (gh-7479)', async function() {
1199+
const schema = new Schema({
1200+
arr: [mongoose.Schema.Types.ObjectId]
1201+
});
1202+
const Model = db.model('Test', schema);
1203+
await Model.create({ arr: [] });
12061204

1207-
const oid = new mongoose.Types.ObjectId();
1208-
yield Model.updateMany({}, {
1209-
$addToSet: { arr: oid }
1210-
});
1211-
let raw = yield Model.collection.findOne();
1212-
assert.equal(raw.arr[0].toHexString(), oid.toHexString());
1205+
const oid = new mongoose.Types.ObjectId();
1206+
await Model.updateMany({}, {
1207+
$addToSet: { arr: oid }
1208+
});
1209+
let raw = await Model.collection.findOne();
1210+
assert.equal(raw.arr[0].toHexString(), oid.toHexString());
12131211

1214-
yield Model.updateMany({}, {
1215-
$push: { arr: oid }
1216-
});
1217-
raw = yield Model.collection.findOne();
1218-
assert.equal(raw.arr[1].toHexString(), oid.toHexString());
1212+
await Model.updateMany({}, {
1213+
$push: { arr: oid }
12191214
});
1215+
raw = await Model.collection.findOne();
1216+
assert.equal(raw.arr[1].toHexString(), oid.toHexString());
12201217
});
12211218
});
12221219

‎test/types.documentarray.test.js

+11-13
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ const start = require('./common');
99
const DocumentArray = require('../lib/types/DocumentArray');
1010
const ArraySubdocument = require('../lib/types/ArraySubdocument');
1111
const assert = require('assert');
12-
const co = require('co');
1312
const idGetter = require('../lib/plugins/idGetter');
1413
const setValue = require('../lib/utils').setValue;
1514

@@ -620,7 +619,7 @@ describe('types.documentarray', function() {
620619
});
621620
});
622621

623-
it('cleans modified subpaths on splice() (gh-7249)', function() {
622+
it('cleans modified subpaths on splice() (gh-7249)', async function() {
624623
const childSchema = mongoose.Schema({
625624
name: { type: String, required: true }
626625
}, { _id: false });
@@ -631,22 +630,21 @@ describe('types.documentarray', function() {
631630

632631
const Parent = db.model('Test', parentSchema);
633632

634-
return co(function*() {
635-
let parent = yield Parent.create({
636-
children: [{ name: '1' }, { name: '2' }]
637-
});
638633

639-
parent = yield Parent.findOne();
634+
let parent = await Parent.create({
635+
children: [{ name: '1' }, { name: '2' }]
636+
});
637+
638+
parent = await Parent.findOne();
640639

641-
parent.children[1].name = '3';
642-
parent.children.splice(0, 1);
640+
parent.children[1].name = '3';
641+
parent.children.splice(0, 1);
643642

644-
yield parent.save();
643+
await parent.save();
645644

646-
parent = yield Parent.findOne();
645+
parent = await Parent.findOne();
647646

648-
assert.deepEqual(parent.toObject().children, [{ name: '3' }]);
649-
});
647+
assert.deepEqual(parent.toObject().children, [{ name: '3' }]);
650648
});
651649

652650
it('modifies ownerDocument() on set (gh-8479)', function() {

0 commit comments

Comments
 (0)
Please sign in to comment.