Skip to content

Commit cf143ac

Browse files
added EJSON tests
1 parent 72e5236 commit cf143ac

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

test/node/extended_json_tests.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const vm = require('vm');
66

77
// BSON types
88
const Binary = BSON.Binary;
9+
const UUID = BSON.UUID;
910
const Code = BSON.Code;
1011
const DBRef = BSON.DBRef;
1112
const Decimal128 = BSON.Decimal128;
@@ -739,4 +740,38 @@ Converting circular structure to EJSON:
739740
});
740741
});
741742
});
743+
744+
describe('UUID stringify', () => {
745+
const uuid = new UUID();
746+
const stringifiedPlainUUID = EJSON.stringify({ u: uuid });
747+
it('should return same values for UUID.toBinary() and UUID', () => {
748+
const stringifiedToBinary = EJSON.stringify({ u: uuid.toBinary() });
749+
expect(stringifiedToBinary).to.deep.equal(stringifiedPlainUUID);
750+
});
751+
it('should serialize to correct subType', () => {
752+
const stringifiedUUIDtoObject = JSON.parse(stringifiedPlainUUID);
753+
const stringifiedBinaryNewUUIDSubType = '04';
754+
expect(stringifiedUUIDtoObject.u.$binary.subType).to.equal(stringifiedBinaryNewUUIDSubType);
755+
});
756+
});
757+
758+
describe('UUID parse', () => {
759+
const uuid = new UUID();
760+
const stringifiedPlainUUID = EJSON.stringify({ u: uuid });
761+
it('should return same values for UUID.toBinary() and UUID', () => {
762+
const stringifiedToBinary = EJSON.stringify({ u: uuid.toBinary() });
763+
const parsedToBinary = EJSON.parse(stringifiedToBinary);
764+
const parsedPlainUUID = EJSON.parse(stringifiedPlainUUID);
765+
expect(parsedToBinary).to.deep.equal(parsedPlainUUID);
766+
});
767+
it('should parse both input formats the same way', () => {
768+
const parsedUndashedInput = EJSON.parse(
769+
`{"u":{"$binary":{"base64":"vDzrMPEAQOGkA8wGUNSOxw==","subType":"04"}}}`
770+
);
771+
const parsedDashedInput = EJSON.parse(
772+
`{"u":{"$uuid":"bc3ceb30-f100-40e1-a403-cc0650d48ec7"}}`
773+
);
774+
expect(parsedUndashedInput).to.deep.equal(parsedDashedInput);
775+
});
776+
});
742777
});

0 commit comments

Comments
 (0)