1
+ import { expect } from 'chai' ;
2
+
1
3
import { CommandStartedEvent , Db , MongoClient } from '../../mongodb' ;
2
4
3
- describe ( 'RunCommand API ' , ( ) => {
5
+ describe ( 'class RunCommandCursor ' , ( ) => {
4
6
let client : MongoClient ;
5
7
let db : Db ;
6
8
let commandsStarted : CommandStartedEvent [ ] ;
@@ -21,31 +23,31 @@ describe('RunCommand API', () => {
21
23
await client . close ( ) ;
22
24
} ) ;
23
25
24
- it ( 'should not rerun init command' , async ( ) => {
26
+ it ( 'should only run init command once ' , async ( ) => {
25
27
const cursor = db . runCursorCommand ( { find : 'collection' , filter : { } , batchSize : 1 } ) ;
26
- cursor . batchSize = 1 ;
28
+ cursor . setBatchSize ( 1 ) ;
27
29
const it0 = cursor [ Symbol . asyncIterator ] ( ) ;
28
30
const it1 = cursor [ Symbol . asyncIterator ] ( ) ;
29
31
30
- const doc0_0 = await it0 . next ( ) ;
31
- const doc0_1 = await it1 . next ( ) ;
32
- const doc1_0 = await it0 . next ( ) ;
33
- const done = await it1 . next ( ) ;
34
- const done2 = await it0 . next ( ) ;
35
- const done3 = await it1 . next ( ) ;
32
+ const next0it0 = await it0 . next ( ) ; // find, 1 doc
33
+ const next0it1 = await it1 . next ( ) ; // getMore, 1 doc
36
34
37
- // console.log(doc0_0, doc0_1, doc1_0, done, done2, done3);
38
- // console.log(commandsStarted);
39
- } ) ;
35
+ expect ( next0it0 ) . to . deep . equal ( { value : { _id : 0 } , done : false } ) ;
36
+ expect ( next0it1 ) . to . deep . equal ( { value : { _id : 1 } , done : false } ) ;
37
+ expect ( commandsStarted . map ( c => c . commandName ) ) . to . have . lengthOf ( 2 ) ;
38
+
39
+ const next1it0 = await it0 . next ( ) ; // getMore, 1 doc
40
+ const next1it1 = await it1 . next ( ) ; // getMore, 0 doc & exhausted id
41
+
42
+ expect ( next1it0 ) . to . deep . equal ( { value : { _id : 2 } , done : false } ) ;
43
+ expect ( next1it1 ) . to . deep . equal ( { value : undefined , done : true } ) ;
44
+ expect ( commandsStarted . map ( c => c . commandName ) ) . to . have . lengthOf ( 4 ) ;
45
+
46
+ const next2it0 = await it0 . next ( ) ;
47
+ const next2it1 = await it1 . next ( ) ;
40
48
41
- it ( 'should be not strange' , async ( ) => {
42
- // const cursor = db.runCursorCommand({ find: 'collection', filter: {}, batchSize: 1 });
43
- // for await (const doc0 of cursor) {
44
- // for await (const doc1 of cursor) {
45
- // for await (const doc2 of cursor) {
46
- // console.log(doc0, doc1, doc2);
47
- // }
48
- // }
49
- // }
49
+ expect ( next2it0 ) . to . deep . equal ( { value : undefined , done : true } ) ;
50
+ expect ( next2it1 ) . to . deep . equal ( { value : undefined , done : true } ) ;
51
+ expect ( commandsStarted . map ( c => c . commandName ) ) . to . have . lengthOf ( 4 ) ;
50
52
} ) ;
51
53
} ) ;
0 commit comments