From 9da03955836f5703c7a6508d05ab352ac5cb8b68 Mon Sep 17 00:00:00 2001 From: Rosemary Yin Date: Fri, 21 Jun 2019 16:39:31 -0400 Subject: [PATCH] fix(createCollection): Db.createCollection should pass readConcern to new collection (#2026) * add tests to verify correct readConcern inheritance * create new test for createCollection * modernized test --- lib/db.js | 4 +++- lib/operations/create_collection.js | 1 + test/functional/readconcern_tests.js | 27 ++++++++++++++++++++++++++- 3 files changed, 30 insertions(+), 2 deletions(-) diff --git a/lib/db.js b/lib/db.js index 9f964fb2a5..5102ec17df 100644 --- a/lib/db.js +++ b/lib/db.js @@ -505,7 +505,9 @@ Db.prototype.createCollection = deprecateOptions( if (typeof options === 'function') (callback = options), (options = {}); options = options || {}; options.promiseLibrary = options.promiseLibrary || this.s.promiseLibrary; - + options.readConcern = options.readConcern + ? new ReadConcern(options.readConcern.level) + : this.readConcern; const createCollectionOperation = new CreateCollectionOperation(this, name, options); return executeOperation(this.s.topology, createCollectionOperation, callback); diff --git a/lib/operations/create_collection.js b/lib/operations/create_collection.js index 055f7efccb..35c3a6f076 100644 --- a/lib/operations/create_collection.js +++ b/lib/operations/create_collection.js @@ -22,6 +22,7 @@ const illegalCommandFields = [ 'raw', 'readPreference', 'session', + 'readConcern', 'writeConcern' ]; diff --git a/test/functional/readconcern_tests.js b/test/functional/readconcern_tests.js index 63b03866a4..3234542bb9 100644 --- a/test/functional/readconcern_tests.js +++ b/test/functional/readconcern_tests.js @@ -10,7 +10,7 @@ describe('ReadConcern', function() { return setupDatabase(configuration); }); - it('Should set local readConcern on db level', { + it('Should set local readConcern on db level when using collection method', { metadata: { requires: { topology: 'replicaset', mongodb: '>= 3.2' } }, test: function(done) { @@ -56,6 +56,31 @@ describe('ReadConcern', function() { } }); + it('Should set local readConcern on db level when using createCollection method', { + metadata: { requires: { topology: 'replicaset', mongodb: '>= 3.2' } }, + + test: function(done) { + // Get a new instance + const configuration = this.configuration; + const client = configuration.newClient( + { w: 1 }, + { poolSize: 1, readConcern: { level: 'local' } } + ); + client.connect((err, client) => { + expect(err).to.not.exist; + const db = client.db(configuration.db); + expect(db.s.readConcern).to.deep.equal({ level: 'local' }); + + // Get a collection using createCollection + db.createCollection('readConcernCollection', (err, collection) => { + // Validate readConcern + expect(collection.s.readConcern).to.deep.equal({ level: 'local' }); + client.close(done); + }); + }); + } + }); + it('Should set majority readConcern on db level', { metadata: { requires: { topology: 'replicaset', mongodb: '>= 3.2' } },