From 16033ac32cc520ed8af0535927c944654a32226b Mon Sep 17 00:00:00 2001 From: Himanshu Singh Date: Wed, 6 Dec 2023 12:22:41 +0100 Subject: [PATCH] chore: refactored duplicate code as a helper function --- src/connectionController.ts | 13 ------ .../activeConnectionCodeLensProvider.ts | 8 +++- src/language/mongoDBService.ts | 19 +++------ src/test/suite/connectionController.test.ts | 40 ------------------- .../activeConnectionCodeLensProvider.test.ts | 14 ++++++- .../suite/language/mongoDBService.test.ts | 10 ++--- 6 files changed, 29 insertions(+), 75 deletions(-) diff --git a/src/connectionController.ts b/src/connectionController.ts index 3a35f58a7..4891acf0a 100644 --- a/src/connectionController.ts +++ b/src/connectionController.ts @@ -1122,17 +1122,4 @@ export default class ConnectionController { selectedQuickPickItem.data.connectionId ); } - - getActiveConnectionDefaultDB(): string | null { - try { - const connectionString = new ConnectionString( - this.getActiveConnectionString() - ); - return connectionString.pathname !== '/' - ? connectionString.pathname.substring(1) - : null; - } catch { - return null; - } - } } diff --git a/src/editors/activeConnectionCodeLensProvider.ts b/src/editors/activeConnectionCodeLensProvider.ts index 6f5ed5fe2..62f227687 100644 --- a/src/editors/activeConnectionCodeLensProvider.ts +++ b/src/editors/activeConnectionCodeLensProvider.ts @@ -3,6 +3,7 @@ import type { TextEditor } from 'vscode'; import EXTENSION_COMMANDS from '../commands'; import type ConnectionController from '../connectionController'; import { isPlayground } from '../utils/playground'; +import { getDBFromConnectionString } from '../utils/connection-string-db'; export default class ActiveConnectionCodeLensProvider implements vscode.CodeLensProvider @@ -48,8 +49,11 @@ export default class ActiveConnectionCodeLensProvider if (this._connectionController.isConnecting()) { message = 'Connecting...'; } else if (this._connectionController.getActiveDataService()) { - const defaultDB = - this._connectionController.getActiveConnectionDefaultDB(); + const connectionString = + this._connectionController.getMongoClientConnectionOptions()?.url; + const defaultDB = connectionString + ? getDBFromConnectionString(connectionString) + : null; message = defaultDB ? `Currently connected to ${this._connectionController.getActiveConnectionName()} with default database ${defaultDB}. Click here to change connection.` : `Currently connected to ${this._connectionController.getActiveConnectionName()}. Click here to change connection.`; diff --git a/src/language/mongoDBService.ts b/src/language/mongoDBService.ts index 8653f4af3..c32f67b59 100644 --- a/src/language/mongoDBService.ts +++ b/src/language/mongoDBService.ts @@ -14,7 +14,6 @@ import type { MarkupContent, Diagnostic, } from 'vscode-languageserver/node'; -import ConnectionString from 'mongodb-connection-string-url'; import { CliServiceProvider } from '@mongosh/service-provider-server'; import type { Document } from '@mongosh/service-provider-core'; import { getFilteredCompletions } from '@mongodb-js/mongodb-constants'; @@ -40,6 +39,7 @@ import type { CompletionState, NamespaceState } from './visitor'; import LINKS from '../utils/links'; import DIAGNOSTIC_CODES from './diagnosticCodes'; +import { getDBFromConnectionString } from '../utils/connection-string-db'; const PROJECT = '$project'; @@ -97,16 +97,6 @@ export default class MongoDBService { return this._currentConnectionOptions; } - get defaultConnectedDB(): string | null { - if (!this.connectionString) { - return null; - } - const connectionString = new ConnectionString(this.connectionString); - return connectionString.pathname !== '/' - ? connectionString.pathname.substring(1) - : null; - } - initialize({ extensionPath, connectionId, @@ -463,10 +453,13 @@ export default class MongoDBService { * database */ withDefaultDatabase(state: T): T { - if (state.databaseName === null && this.defaultConnectedDB !== null) { + const defaultDB = this.connectionString + ? getDBFromConnectionString(this.connectionString) + : null; + if (state.databaseName === null && defaultDB !== null) { return { ...state, - databaseName: this.defaultConnectedDB, + databaseName: defaultDB, }; } return state; diff --git a/src/test/suite/connectionController.test.ts b/src/test/suite/connectionController.test.ts index a078a40ce..c583f3ede 100644 --- a/src/test/suite/connectionController.test.ts +++ b/src/test/suite/connectionController.test.ts @@ -30,7 +30,6 @@ import { TEST_DATABASE_URI_USER, TEST_USER_USERNAME, TEST_USER_PASSWORD, - TEST_DB_NAME, } from './dbTestHelper'; import KeytarStub from './keytarStub'; import { ext } from '../../extensionConstants'; @@ -1853,43 +1852,4 @@ suite('Connection Controller Test Suite', function () { ]); }); }); - - suite('getActiveConnectionDefaultDB', () => { - suite('when connected to a connection and a default database', () => { - test('it should return the default connected database', async () => { - const succesfullyConnected = - await testConnectionController.addNewConnectionStringAndConnect( - `${TEST_DATABASE_URI}/${TEST_DB_NAME}` - ); - assert.strictEqual(succesfullyConnected, true); - assert.strictEqual( - testConnectionController.getActiveConnectionDefaultDB(), - TEST_DB_NAME - ); - }); - }); - - suite('when connected to a connection without a default database', () => { - test('it should return the null', async () => { - const succesfullyConnected = - await testConnectionController.addNewConnectionStringAndConnect( - `${TEST_DATABASE_URI}` - ); - assert.strictEqual(succesfullyConnected, true); - assert.strictEqual( - testConnectionController.getActiveConnectionDefaultDB(), - null - ); - }); - }); - - suite('when not connected to a connection', () => { - test('it should return the null', () => { - assert.strictEqual( - testConnectionController.getActiveConnectionDefaultDB(), - null - ); - }); - }); - }); }); diff --git a/src/test/suite/editors/activeConnectionCodeLensProvider.test.ts b/src/test/suite/editors/activeConnectionCodeLensProvider.test.ts index 845bfaee4..fd3e2fdaa 100644 --- a/src/test/suite/editors/activeConnectionCodeLensProvider.test.ts +++ b/src/test/suite/editors/activeConnectionCodeLensProvider.test.ts @@ -10,6 +10,7 @@ import { StatusView } from '../../../views'; import { StorageController } from '../../../storage'; import { ExtensionContextStub } from '../stubs'; import TelemetryService from '../../../telemetry/telemetryService'; +import { TEST_DATABASE_URI } from '../dbTestHelper'; const expect = chai.expect; @@ -100,6 +101,13 @@ suite('Active Connection CodeLens Provider Test Suite', () => { }); test('show active connection in code lenses', () => { + sandbox.replace( + testConnectionController, + 'getMongoClientConnectionOptions', + sandbox.fake.returns({ + url: TEST_DATABASE_URI, + }) + ); const codeLens = testCodeLensProvider.provideCodeLenses(); expect(codeLens).to.be.an('array'); @@ -117,8 +125,10 @@ suite('Active Connection CodeLens Provider Test Suite', () => { test('show active connection and default database in code lenses, when connected to a default database', () => { sandbox.replace( testConnectionController, - 'getActiveConnectionDefaultDB', - sandbox.fake.returns('fakeDBName') + 'getMongoClientConnectionOptions', + sandbox.fake.returns({ + url: `${TEST_DATABASE_URI}/fakeDBName`, + }) ); const codeLens = testCodeLensProvider.provideCodeLenses(); expect(codeLens).to.be.an('array'); diff --git a/src/test/suite/language/mongoDBService.test.ts b/src/test/suite/language/mongoDBService.test.ts index 28889e870..87ab182ca 100644 --- a/src/test/suite/language/mongoDBService.test.ts +++ b/src/test/suite/language/mongoDBService.test.ts @@ -1318,8 +1318,8 @@ suite('MongoDBService Test Suite', () => { dbInUse: 'defaultDB', defaultContent: '', beforeAssertions: () => { - Sinon.stub(testMongoDBService, 'defaultConnectedDB').get( - () => 'defaultDB' + Sinon.stub(testMongoDBService, 'connectionString').get( + () => `${params.connectionString}/defaultDB` ); }, }, @@ -1329,8 +1329,8 @@ suite('MongoDBService Test Suite', () => { dbInUse: 'anotherTestDB', defaultContent: "use('anotherTestDB');", beforeAssertions: () => { - Sinon.stub(testMongoDBService, 'defaultConnectedDB').get( - () => 'defaultDB' + Sinon.stub(testMongoDBService, 'connectionString').get( + () => `${params.connectionString}/defaultDB` ); }, }, @@ -1340,7 +1340,7 @@ suite('MongoDBService Test Suite', () => { dbInUse: 'anotherTestDB', defaultContent: "use('anotherTestDB');", beforeAssertions: () => { - Sinon.stub(testMongoDBService, 'defaultConnectedDB').get(() => null); + () => params.connectionString; }, }, ].forEach(