File tree Expand file tree Collapse file tree 2 files changed +26
-2
lines changed
src/cmap/wire_protocol/on_demand
test/unit/cmap/wire_protocol/on_demand Expand file tree Collapse file tree 2 files changed +26
-2
lines changed Original file line number Diff line number Diff line change @@ -261,7 +261,11 @@ export class OnDemandDocument {
261
261
return this . bson [ offset ] ? 1 : 0 ;
262
262
}
263
263
264
- return null ;
264
+ if ( required === true ) {
265
+ throw new BSONError ( `BSON element "${ name } " does not have numeric type: ${ type } ` ) ;
266
+ } else {
267
+ return null ;
268
+ }
265
269
}
266
270
267
271
/**
Original file line number Diff line number Diff line change @@ -185,7 +185,8 @@ describe('class OnDemandDocument', () => {
185
185
int : 1 ,
186
186
long : 2n ,
187
187
double : 2.3 ,
188
- bool : false
188
+ bool : false ,
189
+ string : 'abc'
189
190
} ;
190
191
191
192
beforeEach ( async function ( ) {
@@ -202,6 +203,25 @@ describe('class OnDemandDocument', () => {
202
203
expect ( ( ) => document . getNumber ( 'blah!' , true ) ) . to . throw ( BSONError ) ;
203
204
} ) ;
204
205
206
+ it ( 'throws if required is set to true and element is not numeric' , ( ) => {
207
+ // just making sure this test does not fail for the non-exist reason
208
+ expect ( document . hasElement ( 'string' ) ) . to . be . true ;
209
+ expect ( ( ) => document . getNumber ( 'string' , true ) ) . to . throw ( BSONError ) ;
210
+ } ) ;
211
+
212
+ it ( 'returns null if required is set to false and element does not exist' , ( ) => {
213
+ expect ( document . getNumber ( 'blah!' , false ) ) . to . be . null ;
214
+ expect ( document . getNumber ( 'blah!' ) ) . to . be . null ;
215
+ } ) ;
216
+
217
+ it ( 'returns null if required is set to false and element is not numeric' , ( ) => {
218
+ // just making sure this test does not fail for the non-exist reason
219
+ expect ( document . hasElement ( 'string' ) ) . to . be . true ;
220
+
221
+ expect ( document . getNumber ( 'string' , false ) ) . to . be . null ;
222
+ expect ( document . getNumber ( 'string' ) ) . to . be . null ;
223
+ } ) ;
224
+
205
225
it ( 'supports parsing int' , ( ) => {
206
226
expect ( document . getNumber ( 'int' ) ) . to . equal ( 1 ) ;
207
227
} ) ;
You can’t perform that action at this time.
0 commit comments