Skip to content

Commit 7fad15a

Browse files
authored
test: improve shared helpers
NODE-2605
1 parent 246669f commit 7fad15a

File tree

6 files changed

+306
-222
lines changed

6 files changed

+306
-222
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
@@ -68,7 +68,7 @@
6868
"test": "npm run lint && mocha --recursive test/functional test/unit test/core",
6969
"test-nolint": "mocha --recursive test/functional test/unit test/core",
7070
"coverage": "istanbul cover mongodb-test-runner -- -t 60000 test/core test/unit test/functional",
71-
"lint": "eslint lib test",
71+
"lint": "eslint -v && eslint lib test",
7272
"format": "prettier --print-width 100 --tab-width 2 --single-quote --write 'test/**/*.js' 'lib/**/*.js'",
7373
"bench": "node test/benchmarks/driverBench/",
7474
"generate-evergreen": "node .evergreen/generate_evergreen_tasks.js",

test/functional/change_stream.test.js

Lines changed: 40 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ var Transform = require('stream').Transform;
44
const MongoError = require('../../lib/core').MongoError;
55
var MongoNetworkError = require('../../lib/core').MongoNetworkError;
66
var setupDatabase = require('./shared').setupDatabase;
7-
var withTempDb = require('./shared').withTempDb;
7+
var withClient = require('./shared').withClient;
88
var delay = require('./shared').delay;
99
var co = require('co');
1010
var mock = require('mongodb-mock-server');
@@ -2632,65 +2632,59 @@ describe('Change Streams', function() {
26322632
});
26332633

