From 3a5232aa565768b930032f5724f5dbf70c8dd92e Mon Sep 17 00:00:00 2001 From: Matt Broadstone Date: Tue, 8 Aug 2017 18:12:43 -0400 Subject: [PATCH] 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. --- lib/mongo_client.js | 5 +++ test/functional/bulk_tests.js | 46 +++++++++++++++------------ test/functional/connection_tests.js | 13 ++++---- test/functional/cursor_tests.js | 11 ++++--- test/functional/mongo_client_tests.js | 2 +- 5 files changed, 44 insertions(+), 33 deletions(-) diff --git a/lib/mongo_client.js b/lib/mongo_client.js index 76f6ac61f6..4d528bf807 100644 --- a/lib/mongo_client.js +++ b/lib/mongo_client.js @@ -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) { diff --git a/test/functional/bulk_tests.js b/test/functional/bulk_tests.js index 24a1f7817f..d023e4ba89 100644 --- a/test/functional/bulk_tests.js +++ b/test/functional/bulk_tests.js @@ -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'); @@ -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(); }); }); @@ -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'); @@ -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(); }); }); @@ -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(); }); }); @@ -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(); }); }); @@ -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! }) @@ -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(); }); } diff --git a/test/functional/connection_tests.js b/test/functional/connection_tests.js index fa33f83241..ccf46b9180 100644 --- a/test/functional/connection_tests.js +++ b/test/functional/connection_tests.js @@ -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); @@ -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) { @@ -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(); })); } diff --git a/test/functional/cursor_tests.js b/test/functional/cursor_tests.js index 728abaae74..c117f97cef 100644 --- a/test/functional/cursor_tests.js +++ b/test/functional/cursor_tests.js @@ -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(); }); }) @@ -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'); @@ -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); @@ -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); diff --git a/test/functional/mongo_client_tests.js b/test/functional/mongo_client_tests.js index 16c7c3abff..52ca4aad10 100644 --- a/test/functional/mongo_client_tests.js +++ b/test/functional/mongo_client_tests.js @@ -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();