@@ -5,12 +5,14 @@ import { Transform } from 'stream';
55import { inspect } from 'util' ;
66
77import {
8+ AbstractCursor ,
89 type Collection ,
910 type FindCursor ,
1011 MongoAPIError ,
1112 type MongoClient ,
1213 MongoCursorExhaustedError ,
13- MongoServerError
14+ MongoServerError ,
15+ GetMoreOperation
1416} from '../../mongodb' ;
1517
1618describe ( 'class AbstractCursor' , function ( ) {
@@ -337,6 +339,7 @@ describe('class AbstractCursor', function () {
337339 describe ( 'when the last document has been iterated' , ( ) => {
338340 it ( 'has a zero id and is closed and is never killed' , async function ( ) {
339341 cursor = client . db ( ) . collection ( 'test' ) . find ( { } ) ;
342+ expect ( cursor ) . to . have . property ( 'closed' , false ) ;
340343 await cursor . next ( ) ;
341344 await cursor . next ( ) ;
342345 await cursor . next ( ) ;
@@ -361,4 +364,39 @@ describe('class AbstractCursor', function () {
361364 } ) ;
362365 } ) ;
363366 } ) ;
367+
368+ describe . only ( 'toArray' , ( ) => {
369+ let nextSpy ;
370+ let getMoreSpy ;
371+ let client : MongoClient ;
372+ let cursor : AbstractCursor ;
373+ let col : Collection ;
374+ const numBatches = 10 ;
375+ const batchSize = 4 ;
376+
377+ beforeEach ( async function ( ) {
378+ client = this . configuration . newClient ( ) ;
379+ col = client . db ( ) . collection ( 'test' ) ;
380+ await col . deleteMany ( { } ) ;
381+ for ( let i = 0 ; i < numBatches ; i ++ ) {
382+ await col . insertMany ( [ { a : 1 } , { a : 2 } , { a : 3 } , { a : 4 } ] ) ;
383+ }
384+ nextSpy = sinon . spy ( AbstractCursor . prototype , 'next' ) ;
385+ getMoreSpy = sinon . spy ( GetMoreOperation . prototype , 'execute' ) ;
386+ } ) ;
387+
388+ afterEach ( async function ( ) {
389+ sinon . restore ( ) ;
390+ await cursor . close ( ) ;
391+ await client . close ( ) ;
392+ } ) ;
393+
394+ it ( 'iterates per batch not per document' , async ( ) => {
395+ cursor = client . db ( ) . collection ( 'test' ) . find ( { } , { batchSize } ) ;
396+ await cursor . toArray ( ) ;
397+ expect ( nextSpy . callCount ) . to . equal ( numBatches + 1 ) ;
398+ const numDocuments = numBatches * batchSize ;
399+ expect ( nextSpy . callCount ) . to . be . lessThan ( numDocuments ) ;
400+ } ) ;
401+ } ) ;
364402} ) ;
0 commit comments