Skip to content

Commit 35c7a2c

Browse files
committed
test(NODE-4877): Add tests to check that BSONErrors get thrown
1 parent 4f50116 commit 35c7a2c

File tree

1 file changed

+111
-17
lines changed

1 file changed

+111
-17
lines changed

test/integration/node-specific/bson-options/useBigInt64.test.ts

Lines changed: 111 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { expect } from 'chai';
22

3-
import { Collection, Db, MongoAPIError, MongoClient } from '../../../mongodb';
3+
import { BSON, Collection, Db, MongoAPIError, MongoClient } from '../../../mongodb';
44
import { setupDatabase } from '../../shared.js';
55

66
describe('useBigInt64 option', function () {
@@ -127,25 +127,119 @@ describe('useBigInt64 option', function () {
127127
});
128128
});
129129

130-
describe('when set to true and promoteLongs is set to false', function () {
131-
it('throws a MongoAPIError', async function () {
132-
expect(() => {
133-
configuration.newClient(configuration.writeConcernMax(), {
134-
useBigInt64: true,
135-
promoteLongs: false
136-
});
137-
}).to.throw(MongoAPIError, /Must request either bigint or Long for int64 deserialization/);
130+
describe('when useBigInt64=true and promoteLongs=false', function () {
131+
let client: MongoClient;
132+
133+
afterEach(async function () {
134+
if (client) {
135+
await client.close();
136+
}
137+
});
138+
139+
describe('when set at client level', function () {
140+
it('throws a MongoAPIError', async function () {
141+
expect(() => {
142+
client = configuration.newClient(configuration.writeConcernMax(), {
143+
useBigInt64: true,
144+
promoteLongs: false
145+
});
146+
}).to.throw(MongoAPIError, /Must request either bigint or Long for int64 deserialization/);
147+
});
148+
});
149+
150+
describe('when set at DB level', function () {
151+
it('throws a BSONError', async function () {
152+
client = configuration.newClient(configuration.writeConcernMax());
153+
await client.connect();
154+
const db = client.db('bsonOptions', { promoteLongs: false, useBigInt64: true });
155+
const e = await db.createCollection('bsonError').catch(e => e);
156+
expect(e).to.be.instanceOf(BSON.BSONError);
157+
});
158+
});
159+
160+
describe('when set at collection level', function () {
161+
it('throws a BSONError', async function () {
162+
client = configuration.newClient(configuration.writeConcernMax());
163+
await client.connect();
164+
const db = client.db('bsonOptions');
165+
const e = await db
166+
.createCollection('bsonError', { promoteLongs: false, useBigInt64: true })
167+
.catch(e => e);
168+
expect(e).to.be.instanceOf(BSON.BSONError);
169+
});
170+
});
171+
172+
describe('when set at the operation level', function () {
173+
it('throws a BSONError', async function () {
174+
client = configuration.newClient(configuration.writeConcernMax());
175+
await client.connect();
176+
177+
const db = client.db('bsonOptions');
178+
const coll = db.collection('bsonError');
179+
const e = await coll
180+
.insertOne({ a: 10n }, { promoteLongs: false, useBigInt64: true })
181+
.catch(e => e);
182+
183+
expect(e).to.be.instanceOf(BSON.BSONError);
184+
});
138185
});
139186
});
140187

141-
describe('when set to true and promoteValues is set to false', function () {
142-
it('throws a MongoAPIError', async function () {
143-
expect(() => {
144-
configuration.newClient(configuration.writeConcernMax(), {
145-
useBigInt64: true,
146-
promoteValues: false
147-
});
148-
}).to.throw(MongoAPIError, /Must request either bigint or Long for int64 deserialization/);
188+
describe('when useBigInt64=true and promoteValues=false', function () {
189+
let client: MongoClient;
190+
191+
afterEach(async function () {
192+
if (client) {
193+
await client.close();
194+
}
195+
});
196+
197+
describe('when set at client level', function () {
198+
it('throws a MongoAPIError', async function () {
199+
expect(() => {
200+
client = configuration.newClient(configuration.writeConcernMax(), {
201+
useBigInt64: true,
202+
promoteValues: false
203+
});
204+
}).to.throw(MongoAPIError, /Must request either bigint or Long for int64 deserialization/);
205+
});
206+
});
207+
208+
describe('when set at DB level', function () {
209+
it('throws a BSONError', async function () {
210+
client = configuration.newClient(configuration.writeConcernMax());
211+
await client.connect();
212+
const db = client.db('bsonOptions', { promoteValues: false, useBigInt64: true });
213+
const e = await db.createCollection('bsonError').catch(e => e);
214+
expect(e).to.be.instanceOf(BSON.BSONError);
215+
});
216+
});
217+
218+
describe('when set at collection level', function () {
219+
it('throws a BSONError', async function () {
220+
client = configuration.newClient(configuration.writeConcernMax());
221+
await client.connect();
222+
const db = client.db('bsonOptions');
223+
const e = await db
224+
.createCollection('bsonError', { promoteValues: false, useBigInt64: true })
225+
.catch(e => e);
226+
expect(e).to.be.instanceOf(BSON.BSONError);
227+
});
228+
});
229+
230+
describe('when set at the operation level', function () {
231+
it('throws a BSONError', async function () {
232+
client = configuration.newClient(configuration.writeConcernMax());
233+
await client.connect();
234+
235+
const db = client.db('bsonOptions');
236+
const coll = db.collection('bsonError');
237+
const e = await coll
238+
.insertOne({ a: 10n }, { promoteValues: false, useBigInt64: true })
239+
.catch(e => e);
240+
241+
expect(e).to.be.instanceOf(BSON.BSONError);
242+
});
149243
});
150244
});
151245
});

0 commit comments

Comments
 (0)