Skip to content

Commit f888cd3

Browse files
add test
1 parent 6abd5f4 commit f888cd3

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

test/integration/node-specific/abstract_cursor.test.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { Transform } from 'stream';
55
import { inspect } from 'util';
66

77
import {
8+
AbstractCursor,
89
type Collection,
910
type FindCursor,
1011
MongoAPIError,
@@ -361,4 +362,37 @@ describe('class AbstractCursor', function () {
361362
});
362363
});
363364
});
365+
366+
describe('toArray', () => {
367+
let nextSpy;
368+
let client: MongoClient;
369+
let cursor: AbstractCursor;
370+
let col: Collection;
371+
const numBatches = 10;
372+
const batchSize = 4;
373+
374+
beforeEach(async function () {
375+
client = this.configuration.newClient();
376+
col = client.db().collection('test');
377+
await col.deleteMany({});
378+
for (let i = 0; i < numBatches; i++) {
379+
await col.insertMany([{ a: 1 }, { a: 2 }, { a: 3 }, { a: 4 }]);
380+
}
381+
nextSpy = sinon.spy(AbstractCursor.prototype, 'next');
382+
});
383+
384+
afterEach(async function () {
385+
sinon.restore();
386+
await cursor.close();
387+
await client.close();
388+
});
389+
390+
it('iterates per batch not per document', async () => {
391+
cursor = client.db().collection('test').find({}, { batchSize });
392+
await cursor.toArray();
393+
expect(nextSpy.callCount).to.equal(numBatches + 1);
394+
const numDocuments = numBatches * batchSize;
395+
expect(nextSpy.callCount).to.be.lessThan(numDocuments);
396+
});
397+
});
364398
});

0 commit comments

Comments
 (0)