Skip to content

Commit

Permalink
fix(tests): migrate 2.x tests to 3.x
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
mbroadst committed Aug 8, 2017
1 parent 0b43abc commit 3a5232a
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 33 deletions.
5 changes: 5 additions & 0 deletions lib/mongo_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -684,6 +684,11 @@ var connect = function(self, url, options, callback) {
|| url instanceof Mongos) {

//authenticate(client, options.user, options.password, options, function(err, success) {
// Set the topology
self.topology = url;
// Add listeners
addListeners(self, url);
// Connect
return url.connect(options, connectHandler(self, options, function(err, topology) {
if(err) return connectCallback(err, topology);
if(options.user || options.password || options.authMechanism) {
Expand Down
46 changes: 26 additions & 20 deletions test/functional/bulk_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -382,8 +382,9 @@ exports['Should return an error when no operations in ordered batch'] = {

// The actual test we wish to run
test: function(configuration, test) {
var db = configuration.newDbInstance({w:1}, {poolSize:1, auto_reconnect:false});
db.open(function(err, db) {
var client = configuration.newDbInstance({w:1}, {poolSize:1, auto_reconnect:false});
client.connect(function(err, client) {
var db = client.db(configuration.database);
// Get the collection
var col = db.collection('batch_write_ordered_ops_8');

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

db.close();
client.close();
test.done();
});
});
Expand Down Expand Up @@ -894,8 +895,9 @@ exports['Should return an error when no operations in unordered batch'] = {

// The actual test we wish to run
test: function(configuration, test) {
var db = configuration.newDbInstance({w:1}, {poolSize:1, auto_reconnect:false});
db.open(function(err, db) {
var client = configuration.newDbInstance({w:1}, {poolSize:1, auto_reconnect:false});
client.connect(function(err, client) {
var db = client.db(configuration.database);
// Get the collection
var col = db.collection('batch_write_ordered_ops_8');

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

db.close();
client.close();
test.done();
});
});
Expand Down Expand Up @@ -1249,14 +1251,14 @@ exports['Should correctly handle bulk operation split for unordered bulk operati

exports['Should return an error instead of throwing when no operations are provided for ordered bulk operation execute'] = {
metadata: { requires: { mongodb: ">=2.6.0" , topology: 'single', node: ">0.10.0" } },
test: function(configure, test) {
var db = configure.newDbInstance({ w: 1 }, { poolSize: 1 });

db.open(function(err, db) {
test: function(configuration, test) {
var client = configuration.newDbInstance({ w: 1 }, { poolSize: 1 });
client.connect(function(err, client) {
var db = client.db(configuration.database);
db.collection('doesnt_matter').insertMany([], function(err, r) {
test.equal(err instanceof Error, true);
test.equal(err.message, 'Invalid Operation, no operations specified');
db.close();
client.close();
test.done();
});
});
Expand All @@ -1265,14 +1267,15 @@ exports['Should return an error instead of throwing when no operations are provi

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

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

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

return db.open()
.then(function() { return db.collection('doesnt_matter').insertMany([]); })
return client.connect()
.then(function() {
var db = client.db(configuration.database);
return db.collection('doesnt_matter').insertMany([]);
})
.then(function() {
test.equal(false, true); // this should not happen!
})
Expand All @@ -1294,7 +1300,7 @@ exports['Should return an error instead of throwing when an empty bulk operation
test.equal(err.message, 'Invalid Operation, no operations specified');
})
.then(function() {
db.close();
client.close();
test.done();
});
}
Expand Down
13 changes: 6 additions & 7 deletions test/functional/connection_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ exports['Should correctly start monitoring for single server connection'] = {
// The actual test we wish to run
test: function(configuration, test) {
var client = configuration.newDbInstanceWithDomainSocket({w:1}, {poolSize: 1, host: "/tmp/mongodb-27017.sock"});
console.log("$$$$$$$$$$$$$$$$$$$$$$$")
console.dir(client)
client.connect(function(err, client) {
var db = client.db(configuration.database);
test.equal(null, err);
Expand Down Expand Up @@ -138,7 +136,7 @@ exports['Should connect to server using domain socket with undefined port'] = {
test: function(configuration, test) {
var client = configuration.newDbInstanceWithDomainSocket({w:1}, {poolSize: 1, host: "/tmp/mongodb-27017.sock", port:undefined});
client.connect(function(err, db) {
var db = client.db(configuration.database);
var db = client.db(configuration.database);
test.equal(null, err);

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

db.addUser(user, password, function(err, result) {
test.equal(err, null);
db.close();
client.close();
restOfTest();
});
});

function restOfTest() {
var opts = { auth: { user: user, password: password } };
connect(configuration.url('baduser', 'badpassword'), opts, connectionTester(test, 'testConnectGoodAuthAsOption', function(db) {
db.close();
connect(configuration.url('baduser', 'badpassword'), opts, connectionTester(test, configuration, 'testConnectGoodAuthAsOption', function(client) {
client.close();
test.done();
}));
}
Expand Down
11 changes: 6 additions & 5 deletions test/functional/cursor_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -3031,7 +3031,7 @@ exports['Should correctly apply map to forEach'] = {
test.equal(4, doc.a);
}, function(err, doc) {
test.equal(null, err);
db.close();
client.close();
test.done();
});
})
Expand All @@ -3057,8 +3057,9 @@ exports['Should correctly apply multiple uses of map and apply forEach'] = {
docs[i] = {'a':i, createdAt:new Date(d)};
}

var db = configuration.newDbInstance(configuration.writeConcernMax(), {poolSize:1});
db.open(function(err, db) {
var client = configuration.newDbInstance(configuration.writeConcernMax(), {poolSize:1});
client.connect(function(err, client) {
var db = client.db(configuration.database);
test.equal(null, err);

var collection = db.collection('map_mapmapforEach');
Expand Down Expand Up @@ -3384,7 +3385,7 @@ exports['Correcly decorate the cursor count command with skip, limit, hint, read
.hint({project:1}).count(true, function(err, r) {
test.equal(null, err);
test.equal(1, started.length);
if(started[0].command.readConcern)
if(started[0].command.readConcern)
test.deepEqual({level: 'local'}, started[0].command.readConcern);
test.deepEqual({ project: 1 }, started[0].command.hint);
test.equal(5, started[0].command.skip);
Expand Down Expand Up @@ -3427,7 +3428,7 @@ exports['Correcly decorate the collection cursor count command with skip, limit,
}, function(err, r) {
test.equal(null, err);
test.equal(1, started.length);
if(started[0].command.readConcern)
if(started[0].command.readConcern)
test.deepEqual({level: 'local'}, started[0].command.readConcern);
test.deepEqual({ project: 1 }, started[0].command.hint);
test.equal(5, started[0].command.skip);
Expand Down
2 changes: 1 addition & 1 deletion test/functional/mongo_client_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -587,7 +587,7 @@ exports['Should correctly pass through appname in options'] = {
// console.dir(url)
MongoClient.connect(url, {appname: 'hello world'}, function(err, db) {
test.equal(null, err);
test.equal('hello world', db.serverConfig.clientInfo.application.name);
test.equal('hello world', db.topology.clientInfo.application.name);

db.close();
test.done();
Expand Down

0 comments on commit 3a5232a

Please sign in to comment.