Skip to content

Commit bb037fe

Browse files
committed
test(NODE-7184): refactor tests
1 parent c0a3b73 commit bb037fe

File tree

1 file changed

+105
-147
lines changed

1 file changed

+105
-147
lines changed
Lines changed: 105 additions & 147 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { expect } from 'chai';
22

3-
import { type MongoClient } from '../../../src';
3+
import { type MongoClient, ReadConcernLevel } from '../../../src';
44
import { filterForCommands, setupDatabase } from '../shared';
55

6-
describe.only('ReadConcern', function () {
6+
describe('ReadConcern', function () {
77
let client: MongoClient;
88

99
before(function () {
@@ -29,35 +29,35 @@ describe.only('ReadConcern', function () {
2929
{
3030
description: 'Should set local readConcern on db level when using collection method',
3131
commandName: 'find',
32-
readConcern: { level: 'local' }
32+
readConcern: { level: ReadConcernLevel.local }
3333
},
3434
{
3535
description: 'Should set majority readConcern on db level',
3636
commandName: 'find',
37-
readConcern: { level: 'majority' }
37+
readConcern: { level: ReadConcernLevel.majority }
3838
},
3939
{
4040
description: 'Should set majority readConcern aggregate command',
4141
commandName: 'aggregate',
42-
readConcern: { level: 'majority' }
42+
readConcern: { level: ReadConcernLevel.majority }
4343
},
4444
{
4545
description: 'Should set local readConcern at collection level',
4646
commandName: 'find',
47-
readConcern: { level: 'local' }
47+
readConcern: { level: ReadConcernLevel.local }
4848
},
4949
{
5050
description: 'Should set majority readConcern at collection level',
5151
commandName: 'find',
52-
readConcern: { level: 'majority' }
52+
readConcern: { level: ReadConcernLevel.majority }
5353
}
5454
];
5555

5656
tests.forEach(test => {
5757
it(test.description, {
5858
metadata: { requires: { topology: 'replicaset', mongodb: '>= 3.2' } },
5959

60-
test: function (done) {
60+
test: async function () {
6161
const started = [];
6262
const succeeded = [];
6363
// Get a new instance
@@ -67,37 +67,29 @@ describe.only('ReadConcern', function () {
6767
{ maxPoolSize: 1, readConcern: test.readConcern, monitorCommands: true }
6868
);
6969

70-
client.connect((err, client) => {
71-
expect(err).to.not.exist;
70+
await client.connect();
7271

73-
const db = client.db(configuration.db);
74-
expect(db.readConcern).to.deep.equal(test.readConcern);
72+
const db = client.db(configuration.db);
73+
expect(db.readConcern).to.deep.equal(test.readConcern);
7574

76-
// Get a collection
77-
const collection = db.collection('readConcernCollection');
75+
// Get a collection
76+
const collection = db.collection('readConcernCollection');
7877

79-
// Validate readConcern
80-
expect(collection.readConcern).to.deep.equal(test.readConcern);
78+
// Validate readConcern
79+
expect(collection.readConcern).to.deep.equal(test.readConcern);
8180

82-
// commandMonitoring
83-
client.on('commandStarted', filterForCommands(test.commandName, started));
84-
client.on('commandSucceeded', filterForCommands(test.commandName, succeeded));
81+
// commandMonitoring
82+
client.on('commandStarted', filterForCommands(test.commandName, started));
83+
client.on('commandSucceeded', filterForCommands(test.commandName, succeeded));
8584

86-
// Execute find
87-
if (test.commandName === 'find') {
88-
collection.find().toArray(err => {
89-
expect(err).to.not.exist;
90-
validateTestResults(started, succeeded, test.commandName, test.readConcern.level);
91-
done();
92-
});
93-
} else if (test.commandName === 'aggregate') {
94-
collection.aggregate([{ $match: {} }]).toArray(err => {
95-
expect(err).to.not.exist;
96-
validateTestResults(started, succeeded, test.commandName, test.readConcern.level);
97-
done();
98-
});
99-
}
100-
});
85+
// Execute find
86+
if (test.commandName === 'find') {
87+
await collection.find().toArray();
88+
validateTestResults(started, succeeded, test.commandName, test.readConcern.level);
89+
} else if (test.commandName === 'aggregate') {
90+
await collection.aggregate([{ $match: {} }]).toArray();
91+
validateTestResults(started, succeeded, test.commandName, test.readConcern.level);
92+
}
10193
}
10294
});
10395
});
@@ -107,23 +99,23 @@ describe.only('ReadConcern', function () {
10799
{
108100
description: 'Should set local readConcern using MongoClient',
109101
urlReadConcernLevel: 'readConcernLevel=local',
110-
readConcern: { level: 'local' }
102+
readConcern: { level: ReadConcernLevel.local }
111103
},
112104
{
113105
description: 'Should set majority readConcern using MongoClient',
114106
urlReadConcernLevel: 'readConcernLevel=majority',
115-
readConcern: { level: 'majority' }
107+
readConcern: { level: ReadConcernLevel.majority }
116108
},
117109
{
118110
description: 'Should set majority readConcern using MongoClient with options',
119-
readConcern: { level: 'majority' }
111+
readConcern: { level: ReadConcernLevel.majority }
120112
}
121113
];
122114

123115
urlTests.forEach(test => {
124116
it(test.description, {
125117
metadata: { requires: { topology: 'replicaset', mongodb: '>= 3.2' } },
126-
test: function (done) {
118+
test: async function () {
127119
const started = [];
128120
const succeeded = [];
129121
// Get a new instance
@@ -143,29 +135,24 @@ describe.only('ReadConcern', function () {
143135
});
144136
}
145137

146-
client.connect((err, client) => {
147-
expect(err).to.not.exist;
138+
await client.connect();
148139

149-
const db = client.db(configuration.db);
150-
expect(db.readConcern).to.deep.equal(test.readConcern);
140+
const db = client.db(configuration.db);
141+
expect(db.readConcern).to.deep.equal(test.readConcern);
151142

152-
// Get a collection
153-
const collection = db.collection('readConcernCollection');
143+
// Get a collection
144+
const collection = db.collection('readConcernCollection');
154145

155-
// Validate readConcern
156-
expect(collection.readConcern).to.deep.equal(test.readConcern);
146+
// Validate readConcern
147+
expect(collection.readConcern).to.deep.equal(test.readConcern);
157148

158-
// commandMonitoring
159-
client.on('commandStarted', filterForCommands('find', started));
160-
client.on('commandSucceeded', filterForCommands('find', succeeded));
149+
// commandMonitoring
150+
client.on('commandStarted', filterForCommands('find', started));
151+
client.on('commandSucceeded', filterForCommands('find', succeeded));
161152

162-
// Execute find
163-
collection.find().toArray(err => {
164-
expect(err).to.not.exist;
165-
validateTestResults(started, succeeded, 'find', test.readConcern.level);
166-
done();
167-
});
168-
});
153+
// Execute find
154+
await collection.find().toArray();
155+
validateTestResults(started, succeeded, 'find', test.readConcern.level);
169156
}
170157
});
171158
});
@@ -176,21 +163,21 @@ describe.only('ReadConcern', function () {
176163
description: 'Should set majority readConcern distinct command',
177164
commandName: 'distinct',
178165
mongodbVersion: '>= 3.2',
179-
readConcern: { level: 'majority' }
166+
readConcern: { level: ReadConcernLevel.majority }
180167
},
181168
{
182169
description: 'Should set majority readConcern count command',
183170
commandName: 'count',
184171
mongodbVersion: '>= 3.2',
185-
readConcern: { level: 'majority' }
172+
readConcern: { level: ReadConcernLevel.majority }
186173
}
187174
];
188175

189176
insertTests.forEach(test => {
190177
it(test.description, {
191178
metadata: { requires: { topology: 'replicaset', mongodb: test.mongodbVersion } },
192179

193-
test: function (done) {
180+
test: async function () {
194181
const started = [];
195182
const succeeded = [];
196183
// Get a new instance
@@ -200,58 +187,47 @@ describe.only('ReadConcern', function () {
200187
{ maxPoolSize: 1, readConcern: test.readConcern, monitorCommands: true }
201188
);
202189

203-
client.connect((err, client) => {
204-
expect(err).to.not.exist;
205-
206-
const db = client.db(configuration.db);
207-
expect(db.readConcern).to.deep.equal(test.readConcern);
190+
await client.connect();
208191

209-
// Get the collection
210-
const collection = db.collection('readConcernCollection');
192+
const db = client.db(configuration.db);
193+
expect(db.readConcern).to.deep.equal(test.readConcern);
194+
195+
// Get the collection
196+
const collection = db.collection('readConcernCollection');
197+
198+
// Insert documents to perform distinct against
199+
await collection.insertMany(
200+
[
201+
{ a: 0, b: { c: 'a' } },
202+
{ a: 1, b: { c: 'b' } },
203+
{ a: 1, b: { c: 'c' } },
204+
{ a: 2, b: { c: 'a' } },
205+
{ a: 3 },
206+
{ a: 3 }
207+
],
208+
configuration.writeConcernMax()
209+
);
211210

212-
// Insert documents to perform distinct against
213-
collection.insertMany(
214-
[
215-
{ a: 0, b: { c: 'a' } },
216-
{ a: 1, b: { c: 'b' } },
217-
{ a: 1, b: { c: 'c' } },
218-
{ a: 2, b: { c: 'a' } },
219-
{ a: 3 },
220-
{ a: 3 }
221-
],
222-
configuration.writeConcernMax(),
223-
err => {
224-
expect(err).to.not.exist;
225-
226-
// Listen to apm events
227-
client.on('commandStarted', filterForCommands(test.commandName, started));
228-
client.on('commandSucceeded', filterForCommands(test.commandName, succeeded));
229-
230-
// Perform a distinct query against the a field
231-
if (test.commandName === 'distinct') {
232-
collection.distinct('a', err => {
233-
expect(err).to.not.exist;
234-
validateTestResults(started, succeeded, test.commandName, test.readConcern.level);
235-
done();
236-
});
237-
} else if (test.commandName === 'count') {
238-
collection.estimatedDocumentCount(err => {
239-
expect(err).to.not.exist;
240-
validateTestResults(started, succeeded, test.commandName, test.readConcern.level);
241-
done();
242-
});
243-
}
244-
}
245-
);
246-
});
211+
// Listen to apm events
212+
client.on('commandStarted', filterForCommands(test.commandName, started));
213+
client.on('commandSucceeded', filterForCommands(test.commandName, succeeded));
214+
215+
// Perform a distinct query against the a field
216+
if (test.commandName === 'distinct') {
217+
await collection.distinct('a');
218+
validateTestResults(started, succeeded, test.commandName, test.readConcern.level);
219+
} else if (test.commandName === 'count') {
220+
await collection.estimatedDocumentCount();
221+
validateTestResults(started, succeeded, test.commandName, test.readConcern.level);
222+
}
247223
}
248224
});
249225
});
250226

251227
it('Should set majority readConcern aggregate command against server >= 4.1', {
252228
metadata: { requires: { topology: 'replicaset' } },
253229

254-
test: function (done) {
230+
test: async function () {
255231
const started = [];
256232
const succeeded = [];
257233
// Get a new instance
@@ -261,65 +237,47 @@ describe.only('ReadConcern', function () {
261237
{ maxPoolSize: 1, readConcern: { level: 'majority' }, monitorCommands: true }
262238
);
263239

264-
client
265-
.connect()
266-
.then(() => {
267-
// Get a collection
268-
const collection = client
269-
.db(configuration.db)
270-
.collection('readConcernCollectionAggregate1');
240+
await client.connect();
241+
// Get a collection
242+
const collection = client.db(configuration.db).collection('readConcernCollectionAggregate1');
271243

272-
// Listen to apm events
273-
client.on('commandStarted', filterForCommands('aggregate', started));
274-
client.on('commandSucceeded', filterForCommands('aggregate', succeeded));
244+
// Listen to apm events
245+
client.on('commandStarted', filterForCommands('aggregate', started));
246+
client.on('commandSucceeded', filterForCommands('aggregate', succeeded));
275247

276-
// Execute find
277-
return collection
278-
.aggregate([{ $match: {} }, { $out: 'readConcernCollectionAggregate1Output' }])
279-
.toArray()
280-
.then(() => {
281-
validateTestResults(started, succeeded, 'aggregate', 'majority');
282-
283-
// Execute find
284-
return collection
285-
.aggregate([{ $match: {} }], { out: 'readConcernCollectionAggregate2Output' })
286-
.toArray()
287-
.then(() => {
288-
validateTestResults(started, succeeded, 'aggregate', 'majority');
289-
});
290-
});
291-
})
292-
.then(
293-
() => client.close(done),
294-
e => client.close(() => done(e))
295-
);
248+
// Execute find
249+
await collection
250+
.aggregate([{ $match: {} }, { $out: 'readConcernCollectionAggregate1Output' }])
251+
.toArray();
252+
253+
validateTestResults(started, succeeded, 'aggregate', 'majority');
254+
255+
// Execute find
256+
await collection
257+
.aggregate([{ $match: {} }], { out: 'readConcernCollectionAggregate2Output' })
258+
.toArray();
259+
validateTestResults(started, succeeded, 'aggregate', 'majority');
296260
}
297261
});
298262

299263
it('Should set local readConcern on db level when using createCollection method', {
300264
metadata: { requires: { topology: 'replicaset', mongodb: '>= 3.2' } },
301265

302-
test: function (done) {
266+
test: async function () {
303267
// Get a new instance
304268
const configuration = this.configuration;
305269
client = configuration.newClient(
306270
{ w: 1 },
307271
{ maxPoolSize: 1, readConcern: { level: 'local' } }
308272
);
309-
client.connect((err, client) => {
310-
expect(err).to.not.exist;
311-
const db = client.db(configuration.db);
312-
expect(db.readConcern).to.deep.equal({ level: 'local' });
313-
314-
// Get a collection using createCollection
315-
db.createCollection('readConcernCollection_createCollection', (err, collection) => {
316-
expect(err).to.not.exist;
317-
318-
// Validate readConcern
319-
expect(collection.readConcern).to.deep.equal({ level: 'local' });
320-
done();
321-
});
322-
});
273+
await client.connect();
274+
const db = client.db(configuration.db);
275+
expect(db.readConcern).to.deep.equal({ level: 'local' });
276+
277+
// Get a collection using createCollection
278+
const collection = await db.createCollection('readConcernCollection_createCollection');
279+
// Validate readConcern
280+
expect(collection.readConcern).to.deep.equal({ level: 'local' });
323281
}
324282
});
325283
});

0 commit comments

Comments
 (0)