Skip to content

Commit ae08fb5

Browse files
authored
test(NODE-7197): migrate integration/crud/find_cursor_methods tests (#4727)
1 parent d14ac3f commit ae08fb5

File tree

1 file changed

+80
-122
lines changed

1 file changed

+80
-122
lines changed

test/integration/crud/find_cursor_methods.test.js renamed to test/integration/crud/find_cursor_methods.test.ts

Lines changed: 80 additions & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
1-
'use strict';
2-
const { expect } = require('chai');
3-
const { filterForCommands } = require('../shared');
4-
const {
5-
promiseWithResolvers,
6-
MongoCursorExhaustedError,
7-
CursorTimeoutContext,
8-
TimeoutContext,
9-
MongoAPIError
10-
} = require('../../mongodb');
1+
import { expect } from 'chai';
2+
3+
import {
4+
type Collection,
5+
type FindCursor,
6+
MongoAPIError,
7+
type MongoClient,
8+
MongoCursorExhaustedError
9+
} from '../../../src';
10+
import { CursorTimeoutContext } from '../../../src/cursor/abstract_cursor';
11+
import { TimeoutContext } from '../../../src/timeout';
12+
import { promiseWithResolvers } from '../../../src/utils';
13+
import { filterForCommands } from '../shared';
1114

1215
describe('Find Cursor', function () {
13-
let client;
16+
let client: MongoClient;
1417

1518
beforeEach(async function () {
1619
const setupClient = this.configuration.newClient();
@@ -32,20 +35,16 @@ describe('Find Cursor', function () {
3235
});
3336

3437
context('#next', function () {
35-
it('should support a batch size', function (done) {
38+
it('should support a batch size', async function () {
3639
const commands = [];
3740
client.on('commandStarted', filterForCommands(['getMore'], commands));
3841

3942
const coll = client.db().collection('abstract_cursor');
4043
const cursor = coll.find({}, { batchSize: 2 });
41-
this.defer(() => cursor.close());
4244

43-
cursor.toArray((err, docs) => {
44-
expect(err).to.not.exist;
45-
expect(docs).to.have.length(6);
46-
expect(commands).to.have.length(3);
47-
done();
48-
});
45+
const docs = await cursor.toArray();
46+
expect(docs).to.have.length(6);
47+
expect(commands).to.have.length(3);
4948
});
5049
});
5150

@@ -174,50 +173,36 @@ describe('Find Cursor', function () {
174173
});
175174

176175
context('#forEach', function () {
177-
it('should iterate each document in a cursor', function (done) {
176+
it('should iterate each document in a cursor', async function () {
178177
const coll = client.db().collection('abstract_cursor');
179178
const cursor = coll.find({}, { batchSize: 2 });
180179

181180
const bag = [];
182-
cursor.forEach(
183-
doc => bag.push(doc),
184-
err => {
185-
expect(err).to.not.exist;
186-
expect(bag).to.have.lengthOf(6);
187-
done();
188-
}
189-
);
181+
await cursor.forEach(doc => {
182+
bag.push(doc);
183+
});
184+
185+
expect(bag).to.have.lengthOf(6);
190186
});
191187
});
192188

193189
context('#tryNext', function () {
194-
it('should return control to the user if an empty batch is returned', function (done) {
190+
it('should return control to the user if an empty batch is returned', async function () {
195191
const db = client.db();
196-
db.createCollection('try_next', { capped: true, size: 10000000 }, () => {
197-
const coll = db.collection('try_next');
198-
coll.insertMany([{}, {}], err => {
199-
expect(err).to.not.exist;
200-
201-
const cursor = coll.find({}, { tailable: true, awaitData: true });
202-
this.defer(() => cursor.close());
203-
204-
cursor.tryNext((err, doc) => {
205-
expect(err).to.not.exist;
206-
expect(doc).to.exist;
207-
208-
cursor.tryNext((err, doc) => {
209-
expect(err).to.not.exist;
210-
expect(doc).to.exist;
211-
212-
cursor.tryNext((err, doc) => {
213-
expect(err).to.not.exist;
214-
expect(doc).to.be.null;
215-
done();
216-
});
217-
});
218-
});
219-
});
220-
});
192+
await db.createCollection('try_next', { capped: true, size: 10000000 });
193+
const coll = db.collection('try_next');
194+
await coll.insertMany([{}, {}]);
195+
196+
const cursor = coll.find({}, { tailable: true, awaitData: true });
197+
198+
const doc1 = await cursor.tryNext();
199+
expect(doc1).to.exist;
200+
201+
const doc2 = await cursor.tryNext();
202+
expect(doc2).to.exist;
203+
204+
const doc3 = await cursor.tryNext();
205+
expect(doc3).to.be.null;
221206
});
222207
});
223208

@@ -293,51 +278,36 @@ describe('Find Cursor', function () {
293278
}
294279
});
295280

296-
it('should end an implicit session on rewind', {
297-
metadata: { requires: { mongodb: '>=3.6' } },
298-
test: function (done) {
299-
const coll = client.db().collection('abstract_cursor');
300-
const cursor = coll.find({}, { batchSize: 1 });
301-
this.defer(() => cursor.close());
302-
303-
cursor.next((err, doc) => {
304-
expect(err).to.not.exist;
305-
expect(doc).to.exist;
306-
307-
const session = cursor.session;
308-
expect(session).property('hasEnded').to.be.false;
309-
cursor.rewind();
310-
expect(session).property('hasEnded').to.be.true;
311-
done();
312-
});
313-
}
314-
});
281+
it('should end an implicit session on rewind', async function () {
282+
const coll = client.db().collection('abstract_cursor');
283+
const cursor = coll.find({}, { batchSize: 1 });
315284

316-
it('should not end an explicit session on rewind', {
317-
metadata: { requires: { mongodb: '>=3.6' } },
318-
test: function (done) {
319-
const coll = client.db().collection('abstract_cursor');
320-
const session = client.startSession();
285+
const doc = await cursor.next();
286+
expect(doc).to.exist;
321287

322-
const cursor = coll.find({}, { batchSize: 1, session });
323-
this.defer(() => cursor.close());
288+
const session = cursor.session;
289+
expect(session).property('hasEnded').to.be.false;
290+
cursor.rewind();
291+
expect(session).property('hasEnded').to.be.true;
292+
});
324293

325-
cursor.next((err, doc) => {
326-
expect(err).to.not.exist;
327-
expect(doc).to.exist;
294+
it('should not end an explicit session on rewind', async function () {
295+
const coll = client.db().collection('abstract_cursor');
296+
const cursor = coll.find({}, { batchSize: 1, session: client.startSession() });
328297

329-
const session = cursor.session;
330-
expect(session).property('hasEnded').to.be.false;
331-
cursor.rewind();
332-
expect(session).property('hasEnded').to.be.false;
298+
const doc = await cursor.next();
299+
expect(doc).to.exist;
333300

334-
session.endSession(done);
335-
});
336-
}
301+
const session = cursor.session;
302+
expect(session).property('hasEnded').to.be.false;
303+
cursor.rewind();
304+
expect(session).property('hasEnded').to.be.false;
305+
306+
await session.endSession();
337307
});
338308

339309
it('emits close after rewind', async () => {
340-
let cursor;
310+
let cursor: FindCursor;
341311
try {
342312
const coll = client.db().collection('abstract_cursor');
343313
cursor = coll.find({}, { batchSize: 1 });
@@ -359,41 +329,33 @@ describe('Find Cursor', function () {
359329
context('#allowDiskUse', function () {
360330
it('should set allowDiskUse to true by default', {
361331
metadata: { requires: { mongodb: '>=4.4' } },
362-
test: function (done) {
332+
test: async function () {
363333
const commands = [];
364334
client.on('commandStarted', filterForCommands(['find'], commands));
365335

366336
const coll = client.db().collection('abstract_cursor');
367337
const cursor = coll.find({}, { sort: 'foo' });
368338
cursor.allowDiskUse();
369-
this.defer(() => cursor.close());
370339

371-
cursor.toArray(err => {
372-
expect(err).to.not.exist;
373-
expect(commands).to.have.length(1);
374-
expect(commands[0].command.allowDiskUse).to.equal(true);
375-
done();
376-
});
340+
await cursor.toArray();
341+
expect(commands).to.have.length(1);
342+
expect(commands[0].command.allowDiskUse).to.equal(true);
377343
}
378344
});
379345

380346
it('should set allowDiskUse to false if specified', {
381347
metadata: { requires: { mongodb: '>=4.4' } },
382-
test: function (done) {
348+
test: async function () {
383349
const commands = [];
384350
client.on('commandStarted', filterForCommands(['find'], commands));
385351

386352
const coll = client.db().collection('abstract_cursor');
387353
const cursor = coll.find({}, { sort: 'foo' });
388354
cursor.allowDiskUse(false);
389-
this.defer(() => cursor.close());
390355

391-
cursor.toArray(err => {
392-
expect(err).to.not.exist;
393-
expect(commands).to.have.length(1);
394-
expect(commands[0].command.allowDiskUse).to.equal(false);
395-
done();
396-
});
356+
await cursor.toArray();
357+
expect(commands).to.have.length(1);
358+
expect(commands[0].command.allowDiskUse).to.equal(false);
397359
}
398360
});
399361

@@ -411,9 +373,9 @@ describe('Find Cursor', function () {
411373
});
412374

413375
describe('mixing iteration APIs', function () {
414-
let client;
415-
let collection;
416-
let cursor;
376+
let client: MongoClient;
377+
let collection: Collection;
378+
let cursor: FindCursor;
417379

418380
beforeEach(async function () {
419381
client = this.configuration.newClient();
@@ -437,7 +399,7 @@ describe('Find Cursor', function () {
437399
});
438400

439401
await cursor.next();
440-
// eslint-disable-next-line no-unused-vars
402+
441403
for await (const _ of cursor) {
442404
/* empty */
443405
}
@@ -454,8 +416,7 @@ describe('Find Cursor', function () {
454416

455417
await cursor.next();
456418

457-
let doc;
458-
while ((doc = (await cursor.next()) && doc != null)) {
419+
while ((await cursor.next()) != null) {
459420
/** empty */
460421
}
461422

@@ -472,8 +433,7 @@ describe('Find Cursor', function () {
472433

473434
await cursor.next();
474435

475-
let doc;
476-
while ((doc = (await cursor.next()) && doc != null)) {
436+
while ((await cursor.next()) != null) {
477437
/** empty */
478438
}
479439

@@ -543,7 +503,7 @@ describe('Find Cursor', function () {
543503
await promise;
544504

545505
let count = 0;
546-
// eslint-disable-next-line no-unused-vars
506+
547507
for await (const _ of cursor) {
548508
count++;
549509
}
@@ -587,7 +547,7 @@ describe('Find Cursor', function () {
587547
cursor = collection.find({});
588548

589549
await cursor.toArray();
590-
// eslint-disable-next-line no-unused-vars
550+
591551
for await (const _ of cursor) {
592552
expect.fail('should not iterate');
593553
}
@@ -633,7 +593,7 @@ describe('Find Cursor', function () {
633593
});
634594

635595
await cursor.next();
636-
// eslint-disable-next-line no-unused-vars
596+
637597
for await (const _ of cursor) {
638598
/* empty */
639599
}
@@ -647,7 +607,6 @@ describe('Find Cursor', function () {
647607
it('throws a MongoCursorExhaustedError', async function () {
648608
cursor = collection.find({}, { batchSize: 1 });
649609

650-
// eslint-disable-next-line no-unused-vars
651610
for await (const _ of cursor) {
652611
/* empty */
653612
break;
@@ -672,8 +631,7 @@ describe('Find Cursor', function () {
672631

673632
await cursor.next();
674633

675-
let doc;
676-
while ((doc = (await cursor.next()) && doc != null)) {
634+
while ((await cursor.next()) != null) {
677635
/** empty */
678636
}
679637

@@ -715,7 +673,7 @@ describe('Find Cursor', function () {
715673
cursor = collection.find({}, { batchSize: 1 });
716674

717675
await cursor.toArray();
718-
// eslint-disable-next-line no-unused-vars
676+
719677
for await (const _ of cursor) {
720678
expect.fail('should not iterate');
721679
}

0 commit comments

Comments
 (0)