Skip to content
This repository has been archived by the owner on Sep 23, 2024. It is now read-only.

Commit

Permalink
test(NODE-4536): add tests for UUID (#87)
Browse files Browse the repository at this point in the history
  • Loading branch information
aditi-khare-mongoDB authored Aug 29, 2022
1 parent 9059b17 commit 8aaa318
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 11 deletions.
19 changes: 11 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
},
"dependencies": {
"bindings": "^1.5.0",
"bson": "^4.5.2",
"bson": "^4.7.0",
"nan": "^2.14.2",
"prebuild-install": "^6.1.2"
},
Expand Down
36 changes: 36 additions & 0 deletions test/node/bson_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2323,4 +2323,40 @@ describe('BSON', function() {
expect(() => bson.serialize(doc, { checkKeys: true })).to.not.throw()
});
});

describe('UUID serialize', function () {
const BSON = createBSON();
const UUID = require('bson').UUID;
const uuid = new UUID();
const BSON_DATA_BINARY = 5;
const BSON_BINARY_SUBTYPE_UUID_NEW = 4;

it('should serialize BSON.UUID() input the same as BSON.UUID().toBinary()', () => {
const toBinarySerialization = BSON.serialize({ uuid: uuid.toBinary() });
const plainUUIDSerialization = BSON.serialize({ uuid: uuid });
expect(plainUUIDSerialization).to.deep.equal(toBinarySerialization);
expect(BSON.deserialize(plainUUIDSerialization).uuid).to.deep.equal(uuid.toBinary());
});

it('should have a valid UUID _bsontype with Object input without error', () => {
const output = BSON.serialize({ uuid: uuid });
expect(output[4]).to.equal(BSON_DATA_BINARY);
expect(output[14]).to.equal(BSON_BINARY_SUBTYPE_UUID_NEW);
expect(BSON.deserialize(output).uuid).to.deep.equal(uuid.toBinary());
});

it('should have a valid UUID _bsontype with Map input without error', () => {
const output = BSON.serialize(new Map([['uuid', uuid]]));
expect(output[4]).to.equal(BSON_DATA_BINARY);
expect(output[14]).to.equal(BSON_BINARY_SUBTYPE_UUID_NEW);
expect(BSON.deserialize(output).uuid).to.deep.equal(uuid.toBinary());
});

it('should have as a valid UUID _bsontype with Array input without error', () => {
const output = BSON.serialize({ a: [uuid] });
expect(output[11]).to.equal(BSON_DATA_BINARY);
expect(output[18]).to.equal(BSON_BINARY_SUBTYPE_UUID_NEW);
expect(BSON.deserialize(output).a).to.deep.equal([uuid.toBinary()]);
});
});
});
2 changes: 1 addition & 1 deletion test/node/null_byte_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@ describe('null byte handling in serializing', () => {
});

it('should throw when null byte in Flags/options for a regular expression', () => {
expect(() => BSON.serialize({ a: new BSON.BSONRegExp('a', 'i\x00m') })).to.throw(/regular expression option/);
expect(() => BSON.serialize({ a: new BSON.BSONRegExp('a', 'i\x00m') })).to.throw(/null bytes/);
});
})
2 changes: 1 addition & 1 deletion test/node/test_full_bson.js
Original file line number Diff line number Diff line change
Expand Up @@ -737,9 +737,9 @@ describe('Full BSON', function() {

it('Should correctly fail to serialize BSONRegExp with null bytes', function(done) {
var doc = {};
doc.test = new BSONRegExp('a\0b');

try {
doc.test = new BSONRegExp('a\0b');
bson.serialize(doc, {
checkKeys: true
});
Expand Down

0 comments on commit 8aaa318

Please sign in to comment.