1
1
/* eslint camelcase: 0, new-cap: 0 */
2
2
import { CommonErrors , MongoshInvalidInputError } from '@mongosh/errors' ;
3
3
import { bson } from '@mongosh/service-provider-core' ;
4
+ import { serialize as bsonSerialize , deserialize as bsonDeserialize } from 'bson' ;
4
5
import { expect } from 'chai' ;
5
6
import sinon from 'ts-sinon' ;
6
7
import { ALL_SERVER_VERSIONS } from './enums' ;
@@ -77,6 +78,10 @@ describe('Shell BSON', () => {
77
78
const s = new ( shellBson . MaxKey as any ) ( ) ;
78
79
expect ( s . _bsontype ) . to . equal ( 'MaxKey' ) ;
79
80
} ) ;
81
+ it ( 'using toBSON' , ( ) => {
82
+ const s = ( shellBson . MaxKey as any ) . toBSON ( ) ;
83
+ expect ( s . _bsontype ) . to . equal ( 'MaxKey' ) ;
84
+ } ) ;
80
85
it ( 'has help and other metadata' , async ( ) => {
81
86
const s = shellBson . MaxKey ( ) ;
82
87
expect ( ( await toShellResult ( s . help ) ) . type ) . to . equal ( 'Help' ) ;
@@ -93,13 +98,31 @@ describe('Shell BSON', () => {
93
98
const s = new ( shellBson . MinKey as any ) ( ) ;
94
99
expect ( s . _bsontype ) . to . equal ( 'MinKey' ) ;
95
100
} ) ;
101
+ it ( 'using toBSON' , ( ) => {
102
+ const s = ( shellBson . MinKey as any ) . toBSON ( ) ;
103
+ expect ( s . _bsontype ) . to . equal ( 'MinKey' ) ;
104
+ } ) ;
96
105
it ( 'has help and other metadata' , async ( ) => {
97
106
const s = shellBson . MinKey ( ) ;
98
107
expect ( ( await toShellResult ( s . help ) ) . type ) . to . equal ( 'Help' ) ;
99
108
expect ( ( await toShellResult ( s . help ( ) ) ) . type ) . to . equal ( 'Help' ) ;
100
109
expect ( s . serverVersions ) . to . deep . equal ( ALL_SERVER_VERSIONS ) ;
101
110
} ) ;
102
111
} ) ;
112
+ describe ( 'MinKey & MaxKey constructor special handling' , ( ) => {
113
+ it ( 'round-trips through bson as expected' , ( ) => {
114
+ const { MinKey, MaxKey } = shellBson as any ;
115
+ const expected = { a : { $minKey : 1 } , b : { $maxKey : 1 } } ;
116
+ function roundtrip ( value : any ) : any {
117
+ return bson . EJSON . serialize ( bsonDeserialize ( bsonSerialize ( value ) ) ) ;
118
+ }
119
+
120
+ expect ( roundtrip ( { a : new MinKey ( ) , b : new MaxKey ( ) } ) ) . to . deep . equal ( expected ) ;
121
+ expect ( roundtrip ( { a : MinKey ( ) , b : MaxKey ( ) } ) ) . to . deep . equal ( expected ) ;
122
+ expect ( roundtrip ( { a : MinKey . toBSON ( ) , b : MaxKey . toBSON ( ) } ) ) . to . deep . equal ( expected ) ;
123
+ expect ( roundtrip ( { a : MinKey , b : MaxKey } ) ) . to . deep . equal ( expected ) ;
124
+ } ) ;
125
+ } ) ;
103
126
describe ( 'ObjectId' , ( ) => {
104
127
it ( 'without new' , ( ) => {
105
128
const s = shellBson . ObjectId ( '5ebbe8e2905bb493d6981b6b' ) ;
@@ -615,6 +638,7 @@ describe('Shell BSON', () => {
615
638
delete bsonProperties . length ;
616
639
delete shellProperties . index ; // ObjectId.index is a random number
617
640
delete bsonProperties . index ; // ObjectId.index is a random number
641
+ delete shellProperties . toBSON ; // toBSON is something we add for MaxKey/MinKey as a shell-specific extension
618
642
try {
619
643
expect ( shellProperties ) . to . deep . equal ( bsonProperties ) ;
620
644
} catch ( err ) {
0 commit comments