Skip to content

Commit cf99e0b

Browse files
committed
add an integration test
1 parent b3dd61b commit cf99e0b

File tree

3 files changed

+45
-10
lines changed

3 files changed

+45
-10
lines changed

packages/cli-repl/src/mongosh-repl.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -444,8 +444,7 @@ class MongoshNodeRepl implements EvaluationListener {
444444
line: string
445445
) => Promise<[string[], string, 'exclusive'] | [string[], string]>;
446446
if (process.env.USE_NEW_AUTOCOMPLETE) {
447-
const autocompletionContext =
448-
instanceState.getAutocompletionContext(instanceState);
447+
const autocompletionContext = instanceState.getAutocompletionContext();
449448
newMongoshCompleter = new MongoDBAutocompleter({
450449
context: autocompletionContext,
451450
});

packages/shell-api/src/integration.spec.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2772,6 +2772,44 @@ describe('Shell API (integration)', function () {
27722772
});
27732773
});
27742774

2775+
describe('getAutocompletionContext', function () {
2776+
beforeEach(async function () {
2777+
// Make sure the collection is present so it is included in autocompletion.
2778+
await collection.insertOne({});
2779+
// Make sure 'database' is the current db in the eyes of the instance state object.
2780+
instanceState.setDbFunc(database);
2781+
});
2782+
2783+
it('returns information for autocomplete', async function () {
2784+
const context = instanceState.getAutocompletionContext();
2785+
const { connectionId, databaseName } =
2786+
context.currentDatabaseAndConnection();
2787+
const databaseNames = await context.databasesForConnection(
2788+
connectionId
2789+
);
2790+
expect(databaseNames).to.include(database.getName());
2791+
const collectionNames = await context.collectionsForDatabase(
2792+
connectionId,
2793+
databaseName
2794+
);
2795+
expect(collectionNames).to.include(collection.getName());
2796+
const schema = await context.schemaInformationForCollection(
2797+
connectionId,
2798+
database.getName(),
2799+
collection.getName()
2800+
);
2801+
expect(schema).to.deep.equal({
2802+
bsonType: 'object',
2803+
properties: {
2804+
_id: {
2805+
bsonType: 'objectId',
2806+
},
2807+
},
2808+
required: ['_id'],
2809+
});
2810+
});
2811+
});
2812+
27752813
describe('getAutocompleteParameters', function () {
27762814
let connectionString: string;
27772815
beforeEach(async function () {

packages/shell-api/src/shell-instance-state.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -415,20 +415,18 @@ export class ShellInstanceState {
415415
throw new Error(`mongo with connection id ${connectionId} not found`);
416416
}
417417

418-
public getAutocompletionContext(
419-
instanceState: ShellInstanceState
420-
): AutocompletionContext {
418+
public getAutocompletionContext(): AutocompletionContext {
421419
return {
422420
currentDatabaseAndConnection: () => {
423421
return {
424-
connectionId: instanceState.currentDb.getMongo()._getConnectionId(),
425-
databaseName: instanceState.currentDb.getName(),
422+
connectionId: this.currentDb.getMongo()._getConnectionId(),
423+
databaseName: this.currentDb.getName(),
426424
};
427425
},
428426
databasesForConnection: async (
429427
connectionId: string
430428
): Promise<string[]> => {
431-
const mongo = instanceState.getMongoByConnectionId(connectionId);
429+
const mongo = this.getMongoByConnectionId(connectionId);
432430
try {
433431
const dbNames = await mongo._getDatabaseNamesForCompletion();
434432
return dbNames.filter(
@@ -448,7 +446,7 @@ export class ShellInstanceState {
448446
connectionId: string,
449447
databaseName: string
450448
): Promise<string[]> => {
451-
const mongo = instanceState.getMongoByConnectionId(connectionId);
449+
const mongo = this.getMongoByConnectionId(connectionId);
452450
try {
453451
const collectionNames = await mongo
454452
._getDb(databaseName)
@@ -471,7 +469,7 @@ export class ShellInstanceState {
471469
databaseName: string,
472470
collectionName: string
473471
): Promise<JSONSchema> => {
474-
const mongo = instanceState.getMongoByConnectionId(connectionId);
472+
const mongo = this.getMongoByConnectionId(connectionId);
475473
const docs = await mongo
476474
._getDb(databaseName)
477475
.getCollection(collectionName)

0 commit comments

Comments
 (0)