From 06590ad0a24fab39090ce52954b973e51cd9823d Mon Sep 17 00:00:00 2001 From: Sakshi Sharma <57200045+SakshiS-harma@users.noreply.github.com> Date: Mon, 8 Jun 2020 10:44:16 -0700 Subject: [PATCH] Schema Compare tests for utils (#10759) * Added a few tests for utils file under Schema Compare * Addressed comment- moved mock test data to a common location --- .../src/test/schemaCompare.test.ts | 46 +++------------- .../schema-compare/src/test/testUtils.ts | 55 +++++++++++++++++++ .../schema-compare/src/test/utils.test.ts | 55 +++++++++++++++++++ extensions/schema-compare/src/utils.ts | 2 +- 4 files changed, 119 insertions(+), 39 deletions(-) create mode 100644 extensions/schema-compare/src/test/testUtils.ts create mode 100644 extensions/schema-compare/src/test/utils.test.ts diff --git a/extensions/schema-compare/src/test/schemaCompare.test.ts b/extensions/schema-compare/src/test/schemaCompare.test.ts index 597e0d9969..78f034f4cd 100644 --- a/extensions/schema-compare/src/test/schemaCompare.test.ts +++ b/extensions/schema-compare/src/test/schemaCompare.test.ts @@ -12,47 +12,12 @@ import 'mocha'; import { SchemaCompareDialog } from './../dialogs/schemaCompareDialog'; import { SchemaCompareMainWindow } from '../schemaCompareMainWindow'; import { SchemaCompareTestService } from './testSchemaCompareService'; +import { mockConnectionProfile, mockDacpacEndpoint } from './testUtils'; // Mock test data -const mockConnectionProfile: azdata.IConnectionProfile = { - connectionName: 'My Connection', - serverName: 'My Server', - databaseName: 'My Server', - userName: 'My User', - password: 'My Pwd', - authenticationType: 'SqlLogin', - savePassword: false, - groupFullName: 'My groupName', - groupId: 'My GroupId', - providerName: 'My Server', - saveProfile: true, - id: 'My Id', - options: null -}; - const mocksource: string = 'source.dacpac'; const mocktarget: string = 'target.dacpac'; -const mockSourceEndpoint: mssql.SchemaCompareEndpointInfo = { - endpointType: mssql.SchemaCompareEndpointType.Dacpac, - serverDisplayName: '', - serverName: '', - databaseName: '', - ownerUri: '', - packageFilePath: mocksource, - connectionDetails: undefined -}; - -const mockTargetEndpoint: mssql.SchemaCompareEndpointInfo = { - endpointType: mssql.SchemaCompareEndpointType.Dacpac, - serverDisplayName: '', - serverName: '', - databaseName: '', - ownerUri: '', - packageFilePath: mocktarget, - connectionDetails: undefined -}; - let mockExtensionContext: TypeMoq.IMock; describe('SchemaCompareDialog.openDialog', function (): void { @@ -86,8 +51,13 @@ describe('SchemaCompareResult.start', function (): void { await promise; should(result.getComparisonResult() === undefined); - result.sourceEndpointInfo = mockSourceEndpoint; - result.targetEndpointInfo = mockTargetEndpoint; + + let sourceEndpointInfo : mssql.SchemaCompareEndpointInfo = {...mockDacpacEndpoint}; + let targetEndpointInfo : mssql.SchemaCompareEndpointInfo = {...mockDacpacEndpoint}; + result.sourceEndpointInfo = sourceEndpointInfo; + result.sourceEndpointInfo.packageFilePath = mocksource; + result.targetEndpointInfo = targetEndpointInfo; + result.targetEndpointInfo.packageFilePath = mocktarget; await result.execute(); should(result.getComparisonResult() !== undefined); diff --git a/extensions/schema-compare/src/test/testUtils.ts b/extensions/schema-compare/src/test/testUtils.ts new file mode 100644 index 0000000000..2f3b966b4f --- /dev/null +++ b/extensions/schema-compare/src/test/testUtils.ts @@ -0,0 +1,55 @@ +/*--------------------------------------------------------------------------------------------- + * Copyright (c) Microsoft Corporation. All rights reserved. + * Licensed under the Source EULA. See License.txt in the project root for license information. + *--------------------------------------------------------------------------------------------*/ + +import * as azdata from 'azdata'; +import * as mssql from '../../../mssql'; + +// Mock test data +export const mockConnectionProfile: azdata.IConnectionProfile = { + 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', + providerName: 'My Provider', + saveProfile: true, + id: 'My Id', + options: null +}; + +export const mockConnectionInfo = { + options: {}, + serverName: 'My Server', + databaseName: 'My Database', + userName: 'My User', + password: 'My Pwd', + authenticationType: 'SqlLogin' +}; + +export const mockFilePath: string = 'test.dacpac'; + +export const mockDacpacEndpoint: mssql.SchemaCompareEndpointInfo = { + endpointType: mssql.SchemaCompareEndpointType.Dacpac, + serverDisplayName: '', + serverName: '', + databaseName: '', + ownerUri: '', + packageFilePath: mockFilePath, + connectionDetails: undefined +}; + +export const mockDatabaseEndpoint: mssql.SchemaCompareEndpointInfo = { + endpointType: mssql.SchemaCompareEndpointType.Database, + serverDisplayName: '', + serverName: '', + databaseName: '', + ownerUri: '', + packageFilePath: '', + connectionDetails: undefined +}; diff --git a/extensions/schema-compare/src/test/utils.test.ts b/extensions/schema-compare/src/test/utils.test.ts new file mode 100644 index 0000000000..ef46ac3313 --- /dev/null +++ b/extensions/schema-compare/src/test/utils.test.ts @@ -0,0 +1,55 @@ +/*--------------------------------------------------------------------------------------------- + * Copyright (c) Microsoft Corporation. All rights reserved. + * Licensed under the Source EULA. See License.txt in the project root for license information. + *--------------------------------------------------------------------------------------------*/ + +import * as should from 'should'; +import * as mssql from '../../../mssql'; +import {getEndpointName, verifyConnectionAndGetOwnerUri } from '../utils'; +import {mockDacpacEndpoint, mockDatabaseEndpoint, mockFilePath, mockConnectionInfo} from './testUtils'; + +describe('utils: Tests to verify getEndpointName', function (): void { + it('Should generate correct endpoint information', async () => { + let endpointInfo: mssql.SchemaCompareEndpointInfo; + + should(getEndpointName(endpointInfo)).equal(' '); + should(getEndpointName(mockDacpacEndpoint)).equal(mockFilePath); + should(getEndpointName(mockDatabaseEndpoint)).equal(' '); + }); + + it('Should get endpoint information from ConnectionInfo', async () => { + let testDatabaseEndpoint: mssql.SchemaCompareEndpointInfo = mockDatabaseEndpoint; + testDatabaseEndpoint.connectionDetails = mockConnectionInfo; + + should(getEndpointName(testDatabaseEndpoint)).equal('My Server.My Database'); + }); + + it('Should get correct endpoint information from SchemaCompareEndpointInfo', async () => { + let dbName = 'My Database'; + let serverName = 'My Server'; + let testDatabaseEndpoint: mssql.SchemaCompareEndpointInfo = {...mockDatabaseEndpoint}; + testDatabaseEndpoint.databaseName = dbName; + testDatabaseEndpoint.serverName = serverName; + + should(getEndpointName(testDatabaseEndpoint)).equal('My Server.My Database'); + }); +}); + +describe('utils: Tests to verify verifyConnectionAndGetOwnerUri', function (): void { + it('Should return undefined for endpoint as dacpac', async function (): Promise { + let ownerUri = undefined; + ownerUri = await verifyConnectionAndGetOwnerUri(mockDacpacEndpoint, 'test'); + + should(ownerUri).equal(undefined); + }); + + it('Should return undefined for endpoint as database and no ConnectionInfo', async function (): Promise { + let ownerUri = undefined; + let testDatabaseEndpoint: mssql.SchemaCompareEndpointInfo = {...mockDatabaseEndpoint}; + testDatabaseEndpoint.connectionDetails = undefined; + + ownerUri = await verifyConnectionAndGetOwnerUri(testDatabaseEndpoint, 'test'); + + should(ownerUri).equal(undefined); + }); +}); diff --git a/extensions/schema-compare/src/utils.ts b/extensions/schema-compare/src/utils.ts index a1733e3e24..44e553ffc5 100644 --- a/extensions/schema-compare/src/utils.ts +++ b/extensions/schema-compare/src/utils.ts @@ -83,7 +83,7 @@ function connectionInfoToConnectionProfile(details: azdata.ConnectionInfo): azda }; } -export async function verifyConnectionAndGetOwnerUri(endpoint: mssql.SchemaCompareEndpointInfo, caller: string): Promise { +export async function verifyConnectionAndGetOwnerUri(endpoint: mssql.SchemaCompareEndpointInfo, caller: string): Promise { let ownerUri = undefined; if (endpoint.endpointType === mssql.SchemaCompareEndpointType.Database && endpoint.connectionDetails) { let connectionProfile = await connectionInfoToConnectionProfile(endpoint.connectionDetails);