@@ -6,6 +6,7 @@ const vm = require('vm');
6
6
7
7
// BSON types
8
8
const Binary = BSON . Binary ;
9
+ const UUID = BSON . UUID ;
9
10
const Code = BSON . Code ;
10
11
const DBRef = BSON . DBRef ;
11
12
const Decimal128 = BSON . Decimal128 ;
@@ -739,4 +740,38 @@ Converting circular structure to EJSON:
739
740
} ) ;
740
741
} ) ;
741
742
} ) ;
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
+ } ) ;
742
777
} ) ;
0 commit comments