mirror of
https://github.com/ckaczor/azuredatastudio.git
synced 2026-01-25 09:35:37 -05:00
Add code coverage tests for notebookContexts.ts (#8430)
This commit is contained in:
@@ -6,7 +6,7 @@
|
||||
import { nb } from 'azdata';
|
||||
|
||||
import { localize } from 'vs/nls';
|
||||
import { IDefaultConnection, notebookConstants } from 'sql/workbench/contrib/notebook/browser/models/modelInterfaces';
|
||||
import { IDefaultConnection } from 'sql/workbench/contrib/notebook/browser/models/modelInterfaces';
|
||||
import { IConnectionManagementService } from 'sql/platform/connection/common/connectionManagement';
|
||||
import { ConnectionProfile } from 'sql/platform/connection/common/connectionProfile';
|
||||
import { IConnectionProfile } from 'sql/platform/connection/common/interfaces';
|
||||
@@ -15,7 +15,7 @@ import { find } from 'vs/base/common/arrays';
|
||||
|
||||
export class NotebookContexts {
|
||||
|
||||
private static get DefaultContext(): IDefaultConnection {
|
||||
public static get DefaultContext(): IDefaultConnection {
|
||||
let defaultConnection: ConnectionProfile = <any>{
|
||||
providerName: mssqlProviderName,
|
||||
id: '-1',
|
||||
@@ -29,7 +29,7 @@ export class NotebookContexts {
|
||||
};
|
||||
}
|
||||
|
||||
private static get LocalContext(): IDefaultConnection {
|
||||
public static get LocalContext(): IDefaultConnection {
|
||||
let localConnection: ConnectionProfile = <any>{
|
||||
providerName: mssqlProviderName,
|
||||
id: '-1',
|
||||
@@ -73,7 +73,7 @@ export class NotebookContexts {
|
||||
* @param connProviderIds array of applicable connection providers to filter connections
|
||||
* @param profile connection profile passed when launching notebook
|
||||
*/
|
||||
public static getActiveContexts(connectionService: IConnectionManagementService, connProviderIds: string[], profile: IConnectionProfile): IDefaultConnection {
|
||||
public static getActiveContexts(connectionService: IConnectionManagementService, connProviderIds: string[], profile?: IConnectionProfile): IDefaultConnection {
|
||||
let defaultConnection: ConnectionProfile = NotebookContexts.DefaultContext.defaultConnection;
|
||||
let activeConnections: ConnectionProfile[] = connectionService.getActiveConnections();
|
||||
if (activeConnections && activeConnections.length > 0) {
|
||||
@@ -95,8 +95,9 @@ export class NotebookContexts {
|
||||
if (connections && connections.length > 0) {
|
||||
defaultConnection = connections[0];
|
||||
if (profile && profile.options) {
|
||||
if (find(connections, connection => connection.serverName === profile.serverName)) {
|
||||
defaultConnection = find(connections, connection => connection.serverName === profile.serverName);
|
||||
let matchingConn = find(connections, connection => connection.serverName === profile.serverName);
|
||||
if (matchingConn) {
|
||||
defaultConnection = matchingConn;
|
||||
}
|
||||
}
|
||||
} else if (connections.length === 0) {
|
||||
@@ -105,43 +106,10 @@ export class NotebookContexts {
|
||||
activeConnections = [];
|
||||
connections.forEach(connection => activeConnections.push(connection));
|
||||
}
|
||||
if (defaultConnection === NotebookContexts.DefaultContext.defaultConnection) {
|
||||
let newConnection = <ConnectionProfile><any>{
|
||||
providerName: 'SQL',
|
||||
id: '-2',
|
||||
serverName: localize('addConnection', "Add New Connection")
|
||||
};
|
||||
activeConnections.push(newConnection);
|
||||
}
|
||||
|
||||
return {
|
||||
otherConnections: activeConnections,
|
||||
defaultConnection: defaultConnection
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param specs kernel specs (comes from session manager)
|
||||
* @param displayName kernel info loaded from
|
||||
*/
|
||||
public static getDefaultKernel(specs: nb.IAllKernels, displayName: string): nb.IKernelSpec {
|
||||
let defaultKernel: nb.IKernelSpec;
|
||||
if (specs) {
|
||||
// find the saved kernel (if it exists)
|
||||
if (displayName) {
|
||||
defaultKernel = find(specs.kernels, (kernel) => kernel.display_name === displayName);
|
||||
}
|
||||
// if no saved kernel exists, use the default KernelSpec
|
||||
if (!defaultKernel) {
|
||||
defaultKernel = find(specs.kernels, (kernel) => kernel.name === specs.defaultKernel);
|
||||
}
|
||||
if (defaultKernel) {
|
||||
return defaultKernel;
|
||||
}
|
||||
}
|
||||
|
||||
// If no default kernel specified (should never happen), default to SQL
|
||||
return notebookConstants.sqlKernelSpec;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,129 @@
|
||||
/*---------------------------------------------------------------------------------------------
|
||||
* Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
* Licensed under the Source EULA. See License.txt in the project root for license information.
|
||||
*--------------------------------------------------------------------------------------------*/
|
||||
|
||||
import * as TypeMoq from 'typemoq';
|
||||
import * as assert from 'assert';
|
||||
import { nb } from 'azdata';
|
||||
import { NotebookContexts } from 'sql/workbench/contrib/notebook/browser/models/notebookContexts';
|
||||
import { IConnectionManagementService } from 'sql/platform/connection/common/connectionManagement';
|
||||
import { TestConnectionManagementService } from 'sql/platform/connection/test/common/testConnectionManagementService';
|
||||
import { ConnectionProfile } from 'sql/platform/connection/common/connectionProfile';
|
||||
import { TestCapabilitiesService } from 'sql/platform/capabilities/test/common/testCapabilitiesService';
|
||||
import { mssqlProviderName } from 'sql/platform/connection/common/constants';
|
||||
import { IDefaultConnection } from 'sql/workbench/contrib/notebook/browser/models/modelInterfaces';
|
||||
|
||||
suite('Notebook Contexts', function (): void {
|
||||
const defaultContext = NotebookContexts.DefaultContext;
|
||||
const localContext = NotebookContexts.LocalContext;
|
||||
|
||||
function createTestConnProfile(): ConnectionProfile {
|
||||
return new ConnectionProfile(new TestCapabilitiesService(), {
|
||||
connectionName: 'Test',
|
||||
savePassword: false,
|
||||
groupFullName: 'testGroup',
|
||||
serverName: 'testServerName',
|
||||
databaseName: 'testDatabaseName',
|
||||
authenticationType: 'integrated',
|
||||
password: 'test',
|
||||
userName: 'testUsername',
|
||||
groupId: undefined,
|
||||
providerName: mssqlProviderName,
|
||||
options: {},
|
||||
saveProfile: true,
|
||||
id: 'testId'
|
||||
});
|
||||
}
|
||||
|
||||
test('Get Contexts For Kernel', async function (): Promise<void> {
|
||||
const connService: TypeMoq.Mock<IConnectionManagementService>
|
||||
= TypeMoq.Mock.ofType<IConnectionManagementService>(TestConnectionManagementService, TypeMoq.MockBehavior.Strict);
|
||||
|
||||
// No kernel or profile info provided
|
||||
let conns = NotebookContexts.getContextsForKernel(connService.object, [mssqlProviderName]);
|
||||
assert.deepStrictEqual(conns, defaultContext);
|
||||
|
||||
// No Profile, Kernels are the same
|
||||
let kernelChangeArgs = <nb.IKernelChangedArgs>{
|
||||
oldValue: <nb.IKernel>{
|
||||
id: '1',
|
||||
name: 'TestKernel'
|
||||
},
|
||||
newValue: <nb.IKernel>{
|
||||
id: '1',
|
||||
name: 'TestKernel'
|
||||
}
|
||||
};
|
||||
conns = NotebookContexts.getContextsForKernel(connService.object, [mssqlProviderName], kernelChangeArgs);
|
||||
assert.deepStrictEqual(conns, defaultContext);
|
||||
|
||||
// Kernel Info and Profile, but no provider IDs
|
||||
let testConn = createTestConnProfile();
|
||||
conns = NotebookContexts.getContextsForKernel(connService.object, [], kernelChangeArgs, testConn);
|
||||
assert.deepStrictEqual(conns, defaultContext);
|
||||
|
||||
// Normal use case
|
||||
connService.setup(c => c.getActiveConnections()).returns(() => [testConn]);
|
||||
conns = NotebookContexts.getContextsForKernel(connService.object, [mssqlProviderName], kernelChangeArgs, testConn);
|
||||
assert.deepStrictEqual(conns, <IDefaultConnection>{
|
||||
otherConnections: [testConn],
|
||||
defaultConnection: testConn
|
||||
});
|
||||
});
|
||||
|
||||
test('Get Active Contexts', async function (): Promise<void> {
|
||||
const connService: TypeMoq.Mock<IConnectionManagementService>
|
||||
= TypeMoq.Mock.ofType<IConnectionManagementService>(TestConnectionManagementService, TypeMoq.MockBehavior.Strict);
|
||||
|
||||
let testConn = createTestConnProfile();
|
||||
|
||||
// No provider IDs
|
||||
connService.setup(c => c.getActiveConnections()).returns(() => [testConn]);
|
||||
let conns = NotebookContexts.getActiveContexts(connService.object, [], testConn);
|
||||
assert.deepStrictEqual(conns, localContext);
|
||||
|
||||
// No connections
|
||||
connService.setup(c => c.getActiveConnections()).returns(() => []);
|
||||
conns = NotebookContexts.getActiveContexts(connService.object, [mssqlProviderName], testConn);
|
||||
assert.deepStrictEqual(conns, defaultContext);
|
||||
|
||||
// No valid connection IDs
|
||||
testConn.id = '-1';
|
||||
connService.setup(c => c.getActiveConnections()).returns(() => [testConn]);
|
||||
conns = NotebookContexts.getActiveContexts(connService.object, [mssqlProviderName], testConn);
|
||||
assert.deepStrictEqual(conns, defaultContext);
|
||||
|
||||
// No matching provider IDs
|
||||
testConn.id = 'testId';
|
||||
connService.setup(c => c.getActiveConnections()).returns(() => [testConn]);
|
||||
conns = NotebookContexts.getActiveContexts(connService.object, ['notARealProvider'], testConn);
|
||||
assert.deepStrictEqual(conns, defaultContext);
|
||||
|
||||
// Normal behavior, valid connection present
|
||||
connService.setup(c => c.getActiveConnections()).returns(() => [testConn]);
|
||||
conns = NotebookContexts.getActiveContexts(connService.object, [mssqlProviderName], testConn);
|
||||
assert.deepStrictEqual(conns, <IDefaultConnection>{
|
||||
otherConnections: [testConn],
|
||||
defaultConnection: testConn
|
||||
});
|
||||
|
||||
// Multiple active connections
|
||||
let newTestConn = createTestConnProfile();
|
||||
newTestConn.serverName = 'otherTestServerName';
|
||||
connService.setup(c => c.getActiveConnections()).returns(() => [newTestConn, testConn]);
|
||||
conns = NotebookContexts.getActiveContexts(connService.object, [mssqlProviderName], testConn);
|
||||
assert.deepStrictEqual(conns, <IDefaultConnection>{
|
||||
otherConnections: [newTestConn, testConn],
|
||||
defaultConnection: testConn
|
||||
});
|
||||
|
||||
// Multiple connections, no profile provided
|
||||
connService.setup(c => c.getActiveConnections()).returns(() => [newTestConn, testConn]);
|
||||
conns = NotebookContexts.getActiveContexts(connService.object, [mssqlProviderName], undefined);
|
||||
assert.deepStrictEqual(conns, <IDefaultConnection>{
|
||||
otherConnections: [newTestConn, testConn],
|
||||
defaultConnection: newTestConn
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user