Schema Compare test coverage (#11042)

* Few tests for Schema Compare

* Addressed comment- removed wait in test

* Split setEndpointInfo for database and dacpac
This commit is contained in:
Sakshi Sharma
2020-06-24 14:03:02 -07:00
committed by GitHub
parent 561b881200
commit de263eacd1
5 changed files with 331 additions and 162 deletions

View File

@@ -4,16 +4,16 @@
*--------------------------------------------------------------------------------------------*/
import * as should from 'should';
import * as azdata from 'azdata';
import * as vscode from 'vscode';
import * as mssql from '../../../mssql';
import * as TypeMoq from 'typemoq';
import * as loc from '../localizedConstants';
import 'mocha';
import { SchemaCompareDialog } from './../dialogs/schemaCompareDialog';
import { SchemaCompareMainWindow } from '../schemaCompareMainWindow';
import { SchemaCompareTestService } from './testSchemaCompareService';
import { SchemaCompareTestService, testStateScmp } from './testSchemaCompareService';
import { createContext, TestContext } from './testContext';
import { mockIConnectionProfile, mockDacpacEndpoint, mockFilePath } from './testUtils';
import { mockIConnectionProfile, mockFilePath, setDacpacEndpointInfo, setDatabaseEndpointInfo, shouldThrowSpecificError } from './testUtils';
// Mock test data
const mocksource: string = 'source.dacpac';
@@ -26,7 +26,7 @@ before(async function (): Promise<void> {
testContext = createContext();
});
describe('SchemaCompareDialog.openDialog', function (): void {
beforeEach(() => {
before(() => {
mockExtensionContext = TypeMoq.Mock.ofType<vscode.ExtensionContext>();
mockExtensionContext.setup(x => x.extensionPath).returns(() => '');
});
@@ -42,27 +42,22 @@ describe('SchemaCompareDialog.openDialog', function (): void {
});
});
describe('SchemaCompareResult.start', function (): void {
beforeEach(() => {
describe('SchemaCompareMainWindow.start', function (): void {
before(() => {
mockExtensionContext = TypeMoq.Mock.ofType<vscode.ExtensionContext>();
mockExtensionContext.setup(x => x.extensionPath).returns(() => '');
});
it('Should be correct when created.', async function (): Promise<void> {
let sc = new SchemaCompareTestService();
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;
should(result.getComparisonResult() === undefined);
let sourceEndpointInfo : mssql.SchemaCompareEndpointInfo = {...mockDacpacEndpoint};
let targetEndpointInfo : mssql.SchemaCompareEndpointInfo = {...mockDacpacEndpoint};
result.sourceEndpointInfo = sourceEndpointInfo;
result.sourceEndpointInfo.packageFilePath = mocksource;
result.targetEndpointInfo = targetEndpointInfo;
result.targetEndpointInfo.packageFilePath = mocktarget;
result.sourceEndpointInfo = await setDacpacEndpointInfo(mocksource);
result.targetEndpointInfo = await setDacpacEndpointInfo(mocktarget);
await result.execute();
should(result.getComparisonResult() !== undefined);
@@ -74,8 +69,6 @@ describe('SchemaCompareResult.start', function (): void {
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;
should.equal(result.sourceEndpointInfo, undefined);
should.equal(result.targetEndpointInfo, undefined);
@@ -86,8 +79,6 @@ describe('SchemaCompareResult.start', function (): void {
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);
@@ -102,8 +93,6 @@ describe('SchemaCompareResult.start', function (): void {
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;
should.notEqual(result.sourceEndpointInfo, undefined);
should.equal(result.sourceEndpointInfo.endpointType, mssql.SchemaCompareEndpointType.Dacpac);
@@ -111,3 +100,86 @@ describe('SchemaCompareResult.start', function (): void {
should.equal(result.targetEndpointInfo, undefined);
});
});
describe('SchemaCompareMainWindow.execute', function (): void {
before(() => {
mockExtensionContext = TypeMoq.Mock.ofType<vscode.ExtensionContext>();
mockExtensionContext.setup(x => x.extensionPath).returns(() => '');
testContext = createContext();
});
beforeEach(async function (): Promise<void> {
testContext.apiWrapper.reset();
});
it('Should fail for failing Schema Compare service', async function (): Promise<void> {
let sc = new SchemaCompareTestService(testStateScmp.FAILURE);
testContext.apiWrapper.setup(x => x.showErrorMessage(TypeMoq.It.isAny())).returns((s) => { throw new Error(s); });
let result = new SchemaCompareMainWindow(testContext.apiWrapper.object, sc, mockExtensionContext.object);
await result.start(null);
should(result.getComparisonResult() === undefined);
result.sourceEndpointInfo = await setDacpacEndpointInfo(mocksource);
result.targetEndpointInfo = await setDacpacEndpointInfo(mocktarget);
await shouldThrowSpecificError(async () => await result.execute(), loc.compareErrorMessage('Test failure'));
});
it('Should exit for failing Schema Compare service', async function (): Promise<void> {
let sc = new SchemaCompareTestService(testStateScmp.FAILURE);
testContext.apiWrapper.setup(x => x.showErrorMessage(TypeMoq.It.isAny())).returns(() => Promise.resolve(''));
let result = new SchemaCompareMainWindow(testContext.apiWrapper.object, sc, mockExtensionContext.object);
await result.start(null);
should(result.getComparisonResult() === undefined);
result.sourceEndpointInfo = await setDacpacEndpointInfo(mocksource);
result.targetEndpointInfo = await setDacpacEndpointInfo(mocktarget);
await result.execute();
testContext.apiWrapper.verify(x => x.showErrorMessage(TypeMoq.It.isAny()), TypeMoq.Times.once());
});
it('Should disable script button and apply button for Schema Compare service for dacpac', async function (): Promise<void> {
let sc = new SchemaCompareTestService(testStateScmp.SUCCESS_NOT_EQUAL);
let result = new SchemaCompareMainWindow(testContext.apiWrapper.object, sc, mockExtensionContext.object);
await result.start(null);
should(result.getComparisonResult() === undefined);
result.sourceEndpointInfo = await setDacpacEndpointInfo(mocksource);
result.targetEndpointInfo = await setDacpacEndpointInfo(mocktarget);
await result.execute();
//Generate script button and apply button should be disabled for dacpac comparison
should(result.verifyButtonsState(false, false)).equal(true);
});
it('Should disable script button and apply button for Schema Compare service for database', async function (): Promise<void> {
let sc = new SchemaCompareTestService(testStateScmp.SUCCESS_NOT_EQUAL);
let result = new SchemaCompareMainWindow(testContext.apiWrapper.object, sc, mockExtensionContext.object);
await result.start(null);
should(result.getComparisonResult() === undefined);
result.sourceEndpointInfo = await setDacpacEndpointInfo(mocksource);
result.targetEndpointInfo = await setDatabaseEndpointInfo();
await result.execute();
//Generate script button and apply button should be enabled for database comparison
should(result.verifyButtonsState(true, true)).equal(true);
});
});

View File

@@ -9,6 +9,16 @@ import * as mssql from '../../../mssql';
export class SchemaCompareTestService implements mssql.ISchemaCompareService {
testOperationId: string = 'Test Operation Id';
testState: testStateScmp;
constructor(state?: testStateScmp) {
if (state) {
this.testState = state;
}
else {
this.testState = testStateScmp.SUCCESS_EQUAL;
}
}
schemaComparePublishChanges(operationId: string, targetServerName: string, targetDatabaseName: string, taskExecutionMode: azdata.TaskExecutionMode): Thenable<azdata.ResultStatus> {
throw new Error('Method not implemented.');
@@ -38,13 +48,56 @@ export class SchemaCompareTestService implements mssql.ISchemaCompareService {
}
schemaCompare(operationId: string, sourceEndpointInfo: mssql.SchemaCompareEndpointInfo, targetEndpointInfo: mssql.SchemaCompareEndpointInfo, taskExecutionMode: azdata.TaskExecutionMode): Thenable<mssql.SchemaCompareResult> {
let result: mssql.SchemaCompareResult = {
operationId: this.testOperationId,
areEqual: true,
differences: [],
success: true,
errorMessage: ''
};
let result: mssql.SchemaCompareResult;
if (this.testState === testStateScmp.FAILURE) {
result = {
operationId: this.testOperationId,
areEqual: false,
differences: [],
success: false,
errorMessage: 'Test failure'
};
}
else if (this.testState === testStateScmp.SUCCESS_NOT_EQUAL) {
result = {
operationId: this.testOperationId,
areEqual: false,
differences: [{
updateAction: 2,
differenceType: 0,
name: 'SqlTable',
sourceValue: ['dbo', 'table1'],
targetValue: null,
parent: null,
children: [{
updateAction: 2,
differenceType: 0,
name: 'SqlSimpleColumn',
sourceValue: ['dbo', 'table1', 'id'],
targetValue: null,
parent: null,
children: [],
sourceScript: '',
targetScript: null,
included: false
}],
sourceScript: 'CREATE TABLE [dbo].[table1](id int)',
targetScript: null,
included: true
}],
success: true,
errorMessage: ''
};
}
else {
result = {
operationId: this.testOperationId,
areEqual: true,
differences: [],
success: true,
errorMessage: null
};
}
return Promise.resolve(result);
}
@@ -63,3 +116,9 @@ export class SchemaCompareTestService implements mssql.ISchemaCompareService {
registerOnUpdated(handler: () => any): void {
}
}
export enum testStateScmp {
SUCCESS_EQUAL,
SUCCESS_NOT_EQUAL,
FAILURE
}

View File

@@ -99,3 +99,24 @@ export async function shouldThrowSpecificError(block: Function, expectedMessage:
throw new AssertionError({ message: `Operation succeeded, but expected failure with exception: "${expectedMessage}".${details ? ' ' + details : ''}` });
}
}
export async function setDacpacEndpointInfo(path: string): Promise<mssql.SchemaCompareEndpointInfo> {
let endpointInfo: mssql.SchemaCompareEndpointInfo;
endpointInfo = { ...mockDacpacEndpoint };
endpointInfo.packageFilePath = path;
return endpointInfo;
}
export async function setDatabaseEndpointInfo(): Promise<mssql.SchemaCompareEndpointInfo> {
let endpointInfo: mssql.SchemaCompareEndpointInfo;
let dbName = 'My Database';
let serverName = 'My Server';
endpointInfo = { ...mockDatabaseEndpoint };
endpointInfo.databaseName = dbName;
endpointInfo.serverName = serverName;
return endpointInfo;
}