mirror of
https://github.com/ckaczor/azuredatastudio.git
synced 2026-02-16 18:46:40 -05:00
Tests for Schema Compare utils file (#10822)
* Introduced ApiWrapper for testability * Added tests for coverage of utils.ts * Addressed comments
This commit is contained in:
@@ -12,14 +12,19 @@ import 'mocha';
|
||||
import { SchemaCompareDialog } from './../dialogs/schemaCompareDialog';
|
||||
import { SchemaCompareMainWindow } from '../schemaCompareMainWindow';
|
||||
import { SchemaCompareTestService } from './testSchemaCompareService';
|
||||
import { mockConnectionProfile, mockDacpacEndpoint } from './testUtils';
|
||||
import { createContext, TestContext } from './testContext';
|
||||
import { mockIConnectionProfile, mockDacpacEndpoint, mockFilePath } from './testUtils';
|
||||
|
||||
// Mock test data
|
||||
const mocksource: string = 'source.dacpac';
|
||||
const mocktarget: string = 'target.dacpac';
|
||||
|
||||
let mockExtensionContext: TypeMoq.IMock<vscode.ExtensionContext>;
|
||||
let testContext: TestContext;
|
||||
|
||||
before(async function (): Promise<void> {
|
||||
testContext = createContext();
|
||||
});
|
||||
describe('SchemaCompareDialog.openDialog', function (): void {
|
||||
beforeEach(() => {
|
||||
mockExtensionContext = TypeMoq.Mock.ofType<vscode.ExtensionContext>();
|
||||
@@ -27,7 +32,7 @@ describe('SchemaCompareDialog.openDialog', function (): void {
|
||||
});
|
||||
|
||||
it('Should be correct when created.', async function (): Promise<void> {
|
||||
let schemaCompareResult = new SchemaCompareMainWindow(undefined, mockExtensionContext.object);
|
||||
let schemaCompareResult = new SchemaCompareMainWindow(testContext.apiWrapper.object, undefined, mockExtensionContext.object);
|
||||
let dialog = new SchemaCompareDialog(schemaCompareResult);
|
||||
await dialog.openDialog();
|
||||
|
||||
@@ -45,7 +50,7 @@ describe('SchemaCompareResult.start', function (): void {
|
||||
it('Should be correct when created.', async function (): Promise<void> {
|
||||
let sc = new SchemaCompareTestService();
|
||||
|
||||
let result = new SchemaCompareMainWindow(sc, mockExtensionContext.object);
|
||||
let result = new SchemaCompareMainWindow(testContext.apiWrapper.object, sc, mockExtensionContext.object);
|
||||
await result.start(null);
|
||||
let promise = new Promise(resolve => setTimeout(resolve, 5000)); // to ensure comparison result view is initialized
|
||||
await promise;
|
||||
@@ -67,7 +72,7 @@ describe('SchemaCompareResult.start', function (): void {
|
||||
it('Should start with the source as undefined', async function (): Promise<void> {
|
||||
let sc = new SchemaCompareTestService();
|
||||
|
||||
let result = new SchemaCompareMainWindow(sc, mockExtensionContext.object);
|
||||
let result = new SchemaCompareMainWindow(testContext.apiWrapper.object, sc, mockExtensionContext.object);
|
||||
await result.start(undefined);
|
||||
let promise = new Promise(resolve => setTimeout(resolve, 5000)); // to ensure comparison result view is initialized
|
||||
await promise;
|
||||
@@ -79,23 +84,23 @@ describe('SchemaCompareResult.start', function (): void {
|
||||
it('Should start with the source as database', async function (): Promise<void> {
|
||||
let sc = new SchemaCompareTestService();
|
||||
|
||||
let result = new SchemaCompareMainWindow(sc, mockExtensionContext.object);
|
||||
await result.start({connectionProfile: mockConnectionProfile});
|
||||
let result = new SchemaCompareMainWindow(testContext.apiWrapper.object, sc, mockExtensionContext.object);
|
||||
await result.start({connectionProfile: mockIConnectionProfile});
|
||||
let promise = new Promise(resolve => setTimeout(resolve, 5000)); // to ensure comparison result view is initialized
|
||||
await promise;
|
||||
|
||||
should.notEqual(result.sourceEndpointInfo, undefined);
|
||||
should.equal(result.sourceEndpointInfo.endpointType, mssql.SchemaCompareEndpointType.Database);
|
||||
should.equal(result.sourceEndpointInfo.serverName, mockConnectionProfile.serverName);
|
||||
should.equal(result.sourceEndpointInfo.databaseName, mockConnectionProfile.databaseName);
|
||||
should.equal(result.sourceEndpointInfo.serverName, mockIConnectionProfile.serverName);
|
||||
should.equal(result.sourceEndpointInfo.databaseName, mockIConnectionProfile.databaseName);
|
||||
should.equal(result.targetEndpointInfo, undefined);
|
||||
});
|
||||
|
||||
it('Should start with the source as dacpac.', async function (): Promise<void> {
|
||||
let sc = new SchemaCompareTestService();
|
||||
|
||||
let result = new SchemaCompareMainWindow(sc, mockExtensionContext.object);
|
||||
const dacpacPath = 'C:\\users\\test\\test.dacpac';
|
||||
let result = new SchemaCompareMainWindow(testContext.apiWrapper.object, sc, mockExtensionContext.object);
|
||||
const dacpacPath = mockFilePath;
|
||||
await result.start(dacpacPath);
|
||||
let promise = new Promise(resolve => setTimeout(resolve, 5000)); // to ensure comparison result view is initialized
|
||||
await promise;
|
||||
|
||||
40
extensions/schema-compare/src/test/testContext.ts
Normal file
40
extensions/schema-compare/src/test/testContext.ts
Normal file
@@ -0,0 +1,40 @@
|
||||
/*---------------------------------------------------------------------------------------------
|
||||
* Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
* Licensed under the Source EULA. See License.txt in the project root for license information.
|
||||
*--------------------------------------------------------------------------------------------*/
|
||||
|
||||
import * as vscode from 'vscode';
|
||||
import * as path from 'path';
|
||||
import * as TypeMoq from 'typemoq';
|
||||
import { ApiWrapper } from '../common/apiWrapper';
|
||||
|
||||
export interface TestContext {
|
||||
apiWrapper: TypeMoq.IMock<ApiWrapper>;
|
||||
context: vscode.ExtensionContext;
|
||||
}
|
||||
|
||||
export function createContext(): TestContext {
|
||||
let extensionPath = path.join(__dirname, '..', '..');
|
||||
|
||||
return {
|
||||
apiWrapper: TypeMoq.Mock.ofType(ApiWrapper),
|
||||
context: {
|
||||
subscriptions: [],
|
||||
workspaceState: {
|
||||
get: () => { return undefined; },
|
||||
update: () => { return Promise.resolve(); }
|
||||
},
|
||||
globalState: {
|
||||
get: () => { return Promise.resolve(); },
|
||||
update: () => { return Promise.resolve(); }
|
||||
},
|
||||
extensionPath: extensionPath,
|
||||
asAbsolutePath: () => { return ''; },
|
||||
storagePath: '',
|
||||
globalStoragePath: '',
|
||||
logPath: '',
|
||||
extensionUri: vscode.Uri.parse(''),
|
||||
environmentVariableCollection: undefined as any
|
||||
},
|
||||
};
|
||||
}
|
||||
@@ -5,9 +5,11 @@
|
||||
|
||||
import * as azdata from 'azdata';
|
||||
import * as mssql from '../../../mssql';
|
||||
import should = require('should');
|
||||
import { AssertionError } from 'assert';
|
||||
|
||||
// Mock test data
|
||||
export const mockConnectionProfile: azdata.IConnectionProfile = {
|
||||
export const mockIConnectionProfile: azdata.IConnectionProfile = {
|
||||
connectionName: 'My Connection',
|
||||
serverName: 'My Server',
|
||||
databaseName: 'My Database',
|
||||
@@ -23,6 +25,35 @@ export const mockConnectionProfile: azdata.IConnectionProfile = {
|
||||
options: null
|
||||
};
|
||||
|
||||
export const mockConnectionProfile: azdata.connection.ConnectionProfile = {
|
||||
providerId: 'My Provider',
|
||||
connectionId: 'My Id',
|
||||
connectionName: 'My Connection',
|
||||
serverName: 'My Server',
|
||||
databaseName: 'My Database',
|
||||
userName: 'My User',
|
||||
password: 'My Pwd',
|
||||
authenticationType: 'SqlLogin',
|
||||
savePassword: false,
|
||||
groupFullName: 'My groupName',
|
||||
groupId: 'My GroupId',
|
||||
saveProfile: true,
|
||||
options: {
|
||||
server: 'My Server',
|
||||
database: 'My Database',
|
||||
user: 'My User',
|
||||
password: 'My Pwd',
|
||||
authenticationType: 'SqlLogin'
|
||||
}
|
||||
};
|
||||
|
||||
export const mockConnectionResult: azdata.ConnectionResult = {
|
||||
connected: false,
|
||||
connectionId: undefined,
|
||||
errorMessage: 'Login failed for user \'sa\'',
|
||||
errorCode: 18456
|
||||
};
|
||||
|
||||
export const mockConnectionInfo = {
|
||||
options: {},
|
||||
serverName: 'My Server',
|
||||
@@ -53,3 +84,18 @@ export const mockDatabaseEndpoint: mssql.SchemaCompareEndpointInfo = {
|
||||
packageFilePath: '',
|
||||
connectionDetails: undefined
|
||||
};
|
||||
|
||||
export async function shouldThrowSpecificError(block: Function, expectedMessage: string, details?: string) {
|
||||
let succeeded = false;
|
||||
try {
|
||||
await block();
|
||||
succeeded = true;
|
||||
}
|
||||
catch (err) {
|
||||
should(err.message).equal(expectedMessage);
|
||||
}
|
||||
|
||||
if (succeeded) {
|
||||
throw new AssertionError({ message: `Operation succeeded, but expected failure with exception: "${expectedMessage}".${details ? ' ' + details : ''}` });
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,9 +4,15 @@
|
||||
*--------------------------------------------------------------------------------------------*/
|
||||
|
||||
import * as should from 'should';
|
||||
import * as azdata from 'azdata';
|
||||
import * as mssql from '../../../mssql';
|
||||
import * as loc from '../localizedConstants';
|
||||
import * as TypeMoq from 'typemoq';
|
||||
import {getEndpointName, verifyConnectionAndGetOwnerUri } from '../utils';
|
||||
import {mockDacpacEndpoint, mockDatabaseEndpoint, mockFilePath, mockConnectionInfo} from './testUtils';
|
||||
import {mockDacpacEndpoint, mockDatabaseEndpoint, mockFilePath, mockConnectionInfo, shouldThrowSpecificError, mockConnectionResult, mockConnectionProfile} from './testUtils';
|
||||
import { createContext, TestContext } from './testContext';
|
||||
|
||||
let testContext: TestContext;
|
||||
|
||||
describe('utils: Tests to verify getEndpointName', function (): void {
|
||||
it('Should generate correct endpoint information', async () => {
|
||||
@@ -18,8 +24,8 @@ describe('utils: Tests to verify getEndpointName', function (): void {
|
||||
});
|
||||
|
||||
it('Should get endpoint information from ConnectionInfo', async () => {
|
||||
let testDatabaseEndpoint: mssql.SchemaCompareEndpointInfo = mockDatabaseEndpoint;
|
||||
testDatabaseEndpoint.connectionDetails = mockConnectionInfo;
|
||||
let testDatabaseEndpoint: mssql.SchemaCompareEndpointInfo = {...mockDatabaseEndpoint};
|
||||
testDatabaseEndpoint.connectionDetails = {...mockConnectionInfo};
|
||||
|
||||
should(getEndpointName(testDatabaseEndpoint)).equal('My Server.My Database');
|
||||
});
|
||||
@@ -35,10 +41,14 @@ describe('utils: Tests to verify getEndpointName', function (): void {
|
||||
});
|
||||
});
|
||||
|
||||
describe('utils: Tests to verify verifyConnectionAndGetOwnerUri', function (): void {
|
||||
describe('utils: Basic tests to verify verifyConnectionAndGetOwnerUri', function (): void {
|
||||
before(async function (): Promise<void> {
|
||||
testContext = createContext();
|
||||
});
|
||||
|
||||
it('Should return undefined for endpoint as dacpac', async function (): Promise<void> {
|
||||
let ownerUri = undefined;
|
||||
ownerUri = await verifyConnectionAndGetOwnerUri(mockDacpacEndpoint, 'test');
|
||||
ownerUri = await verifyConnectionAndGetOwnerUri(mockDacpacEndpoint, 'test', testContext.apiWrapper.object);
|
||||
|
||||
should(ownerUri).equal(undefined);
|
||||
});
|
||||
@@ -48,8 +58,73 @@ describe('utils: Tests to verify verifyConnectionAndGetOwnerUri', function (): v
|
||||
let testDatabaseEndpoint: mssql.SchemaCompareEndpointInfo = {...mockDatabaseEndpoint};
|
||||
testDatabaseEndpoint.connectionDetails = undefined;
|
||||
|
||||
ownerUri = await verifyConnectionAndGetOwnerUri(testDatabaseEndpoint, 'test');
|
||||
ownerUri = await verifyConnectionAndGetOwnerUri(testDatabaseEndpoint, 'test', testContext.apiWrapper.object);
|
||||
|
||||
should(ownerUri).equal(undefined);
|
||||
});
|
||||
});
|
||||
|
||||
describe('utils: In-depth tests to verify verifyConnectionAndGetOwnerUri', function (): void {
|
||||
before(async function (): Promise<void> {
|
||||
testContext = createContext();
|
||||
});
|
||||
|
||||
it('Should throw an error asking to make a connection', async function (): Promise<void> {
|
||||
let getConnectionsResults: azdata.connection.ConnectionProfile[] = [];
|
||||
let connection = {...mockConnectionResult};
|
||||
let testDatabaseEndpoint: mssql.SchemaCompareEndpointInfo = {...mockDatabaseEndpoint};
|
||||
testDatabaseEndpoint.connectionDetails = {...mockConnectionInfo};
|
||||
const getConnectionString = loc.getConnectionString('test');
|
||||
|
||||
testContext.apiWrapper.setup(x => x.connect(TypeMoq.It.isAny(), TypeMoq.It.isAny(), TypeMoq.It.isAny())).returns(() => { return Promise.resolve(connection); });
|
||||
testContext.apiWrapper.setup(x => x.getUriForConnection(TypeMoq.It.isAny())).returns(() => { return Promise.resolve(undefined); });
|
||||
testContext.apiWrapper.setup(x => x.getConnections(TypeMoq.It.isAny())).returns(() => { return Promise.resolve(getConnectionsResults); });
|
||||
testContext.apiWrapper.setup(x => x.showWarningMessage(TypeMoq.It.isAny(), TypeMoq.It.isAny(), TypeMoq.It.isAny())).returns((s) => { throw new Error(s); });
|
||||
|
||||
await shouldThrowSpecificError(async () => await verifyConnectionAndGetOwnerUri(testDatabaseEndpoint, 'test', testContext.apiWrapper.object), getConnectionString);
|
||||
});
|
||||
|
||||
it('Should throw an error for login failure', async function (): Promise<void> {
|
||||
let getConnectionsResults: azdata.connection.ConnectionProfile[] = [{...mockConnectionProfile}];
|
||||
let connection = {...mockConnectionResult};
|
||||
let testDatabaseEndpoint: mssql.SchemaCompareEndpointInfo = {...mockDatabaseEndpoint};
|
||||
testDatabaseEndpoint.connectionDetails = {...mockConnectionInfo};
|
||||
|
||||
testContext.apiWrapper.setup(x => x.connect(TypeMoq.It.isAny(), TypeMoq.It.isAny(), TypeMoq.It.isAny())).returns(() => { return Promise.resolve(connection); });
|
||||
testContext.apiWrapper.setup(x => x.getUriForConnection(TypeMoq.It.isAny())).returns(() => { return Promise.resolve(undefined); });
|
||||
testContext.apiWrapper.setup(x => x.getConnections(TypeMoq.It.isAny())).returns(() => { return Promise.resolve(getConnectionsResults); });
|
||||
testContext.apiWrapper.setup(x => x.showErrorMessage(TypeMoq.It.isAny())).returns((s) => { throw new Error(s); });
|
||||
|
||||
await shouldThrowSpecificError(async () => await verifyConnectionAndGetOwnerUri(testDatabaseEndpoint, 'test', testContext.apiWrapper.object), connection.errorMessage);
|
||||
});
|
||||
|
||||
it('Should throw an error for login failure with openConnectionDialog but no ownerUri', async function (): Promise<void> {
|
||||
let getConnectionsResults: azdata.connection.ConnectionProfile[] = [];
|
||||
let connection = {...mockConnectionResult};
|
||||
let testDatabaseEndpoint: mssql.SchemaCompareEndpointInfo = {...mockDatabaseEndpoint};
|
||||
testDatabaseEndpoint.connectionDetails = {...mockConnectionInfo};
|
||||
|
||||
testContext.apiWrapper.setup(x => x.connect(TypeMoq.It.isAny(), TypeMoq.It.isAny(), TypeMoq.It.isAny())).returns(() => { return Promise.resolve(connection); });
|
||||
testContext.apiWrapper.setup(x => x.getUriForConnection(TypeMoq.It.isAny())).returns(() => { return Promise.resolve(undefined); });
|
||||
testContext.apiWrapper.setup(x => x.getConnections(TypeMoq.It.isAny())).returns(() => { return Promise.resolve(getConnectionsResults); });
|
||||
testContext.apiWrapper.setup(x => x.showWarningMessage(TypeMoq.It.isAny(), TypeMoq.It.isAny(), TypeMoq.It.isAny())).returns(() => { return Promise.resolve(loc.YesButtonText); });
|
||||
testContext.apiWrapper.setup(x => x.openConnectionDialog(TypeMoq.It.isAny(), TypeMoq.It.isAny())).returns(() => { return Promise.resolve(undefined); });
|
||||
|
||||
await shouldThrowSpecificError(async () => await verifyConnectionAndGetOwnerUri(testDatabaseEndpoint, 'test', testContext.apiWrapper.object), connection.errorMessage);
|
||||
});
|
||||
|
||||
it('Should not throw an error and set ownerUri appropriately', async function (): Promise<void> {
|
||||
let ownerUri = undefined;
|
||||
let connection = {...mockConnectionResult};
|
||||
let testDatabaseEndpoint: mssql.SchemaCompareEndpointInfo = {...mockDatabaseEndpoint};
|
||||
let expectedOwnerUri: string = 'providerName:MSSQL|authenticationType:SqlLogin|database:My Database|server:My Server|user:My User|databaseDisplayName:My Database';
|
||||
testDatabaseEndpoint.connectionDetails = {...mockConnectionInfo};
|
||||
|
||||
testContext.apiWrapper.setup(x => x.connect(TypeMoq.It.isAny(), TypeMoq.It.isAny(), TypeMoq.It.isAny())).returns(() => { return Promise.resolve(connection); });
|
||||
testContext.apiWrapper.setup(x => x.getUriForConnection(TypeMoq.It.isAny())).returns(() => { return Promise.resolve(expectedOwnerUri); });
|
||||
|
||||
ownerUri = await verifyConnectionAndGetOwnerUri(testDatabaseEndpoint, 'test', testContext.apiWrapper.object);
|
||||
|
||||
should(ownerUri).equal(expectedOwnerUri);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user