Skip to content

Commit 4d1f84e

Browse files
committed
test(NODE-4873): move EJSON bigint tests
1 parent 78c0513 commit 4d1f84e

File tree

2 files changed

+89
-88
lines changed

2 files changed

+89
-88
lines changed

test/node/bigint.test.ts

Lines changed: 89 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { BSON, BSONError } from '../register-bson';
1+
import { BSON, EJSON, BSONError } from '../register-bson';
22
import { bufferFromHexArray } from './tools/utils';
33
import { expect } from 'chai';
44
import { BSON_DATA_LONG } from '../../src/constants';
@@ -263,4 +263,92 @@ describe('BSON BigInt support', function () {
263263
expect(serializedMap).to.deep.equal(expectedSerialization);
264264
});
265265
});
266+
267+
describe('EJSON.stringify()', function () {
268+
it('truncates bigint values when they are outside the range [BSON_INT64_MIN, BSON_INT64_MAX] in canonical mode', function () {
269+
const numbers = { a: 2n ** 64n + 1n, b: -(2n ** 64n) - 1n };
270+
const serialized = EJSON.stringify(numbers, { relaxed: false });
271+
expect(serialized).to.equal('{"a":{"$numberLong":"1"},"b":{"$numberLong":"-1"}}');
272+
});
273+
274+
it('truncates bigint values in the same way as BSON.serialize in canonical mode', function () {
275+
const number = { a: 0x1234_5678_1234_5678_9999n };
276+
const stringified = EJSON.stringify(number, { relaxed: false });
277+
const serialized = BSON.serialize(number);
278+
279+
const VALUE_OFFSET = 7;
280+
const dataView = BSONDataView.fromUint8Array(serialized);
281+
const serializedValue = dataView.getBigInt64(VALUE_OFFSET, true);
282+
const parsed = JSON.parse(stringified);
283+
284+
expect(parsed).to.have.property('a');
285+
expect(parsed['a']).to.have.property('$numberLong');
286+
expect(parsed.a.$numberLong).to.equal(0x5678_1234_5678_9999n.toString());
287+
288+
expect(parsed.a.$numberLong).to.equal(serializedValue.toString());
289+
});
290+
291+
it('truncates bigint values in the same way as BSON.serialize in relaxed mode', function () {
292+
const number = { a: 0x1234_0000_1234_5678_9999n }; // Ensure that the truncated number can be exactly represented as a JS number
293+
const stringified = EJSON.stringify(number, { relaxed: true });
294+
const serializedDoc = BSON.serialize(number);
295+
296+
const VALUE_OFFSET = 7;
297+
const dataView = BSONDataView.fromUint8Array(serializedDoc);
298+
const parsed = JSON.parse(stringified);
299+
300+
expect(parsed).to.have.property('a');
301+
expect(parsed.a).to.equal(0x0000_1234_5678_9999);
302+
303+
expect(parsed.a).to.equal(Number(dataView.getBigInt64(VALUE_OFFSET, true)));
304+
});
305+
306+
it('serializes bigint values to numberLong in canonical mode', function () {
307+
const number = { a: 2n };
308+
const serialized = EJSON.stringify(number, { relaxed: false });
309+
expect(serialized).to.equal('{"a":{"$numberLong":"2"}}');
310+
});
311+
312+
it('serializes bigint values to Number in relaxed mode', function () {
313+
const number = { a: 10000n };
314+
const serialized = EJSON.stringify(number, { relaxed: true });
315+
expect(serialized).to.equal('{"a":10000}');
316+
});
317+
318+
it('loses precision when serializing bigint values outside of range [Number.MIN_SAFE_INTEGER, Number.MAX_SAFE_INTEGER] in relaxed mode', function () {
319+
const numbers = { a: -(2n ** 53n) - 1n, b: 2n ** 53n + 2n };
320+
const serialized = EJSON.stringify(numbers, { relaxed: true });
321+
expect(serialized).to.equal('{"a":-9007199254740992,"b":9007199254740994}');
322+
});
323+
324+
it('produces bigint strings that pass loose equality checks with native bigint values that are are 64 bits wide or less', function () {
325+
const number = { a: 12345n };
326+
const serialized = EJSON.stringify(number, { relaxed: false });
327+
const parsed = JSON.parse(serialized);
328+
// eslint-disable-next-line eqeqeq
329+
expect(parsed.a.$numberLong == 12345n).true;
330+
});
331+
332+
it('produces bigint strings that are equal to the strings generated when using BigInt.toString when the bigint values used are 64 bits wide or less', function () {
333+
const number = { a: 12345n };
334+
const serialized = EJSON.stringify(number, { relaxed: false });
335+
const parsed = JSON.parse(serialized);
336+
expect(parsed.a.$numberLong).to.equal(12345n.toString());
337+
});
338+
339+
it('produces bigint strings that fail loose equality checks with native bigint values that are more than 64 bits wide', function () {
340+
const number = { a: 0x1234_5678_1234_5678_9999n };
341+
const serialized = EJSON.stringify(number, { relaxed: false });
342+
const parsed = JSON.parse(serialized);
343+
// eslint-disable-next-line eqeqeq
344+
expect(parsed.a.$numberLong == 0x1234_5678_1234_5678_9999n).false;
345+
});
346+
347+
it('produces bigint strings that are not equal to the strings generated when using BigInt.toString when the bigint values used are more than 64 bits wide', function () {
348+
const number = { a: 0x1234_5678_1234_5678_9999n };
349+
const serialized = EJSON.stringify(number, { relaxed: false });
350+
const parsed = JSON.parse(serialized);
351+
expect(parsed.a.$numberLong).to.not.equal(0x1234_5678_1234_5678_9999n.toString());
352+
});
353+
});
266354
});

