Skip to content

Commit

Permalink
test(NODE-4877): Add tests to check that BSONErrors get thrown
Browse files Browse the repository at this point in the history
  • Loading branch information
W-A-James committed Feb 21, 2023
1 parent 4f50116 commit 35c7a2c
Showing 1 changed file with 111 additions and 17 deletions.
128 changes: 111 additions & 17 deletions test/integration/node-specific/bson-options/useBigInt64.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { expect } from 'chai';

import { Collection, Db, MongoAPIError, MongoClient } from '../../../mongodb';
import { BSON, Collection, Db, MongoAPIError, MongoClient } from '../../../mongodb';
import { setupDatabase } from '../../shared.js';

describe('useBigInt64 option', function () {
Expand Down Expand Up @@ -127,25 +127,119 @@ describe('useBigInt64 option', function () {
});
});

describe('when set to true and promoteLongs is set to false', function () {
it('throws a MongoAPIError', async function () {
expect(() => {
configuration.newClient(configuration.writeConcernMax(), {
useBigInt64: true,
promoteLongs: false
});
}).to.throw(MongoAPIError, /Must request either bigint or Long for int64 deserialization/);
describe('when useBigInt64=true and promoteLongs=false', function () {
let client: MongoClient;

afterEach(async function () {
if (client) {
await client.close();
}
});

describe('when set at client level', function () {
it('throws a MongoAPIError', async function () {
expect(() => {
client = configuration.newClient(configuration.writeConcernMax(), {
useBigInt64: true,
promoteLongs: false
});
}).to.throw(MongoAPIError, /Must request either bigint or Long for int64 deserialization/);
});
});

describe('when set at DB level', function () {
it('throws a BSONError', async function () {
client = configuration.newClient(configuration.writeConcernMax());
await client.connect();
const db = client.db('bsonOptions', { promoteLongs: false, useBigInt64: true });
const e = await db.createCollection('bsonError').catch(e => e);
expect(e).to.be.instanceOf(BSON.BSONError);
});
});

describe('when set at collection level', function () {
it('throws a BSONError', async function () {
client = configuration.newClient(configuration.writeConcernMax());
await client.connect();
const db = client.db('bsonOptions');
const e = await db
.createCollection('bsonError', { promoteLongs: false, useBigInt64: true })
.catch(e => e);
expect(e).to.be.instanceOf(BSON.BSONError);
});
});

describe('when set at the operation level', function () {
it('throws a BSONError', async function () {
client = configuration.newClient(configuration.writeConcernMax());
await client.connect();

const db = client.db('bsonOptions');
const coll = db.collection('bsonError');
const e = await coll
.insertOne({ a: 10n }, { promoteLongs: false, useBigInt64: true })
.catch(e => e);

expect(e).to.be.instanceOf(BSON.BSONError);
});
});
});

describe('when set to true and promoteValues is set to false', function () {
it('throws a MongoAPIError', async function () {
expect(() => {
configuration.newClient(configuration.writeConcernMax(), {
useBigInt64: true,
promoteValues: false
});
}).to.throw(MongoAPIError, /Must request either bigint or Long for int64 deserialization/);
describe('when useBigInt64=true and promoteValues=false', function () {
let client: MongoClient;

afterEach(async function () {
if (client) {
await client.close();
}
});

describe('when set at client level', function () {
it('throws a MongoAPIError', async function () {
expect(() => {
client = configuration.newClient(configuration.writeConcernMax(), {
useBigInt64: true,
promoteValues: false
});
}).to.throw(MongoAPIError, /Must request either bigint or Long for int64 deserialization/);
});
});

describe('when set at DB level', function () {
it('throws a BSONError', async function () {
client = configuration.newClient(configuration.writeConcernMax());
await client.connect();
const db = client.db('bsonOptions', { promoteValues: false, useBigInt64: true });
const e = await db.createCollection('bsonError').catch(e => e);
expect(e).to.be.instanceOf(BSON.BSONError);
});
});

describe('when set at collection level', function () {
it('throws a BSONError', async function () {
client = configuration.newClient(configuration.writeConcernMax());
await client.connect();
const db = client.db('bsonOptions');
const e = await db
.createCollection('bsonError', { promoteValues: false, useBigInt64: true })
.catch(e => e);
expect(e).to.be.instanceOf(BSON.BSONError);
});
});

describe('when set at the operation level', function () {
it('throws a BSONError', async function () {
client = configuration.newClient(configuration.writeConcernMax());
await client.connect();

const db = client.db('bsonOptions');
const coll = db.collection('bsonError');
const e = await coll
.insertOne({ a: 10n }, { promoteValues: false, useBigInt64: true })
.catch(e => e);

expect(e).to.be.instanceOf(BSON.BSONError);
});
});
});
});

0 comments on commit 35c7a2c

Please sign in to comment.