Skip to content

Commit

Permalink
chore: refactored duplicate code as a helper function
Browse files Browse the repository at this point in the history
  • Loading branch information
himanshusinghs committed Dec 6, 2023
1 parent 74a4f34 commit 16033ac
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 75 deletions.
13 changes: 0 additions & 13 deletions src/connectionController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}
8 changes: 6 additions & 2 deletions src/editors/activeConnectionCodeLensProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Check failure on line 6 in src/editors/activeConnectionCodeLensProvider.ts

View workflow job for this annotation

GitHub Actions / Test and Build (ubuntu-latest)

Cannot find module '../utils/connection-string-db' or its corresponding type declarations.

Check failure on line 6 in src/editors/activeConnectionCodeLensProvider.ts

View workflow job for this annotation

GitHub Actions / Test and Build (windows-2019)

Cannot find module '../utils/connection-string-db' or its corresponding type declarations.

Check failure on line 6 in src/editors/activeConnectionCodeLensProvider.ts

View workflow job for this annotation

GitHub Actions / Test and Build (macos-latest)

Cannot find module '../utils/connection-string-db' or its corresponding type declarations.

export default class ActiveConnectionCodeLensProvider
implements vscode.CodeLensProvider
Expand Down Expand Up @@ -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.`;
Expand Down
19 changes: 6 additions & 13 deletions src/language/mongoDBService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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';

Check failure on line 42 in src/language/mongoDBService.ts

View workflow job for this annotation

GitHub Actions / Test and Build (ubuntu-latest)

Cannot find module '../utils/connection-string-db' or its corresponding type declarations.

Check failure on line 42 in src/language/mongoDBService.ts

View workflow job for this annotation

GitHub Actions / Test and Build (windows-2019)

Cannot find module '../utils/connection-string-db' or its corresponding type declarations.

Check failure on line 42 in src/language/mongoDBService.ts

View workflow job for this annotation

GitHub Actions / Test and Build (macos-latest)

Cannot find module '../utils/connection-string-db' or its corresponding type declarations.

const PROJECT = '$project';

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -463,10 +453,13 @@ export default class MongoDBService {
* database
*/
withDefaultDatabase<T extends NamespaceState | CompletionState>(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;
Expand Down
40 changes: 0 additions & 40 deletions src/test/suite/connectionController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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
);
});
});
});
});
14 changes: 12 additions & 2 deletions src/test/suite/editors/activeConnectionCodeLensProvider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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');
Expand All @@ -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');
Expand Down
10 changes: 5 additions & 5 deletions src/test/suite/language/mongoDBService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`
);
},
},
Expand All @@ -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`
);
},
},
Expand All @@ -1340,7 +1340,7 @@ suite('MongoDBService Test Suite', () => {
dbInUse: 'anotherTestDB',
defaultContent: "use('anotherTestDB');",
beforeAssertions: () => {
Sinon.stub(testMongoDBService, 'defaultConnectedDB').get(() => null);
() => params.connectionString;
},
},
].forEach(
Expand Down

0 comments on commit 16033ac

Please sign in to comment.