Skip to content

Commit 3a5232a

Browse files
committed
fix(tests): migrate 2.x tests to 3.x
Mostly related to `db.open` vs `db.connect`, these are tests that were introduced recently in 2.x and needed massaging to work with the major changes introduced in 3.x.
1 parent 0b43abc commit 3a5232a

File tree

5 files changed

+44
-33
lines changed

5 files changed

+44
-33
lines changed

lib/mongo_client.js

+5
Original file line numberDiff line numberDiff line change
@@ -684,6 +684,11 @@ var connect = function(self, url, options, callback) {
684684
|| url instanceof Mongos) {
685685

686686
//authenticate(client, options.user, options.password, options, function(err, success) {
687+
// Set the topology
688+
self.topology = url;
689+
// Add listeners
690+
addListeners(self, url);
691+
// Connect
687692
return url.connect(options, connectHandler(self, options, function(err, topology) {
688693
if(err) return connectCallback(err, topology);
689694
if(options.user || options.password || options.authMechanism) {

test/functional/bulk_tests.js

+26-20
Original file line numberDiff line numberDiff line change
@@ -382,8 +382,9 @@ exports['Should return an error when no operations in ordered batch'] = {
382382

383383
// The actual test we wish to run
384384
test: function(configuration, test) {
385-
var db = configuration.newDbInstance({w:1}, {poolSize:1, auto_reconnect:false});
386-
db.open(function(err, db) {
385+
var client = configuration.newDbInstance({w:1}, {poolSize:1, auto_reconnect:false});
386+
client.connect(function(err, client) {
387+
var db = client.db(configuration.database);
387388
// Get the collection
388389
var col = db.collection('batch_write_ordered_ops_8');
389390

@@ -392,7 +393,7 @@ exports['Should return an error when no operations in ordered batch'] = {
392393
test.equal(err instanceof Error, true);
393394
test.equal(err.message, 'Invalid Operation, no operations specified');
394395

395-
db.close();
396+
client.close();
396397
test.done();
397398
});
398399
});
@@ -894,8 +895,9 @@ exports['Should return an error when no operations in unordered batch'] = {
894895

895896
// The actual test we wish to run
896897
test: function(configuration, test) {
897-
var db = configuration.newDbInstance({w:1}, {poolSize:1, auto_reconnect:false});
898-
db.open(function(err, db) {
898+
var client = configuration.newDbInstance({w:1}, {poolSize:1, auto_reconnect:false});
899+
client.connect(function(err, client) {
900+
var db = client.db(configuration.database);
899901
// Get the collection
900902
var col = db.collection('batch_write_ordered_ops_8');
901903

@@ -904,7 +906,7 @@ exports['Should return an error when no operations in unordered batch'] = {
904906
test.equal(err instanceof Error, true);
905907
test.equal(err.message, 'Invalid Operation, no operations specified');
906908

907-
db.close();
909+
client.close();
908910
test.done();
909911
});
910912
});
@@ -1249,14 +1251,14 @@ exports['Should correctly handle bulk operation split for unordered bulk operati
12491251

12501252
exports['Should return an error instead of throwing when no operations are provided for ordered bulk operation execute'] = {
12511253
metadata: { requires: { mongodb: ">=2.6.0" , topology: 'single', node: ">0.10.0" } },
1252-
test: function(configure, test) {
1253-
var db = configure.newDbInstance({ w: 1 }, { poolSize: 1 });
1254-
1255-
db.open(function(err, db) {
1254+
test: function(configuration, test) {
1255+
var client = configuration.newDbInstance({ w: 1 }, { poolSize: 1 });
1256+
client.connect(function(err, client) {
1257+
var db = client.db(configuration.database);
12561258
db.collection('doesnt_matter').insertMany([], function(err, r) {
12571259
test.equal(err instanceof Error, true);
12581260
test.equal(err.message, 'Invalid Operation, no operations specified');
1259-
db.close();
1261+
client.close();
12601262
test.done();
12611263
});
12621264
});
@@ -1265,14 +1267,15 @@ exports['Should return an error instead of throwing when no operations are provi
12651267

12661268
exports['Should return an error instead of throwing when no operations are provided for unordered bulk operation execute'] = {
12671269
metadata: { requires: { mongodb: ">=2.6.0" , topology: 'single', node: ">0.10.0" } },
1268-
test: function(configure, test) {
1269-
var db = configure.newDbInstance({ w: 1 }, { poolSize: 1 });
1270+
test: function(configuration, test) {
1271+
var client = configuration.newDbInstance({ w: 1 }, { poolSize: 1 });
12701272

1271-
db.open(function(err, db) {
1273+
client.connect(function(err, client) {
1274+
var db = client.db(configuration.database);
12721275
db.collection('doesnt_matter').insertMany([], { ordered: false }, function(err, r) {
12731276
test.equal(err instanceof Error, true);
12741277
test.equal(err.message, 'Invalid Operation, no operations specified');
1275-
db.close();
1278+
client.close();
12761279
test.done();
12771280
});
12781281
});
@@ -1281,11 +1284,14 @@ exports['Should return an error instead of throwing when no operations are provi
12811284

12821285
exports['Should return an error instead of throwing when an empty bulk operation is submitted (with promise)'] = {
12831286
metadata: { requires: { promises: true, node: ">0.12.0" } },
1284-
test: function(configure, test) {
1285-
var db = configure.newDbInstance({ w: 1 }, { poolSize: 1 });
1287+
test: function(configuration, test) {
1288+
var client = configuration.newDbInstance({ w: 1 }, { poolSize: 1 });
12861289

1287-
return db.open()
1288-
.then(function() { return db.collection('doesnt_matter').insertMany([]); })
1290+
return client.connect()
1291+
.then(function() {
1292+
var db = client.db(configuration.database);
1293+
return db.collection('doesnt_matter').insertMany([]);
1294+
})
12891295
.then(function() {
12901296
test.equal(false, true); // this should not happen!
12911297
})
@@ -1294,7 +1300,7 @@ exports['Should return an error instead of throwing when an empty bulk operation
12941300
test.equal(err.message, 'Invalid Operation, no operations specified');
12951301
})
12961302
.then(function() {
1297-
db.close();
1303+
client.close();
12981304
test.done();
12991305
});
13001306
}

test/functional/connection_tests.js

+6-7
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ exports['Should correctly start monitoring for single server connection'] = {
99
// The actual test we wish to run
1010
test: function(configuration, test) {
1111
var client = configuration.newDbInstanceWithDomainSocket({w:1}, {poolSize: 1, host: "/tmp/mongodb-27017.sock"});
12-
console.log("$$$$$$$$$$$$$$$$$$$$$$$")
13-
console.dir(client)
1412
client.connect(function(err, client) {
1513
var db = client.db(configuration.database);
1614
test.equal(null, err);
@@ -138,7 +136,7 @@ exports['Should connect to server using domain socket with undefined port'] = {
138136
test: function(configuration, test) {
139137
var client = configuration.newDbInstanceWithDomainSocket({w:1}, {poolSize: 1, host: "/tmp/mongodb-27017.sock", port:undefined});
140138
client.connect(function(err, db) {
141-
var db = client.db(configuration.database);
139+
var db = client.db(configuration.database);
142140
test.equal(null, err);
143141

144142
db.collection("domainSocketCollection1").insert({x:1}, {w:1}, function(err, item) {
@@ -311,20 +309,21 @@ exports.testConnectGoodAuthAsOption = {
311309
var connect = configuration.require;
312310
var user = 'testConnectGoodAuthAsOption', password = 'password';
313311
// First add a user.
314-
connect(configuration.url(), function(err, db) {
312+
connect(configuration.url(), function(err, client) {
315313
test.equal(err, null);
314+
var db = client.db(configuration.database);
316315

317316
db.addUser(user, password, function(err, result) {
318317
test.equal(err, null);
319-
db.close();
318+
client.close();
320319
restOfTest();
321320
});
322321
});
323322

324323
function restOfTest() {
325324
var opts = { auth: { user: user, password: password } };
326-
connect(configuration.url('baduser', 'badpassword'), opts, connectionTester(test, 'testConnectGoodAuthAsOption', function(db) {
327-
db.close();
325+
connect(configuration.url('baduser', 'badpassword'), opts, connectionTester(test, configuration, 'testConnectGoodAuthAsOption', function(client) {
326+
client.close();
328327
test.done();
329328
}));
330329
}

test/functional/cursor_tests.js

+6-5
Original file line numberDiff line numberDiff line change
@@ -3031,7 +3031,7 @@ exports['Should correctly apply map to forEach'] = {
30313031
test.equal(4, doc.a);
30323032
}, function(err, doc) {
30333033
test.equal(null, err);
3034-
db.close();
3034+
client.close();
30353035
test.done();
30363036
});
30373037
})
@@ -3057,8 +3057,9 @@ exports['Should correctly apply multiple uses of map and apply forEach'] = {
30573057
docs[i] = {'a':i, createdAt:new Date(d)};
30583058
}
30593059

3060-
var db = configuration.newDbInstance(configuration.writeConcernMax(), {poolSize:1});
3061-
db.open(function(err, db) {
3060+
var client = configuration.newDbInstance(configuration.writeConcernMax(), {poolSize:1});
3061+
client.connect(function(err, client) {
3062+
var db = client.db(configuration.database);
30623063
test.equal(null, err);
30633064

30643065
var collection = db.collection('map_mapmapforEach');
@@ -3384,7 +3385,7 @@ exports['Correcly decorate the cursor count command with skip, limit, hint, read
33843385
.hint({project:1}).count(true, function(err, r) {
33853386
test.equal(null, err);
33863387
test.equal(1, started.length);
3387-
if(started[0].command.readConcern)
3388+
if(started[0].command.readConcern)
33883389
test.deepEqual({level: 'local'}, started[0].command.readConcern);
33893390
test.deepEqual({ project: 1 }, started[0].command.hint);
33903391
test.equal(5, started[0].command.skip);
@@ -3427,7 +3428,7 @@ exports['Correcly decorate the collection cursor count command with skip, limit,
34273428
}, function(err, r) {
34283429
test.equal(null, err);
34293430
test.equal(1, started.length);
3430-
if(started[0].command.readConcern)
3431+
if(started[0].command.readConcern)
34313432
test.deepEqual({level: 'local'}, started[0].command.readConcern);
34323433
test.deepEqual({ project: 1 }, started[0].command.hint);
34333434
test.equal(5, started[0].command.skip);

test/functional/mongo_client_tests.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -587,7 +587,7 @@ exports['Should correctly pass through appname in options'] = {
587587
// console.dir(url)
588588
MongoClient.connect(url, {appname: 'hello world'}, function(err, db) {
589589
test.equal(null, err);
590-
test.equal('hello world', db.serverConfig.clientInfo.application.name);
590+
test.equal('hello world', db.topology.clientInfo.application.name);
591591

592592
db.close();
593593
test.done();

0 commit comments

Comments
 (0)