From 0a668ee15a8f3cc59c3edc1e3c6da1e5311b1b93 Mon Sep 17 00:00:00 2001 From: Kwabena Ampofo Date: Wed, 27 Apr 2022 15:21:52 -0400 Subject: [PATCH 01/14] chore(NODE-3565): Error for insertMany with partially empty array is unhelpful --- src/bulk/common.ts | 4 ++ test/integration/crud/bulk.test.js | 66 ++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+) diff --git a/src/bulk/common.ts b/src/bulk/common.ts index 36afe6d04d..05c4975d58 100644 --- a/src/bulk/common.ts +++ b/src/bulk/common.ts @@ -1,3 +1,4 @@ +/* eslint-disable prettier/prettier */ import { BSONSerializeOptions, Document, @@ -1133,6 +1134,9 @@ export abstract class BulkOperationBase { /** Specifies a raw operation to perform in the bulk write. */ raw(op: AnyBulkWriteOperation): this { + if (!op) { + throw new MongoInvalidArgumentError('Cannot perform bulkwrite operation on undefined operation'); + } if ('insertOne' in op) { const forceServerObjectId = shouldForceServerObjectId(this); if (op.insertOne && op.insertOne.document == null) { diff --git a/test/integration/crud/bulk.test.js b/test/integration/crud/bulk.test.js index bfd9a897ee..1a1a0664e0 100644 --- a/test/integration/crud/bulk.test.js +++ b/test/integration/crud/bulk.test.js @@ -23,6 +23,72 @@ describe('Bulk', function () { return setupDatabase(this.configuration); }); + context('ops tests', () => { + // eslint-disable-next-line no-restricted-properties + it.only('Should raise an error when attempting bulkwrite operation on undefined operation', function (done) { + var configuration = this.configuration; + const client = configuration.newClient(); + const docs = []; + docs[1] = {}; // works for docs[0] = {} + // client.db('test').collection('test').insertMany(docs); + // client.close(); + + let error = null; + let result = null; + + client + .connect() + .then(function (client) { + return client.db('test').collection('test').insertMany(docs); + }) + .then(function (r) { + result = r; + }) + .catch(function (err) { + console.log(err); + error = err; + }) + .then(function () { + expect(result).to.not.exist; + test.ok(error); + console.log(error, 'KOBBY'); + client.close(done); + }); + }); + + // eslint-disable-next-line no-restricted-properties + it.only('Should not raise an error when attempting bulkwrite operation on undefined operation', function (done) { + var configuration = this.configuration; + const client = configuration.newClient(); + const docs = []; + docs[0] = {}; // works for docs[0] = {} + // client.db('test').collection('test').insertMany(docs); + // client.close(); + + let error = null; + let result = null; + + client + .connect() + .then(function (client) { + return client.db('test').collection('test').insertMany(docs); + }) + .then(function (r) { + result = r; + }) + .catch(function (err) { + console.log(err); + error = err; + }) + .then(function () { + expect(error).to.not.exist; + console.log(result, 'KOBBY'); + test.ok(result); + client.close(done); + }); + }); + }); + context('promise tests', () => { it('Should correctly execute unordered bulk operation in promise form', function (done) { const configuration = this.configuration; From 7ce28fc8c1681ce5b5ffb353000c91c6a272f6c1 Mon Sep 17 00:00:00 2001 From: Kwabena Ampofo Date: Thu, 28 Apr 2022 17:59:36 -0400 Subject: [PATCH 02/14] write tests for code changes --- test/integration/crud/bulk.test.js | 62 +++++++++++++++--------------- 1 file changed, 31 insertions(+), 31 deletions(-) diff --git a/test/integration/crud/bulk.test.js b/test/integration/crud/bulk.test.js index 1a1a0664e0..ab13c0abdf 100644 --- a/test/integration/crud/bulk.test.js +++ b/test/integration/crud/bulk.test.js @@ -7,7 +7,12 @@ const { ignoreNsNotFound, assert: test } = require('../shared'); -const { Long, MongoBatchReExecutionError, MongoDriverError } = require('../../../src'); +const { + Long, + MongoBatchReExecutionError, + MongoDriverError, + MongoInvalidArgumentError +} = require('../../../src'); const crypto = require('crypto'); const chai = require('chai'); @@ -24,47 +29,42 @@ describe('Bulk', function () { }); context('ops tests', () => { - // eslint-disable-next-line no-restricted-properties - it.only('Should raise an error when attempting bulkwrite operation on undefined operation', function (done) { + it('Should raise an error when attempting bulkwrite operation on undefined operation', async function () { var configuration = this.configuration; const client = configuration.newClient(); - const docs = []; - docs[1] = {}; // works for docs[0] = {} - // client.db('test').collection('test').insertMany(docs); - // client.close(); + await client.connect(); - let error = null; - let result = null; + try { + client.db('test').collection('test').initializeUnorderedBulkOp().raw(undefined); + expect.fail('Failure to throw error'); + } catch (error) { + expect(error).to.be.instanceOf(MongoInvalidArgumentError); + } + await client.close(); + }); - client - .connect() - .then(function (client) { - return client.db('test').collection('test').insertMany(docs); - }) - .then(function (r) { - result = r; - }) - .catch(function (err) { - console.log(err); - error = err; - }) - .then(function () { - expect(result).to.not.exist; - test.ok(error); - console.log(error, 'KOBBY'); - client.close(done); - }); + it('Should not raise an error when attempting bulkwrite operation on undefined operation', async function () { + var configuration = this.configuration; + const client = configuration.newClient(); + await client.connect(); + + try { + client.db('test').collection('test').initializeUnorderedBulkOp().raw({ insertOne: {} }); + } catch (error) { + expect(error).not.to.exist; + } + await client.close(); }); // eslint-disable-next-line no-restricted-properties - it.only('Should not raise an error when attempting bulkwrite operation on undefined operation', function (done) { + it('Should not raise an error when attempting bulkwrite operation on undefined operation', function (done) { var configuration = this.configuration; const client = configuration.newClient(); - const docs = []; - docs[0] = {}; // works for docs[0] = {} + // const docs = []; + // docs[0] = {}; // works for docs[0] = {} // client.db('test').collection('test').insertMany(docs); // client.close(); - + client.connect(); let error = null; let result = null; From 73cb6fb4ceb5d0a1988c8f09044a7bc3fdf11191 Mon Sep 17 00:00:00 2001 From: Kwabena Ampofo Date: Fri, 29 Apr 2022 11:04:43 -0400 Subject: [PATCH 03/14] Delete unecessary test --- test/integration/crud/bulk.test.js | 32 ------------------------------ 1 file changed, 32 deletions(-) diff --git a/test/integration/crud/bulk.test.js b/test/integration/crud/bulk.test.js index ab13c0abdf..cb30794de1 100644 --- a/test/integration/crud/bulk.test.js +++ b/test/integration/crud/bulk.test.js @@ -55,38 +55,6 @@ describe('Bulk', function () { } await client.close(); }); - - // eslint-disable-next-line no-restricted-properties - it('Should not raise an error when attempting bulkwrite operation on undefined operation', function (done) { - var configuration = this.configuration; - const client = configuration.newClient(); - // const docs = []; - // docs[0] = {}; // works for docs[0] = {} - // client.db('test').collection('test').insertMany(docs); - // client.close(); - client.connect(); - let error = null; - let result = null; - - client - .connect() - .then(function (client) { - return client.db('test').collection('test').insertMany(docs); - }) - .then(function (r) { - result = r; - }) - .catch(function (err) { - console.log(err); - error = err; - }) - .then(function () { - expect(error).to.not.exist; - console.log(result, 'KOBBY'); - test.ok(result); - client.close(done); - }); - }); }); context('promise tests', () => { From adffcfc9da3dd0cc4b8f24ea49b9928c6202fd6b Mon Sep 17 00:00:00 2001 From: Kwabena Ampofo Date: Fri, 29 Apr 2022 12:44:47 -0400 Subject: [PATCH 04/14] address comments, code clean up --- src/bulk/common.ts | 3 +- test/integration/crud/bulk.test.js | 47 +++++++++++++++--------------- 2 files changed, 24 insertions(+), 26 deletions(-) diff --git a/src/bulk/common.ts b/src/bulk/common.ts index 05c4975d58..873b4cb2e8 100644 --- a/src/bulk/common.ts +++ b/src/bulk/common.ts @@ -1,4 +1,3 @@ -/* eslint-disable prettier/prettier */ import { BSONSerializeOptions, Document, @@ -1135,7 +1134,7 @@ export abstract class BulkOperationBase { /** Specifies a raw operation to perform in the bulk write. */ raw(op: AnyBulkWriteOperation): this { if (!op) { - throw new MongoInvalidArgumentError('Cannot perform bulkwrite operation on undefined operation'); + throw new MongoInvalidArgumentError('Cannot call .raw() with an undefined operation'); } if ('insertOne' in op) { const forceServerObjectId = shouldForceServerObjectId(this); diff --git a/test/integration/crud/bulk.test.js b/test/integration/crud/bulk.test.js index cb30794de1..309812211e 100644 --- a/test/integration/crud/bulk.test.js +++ b/test/integration/crud/bulk.test.js @@ -27,33 +27,32 @@ describe('Bulk', function () { before(function () { return setupDatabase(this.configuration); }); + describe('class BulkOperationBase', () => { + context('.raw()', () => { + it('should throw a MongoInvalidArgument error when called with an undefined operation', async function () { + const client = this.configuration.newClient(); + await client.connect(); - context('ops tests', () => { - it('Should raise an error when attempting bulkwrite operation on undefined operation', async function () { - var configuration = this.configuration; - const client = configuration.newClient(); - await client.connect(); - - try { - client.db('test').collection('test').initializeUnorderedBulkOp().raw(undefined); - expect.fail('Failure to throw error'); - } catch (error) { - expect(error).to.be.instanceOf(MongoInvalidArgumentError); - } - await client.close(); - }); + try { + client.db('test').collection('test').initializeUnorderedBulkOp().raw(undefined); + expect.fail('Failure to throw error'); + } catch (error) { + expect(error).to.be.instanceOf(MongoInvalidArgumentError); + } + await client.close(); + }); - it('Should not raise an error when attempting bulkwrite operation on undefined operation', async function () { - var configuration = this.configuration; - const client = configuration.newClient(); - await client.connect(); + it('should not throw a MongoInvalidArgument error when called with a valid operation', async function () { + const client = this.configuration.newClient(); + await client.connect(); - try { - client.db('test').collection('test').initializeUnorderedBulkOp().raw({ insertOne: {} }); - } catch (error) { - expect(error).not.to.exist; - } - await client.close(); + try { + client.db('test').collection('test').initializeUnorderedBulkOp().raw({ insertOne: {} }); + } catch (error) { + expect(error).not.to.exist; + } + await client.close(); + }); }); }); From c1d88f79a1cf709186834e55b49aac2e1b04aba8 Mon Sep 17 00:00:00 2001 From: Kwabena Ampofo Date: Fri, 29 Apr 2022 16:01:46 -0400 Subject: [PATCH 05/14] Clean up tests address comments --- test/integration/crud/bulk.test.js | 72 +++++++++++++++++++++++------- 1 file changed, 55 insertions(+), 17 deletions(-) diff --git a/test/integration/crud/bulk.test.js b/test/integration/crud/bulk.test.js index 309812211e..27ce8cfe01 100644 --- a/test/integration/crud/bulk.test.js +++ b/test/integration/crud/bulk.test.js @@ -27,32 +27,70 @@ describe('Bulk', function () { before(function () { return setupDatabase(this.configuration); }); - describe('class BulkOperationBase', () => { - context('.raw()', () => { - it('should throw a MongoInvalidArgument error when called with an undefined operation', async function () { + describe('BulkOperationBase', () => { + describe('#raw', function () { + beforeEach(async function () { const client = this.configuration.newClient(); await client.connect(); - - try { - client.db('test').collection('test').initializeUnorderedBulkOp().raw(undefined); - expect.fail('Failure to throw error'); - } catch (error) { - expect(error).to.be.instanceOf(MongoInvalidArgumentError); - } + }); + afterEach(async function () { await client.close(); }); + context('when called with an undefined operation', function () { + it('should throw a MongoInvalidArgument error ', async function () { + try { + client.db('test').collection('test').initializeUnorderedBulkOp().raw(undefined); + expect.fail('Failure to throw error'); + } catch (error) { + expect(error).to.be.instanceOf(MongoInvalidArgumentError); + } + }); + }); - it('should not throw a MongoInvalidArgument error when called with a valid operation', async function () { + context('when called with a valid operation', function () { + it('should not throw a MongoInvalidArgument error', async function () { + try { + client.db('test').collection('test').initializeUnorderedBulkOp().raw({ insertOne: {} }); + } catch (error) { + expect(error).not.to.exist; + } + }); + }); + }); + }); + + describe('Db.collection', function () { + describe('#insertMany', function () { + beforeEach(async function () { const client = this.configuration.newClient(); await client.connect(); - - try { - client.db('test').collection('test').initializeUnorderedBulkOp().raw({ insertOne: {} }); - } catch (error) { - expect(error).not.to.exist; - } + }); + afterEach(async function () { await client.close(); }); + context('when passed an invalid or sparse list', function () { + it('insertmany should throw a MongoInvalidArgument error when called with a valid operation', async function () { + try { + const docs = []; + docs[1] = {}; // works for docs[0] = {} + await client.db('test').collection('test').insertMany(docs); + expect.fail('Failure to throw error'); + } catch (error) { + expect(error).to.be.instanceOf(MongoInvalidArgumentError); + } + }); + }); + context('when passed a valid document list', function () { + it('insertmany should not throw a MongoInvalidArgument error when called with a valid operation', async function () { + try { + const docs = []; + docs[0] = {}; // works for docs[0] = {} + await client.db('test').collection('test').insertMany(docs); + } catch (error) { + expect(error).not.to.exist; + } + }); + }); }); }); From 4524a7115f57f703310e85684be2e1f8c214d36a Mon Sep 17 00:00:00 2001 From: Kwabena Ampofo Date: Mon, 2 May 2022 13:46:54 -0400 Subject: [PATCH 06/14] Close clients properly to fix evergreen time-outs --- test/integration/crud/bulk.test.js | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/test/integration/crud/bulk.test.js b/test/integration/crud/bulk.test.js index 27ce8cfe01..88993d27ef 100644 --- a/test/integration/crud/bulk.test.js +++ b/test/integration/crud/bulk.test.js @@ -27,10 +27,12 @@ describe('Bulk', function () { before(function () { return setupDatabase(this.configuration); }); - describe('BulkOperationBase', () => { + // eslint-disable-next-line no-restricted-properties + describe.only('BulkOperationBase', () => { describe('#raw', function () { + let client; beforeEach(async function () { - const client = this.configuration.newClient(); + client = this.configuration.newClient(); await client.connect(); }); afterEach(async function () { @@ -59,17 +61,19 @@ describe('Bulk', function () { }); }); - describe('Db.collection', function () { + // eslint-disable-next-line no-restricted-properties + describe.only('Db.collection', function () { describe('#insertMany', function () { + let client; beforeEach(async function () { - const client = this.configuration.newClient(); + client = this.configuration.newClient(); await client.connect(); }); afterEach(async function () { await client.close(); }); context('when passed an invalid or sparse list', function () { - it('insertmany should throw a MongoInvalidArgument error when called with a valid operation', async function () { + it('insertMany should throw a MongoInvalidArgument error when called with a valid operation', async function () { try { const docs = []; docs[1] = {}; // works for docs[0] = {} @@ -81,11 +85,12 @@ describe('Bulk', function () { }); }); context('when passed a valid document list', function () { - it('insertmany should not throw a MongoInvalidArgument error when called with a valid operation', async function () { + it('insertMany should not throw a MongoInvalidArgument error when called with a valid operation', async function () { try { const docs = []; docs[0] = {}; // works for docs[0] = {} - await client.db('test').collection('test').insertMany(docs); + let result = await client.db('test').collection('test').insertMany(docs); + expect(result).to.be.true; } catch (error) { expect(error).not.to.exist; } From b16ced4c59c6034ffe4292749dfb2cd140246121 Mon Sep 17 00:00:00 2001 From: Kwabena Ampofo Date: Mon, 2 May 2022 14:15:02 -0400 Subject: [PATCH 07/14] Fix failing test --- test/integration/crud/bulk.test.js | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/test/integration/crud/bulk.test.js b/test/integration/crud/bulk.test.js index 88993d27ef..4f46ba4b1d 100644 --- a/test/integration/crud/bulk.test.js +++ b/test/integration/crud/bulk.test.js @@ -27,8 +27,7 @@ describe('Bulk', function () { before(function () { return setupDatabase(this.configuration); }); - // eslint-disable-next-line no-restricted-properties - describe.only('BulkOperationBase', () => { + describe('BulkOperationBase', () => { describe('#raw', function () { let client; beforeEach(async function () { @@ -61,8 +60,7 @@ describe('Bulk', function () { }); }); - // eslint-disable-next-line no-restricted-properties - describe.only('Db.collection', function () { + describe('Db.collection', function () { describe('#insertMany', function () { let client; beforeEach(async function () { @@ -76,9 +74,9 @@ describe('Bulk', function () { it('insertMany should throw a MongoInvalidArgument error when called with a valid operation', async function () { try { const docs = []; - docs[1] = {}; // works for docs[0] = {} + docs[1] = { color: 'red' }; await client.db('test').collection('test').insertMany(docs); - expect.fail('Failure to throw error'); + expect.fail('Expected insertMany to throw error, failed to throw error'); } catch (error) { expect(error).to.be.instanceOf(MongoInvalidArgumentError); } @@ -87,10 +85,11 @@ describe('Bulk', function () { context('when passed a valid document list', function () { it('insertMany should not throw a MongoInvalidArgument error when called with a valid operation', async function () { try { - const docs = []; - docs[0] = {}; // works for docs[0] = {} - let result = await client.db('test').collection('test').insertMany(docs); - expect(result).to.be.true; + let result = await client + .db('test') + .collection('test') + .insertMany([{ color: 'blue' }]); + expect(result).to.exist; } catch (error) { expect(error).not.to.exist; } From 52af24c38f0efdac026db566e48e6fb6de889ca4 Mon Sep 17 00:00:00 2001 From: Kwabena Ampofo Date: Mon, 2 May 2022 16:32:46 -0400 Subject: [PATCH 08/14] Update test to be more specific --- src/bulk/common.ts | 4 ++-- test/integration/crud/bulk.test.js | 13 ++++++------- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/src/bulk/common.ts b/src/bulk/common.ts index 873b4cb2e8..9a050f918a 100644 --- a/src/bulk/common.ts +++ b/src/bulk/common.ts @@ -1133,8 +1133,8 @@ export abstract class BulkOperationBase { /** Specifies a raw operation to perform in the bulk write. */ raw(op: AnyBulkWriteOperation): this { - if (!op) { - throw new MongoInvalidArgumentError('Cannot call .raw() with an undefined operation'); + if (op == null || typeof op !== 'object') { + throw new MongoInvalidArgumentError('Operation must be an object with an operation key'); } if ('insertOne' in op) { const forceServerObjectId = shouldForceServerObjectId(this); diff --git a/test/integration/crud/bulk.test.js b/test/integration/crud/bulk.test.js index 4f46ba4b1d..e9e3a09464 100644 --- a/test/integration/crud/bulk.test.js +++ b/test/integration/crud/bulk.test.js @@ -27,7 +27,8 @@ describe('Bulk', function () { before(function () { return setupDatabase(this.configuration); }); - describe('BulkOperationBase', () => { + // eslint-disable-next-line no-restricted-properties + describe.only('BulkOperationBase', () => { describe('#raw', function () { let client; beforeEach(async function () { @@ -39,12 +40,10 @@ describe('Bulk', function () { }); context('when called with an undefined operation', function () { it('should throw a MongoInvalidArgument error ', async function () { - try { - client.db('test').collection('test').initializeUnorderedBulkOp().raw(undefined); - expect.fail('Failure to throw error'); - } catch (error) { - expect(error).to.be.instanceOf(MongoInvalidArgumentError); - } + const bulkOp = client.db('test').collection('test').initializeUnorderedBulkOp(); + expect(() => bulkOp.raw(undefined)).to.throw(MongoInvalidArgumentError); + expect(() => bulkOp.raw(true)).to.throw(MongoInvalidArgumentError); + expect(() => bulkOp.raw(3)).to.throw(MongoInvalidArgumentError); }); }); From f7cce9779956a26199140ef21b5c6e95c0e9bcfa Mon Sep 17 00:00:00 2001 From: Kwabena Ampofo Date: Mon, 2 May 2022 16:45:58 -0400 Subject: [PATCH 09/14] Remove .only from test --- test/integration/crud/bulk.test.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/test/integration/crud/bulk.test.js b/test/integration/crud/bulk.test.js index e9e3a09464..33041dbd6c 100644 --- a/test/integration/crud/bulk.test.js +++ b/test/integration/crud/bulk.test.js @@ -27,8 +27,7 @@ describe('Bulk', function () { before(function () { return setupDatabase(this.configuration); }); - // eslint-disable-next-line no-restricted-properties - describe.only('BulkOperationBase', () => { + describe('BulkOperationBase', () => { describe('#raw', function () { let client; beforeEach(async function () { From d1e02242740e00deec663ffd0a7f810fb9cd9232 Mon Sep 17 00:00:00 2001 From: Kwabena Ampofo Date: Wed, 4 May 2022 15:45:56 -0400 Subject: [PATCH 10/14] Throw specific error in the case of a sparse array, create new test to check the expected error message has not been changed, fix grammar issues --- src/operations/insert.ts | 9 ++++++++- test/integration/crud/bulk.test.js | 21 ++++++++++++++++++--- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/src/operations/insert.ts b/src/operations/insert.ts index 4a31017bff..f2cccb20a4 100644 --- a/src/operations/insert.ts +++ b/src/operations/insert.ts @@ -136,7 +136,14 @@ export class InsertManyOperation extends AbstractOperation { ); bulkWriteOperation.execute(server, session, (err, res) => { - if (err || res == null) return callback(err); + if (err || res == null) { + if (err && err.message === 'Operation must be an object with an operation key') { + err = new MongoInvalidArgumentError( + 'Argument "docs" is a sparse array containing element(s) that are null or undefined' + ); + } + return callback(err); + } callback(undefined, { acknowledged: writeConcern?.w !== 0 ?? true, insertedCount: res.insertedCount, diff --git a/test/integration/crud/bulk.test.js b/test/integration/crud/bulk.test.js index 33041dbd6c..71f51985f2 100644 --- a/test/integration/crud/bulk.test.js +++ b/test/integration/crud/bulk.test.js @@ -28,7 +28,8 @@ describe('Bulk', function () { return setupDatabase(this.configuration); }); describe('BulkOperationBase', () => { - describe('#raw', function () { + // eslint-disable-next-line no-restricted-properties + describe.only('#raw', function () { let client; beforeEach(async function () { client = this.configuration.newClient(); @@ -44,6 +45,18 @@ describe('Bulk', function () { expect(() => bulkOp.raw(true)).to.throw(MongoInvalidArgumentError); expect(() => bulkOp.raw(3)).to.throw(MongoInvalidArgumentError); }); + + it('should throw an error with the specifc message: "Operation must be an object with an operation key"', async function () { + const bulkOp = client.db('test').collection('test').initializeUnorderedBulkOp(); + try { + bulkOp.raw(undefined); + expect.fail( + 'Expected passing argument of "undefined" to .raw to throw error, failed to throw error' + ); + } catch (error) { + expect(error.message).to.equal('Operation must be an object with an operation key'); + } + }); }); context('when called with a valid operation', function () { @@ -58,6 +71,8 @@ describe('Bulk', function () { }); }); + //write test to check if error thrown at raw op hasnt changed + describe('Db.collection', function () { describe('#insertMany', function () { let client; @@ -68,8 +83,8 @@ describe('Bulk', function () { afterEach(async function () { await client.close(); }); - context('when passed an invalid or sparse list', function () { - it('insertMany should throw a MongoInvalidArgument error when called with a valid operation', async function () { + context('when passed an invalid docs argument', function () { + it('insertMany should throw a MongoInvalidArgument error when called with a invalid operation', async function () { try { const docs = []; docs[1] = { color: 'red' }; From 77f18aa0eb6b7bf3a82f5bdacb7cbc6030b162a7 Mon Sep 17 00:00:00 2001 From: Kwabena Ampofo Date: Wed, 4 May 2022 15:49:31 -0400 Subject: [PATCH 11/14] Remove unecessary comment, and .only from test --- test/integration/crud/bulk.test.js | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/test/integration/crud/bulk.test.js b/test/integration/crud/bulk.test.js index 71f51985f2..624864759e 100644 --- a/test/integration/crud/bulk.test.js +++ b/test/integration/crud/bulk.test.js @@ -28,8 +28,7 @@ describe('Bulk', function () { return setupDatabase(this.configuration); }); describe('BulkOperationBase', () => { - // eslint-disable-next-line no-restricted-properties - describe.only('#raw', function () { + describe('#raw', function () { let client; beforeEach(async function () { client = this.configuration.newClient(); @@ -71,8 +70,6 @@ describe('Bulk', function () { }); }); - //write test to check if error thrown at raw op hasnt changed - describe('Db.collection', function () { describe('#insertMany', function () { let client; From deb3196b39c08821be4d9a0245d00e8b4b668b22 Mon Sep 17 00:00:00 2001 From: Kwabena Ampofo Date: Wed, 4 May 2022 17:41:05 -0400 Subject: [PATCH 12/14] Address comments --- src/operations/insert.ts | 2 +- test/integration/crud/bulk.test.js | 16 +++++++--------- 2 files changed, 8 insertions(+), 10 deletions(-) diff --git a/src/operations/insert.ts b/src/operations/insert.ts index f2cccb20a4..6475a5cfe6 100644 --- a/src/operations/insert.ts +++ b/src/operations/insert.ts @@ -139,7 +139,7 @@ export class InsertManyOperation extends AbstractOperation { if (err || res == null) { if (err && err.message === 'Operation must be an object with an operation key') { err = new MongoInvalidArgumentError( - 'Argument "docs" is a sparse array containing element(s) that are null or undefined' + 'Collection.insertMany() cannot be called with an array that has null/undefined values' ); } return callback(err); diff --git a/test/integration/crud/bulk.test.js b/test/integration/crud/bulk.test.js index 624864759e..0a5fb5113d 100644 --- a/test/integration/crud/bulk.test.js +++ b/test/integration/crud/bulk.test.js @@ -47,14 +47,9 @@ describe('Bulk', function () { it('should throw an error with the specifc message: "Operation must be an object with an operation key"', async function () { const bulkOp = client.db('test').collection('test').initializeUnorderedBulkOp(); - try { - bulkOp.raw(undefined); - expect.fail( - 'Expected passing argument of "undefined" to .raw to throw error, failed to throw error' - ); - } catch (error) { - expect(error.message).to.equal('Operation must be an object with an operation key'); - } + expect(() => bulkOp.raw(undefined)) + .to.throw(MongoInvalidArgumentError) + .to.match(/Operation must be an object with an operation key/); }); }); @@ -81,7 +76,7 @@ describe('Bulk', function () { await client.close(); }); context('when passed an invalid docs argument', function () { - it('insertMany should throw a MongoInvalidArgument error when called with a invalid operation', async function () { + it('insertMany should throw a MongoInvalidArgument error when called with an invalid operation', async function () { try { const docs = []; docs[1] = { color: 'red' }; @@ -89,6 +84,9 @@ describe('Bulk', function () { expect.fail('Expected insertMany to throw error, failed to throw error'); } catch (error) { expect(error).to.be.instanceOf(MongoInvalidArgumentError); + expect(error.message).to.equal( + 'Collection.insertMany() cannot be called with an array that has null/undefined values' + ); } }); }); From 691fb790f2d29aae895ef3136e577d30cc3ba46e Mon Sep 17 00:00:00 2001 From: Kwabena Ampofo Date: Thu, 5 May 2022 10:29:59 -0400 Subject: [PATCH 13/14] improve test wording --- test/integration/crud/bulk.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/integration/crud/bulk.test.js b/test/integration/crud/bulk.test.js index 0a5fb5113d..4882c27887 100644 --- a/test/integration/crud/bulk.test.js +++ b/test/integration/crud/bulk.test.js @@ -76,7 +76,7 @@ describe('Bulk', function () { await client.close(); }); context('when passed an invalid docs argument', function () { - it('insertMany should throw a MongoInvalidArgument error when called with an invalid operation', async function () { + it('should throw a MongoInvalidArgument error', async function () { try { const docs = []; docs[1] = { color: 'red' }; From d36af4be5ebd8b49abc906efd51ae238c3f7e08b Mon Sep 17 00:00:00 2001 From: Kwabena Ampofo Date: Thu, 5 May 2022 12:17:21 -0400 Subject: [PATCH 14/14] Fix test description to match convention --- test/integration/crud/bulk.test.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/integration/crud/bulk.test.js b/test/integration/crud/bulk.test.js index 4882c27887..ceb908c0f7 100644 --- a/test/integration/crud/bulk.test.js +++ b/test/integration/crud/bulk.test.js @@ -28,7 +28,7 @@ describe('Bulk', function () { return setupDatabase(this.configuration); }); describe('BulkOperationBase', () => { - describe('#raw', function () { + describe('#raw()', function () { let client; beforeEach(async function () { client = this.configuration.newClient(); @@ -65,8 +65,8 @@ describe('Bulk', function () { }); }); - describe('Db.collection', function () { - describe('#insertMany', function () { + describe('Collection', function () { + describe('#insertMany()', function () { let client; beforeEach(async function () { client = this.configuration.newClient();