-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
write_concern.test.js
120 lines (106 loc) · 3.66 KB
/
write_concern.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
'use strict';
const chai = require('chai');
const expect = chai.expect;
const TestRunnerContext = require('./spec-runner').TestRunnerContext;
const generateTopologyTests = require('./spec-runner').generateTopologyTests;
const loadSpecTests = require('../spec').loadSpecTests;
const { withMonitoredClient } = require('./shared');
const mock = require('../tools/mock');
const { MongoClient } = require('../../src');
describe('Write Concern', function () {
describe('spec tests', function () {
const testContext = new TestRunnerContext();
const testSuites = loadSpecTests('read-write-concern/operation');
after(() => testContext.teardown());
before(function () {
return testContext.setup(this.configuration);
});
generateTopologyTests(testSuites, testContext);
});
it(
'should respect writeConcern from uri',
withMonitoredClient('insert', { queryOptions: { w: 0 } }, function (client, events, done) {
expect(client.writeConcern).to.eql({ w: 0 });
client
.db('test')
.collection('test')
.insertOne({ a: 1 }, (err, result) => {
expect(err).to.not.exist;
expect(result).to.exist;
expect(events).to.be.an('array').with.lengthOf(1);
expect(events[0]).to.containSubset({
commandName: 'insert',
command: {
writeConcern: { w: 0 }
}
});
done();
});
})
);
// TODO: once `read-write-concern/connection-string` spec tests are implemented these can likely be removed
describe('test journal connection string option', function () {
function journalOptionTest(client, events, done) {
expect(client).to.have.nested.property('s.options.writeConcern');
expect(client.s.options.writeConcern).to.satisfy(wc => wc.j || wc.journal);
client
.db('test')
.collection('test')
.insertOne({ a: 1 }, (err, result) => {
expect(err).to.not.exist;
expect(result).to.exist;
expect(events).to.be.an('array').with.lengthOf(1);
expect(events[0]).to.containSubset({
commandName: 'insert',
command: {
writeConcern: { j: true }
}
});
done();
});
}
// baseline to confirm client option is working
it(
'should set write concern with j: true client option',
withMonitoredClient(
'insert',
{ clientOptions: { writeConcern: { j: true } } },
journalOptionTest
)
);
// ensure query option in connection string passes through
it(
'should set write concern with journal=true connection string option',
withMonitoredClient('insert', { queryOptions: { journal: true } }, journalOptionTest)
);
});
let server;
before(() => {
return mock.createServer().then(s => {
server = s;
});
});
after(() => mock.cleanup());
it('should pipe writeConcern from client down to API call', function () {
server.setMessageHandler(request => {
if (request.document && request.document.ismaster) {
return request.reply(mock.DEFAULT_ISMASTER);
}
expect(request.document.writeConcern).to.exist;
expect(request.document.writeConcern.w).to.equal('majority');
return request.reply({ ok: 1 });
});
const uri = `mongodb://${server.uri()}`;
const client = new MongoClient(uri, { writeConcern: 'majority' });
return client
.connect()
.then(() => {
const db = client.db('wc_test');
const collection = db.collection('wc');
return collection.insertMany([{ a: 2 }]);
})
.then(() => {
return client.close();
});
});
});