Skip to content

Commit 38efad2

Browse files
committed
use withClient for some tests
1 parent dcffd13 commit 38efad2

File tree

2 files changed

+83
-117
lines changed

2 files changed

+83
-117
lines changed

test/functional/ignore_undefined.test.js

Lines changed: 76 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ var test = require('./shared').assert;
33
const { expect } = require('chai');
44
var setupDatabase = require('./shared').setupDatabase;
55
const { ObjectId } = require('../../src');
6+
const withClient = require('./shared').withClient;
67

78
describe('Ignore Undefined', function () {
89
before(function () {
@@ -152,23 +153,23 @@ describe('Ignore Undefined', function () {
152153
it('Should correctly inherit ignore undefined field from db during insert', {
153154
metadata: { requires: { topology: ['single'] } },
154155

155-
test: function (done) {
156+
test: function () {
156157
var configuration = this.configuration;
157158
var client = configuration.newClient(configuration.writeConcernMax(), {
158159
poolSize: 1,
159160
ignoreUndefined: false
160161
});
161162

162-
client.connect(function (err, client) {
163+
return withClient(client, (client, done) => {
163164
var db = client.db(configuration.db, { ignoreUndefined: true });
164165
var collection = db.collection('shouldCorrectlyIgnoreUndefinedValue3');
165166

166167
// Ignore the undefined field
167-
collection.insert({ a: 1, b: undefined }, configuration.writeConcernMax(), function (err) {
168+
collection.insert({ a: 1, b: undefined }, configuration.writeConcernMax(), err => {
168169
expect(err).to.not.exist;
169170

170171
// Locate the doument
171-
collection.findOne(function (err, item) {
172+
collection.findOne((err, item) => {
172173
test.equal(1, item.a);
173174
test.ok(item.b === undefined);
174175
client.close(done);
@@ -178,134 +179,98 @@ describe('Ignore Undefined', function () {
178179
}
179180
});
180181

181-
it('Should correctly inherit ignore undefined field from collection during insert', {
182-
metadata: { requires: { topology: ['single'] } },
183-
184-
test: function (done) {
185-
var configuration = this.configuration;
186-
var client = configuration.newClient(configuration.writeConcernMax(), {
187-
poolSize: 1
182+
it(
183+
'Should correctly inherit ignore undefined field from collection during insert',
184+
withClient(function (client, done) {
185+
var db = client.db('shouldCorrectlyIgnoreUndefinedValue4', { ignoreUndefined: false });
186+
var collection = db.collection('shouldCorrectlyIgnoreUndefinedValue4', {
187+
ignoreUndefined: true
188188
});
189189

190-
client.connect(function (err, client) {
191-
var db = client.db(configuration.db, { ignoreUndefined: false });
192-
var collection = db.collection('shouldCorrectlyIgnoreUndefinedValue4', {
193-
ignoreUndefined: true
194-
});
195-
196-
// Ignore the undefined field
197-
collection.insert({ a: 1, b: undefined }, configuration.writeConcernMax(), function (err) {
198-
expect(err).to.not.exist;
190+
// Ignore the undefined field
191+
collection.insert({ a: 1, b: undefined }, err => {
192+
expect(err).to.not.exist;
199193

200-
// Locate the doument
201-
collection.findOne(function (err, item) {
202-
test.equal(1, item.a);
203-
test.ok(item.b === undefined);
204-
client.close(done);
205-
});
194+
// Locate the doument
195+
collection.findOne((err, item) => {
196+
test.equal(1, item.a);
197+
test.ok(item.b === undefined);
198+
done();
206199
});
207200
});
208-
}
209-
});
210-
211-
it('Should correctly inherit ignore undefined field from operation during insert', {
212-
metadata: { requires: { topology: ['single'] } },
201+
})
202+
);
213203

214-
test: function (done) {
215-
var configuration = this.configuration;
216-
var client = configuration.newClient(configuration.writeConcernMax(), {
217-
poolSize: 1
204+
it(
205+
'Should correctly inherit ignore undefined field from operation during insert',
206+
withClient(function (client, done) {
207+
var db = client.db('shouldCorrectlyIgnoreUndefinedValue5');
208+
var collection = db.collection('shouldCorrectlyIgnoreUndefinedValue5', {
209+
ignoreUndefined: false
218210
});
219211

220-
client.connect(function (err, client) {
221-
var db = client.db(configuration.db);
222-
var collection = db.collection('shouldCorrectlyIgnoreUndefinedValue5', {
223-
ignoreUndefined: false
224-
});
212+
// Ignore the undefined field
213+
collection.insert({ a: 1, b: undefined }, { ignoreUndefined: true }, err => {
214+
expect(err).to.not.exist;
225215

226-
// Ignore the undefined field
227-
collection.insert({ a: 1, b: undefined }, { ignoreUndefined: true }, function (err) {
216+
// Locate the doument
217+
collection.findOne({}, (err, item) => {
228218
expect(err).to.not.exist;
229-
230-
// Locate the doument
231-
collection.findOne({}, function (err, item) {
232-
expect(err).to.not.exist;
233-
test.equal(1, item.a);
234-
test.ok(item.b === undefined);
235-
client.close(done);
236-
});
219+
test.equal(1, item.a);
220+
test.ok(item.b === undefined);
221+
done();
237222
});
238223
});
239-
}
240-
});
241-
242-
it('Should correctly inherit ignore undefined field from operation during findOneAndReplace', {
243-
metadata: { requires: { topology: ['single'] } },
224+
})
225+
);
244226

245-
test: function (done) {
246-
var configuration = this.configuration;
247-
var client = configuration.newClient(configuration.writeConcernMax(), {
248-
poolSize: 1,
227+
it(
228+
'Should correctly inherit ignore undefined field from operation during findOneAndReplace',
229+
withClient(function (client, done) {
230+
var db = client.db('shouldCorrectlyIgnoreUndefinedValue6');
231+
var collection = db.collection('shouldCorrectlyIgnoreUndefinedValue6', {
249232
ignoreUndefined: false
250233
});
251234

252-
client.connect(function (err, client) {
253-
var db = client.db(configuration.db);
254-
var collection = db.collection('shouldCorrectlyIgnoreUndefinedValue6');
235+
collection.insert({ a: 1, b: 2 }, err => {
236+
expect(err).to.not.exist;
255237

256-
collection.insert({ a: 1, b: 2 }, configuration.writeConcernMax(), function (err) {
238+
// Replace the doument, ignoring undefined fields
239+
collection.findOneAndReplace({}, { a: 1, b: undefined }, { ignoreUndefined: true }, err => {
257240
expect(err).to.not.exist;
258241

259-
// Replace the doument, ignoring undefined fields
260-
collection.findOneAndReplace(
261-
{},
262-
{ a: 1, b: undefined },
263-
{ ignoreUndefined: true },
264-
function (err) {
265-
expect(err).to.not.exist;
266-
267-
// Locate the doument
268-
collection.findOne(function (err, item) {
269-
test.equal(1, item.a);
270-
test.ok(item.b === undefined);
271-
client.close(done);
272-
});
273-
}
274-
);
242+
// Locate the doument
243+
collection.findOne((err, item) => {
244+
test.equal(1, item.a);
245+
test.ok(item.b === undefined);
246+
done();
247+
});
275248
});
276249
});
277-
}
278-
});
279-
280-
it('Should correctly ignore undefined field during bulk write', {
281-
metadata: { requires: { topology: ['single'] } },
282-
283-
test: function (done) {
284-
var configuration = this.configuration;
285-
var client = configuration.newClient(configuration.writeConcernMax(), {
286-
poolSize: 1
287-
});
288-
289-
client.connect(function (err, client) {
290-
var db = client.db(configuration.db);
291-
var collection = db.collection('shouldCorrectlyIgnoreUndefinedValue7');
250+
})
251+
);
292252

293-
// Ignore the undefined field
294-
collection.bulkWrite(
295-
[{ insertOne: { a: 0, b: undefined } }],
296-
{ ignoreUndefined: true },
297-
function (err) {
298-
expect(err).to.not.exist;
253+
it(
254+
'Should correctly ignore undefined field during bulk write',
255+
withClient(function (client, done) {
256+
var db = client.db('shouldCorrectlyIgnoreUndefinedValue7');
257+
var collection = db.collection('shouldCorrectlyIgnoreUndefinedValue7');
258+
259+
// Ignore the undefined field
260+
collection.bulkWrite(
261+
[{ insertOne: { a: 0, b: undefined } }],
262+
{ ignoreUndefined: true },
263+
err => {
264+
expect(err).to.not.exist;
299265

300-
// Locate the doument
301-
collection.findOne(function (err, item) {
302-
test.equal(0, item.a);
303-
test.ok(item.b === undefined);
304-
client.close(done);
305-
});
306-
}
307-
);
308-
});
309-
}
310-
});
266+
// Locate the doument
267+
collection.findOne((err, item) => {
268+
test.equal(0, item.a);
269+
test.ok(item.b === undefined);
270+
done();
271+
});
272+
}
273+
);
274+
})
275+
);
311276
});

test/functional/insert.test.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
'use strict';
2-
const { assert: test } = require('./shared');
2+
const { assert: test, withClient } = require('./shared');
33
const { setupDatabase } = require('./shared');
44
const Script = require('vm');
55
const { expect } = require('chai');
@@ -1909,14 +1909,15 @@ describe('Insert', function () {
19091909
requires: { topology: ['single', 'replicaset', 'sharded', 'ssl', 'heap', 'wiredtiger'] }
19101910
},
19111911

1912-
test: function (done) {
1912+
test: function () {
19131913
var configuration = this.configuration;
19141914

19151915
var client = configuration.newClient(configuration.writeConcernMax(), {
19161916
poolSize: 1,
19171917
promoteLongs: true
19181918
});
1919-
client.connect(function (err, client) {
1919+
1920+
return withClient(client, (client, done) => {
19201921
var db = client.db(configuration.db, { promoteLongs: false });
19211922
db.collection('shouldCorrectlyInheritPromoteLongFalseNativeBSONWithGetMore').insertMany(
19221923
[
@@ -1945,19 +1946,19 @@ describe('Insert', function () {
19451946
{ a: Long.fromNumber(10) },
19461947
{ a: Long.fromNumber(10) }
19471948
],
1948-
function (err, doc) {
1949+
(err, doc) => {
19491950
expect(err).to.not.exist;
19501951
test.ok(doc);
19511952

19521953
db.collection('shouldCorrectlyInheritPromoteLongFalseNativeBSONWithGetMore')
19531954
.find({})
19541955
.batchSize(2)
1955-
.toArray(function (err, docs) {
1956+
.toArray((err, docs) => {
19561957
expect(err).to.not.exist;
19571958
var doc = docs.pop();
19581959

19591960
test.ok(doc.a._bsontype === 'Long');
1960-
client.close(done);
1961+
done();
19611962
});
19621963
}
19631964
);

0 commit comments

Comments
 (0)