diff --git a/test/functional/command_write_concern.test.js b/test/functional/command_write_concern.test.js index 8a614a4f0cd..7f3f5797658 100644 --- a/test/functional/command_write_concern.test.js +++ b/test/functional/command_write_concern.test.js @@ -1,1205 +1,213 @@ 'use strict'; -var test = require('./shared').assert; -var co = require('co'); -var mock = require('mongodb-mock-server'); - -// 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; -}; - -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 = [ - extend(defaultFields, { +const co = require('co'); +const mock = require('mongodb-mock-server'); +const expect = require('chai').expect; + +const TEST_OPTIONS = { writeConcern: { w: 2, wtimeout: 1000 } }; + +class WriteConcernTest { + constructor(configuration) { + this.configuration = configuration; + 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'], + primary: 'localhost:32000', + arbiters: ['localhost:32002'] + }); + this.serverStates = { + primary: [ + Object.assign({}, defaultFields, { ismaster: true, secondary: false, - me: 'localhost:32000', - primary: 'localhost:32000', - tags: { loc: 'ny' } + me: 'localhost:32000' }) - ]; - - // Primary server states - var firstSecondary = [ - extend(defaultFields, { + ], + firstSecondary: [ + Object.assign({}, defaultFields, { ismaster: false, secondary: true, - me: 'localhost:32001', - primary: 'localhost:32000', - tags: { loc: 'sf' } + me: 'localhost:32001' }) - ]; - - // Primary server states - var arbiter = [ - extend(defaultFields, { + ], + arbiter: [ + Object.assign({}, defaultFields, { ismaster: false, secondary: false, arbiterOnly: true, - me: 'localhost:32002', - primary: 'localhost:32000' + me: 'localhost:32002' }) - ]; - - // 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'); - - 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 }); - } - }); - - 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; - 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); + ] + }; + } + 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 => { + const doc = request.document; + if (doc.ismaster) { + request.reply(self.serverStates.primary[0]); + } else if (doc[resultKey]) { + self.commandResult = doc; + request.reply(Object.assign({ ok: 1 }, self.responseDecoration)); + } else if (doc.endSessions) { + request.reply({ ok: 1 }); + } + }); + + firstSecondaryServer.setMessageHandler(request => { + const doc = request.document; + if (doc.ismaster) { + request.reply(self.serverStates.firstSecondary[0]); + } else if (doc.endSessions) { + request.reply({ ok: 1 }); + } + }); + + arbiterServer.setMessageHandler(request => { + const doc = request.document; + if (doc.ismaster) { + request.reply(self.serverStates.arbiter[0]); + } else if (doc.endSessions) { + request.reply({ ok: 1 }); + } + }); + + const client = self.configuration.newClient( + 'mongodb://localhost:32000,localhost:32001,localhost:32002/test?replicaSet=rs' + ); + + client.connect(function(err, client) { + expect(err).to.not.exist; + testFn(client, client.db(self.configuration.db)); + }); + }); + } +} + +function writeConcernTest(command, testFn) { + return function(done) { + const t = new WriteConcernTest(this.configuration); + switch (command) { + case 'mapReduce': + t.decorateResponse({ result: 'tempCollection' }); + break; + } + t.run(command, (client, db) => + 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); + }) + ); + }; +} - 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); +describe('Command Write Concern', function() { + afterEach(() => mock.cleanup()); + const metadata = { requires: { generators: true, topology: 'single' } }; - client.close(done); - }); - }); - }); - } + it('successfully pass through writeConcern to aggregate command', { + metadata, + 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', { - metadata: { - requires: { - generators: true, - topology: 'single' - } - }, - - 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 }); - } - }); - - 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) { - 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); - - client.close(done); - }); - }); - }); - } + 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' - } - }, - - 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 }); - } - }); - - 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) { - 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); - } - ); - }); - }); - } + metadata, + 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', { - 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 = [ - 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 }); - } - }); - - 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) { - 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); - } - ); - }); - }); - } + 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' - } - }, - - 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 }); - } - }); - - 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) { - 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); - } - ); - }); - }); - } + 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' - } - }, - - 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 }); - } - }); - - 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) { - 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); - } - ); - }); - }); - } + 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' - } - }, - - 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 }); - } - }); - - 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) { - 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); - } - ); - }); - }); - } + metadata, + test: writeConcernTest('mapReduce', function(db, writeConcernTestOptions, done) { + const Code = this.configuration.require.Code; + 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', { - 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 = [ - 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) { - 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); - - client.close(done); - }); - }); - }); - } + 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' - } - }, - - 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) { - 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); - - client.close(done); - }); - }); - }); - } + 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' - } - }, - - 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 }); - } - }); - - 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) { - 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); - } - ); - }); - }); - } + metadata, + test: writeConcernTest('findAndModify', function(db, writeConcernTestOptions, done) { + db.collection('test').findAndModify( + { a: 1 }, + [['a', 1]], + { $set: { b1: 1 } }, + Object.assign({ new: true }, writeConcernTestOptions), + done + ); + }) }); });