Skip to content

Commit 06b1e90

Browse files
committed
add tests
1 parent db87e39 commit 06b1e90

File tree

2 files changed

+118
-9
lines changed

2 files changed

+118
-9
lines changed

src/storage/connectionStorage.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ export interface StoreConnectionInfo {
2323
id: string; // Connection model id or a new uuid.
2424
name: string; // Possibly user given name, not unique.
2525
storageLocation: StorageLocation;
26-
secretStorageLocation: SecretStorageLocationType;
27-
connectionOptions: ConnectionOptions;
26+
secretStorageLocation?: SecretStorageLocationType;
27+
connectionOptions?: ConnectionOptions;
2828
isMutable?: boolean;
2929
lastUsed?: Date; // Date and time when the connection was last used, i.e. connected with.
3030
}
@@ -34,7 +34,15 @@ export type PresetSavedConnection = {
3434
connectionString: string;
3535
};
3636

37-
export type LoadedConnection = StoreConnectionInfo;
37+
type StoreConnectionInfoWithConnectionOptions = StoreConnectionInfo &
38+
Required<Pick<StoreConnectionInfo, 'connectionOptions'>>;
39+
40+
type StoreConnectionInfoWithSecretStorageLocation = StoreConnectionInfo &
41+
Required<Pick<StoreConnectionInfo, 'secretStorageLocation'>>;
42+
43+
export type LoadedConnection = StoreConnectionInfoWithConnectionOptions &
44+
StoreConnectionInfoWithSecretStorageLocation;
45+
3846
export class ConnectionStorage {
3947
_storageController: StorageController;
4048

@@ -84,7 +92,7 @@ export class ConnectionStorage {
8492
(await this._storageController.getSecret(connectionInfo.id)) ?? '';
8593

8694
return this._mergedConnectionInfoWithSecrets(
87-
connectionInfo,
95+
connectionInfo as LoadedConnection,
8896
unparsedSecrets
8997
);
9098
} catch (error) {

src/test/suite/storage/connectionStorage.test.ts

Lines changed: 106 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,15 @@ import {
1515
TEST_DATABASE_URI_USER,
1616
TEST_USER_PASSWORD,
1717
} from '../dbTestHelper';
18-
import type { StoreConnectionInfo } from '../../../storage/connectionStorage';
18+
import type { LoadedConnection } from '../../../storage/connectionStorage';
1919
import { ConnectionStorage } from '../../../storage/connectionStorage';
2020

2121
const testDatabaseConnectionName = 'localhost:27088';
2222

23-
const newTestConnection = (connectionStorage: ConnectionStorage, id: string) =>
23+
const newTestConnection = (
24+
connectionStorage: ConnectionStorage,
25+
id: string
26+
): LoadedConnection =>
2427
connectionStorage.createNewConnection({
2528
connectionId: id,
2629
connectionOptions: {
@@ -305,9 +308,7 @@ suite('Connection Storage Test Suite', function () {
305308
expect(connections.length).to.equal(1);
306309

307310
const newSavedConnectionInfoWithSecrets =
308-
await testConnectionStorage._getConnectionInfoWithSecrets(
309-
connections[0] as StoreConnectionInfo
310-
);
311+
await testConnectionStorage._getConnectionInfoWithSecrets(connections[0]);
311312

312313
expect(newSavedConnectionInfoWithSecrets).to.deep.equal(connectionInfo);
313314
});
@@ -321,6 +322,104 @@ suite('Connection Storage Test Suite', function () {
321322
extensionSandbox.restore();
322323
});
323324

325+
suite('when there are preset connections', () => {
326+
const presetConnections = [
327+
{
328+
name: 'Preset Connection 1',
329+
connectionString: 'mongodb://localhost:27017/',
330+
},
331+
{
332+
name: 'Preset Connection 2',
333+
connectionString: 'mongodb://localhost:27018/',
334+
},
335+
];
336+
337+
let getConfigurationStub: sinon.SinonStub<
338+
[
339+
section?: string | undefined,
340+
scope?: vscode.ConfigurationScope | null | undefined
341+
],
342+
vscode.WorkspaceConfiguration
343+
>;
344+
let getPresetSavedConnectionsStub: sinon.SinonStub;
345+
346+
beforeEach(() => {
347+
testSandbox.restore();
348+
getPresetSavedConnectionsStub = testSandbox.stub();
349+
});
350+
351+
test('loads the preset connections', async () => {
352+
getConfigurationStub = testSandbox.stub(
353+
vscode.workspace,
354+
'getConfiguration'
355+
);
356+
getConfigurationStub.returns({
357+
get: getPresetSavedConnectionsStub,
358+
} as any);
359+
360+
getPresetSavedConnectionsStub
361+
.withArgs('presetSavedConnections')
362+
.returns(presetConnections);
363+
364+
const connections = await testConnectionStorage.loadConnections();
365+
366+
expect(connections.length).to.equal(2);
367+
368+
for (let i = 0; i < connections.length; i++) {
369+
const connection = connections[i];
370+
const presetConnection = presetConnections[i];
371+
expect(connection.name).equals(
372+
`${presetConnection.name} (From Configuration)`
373+
);
374+
expect(connection.connectionOptions.connectionString).equals(
375+
presetConnection.connectionString
376+
);
377+
expect(connection.isMutable).equals(false);
378+
}
379+
});
380+
381+
test('loads both preset and other saved connections', async () => {
382+
const savedConnection = newTestConnection(testConnectionStorage, '1');
383+
await testConnectionStorage.saveConnection(savedConnection);
384+
385+
getConfigurationStub = testSandbox.stub(
386+
vscode.workspace,
387+
'getConfiguration'
388+
);
389+
getConfigurationStub.returns({
390+
get: getPresetSavedConnectionsStub,
391+
} as any);
392+
393+
getPresetSavedConnectionsStub
394+
.withArgs('presetSavedConnections')
395+
.returns(presetConnections);
396+
397+
const loadedConnections = await testConnectionStorage.loadConnections();
398+
399+
expect(loadedConnections.length).equals(3);
400+
401+
for (let i = 0; i < presetConnections.length; i++) {
402+
const connection = loadedConnections[i];
403+
const presetConnection = presetConnections[i];
404+
expect(connection.name).equals(
405+
`${presetConnection.name} (From Configuration)`
406+
);
407+
expect(connection.connectionOptions.connectionString).equals(
408+
presetConnection.connectionString
409+
);
410+
expect(connection.isMutable).equals(false);
411+
}
412+
413+
const savedLoadedConnection = loadedConnections[2];
414+
415+
expect(savedLoadedConnection.name).equals(savedConnection.name);
416+
expect(
417+
savedLoadedConnection.connectionOptions.connectionString
418+
).contains(savedConnection.connectionOptions.connectionString);
419+
expect(savedLoadedConnection.isMutable).equals(true);
420+
});
421+
});
422+
324423
suite('when connection secrets are already in SecretStorage', () => {
325424
afterEach(() => {
326425
testSandbox.restore();
@@ -341,6 +440,8 @@ suite('Connection Storage Test Suite', function () {
341440

342441
// By default the connection secrets are already stored in SecretStorage
343442
const savedConnections = await testConnectionStorage.loadConnections();
443+
444+
expect(savedConnections.length).equals(2);
344445
expect(
345446
savedConnections.every(
346447
({ secretStorageLocation }) =>

0 commit comments

Comments
 (0)