Skip to content

fix(NODE-3565): Error for insertMany with partially empty array is unhelpful #3221

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 14 commits into from
May 5, 2022
3 changes: 3 additions & 0 deletions src/bulk/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1133,6 +1133,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 call .raw() with an undefined operation');
}
if ('insertOne' in op) {
const forceServerObjectId = shouldForceServerObjectId(this);
if (op.insertOne && op.insertOne.document == null) {
Expand Down
77 changes: 76 additions & 1 deletion test/integration/crud/bulk.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand All @@ -22,6 +27,76 @@ describe('Bulk', function () {
before(function () {
return setupDatabase(this.configuration);
});
describe('BulkOperationBase', () => {
describe('#raw', function () {
let client;
beforeEach(async function () {
client = this.configuration.newClient();
await client.connect();
});
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);
}
});
});

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 () {
let client;
beforeEach(async function () {
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 () {
try {
const docs = [];
docs[1] = { color: 'red' };
await client.db('test').collection('test').insertMany(docs);
expect.fail('Expected insertMany to throw error, failed 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 {
let result = await client
.db('test')
.collection('test')
.insertMany([{ color: 'blue' }]);
expect(result).to.exist;
} catch (error) {
expect(error).not.to.exist;
}
});
});
});
});

context('promise tests', () => {
it('Should correctly execute unordered bulk operation in promise form', function (done) {
Expand Down