1
1
'use strict' ;
2
- const { assert : test } = require ( '../shared' ) ;
2
+ const { assert : test , filterForCommands } = require ( '../shared' ) ;
3
3
const { expect } = require ( 'chai' ) ;
4
4
const sinon = require ( 'sinon' ) ;
5
5
const { setTimeout } = require ( 'timers' ) ;
6
- const { Code, ObjectId, Long, Binary, ReturnDocument, CursorResponse } = require ( '../../mongodb' ) ;
6
+ const {
7
+ Code,
8
+ ObjectId,
9
+ Long,
10
+ Binary,
11
+ ReturnDocument,
12
+ CursorResponse,
13
+ MongoServerError
14
+ } = require ( '../../mongodb' ) ;
7
15
8
16
describe ( 'Find' , function ( ) {
9
17
/** @type (import('../../mongodb').MongoClient */
@@ -499,7 +507,13 @@ describe('Find', function () {
499
507
500
508
it ( 'shouldCorrectlyPerformFindsWithHintTurnedOn' , async function ( ) {
501
509
const configuration = this . configuration ;
502
- client = configuration . newClient ( configuration . writeConcernMax ( ) , { maxPoolSize : 1 } ) ;
510
+ client = configuration . newClient ( configuration . writeConcernMax ( ) , {
511
+ monitorCommands : true
512
+ } ) ;
513
+
514
+ const finds = [ ] ;
515
+ client . on ( 'commandStarted' , filterForCommands ( 'find' , finds ) ) ;
516
+
503
517
await client . connect ( ) ;
504
518
505
519
const db = client . db ( configuration . db ) ;
@@ -515,30 +529,16 @@ describe('Find', function () {
515
529
. find ( { a : 1 } , { hint : 'a' } )
516
530
. toArray ( )
517
531
. catch ( e => e )
518
- ) . to . be . instanceOf ( Error ) ;
532
+ ) . to . be . instanceOf ( MongoServerError ) ;
533
+ expect ( finds [ 0 ] . command . hint ) . to . equal ( 'a' ) ;
519
534
520
535
// Test with hint as array
521
536
expect ( await collection . find ( { a : 1 } , { hint : [ 'a' ] } ) . toArray ( ) ) . to . have . lengthOf ( 1 ) ;
537
+ expect ( finds [ 1 ] . command . hint ) . to . deep . equal ( { a : 1 } ) ;
522
538
523
539
// Test with hint as object
524
540
expect ( await collection . find ( { a : 1 } , { hint : { a : 1 } } ) . toArray ( ) ) . to . have . lengthOf ( 1 ) ;
525
-
526
- // Modify hints
527
- collection . hint = 'a_1' ;
528
- expect ( collection . hint ) . to . equal ( 'a_1' ) ;
529
- expect ( await collection . find ( { a : 1 } ) . toArray ( ) ) . to . have . lengthOf ( 1 ) ;
530
-
531
- collection . hint = [ 'a' ] ;
532
- expect ( collection . hint [ 'a' ] ) . to . equal ( 1 ) ;
533
- expect ( await collection . find ( { a : 1 } ) . toArray ( ) ) . to . have . lengthOf ( 1 ) ;
534
-
535
- collection . hint = { a : 1 } ;
536
- expect ( collection . hint [ 'a' ] ) . to . equal ( 1 ) ;
537
- expect ( await collection . find ( { a : 1 } ) . toArray ( ) ) . to . have . lengthOf ( 1 ) ;
538
-
539
- collection . hint = null ;
540
- expect ( collection . hint ) . to . be . undefined ;
541
- expect ( await collection . find ( { a : 1 } ) . toArray ( ) ) . to . have . lengthOf ( 1 ) ;
541
+ expect ( finds [ 2 ] . command . hint ) . to . deep . equal ( { a : 1 } ) ;
542
542
} ) ;
543
543
544
544
it ( 'shouldCorrectlyPerformFindByObjectId' , {
0 commit comments