|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const EventEmitter = require('events'); |
| 4 | +const expect = require('chai').expect; |
| 5 | +const sinon = require('sinon'); |
| 6 | + |
| 7 | +class MockTopology extends EventEmitter { |
| 8 | + constructor() { |
| 9 | + super(); |
| 10 | + } |
| 11 | + |
| 12 | + capabilities() { |
| 13 | + return {}; |
| 14 | + } |
| 15 | +} |
| 16 | + |
| 17 | +const test = {}; |
| 18 | +describe('Database', function() { |
| 19 | + before(() => { |
| 20 | + // NOTE: These modules are being used prior to test run. In order to monkey-patch them |
| 21 | + // we must remove their cached versions. |
| 22 | + const resolvedUtils = require.resolve('../../lib/utils'); |
| 23 | + const resolvedDb = require.resolve('../../lib/db'); |
| 24 | + delete require.cache[resolvedUtils]; |
| 25 | + delete require.cache[resolvedDb]; |
| 26 | + test.utils = require('../../lib/utils'); |
| 27 | + |
| 28 | + // create a sandbox for stub cleanup |
| 29 | + test.sandbox = sinon.sandbox.create(); |
| 30 | + }); |
| 31 | + |
| 32 | + afterEach(() => test.sandbox.restore()); |
| 33 | + |
| 34 | + it('should ignore a readPreference for dropDatabase', { |
| 35 | + metadata: { requires: { topology: 'single' } }, |
| 36 | + test: function() { |
| 37 | + sinon.stub(test.utils, 'executeOperation').callsFake((topology, operation, args) => { |
| 38 | + const options = args[args.length - 2]; |
| 39 | + expect(options.readPreference).to.equal('primary'); |
| 40 | + }); |
| 41 | + |
| 42 | + const Db = require('../../lib/db'); |
| 43 | + const db = new Db('fakeDb', new MockTopology(), { readPreference: 'nearest' }); |
| 44 | + db.dropDatabase(); |
| 45 | + } |
| 46 | + }); |
| 47 | +}); |
0 commit comments