-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(db): ensure
dropDatabase
always uses primary read preference
- Loading branch information
Showing
2 changed files
with
51 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
'use strict'; | ||
|
||
const EventEmitter = require('events'); | ||
const expect = require('chai').expect; | ||
const sinon = require('sinon'); | ||
|
||
class MockTopology extends EventEmitter { | ||
constructor() { | ||
super(); | ||
} | ||
|
||
capabilities() { | ||
return {}; | ||
} | ||
} | ||
|
||
const test = {}; | ||
describe('Database', function() { | ||
before(() => { | ||
// NOTE: These modules are being used prior to test run. In order to monkey-patch them | ||
// we must remove their cached versions. | ||
const resolvedUtils = require.resolve('../../lib/utils'); | ||
const resolvedDb = require.resolve('../../lib/db'); | ||
delete require.cache[resolvedUtils]; | ||
delete require.cache[resolvedDb]; | ||
test.utils = require('../../lib/utils'); | ||
|
||
// create a sandbox for stub cleanup | ||
test.sandbox = sinon.sandbox.create(); | ||
}); | ||
|
||
afterEach(() => test.sandbox.restore()); | ||
|
||
it('should ignore a readPreference for dropDatabase', { | ||
metadata: { requires: { topology: 'single' } }, | ||
test: function() { | ||
sinon.stub(test.utils, 'executeOperation').callsFake((topology, operation, args) => { | ||
const options = args[args.length - 2]; | ||
expect(options.readPreference).to.equal('primary'); | ||
}); | ||
|
||
const Db = require('../../lib/db'); | ||
const db = new Db('fakeDb', new MockTopology(), { readPreference: 'nearest' }); | ||
db.dropDatabase(); | ||
} | ||
}); | ||
}); |