Schema Compare open SCMP file (#825)

* initial open scmp changes

* more open scmp changes and adding tests

* moving some common test code

* simplify endpoint info parsing

* addressing comments and moving more common test code

* addressing comments
This commit is contained in:
Kim Santiago
2019-06-13 14:24:19 -07:00
committed by GitHub
parent 3566471c6c
commit b451670222
12 changed files with 472 additions and 8 deletions

View File

@@ -13,7 +13,6 @@ using System;
using System.IO;
using System.Threading.Tasks;
using Xunit;
using Microsoft.SqlTools.ServiceLayer.SchemaCopmare;
namespace Microsoft.SqlTools.ServiceLayer.IntegrationTests.SchemaCompare
{

View File

@@ -2,6 +2,7 @@
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//
using Microsoft.SqlServer.Dac.Compare;
using Microsoft.SqlTools.Hosting.Protocol;
using Microsoft.SqlTools.ServiceLayer.SchemaCompare;
using Microsoft.SqlTools.ServiceLayer.SchemaCompare.Contracts;
@@ -495,5 +496,117 @@ CREATE TABLE [dbo].[table3]
string afterIncludeScript = generateScriptOperation.ScriptGenerationResult.Script;
Assert.True(initialScript.Length == afterIncludeScript.Length, $"Changes should be same as inital since we included what we excluded, before {initialScript}, now {afterIncludeScript}");
}
/// <summary>
/// Verify opening an scmp comparing two databases
/// </summary>
[Fact]
public async void SchemaCompareOpenScmpDatabaseToDatabaseRequest()
{
await CreateAndOpenScmp(SchemaCompareEndpointType.Database, SchemaCompareEndpointType.Database);
}
/// <summary>
/// Verify opening an scmp comparing a dacpac and database
/// </summary>
[Fact]
public async void SchemaCompareOpenScmpDacpacToDatabaseRequest()
{
await CreateAndOpenScmp(SchemaCompareEndpointType.Dacpac, SchemaCompareEndpointType.Database);
}
/// <summary>
/// Verify opening an scmp comparing two dacpacs
/// </summary>
[Fact]
public async void SchemaCompareOpenScmpDacpacToDacpacRequest()
{
await CreateAndOpenScmp(SchemaCompareEndpointType.Dacpac, SchemaCompareEndpointType.Dacpac);
}
private async Task CreateAndOpenScmp(SchemaCompareEndpointType sourceEndpointType, SchemaCompareEndpointType targetEndpointType)
{
SqlTestDb sourceDb = await SqlTestDb.CreateNewAsync(TestServerType.OnPrem, false, null, SourceScript, "SchemaCompareOpenScmpSource");
SqlTestDb targetDb = await SqlTestDb.CreateNewAsync(TestServerType.OnPrem, false, null, TargetScript, "SchemaCompareOpenScmpTarget");
try
{
SchemaCompareEndpoint sourceEndpoint = CreateSchemaCompareEndpoint(sourceDb, sourceEndpointType);
SchemaCompareEndpoint targetEndpoint = CreateSchemaCompareEndpoint(targetDb, targetEndpointType);
// create a comparison and exclude the first difference
SchemaComparison compare = new SchemaComparison(sourceEndpoint, targetEndpoint);
SchemaComparisonResult result = compare.Compare();
Assert.NotEmpty(result.Differences);
SchemaDifference difference = result.Differences.First();
if (difference.SourceObject != null)
{
compare.ExcludedSourceObjects.Add(new SchemaComparisonExcludedObjectId(difference.SourceObject.ObjectType, difference.SourceObject.Name));
}
else
{
compare.ExcludedSourceObjects.Add(new SchemaComparisonExcludedObjectId(difference.TargetObject.ObjectType, difference.TargetObject.Name));
}
// save to scmp
string folderPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "SchemaCompareTest");
Directory.CreateDirectory(folderPath);
string filePath = Path.Combine(folderPath, string.Format("SchemaCompareOpenScmpTest{0}.scmp", DateTime.Now.ToFileTime()));
compare.SaveToFile(filePath);
Assert.True(File.Exists(filePath));
var schemaCompareOpenScmpParams = new SchemaCompareOpenScmpParams
{
filePath = filePath
};
SchemaCompareOpenScmpOperation schemaCompareOpenScmpOperation = new SchemaCompareOpenScmpOperation(schemaCompareOpenScmpParams);
schemaCompareOpenScmpOperation.Execute(TaskExecutionMode.Execute);
Assert.NotNull(schemaCompareOpenScmpOperation.Result);
Assert.True(schemaCompareOpenScmpOperation.Result.Success);
Assert.NotEmpty(schemaCompareOpenScmpOperation.Result.ExcludedSourceElements);
Assert.Equal(1, schemaCompareOpenScmpOperation.Result.ExcludedSourceElements.Count());
Assert.Empty(schemaCompareOpenScmpOperation.Result.ExcludedTargetElements);
Assert.Equal(targetDb.DatabaseName, schemaCompareOpenScmpOperation.Result.OriginalTargetName);
ValidateResultEndpointInfo(sourceEndpoint, schemaCompareOpenScmpOperation.Result.SourceEndpointInfo, sourceDb.ConnectionString);
ValidateResultEndpointInfo(targetEndpoint, schemaCompareOpenScmpOperation.Result.TargetEndpointInfo, targetDb.ConnectionString);
SchemaCompareTestUtils.VerifyAndCleanup(filePath);
}
finally
{
sourceDb.Cleanup();
targetDb.Cleanup();
}
}
private SchemaCompareEndpoint CreateSchemaCompareEndpoint(SqlTestDb db, SchemaCompareEndpointType endpointType)
{
if (endpointType == SchemaCompareEndpointType.Dacpac)
{
string dacpacFilePath = SchemaCompareTestUtils.CreateDacpac(db);
return new SchemaCompareDacpacEndpoint(dacpacFilePath);
}
else
{
return new SchemaCompareDatabaseEndpoint(db.ConnectionString);
}
}
private void ValidateResultEndpointInfo(SchemaCompareEndpoint originalEndpoint, SchemaCompareEndpointInfo resultEndpoint, string connectionString)
{
if (resultEndpoint.EndpointType == SchemaCompareEndpointType.Dacpac)
{
SchemaCompareDacpacEndpoint dacpacEndpoint = originalEndpoint as SchemaCompareDacpacEndpoint;
Assert.Equal(dacpacEndpoint.FilePath, resultEndpoint.PackageFilePath);
}
else
{
SchemaCompareDatabaseEndpoint databaseEndpoint = originalEndpoint as SchemaCompareDatabaseEndpoint;
Assert.Equal(databaseEndpoint.DatabaseName, resultEndpoint.DatabaseName);
Assert.Contains(resultEndpoint.ConnectionDetails.ConnectionString, connectionString); // connectionString has password but resultEndpoint doesn't
}
}
}
}