Skip to content

Commit 96d2ca6

Browse files
authored
test: improve shared helpers
NODE-2605
1 parent e5b762c commit 96d2ca6

File tree

7 files changed

+209
-239
lines changed

7 files changed

+209
-239
lines changed

.evergreen/install-dependencies.sh

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,17 @@ NPM_TMP_DIR="${NODE_ARTIFACTS_PATH}/tmp"
99

1010
# this needs to be explicitly exported for the nvm install below
1111
export NVM_DIR="${NODE_ARTIFACTS_PATH}/nvm"
12+
export XDG_CONFIG_HOME=${NODE_ARTIFACTS_PATH}
1213

1314
# create node artifacts path if needed
1415
mkdir -p ${NODE_ARTIFACTS_PATH}
1516
mkdir -p ${NPM_CACHE_DIR}
1617
mkdir -p "${NPM_TMP_DIR}"
1718

1819
# install Node.js
19-
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.8/install.sh | bash
20+
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.35.3/install.sh | bash
2021
[ -s "${NVM_DIR}/nvm.sh" ] && \. "${NVM_DIR}/nvm.sh"
21-
nvm install --lts=${NODE_LTS_NAME}
22+
nvm install --no-progress --lts=${NODE_LTS_NAME}
2223

2324
# setup npm cache in a local directory
2425
cat <<EOT > .npmrc

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@
7272
"check:atlas": "node test/tools/atlas_connectivity_tests.js",
7373
"check:bench": "node test/benchmarks/driverBench",
7474
"check:coverage": "nyc mocha --timeout 60000 --recursive test/functional test/unit",
75-
"check:lint": "eslint index.js lib test",
75+
"check:lint": "eslint -v && eslint index.js lib test",
7676
"check:test": "mocha --recursive test/functional test/unit",
7777
"check:types": "tsc -p tsconfig.check.json",
7878
"release": "standard-version -i HISTORY.md",

test/functional/change_stream.test.js

Lines changed: 40 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
const assert = require('assert');
33
const { Transform } = require('stream');
44
const { MongoError, MongoNetworkError } = require('../../lib/error');
5-
const { setupDatabase, withTempDb, delay } = require('./shared');
5+
const { delay, setupDatabase, withClient } = require('./shared');
66
const co = require('co');
77
const mock = require('mongodb-mock-server');
88
const chai = require('chai');
@@ -2599,65 +2599,59 @@ describe('Change Streams', function() {
25992599
});
26002600

