@@ -3,9 +3,10 @@ import { expect } from 'chai';
33
44import { LEGACY_HELLO_COMMAND } from '../../src/constants' ;
55import { MongoRuntimeError } from '../../src/error' ;
6- import { Promise as PromiseProvider } from '../../src/index' ;
6+ import { ObjectId , Promise as PromiseProvider } from '../../src/index' ;
77import {
88 BufferPool ,
9+ compareObjectId ,
910 eachAsync ,
1011 HostAddress ,
1112 isHello ,
@@ -481,4 +482,48 @@ describe('driver utils', function () {
481482 } ) ;
482483 } ) ;
483484 } ) ;
485+
486+ describe ( 'compareObjectId()' , ( ) => {
487+ const table = [
488+ { oid1 : null , oid2 : null , result : 0 } ,
489+ { oid1 : undefined , oid2 : null , result : 0 } ,
490+ { oid1 : null , oid2 : undefined , result : 0 } ,
491+ { oid1 : undefined , oid2 : undefined , result : 0 } ,
492+ { oid1 : new ObjectId ( '00' . repeat ( 12 ) ) , oid2 : undefined , result : 1 } ,
493+ { oid1 : new ObjectId ( '00' . repeat ( 12 ) ) , oid2 : null , result : 1 } ,
494+ { oid1 : undefined , oid2 : new ObjectId ( '00' . repeat ( 12 ) ) , result : - 1 } ,
495+ { oid1 : null , oid2 : new ObjectId ( '00' . repeat ( 12 ) ) , result : - 1 } ,
496+ { oid1 : new ObjectId ( '00' . repeat ( 12 ) ) , oid2 : new ObjectId ( '00' . repeat ( 12 ) ) , result : 0 } ,
497+ {
498+ oid1 : new ObjectId ( '00' . repeat ( 11 ) + '01' ) ,
499+ oid2 : new ObjectId ( '00' . repeat ( 12 ) ) ,
500+ result : 1
501+ } ,
502+ {
503+ oid1 : new ObjectId ( '00' . repeat ( 12 ) ) ,
504+ oid2 : new ObjectId ( '00' . repeat ( 11 ) + '01' ) ,
505+ result : - 1
506+ } ,
507+ {
508+ oid1 : 2 ,
509+ oid2 : 1 ,
510+ result : 'throws'
511+ }
512+ ] ;
513+
514+ for ( const { oid1, oid2, result } of table ) {
515+ if ( result === 'throws' ) {
516+ it ( 'passing non-objectId values throw' , ( ) =>
517+ // @ts -expect-error: Passing bad values to ensure thrown error
518+ expect ( ( ) => compareObjectId ( oid1 , oid2 ) ) . to . throw ( ) ) ;
519+ continue ;
520+ }
521+
522+ const title = `comparing ${ oid1 } to ${ oid2 } returns ${
523+ result === 0 ? 'equal' : result === - 1 ? 'less than' : 'greater than'
524+ } `;
525+ // @ts -expect-error: not narrowed based on numeric result, but these values are correct
526+ it ( title , ( ) => expect ( compareObjectId ( oid1 , oid2 ) ) . to . equal ( result ) ) ;
527+ }
528+ } ) ;
484529} ) ;
0 commit comments