26342634
describe('tryNext', function() {
2635+
function withTemporaryCollectionOnDb(database, testFn) {
2636+
return withClient((client, done) => {
2637+
const db = client.db(database);
2638+
db.createCollection('test', { w: 'majority' }, (err, collection) => {
2639+
if (err) return done(err);
2640+
testFn(collection, () => db.dropDatabase(done));
2641+
});
2642+
});
2643+
}
26352644
it('should return null on single iteration of empty cursor', {
26362645
metadata: { requires: { topology: 'replicaset', mongodb: '>=3.6' } },
2637-
test: function() {
2638-
return withTempDb(
2639-
'testTryNext',
2640-
{ w: 'majority' },
2641-
this.configuration.newClient(),
2642-
db => done => {
2643-
const changeStream = db.collection('test').watch();
2644-
tryNext(changeStream, (err, doc) => {
2645-
expect(err).to.not.exist;
2646-
expect(doc).to.not.exist;
2646+
test: withTemporaryCollectionOnDb('testTryNext', (collection, done) => {
2647+
const changeStream = collection.watch();
2648+
tryNext(changeStream, (err, doc) => {
2649+
expect(err).to.not.exist;
2650+
expect(doc).to.not.exist;
26472651

2648-
changeStream.close(done);
2649-
});
2650-
}
2651-
);
2652-
}
2652+
changeStream.close(done);
2653+
});
2654+
})
26532655
});
26542656

26552657
it('should iterate a change stream until first empty batch', {
26562658
metadata: { requires: { topology: 'replicaset', mongodb: '>=3.6' } },
2657-
test: function() {
2658-
return withTempDb(
2659-
'testTryNext',
2660-
{ w: 'majority' },
2661-
this.configuration.newClient(),
2662-
db => done => {
2663-
const collection = db.collection('test');
2664-
const changeStream = collection.watch();
2665-
waitForStarted(changeStream, () => {
2666-
collection.insertOne({ a: 42 }, err => {
2667-
expect(err).to.not.exist;
2659+
test: withTemporaryCollectionOnDb('testTryNext', (collection, done) => {
2660+
const changeStream = collection.watch();
2661+
waitForStarted(changeStream, () => {
2662+
collection.insertOne({ a: 42 }, err => {
2663+
expect(err).to.not.exist;
26682664

2669-
collection.insertOne({ b: 24 }, err => {
2670-
expect(err).to.not.exist;
2671-
});
2672-
});
2665+
collection.insertOne({ b: 24 }, err => {
2666+
expect(err).to.not.exist;
26732667
});
2668+
});
2669+
});
26742670

2675-
tryNext(changeStream, (err, doc) => {
2676-
expect(err).to.not.exist;
2677-
expect(doc).to.exist;
2671+
tryNext(changeStream, (err, doc) => {
2672+
expect(err).to.not.exist;
2673+
expect(doc).to.exist;
26782674

2679-
tryNext(changeStream, (err, doc) => {
2680-
expect(err).to.not.exist;
2681-
expect(doc).to.exist;
2675+
tryNext(changeStream, (err, doc) => {
2676+
expect(err).to.not.exist;
2677+
expect(doc).to.exist;
26822678

2683-
tryNext(changeStream, (err, doc) => {
2684-
expect(err).to.not.exist;
2685-
expect(doc).to.not.exist;
2679+
tryNext(changeStream, (err, doc) => {
2680+
expect(err).to.not.exist;
2681+
expect(doc).to.not.exist;
26862682

2687-
changeStream.close(done);
2688-
});
2689-
});
2683+
changeStream.close(done);
26902684
});
2691-
}
2692-
);
2693-
}
2685+
});
2686+
});
2687+
})
26942688
});
26952689
});
26962690

test/functional/logger.test.js

Lines changed: 25 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict';
22
var expect = require('chai').expect;
3-
var connectToDb = require('./shared').connectToDb;
3+
var withClient = require('./shared').withClient;
44

55
describe('Logger', function() {
66
/**
@@ -57,29 +57,21 @@ describe('Logger', function() {
5757
it('should not fail with undefined id', {
5858
metadata: { requires: { topology: ['single'] } },
5959

60-
// The actual test we wish to run
61-
test: function(done) {
62-
var self = this,
63-
Logger = self.configuration.require.Logger;
64-
60+
test: function() {
61+
const Logger = this.configuration.require.Logger;
6562
// set a custom logger per http://mongodb.github.io/node-mongodb-native/2.0/tutorials/logging/
6663
Logger.setCurrentLogger(function() {});
6764
Logger.setLevel('debug');
6865

69-
connectToDb('mongodb://localhost:27017/test', self.configuration.db, function(
70-
err,
71-
db,
72-
client
73-
) {
74-
expect(err).to.not.exist;
75-
66+
return withClient('mongodb://localhost:27017/test', (client, done) => {
67+
const db = client.db(this.configuration.db);
7668
// perform any operation that gets logged
7769
db.collection('foo').findOne({}, function(err) {
7870
expect(err).to.not.exist;
7971

8072
// Clean up
8173
Logger.reset();
82-
client.close(done);
74+
done();
8375
});
8476
});
8577
}
@@ -92,18 +84,10 @@ describe('Logger', function() {
9284
it('should correctly log cursor', {
9385
metadata: { requires: { topology: ['single'] } },
9486

95-
// The actual test we wish to run
96-
test: function(done) {
97-
var self = this,
98-
Logger = self.configuration.require.Logger;
99-
100-
connectToDb('mongodb://localhost:27017/test', self.configuration.db, function(
101-
err,
102-
db,
103-
client
104-
) {
105-
expect(err).to.not.exist;
106-
87+
test: function() {
88+
const Logger = this.configuration.require.Logger;
89+
return withClient('mongodb://localhost:27017/test', (client, done) => {
90+
const db = client.db(this.configuration.db);
10791
// Status
10892
var logged = false;
10993

@@ -128,7 +112,7 @@ describe('Logger', function() {
128112

129113
// Clean up
130114
Logger.reset();
131-
client.close(done);
115+
done();
132116
});
133117
});
134118
}
@@ -141,37 +125,29 @@ describe('Logger', function() {
141125
it('should pass the logLevel down through the options', {
142126
metadata: { requires: { topology: ['single'] } },
143127

144-
// The actual test we wish to run
145-
test: function(done) {
146-
var self = this,
147-
Logger = self.configuration.require.Logger;
148-
128+
test: function() {
129+
const Logger = this.configuration.require.Logger;
149130
Logger.filter('class', ['Cursor']);
150131
var logged = false;
151132

152-
connectToDb(
153-
'mongodb://localhost:27017/test',
154-
self.configuration.db,
155-
{
133+
return withClient('mongodb://localhost:27017/test', (client, done) => {
134+
const db = client.db(this.configuration.db, {
156135
loggerLevel: 'debug',
157136
logger: function() {
158137
logged = true;
159138
}
160-
},
161-
function(err, db, client) {
162-
expect(err).to.not.exist;
139+
});
163140

164-
// perform any operation that gets logged
165-
db.collection('foo').findOne({}, function(err) {
166-
expect(err).to.not.exist;
167-
expect(logged).to.be.true;
141+
// perform any operation that gets logged
142+
db.collection('foo').findOne({}, function(err) {
143+
expect(err).to.not.exist;
144+
expect(logged).to.be.true;
168145

169-
// Clean up
170-
Logger.reset();
171-
client.close(done);
172-
});
173-
}
174-
);
146+
// Clean up
147+
Logger.reset();
148+
done();
149+
});
150+
});
175151
}
176152
});
177153
});

0 commit comments

Comments
 (0)