26012601
describe('tryNext', function() {
2602+
function withTemporaryCollectionOnDb(database, testFn) {
2603+
return withClient((client, done) => {
2604+
const db = client.db(database);
2605+
db.createCollection('test', { w: 'majority' }, (err, collection) => {
2606+
if (err) return done(err);
2607+
testFn(collection, () => db.dropDatabase(done));
2608+
});
2609+
});
2610+
}
26022611
it('should return null on single iteration of empty cursor', {
26032612
metadata: { requires: { topology: 'replicaset', mongodb: '>=3.6' } },
2604-
test: function() {
2605-
return withTempDb(
2606-
'testTryNext',
2607-
{ w: 'majority' },
2608-
this.configuration.newClient(),
2609-
db => done => {
2610-
const changeStream = db.collection('test').watch();
2611-
tryNext(changeStream, (err, doc) => {
2612-
expect(err).to.not.exist;
2613-
expect(doc).to.not.exist;
2613+
test: withTemporaryCollectionOnDb('testTryNext', (collection, done) => {
2614+
const changeStream = collection.watch();
2615+
tryNext(changeStream, (err, doc) => {
2616+
expect(err).to.not.exist;
2617+
expect(doc).to.not.exist;
26142618

2615-
changeStream.close(done);
2616-
});
2617-
}
2618-
);
2619-
}
2619+
changeStream.close(done);
2620+
});
2621+
})
26202622
});
26212623

26222624
it('should iterate a change stream until first empty batch', {
26232625
metadata: { requires: { topology: 'replicaset', mongodb: '>=3.6' } },
2624-
test: function() {
2625-
return withTempDb(
2626-
'testTryNext',
2627-
{ w: 'majority' },
2628-
this.configuration.newClient(),
2629-
db => done => {
2630-
const collection = db.collection('test');
2631-
const changeStream = collection.watch();
2632-
waitForStarted(changeStream, () => {
2633-
collection.insertOne({ a: 42 }, err => {
2634-
expect(err).to.not.exist;
2626+
test: withTemporaryCollectionOnDb('testTryNext', (collection, done) => {
2627+
const changeStream = collection.watch();
2628+
waitForStarted(changeStream, () => {
2629+
collection.insertOne({ a: 42 }, err => {
2630+
expect(err).to.not.exist;
26352631

2636-
collection.insertOne({ b: 24 }, err => {
2637-
expect(err).to.not.exist;
2638-
});
2639-
});
2632+
collection.insertOne({ b: 24 }, err => {
2633+
expect(err).to.not.exist;
26402634
});
2635+
});
2636+
});
26412637

2642-
tryNext(changeStream, (err, doc) => {
2643-
expect(err).to.not.exist;
2644-
expect(doc).to.exist;
2638+
tryNext(changeStream, (err, doc) => {
2639+
expect(err).to.not.exist;
2640+
expect(doc).to.exist;
26452641

2646-
tryNext(changeStream, (err, doc) => {
2647-
expect(err).to.not.exist;
2648-
expect(doc).to.exist;
2642+
tryNext(changeStream, (err, doc) => {
2643+
expect(err).to.not.exist;
2644+
expect(doc).to.exist;
26492645

2650-
tryNext(changeStream, (err, doc) => {
2651-
expect(err).to.not.exist;
2652-
expect(doc).to.not.exist;
2646+
tryNext(changeStream, (err, doc) => {
2647+
expect(err).to.not.exist;
2648+
expect(doc).to.not.exist;
26532649

2654-
changeStream.close(done);
2655-
});
2656-
});
2650+
changeStream.close(done);
26572651
});
2658-
}
2659-
);
2660-
}
2652+
});
2653+
});
2654+
})
26612655
});
26622656
});
26632657

test/functional/index.test.js

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
'use strict';
2-
var test = require('./shared').assert;
3-
var setupDatabase = require('./shared').setupDatabase;
4-
const expect = require('chai').expect;
5-
const withClient = require('./shared').withClient;
6-
const withMonitoredClient = require('./shared').withMonitoredClient;
2+
const { expect } = require('chai');
3+
const { assert: test, setupDatabase, withClient, withMonitoredClient } = require('./shared');
74

85
describe('Indexes', function() {
96
before(function() {
@@ -1208,20 +1205,18 @@ describe('Indexes', function() {
12081205
function throwErrorTest(testCommand) {
12091206
return {
12101207
metadata: { requires: { mongodb: '<4.4' } },
1211-
test: function() {
1212-
return withClient(this.configuration.newClient(), client => done => {
1213-
const db = client.db('test');
1214-
const collection = db.collection('commitQuorum');
1215-
testCommand(db, collection, (err, result) => {
1216-
expect(err).to.exist;
1217-
expect(err.message).to.equal(
1218-
'`commitQuorum` option for `createIndexes` not supported on servers < 4.4'
1219-
);
1220-
expect(result).to.not.exist;
1221-
done();
1222-
});
1208+
test: withClient((client, done) => {
1209+
const db = client.db('test');
1210+
const collection = db.collection('commitQuorum');
1211+
testCommand(db, collection, (err, result) => {
1212+
expect(err).to.exist;
1213+
expect(err.message).to.equal(
1214+
'`commitQuorum` option for `createIndexes` not supported on servers < 4.4'
1215+
);
1216+
expect(result).to.not.exist;
1217+
done();
12231218
});
1224-
}
1219+
})
12251220
};
12261221
}
12271222
it(

test/functional/logger.test.js

Lines changed: 23 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict';
2-
var expect = require('chai').expect;
3-
var connectToDb = require('./shared').connectToDb;
2+
const expect = require('chai').expect;
3+
const { withClient } = require('./shared');
44
const Logger = require('../../lib/logger');
55

66
describe('Logger', function() {
@@ -54,27 +54,20 @@ describe('Logger', function() {
5454
it('should not fail with undefined id', {
5555
metadata: { requires: { topology: ['single'] } },
5656

57-
test: function(done) {
58-
var self = this;
59-
57+
test: function() {
6058
// set a custom logger per http://mongodb.github.io/node-mongodb-native/2.0/tutorials/logging/
6159
Logger.setCurrentLogger(function() {});
6260
Logger.setLevel('debug');
6361

64-
connectToDb('mongodb://localhost:27017/test', self.configuration.db, function(
65-
err,
66-
db,
67-
client
68-
) {
69-
expect(err).to.not.exist;
70-
62+
return withClient('mongodb://localhost:27017/test', (client, done) => {
63+
const db = client.db(this.configuration.db);
7164
// perform any operation that gets logged
7265
db.collection('foo').findOne({}, function(err) {
7366
expect(err).to.not.exist;
7467

7568
// Clean up
7669
Logger.reset();
77-
client.close(done);
70+
done();
7871
});
7972
});
8073
}
@@ -86,16 +79,9 @@ describe('Logger', function() {
8679
it('should correctly log cursor', {
8780
metadata: { requires: { topology: ['single'] } },
8881

89-
test: function(done) {
90-
var self = this;
91-
92-
connectToDb('mongodb://localhost:27017/test', self.configuration.db, function(
93-
err,
94-
db,
95-
client
96-
) {
97-
expect(err).to.not.exist;
98-
82+
test: function() {
83+
return withClient('mongodb://localhost:27017/test', (client, done) => {
84+
const db = client.db(this.configuration.db);
9985
// Status
10086
var logged = false;
10187

@@ -120,7 +106,7 @@ describe('Logger', function() {
120106

121107
// Clean up
122108
Logger.reset();
123-
client.close(done);
109+
done();
124110
});
125111
});
126112
}
@@ -132,35 +118,28 @@ describe('Logger', function() {
132118
it('should pass the logLevel down through the options', {
133119
metadata: { requires: { topology: ['single'] } },
134120

135-
test: function(done) {
136-
var self = this;
137-
121+
test: function() {
138122
Logger.filter('class', ['Cursor']);
139123
var logged = false;
140124

141-
connectToDb(
142-
'mongodb://localhost:27017/test',
143-
self.configuration.db,
144-
{
125+
return withClient('mongodb://localhost:27017/test', (client, done) => {
126+
const db = client.db(this.configuration.db, {
145127
loggerLevel: 'debug',
146128
logger: function() {
147129
logged = true;
148130
}
149-
},
150-
function(err, db, client) {
151-
expect(err).to.not.exist;
131+
});
152132

153-
// perform any operation that gets logged
154-
db.collection('foo').findOne({}, function(err) {
155-
expect(err).to.not.exist;
156-
expect(logged).to.be.true;
133+
// perform any operation that gets logged
134+
db.collection('foo').findOne({}, function(err) {
135+
expect(err).to.not.exist;
136+
expect(logged).to.be.true;
157137

158-
// Clean up
159-
Logger.reset();
160-
client.close(done);
161-
});
162-
}
163-
);
138+
// Clean up
139+
Logger.reset();
140+
done();
141+
});
142+
});
164143
}
165144
});
166145
});

0 commit comments

Comments
 (0)