Skip to content

Commit 12d011b

Browse files
authored
test: add tests to ensure readPreference and writeConcern are respected from uri (#2715)
NODE-2965
1 parent 25ef870 commit 12d011b

File tree

2 files changed

+50
-1
lines changed

2 files changed

+50
-1
lines changed

test/functional/readpreference.test.js

+28-1
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
const test = require('./shared').assert;
44
const setupDatabase = require('./shared').setupDatabase;
55
const withMonitoredClient = require('./shared').withMonitoredClient;
6-
const expect = require('chai').expect;
76
const { ReadPreference, Topology } = require('../../src');
87
const { withClient } = require('./shared');
8+
const chai = require('chai');
9+
chai.use(require('chai-subset'));
10+
const expect = chai.expect;
911

1012
describe('ReadPreference', function () {
1113
before(function () {
@@ -620,4 +622,29 @@ describe('ReadPreference', function () {
620622
});
621623
});
622624
});
625+
626+
it('should respect readPreference from uri', {
627+
metadata: { requires: { topology: 'replicaset', mongodb: '>=3.6' } },
628+
test: withMonitoredClient('find', { queryOptions: { readPreference: 'secondary' } }, function (
629+
client,
630+
events,
631+
done
632+
) {
633+
expect(client.readPreference.mode).to.equal('secondary');
634+
client
635+
.db('test')
636+
.collection('test')
637+
.findOne({ a: 1 }, err => {
638+
expect(err).to.not.exist;
639+
expect(events).to.be.an('array').with.lengthOf(1);
640+
expect(events[0]).to.containSubset({
641+
commandName: 'find',
642+
command: {
643+
$readPreference: { mode: 'secondary' }
644+
}
645+
});
646+
done();
647+
});
648+
})
649+
});
623650
});

test/functional/write_concern.test.js

+22
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,28 @@ describe('Write Concern', function () {
2323
generateTopologyTests(testSuites, testContext);
2424
});
2525

26+
it(
27+
'should respect writeConcern from uri',
28+
withMonitoredClient('insert', { queryOptions: { w: 0 } }, function (client, events, done) {
29+
expect(client.writeConcern).to.eql({ w: 0 });
30+
client
31+
.db('test')
32+
.collection('test')
33+
.insertOne({ a: 1 }, (err, result) => {
34+
expect(err).to.not.exist;
35+
expect(result).to.exist;
36+
expect(events).to.be.an('array').with.lengthOf(1);
37+
expect(events[0]).to.containSubset({
38+
commandName: 'insert',
39+
command: {
40+
writeConcern: { w: 0 }
41+
}
42+
});
43+
done();
44+
});
45+
})
46+
);
47+
2648
// TODO: once `read-write-concern/connection-string` spec tests are implemented these can likely be removed
2749
describe('test journal connection string option', function () {
2850
function journalOptionTest(client, events, done) {

0 commit comments

Comments
 (0)