@@ -193,4 +193,65 @@ describe('UUID', () => {
193
193
expect ( plainUUIDSerialization ) . to . deep . equal ( toBinarySerialization ) ;
194
194
} ) ;
195
195
} ) ;
196
+
197
+ describe ( 'deserialize' , ( ) => {
198
+ const originalUUID = new BSON . UUID ( ) ;
199
+ const binaryUUID = originalUUID . toBinary ( ) ;
200
+ const serializedUUID = BSON . serialize ( { uuid : originalUUID . toBinary ( ) } ) ;
201
+
202
+ it ( 'should promoteUUIDs when flag is true' , ( ) => {
203
+ const { uuid : promotedUUID } = BSON . deserialize ( serializedUUID , { promoteUUIDs : true } ) ;
204
+ expect ( promotedUUID . _bsontype ) . to . equal ( 'UUID' ) ;
205
+ expect ( promotedUUID ) . to . deep . equal ( originalUUID ) ;
206
+ } ) ;
207
+
208
+ it ( 'should not promoteUUIDs when flag is false' , ( ) => {
209
+ const { uuid : unpromotedUUID } = BSON . deserialize ( serializedUUID , { promoteUUIDs : false } ) ;
210
+ expect ( unpromotedUUID . _bsontype ) . to . equal ( 'Binary' ) ;
211
+ expect ( unpromotedUUID ) . to . deep . equal ( binaryUUID ) ;
212
+ } ) ;
213
+
214
+ it ( 'should not promoteUUIDs when flag is omitted' , ( ) => {
215
+ const { uuid : omittedFlagUUID } = BSON . deserialize ( serializedUUID ) ;
216
+ expect ( omittedFlagUUID . _bsontype ) . to . equal ( 'Binary' ) ;
217
+ expect ( omittedFlagUUID ) . to . deep . equal ( binaryUUID ) ;
218
+ } ) ;
219
+
220
+ it ( 'should throw BSONTypeError if _bsontype is not UUID and promoteUUIDs is true' , ( ) => {
221
+ const binaryVar = new Binary ( Buffer . from ( 'abc' ) ) ;
222
+ const serializedBinary = BSON . serialize ( binaryVar ) ;
223
+ expect ( ( ) => {
224
+ BSON . deserialize ( serializedBinary , { promoteUUIDs : true } ) ;
225
+ } ) . to . throw ( BSONTypeError ) ;
226
+ } ) ;
227
+
228
+ describe ( 'promoteBuffers' , ( ) => {
229
+ const promoteUUIDValues = [ true , false , undefined ] ;
230
+ const promoteBufferValues = [ true , false , undefined ] ;
231
+
232
+ const testCases = promoteUUIDValues . flatMap ( promoteUUIDs =>
233
+ promoteBufferValues . flatMap ( promoteBuffers => ( {
234
+ options : { promoteUUIDs, promoteBuffers } ,
235
+ // promoteBuffers: true returns a Buffer so _bsontype does not exist
236
+ outcome : promoteUUIDs ? 'UUID' : promoteBuffers ? undefined : 'Binary'
237
+ } ) )
238
+ ) ;
239
+
240
+ for ( const { options, outcome } of testCases ) {
241
+ it ( `should deserialize to ${ outcome } type when promoteUUIDs is ${ options . promoteUUIDs } and promoteBuffers is ${ options . promoteBuffers } ` , ( ) => {
242
+ const { uuid } = BSON . deserialize ( serializedUUID , options ) ;
243
+ expect ( uuid . _bsontype ) . to . equal ( outcome ) ;
244
+ if ( uuid . _bsontype === 'UUID' ) {
245
+ expect ( uuid . id ) . to . deep . equal ( originalUUID . id ) ;
246
+ } else if ( uuid . _bsontype === 'Binary' ) {
247
+ expect ( uuid . buffer ) . to . deep . equal ( originalUUID . id ) ;
248
+ } else if ( uuid . _bsontype === undefined ) {
249
+ expect ( uuid ) . to . deep . equal ( originalUUID . id ) ;
250
+ } else {
251
+ expect . fail ( 'Unexpected _bsontype' ) ;
252
+ }
253
+ } ) ;
254
+ }
255
+ } ) ;
256
+ } ) ;
196
257
} ) ;
0 commit comments