Skip to content

Commit

Permalink
fix(createCollection): Db.createCollection should pass readConcern to…
Browse files Browse the repository at this point in the history
… new collection (#2026)

* add tests to verify correct readConcern inheritance

* create new test for createCollection

* modernized test
  • Loading branch information
rosemaryy authored and daprahamian committed Aug 13, 2019
1 parent 4df8399 commit 6145d4b
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
4 changes: 3 additions & 1 deletion lib/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions lib/operations/create_collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const illegalCommandFields = [
'raw',
'readPreference',
'session',
'readConcern',
'writeConcern'
];

Expand Down
27 changes: 26 additions & 1 deletion test/functional/readconcern_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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' } },

Expand Down

0 comments on commit 6145d4b

Please sign in to comment.