1
+
2
+
1
3
'use strict' ;
2
4
3
5
const { Buffer } = require ( 'buffer' ) ;
@@ -6,6 +8,8 @@ const { inspect } = require('util');
6
8
const { validate : uuidStringValidate , version : uuidStringVersion } = require ( 'uuid' ) ;
7
9
const BSON = require ( '../register-bson' ) ;
8
10
const BSONTypeError = BSON . BSONTypeError ;
11
+ const BSON_DATA_BINARY = BSON . BSON_DATA_BINARY ;
12
+ const BSON_BINARY_SUBTYPE_UUID_NEW = BSON . BSON_BINARY_SUBTYPE_UUID_NEW ;
9
13
10
14
// Test values
11
15
const UPPERCASE_DASH_SEPARATED_UUID_STRING = 'AAAAAAAA-AAAA-4AAA-AAAA-AAAAAAAAAAAA' ;
@@ -162,4 +166,31 @@ describe('UUID', () => {
162
166
const uuid = new UUID ( UPPERCASE_DASH_SEPARATED_UUID_STRING ) ;
163
167
expect ( inspect ( uuid ) ) . to . equal ( `new UUID("${ LOWERCASE_DASH_SEPARATED_UUID_STRING } ")` ) ;
164
168
} ) ;
169
+
170
+ describe ( 'serialize' , ( ) => {
171
+ it ( 'should have a valid UUID _bsontype with Object input without error' , ( ) => {
172
+ const output = BSON . serialize ( { uuid : new BSON . UUID ( ) } ) ;
173
+ expect ( output [ 4 ] ) . to . equal ( BSON_DATA_BINARY ) ;
174
+ expect ( output [ 14 ] ) . to . equal ( BSON_BINARY_SUBTYPE_UUID_NEW ) ;
175
+ } ) ;
176
+
177
+ it ( 'should have a valid UUID _bsontype with Map input without error' , ( ) => {
178
+ const output = BSON . serialize ( new Map ( [ [ 'uuid' , new BSON . UUID ( ) ] ] ) ) ;
179
+ expect ( output [ 4 ] ) . to . equal ( BSON_DATA_BINARY ) ;
180
+ expect ( output [ 14 ] ) . to . equal ( BSON_BINARY_SUBTYPE_UUID_NEW ) ;
181
+ } ) ;
182
+
183
+ it ( 'should have as a valid UUID _bsontype with Array input without error' , ( ) => {
184
+ const output = BSON . serialize ( { a : [ new BSON . UUID ( ) ] } ) ;
185
+ expect ( output [ 11 ] ) . to . equal ( BSON_DATA_BINARY ) ;
186
+ expect ( output [ 18 ] ) . to . equal ( BSON_BINARY_SUBTYPE_UUID_NEW ) ;
187
+ } ) ;
188
+
189
+ it ( 'should serialize BSON.UUID() input the same as BSON.UUID().toBinary()' , ( ) => {
190
+ const exampleUUID = new BSON . UUID ( ) ;
191
+ const toBinarySerialization = BSON . serialize ( { uuid : exampleUUID . toBinary ( ) } ) ;
192
+ const plainUUIDSerialization = BSON . serialize ( { uuid : exampleUUID } ) ;
193
+ expect ( plainUUIDSerialization ) . to . deep . equal ( toBinarySerialization ) ;
194
+ } ) ;
195
+ } ) ;
165
196
} ) ;
0 commit comments