@@ -5,12 +5,14 @@ import { Transform } from 'stream';
5
5
import { inspect } from 'util' ;
6
6
7
7
import {
8
+ AbstractCursor ,
8
9
type Collection ,
9
10
type FindCursor ,
10
11
MongoAPIError ,
11
12
type MongoClient ,
12
13
MongoCursorExhaustedError ,
13
- MongoServerError
14
+ MongoServerError ,
15
+ GetMoreOperation
14
16
} from '../../mongodb' ;
15
17
16
18
describe ( 'class AbstractCursor' , function ( ) {
@@ -337,6 +339,7 @@ describe('class AbstractCursor', function () {
337
339
describe ( 'when the last document has been iterated' , ( ) => {
338
340
it ( 'has a zero id and is closed and is never killed' , async function ( ) {
339
341
cursor = client . db ( ) . collection ( 'test' ) . find ( { } ) ;
342
+ expect ( cursor ) . to . have . property ( 'closed' , false ) ;
340
343
await cursor . next ( ) ;
341
344
await cursor . next ( ) ;
342
345
await cursor . next ( ) ;
@@ -361,4 +364,39 @@ describe('class AbstractCursor', function () {
361
364
} ) ;
362
365
} ) ;
363
366
} ) ;
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
+ } ) ;
364
402
} ) ;
0 commit comments