test/node/extended_json.test.ts

Lines changed: 0 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import * as BSON from '../register-bson';
22
const EJSON = BSON.EJSON;
33
import * as vm from 'node:vm';
44
import { expect } from 'chai';
5-
import { BSONDataView } from '../../src/utils/byte_utils';
65
import { BSONError } from '../../src';
76

87
// BSON types
@@ -182,92 +181,6 @@ describe('Extended JSON', function () {
182181
expect(EJSON.parse(serialized)).to.deep.equal(numbers);
183182
});
184183

185-
it('truncates bigint values when they are outside the range [BSON_INT64_MIN, BSON_INT64_MAX] in canonical mode', function () {
186-
const numbers = { a: 2n ** 64n + 1n, b: -(2n ** 64n) - 1n };
187-
const serialized = EJSON.stringify(numbers, { relaxed: false });
188-
expect(serialized).to.equal('{"a":{"$numberLong":"1"},"b":{"$numberLong":"-1"}}');
189-
});
190-
191-
it('truncates bigint values in the same way as BSON.serialize in canonical mode', function () {
192-
const number = { a: 0x1234_5678_1234_5678_9999n };
193-
const stringified = EJSON.stringify(number, { relaxed: false });
194-
const serialized = BSON.serialize(number);
195-
196-
const VALUE_OFFSET = 7;
197-
const dataView = BSONDataView.fromUint8Array(serialized);
198-
const serializedValue = dataView.getBigInt64(VALUE_OFFSET, true);
199-
const parsed = JSON.parse(stringified);
200-
201-
expect(parsed).to.have.property('a');
202-
expect(parsed['a']).to.have.property('$numberLong');
203-
expect(parsed.a.$numberLong).to.equal(0x5678_1234_5678_9999n.toString());
204-
205-
expect(parsed.a.$numberLong).to.equal(serializedValue.toString());
206-
});
207-
208-
it('truncates bigint values in the same way as BSON.serialize in relaxed mode', function () {
209-
const number = { a: 0x1234_0000_1234_5678_9999n }; // Ensure that the truncated number can be exactly represented as a JS number
210-
const stringified = EJSON.stringify(number, { relaxed: true });
211-
const serializedDoc = BSON.serialize(number);
212-
213-
const VALUE_OFFSET = 7;
214-
const dataView = BSONDataView.fromUint8Array(serializedDoc);
215-
const parsed = JSON.parse(stringified);
216-
217-
expect(parsed).to.have.property('a');
218-
expect(parsed.a).to.equal(0x0000_1234_5678_9999);
219-
220-
expect(parsed.a).to.equal(Number(dataView.getBigInt64(VALUE_OFFSET, true)));
221-
});
222-
223-
it('serializes bigint values to numberLong in canonical mode', function () {
224-
const number = { a: 2n };
225-
const serialized = EJSON.stringify(number, { relaxed: false });
226-
expect(serialized).to.equal('{"a":{"$numberLong":"2"}}');
227-
});
228-
229-
it('serializes bigint values to Number in relaxed mode', function () {
230-
const number = { a: 10000n };
231-
const serialized = EJSON.stringify(number, { relaxed: true });
232-
expect(serialized).to.equal('{"a":10000}');
233-
});
234-
235-
it('loses precision when serializing bigint values outside of range [Number.MIN_SAFE_INTEGER, Number.MAX_SAFE_INTEGER] in relaxed mode', function () {
236-
const numbers = { a: -(2n ** 53n) - 1n, b: 2n ** 53n + 2n };
237-
const serialized = EJSON.stringify(numbers, { relaxed: true });
238-
expect(serialized).to.equal('{"a":-9007199254740992,"b":9007199254740994}');
239-
});
240-
241-
it('produces bigint strings that pass loose equality checks with native bigint values that are are 64 bits wide or less', function () {
242-
const number = { a: 12345n };
243-
const serialized = EJSON.stringify(number, { relaxed: false });
244-
const parsed = JSON.parse(serialized);
245-
// eslint-disable-next-line eqeqeq
246-
expect(parsed.a.$numberLong == 12345n).true;
247-
});
248-
249-
it('produces bigint strings that are equal to the strings generated when using BigInt.toString when the bigint values used are 64 bits wide or less', function () {
250-
const number = { a: 12345n };
251-
const serialized = EJSON.stringify(number, { relaxed: false });
252-
const parsed = JSON.parse(serialized);
253-
expect(parsed.a.$numberLong).to.equal(12345n.toString());
254-
});
255-
256-
it('produces bigint strings that fail loose equality checks with native bigint values that are more than 64 bits wide', function () {
257-
const number = { a: 0x1234_5678_1234_5678_9999n };
258-
const serialized = EJSON.stringify(number, { relaxed: false });
259-
const parsed = JSON.parse(serialized);
260-
// eslint-disable-next-line eqeqeq
261-
expect(parsed.a.$numberLong == 0x1234_5678_1234_5678_9999n).false;
262-
});
263-
264-
it('produces bigint strings that are not equal to the strings generated when using BigInt.toString when the bigint values used are more than 64 bits wide', function () {
265-
const number = { a: 0x1234_5678_1234_5678_9999n };
266-
const serialized = EJSON.stringify(number, { relaxed: false });
267-
const parsed = JSON.parse(serialized);
268-
expect(parsed.a.$numberLong).to.not.equal(0x1234_5678_1234_5678_9999n.toString());
269-
});
270-
271184
it('should correctly parse null values', function () {
272185
expect(EJSON.parse('null')).to.be.null;
273186
expect(EJSON.parse('[null]')[0]).to.be.null;

0 commit comments

Comments
 (0)