@@ -4,14 +4,13 @@ import { Collection, Db, GridFSFile, MongoClient, ObjectId, AbstractCursor } fro
4
4
import { ReadConcern } from '../../../src/read_concern' ;
5
5
import { ReadPreference } from '../../../src/read_preference' ;
6
6
import { WriteConcern } from '../../../src/write_concern' ;
7
- import { Document , InsertOneOptions } from '../../../src' ;
7
+ import { Document } from '../../../src' ;
8
8
import { EventCollector } from '../../tools/utils' ;
9
9
import { EntitiesMap } from './entities' ;
10
10
import { expectErrorCheck , resultCheck } from './match' ;
11
11
import type { OperationDescription } from './schema' ;
12
12
import { CommandStartedEvent } from '../../../src/cmap/command_monitoring_events' ;
13
13
import { translateOptions } from './unified-utils' ;
14
- import { getSymbolFrom } from '../../tools/utils' ;
15
14
16
15
interface OperationFunctionParams {
17
16
client : MongoClient ;
@@ -22,18 +21,6 @@ interface OperationFunctionParams {
22
21
type RunOperationFn = ( p : OperationFunctionParams ) => Promise < Document | boolean | number | void > ;
23
22
export const operations = new Map < string , RunOperationFn > ( ) ;
24
23
25
- function executeWithPotentialSession (
26
- entities : EntitiesMap ,
27
- operation : OperationDescription ,
28
- cursor : AbstractCursor
29
- ) {
30
- const session = entities . getEntity ( 'session' , operation . arguments . session , false ) ;
31
- if ( session ) {
32
- cursor . session = session ;
33
- }
34
- return cursor . toArray ( ) ;
35
- }
36
-
37
24
operations . set ( 'abortTransaction' , async ( { entities, operation } ) => {
38
25
const session = entities . getEntity ( 'session' , operation . object ) ;
39
26
return session . abortTransaction ( ) ;
@@ -44,18 +31,9 @@ operations.set('aggregate', async ({ entities, operation }) => {
44
31
if ( ! ( dbOrCollection instanceof Db || dbOrCollection instanceof Collection ) ) {
45
32
throw new Error ( `Operation object '${ operation . object } ' must be a db or collection` ) ;
46
33
}
47
- const cursor = dbOrCollection . aggregate ( operation . arguments . pipeline , {
48
- allowDiskUse : operation . arguments . allowDiskUse ,
49
- batchSize : operation . arguments . batchSize ,
50
- bypassDocumentValidation : operation . arguments . bypassDocumentValidation ,
51
- maxTimeMS : operation . arguments . maxTimeMS ,
52
- maxAwaitTimeMS : operation . arguments . maxAwaitTimeMS ,
53
- collation : operation . arguments . collation ,
54
- hint : operation . arguments . hint ,
55
- let : operation . arguments . let ,
56
- out : operation . arguments . out
57
- } ) ;
58
- return executeWithPotentialSession ( entities , operation , cursor ) ;
34
+ const { pipeline, ...opts } = operation . arguments ;
35
+ const cursor = dbOrCollection . aggregate ( pipeline , opts ) ;
36
+ return cursor . toArray ( ) ;
59
37
} ) ;
60
38
61
39
operations . set ( 'assertCollectionExists' , async ( { operation, client } ) => {
@@ -139,27 +117,27 @@ operations.set('assertSameLsidOnLastTwoCommands', async ({ entities, operation }
139
117
} ) ;
140
118
141
119
operations . set ( 'assertSessionDirty' , async ( { entities, operation } ) => {
142
- const session = entities . getEntity ( 'session' , operation . arguments . session ) ;
120
+ const session = operation . arguments . session ;
143
121
expect ( session . serverSession . isDirty ) . to . be . true ;
144
122
} ) ;
145
123
146
124
operations . set ( 'assertSessionNotDirty' , async ( { entities, operation } ) => {
147
- const session = entities . getEntity ( 'session' , operation . arguments . session ) ;
125
+ const session = operation . arguments . session ;
148
126
expect ( session . serverSession . isDirty ) . to . be . false ;
149
127
} ) ;
150
128
151
129
operations . set ( 'assertSessionPinned' , async ( { entities, operation } ) => {
152
- const session = entities . getEntity ( 'session' , operation . arguments . session ) ;
130
+ const session = operation . arguments . session ;
153
131
expect ( session . transaction . isPinned ) . to . be . true ;
154
132
} ) ;
155
133
156
134
operations . set ( 'assertSessionUnpinned' , async ( { entities, operation } ) => {
157
- const session = entities . getEntity ( 'session' , operation . arguments . session ) ;
135
+ const session = operation . arguments . session ;
158
136
expect ( session . transaction . isPinned ) . to . be . false ;
159
137
} ) ;
160
138
161
139
operations . set ( 'assertSessionTransactionState' , async ( { entities, operation } ) => {
162
- const session = entities . getEntity ( 'session' , operation . arguments . session ) ;
140
+ const session = operation . arguments . session ;
163
141
164
142
const transactionStateTranslation = {
165
143
none : 'NO_TRANSACTION' ,
@@ -236,17 +214,14 @@ operations.set('createChangeStream', async ({ entities, operation }) => {
236
214
237
215
operations . set ( 'createCollection' , async ( { entities, operation } ) => {
238
216
const db = entities . getEntity ( 'db' , operation . object ) ;
239
- const { session, collection, ...opts } = operation . arguments ;
240
- await db . createCollection ( collection , {
241
- session : entities . getEntity ( 'session' , session , false ) ,
242
- ...opts
243
- } ) ;
217
+ const { collection, ...opts } = operation . arguments ;
218
+ await db . createCollection ( collection , opts ) ;
244
219
} ) ;
245
220
246
221
operations . set ( 'createFindCursor' , async ( { entities, operation } ) => {
247
222
const collection = entities . getEntity ( 'collection' , operation . object ) ;
248
- const { filter, sort , batchSize , limit , let : vars } = operation . arguments ;
249
- const cursor = collection . find ( filter , { sort , batchSize , limit , let : vars } ) ;
223
+ const { filter, ... opts } = operation . arguments ;
224
+ const cursor = collection . find ( filter , opts ) ;
250
225
// The spec dictates that we create the cursor and force the find command
251
226
// to execute, but don't move the cursor forward. hasNext() accomplishes
252
227
// this.
@@ -256,11 +231,8 @@ operations.set('createFindCursor', async ({ entities, operation }) => {
256
231
257
232
operations . set ( 'createIndex' , async ( { entities, operation } ) => {
258
233
const collection = entities . getEntity ( 'collection' , operation . object ) ;
259
- const session = entities . getEntity ( 'session' , operation . arguments . session , false ) ;
260
- await collection . createIndex ( operation . arguments . keys , {
261
- session,
262
- name : operation . arguments . name
263
- } ) ;
234
+ const { keys, ...opts } = operation . arguments ;
235
+ await collection . createIndex ( keys , opts ) ;
264
236
} ) ;
265
237
266
238
operations . set ( 'deleteOne' , async ( { entities, operation } ) => {
@@ -271,7 +243,8 @@ operations.set('deleteOne', async ({ entities, operation }) => {
271
243
272
244
operations . set ( 'dropCollection' , async ( { entities, operation } ) => {
273
245
const db = entities . getEntity ( 'db' , operation . object ) ;
274
- return await db . dropCollection ( operation . arguments . collection ) ;
246
+ const { collection, ...opts } = operation . arguments ;
247
+ return await db . dropCollection ( collection , opts ) ;
275
248
} ) ;
276
249
277
250
operations . set ( 'endSession' , async ( { entities, operation } ) => {
@@ -281,9 +254,8 @@ operations.set('endSession', async ({ entities, operation }) => {
281
254
282
255
operations . set ( 'find' , async ( { entities, operation } ) => {
283
256
const collection = entities . getEntity ( 'collection' , operation . object ) ;
284
- const { filter, sort, batchSize, limit, let : vars } = operation . arguments ;
285
- const cursor = collection . find ( filter , { sort, batchSize, limit, let : vars } ) ;
286
- return executeWithPotentialSession ( entities , operation , cursor ) ;
257
+ const { filter, ...opts } = operation . arguments ;
258
+ return collection . find ( filter , opts ) . toArray ( ) ;
287
259
} ) ;
288
260
289
261
operations . set ( 'findOneAndReplace' , async ( { entities, operation } ) => {
@@ -305,27 +277,14 @@ operations.set('failPoint', async ({ entities, operation }) => {
305
277
306
278
operations . set ( 'insertOne' , async ( { entities, operation } ) => {
307
279
const collection = entities . getEntity ( 'collection' , operation . object ) ;
308
-
309
- const session = entities . getEntity ( 'session' , operation . arguments . session , false ) ;
310
-
311
- const options = {
312
- session
313
- } as InsertOneOptions ;
314
-
315
- return collection . insertOne ( operation . arguments . document , options ) ;
280
+ const { document, ...opts } = operation . arguments ;
281
+ return collection . insertOne ( document , opts ) ;
316
282
} ) ;
317
283
318
284
operations . set ( 'insertMany' , async ( { entities, operation } ) => {
319
285
const collection = entities . getEntity ( 'collection' , operation . object ) ;
320
-
321
- const session = entities . getEntity ( 'session' , operation . arguments . session , false ) ;
322
-
323
- const options = {
324
- session,
325
- ordered : operation . arguments . ordered ?? true
326
- } ;
327
-
328
- return collection . insertMany ( operation . arguments . documents , options ) ;
286
+ const { documents, ...opts } = operation . arguments ;
287
+ return collection . insertMany ( documents , opts ) ;
329
288
} ) ;
330
289
331
290
operations . set ( 'iterateUntilDocumentOrError' , async ( { entities, operation } ) => {
@@ -350,7 +309,7 @@ operations.set('listCollections', async ({ entities, operation }) => {
350
309
351
310
operations . set ( 'listDatabases' , async ( { entities, operation } ) => {
352
311
const client = entities . getEntity ( 'client' , operation . object ) ;
353
- return client . db ( ) . admin ( ) . listDatabases ( ) ;
312
+ return client . db ( ) . admin ( ) . listDatabases ( operation . arguments ) ;
354
313
} ) ;
355
314
356
315
operations . set ( 'listIndexes' , async ( { entities, operation } ) => {
@@ -360,12 +319,8 @@ operations.set('listIndexes', async ({ entities, operation }) => {
360
319
361
320
operations . set ( 'replaceOne' , async ( { entities, operation } ) => {
362
321
const collection = entities . getEntity ( 'collection' , operation . object ) ;
363
- return collection . replaceOne ( operation . arguments . filter , operation . arguments . replacement , {
364
- bypassDocumentValidation : operation . arguments . bypassDocumentValidation ,
365
- collation : operation . arguments . collation ,
366
- hint : operation . arguments . hint ,
367
- upsert : operation . arguments . upsert
368
- } ) ;
322
+ const { filter, replacement, ...opts } = operation . arguments ;
323
+ return collection . replaceOne ( filter , replacement , opts ) ;
369
324
} ) ;
370
325
371
326
operations . set ( 'startTransaction' , async ( { entities, operation } ) => {
@@ -374,7 +329,7 @@ operations.set('startTransaction', async ({ entities, operation }) => {
374
329
} ) ;
375
330
376
331
operations . set ( 'targetedFailPoint' , async ( { entities, operation } ) => {
377
- const session = entities . getEntity ( 'session' , operation . arguments . session ) ;
332
+ const session = operation . arguments . session ;
378
333
expect ( session . transaction . isPinned , 'Session must be pinned for a targetedFailPoint' ) . to . be . true ;
379
334
await entities . failPoints . enableFailPoint (
380
335
session . transaction . _pinnedServer . s . description . hostAddress ,
@@ -433,20 +388,20 @@ operations.set('withTransaction', async ({ entities, operation, client }) => {
433
388
434
389
operations . set ( 'countDocuments' , async ( { entities, operation } ) => {
435
390
const collection = entities . getEntity ( 'collection' , operation . object ) ;
436
- return collection . countDocuments ( operation . arguments . filter as Document ) ;
391
+ const { filter, ...opts } = operation . arguments ;
392
+ return collection . countDocuments ( filter , opts ) ;
437
393
} ) ;
438
394
439
395
operations . set ( 'deleteMany' , async ( { entities, operation } ) => {
440
396
const collection = entities . getEntity ( 'collection' , operation . object ) ;
441
- return collection . deleteMany ( operation . arguments . filter ) ;
397
+ const { filter, ...opts } = operation . arguments ;
398
+ return collection . deleteMany ( filter , opts ) ;
442
399
} ) ;
443
400
444
401
operations . set ( 'distinct' , async ( { entities, operation } ) => {
445
402
const collection = entities . getEntity ( 'collection' , operation . object ) ;
446
- return collection . distinct (
447
- operation . arguments . fieldName as string ,
448
- operation . arguments . filter as Document
449
- ) ;
403
+ const { fieldName, filter, ...opts } = operation . arguments ;
404
+ return collection . distinct ( fieldName , filter , opts ) ;
450
405
} ) ;
451
406
452
407
operations . set ( 'estimatedDocumentCount' , async ( { entities, operation } ) => {
@@ -456,12 +411,14 @@ operations.set('estimatedDocumentCount', async ({ entities, operation }) => {
456
411
457
412
operations . set ( 'findOneAndDelete' , async ( { entities, operation } ) => {
458
413
const collection = entities . getEntity ( 'collection' , operation . object ) ;
459
- return collection . findOneAndDelete ( operation . arguments . filter ) ;
414
+ const { filter, ...opts } = operation . arguments ;
415
+ return collection . findOneAndDelete ( filter , opts ) ;
460
416
} ) ;
461
417
462
418
operations . set ( 'runCommand' , async ( { entities, operation } : OperationFunctionParams ) => {
463
419
const db = entities . getEntity ( 'db' , operation . object ) ;
464
- return db . command ( operation . arguments . command ) ;
420
+ const { command, ...opts } = operation . arguments ;
421
+ return db . command ( command , opts ) ;
465
422
} ) ;
466
423
467
424
operations . set ( 'updateMany' , async ( { entities, operation } ) => {
@@ -484,6 +441,11 @@ export async function executeOperationAndCheck(
484
441
const opFunc = operations . get ( operation . name ) ;
485
442
expect ( opFunc , `Unknown operation: ${ operation . name } ` ) . to . exist ;
486
443
444
+ if ( operation . arguments ?. session ) {
445
+ const session = entities . getEntity ( 'session' , operation . arguments . session , false ) ;
446
+ operation . arguments . session = session ;
447
+ }
448
+
487
449
let result ;
488
450
489
451
try {
0 commit comments