Skip to content

Commit 1477d90

Browse files
migrate cbs to promises
1 parent 4e7d05d commit 1477d90

File tree

1 file changed

+130
-165
lines changed

1 file changed

+130
-165
lines changed

test/integration/node-specific/mongo_client.test.ts

Lines changed: 130 additions & 165 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,15 @@ import {
88
type CommandFailedEvent,
99
type CommandStartedEvent,
1010
type CommandSucceededEvent,
11-
Connection,
1211
Db,
13-
getTopology,
1412
MongoClient,
1513
MongoNotConnectedError,
1614
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';
2120
import { clearFailPoint, configureFailPoint } from '../../tools/utils';
2221
import { setupDatabase } from '../shared';
2322

@@ -34,16 +33,17 @@ describe('class MongoClient', function () {
3433
client = undefined;
3534
});
3635

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 () {
4040
const configuration = this.configuration;
4141
const client = configuration.newClient(
4242
{},
4343
{
4444
writeConcern: { w: 1, wtimeoutMS: 1000, fsync: true, j: true },
4545
readPreference: 'nearest',
46-
readPreferenceTags: { loc: 'ny' },
46+
readPreferenceTags: [{ loc: 'ny' }],
4747
forceServerObjectId: true,
4848
pkFactory: {
4949
createPk() {
@@ -54,74 +54,55 @@ describe('class MongoClient', function () {
5454
}
5555
);
5656

57-
client.connect(function (err, client) {
58-
expect(err).to.be.undefined;
57+
await client.connect();
5958

60-
const db = client.db(configuration.db);
59+
const db = client.db(configuration.db);
6160

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);
6665

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' }]);
7372

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');
7978

80-
client.close(done);
81-
});
79+
await client.close();
8280
}
83-
});
81+
);
8482

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+
);
9487
});
9588

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+
});
10994

110-
done();
111-
});
112-
}
95+
const error = await client.connect().catch(e => e);
96+
expect(error).to.be.instanceOf(MongoServerSelectionError);
11397
});
11498

11599
it('should correctly connect to mongodb using domain socket', {
116100
metadata: { requires: { topology: ['single'], os: '!win32' } },
117-
118-
test: function (done) {
101+
test: async function () {
119102
const configuration = this.configuration;
120103
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();
125106
}
126107
});
127108

@@ -236,6 +217,7 @@ describe('class MongoClient', function () {
236217
beforeEach(async function () {
237218
spy = sinon.spy(net, 'createConnection');
238219
const uri = this.configuration.url();
220+
// @ts-expect-error Intentional test of invalid options
239221
client = new MongoClient(uri, options);
240222
});
241223

@@ -359,92 +341,85 @@ describe('class MongoClient', function () {
359341
expect(name).to.equal('hello world');
360342
});
361343

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
394351
}
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+
);
415353

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);
420358

421-
const client = configuration.newClient(url);
422-
return client.connect().then(() => client.close());
359+
await client.close();
423360
});
424361

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+
// });
448423

449424
it('should be able to access a database named "constructor"', function () {
450425
const client = this.configuration.newClient();
@@ -474,28 +449,18 @@ describe('class MongoClient', function () {
474449
expect(client.readPreference).to.have.property('mode', ReadPreference.SECONDARY);
475450
});
476451

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(/options notlegal, validateoptions are not supported/);
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(/options notlegal, validateoptions are not supported/);
499464
});
500465

501466
it('should error on unexpected options (promise)', {

0 commit comments

Comments
 (0)