From 1aa6fad4922bffda4b398bdfe6064ada1d05646c Mon Sep 17 00:00:00 2001 From: Valeri Karpov Date: Mon, 16 Jan 2023 17:39:35 -0500 Subject: [PATCH 01/10] fix(query): correctly pass context when casting `$elemMatch` Fix #12902 Re: #12909 --- lib/cast.js | 3 ++- lib/schema/array.js | 12 ++++++------ test/query.test.js | 34 ++++++++++++++++++++++++++++++++++ 3 files changed, 42 insertions(+), 7 deletions(-) diff --git a/lib/cast.js b/lib/cast.js index ea0bccdca3f..8a5bb696999 100644 --- a/lib/cast.js +++ b/lib/cast.js @@ -387,7 +387,8 @@ function getStrictQuery(queryOptions, schemaUserProvidedOptions, schemaOptions, if ('strict' in schemaUserProvidedOptions) { return schemaUserProvidedOptions.strict; } - const mongooseOptions = context.mongooseCollection && + const mongooseOptions = context && + context.mongooseCollection && context.mongooseCollection.conn && context.mongooseCollection.conn.base && context.mongooseCollection.conn.base.options; diff --git a/lib/schema/array.js b/lib/schema/array.js index 0140fe68a0c..7dc81903054 100644 --- a/lib/schema/array.js +++ b/lib/schema/array.js @@ -557,17 +557,17 @@ function cast$all(val) { val = [val]; } - val = val.map(function(v) { + val = val.map((v) => { if (!utils.isObject(v)) { return v; } if (v.$elemMatch != null) { - return { $elemMatch: cast(this.casterConstructor.schema, v.$elemMatch) }; + return { $elemMatch: cast(this.casterConstructor.schema, v.$elemMatch, null, this && this.$$context) }; } const o = {}; o[this.path] = v; - return cast(this.casterConstructor.schema, o)[this.path]; + return cast(this.casterConstructor.schema, o, null, this && this.$$context)[this.path]; }, this); return this.castForQuery(val); @@ -598,10 +598,10 @@ function cast$elemMatch(val) { if (discriminatorKey != null && val[discriminatorKey] != null && discriminators[val[discriminatorKey]] != null) { - return cast(discriminators[val[discriminatorKey]], val); + return cast(discriminators[val[discriminatorKey]], val, null, this && this.$$context); } - return cast(this.casterConstructor.schema, val); + return cast(this.casterConstructor.schema, val, null, this && this.$$context); } const handle = SchemaArray.prototype.$conditionalHandlers = {}; @@ -622,7 +622,7 @@ function createLogicalQueryOperatorHandler(op) { const ret = []; for (const obj of val) { - ret.push(cast(this.casterConstructor.schema, obj)); + ret.push(cast(this.casterConstructor.schema, obj, null, this && this.$$context)); } return ret; diff --git a/test/query.test.js b/test/query.test.js index 3d1220a0cc4..a41ae447b2e 100644 --- a/test/query.test.js +++ b/test/query.test.js @@ -4349,4 +4349,38 @@ describe('Query', function() { assert.ok('title' in replacedDoc === false); }); + + it('handles $elemMatch with nested schema (gh-12902)', async function() { + const bioSchema = new Schema({ + name: { type: String } + }); + + const Book = db.model('book', new Schema({ + name: String, + authors: [{ + bio: bioSchema + }] + })); + + await new Book({ + name: 'Mongoose Fundamentals', + authors: [{ + bio: { + name: 'Foo Bar' + } + }] + }).save(); + + const books = await Book.find({ + name: 'Mongoose Fundamentals', + authors: { + $elemMatch: { + 'bio.name': { $in: ['Foo Bar'] }, + 'bio.location': 'Mandurah' // Not in schema + } + } + }); + + assert.strictEqual(books.length, 1); + }); }); From 386c366817d3931aef7494d5a3d2b208072458b4 Mon Sep 17 00:00:00 2001 From: Valeri Karpov Date: Tue, 17 Jan 2023 10:59:29 -0500 Subject: [PATCH 02/10] test: some cleanup re: #12890 --- lib/cursor/ChangeStream.js | 12 ++++++ test/collection.capped.test.js | 16 +++---- test/collection.test.js | 19 ++++---- test/connection.test.js | 16 +++---- test/model.test.js | 69 +---------------------------- test/model.watch.test.js | 79 ++++++++++++++++++++++++++++++++++ test/query.middleware.test.js | 6 --- 7 files changed, 117 insertions(+), 100 deletions(-) create mode 100644 test/model.watch.test.js diff --git a/lib/cursor/ChangeStream.js b/lib/cursor/ChangeStream.js index 0158066c1f7..72b844ac67d 100644 --- a/lib/cursor/ChangeStream.js +++ b/lib/cursor/ChangeStream.js @@ -54,6 +54,12 @@ class ChangeStream extends EventEmitter { ['close', 'change', 'end', 'error'].forEach(ev => { this.driverChangeStream.on(ev, data => { + // Sometimes Node driver still polls after close, so + // avoid any uncaught exceptions due to closed change streams + // See tests for gh-7022 + if (ev === 'error' && this.closed) { + return; + } if (data != null && data.fullDocument != null && this.options && this.options.hydrate) { data.fullDocument = this.options.model.hydrate(data.fullDocument); } @@ -71,6 +77,12 @@ class ChangeStream extends EventEmitter { ['close', 'change', 'end', 'error'].forEach(ev => { this.driverChangeStream.on(ev, data => { + // Sometimes Node driver still polls after close, so + // avoid any uncaught exceptions due to closed change streams + // See tests for gh-7022 + if (ev === 'error' && this.closed) { + return; + } this.emit(ev, data); }); }); diff --git a/test/collection.capped.test.js b/test/collection.capped.test.js index ddd39600537..786bbe39d68 100644 --- a/test/collection.capped.test.js +++ b/test/collection.capped.test.js @@ -19,15 +19,12 @@ const Schema = mongoose.Schema; describe('collections: capped:', function() { let db; - const connectionsToClose = []; - - before(function() { - db = start(); - }); - - after(async function() { + afterEach(async function() { + if (db == null) { + return; + } await db.close(); - await Promise.all(connectionsToClose.map((v) => v.close())); + db = null; }); it('schemas should have option size', function() { @@ -41,6 +38,8 @@ describe('collections: capped:', function() { it('creation', async function() { this.timeout(15000); + db = start(); + await db.dropCollection('Test').catch(() => {}); const capped = new Schema({ key: String }); @@ -55,7 +54,6 @@ describe('collections: capped:', function() { it('skips when setting autoCreate to false (gh-8566)', async function() { const db = start(); - connectionsToClose.push(db); this.timeout(30000); await db.dropDatabase(); diff --git a/test/collection.test.js b/test/collection.test.js index 30a98c75289..c3307d5af7e 100644 --- a/test/collection.test.js +++ b/test/collection.test.js @@ -8,15 +8,18 @@ const assert = require('assert'); const mongoose = start.mongoose; describe('collections:', function() { - const connectionsToClose = []; + let db = null; - after(async function() { - await Promise.all(connectionsToClose.map((v) => v.close())); + this.afterEach(async function() { + if (db == null) { + return; + } + await db.close(); + db = null; }); it('should buffer commands until connection is established', function(done) { - const db = mongoose.createConnection(); - connectionsToClose.push(db); + db = mongoose.createConnection(); const collection = db.collection('test-buffering-collection'); let connected = false; let insertedId = undefined; @@ -49,8 +52,7 @@ describe('collections:', function() { }); it('returns a promise if buffering and no callback (gh-7676)', function(done) { - const db = mongoose.createConnection(); - connectionsToClose.push(db); + db = mongoose.createConnection(); const collection = db.collection('gh7676'); const promise = collection.insertOne({ foo: 'bar' }, {}) @@ -152,8 +154,7 @@ describe('collections:', function() { }); it('buffers for sync methods (gh-10610)', function(done) { - const db = mongoose.createConnection(); - connectionsToClose.push(db); + mongoose.createConnection(); const collection = db.collection('gh10610'); collection.find({}, {}, function(err, res) { diff --git a/test/connection.test.js b/test/connection.test.js index fcb78b7fc95..f637411dc24 100644 --- a/test/connection.test.js +++ b/test/connection.test.js @@ -773,29 +773,29 @@ describe('connections:', function() { db2.close(); }); - it('cache connections to the same db', function(done) { + it('cache connections to the same db', function() { const db = start(); const db2 = db.useDb(start.databases[1], { useCache: true }); const db3 = db.useDb(start.databases[1], { useCache: true }); assert.strictEqual(db2, db3); - db.close(done); + return db.close(); }); }); describe('shouldAuthenticate()', function() { describe('when using standard authentication', function() { describe('when username and password are undefined', function() { - it('should return false', function(done) { + it('should return false', function() { const db = mongoose.createConnection(start.uri, {}); assert.equal(db.shouldAuthenticate(), false); - db.close(done); + return db.close(); }); }); describe('when username and password are empty strings', function() { - it('should return false', function(done) { + it('should return false', function() { const db = mongoose.createConnection(start.uri, { user: '', pass: '' @@ -804,7 +804,7 @@ describe('connections:', function() { assert.equal(db.shouldAuthenticate(), false); - db.close(done); + return db.close(); }); }); describe('when both username and password are defined', function() { @@ -823,14 +823,14 @@ describe('connections:', function() { }); describe('when using MONGODB-X509 authentication', function() { describe('when username and password are undefined', function() { - it('should return false', function(done) { + it('should return false', function() { const db = mongoose.createConnection(start.uri, {}); db.on('error', function() { }); assert.equal(db.shouldAuthenticate(), false); - db.close(done); + return db.close(); }); }); describe('when only username is defined', function() { diff --git a/test/model.test.js b/test/model.test.js index 04c7765ce76..faf181acde3 100644 --- a/test/model.test.js +++ b/test/model.test.js @@ -20,13 +20,10 @@ const EmbeddedDocument = mongoose.Types.Subdocument; const MongooseError = mongoose.Error; describe('Model', function() { - let db; let Comments; let BlogPost; - const connectionsToClose = []; - beforeEach(() => db.deleteModel(/.*/)); beforeEach(function() { @@ -84,7 +81,6 @@ describe('Model', function() { after(async function() { await db.close(); - await Promise.all(connectionsToClose.map(async(v) => /* v instanceof Promise ? (await v).close() : */ v.close())); }); afterEach(() => util.clearTestData(db)); @@ -3722,9 +3718,6 @@ describe('Model', function() { }); it('with positional notation on path not existing in schema (gh-1048)', function(done) { - const db = start(); - connectionsToClose.push(db); - const M = db.model('Test', Schema({ name: 'string' })); db.on('open', function() { const o = { @@ -4336,7 +4329,6 @@ describe('Model', function() { it('save max bson size error with buffering (gh-3906)', async function() { this.timeout(10000); const db = start({ noErrorListener: true }); - connectionsToClose.push(db); const Test = db.model('Test', { name: Object }); const test = new Test({ @@ -4355,7 +4347,6 @@ describe('Model', function() { it('reports max bson size error in save (gh-3906)', async function() { this.timeout(10000); const db = await start({ noErrorListener: true }); - connectionsToClose.push(db); const Test = db.model('Test', { name: Object }); const test = new Test({ @@ -5339,63 +5330,6 @@ describe('Model', function() { assert.equal(changeData.operationType, 'insert'); assert.equal(changeData.fullDocument.name, 'Child'); }); - - it('watch() before connecting (gh-5964)', async function() { - const db = start(); - connectionsToClose.push(db); - - const MyModel = db.model('Test5964', new Schema({ name: String })); - - // Synchronous, before connection happens - const changeStream = MyModel.watch(); - const changed = new global.Promise(resolve => { - changeStream.once('change', data => resolve(data)); - }); - - await db; - await MyModel.create({ name: 'Ned Stark' }); - - const changeData = await changed; - assert.equal(changeData.operationType, 'insert'); - assert.equal(changeData.fullDocument.name, 'Ned Stark'); - }); - - it('watch() close() prevents buffered watch op from running (gh-7022)', async function() { - const db = start(); - connectionsToClose.push(db); - const MyModel = db.model('Test', new Schema({})); - const changeStream = MyModel.watch(); - const ready = new global.Promise(resolve => { - changeStream.once('data', () => { - resolve(true); - }); - setTimeout(resolve, 500, false); - }); - - changeStream.close(); - await db; - const readyCalled = await ready; - assert.strictEqual(readyCalled, false); - }); - - it('watch() close() closes the stream (gh-7022)', async function() { - const db = await start(); - connectionsToClose.push(db); - const MyModel = db.model('Test', new Schema({ name: String })); - - await MyModel.init(); - - const changeStream = MyModel.watch(); - const closed = new global.Promise(resolve => { - changeStream.once('close', () => resolve(true)); - }); - - await MyModel.create({ name: 'Hodor' }); - - changeStream.close(); - const closedData = await closed; - assert.strictEqual(closedData, true); - }); }); describe('sessions (gh-6362)', function() { @@ -5429,9 +5363,7 @@ describe('Model', function() { }); it('startSession() before connecting', async function() { - const db = start(); - connectionsToClose.push(db); const MyModel = db.model('Test', new Schema({ name: String })); @@ -5446,6 +5378,7 @@ describe('Model', function() { session.endSession(); + await db.close(); }); it('sets session when pulling a document from db', async function() { diff --git a/test/model.watch.test.js b/test/model.watch.test.js new file mode 100644 index 00000000000..84d41dc5b14 --- /dev/null +++ b/test/model.watch.test.js @@ -0,0 +1,79 @@ +'use strict'; + +const assert = require('assert'); +const start = require('./common'); + +const mongoose = start.mongoose; +const Schema = mongoose.Schema; + +describe('model: watch: ', function() { + describe('with buffering', function() { + let db; + + before(function() { + if (!process.env.REPLICA_SET) { + this.skip(); + } + }); + + beforeEach(function() { + db = start(); + }); + + afterEach(() => db.close()); + + it('watch() before connecting (gh-5964)', async function() { + const MyModel = db.model('Test5964', new Schema({ name: String })); + + // Synchronous, before connection happens + const changeStream = MyModel.watch(); + const changed = new global.Promise(resolve => { + changeStream.once('change', data => resolve(data)); + }); + + await db.asPromise(); + await MyModel.create({ name: 'Ned Stark' }); + + const changeData = await changed; + assert.equal(changeData.operationType, 'insert'); + assert.equal(changeData.fullDocument.name, 'Ned Stark'); + }); + + it('watch() close() prevents buffered watch op from running (gh-7022)', async function() { + const MyModel = db.model('Test', new Schema({})); + const changeStream = MyModel.watch(); + const ready = new global.Promise(resolve => { + changeStream.once('data', () => { + resolve(true); + }); + setTimeout(resolve, 500, false); + }); + + const close = changeStream.close(); + await db.asPromise(); + const readyCalled = await ready; + assert.strictEqual(readyCalled, false); + + await close; + }); + + it('watch() close() closes the stream (gh-7022)', async function() { + const MyModel = db.model('Test', new Schema({ name: String })); + + await db.asPromise(); + await MyModel.init(); + + const changeStream = MyModel.watch(); + const closed = new global.Promise(resolve => { + changeStream.once('close', () => resolve(true)); + }); + + await MyModel.create({ name: 'Hodor' }); + + await changeStream.close(); + + const closedData = await closed; + assert.strictEqual(closedData, true); + }); + }); +}); diff --git a/test/query.middleware.test.js b/test/query.middleware.test.js index d18118fd3b6..153189ba8d1 100644 --- a/test/query.middleware.test.js +++ b/test/query.middleware.test.js @@ -14,15 +14,12 @@ describe('query middleware', function() { let Author; let Publisher; - const connectionsToClose = []; - before(function() { db = start(); }); after(async function() { await db.close(); - await Promise.all(connectionsToClose.map((v) => v.close())); }); const initializeData = function(done) { @@ -84,9 +81,6 @@ describe('query middleware', function() { next(); }); - const conn = start(); - connectionsToClose.push(conn); - initializeData(function(error) { assert.ifError(error); Author.find({ x: 1 }, function(error) { From fb00ab6c3790af56e86c664ce6e4e09d1cab4678 Mon Sep 17 00:00:00 2001 From: Valeri Karpov Date: Tue, 17 Jan 2023 11:06:14 -0500 Subject: [PATCH 03/10] fix some tests --- test/collection.test.js | 2 +- test/model.test.js | 37 +++++++++++++++++-------------------- 2 files changed, 18 insertions(+), 21 deletions(-) diff --git a/test/collection.test.js b/test/collection.test.js index c3307d5af7e..324bb531ae5 100644 --- a/test/collection.test.js +++ b/test/collection.test.js @@ -154,7 +154,7 @@ describe('collections:', function() { }); it('buffers for sync methods (gh-10610)', function(done) { - mongoose.createConnection(); + db = mongoose.createConnection(); const collection = db.collection('gh10610'); collection.find({}, {}, function(err, res) { diff --git a/test/model.test.js b/test/model.test.js index faf181acde3..389c84e3508 100644 --- a/test/model.test.js +++ b/test/model.test.js @@ -3719,28 +3719,25 @@ describe('Model', function() { it('with positional notation on path not existing in schema (gh-1048)', function(done) { const M = db.model('Test', Schema({ name: 'string' })); - db.on('open', function() { - const o = { - name: 'gh-1048', - _id: new mongoose.Types.ObjectId(), - databases: { - 0: { keys: 100, expires: 0 }, - 15: { keys: 1, expires: 0 } - } - }; + const o = { + name: 'gh-1048', + _id: new mongoose.Types.ObjectId(), + databases: { + 0: { keys: 100, expires: 0 }, + 15: { keys: 1, expires: 0 } + } + }; - M.collection.insertOne(o, function(err) { + M.updateOne({ _id: o._id }, o, { upsert: true, strict: false }, function(err) { + assert.ifError(err); + M.findById(o._id, function(err, doc) { assert.ifError(err); - M.findById(o._id, function(err, doc) { - db.close(); - assert.ifError(err); - assert.ok(doc); - assert.ok(doc._doc.databases); - assert.ok(doc._doc.databases['0']); - assert.ok(doc._doc.databases['15']); - assert.equal(doc.databases, undefined); - done(); - }); + assert.ok(doc); + assert.ok(doc._doc.databases); + assert.ok(doc._doc.databases['0']); + assert.ok(doc._doc.databases['15']); + assert.equal(doc.databases, undefined); + done(); }); }); }); From 21d5358897a5e7b7967b6658e499809d35fa22af Mon Sep 17 00:00:00 2001 From: Panagiotis Papadopoulos Date: Tue, 17 Jan 2023 22:28:45 +0100 Subject: [PATCH 04/10] docs(validation): fix dead links fixes Automattic#12921 --- docs/validation.md | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/docs/validation.md b/docs/validation.md index 80dedb0304c..1d8548086f0 100644 --- a/docs/validation.md +++ b/docs/validation.md @@ -34,8 +34,8 @@ Before we get into the specifics of validation syntax, please keep the following Mongoose has several built-in validators. - All [SchemaTypes](schematypes.html) have the built-in [required](api/schematype.html#schematype_SchemaType-required) validator. The required validator uses the [SchemaType's `checkRequired()` function](api/schematype.html#schematype_SchemaType-checkRequired) to determine if the value satisfies the required validator. -- [Numbers](api/schema-number-js.html#schema-number-js) have [`min` and `max`](schematypes.html#number-validators) validators. -- [Strings](api/schema-string-js.html#schema-string-js) have [`enum`, `match`, `minLength`, and `maxLength`](schematypes.html#string-validators) validators. +- [Numbers](schematypes.html#numbers) have [`min` and `max`](schematypes.html#number-validators) validators. +- [Strings](schematypes.html#strings) have [`enum`, `match`, `minLength`, and `maxLength`](schematypes.html#string-validators) validators. Each of the validator links above provide more information about how to enable them and customize their error messages. @@ -96,7 +96,7 @@ the value `false`, Mongoose will consider that a validation error. Errors returned after failed validation contain an `errors` object whose values are `ValidatorError` objects. Each -[ValidatorError](api/error-validation-js.html#error-validation-js) has `kind`, `path`, +[ValidatorError](api/error.html#error_Error-ValidatorError) has `kind`, `path`, `value`, and `message` properties. A ValidatorError also may have a `reason` property. If an error was thrown in the validator, this property will contain the error that was @@ -142,10 +142,10 @@ nested objects are not fully fledged paths. ### Update Validators In the above examples, you learned about document validation. Mongoose also -supports validation for [`update()`](query.html#query_Query-update), -[`updateOne()`](query.html#query_Query-updateOne), -[`updateMany()`](query.html#query_Query-updateMany), -and [`findOneAndUpdate()`](query.html#query_Query-findOneAndUpdate) operations. +supports validation for [`update()`](api/query.html#query_Query-update), +[`updateOne()`](api/query.html#query_Query-updateOne), +[`updateMany()`](api/query.html#query_Query-updateMany), +and [`findOneAndUpdate()`](api/query.html#query_Query-findOneAndUpdate) operations. Update validators are off by default - you need to specify the `runValidators` option. From 2dd8bf3d005f1db913ffb1107b33107b488ed0da Mon Sep 17 00:00:00 2001 From: hasezoey Date: Mon, 23 Jan 2023 20:34:29 +0100 Subject: [PATCH 05/10] test(aggregate): increase timeout for a "beforeEach" hook because tests were failing because of this, hopefully this fixes it (or at least lowers it) --- test/aggregate.test.js | 1 + 1 file changed, 1 insertion(+) diff --git a/test/aggregate.test.js b/test/aggregate.test.js index 91acbf8716e..2e052a8e564 100644 --- a/test/aggregate.test.js +++ b/test/aggregate.test.js @@ -614,6 +614,7 @@ describe('aggregate: ', function() { describe('exec', function() { beforeEach(async function() { + this.timeout(4000); // double the default of 2 seconds await setupData(db); }); From 48b5edb6ccf2be0d07007af00367c0abc144142a Mon Sep 17 00:00:00 2001 From: Valeri Karpov Date: Mon, 23 Jan 2023 16:45:12 -0500 Subject: [PATCH 06/10] test: try skipping global strictQuery test to avoid deno test failures --- test/schema.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/schema.test.js b/test/schema.test.js index dec8be82d56..02ca3c06189 100644 --- a/test/schema.test.js +++ b/test/schema.test.js @@ -2452,7 +2452,7 @@ describe('schema', function() { assert.ok(!schema.virtuals.id); }); - describe('mongoose.set(`strictQuery`, value); (gh-6658)', function() { + describe.skip('mongoose.set(`strictQuery`, value); (gh-6658)', function() { let strictQueryOriginalValue; this.beforeEach(() => strictQueryOriginalValue = mongoose.get('strictQuery')); From 57fa99d0f63601f0dfd3643116c5fd5f59203708 Mon Sep 17 00:00:00 2001 From: Valeri Karpov Date: Mon, 23 Jan 2023 16:51:37 -0500 Subject: [PATCH 07/10] test: fix global cleanup test on deno re: #12902 --- test/schema.test.js | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/test/schema.test.js b/test/schema.test.js index 02ca3c06189..723049dbf85 100644 --- a/test/schema.test.js +++ b/test/schema.test.js @@ -2452,18 +2452,14 @@ describe('schema', function() { assert.ok(!schema.virtuals.id); }); - describe.skip('mongoose.set(`strictQuery`, value); (gh-6658)', function() { - let strictQueryOriginalValue; - - this.beforeEach(() => strictQueryOriginalValue = mongoose.get('strictQuery')); - this.afterEach(() => mongoose.set('strictQuery', strictQueryOriginalValue)); - + describe('mongoose.set(`strictQuery`, value); (gh-6658)', function() { it('setting `strictQuery` on base sets strictQuery to schema (gh-6658)', function() { // Arrange - mongoose.set('strictQuery', 'some value'); + const m = new mongoose.Mongoose(); + m.set('strictQuery', 'some value'); // Act - const schema = new Schema(); + const schema = new m.Schema(); // Assert assert.equal(schema.get('strictQuery'), 'some value'); @@ -2471,10 +2467,11 @@ describe('schema', function() { it('`strictQuery` set on base gets overwritten by option set on schema (gh-6658)', function() { // Arrange - mongoose.set('strictQuery', 'base option'); + const m = new mongoose.Mongoose(); + m.set('strictQuery', 'base option'); // Act - const schema = new Schema({}, { strictQuery: 'schema option' }); + const schema = new m.Schema({}, { strictQuery: 'schema option' }); // Assert assert.equal(schema.get('strictQuery'), 'schema option'); From 08f8559c8150d21bac3bbff5bef56af7df9a67ae Mon Sep 17 00:00:00 2001 From: Valeri Karpov Date: Mon, 23 Jan 2023 17:16:04 -0500 Subject: [PATCH 08/10] Update test/collection.capped.test.js Co-authored-by: hasezoey --- test/collection.capped.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/collection.capped.test.js b/test/collection.capped.test.js index 786bbe39d68..13816723612 100644 --- a/test/collection.capped.test.js +++ b/test/collection.capped.test.js @@ -53,7 +53,7 @@ describe('collections: capped:', function() { }); it('skips when setting autoCreate to false (gh-8566)', async function() { - const db = start(); + db = start(); this.timeout(30000); await db.dropDatabase(); From bf2ad981252c6a3330641843ff97a17a0752b3ac Mon Sep 17 00:00:00 2001 From: Valeri Karpov Date: Mon, 23 Jan 2023 17:16:08 -0500 Subject: [PATCH 09/10] Update test/collection.test.js Co-authored-by: hasezoey --- test/collection.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/collection.test.js b/test/collection.test.js index 324bb531ae5..8b4fa71ea4c 100644 --- a/test/collection.test.js +++ b/test/collection.test.js @@ -10,7 +10,7 @@ const mongoose = start.mongoose; describe('collections:', function() { let db = null; - this.afterEach(async function() { + afterEach(async function() { if (db == null) { return; } From d4433f57d02753c97d0dd4eb5c6fdb4ecafbb850 Mon Sep 17 00:00:00 2001 From: Valeri Karpov Date: Mon, 23 Jan 2023 17:19:22 -0500 Subject: [PATCH 10/10] test: clean up db handle in bson size error test --- test/model.test.js | 57 +++++++++++++++++++++++++++------------------- 1 file changed, 33 insertions(+), 24 deletions(-) diff --git a/test/model.test.js b/test/model.test.js index 389c84e3508..7d71d07783c 100644 --- a/test/model.test.js +++ b/test/model.test.js @@ -4323,40 +4323,49 @@ describe('Model', function() { }); }); - it('save max bson size error with buffering (gh-3906)', async function() { - this.timeout(10000); - const db = start({ noErrorListener: true }); - const Test = db.model('Test', { name: Object }); + describe('max bson size error', function() { + let db; - const test = new Test({ - name: { - data: (new Array(16 * 1024 * 1024)).join('x') + afterEach(async() => { + if (db != null) { + await db.close(); + db = null; } }); - const error = await test.save().then(() => null, err => err); + it('save max bson size error with buffering (gh-3906)', async function() { + this.timeout(10000); + db = start({ noErrorListener: true }); + const Test = db.model('Test', { name: Object }); - assert.ok(error); - assert.equal(error.name, 'MongoServerError'); - await db.close(); - }); + const test = new Test({ + name: { + data: (new Array(16 * 1024 * 1024)).join('x') + } + }); - it('reports max bson size error in save (gh-3906)', async function() { - this.timeout(10000); - const db = await start({ noErrorListener: true }); - const Test = db.model('Test', { name: Object }); + const error = await test.save().then(() => null, err => err); - const test = new Test({ - name: { - data: (new Array(16 * 1024 * 1024)).join('x') - } + assert.ok(error); + assert.equal(error.name, 'MongoServerError'); }); - const error = await test.save().then(() => null, err => err); + it('reports max bson size error in save (gh-3906)', async function() { + this.timeout(10000); + db = await start({ noErrorListener: true }); + const Test = db.model('Test', { name: Object }); - assert.ok(error); - assert.equal(error.name, 'MongoServerError'); - await db.close(); + const test = new Test({ + name: { + data: (new Array(16 * 1024 * 1024)).join('x') + } + }); + + const error = await test.save().then(() => null, err => err); + + assert.ok(error); + assert.equal(error.name, 'MongoServerError'); + }); }); describe('insertMany()', function() {