@@ -8,16 +8,15 @@ import {
8
8
type CommandFailedEvent ,
9
9
type CommandStartedEvent ,
10
10
type CommandSucceededEvent ,
11
- Connection ,
12
11
Db ,
13
- getTopology ,
14
12
MongoClient ,
15
13
MongoNotConnectedError ,
16
14
MongoServerSelectionError ,
17
- ReadPreference ,
18
- ServerDescription ,
19
- Topology
20
- } from '../../mongodb' ;
15
+ ReadPreference
16
+ } from '../../../src' ;
17
+ import { Connection } from '../../../src/cmap/connection' ;
18
+ import { ServerDescription } from '../../../src/sdam/server_description' ;
19
+ import { Topology } from '../../../src/sdam/topology' ;
21
20
import { clearFailPoint , configureFailPoint } from '../../tools/utils' ;
22
21
import { setupDatabase } from '../shared' ;
23
22
@@ -34,16 +33,17 @@ describe('class MongoClient', function () {
34
33
client = undefined ;
35
34
} ) ;
36
35
37
- it ( 'should correctly pass through extra db options' , {
38
- metadata : { requires : { topology : [ 'single' ] } } ,
39
- test : function ( done ) {
36
+ it (
37
+ 'should correctly pass through extra db options' ,
38
+ { requires : { topology : 'single' } } ,
39
+ async function ( ) {
40
40
const configuration = this . configuration ;
41
41
const client = configuration . newClient (
42
42
{ } ,
43
43
{
44
44
writeConcern : { w : 1 , wtimeoutMS : 1000 , fsync : true , j : true } ,
45
45
readPreference : 'nearest' ,
46
- readPreferenceTags : { loc : 'ny' } ,
46
+ readPreferenceTags : [ { loc : 'ny' } ] ,
47
47
forceServerObjectId : true ,
48
48
pkFactory : {
49
49
createPk ( ) {
@@ -54,74 +54,55 @@ describe('class MongoClient', function () {
54
54
}
55
55
) ;
56
56
57
- client . connect ( function ( err , client ) {
58
- expect ( err ) . to . be . undefined ;
57
+ await client . connect ( ) ;
59
58
60
- const db = client . db ( configuration . db ) ;
59
+ const db = client . db ( configuration . db ) ;
61
60
62
- expect ( db ) . to . have . property ( 'writeConcern' ) ;
63
- expect ( db . writeConcern ) . to . have . property ( 'w' , 1 ) ;
64
- expect ( db . writeConcern ) . to . have . property ( 'wtimeoutMS' , 1000 ) ;
65
- expect ( db . writeConcern ) . to . have . property ( 'journal' , true ) ;
61
+ expect ( db ) . to . have . property ( 'writeConcern' ) ;
62
+ expect ( db . writeConcern ) . to . have . property ( 'w' , 1 ) ;
63
+ expect ( db . writeConcern ) . to . have . property ( 'wtimeoutMS' , 1000 ) ;
64
+ expect ( db . writeConcern ) . to . have . property ( 'journal' , true ) ;
66
65
67
- expect ( db ) . to . have . property ( 's' ) ;
68
- expect ( db . s ) . to . have . property ( 'readPreference' ) ;
69
- expect ( db . s . readPreference ) . to . have . property ( 'mode' , 'nearest' ) ;
70
- expect ( db . s . readPreference )
71
- . to . have . property ( 'tags' )
72
- . that . deep . equals ( [ { loc : 'ny' } ] ) ;
66
+ expect ( db ) . to . have . property ( 's' ) ;
67
+ expect ( db . s ) . to . have . property ( 'readPreference' ) ;
68
+ expect ( db . s . readPreference ) . to . have . property ( 'mode' , 'nearest' ) ;
69
+ expect ( db . s . readPreference )
70
+ . to . have . property ( 'tags' )
71
+ . that . deep . equals ( [ { loc : 'ny' } ] ) ;
73
72
74
- expect ( db . s ) . to . have . nested . property ( 'options.forceServerObjectId' ) ;
75
- expect ( db . s . options ) . to . have . property ( 'forceServerObjectId' , true ) ;
76
- expect ( db . s ) . to . have . nested . property ( 'pkFactory.createPk' ) . that . is . a ( 'function' ) ;
77
- expect ( db . s . pkFactory . createPk ( ) ) . to . equal ( 1 ) ;
78
- expect ( db ) . to . have . nested . property ( 'bsonOptions.serializeFunctions' ) ;
73
+ expect ( db . s ) . to . have . nested . property ( 'options.forceServerObjectId' ) ;
74
+ expect ( db . s . options ) . to . have . property ( 'forceServerObjectId' , true ) ;
75
+ expect ( db . s ) . to . have . nested . property ( 'pkFactory.createPk' ) . that . is . a ( 'function' ) ;
76
+ expect ( db . s . pkFactory . createPk ( ) ) . to . equal ( 1 ) ;
77
+ expect ( db ) . to . have . nested . property ( 'bsonOptions.serializeFunctions' ) ;
79
78
80
- client . close ( done ) ;
81
- } ) ;
79
+ await client . close ( ) ;
82
80
}
83
- } ) ;
81
+ ) ;
84
82
85
- it ( 'Should fail due to wrong uri user:password@localhost' , {
86
- metadata : {
87
- requires : { topology : [ 'single' , 'replicaset' , 'sharded' ] }
88
- } ,
89
- test ( ) {
90
- expect ( ( ) => this . configuration . newClient ( 'user:password@localhost:27017/test' ) ) . to . throw (
91
- 'Invalid scheme, expected connection string to start with "mongodb://" or "mongodb+srv://"'
92
- ) ;
93
- }
83
+ it ( 'Should fail due to wrong uri user:password@localhost' , function ( ) {
84
+ expect ( ( ) => this . configuration . newClient ( 'user:password@localhost:27017/test' ) ) . to . throw (
85
+ 'Invalid scheme, expected connection string to start with "mongodb://" or "mongodb+srv://"'
86
+ ) ;
94
87
} ) ;
95
88
96
- it ( 'correctly error out when no socket available on MongoClient `connect`' , {
97
- metadata : {
98
- requires : { topology : [ 'single' , 'replicaset' , 'sharded' ] }
99
- } ,
100
-
101
- test : function ( done ) {
102
- const configuration = this . configuration ;
103
- const client = configuration . newClient ( 'mongodb://localhost:27088/test' , {
104
- serverSelectionTimeoutMS : 10
105
- } ) ;
106
-
107
- client . connect ( function ( err ) {
108
- expect ( err ) . to . exist ;
89
+ it ( 'correctly error out when no socket available on MongoClient `connect`' , async function ( ) {
90
+ const configuration = this . configuration ;
91
+ const client = configuration . newClient ( 'mongodb://localhost:27088/test' , {
92
+ serverSelectionTimeoutMS : 10
93
+ } ) ;
109
94
110
- done ( ) ;
111
- } ) ;
112
- }
95
+ const error = await client . connect ( ) . catch ( e => e ) ;
96
+ expect ( error ) . to . be . instanceOf ( MongoServerSelectionError ) ;
113
97
} ) ;
114
98
115
99
it ( 'should correctly connect to mongodb using domain socket' , {
116
100
metadata : { requires : { topology : [ 'single' ] , os : '!win32' } } ,
117
-
118
- test : function ( done ) {
101
+ test : async function ( ) {
119
102
const configuration = this . configuration ;
120
103
const client = configuration . newClient ( 'mongodb://%2Ftmp%2Fmongodb-27017.sock/test' ) ;
121
- client . connect ( function ( err ) {
122
- expect ( err ) . to . not . exist ;
123
- client . close ( done ) ;
124
- } ) ;
104
+ await client . connect ( ) ;
105
+ await client . close ( ) ;
125
106
}
126
107
} ) ;
127
108
@@ -236,6 +217,7 @@ describe('class MongoClient', function () {
236
217
beforeEach ( async function ( ) {
237
218
spy = sinon . spy ( net , 'createConnection' ) ;
238
219
const uri = this . configuration . url ( ) ;
220
+ // @ts -expect-error Intentional test of invalid options
239
221
client = new MongoClient ( uri , options ) ;
240
222
} ) ;
241
223
@@ -359,92 +341,85 @@ describe('class MongoClient', function () {
359
341
expect ( name ) . to . equal ( 'hello world' ) ;
360
342
} ) ;
361
343
362
- it ( 'Should correctly pass through socketTimeoutMS and connectTimeoutMS' , {
363
- metadata : {
364
- requires : {
365
- topology : [ 'single' , 'replicaset' , 'sharded' ]
366
- }
367
- } ,
368
-
369
- test : function ( done ) {
370
- const configuration = this . configuration ;
371
- const client = configuration . newClient (
372
- { } ,
373
- {
374
- socketTimeoutMS : 0 ,
375
- connectTimeoutMS : 0
376
- }
377
- ) ;
378
-
379
- client . connect ( function ( err , client ) {
380
- expect ( err ) . to . not . exist ;
381
- const topology = getTopology ( client . db ( configuration . db ) ) ;
382
- expect ( topology ) . nested . property ( 's.options.connectTimeoutMS' ) . to . equal ( 0 ) ;
383
- expect ( topology ) . nested . property ( 's.options.socketTimeoutMS' ) . to . equal ( 0 ) ;
384
-
385
- client . close ( done ) ;
386
- } ) ;
387
- }
388
- } ) ;
389
-
390
- it ( 'should open a new MongoClient connection' , {
391
- metadata : {
392
- requires : {
393
- topology : [ 'single' ]
344
+ it ( 'Should correctly pass through socketTimeoutMS and connectTimeoutMS' , async function ( ) {
345
+ const configuration = this . configuration ;
346
+ const client = configuration . newClient (
347
+ { } ,
348
+ {
349
+ socketTimeoutMS : 0 ,
350
+ connectTimeoutMS : 0
394
351
}
395
- } ,
396
-
397
- test : function ( done ) {
398
- const configuration = this . configuration ;
399
- const client = configuration . newClient ( ) ;
400
- client . connect ( function ( err , mongoclient ) {
401
- expect ( err ) . to . not . exist ;
402
-
403
- mongoclient
404
- . db ( 'integration_tests' )
405
- . collection ( 'new_mongo_client_collection' )
406
- . insertOne ( { a : 1 } , function ( err , r ) {
407
- expect ( err ) . to . not . exist ;
408
- expect ( r ) . to . be . an ( 'object' ) ;
409
-
410
- mongoclient . close ( done ) ;
411
- } ) ;
412
- } ) ;
413
- }
414
- } ) ;
352
+ ) ;
415
353
416
- it ( 'should correctly connect with MongoClient ` connect` using Promise' , function ( ) {
417
- const configuration = this . configuration ;
418
- let url = configuration . url ( ) ;
419
- url = url . indexOf ( '?' ) !== - 1 ? ` ${ url } &maxPoolSize=100` : ` ${ url } ?maxPoolSize=100` ;
354
+ await client . connect ( ) ;
355
+ const topology = client . topology ;
356
+ expect ( topology ) . nested . property ( 's.options.connectTimeoutMS' ) . to . equal ( 0 ) ;
357
+ expect ( topology ) . nested . property ( 's.options.socketTimeoutMS' ) . to . equal ( 0 ) ;
420
358
421
- const client = configuration . newClient ( url ) ;
422
- return client . connect ( ) . then ( ( ) => client . close ( ) ) ;
359
+ await client . close ( ) ;
423
360
} ) ;
424
361
425
- it ( 'should open a new MongoClient connection using promise' , {
426
- metadata : {
427
- requires : {
428
- topology : [ 'single' ]
429
- }
430
- } ,
431
-
432
- test : function ( done ) {
433
- const configuration = this . configuration ;
434
- const client = configuration . newClient ( ) ;
435
- client . connect ( ) . then ( function ( mongoclient ) {
436
- mongoclient
437
- . db ( 'integration_tests' )
438
- . collection ( 'new_mongo_client_collection' )
439
- . insertOne ( { a : 1 } )
440
- . then ( function ( r ) {
441
- expect ( r ) . to . exist ;
442
-
443
- mongoclient . close ( done ) ;
444
- } ) ;
445
- } ) ;
446
- }
447
- } ) ;
362
+ // TODO(NODE-7219): remove unnecessary test
363
+ // it('should open a new MongoClient connection', {
364
+ // metadata: {
365
+ // requires: {
366
+ // topology: ['single']
367
+ // }
368
+ // },
369
+
370
+ // test: function (done) {
371
+ // const configuration = this.configuration;
372
+ // const client = configuration.newClient();
373
+ // client.connect(function (err, mongoclient) {
374
+ // expect(err).to.not.exist;
375
+
376
+ // mongoclient
377
+ // .db('integration_tests')
378
+ // .collection('new_mongo_client_collection')
379
+ // .insertOne({ a: 1 }, function (err, r) {
380
+ // expect(err).to.not.exist;
381
+ // expect(r).to.be.an('object');
382
+
383
+ // mongoclient.close(done);
384
+ // });
385
+ // });
386
+ // }
387
+ // });
388
+
389
+ // TODO(NODE-7219): remove unnecessary test
390
+ // it('should correctly connect with MongoClient `connect` using Promise', function () {
391
+ // const configuration = this.configuration;
392
+ // let url = configuration.url();
393
+ // url = url.indexOf('?') !== -1 ? `${url}&maxPoolSize=100` : `${url}?maxPoolSize=100`;
394
+
395
+ // const client = configuration.newClient(url);
396
+ // return client.connect().then(() => client.close());
397
+ // });
398
+
399
+ // TODO(NODE-7219): remove unnecessary test
400
+ // it('should open a new MongoClient connection using promise', {
401
+ // metadata: {
402
+ // requires: {
403
+ // topology: ['single']
404
+ // }
405
+ // },
406
+
407
+ // test: function (done) {
408
+ // const configuration = this.configuration;
409
+ // const client = configuration.newClient();
410
+ // client.connect().then(function (mongoclient) {
411
+ // mongoclient
412
+ // .db('integration_tests')
413
+ // .collection('new_mongo_client_collection')
414
+ // .insertOne({ a: 1 })
415
+ // .then(function (r) {
416
+ // expect(r).to.exist;
417
+
418
+ // mongoclient.close(done);
419
+ // });
420
+ // });
421
+ // }
422
+ // });
448
423
449
424
it ( 'should be able to access a database named "constructor"' , function ( ) {
450
425
const client = this . configuration . newClient ( ) ;
@@ -474,28 +449,18 @@ describe('class MongoClient', function () {
474
449
expect ( client . readPreference ) . to . have . property ( 'mode' , ReadPreference . SECONDARY ) ;
475
450
} ) ;
476
451
477
- it ( 'should error on unexpected options' , {
478
- metadata : { requires : { topology : 'single' } } ,
479
-
480
- test : function ( done ) {
481
- const configuration = this . configuration ;
482
- MongoClient . connect (
483
- configuration . url ( ) ,
484
- {
485
- maxPoolSize : 4 ,
486
- // @ts -expect-error: unexpected option test
487
- notlegal : { } ,
488
- validateOptions : true
489
- } ,
490
- function ( err , client ) {
491
- expect ( err )
492
- . property ( 'message' )
493
- . to . match ( / o p t i o n s n o t l e g a l , v a l i d a t e o p t i o n s a r e n o t s u p p o r t e d / ) ;
494
- expect ( client ) . to . not . exist ;
495
- done ( ) ;
496
- }
497
- ) ;
498
- }
452
+ it ( 'should error on unexpected options' , async function ( ) {
453
+ const configuration = this . configuration ;
454
+ const error = await MongoClient . connect ( configuration . url ( ) , {
455
+ maxPoolSize : 4 ,
456
+ // @ts -expect-error: unexpected option test
457
+ notlegal : { } ,
458
+ validateOptions : true
459
+ } ) . catch ( e => e ) ;
460
+
461
+ expect ( error )
462
+ . property ( 'message' )
463
+ . to . match ( / o p t i o n s n o t l e g a l , v a l i d a t e o p t i o n s a r e n o t s u p p o r t e d / ) ;
499
464
} ) ;
500
465
501
466
it ( 'should error on unexpected options (promise)' , {
0 commit comments