From a410b0b1a3077158011a89a47149d19c2ea232cf Mon Sep 17 00:00:00 2001 From: emadum Date: Sat, 2 May 2020 19:20:02 -0400 Subject: [PATCH 1/4] reduce duplication in write concern tests --- test/functional/command_write_concern.test.js | 1211 +++-------------- 1 file changed, 203 insertions(+), 1008 deletions(-) diff --git a/test/functional/command_write_concern.test.js b/test/functional/command_write_concern.test.js index 8a614a4f0cd..961a97b05ec 100644 --- a/test/functional/command_write_concern.test.js +++ b/test/functional/command_write_concern.test.js @@ -17,32 +17,22 @@ var extend = function(template, fields) { return object; }; -describe('Command Write Concern', function() { - afterEach(() => mock.cleanup()); - - it('successfully pass through writeConcern to aggregate command', { - metadata: { - requires: { - generators: true, - topology: 'single' - } - }, - - test: function(done) { - var configuration = this.configuration, - ObjectId = configuration.require.ObjectId; - - var electionIds = [new ObjectId(), new ObjectId()]; - var defaultFields = Object.assign({}, mock.DEFAULT_ISMASTER, { - setName: 'rs', - setVersion: 1, - electionId: electionIds[0], - hosts: ['localhost:32000', 'localhost:32001', 'localhost:32002'], - arbiters: ['localhost:32002'] - }); - - // Primary server states - var primary = [ +class WriteConcernTest { + constructor(configuration) { + this.configuration = configuration; + this.handlers = {}; + this.responseDecoration = {}; + const ObjectId = configuration.require.ObjectId; + const electionIds = [new ObjectId(), new ObjectId()]; + const defaultFields = Object.assign({}, mock.DEFAULT_ISMASTER, { + setName: 'rs', + setVersion: 1, + electionId: electionIds[0], + hosts: ['localhost:32000', 'localhost:32001', 'localhost:32002'], + arbiters: ['localhost:32002'] + }); + this.serverStates = { + primary: [ extend(defaultFields, { ismaster: true, secondary: false, @@ -50,10 +40,8 @@ describe('Command Write Concern', function() { primary: 'localhost:32000', tags: { loc: 'ny' } }) - ]; - - // Primary server states - var firstSecondary = [ + ], + firstSecondary: [ extend(defaultFields, { ismaster: false, secondary: true, @@ -61,10 +49,8 @@ describe('Command Write Concern', function() { primary: 'localhost:32000', tags: { loc: 'sf' } }) - ]; - - // Primary server states - var arbiter = [ + ], + arbiter: [ extend(defaultFields, { ismaster: false, secondary: false, @@ -72,65 +58,93 @@ describe('Command Write Concern', function() { me: 'localhost:32002', primary: 'localhost:32000' }) - ]; + ] + }; + } + setHandler(docKey, handler) { + this.docKey = docKey; + this.handler = handler; + } + decorateResponse(obj) { + Object.assign(this.responseDecoration, obj); + } + run(resultKey, testFn) { + const self = this; + co(function*() { + let primaryServer = yield mock.createServer(32000, 'localhost'); + let firstSecondaryServer = yield mock.createServer(32001, 'localhost'); + let arbiterServer = yield mock.createServer(32002, 'localhost'); + + primaryServer.setMessageHandler(request => { + var doc = request.document; + if (doc.ismaster) { + request.reply(self.serverStates.primary[0]); + } else if (self.docKey && doc[self.docKey]) { + this.handler(doc); + } else if (doc[resultKey]) { + self.commandResult = doc; + request.reply(Object.assign({ ok: 1 }, self.responseDecoration)); + } else if (doc.endSessions) { + request.reply({ ok: 1 }); + } + }); - // Boot the mock - co(function*() { - let primaryServer = yield mock.createServer(32000, 'localhost'); - let firstSecondaryServer = yield mock.createServer(32001, 'localhost'); - let arbiterServer = yield mock.createServer(32002, 'localhost'); + firstSecondaryServer.setMessageHandler(request => { + var doc = request.document; + if (doc.ismaster) { + request.reply(self.serverStates.firstSecondary[0]); + } else if (doc.endSessions) { + request.reply({ ok: 1 }); + } + }); - primaryServer.setMessageHandler(request => { - var doc = request.document; - if (doc.ismaster) { - request.reply(primary[0]); - } else if (doc.aggregate) { - commandResult = doc; - request.reply({ ok: 1 }); - } else if (doc.endSessions) { - request.reply({ ok: 1 }); - } - }); + arbiterServer.setMessageHandler(request => { + var doc = request.document; + if (doc.ismaster) { + request.reply(self.serverStates.arbiter[0]); + } else if (doc.endSessions) { + request.reply({ ok: 1 }); + } + }); - firstSecondaryServer.setMessageHandler(request => { - var doc = request.document; - if (doc.ismaster) { - request.reply(firstSecondary[0]); - } else if (doc.endSessions) { - request.reply({ ok: 1 }); - } - }); + const client = self.configuration.newClient( + 'mongodb://localhost:32000,localhost:32001,localhost:32002/test?replicaSet=rs' + ); - arbiterServer.setMessageHandler(request => { - var doc = request.document; - if (doc.ismaster) { - request.reply(arbiter[0]); - } else if (doc.endSessions) { - request.reply({ ok: 1 }); - } - }); + client.connect(function(err, client) { + test.equal(null, err); + var db = client.db(self.configuration.db); + testFn(client, db); + }); + }); + } +} - var commandResult = null; - const client = configuration.newClient( - 'mongodb://localhost:32000,localhost:32001,localhost:32002/test?replicaSet=rs' - ); +describe('Command Write Concern', function() { + afterEach(() => mock.cleanup()); - client.connect(function(err, client) { - test.equal(null, err); - var db = client.db(configuration.db); + it('successfully pass through writeConcern to aggregate command', { + metadata: { + requires: { + generators: true, + topology: 'single' + } + }, - db.collection('test') - .aggregate([{ $match: {} }, { $out: 'readConcernCollectionAggregate1Output' }], { - w: 2, - wtimeout: 1000 - }) - .toArray(function(err) { - test.equal(null, err); - test.deepEqual({ w: 2, wtimeout: 1000 }, commandResult.writeConcern); + test: function(done) { + const t = new WriteConcernTest(this.configuration); + t.run('aggregate', (client, db) => { + db.collection('test') + .aggregate([{ $match: {} }, { $out: 'readConcernCollectionAggregate1Output' }], { + w: 2, + wtimeout: 1000 + }) + .toArray(function(err) { + test.equal(null, err); + test.deepEqual({ w: 2, wtimeout: 1000 }, t.commandResult.writeConcern); - client.close(done); - }); - }); + client.close(done); + }); }); } }); @@ -144,115 +158,24 @@ describe('Command Write Concern', function() { }, test: function(done) { - var configuration = this.configuration, - ObjectId = configuration.require.ObjectId, - Long = configuration.require.Long; - - var electionIds = [new ObjectId(), new ObjectId()]; - var defaultFields = Object.assign({}, mock.DEFAULT_ISMASTER, { - setName: 'rs', - setVersion: 1, - electionId: electionIds[0], - hosts: ['localhost:32000', 'localhost:32001', 'localhost:32002'], - arbiters: ['localhost:32002'] - }); - - // Primary server states - var primary = [ - extend(defaultFields, { - ismaster: true, - secondary: false, - me: 'localhost:32000', - primary: 'localhost:32000', - tags: { loc: 'ny' } - }) - ]; - - // Primary server states - var firstSecondary = [ - extend(defaultFields, { - ismaster: false, - secondary: true, - me: 'localhost:32001', - primary: 'localhost:32000', - tags: { loc: 'sf' } - }) - ]; - - // Primary server states - var arbiter = [ - extend(defaultFields, { - ismaster: false, - secondary: false, - arbiterOnly: true, - me: 'localhost:32002', - primary: 'localhost:32000' - }) - ]; - - // Boot the mock - co(function*() { - const primaryServer = yield mock.createServer(32000, 'localhost'); - const firstSecondaryServer = yield mock.createServer(32001, 'localhost'); - const arbiterServer = yield mock.createServer(32002, 'localhost'); - - primaryServer.setMessageHandler(request => { - var doc = request.document; - - if (doc.ismaster) { - request.reply(primary[0]); - } else if (doc.listCollections) { - request.reply({ - ok: 1, - cursor: { - id: Long.fromNumber(0), - ns: 'test.cmd$.listCollections', - firstBatch: [] - } - }); - } else if (doc.create) { - commandResult = doc; - request.reply({ ok: 1 }); - } else if (doc.endSessions) { - request.reply({ ok: 1 }); + const Long = this.configuration.require.Long; + const t = new WriteConcernTest(this.configuration); + t.setHandler('listCollections', request => + request.reply({ + ok: 1, + cursor: { + id: Long.fromNumber(0), + ns: 'test.cmd$.listCollections', + firstBatch: [] } - }); - - firstSecondaryServer.setMessageHandler(request => { - var doc = request.document; - if (doc.ismaster) { - request.reply(firstSecondary[0]); - } else if (doc.endSessions) { - request.reply({ ok: 1 }); - } - }); - - arbiterServer.setMessageHandler(request => { - var doc = request.document; - if (doc.ismaster) { - request.reply(arbiter[0]); - } else if (doc.endSessions) { - request.reply({ ok: 1 }); - } - }); - - var commandResult = null; - - // Connect to the mocks - const client = configuration.newClient( - 'mongodb://localhost:32000,localhost:32001,localhost:32002/test?replicaSet=rs' - ); - - client.connect(function(err, client) { + }) + ); + t.run('create', (client, db) => { + db.createCollection('test_collection_methods', { w: 2, wtimeout: 1000 }, function(err) { test.equal(null, err); - var db = client.db(configuration.db); - - db.createCollection('test_collection_methods', { w: 2, wtimeout: 1000 }, function(err) { - test.equal(null, err); - test.deepEqual({ w: 2, wtimeout: 1000 }, commandResult.writeConcern); + test.deepEqual({ w: 2, wtimeout: 1000 }, t.commandResult.writeConcern); - client.close(done); - }); + client.close(done); }); }); } @@ -267,114 +190,22 @@ describe('Command Write Concern', function() { }, test: function(done) { - var configuration = this.configuration, - ObjectId = configuration.require.ObjectId; - - var electionIds = [new ObjectId(), new ObjectId()]; - var defaultFields = Object.assign({}, mock.DEFAULT_ISMASTER, { - setName: 'rs', - setVersion: 1, - electionId: electionIds[0], - hosts: ['localhost:32000', 'localhost:32001', 'localhost:32002'], - arbiters: ['localhost:32002'] - }); - - // Primary server states - var primary = [ - extend(defaultFields, { - ismaster: true, - secondary: false, - me: 'localhost:32000', - primary: 'localhost:32000', - tags: { loc: 'ny' } - }) - ]; - - // Primary server states - var firstSecondary = [ - extend(defaultFields, { - ismaster: false, - secondary: true, - me: 'localhost:32001', - primary: 'localhost:32000', - tags: { loc: 'sf' } - }) - ]; - - // Primary server states - var arbiter = [ - extend(defaultFields, { - ismaster: false, - secondary: false, - arbiterOnly: true, - me: 'localhost:32002', - primary: 'localhost:32000' - }) - ]; - - // Boot the mock - co(function*() { - const primaryServer = yield mock.createServer(32000, 'localhost'); - const firstSecondaryServer = yield mock.createServer(32001, 'localhost'); - const arbiterServer = yield mock.createServer(32002, 'localhost'); - - primaryServer.setMessageHandler(request => { - var doc = request.document; - - if (doc.ismaster) { - request.reply(primary[0]); - } else if (doc.createIndexes) { - commandResult = doc; - request.reply({ ok: 1 }); - } else if (doc.endSessions) { - request.reply({ ok: 1 }); - } - }); - - firstSecondaryServer.setMessageHandler(request => { - var doc = request.document; - if (doc.ismaster) { - request.reply(firstSecondary[0]); - } else if (doc.endSessions) { - request.reply({ ok: 1 }); - } - }); + const t = new WriteConcernTest(this.configuration); + t.run('createIndexes', (client, db) => { + db.collection('indexOptionDefault').createIndex( + { a: 1 }, + { + indexOptionDefaults: true, + w: 2, + wtimeout: 1000 + }, + function(err) { + test.equal(null, err); + test.deepEqual({ w: 2, wtimeout: 1000 }, t.commandResult.writeConcern); - arbiterServer.setMessageHandler(request => { - var doc = request.document; - if (doc.ismaster) { - request.reply(arbiter[0]); - } else if (doc.endSessions) { - request.reply({ ok: 1 }); + client.close(done); } - }); - - var commandResult = null; - - // Connect to the mocks - const client = configuration.newClient( - 'mongodb://localhost:32000,localhost:32001,localhost:32002/test?replicaSet=rs' ); - - client.connect(function(err, client) { - test.equal(null, err); - var db = client.db(configuration.db); - - db.collection('indexOptionDefault').createIndex( - { a: 1 }, - { - indexOptionDefaults: true, - w: 2, - wtimeout: 1000 - }, - function(err) { - test.equal(null, err); - test.deepEqual({ w: 2, wtimeout: 1000 }, commandResult.writeConcern); - - client.close(done); - } - ); - }); }); } }); @@ -388,111 +219,20 @@ describe('Command Write Concern', function() { }, test: function(done) { - var configuration = this.configuration, - ObjectId = configuration.require.ObjectId; - - var electionIds = [new ObjectId(), new ObjectId()]; - var defaultFields = Object.assign({}, mock.DEFAULT_ISMASTER, { - setName: 'rs', - setVersion: 1, - electionId: electionIds[0], - hosts: ['localhost:32000', 'localhost:32001', 'localhost:32002'], - arbiters: ['localhost:32002'] - }); - - // Primary server states - var primary = [ - extend(defaultFields, { - ismaster: true, - secondary: false, - me: 'localhost:32000', - primary: 'localhost:32000', - tags: { loc: 'ny' } - }) - ]; - - // Primary server states - var firstSecondary = [ - extend(defaultFields, { - ismaster: false, - secondary: true, - me: 'localhost:32001', - primary: 'localhost:32000', - tags: { loc: 'sf' } - }) - ]; - - // Primary server states - var arbiter = [ - extend(defaultFields, { - ismaster: false, - secondary: false, - arbiterOnly: true, - me: 'localhost:32002', - primary: 'localhost:32000' - }) - ]; - - // Boot the mock - co(function*() { - const primaryServer = yield mock.createServer(32000, 'localhost'); - const firstSecondaryServer = yield mock.createServer(32001, 'localhost'); - const arbiterServer = yield mock.createServer(32002, 'localhost'); - - primaryServer.setMessageHandler(request => { - var doc = request.document; - if (doc.ismaster) { - request.reply(primary[0]); - } else if (doc.drop) { - commandResult = doc; - request.reply({ ok: 1 }); - } else if (doc.endSessions) { - request.reply({ ok: 1 }); - } - }); - - firstSecondaryServer.setMessageHandler(request => { - var doc = request.document; - if (doc.ismaster) { - request.reply(firstSecondary[0]); - } else if (doc.endSessions) { - request.reply({ ok: 1 }); - } - }); + const t = new WriteConcernTest(this.configuration); + t.run('drop', (client, db) => { + db.collection('indexOptionDefault').drop( + { + w: 2, + wtimeout: 1000 + }, + function(err) { + test.equal(null, err); + test.deepEqual({ w: 2, wtimeout: 1000 }, t.commandResult.writeConcern); - arbiterServer.setMessageHandler(request => { - var doc = request.document; - if (doc.ismaster) { - request.reply(arbiter[0]); - } else if (doc.endSessions) { - request.reply({ ok: 1 }); + client.close(done); } - }); - - var commandResult = null; - - // Connect to the mocks - const client = configuration.newClient( - 'mongodb://localhost:32000,localhost:32001,localhost:32002/test?replicaSet=rs' ); - - client.connect(function(err, client) { - test.equal(null, err); - var db = client.db(configuration.db); - - db.collection('indexOptionDefault').drop( - { - w: 2, - wtimeout: 1000 - }, - function(err) { - test.equal(null, err); - test.deepEqual({ w: 2, wtimeout: 1000 }, commandResult.writeConcern); - - client.close(done); - } - ); - }); }); } }); @@ -506,111 +246,20 @@ describe('Command Write Concern', function() { }, test: function(done) { - var configuration = this.configuration, - ObjectId = configuration.require.ObjectId; - - var electionIds = [new ObjectId(), new ObjectId()]; - var defaultFields = Object.assign({}, mock.DEFAULT_ISMASTER, { - setName: 'rs', - setVersion: 1, - electionId: electionIds[0], - hosts: ['localhost:32000', 'localhost:32001', 'localhost:32002'], - arbiters: ['localhost:32002'] - }); - - // Primary server states - var primary = [ - extend(defaultFields, { - ismaster: true, - secondary: false, - me: 'localhost:32000', - primary: 'localhost:32000', - tags: { loc: 'ny' } - }) - ]; - - // Primary server states - var firstSecondary = [ - extend(defaultFields, { - ismaster: false, - secondary: true, - me: 'localhost:32001', - primary: 'localhost:32000', - tags: { loc: 'sf' } - }) - ]; - - // Primary server states - var arbiter = [ - extend(defaultFields, { - ismaster: false, - secondary: false, - arbiterOnly: true, - me: 'localhost:32002', - primary: 'localhost:32000' - }) - ]; - - // Boot the mock - co(function*() { - const primaryServer = yield mock.createServer(32000, 'localhost'); - const firstSecondaryServer = yield mock.createServer(32001, 'localhost'); - const arbiterServer = yield mock.createServer(32002, 'localhost'); - - primaryServer.setMessageHandler(request => { - var doc = request.document; - if (doc.ismaster) { - request.reply(primary[0]); - } else if (doc.dropDatabase) { - commandResult = doc; - request.reply({ ok: 1 }); - } else if (doc.endSessions) { - request.reply({ ok: 1 }); - } - }); - - firstSecondaryServer.setMessageHandler(request => { - var doc = request.document; - if (doc.ismaster) { - request.reply(firstSecondary[0]); - } else if (doc.endSessions) { - request.reply({ ok: 1 }); - } - }); + const t = new WriteConcernTest(this.configuration); + t.run('dropDatabase', (client, db) => { + db.dropDatabase( + { + w: 2, + wtimeout: 1000 + }, + function(err) { + test.equal(null, err); + test.deepEqual({ w: 2, wtimeout: 1000 }, t.commandResult.writeConcern); - arbiterServer.setMessageHandler(request => { - var doc = request.document; - if (doc.ismaster) { - request.reply(arbiter[0]); - } else if (doc.endSessions) { - request.reply({ ok: 1 }); + client.close(done); } - }); - - var commandResult = null; - - // Connect to the mocks - const client = configuration.newClient( - 'mongodb://localhost:32000,localhost:32001,localhost:32002/test?replicaSet=rs' ); - - client.connect(function(err, client) { - test.equal(null, err); - var db = client.db(configuration.db); - - db.dropDatabase( - { - w: 2, - wtimeout: 1000 - }, - function(err) { - test.equal(null, err); - test.deepEqual({ w: 2, wtimeout: 1000 }, commandResult.writeConcern); - - client.close(done); - } - ); - }); }); } }); @@ -624,111 +273,20 @@ describe('Command Write Concern', function() { }, test: function(done) { - var configuration = this.configuration, - ObjectId = configuration.require.ObjectId; - - var electionIds = [new ObjectId(), new ObjectId()]; - var defaultFields = Object.assign({}, mock.DEFAULT_ISMASTER, { - setName: 'rs', - setVersion: 1, - electionId: electionIds[0], - hosts: ['localhost:32000', 'localhost:32001', 'localhost:32002'], - arbiters: ['localhost:32002'] - }); - - // Primary server states - var primary = [ - extend(defaultFields, { - ismaster: true, - secondary: false, - me: 'localhost:32000', - primary: 'localhost:32000', - tags: { loc: 'ny' } - }) - ]; - - // Primary server states - var firstSecondary = [ - extend(defaultFields, { - ismaster: false, - secondary: true, - me: 'localhost:32001', - primary: 'localhost:32000', - tags: { loc: 'sf' } - }) - ]; - - // Primary server states - var arbiter = [ - extend(defaultFields, { - ismaster: false, - secondary: false, - arbiterOnly: true, - me: 'localhost:32002', - primary: 'localhost:32000' - }) - ]; - - // Boot the mock - co(function*() { - const primaryServer = yield mock.createServer(32000, 'localhost'); - const firstSecondaryServer = yield mock.createServer(32001, 'localhost'); - const arbiterServer = yield mock.createServer(32002, 'localhost'); - - primaryServer.setMessageHandler(request => { - var doc = request.document; - if (doc.ismaster) { - request.reply(primary[0]); - } else if (doc.dropIndexes) { - commandResult = doc; - request.reply({ ok: 1 }); - } else if (doc.endSessions) { - request.reply({ ok: 1 }); - } - }); - - firstSecondaryServer.setMessageHandler(request => { - var doc = request.document; - if (doc.ismaster) { - request.reply(firstSecondary[0]); - } else if (doc.endSessions) { - request.reply({ ok: 1 }); - } - }); + const t = new WriteConcernTest(this.configuration); + t.run('dropIndexes', (client, db) => { + db.collection('test').dropIndexes( + { + w: 2, + wtimeout: 1000 + }, + function(err) { + test.equal(null, err); + test.deepEqual({ w: 2, wtimeout: 1000 }, t.commandResult.writeConcern); - arbiterServer.setMessageHandler(request => { - var doc = request.document; - if (doc.ismaster) { - request.reply(arbiter[0]); - } else if (doc.endSessions) { - request.reply({ ok: 1 }); + client.close(done); } - }); - - var commandResult = null; - - // Connect to the mocks - const client = configuration.newClient( - 'mongodb://localhost:32000,localhost:32001,localhost:32002/test?replicaSet=rs' ); - - client.connect(function(err, client) { - test.equal(null, err); - var db = client.db(configuration.db); - - db.collection('test').dropIndexes( - { - w: 2, - wtimeout: 1000 - }, - function(err) { - test.equal(null, err); - test.deepEqual({ w: 2, wtimeout: 1000 }, commandResult.writeConcern); - - client.close(done); - } - ); - }); }); } }); @@ -742,120 +300,30 @@ describe('Command Write Concern', function() { }, test: function(done) { - var configuration = this.configuration, - ObjectId = configuration.require.ObjectId, - Code = configuration.require.Code; - - var electionIds = [new ObjectId(), new ObjectId()]; - var defaultFields = Object.assign({}, mock.DEFAULT_ISMASTER, { - setName: 'rs', - setVersion: 1, - electionId: electionIds[0], - hosts: ['localhost:32000', 'localhost:32001', 'localhost:32002'], - arbiters: ['localhost:32002'] - }); - - // Primary server states - var primary = [ - extend(defaultFields, { - ismaster: true, - secondary: false, - me: 'localhost:32000', - primary: 'localhost:32000', - tags: { loc: 'ny' } - }) - ]; - - // Primary server states - var firstSecondary = [ - extend(defaultFields, { - ismaster: false, - secondary: true, - me: 'localhost:32001', - primary: 'localhost:32000', - tags: { loc: 'sf' } - }) - ]; - - // Primary server states - var arbiter = [ - extend(defaultFields, { - ismaster: false, - secondary: false, - arbiterOnly: true, - me: 'localhost:32002', - primary: 'localhost:32000' - }) - ]; - - // Boot the mock - co(function*() { - const primaryServer = yield mock.createServer(32000, 'localhost'); - const firstSecondaryServer = yield mock.createServer(32001, 'localhost'); - const arbiterServer = yield mock.createServer(32002, 'localhost'); - - primaryServer.setMessageHandler(request => { - var doc = request.document; - if (doc.ismaster) { - request.reply(primary[0]); - } else if (doc.mapReduce) { - commandResult = doc; - request.reply({ ok: 1, result: 'tempCollection' }); - } else if (doc.endSessions) { - request.reply({ ok: 1 }); - } - }); - - firstSecondaryServer.setMessageHandler(request => { - var doc = request.document; - if (doc.ismaster) { - request.reply(firstSecondary[0]); - } else if (doc.endSessions) { - request.reply({ ok: 1 }); - } - }); + const Code = this.configuration.require.Code; + const t = new WriteConcernTest(this.configuration); + t.decorateResponse({ result: 'tempCollection' }); + t.run('mapReduce', (client, db) => { + // String functions + var map = new Code('function() { emit(this.user_id, 1); }'); + var reduce = new Code('function(k,vals) { return 1; }'); + + // db.collection('test').mapReduce({ + db.collection('test').mapReduce( + map, + reduce, + { + out: { replace: 'tempCollection' }, + w: 2, + wtimeout: 1000 + }, + function(err) { + test.equal(null, err); + test.deepEqual({ w: 2, wtimeout: 1000 }, t.commandResult.writeConcern); - arbiterServer.setMessageHandler(request => { - var doc = request.document; - if (doc.ismaster) { - request.reply(arbiter[0]); - } else if (doc.endSessions) { - request.reply({ ok: 1 }); + client.close(done); } - }); - - var commandResult = null; - - // Connect to the mocks - const client = configuration.newClient( - 'mongodb://localhost:32000,localhost:32001,localhost:32002/test?replicaSet=rs' ); - - client.connect(function(err, client) { - test.equal(null, err); - var db = client.db(configuration.db); - - // String functions - var map = new Code('function() { emit(this.user_id, 1); }'); - var reduce = new Code('function(k,vals) { return 1; }'); - - // db.collection('test').mapReduce({ - db.collection('test').mapReduce( - map, - reduce, - { - out: { replace: 'tempCollection' }, - w: 2, - wtimeout: 1000 - }, - function(err) { - test.equal(null, err); - test.deepEqual({ w: 2, wtimeout: 1000 }, commandResult.writeConcern); - - client.close(done); - } - ); - }); }); } }); @@ -869,104 +337,13 @@ describe('Command Write Concern', function() { }, test: function(done) { - var configuration = this.configuration, - ObjectId = configuration.require.ObjectId; - - var electionIds = [new ObjectId(), new ObjectId()]; - var defaultFields = Object.assign({}, mock.DEFAULT_ISMASTER, { - setName: 'rs', - setVersion: 1, - electionId: electionIds[0], - hosts: ['localhost:32000', 'localhost:32001', 'localhost:32002'], - arbiters: ['localhost:32002'] - }); - - // Primary server states - var primary = [ - extend(defaultFields, { - ismaster: true, - secondary: false, - me: 'localhost:32000', - primary: 'localhost:32000', - tags: { loc: 'ny' } - }) - ]; - - // Primary server states - var firstSecondary = [ - extend(defaultFields, { - ismaster: false, - secondary: true, - me: 'localhost:32001', - primary: 'localhost:32000', - tags: { loc: 'sf' } - }) - ]; - - // Primary server states - var arbiter = [ - extend(defaultFields, { - ismaster: false, - secondary: false, - arbiterOnly: true, - me: 'localhost:32002', - primary: 'localhost:32000' - }) - ]; - - // Boot the mock - co(function*() { - const primaryServer = yield mock.createServer(32000, 'localhost'); - const firstSecondaryServer = yield mock.createServer(32001, 'localhost'); - const arbiterServer = yield mock.createServer(32002, 'localhost'); - - primaryServer.setMessageHandler(request => { - var doc = request.document; - if (doc.ismaster) { - request.reply(primary[0]); - } else if (doc.createUser) { - commandResult = doc; - request.reply({ ok: 1 }); - } else if (doc.endSessions) { - request.reply({ ok: 1 }); - } - }); - - firstSecondaryServer.setMessageHandler(request => { - var doc = request.document; - if (doc.ismaster) { - request.reply(firstSecondary[0]); - } else if (doc.endSessions) { - request.reply({ ok: 1 }); - } - }); - - arbiterServer.setMessageHandler(request => { - var doc = request.document; - if (doc.ismaster) { - request.reply(arbiter[0]); - } else if (doc.endSessions) { - request.reply({ ok: 1 }); - } - }); - - var commandResult = null; - - // Connect to the mocks - const client = configuration.newClient( - 'mongodb://localhost:32000,localhost:32001,localhost:32002/test?replicaSet=rs' - ); - - client.connect(function(err, client) { + const t = new WriteConcernTest(this.configuration); + t.run('createUser', (client, db) => { + db.admin().addUser('kay:kay', 'abc123', { w: 2, wtimeout: 1000 }, function(err) { test.equal(null, err); - var db = client.db(configuration.db); - - db.admin().addUser('kay:kay', 'abc123', { w: 2, wtimeout: 1000 }, function(err) { - test.equal(null, err); - test.deepEqual({ w: 2, wtimeout: 1000 }, commandResult.writeConcern); + test.deepEqual({ w: 2, wtimeout: 1000 }, t.commandResult.writeConcern); - client.close(done); - }); + client.close(done); }); }); } @@ -981,104 +358,13 @@ describe('Command Write Concern', function() { }, test: function(done) { - var configuration = this.configuration, - ObjectId = configuration.require.ObjectId; - - var electionIds = [new ObjectId(), new ObjectId()]; - var defaultFields = Object.assign({}, mock.DEFAULT_ISMASTER, { - setName: 'rs', - setVersion: 1, - electionId: electionIds[0], - hosts: ['localhost:32000', 'localhost:32001', 'localhost:32002'], - arbiters: ['localhost:32002'] - }); - - // Primary server states - var primary = [ - extend(defaultFields, { - ismaster: true, - secondary: false, - me: 'localhost:32000', - primary: 'localhost:32000', - tags: { loc: 'ny' } - }) - ]; - - // Primary server states - var firstSecondary = [ - extend(defaultFields, { - ismaster: false, - secondary: true, - me: 'localhost:32001', - primary: 'localhost:32000', - tags: { loc: 'sf' } - }) - ]; - - // Primary server states - var arbiter = [ - extend(defaultFields, { - ismaster: false, - secondary: false, - arbiterOnly: true, - me: 'localhost:32002', - primary: 'localhost:32000' - }) - ]; - - // Boot the mock - co(function*() { - const primaryServer = yield mock.createServer(32000, 'localhost'); - const firstSecondaryServer = yield mock.createServer(32001, 'localhost'); - const arbiterServer = yield mock.createServer(32002, 'localhost'); - - primaryServer.setMessageHandler(request => { - var doc = request.document; - if (doc.ismaster) { - request.reply(primary[0]); - } else if (doc.dropUser) { - commandResult = doc; - request.reply({ ok: 1 }); - } else if (doc.endSessions) { - request.reply({ ok: 1 }); - } - }); - - firstSecondaryServer.setMessageHandler(request => { - var doc = request.document; - if (doc.ismaster) { - request.reply(firstSecondary[0]); - } else if (doc.endSessions) { - request.reply({ ok: 1 }); - } - }); - - arbiterServer.setMessageHandler(request => { - var doc = request.document; - if (doc.ismaster) { - request.reply(arbiter[0]); - } else if (doc.endSessions) { - request.reply({ ok: 1 }); - } - }); - - var commandResult = null; - - // Connect to the mocks - const client = configuration.newClient( - 'mongodb://localhost:32000,localhost:32001,localhost:32002/test?replicaSet=rs' - ); - - client.connect(function(err, client) { + const t = new WriteConcernTest(this.configuration); + t.run('dropUser', (client, db) => { + db.admin().removeUser('kay:kay', { w: 2, wtimeout: 1000 }, function(err) { test.equal(null, err); - var db = client.db(configuration.db); - - db.admin().removeUser('kay:kay', { w: 2, wtimeout: 1000 }, function(err) { - test.equal(null, err); - test.deepEqual({ w: 2, wtimeout: 1000 }, commandResult.writeConcern); + test.deepEqual({ w: 2, wtimeout: 1000 }, t.commandResult.writeConcern); - client.close(done); - }); + client.close(done); }); }); } @@ -1093,112 +379,21 @@ describe('Command Write Concern', function() { }, test: function(done) { - var configuration = this.configuration, - ObjectId = configuration.require.ObjectId; - - var electionIds = [new ObjectId(), new ObjectId()]; - var defaultFields = Object.assign({}, mock.DEFAULT_ISMASTER, { - setName: 'rs', - setVersion: 1, - electionId: electionIds[0], - hosts: ['localhost:32000', 'localhost:32001', 'localhost:32002'], - arbiters: ['localhost:32002'] - }); - - // Primary server states - var primary = [ - extend(defaultFields, { - ismaster: true, - secondary: false, - me: 'localhost:32000', - primary: 'localhost:32000', - tags: { loc: 'ny' } - }) - ]; - - // Primary server states - var firstSecondary = [ - extend(defaultFields, { - ismaster: false, - secondary: true, - me: 'localhost:32001', - primary: 'localhost:32000', - tags: { loc: 'sf' } - }) - ]; - - // Primary server states - var arbiter = [ - extend(defaultFields, { - ismaster: false, - secondary: false, - arbiterOnly: true, - me: 'localhost:32002', - primary: 'localhost:32000' - }) - ]; - - // Boot the mock - co(function*() { - const primaryServer = yield mock.createServer(32000, 'localhost'); - const firstSecondaryServer = yield mock.createServer(32001, 'localhost'); - const arbiterServer = yield mock.createServer(32002, 'localhost'); - - primaryServer.setMessageHandler(request => { - var doc = request.document; - if (doc.ismaster) { - request.reply(primary[0]); - } else if (doc.findAndModify) { - commandResult = doc; - request.reply({ ok: 1, result: {} }); - } else if (doc.endSessions) { - request.reply({ ok: 1 }); - } - }); - - firstSecondaryServer.setMessageHandler(request => { - var doc = request.document; - if (doc.ismaster) { - request.reply(firstSecondary[0]); - } else if (doc.endSessions) { - request.reply({ ok: 1 }); - } - }); + const t = new WriteConcernTest(this.configuration); + t.decorateResponse({ result: {} }); + t.run('findAndModify', (client, db) => { + db.collection('test').findAndModify( + { a: 1 }, + [['a', 1]], + { $set: { b1: 1 } }, + { new: true, w: 2, wtimeout: 1000 }, + function(err) { + test.equal(null, err); + test.deepEqual({ w: 2, wtimeout: 1000 }, t.commandResult.writeConcern); - arbiterServer.setMessageHandler(request => { - var doc = request.document; - if (doc.ismaster) { - request.reply(arbiter[0]); - } else if (doc.endSessions) { - request.reply({ ok: 1 }); + client.close(done); } - }); - - var commandResult = null; - - // Connect to the mocks - const client = configuration.newClient( - 'mongodb://localhost:32000,localhost:32001,localhost:32002/test?replicaSet=rs' ); - - client.connect(function(err, client) { - test.equal(null, err); - var db = client.db(configuration.db); - - // Simple findAndModify command returning the new document - db.collection('test').findAndModify( - { a: 1 }, - [['a', 1]], - { $set: { b1: 1 } }, - { new: true, w: 2, wtimeout: 1000 }, - function(err) { - test.equal(null, err); - test.deepEqual({ w: 2, wtimeout: 1000 }, commandResult.writeConcern); - - client.close(done); - } - ); - }); }); } }); From fef9b219bbfb8bedcabd2ee4a168d997687f441f Mon Sep 17 00:00:00 2001 From: emadum Date: Sat, 2 May 2020 19:41:19 -0400 Subject: [PATCH 2/4] cleanup --- test/functional/command_write_concern.test.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/test/functional/command_write_concern.test.js b/test/functional/command_write_concern.test.js index 961a97b05ec..5c32c89d03e 100644 --- a/test/functional/command_write_concern.test.js +++ b/test/functional/command_write_concern.test.js @@ -20,7 +20,6 @@ var extend = function(template, fields) { class WriteConcernTest { constructor(configuration) { this.configuration = configuration; - this.handlers = {}; this.responseDecoration = {}; const ObjectId = configuration.require.ObjectId; const electionIds = [new ObjectId(), new ObjectId()]; @@ -80,7 +79,7 @@ class WriteConcernTest { if (doc.ismaster) { request.reply(self.serverStates.primary[0]); } else if (self.docKey && doc[self.docKey]) { - this.handler(doc); + self.handler(doc); } else if (doc[resultKey]) { self.commandResult = doc; request.reply(Object.assign({ ok: 1 }, self.responseDecoration)); From e2077444d99cd0d8849191f0fd550eddece01140 Mon Sep 17 00:00:00 2001 From: emadum Date: Sun, 3 May 2020 17:38:38 -0400 Subject: [PATCH 3/4] refactor: create withStubbedClient and writeConcernTest helpers to prep for stubbed client refactor --- test/functional/command_write_concern.test.js | 258 ++++++------------ 1 file changed, 78 insertions(+), 180 deletions(-) diff --git a/test/functional/command_write_concern.test.js b/test/functional/command_write_concern.test.js index 5c32c89d03e..acbd05c00c0 100644 --- a/test/functional/command_write_concern.test.js +++ b/test/functional/command_write_concern.test.js @@ -60,10 +60,6 @@ class WriteConcernTest { ] }; } - setHandler(docKey, handler) { - this.docKey = docKey; - this.handler = handler; - } decorateResponse(obj) { Object.assign(this.responseDecoration, obj); } @@ -78,8 +74,6 @@ class WriteConcernTest { var doc = request.document; if (doc.ismaster) { request.reply(self.serverStates.primary[0]); - } else if (self.docKey && doc[self.docKey]) { - self.handler(doc); } else if (doc[resultKey]) { self.commandResult = doc; request.reply(Object.assign({ ok: 1 }, self.responseDecoration)); @@ -119,6 +113,32 @@ class WriteConcernTest { } } +function withStubbedClient(testFn) { + return function(done) { + const t = new WriteConcernTest(this.configuration); + testFn.call(this, t, done); + }; +} + +const writeConcernTestOptions = { w: 2, wtimeout: 1000 }; + +function writeConcernTest(command, testFn) { + return withStubbedClient(function(t, done) { + switch (command) { + case 'mapReduce': + t.decorateResponse({ result: 'tempCollection' }); + break; + } + t.run(command, (client, db) => + testFn.call(this, db, Object.assign({}, writeConcernTestOptions), err => { + test.equal(null, err); + test.deepEqual(writeConcernTestOptions, t.commandResult.writeConcern); + client.close(done); + }) + ); + }); +} + describe('Command Write Concern', function() { afterEach(() => mock.cleanup()); @@ -130,22 +150,14 @@ describe('Command Write Concern', function() { } }, - test: function(done) { - const t = new WriteConcernTest(this.configuration); - t.run('aggregate', (client, db) => { - db.collection('test') - .aggregate([{ $match: {} }, { $out: 'readConcernCollectionAggregate1Output' }], { - w: 2, - wtimeout: 1000 - }) - .toArray(function(err) { - test.equal(null, err); - test.deepEqual({ w: 2, wtimeout: 1000 }, t.commandResult.writeConcern); - - client.close(done); - }); - }); - } + test: writeConcernTest('aggregate', function(db, writeConcernTestOptions, done) { + db.collection('test') + .aggregate( + [{ $match: {} }, { $out: 'readConcernCollectionAggregate1Output' }], + writeConcernTestOptions + ) + .toArray(done); + }) }); it('successfully pass through writeConcern to create command', { @@ -156,28 +168,9 @@ describe('Command Write Concern', function() { } }, - test: function(done) { - const Long = this.configuration.require.Long; - const t = new WriteConcernTest(this.configuration); - t.setHandler('listCollections', request => - request.reply({ - ok: 1, - cursor: { - id: Long.fromNumber(0), - ns: 'test.cmd$.listCollections', - firstBatch: [] - } - }) - ); - t.run('create', (client, db) => { - db.createCollection('test_collection_methods', { w: 2, wtimeout: 1000 }, function(err) { - test.equal(null, err); - test.deepEqual({ w: 2, wtimeout: 1000 }, t.commandResult.writeConcern); - - client.close(done); - }); - }); - } + test: writeConcernTest('create', function(db, writeConcernTestOptions, done) { + db.createCollection('test_collection_methods', writeConcernTestOptions, done); + }) }); it('successfully pass through writeConcern to createIndexes command', { @@ -188,25 +181,13 @@ describe('Command Write Concern', function() { } }, - test: function(done) { - const t = new WriteConcernTest(this.configuration); - t.run('createIndexes', (client, db) => { - db.collection('indexOptionDefault').createIndex( - { a: 1 }, - { - indexOptionDefaults: true, - w: 2, - wtimeout: 1000 - }, - function(err) { - test.equal(null, err); - test.deepEqual({ w: 2, wtimeout: 1000 }, t.commandResult.writeConcern); - - client.close(done); - } - ); - }); - } + test: writeConcernTest('createIndexes', function(db, writeConcernTestOptions, done) { + db.collection('indexOptionDefault').createIndex( + { a: 1 }, + Object.assign({ indexOptionDefaults: true }, writeConcernTestOptions), + done + ); + }) }); it('successfully pass through writeConcern to drop command', { @@ -217,23 +198,9 @@ describe('Command Write Concern', function() { } }, - test: function(done) { - const t = new WriteConcernTest(this.configuration); - t.run('drop', (client, db) => { - db.collection('indexOptionDefault').drop( - { - w: 2, - wtimeout: 1000 - }, - function(err) { - test.equal(null, err); - test.deepEqual({ w: 2, wtimeout: 1000 }, t.commandResult.writeConcern); - - client.close(done); - } - ); - }); - } + test: writeConcernTest('drop', function(db, writeConcernTestOptions, done) { + db.collection('indexOptionDefault').drop(writeConcernTestOptions, done); + }) }); it('successfully pass through writeConcern to dropDatabase command', { @@ -244,23 +211,9 @@ describe('Command Write Concern', function() { } }, - test: function(done) { - const t = new WriteConcernTest(this.configuration); - t.run('dropDatabase', (client, db) => { - db.dropDatabase( - { - w: 2, - wtimeout: 1000 - }, - function(err) { - test.equal(null, err); - test.deepEqual({ w: 2, wtimeout: 1000 }, t.commandResult.writeConcern); - - client.close(done); - } - ); - }); - } + test: writeConcernTest('dropDatabase', function(db, writeConcernTestOptions, done) { + db.dropDatabase(writeConcernTestOptions, done); + }) }); it('successfully pass through writeConcern to dropIndexes command', { @@ -271,23 +224,9 @@ describe('Command Write Concern', function() { } }, - test: function(done) { - const t = new WriteConcernTest(this.configuration); - t.run('dropIndexes', (client, db) => { - db.collection('test').dropIndexes( - { - w: 2, - wtimeout: 1000 - }, - function(err) { - test.equal(null, err); - test.deepEqual({ w: 2, wtimeout: 1000 }, t.commandResult.writeConcern); - - client.close(done); - } - ); - }); - } + test: writeConcernTest('dropIndexes', function(db, writeConcernTestOptions, done) { + db.collection('test').dropIndexes(writeConcernTestOptions, done); + }) }); it('successfully pass through writeConcern to mapReduce command', { @@ -298,33 +237,17 @@ describe('Command Write Concern', function() { } }, - test: function(done) { + test: writeConcernTest('mapReduce', function(db, writeConcernTestOptions, done) { const Code = this.configuration.require.Code; - const t = new WriteConcernTest(this.configuration); - t.decorateResponse({ result: 'tempCollection' }); - t.run('mapReduce', (client, db) => { - // String functions - var map = new Code('function() { emit(this.user_id, 1); }'); - var reduce = new Code('function(k,vals) { return 1; }'); - - // db.collection('test').mapReduce({ - db.collection('test').mapReduce( - map, - reduce, - { - out: { replace: 'tempCollection' }, - w: 2, - wtimeout: 1000 - }, - function(err) { - test.equal(null, err); - test.deepEqual({ w: 2, wtimeout: 1000 }, t.commandResult.writeConcern); - - client.close(done); - } - ); - }); - } + const map = new Code('function() { emit(this.user_id, 1); }'); + const reduce = new Code('function(k,vals) { return 1; }'); + db.collection('test').mapReduce( + map, + reduce, + Object.assign({ out: { replace: 'tempCollection' } }, writeConcernTestOptions), + done + ); + }) }); it('successfully pass through writeConcern to createUser command', { @@ -335,17 +258,9 @@ describe('Command Write Concern', function() { } }, - test: function(done) { - const t = new WriteConcernTest(this.configuration); - t.run('createUser', (client, db) => { - db.admin().addUser('kay:kay', 'abc123', { w: 2, wtimeout: 1000 }, function(err) { - test.equal(null, err); - test.deepEqual({ w: 2, wtimeout: 1000 }, t.commandResult.writeConcern); - - client.close(done); - }); - }); - } + test: writeConcernTest('createUser', function(db, writeConcernTestOptions, done) { + db.admin().addUser('kay:kay', 'abc123', writeConcernTestOptions, done); + }) }); it('successfully pass through writeConcern to dropUser command', { @@ -356,17 +271,9 @@ describe('Command Write Concern', function() { } }, - test: function(done) { - const t = new WriteConcernTest(this.configuration); - t.run('dropUser', (client, db) => { - db.admin().removeUser('kay:kay', { w: 2, wtimeout: 1000 }, function(err) { - test.equal(null, err); - test.deepEqual({ w: 2, wtimeout: 1000 }, t.commandResult.writeConcern); - - client.close(done); - }); - }); - } + test: writeConcernTest('dropUser', function(db, writeConcernTestOptions, done) { + db.admin().removeUser('kay:kay', writeConcernTestOptions, done); + }) }); it('successfully pass through writeConcern to findAndModify command', { @@ -377,23 +284,14 @@ describe('Command Write Concern', function() { } }, - test: function(done) { - const t = new WriteConcernTest(this.configuration); - t.decorateResponse({ result: {} }); - t.run('findAndModify', (client, db) => { - db.collection('test').findAndModify( - { a: 1 }, - [['a', 1]], - { $set: { b1: 1 } }, - { new: true, w: 2, wtimeout: 1000 }, - function(err) { - test.equal(null, err); - test.deepEqual({ w: 2, wtimeout: 1000 }, t.commandResult.writeConcern); - - client.close(done); - } - ); - }); - } + test: writeConcernTest('findAndModify', function(db, writeConcernTestOptions, done) { + db.collection('test').findAndModify( + { a: 1 }, + [['a', 1]], + { $set: { b1: 1 } }, + Object.assign({ new: true }, writeConcernTestOptions), + done + ); + }) }); }); From ba4c8f67ebfbb6a3bff89e37f99b8d68377ab4ba Mon Sep 17 00:00:00 2001 From: emadum Date: Sat, 5 Dec 2020 19:10:42 -0500 Subject: [PATCH 4/4] more clean up, fix deprecation warnings --- test/functional/command_write_concern.test.js | 148 ++++-------------- 1 file changed, 32 insertions(+), 116 deletions(-) diff --git a/test/functional/command_write_concern.test.js b/test/functional/command_write_concern.test.js index acbd05c00c0..7f3f5797658 100644 --- a/test/functional/command_write_concern.test.js +++ b/test/functional/command_write_concern.test.js @@ -1,21 +1,9 @@ 'use strict'; -var test = require('./shared').assert; -var co = require('co'); -var mock = require('mongodb-mock-server'); +const co = require('co'); +const mock = require('mongodb-mock-server'); +const expect = require('chai').expect; -// Extend the object -var extend = function(template, fields) { - var object = {}; - for (var name in template) { - object[name] = template[name]; - } - - for (var fieldName in fields) { - object[fieldName] = fields[fieldName]; - } - - return object; -}; +const TEST_OPTIONS = { writeConcern: { w: 2, wtimeout: 1000 } }; class WriteConcernTest { constructor(configuration) { @@ -28,34 +16,30 @@ class WriteConcernTest { setVersion: 1, electionId: electionIds[0], hosts: ['localhost:32000', 'localhost:32001', 'localhost:32002'], + primary: 'localhost:32000', arbiters: ['localhost:32002'] }); this.serverStates = { primary: [ - extend(defaultFields, { + Object.assign({}, defaultFields, { ismaster: true, secondary: false, - me: 'localhost:32000', - primary: 'localhost:32000', - tags: { loc: 'ny' } + me: 'localhost:32000' }) ], firstSecondary: [ - extend(defaultFields, { + Object.assign({}, defaultFields, { ismaster: false, secondary: true, - me: 'localhost:32001', - primary: 'localhost:32000', - tags: { loc: 'sf' } + me: 'localhost:32001' }) ], arbiter: [ - extend(defaultFields, { + Object.assign({}, defaultFields, { ismaster: false, secondary: false, arbiterOnly: true, - me: 'localhost:32002', - primary: 'localhost:32000' + me: 'localhost:32002' }) ] }; @@ -71,7 +55,7 @@ class WriteConcernTest { let arbiterServer = yield mock.createServer(32002, 'localhost'); primaryServer.setMessageHandler(request => { - var doc = request.document; + const doc = request.document; if (doc.ismaster) { request.reply(self.serverStates.primary[0]); } else if (doc[resultKey]) { @@ -83,7 +67,7 @@ class WriteConcernTest { }); firstSecondaryServer.setMessageHandler(request => { - var doc = request.document; + const doc = request.document; if (doc.ismaster) { request.reply(self.serverStates.firstSecondary[0]); } else if (doc.endSessions) { @@ -92,7 +76,7 @@ class WriteConcernTest { }); arbiterServer.setMessageHandler(request => { - var doc = request.document; + const doc = request.document; if (doc.ismaster) { request.reply(self.serverStates.arbiter[0]); } else if (doc.endSessions) { @@ -105,51 +89,37 @@ class WriteConcernTest { ); client.connect(function(err, client) { - test.equal(null, err); - var db = client.db(self.configuration.db); - testFn(client, db); + expect(err).to.not.exist; + testFn(client, client.db(self.configuration.db)); }); }); } } -function withStubbedClient(testFn) { +function writeConcernTest(command, testFn) { return function(done) { const t = new WriteConcernTest(this.configuration); - testFn.call(this, t, done); - }; -} - -const writeConcernTestOptions = { w: 2, wtimeout: 1000 }; - -function writeConcernTest(command, testFn) { - return withStubbedClient(function(t, done) { switch (command) { case 'mapReduce': t.decorateResponse({ result: 'tempCollection' }); break; } t.run(command, (client, db) => - testFn.call(this, db, Object.assign({}, writeConcernTestOptions), err => { - test.equal(null, err); - test.deepEqual(writeConcernTestOptions, t.commandResult.writeConcern); + testFn.call(this, db, Object.assign({}, TEST_OPTIONS), err => { + expect(err).to.not.exist; + expect(TEST_OPTIONS.writeConcern).to.deep.equal(t.commandResult.writeConcern); client.close(done); }) ); - }); + }; } describe('Command Write Concern', function() { afterEach(() => mock.cleanup()); + const metadata = { requires: { generators: true, topology: 'single' } }; it('successfully pass through writeConcern to aggregate command', { - metadata: { - requires: { - generators: true, - topology: 'single' - } - }, - + metadata, test: writeConcernTest('aggregate', function(db, writeConcernTestOptions, done) { db.collection('test') .aggregate( @@ -161,26 +131,14 @@ describe('Command Write Concern', function() { }); it('successfully pass through writeConcern to create command', { - metadata: { - requires: { - generators: true, - topology: 'single' - } - }, - + metadata, test: writeConcernTest('create', function(db, writeConcernTestOptions, done) { db.createCollection('test_collection_methods', writeConcernTestOptions, done); }) }); it('successfully pass through writeConcern to createIndexes command', { - metadata: { - requires: { - generators: true, - topology: 'single' - } - }, - + metadata, test: writeConcernTest('createIndexes', function(db, writeConcernTestOptions, done) { db.collection('indexOptionDefault').createIndex( { a: 1 }, @@ -191,52 +149,28 @@ describe('Command Write Concern', function() { }); it('successfully pass through writeConcern to drop command', { - metadata: { - requires: { - generators: true, - topology: 'single' - } - }, - + metadata, test: writeConcernTest('drop', function(db, writeConcernTestOptions, done) { db.collection('indexOptionDefault').drop(writeConcernTestOptions, done); }) }); it('successfully pass through writeConcern to dropDatabase command', { - metadata: { - requires: { - generators: true, - topology: 'single' - } - }, - + metadata, test: writeConcernTest('dropDatabase', function(db, writeConcernTestOptions, done) { db.dropDatabase(writeConcernTestOptions, done); }) }); it('successfully pass through writeConcern to dropIndexes command', { - metadata: { - requires: { - generators: true, - topology: 'single' - } - }, - + metadata, test: writeConcernTest('dropIndexes', function(db, writeConcernTestOptions, done) { db.collection('test').dropIndexes(writeConcernTestOptions, done); }) }); it('successfully pass through writeConcern to mapReduce command', { - metadata: { - requires: { - generators: true, - topology: 'single' - } - }, - + metadata, test: writeConcernTest('mapReduce', function(db, writeConcernTestOptions, done) { const Code = this.configuration.require.Code; const map = new Code('function() { emit(this.user_id, 1); }'); @@ -251,39 +185,21 @@ describe('Command Write Concern', function() { }); it('successfully pass through writeConcern to createUser command', { - metadata: { - requires: { - generators: true, - topology: 'single' - } - }, - + metadata, test: writeConcernTest('createUser', function(db, writeConcernTestOptions, done) { db.admin().addUser('kay:kay', 'abc123', writeConcernTestOptions, done); }) }); it('successfully pass through writeConcern to dropUser command', { - metadata: { - requires: { - generators: true, - topology: 'single' - } - }, - + metadata, test: writeConcernTest('dropUser', function(db, writeConcernTestOptions, done) { db.admin().removeUser('kay:kay', writeConcernTestOptions, done); }) }); it('successfully pass through writeConcern to findAndModify command', { - metadata: { - requires: { - generators: true, - topology: 'single' - } - }, - + metadata, test: writeConcernTest('findAndModify', function(db, writeConcernTestOptions, done) { db.collection('test').findAndModify( { a: 1 },