Add global current connection (#505)

This commit is contained in:
Matt Irvine
2018-01-24 10:11:34 -08:00
committed by GitHub
parent e432884e25
commit a63800deb1
12 changed files with 326 additions and 15 deletions

View File

@@ -16,6 +16,7 @@ import { TPromise } from 'vs/base/common/winjs.base';
import * as data from 'data';
import * as TypeMoq from 'typemoq';
import * as assert from 'assert';
import { ServerTreeView } from 'sql/parts/registeredServer/viewlet/serverTreeView';
import { ConnectionOptionSpecialType } from 'sql/workbench/api/common/sqlExtHostTypes';
suite('SQL Object Explorer Service tests', () => {
@@ -448,4 +449,57 @@ suite('SQL Object Explorer Service tests', () => {
assert.equal(table1Node.getConnectionProfile(), connection);
assert.equal(table1Node.getDatabaseName(), 'Db1');
});
test('getSelectedProfileAndDatabase returns the profile if it is selected', () => {
let serverTreeView = TypeMoq.Mock.ofInstance({ getSelection: () => undefined } as ServerTreeView);
serverTreeView.setup(x => x.getSelection()).returns(() => [connection]);
objectExplorerService.registerServerTreeView(serverTreeView.object);
let selectedProfileAndDatabase = objectExplorerService.getSelectedProfileAndDatabase();
assert.equal(selectedProfileAndDatabase.profile, connection);
assert.equal(selectedProfileAndDatabase.databaseName, undefined);
});
test('getSelectedProfileAndDatabase returns the profile but no database if children of a server are selected', () => {
let serverTreeView = TypeMoq.Mock.ofInstance({ getSelection: () => undefined } as ServerTreeView);
let databaseNode = new TreeNode(NodeType.Folder, 'Folder1', false, 'testServerName\\Folder1', '', '', undefined, undefined);
databaseNode.connection = connection;
serverTreeView.setup(x => x.getSelection()).returns(() => [databaseNode]);
objectExplorerService.registerServerTreeView(serverTreeView.object);
let selectedProfileAndDatabase = objectExplorerService.getSelectedProfileAndDatabase();
assert.equal(selectedProfileAndDatabase.profile, connection);
assert.equal(selectedProfileAndDatabase.databaseName, undefined);
});
test('getSelectedProfileAndDatabase returns the profile and database if children of a database node are selected', () => {
let serverTreeView = TypeMoq.Mock.ofInstance({ getSelection: () => undefined } as ServerTreeView);
let databaseMetadata = {
metadataType: 0,
metadataTypeName: 'Database',
urn: '//server/db1/',
name: 'Db1',
schema: undefined
};
let databaseName = 'Db1';
let databaseNode = new TreeNode(NodeType.Database, databaseName, false, 'testServerName\\Db1', '', '', undefined, databaseMetadata);
let tablesNode = new TreeNode(NodeType.Folder, 'Tables', false, 'testServerName\\Db1\\tables', '', '', databaseNode, undefined);
databaseNode.connection = connection;
databaseNode.children = [tablesNode];
serverTreeView.setup(x => x.getSelection()).returns(() => [tablesNode]);
objectExplorerService.registerServerTreeView(serverTreeView.object);
let selectedProfileAndDatabase = objectExplorerService.getSelectedProfileAndDatabase();
assert.equal(selectedProfileAndDatabase.profile, connection);
assert.equal(selectedProfileAndDatabase.databaseName, databaseName);
});
test('getSelectedProfileAndDatabase returns undefined when there is no selection', () => {
let serverTreeView = TypeMoq.Mock.ofInstance({ getSelection: () => undefined } as ServerTreeView);
serverTreeView.setup(x => x.getSelection()).returns(() => []);
objectExplorerService.registerServerTreeView(serverTreeView.object);
let selectedProfileAndDatabase = objectExplorerService.getSelectedProfileAndDatabase();
assert.equal(selectedProfileAndDatabase, undefined);
});
});

View File

@@ -0,0 +1,113 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the Source EULA. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
'use strict';
import * as assert from 'assert';
import * as TypeMoq from 'typemoq';
import * as TaskUtilities from 'sql/workbench/common/taskUtilities';
import { IObjectExplorerService } from 'sql/parts/registeredServer/common/objectExplorerService';
import { TestConnectionManagementService } from 'sqltest/stubs/connectionManagementService.test';
import { IConnectionProfile } from 'sql/parts/connection/common/interfaces';
import { ConnectionProfile } from 'sql/parts/connection/common/connectionProfile';
import { WorkbenchEditorTestService } from 'sqltest/stubs/workbenchEditorTestService';
import URI from 'vs/base/common/uri';
import { UntitledEditorInput } from 'vs/workbench/common/editor/untitledEditorInput';
import { QueryInput } from 'sql/parts/query/common/queryInput';
suite('TaskUtilities', function() {
test('getCurrentGlobalConnection returns the selected OE server if a server or one of its children is selected', () => {
let connectionProfile = { databaseName: 'test_database', id: 'test_id', authenticationType: 'SQL Login', password: 'test_password', serverName: 'test_server', userName: 'test_user' } as IConnectionProfile;
let mockObjectExplorerService = TypeMoq.Mock.ofInstance({isFocused: () => undefined, getSelectedProfileAndDatabase: () => undefined } as IObjectExplorerService);
let mockConnectionManagementService = TypeMoq.Mock.ofType(TestConnectionManagementService);
let mockWorkbenchEditorService = TypeMoq.Mock.ofType(WorkbenchEditorTestService);
let expectedProfile = new ConnectionProfile(undefined, connectionProfile);
mockObjectExplorerService.setup(x => x.isFocused()).returns(() => true);
mockObjectExplorerService.setup(x => x.getSelectedProfileAndDatabase()).returns(() => {
return { profile: expectedProfile, databaseName: undefined };
});
mockConnectionManagementService.setup(x => x.isProfileConnected(TypeMoq.It.is(profile => profile === expectedProfile))).returns(() => true);
mockWorkbenchEditorService.setup(x => x.getActiveEditorInput()).returns(() => undefined);
// If I call getCurrentGlobalConnection, it should return the expected server profile
let actualProfile = TaskUtilities.getCurrentGlobalConnection(mockObjectExplorerService.object, mockConnectionManagementService.object, mockWorkbenchEditorService.object);
assert.equal(actualProfile, expectedProfile);
});
test('getCurrentGlobalConnection returns the selected OE database if a database or its children is selected', () => {
let connectionProfile = { databaseName: 'test_database', id: 'test_id', authenticationType: 'SQL Login', password: 'test_password', serverName: 'test_server', userName: 'test_user' } as IConnectionProfile;
let mockObjectExplorerService = TypeMoq.Mock.ofInstance({isFocused: () => undefined, getSelectedProfileAndDatabase: () => undefined } as IObjectExplorerService);
let mockConnectionManagementService = TypeMoq.Mock.ofType(TestConnectionManagementService);
let mockWorkbenchEditorService = TypeMoq.Mock.ofType(WorkbenchEditorTestService);
let serverProfile = new ConnectionProfile(undefined, connectionProfile);
let dbName = 'test_database';
mockObjectExplorerService.setup(x => x.isFocused()).returns(() => true);
mockObjectExplorerService.setup(x => x.getSelectedProfileAndDatabase()).returns(() => {
return { profile: serverProfile, databaseName: dbName };
});
mockConnectionManagementService.setup(x => x.isProfileConnected(TypeMoq.It.is(profile => profile === serverProfile))).returns(() => true);
mockWorkbenchEditorService.setup(x => x.getActiveEditorInput()).returns(() => undefined);
// If I call getCurrentGlobalConnection, it should return the expected database profile
let actualProfile = TaskUtilities.getCurrentGlobalConnection(mockObjectExplorerService.object, mockConnectionManagementService.object, mockWorkbenchEditorService.object);
assert.equal(actualProfile.databaseName, dbName);
assert.notEqual(actualProfile.id, serverProfile.id);
// Other connection attributes still match
assert.equal(actualProfile.authenticationType, serverProfile.authenticationType);
assert.equal(actualProfile.password, serverProfile.password);
assert.equal(actualProfile.serverName, serverProfile.serverName);
assert.equal(actualProfile.userName, serverProfile.userName);
});
test('getCurrentGlobalConnection returns the connection from the active tab, if there is one and OE is not focused', () => {
let connectionProfile = { databaseName: 'test_database', id: 'test_id', authenticationType: 'SQL Login', password: 'test_password', serverName: 'test_server', userName: 'test_user' } as IConnectionProfile;
let mockObjectExplorerService = TypeMoq.Mock.ofInstance({isFocused: () => undefined, getSelectedProfileAndDatabase: () => undefined } as IObjectExplorerService);
let mockConnectionManagementService = TypeMoq.Mock.ofType(TestConnectionManagementService);
let mockWorkbenchEditorService = TypeMoq.Mock.ofType(WorkbenchEditorTestService);
let oeProfile = new ConnectionProfile(undefined, connectionProfile);
let connectionProfile2 = Object.assign({}, connectionProfile);
connectionProfile2.serverName = 'test_server_2';
connectionProfile2.id = 'test_id_2';
let tabProfile = new ConnectionProfile(undefined, connectionProfile2);
mockObjectExplorerService.setup(x => x.isFocused()).returns(() => false);
mockObjectExplorerService.setup(x => x.getSelectedProfileAndDatabase()).returns(() => {
return { profile: oeProfile, databaseName: undefined };
});
mockConnectionManagementService.setup(x => x.isProfileConnected(TypeMoq.It.is(profile => profile === oeProfile || profile === tabProfile))).returns(() => true);
// Mock the workbench service to return the active tab connection
let tabConnectionUri = 'test_uri';
let editorInput = new UntitledEditorInput(URI.parse(tabConnectionUri), false, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined);
let queryInput = new QueryInput(undefined, undefined, editorInput, undefined, undefined, undefined, undefined, undefined);
mockWorkbenchEditorService.setup(x => x.getActiveEditorInput()).returns(() => queryInput);
mockConnectionManagementService.setup(x => x.getConnectionProfile(tabConnectionUri)).returns(() => tabProfile);
// If I call getCurrentGlobalConnection, it should return the expected profile from the active tab
let actualProfile = TaskUtilities.getCurrentGlobalConnection(mockObjectExplorerService.object, mockConnectionManagementService.object, mockWorkbenchEditorService.object);
assert.equal(actualProfile.databaseName, tabProfile.databaseName);
assert.equal(actualProfile.authenticationType, tabProfile.authenticationType);
assert.equal(actualProfile.password, tabProfile.password);
assert.equal(actualProfile.serverName, tabProfile.serverName);
assert.equal(actualProfile.userName, tabProfile.userName);
});
test('getCurrentGlobalConnection returns the connection from OE if there is no active tab, even if OE is not focused', () => {
let connectionProfile = { databaseName: 'test_database', id: 'test_id', authenticationType: 'SQL Login', password: 'test_password', serverName: 'test_server', userName: 'test_user' } as IConnectionProfile;
let mockObjectExplorerService = TypeMoq.Mock.ofInstance({isFocused: () => undefined, getSelectedProfileAndDatabase: () => undefined } as IObjectExplorerService);
let mockConnectionManagementService = TypeMoq.Mock.ofType(TestConnectionManagementService);
let mockWorkbenchEditorService = TypeMoq.Mock.ofType(WorkbenchEditorTestService);
let oeProfile = new ConnectionProfile(undefined, connectionProfile);
mockObjectExplorerService.setup(x => x.isFocused()).returns(() => false);
mockObjectExplorerService.setup(x => x.getSelectedProfileAndDatabase()).returns(() => {
return { profile: oeProfile, databaseName: undefined };
});
mockConnectionManagementService.setup(x => x.isProfileConnected(TypeMoq.It.is(profile => profile === oeProfile))).returns(() => true);
mockWorkbenchEditorService.setup(x => x.getActiveEditorInput()).returns(() => undefined);
// If I call getCurrentGlobalConnection, it should return the expected profile from OE
let actualProfile = TaskUtilities.getCurrentGlobalConnection(mockObjectExplorerService.object, mockConnectionManagementService.object, mockWorkbenchEditorService.object);
assert.equal(actualProfile, oeProfile);
});
});