Skip to content

Commit 13158be

Browse files
authored
test(NODE-4347): add coverage for auto connecting operations (#3299)
1 parent c9083db commit 13158be

File tree

9 files changed

+981
-89
lines changed

9 files changed

+981
-89
lines changed

src/collection.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { UnorderedBulkOperation } from './bulk/unordered';
55
import { ChangeStream, ChangeStreamDocument, ChangeStreamOptions } from './change_stream';
66
import { AggregationCursor } from './cursor/aggregation_cursor';
77
import { FindCursor } from './cursor/find_cursor';
8+
import { ListIndexesCursor } from './cursor/list_indexes_cursor';
89
import type { Db } from './db';
910
import { MongoInvalidArgumentError } from './error';
1011
import type { Logger, LoggerOptions } from './logger';
@@ -57,7 +58,6 @@ import {
5758
IndexExistsOperation,
5859
IndexInformationOperation,
5960
IndexSpecification,
60-
ListIndexesCursor,
6161
ListIndexesOptions
6262
} from './operations/indexes';
6363
import {
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import type { Document } from '../bson';
2+
import type { Db } from '../db';
3+
import { executeOperation, ExecutionResult } from '../operations/execute_operation';
4+
import {
5+
CollectionInfo,
6+
ListCollectionsOperation,
7+
ListCollectionsOptions
8+
} from '../operations/list_collections';
9+
import type { ClientSession } from '../sessions';
10+
import type { Callback } from '../utils';
11+
import { AbstractCursor } from './abstract_cursor';
12+
13+
/** @public */
14+
export class ListCollectionsCursor<
15+
T extends Pick<CollectionInfo, 'name' | 'type'> | CollectionInfo =
16+
| Pick<CollectionInfo, 'name' | 'type'>
17+
| CollectionInfo
18+
> extends AbstractCursor<T> {
19+
parent: Db;
20+
filter: Document;
21+
options?: ListCollectionsOptions;
22+
23+
constructor(db: Db, filter: Document, options?: ListCollectionsOptions) {
24+
super(db.s.client, db.s.namespace, options);
25+
this.parent = db;
26+
this.filter = filter;
27+
this.options = options;
28+
}
29+
30+
clone(): ListCollectionsCursor<T> {
31+
return new ListCollectionsCursor(this.parent, this.filter, {
32+
...this.options,
33+
...this.cursorOptions
34+
});
35+
}
36+
37+
/** @internal */
38+
_initialize(session: ClientSession | undefined, callback: Callback<ExecutionResult>): void {
39+
const operation = new ListCollectionsOperation(this.parent, this.filter, {
40+
...this.cursorOptions,
41+
...this.options,
42+
session
43+
});
44+
45+
executeOperation(this.parent.s.client, operation, (err, response) => {
46+
if (err || response == null) return callback(err);
47+
48+
// TODO: NODE-2882
49+
callback(undefined, { server: operation.server, session, response });
50+
});
51+
}
52+
}

src/cursor/list_indexes_cursor.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import type { Collection } from '../collection';
2+
import { executeOperation, ExecutionResult } from '../operations/execute_operation';
3+
import { ListIndexesOperation, ListIndexesOptions } from '../operations/indexes';
4+
import type { ClientSession } from '../sessions';
5+
import type { Callback } from '../utils';
6+
import { AbstractCursor } from './abstract_cursor';
7+
8+
/** @public */
9+
export class ListIndexesCursor extends AbstractCursor {
10+
parent: Collection;
11+
options?: ListIndexesOptions;
12+
13+
constructor(collection: Collection, options?: ListIndexesOptions) {
14+
super(collection.s.db.s.client, collection.s.namespace, options);
15+
this.parent = collection;
16+
this.options = options;
17+
}
18+
19+
clone(): ListIndexesCursor {
20+
return new ListIndexesCursor(this.parent, {
21+
...this.options,
22+
...this.cursorOptions
23+
});
24+
}
25+
26+
/** @internal */
27+
_initialize(session: ClientSession | undefined, callback: Callback<ExecutionResult>): void {
28+
const operation = new ListIndexesOperation(this.parent, {
29+
...this.cursorOptions,
30+
...this.options,
31+
session
32+
});
33+
34+
executeOperation(this.parent.s.db.s.client, operation, (err, response) => {
35+
if (err || response == null) return callback(err);
36+
37+
// TODO: NODE-2882
38+
callback(undefined, { server: operation.server, session, response });
39+
});
40+
}
41+
}

src/db.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { ChangeStream, ChangeStreamDocument, ChangeStreamOptions } from './chang
44
import { Collection, CollectionOptions } from './collection';
55
import * as CONSTANTS from './constants';
66
import { AggregationCursor } from './cursor/aggregation_cursor';
7+
import { ListCollectionsCursor } from './cursor/list_collections_cursor';
78
import { MongoAPIError, MongoInvalidArgumentError } from './error';
89
import { Logger, LoggerOptions } from './logger';
910
import type { MongoClient, PkFactory } from './mongo_client';
@@ -26,11 +27,7 @@ import {
2627
IndexInformationOperation,
2728
IndexSpecification
2829
} from './operations/indexes';
29-
import {
30-
CollectionInfo,
31-
ListCollectionsCursor,
32-
ListCollectionsOptions
33-
} from './operations/list_collections';
30+
import type { CollectionInfo, ListCollectionsOptions } from './operations/list_collections';
3431
import { ProfilingLevelOperation, ProfilingLevelOptions } from './operations/profiling_level';
3532
import { RemoveUserOperation, RemoveUserOptions } from './operations/remove_user';
3633
import { RenameOperation, RenameOptions } from './operations/rename';

src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ import { Collection } from './collection';
44
import { AbstractCursor } from './cursor/abstract_cursor';
55
import { AggregationCursor } from './cursor/aggregation_cursor';
66
import { FindCursor } from './cursor/find_cursor';
7+
import { ListCollectionsCursor } from './cursor/list_collections_cursor';
8+
import { ListIndexesCursor } from './cursor/list_indexes_cursor';
79
import { Db } from './db';
810
import { GridFSBucket } from './gridfs';
911
import { Logger } from './logger';
1012
import { MongoClient } from './mongo_client';
1113
import { CancellationToken } from './mongo_types';
12-
import { ListIndexesCursor } from './operations/indexes';
13-
import { ListCollectionsCursor } from './operations/list_collections';
1414
import { PromiseProvider } from './promise_provider';
1515

1616
export {

src/operations/indexes.ts

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import type { Document } from '../bson';
22
import type { Collection } from '../collection';
3-
import { AbstractCursor } from '../cursor/abstract_cursor';
43
import type { Db } from '../db';
54
import { MongoCompatibilityError, MONGODB_ERROR_CODES, MongoServerError } from '../error';
65
import type { OneOrMore } from '../mongo_types';
@@ -15,7 +14,6 @@ import {
1514
OperationParent
1615
} from './command';
1716
import { indexInformation, IndexInformationOptions } from './common_functions';
18-
import { executeOperation, ExecutionResult } from './execute_operation';
1917
import { AbstractOperation, Aspect, defineAspects } from './operation';
2018

2119
const VALID_INDEX_OPTIONS = new Set([
@@ -412,41 +410,6 @@ export class ListIndexesOperation extends CommandOperation<Document> {
412410
}
413411
}
414412

415-
/** @public */
416-
export class ListIndexesCursor extends AbstractCursor {
417-
parent: Collection;
418-
options?: ListIndexesOptions;
419-
420-
constructor(collection: Collection, options?: ListIndexesOptions) {
421-
super(collection.s.db.s.client, collection.s.namespace, options);
422-
this.parent = collection;
423-
this.options = options;
424-
}
425-
426-
clone(): ListIndexesCursor {
427-
return new ListIndexesCursor(this.parent, {
428-
...this.options,
429-
...this.cursorOptions
430-
});
431-
}
432-
433-
/** @internal */
434-
_initialize(session: ClientSession | undefined, callback: Callback<ExecutionResult>): void {
435-
const operation = new ListIndexesOperation(this.parent, {
436-
...this.cursorOptions,
437-
...this.options,
438-
session
439-
});
440-
441-
executeOperation(this.parent.s.db.s.client, operation, (err, response) => {
442-
if (err || response == null) return callback(err);
443-
444-
// TODO: NODE-2882
445-
callback(undefined, { server: operation.server, session, response });
446-
});
447-
}
448-
}
449-
450413
/** @internal */
451414
export class IndexExistsOperation extends AbstractOperation<boolean> {
452415
override options: IndexInformationOptions;

src/operations/list_collections.ts

Lines changed: 0 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
import type { Binary, Document } from '../bson';
2-
import { AbstractCursor } from '../cursor/abstract_cursor';
32
import type { Db } from '../db';
43
import type { Server } from '../sdam/server';
54
import type { ClientSession } from '../sessions';
65
import { Callback, maxWireVersion } from '../utils';
76
import { CommandOperation, CommandOperationOptions } from './command';
8-
import { executeOperation, ExecutionResult } from './execute_operation';
97
import { Aspect, defineAspects } from './operation';
108

119
/** @public */
@@ -86,47 +84,6 @@ export interface CollectionInfo extends Document {
8684
idIndex?: Document;
8785
}
8886

89-
/** @public */
90-
export class ListCollectionsCursor<
91-
T extends Pick<CollectionInfo, 'name' | 'type'> | CollectionInfo =
92-
| Pick<CollectionInfo, 'name' | 'type'>
93-
| CollectionInfo
94-
> extends AbstractCursor<T> {
95-
parent: Db;
96-
filter: Document;
97-
options?: ListCollectionsOptions;
98-
99-
constructor(db: Db, filter: Document, options?: ListCollectionsOptions) {
100-
super(db.s.client, db.s.namespace, options);
101-
this.parent = db;
102-
this.filter = filter;
103-
this.options = options;
104-
}
105-
106-
clone(): ListCollectionsCursor<T> {
107-
return new ListCollectionsCursor(this.parent, this.filter, {
108-
...this.options,
109-
...this.cursorOptions
110-
});
111-
}
112-
113-
/** @internal */
114-
_initialize(session: ClientSession | undefined, callback: Callback<ExecutionResult>): void {
115-
const operation = new ListCollectionsOperation(this.parent, this.filter, {
116-
...this.cursorOptions,
117-
...this.options,
118-
session
119-
});
120-
121-
executeOperation(this.parent.s.client, operation, (err, response) => {
122-
if (err || response == null) return callback(err);
123-
124-
// TODO: NODE-2882
125-
callback(undefined, { server: operation.server, session, response });
126-
});
127-
}
128-
}
129-
13087
defineAspects(ListCollectionsOperation, [
13188
Aspect.READ_OPERATION,
13289
Aspect.RETRYABLE,

0 commit comments

Comments
 (0)