Skip to content

Commit 4dbd767

Browse files
committed
improve tests
1 parent 85439d9 commit 4dbd767

File tree

2 files changed

+50
-16
lines changed

2 files changed

+50
-16
lines changed

test/functional/readpreference.test.js

+28-8
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,12 @@ const Topology = require('../../lib/core/sdam/topology').Topology;
44
const test = require('./shared').assert;
55
const setupDatabase = require('./shared').setupDatabase;
66
const withMonitoredClient = require('./shared').withMonitoredClient;
7-
const expect = require('chai').expect;
7+
88
const ReadPreference = require('../../lib/core/topologies/read_preference');
99
const withClient = require('./shared').withClient;
10+
const chai = require('chai');
11+
chai.use(require('chai-subset'));
12+
const expect = chai.expect;
1013

1114
describe('ReadPreference', function() {
1215
before(function() {
@@ -730,13 +733,30 @@ describe('ReadPreference', function() {
730733
});
731734
});
732735

733-
it('should respect readPreference from uri', function(done) {
734-
const configuration = this.configuration;
735-
const client = configuration.newClient(`${configuration.url()}?readPreference=secondary`);
736-
client.connect(err => {
737-
expect(err).to.not.exist;
736+
it('should respect readPreference from uri', {
737+
metadata: { requires: { topology: 'replicaset', mongodb: '>=3.6' } },
738+
test: withMonitoredClient('find', { queryOptions: { readPreference: 'secondary' } }, function(
739+
client,
740+
events,
741+
done
742+
) {
738743
expect(client.readPreference.mode).to.equal('secondary');
739-
client.close(done);
740-
});
744+
client
745+
.db('test')
746+
.collection('test')
747+
.findOne({ a: 1 }, err => {
748+
expect(err).to.not.exist;
749+
expect(events)
750+
.to.be.an('array')
751+
.with.lengthOf(1);
752+
expect(events[0]).to.containSubset({
753+
commandName: 'find',
754+
command: {
755+
$readPreference: { mode: 'secondary' }
756+
}
757+
});
758+
done();
759+
});
760+
})
741761
});
742762
});

test/functional/write_concern.test.js

+22-8
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,29 @@ describe('Write Concern', function() {
2121
generateTopologyTests(testSuites, testContext);
2222
});
2323

24-
it('should respect writeConcern from uri', function(done) {
25-
const configuration = this.configuration;
26-
const client = configuration.newClient(`${configuration.url()}?w=0`);
27-
client.connect(err => {
28-
expect(err).to.not.exist;
24+
it(
25+
'should respect writeConcern from uri',
26+
withMonitoredClient('insert', { queryOptions: { w: 0 } }, function(client, events, done) {
2927
expect(client.writeConcern).to.eql({ w: 0 });
30-
client.close(done);
31-
});
32-
});
28+
client
29+
.db('test')
30+
.collection('test')
31+
.insertOne({ a: 1 }, (err, result) => {
32+
expect(err).to.not.exist;
33+
expect(result).to.exist;
34+
expect(events)
35+
.to.be.an('array')
36+
.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+
);
3347

3448
// TODO: once `read-write-concern/connection-string` spec tests are implemented these can likely be removed
3549
describe('test journal connection string option', function() {

0 commit comments

Comments
 (0)