Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
SeanReece committed Aug 15, 2024
1 parent e3bf7de commit 5d01f60
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 9 deletions.
12 changes: 8 additions & 4 deletions test/node/bson_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@ const MaxKey = BSON.MaxKey;
const BSONError = BSON.BSONError;
const { BinaryParser } = require('./tools/binary_parser');
const vm = require('vm');
const { assertBuffersEqual, isBufferOrUint8Array } = require('./tools/utils');
const {
assertBuffersEqual,
isBufferOrUint8Array,
assertDeepEqualsWithObjectId
} = require('./tools/utils');
const { inspect } = require('util');

/**
Expand Down Expand Up @@ -707,7 +711,7 @@ describe('BSON', function () {
expect(serialized_data).to.deep.equal(serialized_data2);

var doc2 = b.deserialize(serialized_data);
expect(doc).to.deep.equal(doc2);
assertDeepEqualsWithObjectId(doc, doc2);
expect(doc2.dbref.oid.toHexString()).to.deep.equal(oid.toHexString());
done();
});
Expand Down Expand Up @@ -1001,7 +1005,7 @@ describe('BSON', function () {

var deserialized_data = BSON.deserialize(serialized_data);
expect(doc.b).to.deep.equal(deserialized_data.b);
expect(doc).to.deep.equal(deserialized_data);
assertDeepEqualsWithObjectId(doc, deserialized_data);
done();
});

Expand Down Expand Up @@ -1213,7 +1217,7 @@ describe('BSON', function () {

var doc2 = BSON.deserialize(serialized_data);

expect(doc).to.deep.equal(doc2);
assertDeepEqualsWithObjectId(doc, doc2);
done();
});

Expand Down
9 changes: 8 additions & 1 deletion test/node/bson_type_classes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
UUID,
BSONValue
} from '../register-bson';
import { assertDeepEqualsWithObjectId } from './tools/utils';
import * as vm from 'node:vm';

const BSONTypeClasses = [
Expand Down Expand Up @@ -128,7 +129,13 @@ describe('BSON Type classes common interfaces', () => {
ctx.ObjectId = ObjectId;
}
vm.runInNewContext(`module.exports.result = ${bsonValue.inspect()}`, ctx);
expect(ctx.module.exports.result).to.deep.equal(bsonValue);

if (ctx.ObjectId) {
// Since ObjectId uses a pool we expect offset to be different
assertDeepEqualsWithObjectId(ctx.module.exports.result, bsonValue);
} else {
expect(ctx.module.exports.result).to.deep.equal(bsonValue);
}
});
}
});
Expand Down
6 changes: 4 additions & 2 deletions test/node/extended_json.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import * as vm from 'node:vm';
import { expect } from 'chai';
import { BSONVersionError, BSONRuntimeError } from '../../src';
import { BSONError } from '../register-bson';
import { assertDeepEqualsWithObjectId } from './tools/utils';

// BSON types
const Binary = BSON.Binary;
Expand Down Expand Up @@ -144,9 +145,10 @@ describe('Extended JSON', function () {
const input = '{"result":[{"_id":{"$oid":"591801a468f9e7024b623939"},"emptyField":null}]}';
const parsed = EJSON.parse(input);

expect(parsed).to.deep.equal({
const expected = {
result: [{ _id: new ObjectId('591801a468f9e7024b623939'), emptyField: null }]
});
};
assertDeepEqualsWithObjectId(parsed, expected);
});

it('should correctly throw when passed a non-string to parse', function () {
Expand Down
2 changes: 0 additions & 2 deletions test/node/object_id.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import { expect } from 'chai';
import { bufferFromHexArray } from './tools/utils';
import { isBufferOrUint8Array } from './tools/utils';

ObjectId.poolSize = 100;

declare module '../register-bson' {
interface ObjectId {
pool: Uint8Array;
Expand Down
22 changes: 22 additions & 0 deletions test/node/tools/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -218,3 +218,25 @@ module.exports.sorted = (iterable, how) => {
items.sort(how);
return items;
};

/** Deeply converts all ObjectIds into their hex value representation */
const deepOidValue = obj => {
if (obj?._bsontype === 'ObjectId') {
return obj.toHexString();
}
if (Array.isArray(obj)) {
return obj.map(item => deepOidValue(item));
} else if (obj !== null && typeof obj === 'object' && !Buffer.isBuffer(obj)) {
return Object.entries(obj).reduce((acc, [key, value]) => {
acc[key] = deepOidValue(value);
return acc;
}, {});
}
return obj;
};

module.exports.assertDeepEqualsWithObjectId = (actual, expected) => {
const a = deepOidValue(actual);
const b = deepOidValue(expected);
expect(a).to.deep.equal(b);
};

0 comments on commit 5d01f60

Please sign in to comment.