|
1 | 1 | 'use strict';
|
| 2 | +const Topology = require('../../lib/core/sdam/topology').Topology; |
2 | 3 | const setupDatabase = require('./shared').setupDatabase;
|
3 | 4 | const chai = require('chai');
|
4 | 5 | const expect = chai.expect;
|
5 | 6 | const sinonChai = require('sinon-chai');
|
6 | 7 | const mock = require('mongodb-mock-server');
|
| 8 | +const ReadPreference = require('../../lib/core/topologies/read_preference'); |
7 | 9 | chai.use(sinonChai);
|
8 | 10 |
|
9 | 11 | describe('Collection', function() {
|
@@ -1307,4 +1309,42 @@ describe('Collection', function() {
|
1307 | 1309 | });
|
1308 | 1310 | }
|
1309 | 1311 | });
|
| 1312 | + |
| 1313 | + context('DDL methods with serverSelection readPreference primary', () => { |
| 1314 | + const collectionMethodSet = { |
| 1315 | + createIndex: [{ quote: 'text' }] |
| 1316 | + }; |
| 1317 | + |
| 1318 | + Object.keys(collectionMethodSet).forEach(operation => { |
| 1319 | + it(`should ${operation} with serverSelection readPreference primary`, { |
| 1320 | + metadata: { |
| 1321 | + requires: { topology: 'replicaset' } |
| 1322 | + }, |
| 1323 | + test: function(done) { |
| 1324 | + const opArgs = collectionMethodSet[operation]; |
| 1325 | + const configuration = this.configuration; |
| 1326 | + const client = configuration.newClient(configuration.writeConcernMax(), { |
| 1327 | + useUnifiedTopology: true, |
| 1328 | + readPreference: 'primaryPreferred' |
| 1329 | + }); |
| 1330 | + client.connect((err, client) => { |
| 1331 | + expect(err).to.not.exist; |
| 1332 | + const db = client.db(configuration.db); |
| 1333 | + const collection = db.collection('db-two'); |
| 1334 | + const TopologySpy = this.sinon.spy(Topology.prototype, 'selectServer'); |
| 1335 | + const callback = err => { |
| 1336 | + expect(err).to.not.exist; |
| 1337 | + expect(TopologySpy.called).to.equal(true); |
| 1338 | + expect(TopologySpy) |
| 1339 | + .nested.property('args[0][0].readPreference.mode') |
| 1340 | + .to.equal(ReadPreference.PRIMARY); |
| 1341 | + client.close(done); |
| 1342 | + }; |
| 1343 | + opArgs.push(callback); |
| 1344 | + collection[operation].apply(collection, opArgs); |
| 1345 | + }); |
| 1346 | + } |
| 1347 | + }); |
| 1348 | + }); |
| 1349 | + }); |
1310 | 1350 | });
|
0 commit comments