1
1
import { expect } from 'chai' ;
2
- import * as sinon from 'sinon' ;
3
2
4
3
import {
5
4
Aspect ,
6
- ClientSession ,
7
5
GetMoreOperation ,
8
6
Long ,
9
- MongoRuntimeError ,
10
7
ns ,
11
8
ReadPreference ,
12
9
Server ,
@@ -23,10 +20,6 @@ describe('GetMoreOperation', function () {
23
20
readPreference : ReadPreference . primary
24
21
} ;
25
22
26
- afterEach ( function ( ) {
27
- sinon . restore ( ) ;
28
- } ) ;
29
-
30
23
describe ( '#constructor' , function ( ) {
31
24
const topology = topologyWithPlaceholderClient ( [ ] , { } as any ) ;
32
25
const server = new Server ( topology , new ServerDescription ( 'a:1' ) , { } as any ) ;
@@ -45,161 +38,24 @@ describe('GetMoreOperation', function () {
45
38
} ) ;
46
39
} ) ;
47
40
48
- describe ( '#execute' , function ( ) {
49
- context ( 'when the server is the same as the instance' , function ( ) {
50
- it ( 'executes a getMore on the provided server' , async function ( ) {
51
- const server = new Server (
52
- topologyWithPlaceholderClient ( [ ] , { } as any ) ,
53
- new ServerDescription ( 'a:1' ) ,
54
- { } as any
55
- ) ;
56
- const opts = {
57
- ...options ,
58
- documentsReturnedIn : 'nextBatch' ,
59
- returnFieldSelector : null ,
60
- timeoutContext : undefined
61
- } ;
62
- const operation = new GetMoreOperation ( namespace , cursorId , server , opts ) ;
63
- const stub = sinon . stub ( server , 'command' ) . resolves ( { } ) ;
64
-
65
- const expectedGetMoreCommand = {
66
- getMore : cursorId ,
67
- collection : namespace . collection ,
68
- batchSize : 100 ,
69
- maxTimeMS : 500
70
- } ;
71
-
72
- await operation . execute ( server , undefined ) ;
73
- expect ( stub . calledOnce ) . to . be . true ;
74
- const call = stub . getCall ( 0 ) ;
75
- expect ( call . args [ 0 ] ) . to . equal ( namespace ) ;
76
- expect ( call . args [ 1 ] ) . to . deep . equal ( expectedGetMoreCommand ) ;
77
- expect ( call . args [ 2 ] ) . to . deep . equal ( opts ) ;
78
- } ) ;
79
- } ) ;
80
-
81
- context ( 'when the server is not the same as the instance' , function ( ) {
82
- it ( 'errors in the callback' , async function ( ) {
83
- const server1 = new Server (
84
- topologyWithPlaceholderClient ( [ ] , { } as any ) ,
85
- new ServerDescription ( 'a:1' ) ,
86
- { } as any
87
- ) ;
88
- const server2 = new Server (
89
- topologyWithPlaceholderClient ( [ ] , { } as any ) ,
90
- new ServerDescription ( 'a:1' ) ,
91
- { } as any
92
- ) ;
93
- const session = sinon . createStubInstance ( ClientSession ) ;
94
- const opts = { ...options , session } ;
95
- const operation = new GetMoreOperation ( namespace , cursorId , server1 , opts ) ;
96
- const error = await operation . execute ( server2 , session ) . catch ( error => error ) ;
97
- expect ( error ) . to . be . instanceOf ( MongoRuntimeError ) ;
98
- expect ( error . message ) . to . equal ( 'Getmore must run on the same server operation began on' ) ;
99
- } ) ;
100
- } ) ;
101
-
102
- context ( 'command construction' , ( ) => {
103
- const cursorId = Long . fromBigInt ( 0xffff_ffffn ) ;
104
- const namespace = ns ( 'db.collection' ) ;
105
- const server = new Server (
106
- topologyWithPlaceholderClient ( [ ] , { } as any ) ,
107
- new ServerDescription ( 'a:1' ) ,
108
- { } as any
109
- ) ;
110
-
111
- it ( 'should build basic getMore command with cursorId and collection' , async ( ) => {
112
- const getMoreOperation = new GetMoreOperation ( namespace , cursorId , server , { } ) ;
113
- const stub = sinon . stub ( server , 'command' ) . resolves ( { } ) ;
114
- await getMoreOperation . execute ( server , undefined ) ;
115
- expect ( stub ) . to . have . been . calledOnceWith ( namespace , {
116
- getMore : cursorId ,
117
- collection : namespace . collection
118
- } ) ;
119
- } ) ;
120
-
121
- it ( 'should build getMore command with batchSize' , async ( ) => {
122
- const options = {
123
- batchSize : 234
124
- } ;
125
- const getMoreOperation = new GetMoreOperation ( namespace , cursorId , server , options ) ;
126
- const stub = sinon . stub ( server , 'command' ) . resolves ( { } ) ;
127
- await getMoreOperation . execute ( server , undefined ) ;
128
- expect ( stub ) . to . have . been . calledOnceWith (
129
- namespace ,
130
- sinon . match . has ( 'batchSize' , options . batchSize )
131
- ) ;
132
- } ) ;
133
-
134
- it ( 'should build getMore command with maxTimeMS if maxAwaitTimeMS specified' , async ( ) => {
135
- const options = {
136
- maxAwaitTimeMS : 234
137
- } ;
138
- const getMoreOperation = new GetMoreOperation ( namespace , cursorId , server , options ) ;
139
- const stub = sinon . stub ( server , 'command' ) . resolves ( { } ) ;
140
- await getMoreOperation . execute ( server , undefined ) ;
141
- expect ( stub ) . to . have . been . calledOnceWith (
142
- namespace ,
143
- sinon . match . has ( 'maxTimeMS' , options . maxAwaitTimeMS )
144
- ) ;
145
- } ) ;
146
-
147
- context ( 'comment' , function ( ) {
148
- const optionsWithComment = {
149
- ...options ,
150
- comment : 'test'
151
- } ;
41
+ context ( 'command construction' , ( ) => {
42
+ const cursorId = Long . fromBigInt ( 0xffff_ffffn ) ;
43
+ const namespace = ns ( 'db.collection' ) ;
44
+ const server = new Server (
45
+ topologyWithPlaceholderClient ( [ ] , { } as any ) ,
46
+ new ServerDescription ( 'a:1' ) ,
47
+ { } as any
48
+ ) ;
152
49
153
- const serverVersions = [
154
- {
155
- serverVersion : 8 ,
156
- getMore : {
157
- getMore : cursorId ,
158
- collection : namespace . collection ,
159
- batchSize : 100 ,
160
- maxTimeMS : 500
161
- }
162
- } ,
163
- {
164
- serverVersion : 9 ,
165
- getMore : {
166
- getMore : cursorId ,
167
- collection : namespace . collection ,
168
- batchSize : 100 ,
169
- maxTimeMS : 500 ,
170
- comment : 'test'
171
- }
172
- } ,
173
- {
174
- serverVersion : 10 ,
175
- getMore : {
176
- getMore : cursorId ,
177
- collection : namespace . collection ,
178
- batchSize : 100 ,
179
- maxTimeMS : 500 ,
180
- comment : 'test'
181
- }
182
- }
183
- ] ;
184
- for ( const { serverVersion, getMore } of serverVersions ) {
185
- const verb = serverVersion < 9 ? 'does not' : 'does' ;
186
- const state = serverVersion < 9 ? 'less than 9' : 'greater than or equal to 9' ;
187
- it ( `${ verb } set the comment on the command if the server wire version is ${ state } ` , async ( ) => {
188
- const server = new Server (
189
- topologyWithPlaceholderClient ( [ ] , { } as any ) ,
190
- new ServerDescription ( 'a:1' ) ,
191
- { } as any
192
- ) ;
193
- server . hello = {
194
- maxWireVersion : serverVersion
195
- } ;
196
- const operation = new GetMoreOperation ( namespace , cursorId , server , optionsWithComment ) ;
197
- const stub = sinon . stub ( server , 'command' ) . resolves ( { } ) ;
198
- await operation . execute ( server , undefined ) ;
199
- expect ( stub ) . to . have . been . calledOnceWith ( namespace , getMore ) ;
200
- } ) ;
201
- }
202
- } ) ;
50
+ it ( 'should build getMore command with maxTimeMS if maxAwaitTimeMS specified' , async ( ) => {
51
+ const options = {
52
+ maxAwaitTimeMS : 234
53
+ } ;
54
+ const getMoreOperation = new GetMoreOperation ( namespace , cursorId , server , options ) ;
55
+ const { maxTimeMS } = getMoreOperation . buildCommand ( {
56
+ description : { }
57
+ } as any ) ;
58
+ expect ( maxTimeMS ) . to . equal ( 234 ) ;
203
59
} ) ;
204
60
205
61
context ( 'error cases' , ( ) => {
@@ -208,18 +64,20 @@ describe('GetMoreOperation', function () {
208
64
new ServerDescription ( 'a:1' ) ,
209
65
{ } as any
210
66
) ;
211
- sinon . stub ( server , 'command' ) . resolves ( { } ) ;
212
67
213
68
it ( 'should throw if the cursorId is undefined' , async ( ) => {
214
69
const getMoreOperation = new GetMoreOperation (
215
70
ns ( 'db.collection' ) ,
216
- // @ts -expect-error: Testing undefined cursorId
217
71
undefined ,
218
72
server ,
219
73
options
220
74
) ;
221
- const error = await getMoreOperation . execute ( server , undefined ) . catch ( error => error ) ;
222
- expect ( error ) . to . be . instanceOf ( MongoRuntimeError ) ;
75
+ const connection = {
76
+ description : { }
77
+ } as any ;
78
+ expect ( ( ) => {
79
+ getMoreOperation . buildCommand ( connection ) ;
80
+ } ) . to . throw ( / U n a b l e t o i t e r a t e c u r s o r w i t h n o i d / ) ;
223
81
} ) ;
224
82
225
83
it ( 'should throw if the collection is undefined' , async ( ) => {
@@ -229,8 +87,12 @@ describe('GetMoreOperation', function () {
229
87
server ,
230
88
options
231
89
) ;
232
- const error = await getMoreOperation . execute ( server , undefined ) . catch ( error => error ) ;
233
- expect ( error ) . to . be . instanceOf ( MongoRuntimeError ) ;
90
+ const connection = {
91
+ description : { }
92
+ } as any ;
93
+ expect ( ( ) => {
94
+ getMoreOperation . buildCommand ( connection ) ;
95
+ } ) . to . throw ( / A c o l l e c t i o n n a m e m u s t b e d e t e r m i n e d b e f o r e g e t M o r e / ) ;
234
96
} ) ;
235
97
236
98
it ( 'should throw if the cursorId is zero' , async ( ) => {
@@ -240,8 +102,12 @@ describe('GetMoreOperation', function () {
240
102
server ,
241
103
options
242
104
) ;
243
- const error = await getMoreOperation . execute ( server , undefined ) . catch ( error => error ) ;
244
- expect ( error ) . to . be . instanceOf ( MongoRuntimeError ) ;
105
+ const connection = {
106
+ description : { }
107
+ } as any ;
108
+ expect ( ( ) => {
109
+ getMoreOperation . buildCommand ( connection ) ;
110
+ } ) . to . throw ( / U n a b l e t o i t e r a t e c u r s o r w i t h n o i d / ) ;
245
111
} ) ;
246
112
} ) ;
247
113
} ) ;
0 commit comments