mirror of
https://github.com/ckaczor/sqltoolsservice.git
synced 2026-02-07 09:35:37 -05:00
Fix script generated for SQL Assessment results (#1058)
* INSERT VALUES has the limit of 1000 rows. Replace with derived table. * Remove NOT NULL restriction from the generated table. * Fix line endings in SQL Assessment source files.
This commit is contained in:
@@ -1,225 +1,225 @@
|
||||
//
|
||||
// Copyright (c) Microsoft. All rights reserved.
|
||||
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
||||
//
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
using Microsoft.SqlServer.Management.Assessment;
|
||||
using Microsoft.SqlTools.ServiceLayer.Connection;
|
||||
using Microsoft.SqlTools.ServiceLayer.Connection.Contracts;
|
||||
using Microsoft.SqlTools.ServiceLayer.Connection.ReliableConnection;
|
||||
using Microsoft.SqlTools.ServiceLayer.IntegrationTests.Utility;
|
||||
using Microsoft.SqlTools.ServiceLayer.SqlAssessment;
|
||||
using Microsoft.SqlTools.ServiceLayer.SqlAssessment.Contracts;
|
||||
using Microsoft.SqlTools.ServiceLayer.Test.Common;
|
||||
using NUnit.Framework;
|
||||
|
||||
namespace Microsoft.SqlTools.ServiceLayer.IntegrationTests.SqlAssessment
|
||||
{
|
||||
public class SqlAssessmentServiceTests
|
||||
{
|
||||
private delegate Task<List<TResult>> AssessmentMethod<TResult>(SqlObjectLocator locator);
|
||||
|
||||
private static readonly string[] AllowedSeverityLevels = { "Information", "Warning", "Critical" };
|
||||
|
||||
[Test]
|
||||
public async Task InvokeSqlAssessmentServerTest()
|
||||
{
|
||||
var liveConnection = LiveConnectionHelper.InitLiveConnectionInfo("master");
|
||||
|
||||
var connection = liveConnection.ConnectionInfo.AllConnections.FirstOrDefault();
|
||||
Debug.Assert(connection != null, "Live connection is always expected providing a connection");
|
||||
|
||||
var serverInfo = ReliableConnectionHelper.GetServerVersion(connection);
|
||||
|
||||
var response = await CallAssessment<AssessmentResultItem>(
|
||||
nameof(SqlAssessmentService.InvokeSqlAssessment),
|
||||
SqlObjectType.Server,
|
||||
liveConnection);
|
||||
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(response.Items.Select(i => i.Message), Has.All.Not.Null.Or.Empty);
|
||||
Assert.That(response.Items.Select(i => i.TargetName), Has.All.EqualTo(serverInfo.ServerName));
|
||||
foreach (var i in response.Items.Where(i => i.Kind == 0))
|
||||
{
|
||||
AssertInfoPresent(i);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task GetAssessmentItemsServerTest()
|
||||
{
|
||||
var liveConnection = LiveConnectionHelper.InitLiveConnectionInfo("master");
|
||||
|
||||
var connection = liveConnection.ConnectionInfo.AllConnections.FirstOrDefault();
|
||||
Debug.Assert(connection != null, "Live connection is always expected providing a connection");
|
||||
|
||||
var serverInfo = ReliableConnectionHelper.GetServerVersion(connection);
|
||||
|
||||
var response = await CallAssessment<CheckInfo>(
|
||||
nameof(SqlAssessmentService.GetAssessmentItems),
|
||||
SqlObjectType.Server,
|
||||
liveConnection);
|
||||
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(response.Items.Select(i => i.TargetName), Has.All.EqualTo(serverInfo.ServerName));
|
||||
foreach (var i in response.Items)
|
||||
{
|
||||
AssertInfoPresent(i);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task GetAssessmentItemsDatabaseTest()
|
||||
{
|
||||
const string DatabaseName = "tempdb";
|
||||
var liveConnection = LiveConnectionHelper.InitLiveConnectionInfo(DatabaseName);
|
||||
var response = await CallAssessment<CheckInfo>(
|
||||
nameof(SqlAssessmentService.GetAssessmentItems),
|
||||
SqlObjectType.Database,
|
||||
liveConnection);
|
||||
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(response.Items.Select(i => i.TargetName), Has.All.EndsWith(":" + DatabaseName));
|
||||
foreach (var i in response.Items)
|
||||
{
|
||||
AssertInfoPresent(i);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task InvokeSqlAssessmentIDatabaseTest()
|
||||
{
|
||||
const string DatabaseName = "tempdb";
|
||||
var liveConnection = LiveConnectionHelper.InitLiveConnectionInfo(DatabaseName);
|
||||
var response = await CallAssessment<AssessmentResultItem>(
|
||||
nameof(SqlAssessmentService.InvokeSqlAssessment),
|
||||
SqlObjectType.Database,
|
||||
liveConnection);
|
||||
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(response.Items.Select(i => i.Message), Has.All.Not.Null.Or.Empty);
|
||||
Assert.That(response.Items.Select(i => i.TargetName), Has.All.EndsWith(":" + DatabaseName));
|
||||
foreach (var i in response.Items.Where(i => i.Kind == 0))
|
||||
{
|
||||
AssertInfoPresent(i);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private static async Task<AssessmentResult<TResult>> CallAssessment<TResult>(
|
||||
string methodName,
|
||||
SqlObjectType sqlObjectType,
|
||||
LiveConnectionHelper.TestConnectionResult liveConnection)
|
||||
where TResult : AssessmentItemInfo
|
||||
{
|
||||
var connInfo = liveConnection.ConnectionInfo;
|
||||
|
||||
AssessmentResult<TResult> response;
|
||||
|
||||
using (var service = new SqlAssessmentService(
|
||||
TestServiceProvider.Instance.ConnectionService,
|
||||
TestServiceProvider.Instance.WorkspaceService))
|
||||
{
|
||||
AddTestRules(service);
|
||||
|
||||
string randomUri = Guid.NewGuid().ToString();
|
||||
AssessmentParams requestParams =
|
||||
new AssessmentParams { OwnerUri = randomUri, TargetType = sqlObjectType };
|
||||
ConnectParams connectParams = new ConnectParams
|
||||
{
|
||||
OwnerUri = requestParams.OwnerUri,
|
||||
Connection = connInfo.ConnectionDetails,
|
||||
Type = ConnectionType.Default
|
||||
};
|
||||
|
||||
var methodInfo = typeof(SqlAssessmentService).GetMethod(
|
||||
methodName,
|
||||
BindingFlags.Instance | BindingFlags.NonPublic);
|
||||
|
||||
Assert.NotNull(methodInfo);
|
||||
|
||||
var func = (AssessmentMethod<TResult>)Delegate.CreateDelegate(
|
||||
typeof(AssessmentMethod<TResult>),
|
||||
service,
|
||||
methodInfo);
|
||||
|
||||
response = await service.CallAssessmentEngine<TResult>(
|
||||
requestParams,
|
||||
connectParams,
|
||||
randomUri,
|
||||
t => func(t));
|
||||
}
|
||||
|
||||
Assert.NotNull(response);
|
||||
if (response.Success)
|
||||
{
|
||||
Assert.That(response.Items.Select(i => i.TargetType), Has.All.EqualTo(sqlObjectType));
|
||||
Assert.That(response.Items.Select(i => i.Level), Has.All.AnyOf(AllowedSeverityLevels));
|
||||
}
|
||||
|
||||
return response;
|
||||
}
|
||||
|
||||
private static void AddTestRules(SqlAssessmentService service)
|
||||
{
|
||||
const string TestRuleset = @"
|
||||
{
|
||||
'name': 'Tags & Checks',
|
||||
'version': '0.3',
|
||||
'schemaVersion': '1.0',
|
||||
'rules': [
|
||||
{
|
||||
'id': 'ServerRule',
|
||||
'itemType': 'definition',
|
||||
'tags': [ 'Test' ],
|
||||
'displayName': 'Test server check',
|
||||
'description': 'This check always fails for testing purposes.',
|
||||
'message': 'This check intentionally fails',
|
||||
'target': { 'type': 'Server' }
|
||||
},
|
||||
{
|
||||
'id': 'DatabaseRule',
|
||||
'itemType': 'definition',
|
||||
'tags': [ 'Test' ],
|
||||
'displayName': 'Test server check',
|
||||
'description': 'This check always fails for testing purposes.',
|
||||
'message': 'This check intentionally fails',
|
||||
'target': { 'type': 'Database' }
|
||||
}
|
||||
]
|
||||
}
|
||||
";
|
||||
using (var reader = new StringReader(TestRuleset))
|
||||
{
|
||||
service.Engine.PushRuleFactoryJson(reader);
|
||||
}
|
||||
}
|
||||
|
||||
private void AssertInfoPresent(AssessmentItemInfo item)
|
||||
{
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(item.CheckId, Is.Not.Null.Or.Empty);
|
||||
Assert.That(item.DisplayName, Is.Not.Null.Or.Empty);
|
||||
Assert.That(item.Description, Is.Not.Null.Or.Empty);
|
||||
Assert.NotNull(item.Tags);
|
||||
Assert.That(item.Tags, Has.All.Not.Null.Or.Empty);
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
//
|
||||
// Copyright (c) Microsoft. All rights reserved.
|
||||
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
||||
//
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
using Microsoft.SqlServer.Management.Assessment;
|
||||
using Microsoft.SqlTools.ServiceLayer.Connection;
|
||||
using Microsoft.SqlTools.ServiceLayer.Connection.Contracts;
|
||||
using Microsoft.SqlTools.ServiceLayer.Connection.ReliableConnection;
|
||||
using Microsoft.SqlTools.ServiceLayer.IntegrationTests.Utility;
|
||||
using Microsoft.SqlTools.ServiceLayer.SqlAssessment;
|
||||
using Microsoft.SqlTools.ServiceLayer.SqlAssessment.Contracts;
|
||||
using Microsoft.SqlTools.ServiceLayer.Test.Common;
|
||||
using NUnit.Framework;
|
||||
|
||||
namespace Microsoft.SqlTools.ServiceLayer.IntegrationTests.SqlAssessment
|
||||
{
|
||||
public class SqlAssessmentServiceTests
|
||||
{
|
||||
private delegate Task<List<TResult>> AssessmentMethod<TResult>(SqlObjectLocator locator);
|
||||
|
||||
private static readonly string[] AllowedSeverityLevels = { "Information", "Warning", "Critical" };
|
||||
|
||||
[Test]
|
||||
public async Task InvokeSqlAssessmentServerTest()
|
||||
{
|
||||
var liveConnection = LiveConnectionHelper.InitLiveConnectionInfo("master");
|
||||
|
||||
var connection = liveConnection.ConnectionInfo.AllConnections.FirstOrDefault();
|
||||
Debug.Assert(connection != null, "Live connection is always expected providing a connection");
|
||||
|
||||
var serverInfo = ReliableConnectionHelper.GetServerVersion(connection);
|
||||
|
||||
var response = await CallAssessment<AssessmentResultItem>(
|
||||
nameof(SqlAssessmentService.InvokeSqlAssessment),
|
||||
SqlObjectType.Server,
|
||||
liveConnection);
|
||||
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(response.Items.Select(i => i.Message), Has.All.Not.Null.Or.Empty);
|
||||
Assert.That(response.Items.Select(i => i.TargetName), Has.All.EqualTo(serverInfo.ServerName));
|
||||
foreach (var i in response.Items.Where(i => i.Kind == 0))
|
||||
{
|
||||
AssertInfoPresent(i);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task GetAssessmentItemsServerTest()
|
||||
{
|
||||
var liveConnection = LiveConnectionHelper.InitLiveConnectionInfo("master");
|
||||
|
||||
var connection = liveConnection.ConnectionInfo.AllConnections.FirstOrDefault();
|
||||
Debug.Assert(connection != null, "Live connection is always expected providing a connection");
|
||||
|
||||
var serverInfo = ReliableConnectionHelper.GetServerVersion(connection);
|
||||
|
||||
var response = await CallAssessment<CheckInfo>(
|
||||
nameof(SqlAssessmentService.GetAssessmentItems),
|
||||
SqlObjectType.Server,
|
||||
liveConnection);
|
||||
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(response.Items.Select(i => i.TargetName), Has.All.EqualTo(serverInfo.ServerName));
|
||||
foreach (var i in response.Items)
|
||||
{
|
||||
AssertInfoPresent(i);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task GetAssessmentItemsDatabaseTest()
|
||||
{
|
||||
const string DatabaseName = "tempdb";
|
||||
var liveConnection = LiveConnectionHelper.InitLiveConnectionInfo(DatabaseName);
|
||||
var response = await CallAssessment<CheckInfo>(
|
||||
nameof(SqlAssessmentService.GetAssessmentItems),
|
||||
SqlObjectType.Database,
|
||||
liveConnection);
|
||||
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(response.Items.Select(i => i.TargetName), Has.All.EndsWith(":" + DatabaseName));
|
||||
foreach (var i in response.Items)
|
||||
{
|
||||
AssertInfoPresent(i);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task InvokeSqlAssessmentIDatabaseTest()
|
||||
{
|
||||
const string DatabaseName = "tempdb";
|
||||
var liveConnection = LiveConnectionHelper.InitLiveConnectionInfo(DatabaseName);
|
||||
var response = await CallAssessment<AssessmentResultItem>(
|
||||
nameof(SqlAssessmentService.InvokeSqlAssessment),
|
||||
SqlObjectType.Database,
|
||||
liveConnection);
|
||||
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(response.Items.Select(i => i.Message), Has.All.Not.Null.Or.Empty);
|
||||
Assert.That(response.Items.Select(i => i.TargetName), Has.All.EndsWith(":" + DatabaseName));
|
||||
foreach (var i in response.Items.Where(i => i.Kind == 0))
|
||||
{
|
||||
AssertInfoPresent(i);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private static async Task<AssessmentResult<TResult>> CallAssessment<TResult>(
|
||||
string methodName,
|
||||
SqlObjectType sqlObjectType,
|
||||
LiveConnectionHelper.TestConnectionResult liveConnection)
|
||||
where TResult : AssessmentItemInfo
|
||||
{
|
||||
var connInfo = liveConnection.ConnectionInfo;
|
||||
|
||||
AssessmentResult<TResult> response;
|
||||
|
||||
using (var service = new SqlAssessmentService(
|
||||
TestServiceProvider.Instance.ConnectionService,
|
||||
TestServiceProvider.Instance.WorkspaceService))
|
||||
{
|
||||
AddTestRules(service);
|
||||
|
||||
string randomUri = Guid.NewGuid().ToString();
|
||||
AssessmentParams requestParams =
|
||||
new AssessmentParams { OwnerUri = randomUri, TargetType = sqlObjectType };
|
||||
ConnectParams connectParams = new ConnectParams
|
||||
{
|
||||
OwnerUri = requestParams.OwnerUri,
|
||||
Connection = connInfo.ConnectionDetails,
|
||||
Type = ConnectionType.Default
|
||||
};
|
||||
|
||||
var methodInfo = typeof(SqlAssessmentService).GetMethod(
|
||||
methodName,
|
||||
BindingFlags.Instance | BindingFlags.NonPublic);
|
||||
|
||||
Assert.NotNull(methodInfo);
|
||||
|
||||
var func = (AssessmentMethod<TResult>)Delegate.CreateDelegate(
|
||||
typeof(AssessmentMethod<TResult>),
|
||||
service,
|
||||
methodInfo);
|
||||
|
||||
response = await service.CallAssessmentEngine<TResult>(
|
||||
requestParams,
|
||||
connectParams,
|
||||
randomUri,
|
||||
t => func(t));
|
||||
}
|
||||
|
||||
Assert.NotNull(response);
|
||||
if (response.Success)
|
||||
{
|
||||
Assert.That(response.Items.Select(i => i.TargetType), Has.All.EqualTo(sqlObjectType));
|
||||
Assert.That(response.Items.Select(i => i.Level), Has.All.AnyOf(AllowedSeverityLevels));
|
||||
}
|
||||
|
||||
return response;
|
||||
}
|
||||
|
||||
private static void AddTestRules(SqlAssessmentService service)
|
||||
{
|
||||
const string TestRuleset = @"
|
||||
{
|
||||
'name': 'Tags & Checks',
|
||||
'version': '0.3',
|
||||
'schemaVersion': '1.0',
|
||||
'rules': [
|
||||
{
|
||||
'id': 'ServerRule',
|
||||
'itemType': 'definition',
|
||||
'tags': [ 'Test' ],
|
||||
'displayName': 'Test server check',
|
||||
'description': 'This check always fails for testing purposes.',
|
||||
'message': 'This check intentionally fails',
|
||||
'target': { 'type': 'Server' }
|
||||
},
|
||||
{
|
||||
'id': 'DatabaseRule',
|
||||
'itemType': 'definition',
|
||||
'tags': [ 'Test' ],
|
||||
'displayName': 'Test server check',
|
||||
'description': 'This check always fails for testing purposes.',
|
||||
'message': 'This check intentionally fails',
|
||||
'target': { 'type': 'Database' }
|
||||
}
|
||||
]
|
||||
}
|
||||
";
|
||||
using (var reader = new StringReader(TestRuleset))
|
||||
{
|
||||
service.Engine.PushRuleFactoryJson(reader);
|
||||
}
|
||||
}
|
||||
|
||||
private void AssertInfoPresent(AssessmentItemInfo item)
|
||||
{
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(item.CheckId, Is.Not.Null.Or.Empty);
|
||||
Assert.That(item.DisplayName, Is.Not.Null.Or.Empty);
|
||||
Assert.That(item.Description, Is.Not.Null.Or.Empty);
|
||||
Assert.NotNull(item.Tags);
|
||||
Assert.That(item.Tags, Has.All.Not.Null.Or.Empty);
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,157 +1,159 @@
|
||||
//
|
||||
// Copyright (c) Microsoft. All rights reserved.
|
||||
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
||||
//
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.SqlServer.Management.Assessment;
|
||||
using Microsoft.SqlTools.ServiceLayer.SqlAssessment;
|
||||
using Microsoft.SqlTools.ServiceLayer.SqlAssessment.Contracts;
|
||||
using Microsoft.SqlTools.ServiceLayer.TaskServices;
|
||||
using Moq;
|
||||
using NUnit.Framework;
|
||||
|
||||
namespace Microsoft.SqlTools.ServiceLayer.UnitTests.SqlAssessment
|
||||
{
|
||||
public class GenerateScriptOperationTests
|
||||
{
|
||||
private static readonly GenerateScriptParams SampleParams = new GenerateScriptParams
|
||||
{
|
||||
Items = new List<AssessmentResultItem>
|
||||
{
|
||||
new AssessmentResultItem
|
||||
{
|
||||
CheckId = "C1",
|
||||
Description = "Desc1",
|
||||
DisplayName = "DN1",
|
||||
HelpLink = "HL1",
|
||||
Kind = AssessmentResultItemKind.Note,
|
||||
Level = "Information",
|
||||
Message = "Msg'1",
|
||||
TargetName = "proj[*]_dev",
|
||||
TargetType = SqlObjectType.Server,
|
||||
Timestamp = new DateTimeOffset(2001, 5, 25, 13, 42, 00, TimeSpan.Zero),
|
||||
RulesetName = "Microsoft Ruleset",
|
||||
RulesetVersion = "1.3"
|
||||
},
|
||||
new AssessmentResultItem
|
||||
{
|
||||
CheckId = "C-2",
|
||||
Description = "Desc2",
|
||||
DisplayName = "D N2",
|
||||
HelpLink = "http://HL2",
|
||||
Kind = AssessmentResultItemKind.Warning,
|
||||
Level = "Warning",
|
||||
Message = "Msg'1",
|
||||
TargetName = "proj[*]_devW",
|
||||
TargetType = SqlObjectType.Database,
|
||||
Timestamp = new DateTimeOffset(2001, 5, 25, 13, 42, 00, TimeSpan.FromHours(3)),
|
||||
RulesetName = "Microsoft Ruleset",
|
||||
RulesetVersion = "1.3"
|
||||
},
|
||||
new AssessmentResultItem
|
||||
{
|
||||
CheckId = "C'3",
|
||||
Description = "Des'c3",
|
||||
DisplayName = "D'N1",
|
||||
HelpLink = "HL'1",
|
||||
Kind = AssessmentResultItemKind.Error,
|
||||
Level = "Critical",
|
||||
Message = "Msg'1",
|
||||
TargetName = "proj[*]_devE",
|
||||
TargetType = SqlObjectType.Server,
|
||||
Timestamp = new DateTimeOffset(2001, 5, 25, 13, 42, 00, TimeSpan.FromMinutes(-90)),
|
||||
RulesetName = "Microsoft Ruleset",
|
||||
RulesetVersion = "1.3"
|
||||
},
|
||||
new AssessmentResultItem
|
||||
{
|
||||
CheckId = "C-2",
|
||||
Description = "Desc2",
|
||||
DisplayName = "D N2",
|
||||
HelpLink = "http://HL2",
|
||||
Kind = AssessmentResultItemKind.Note,
|
||||
Level = "Warning",
|
||||
Message = "Msg'1",
|
||||
TargetName = "proj[*]_dev",
|
||||
TargetType = SqlObjectType.Database,
|
||||
Timestamp = new DateTimeOffset(2001, 5, 25, 13, 42, 00, TimeSpan.FromHours(3)),
|
||||
RulesetName = "Microsoft Ruleset",
|
||||
RulesetVersion = "1.3"
|
||||
},
|
||||
new AssessmentResultItem
|
||||
{
|
||||
CheckId = "C'3",
|
||||
Description = "Des'c3",
|
||||
DisplayName = "D'N1",
|
||||
HelpLink = "HL'1",
|
||||
Kind = AssessmentResultItemKind.Note,
|
||||
Level = "Critical",
|
||||
Message = "Msg'1",
|
||||
TargetName = "proj[*]_dev",
|
||||
TargetType = SqlObjectType.Server,
|
||||
Timestamp = new DateTimeOffset(2001, 5, 25, 13, 42, 00, TimeSpan.FromMinutes(-90)),
|
||||
RulesetName = "Microsoft Ruleset",
|
||||
RulesetVersion = "1.3"
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
private const string SampleScript =
|
||||
@"IF (NOT EXISTS(SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = 'dbo' AND TABLE_NAME = 'AssessmentResult'))
|
||||
BEGIN
|
||||
CREATE TABLE [dbo].[AssessmentResult](
|
||||
[CheckName] [nvarchar](max) NOT NULL,
|
||||
[CheckId] [nvarchar](max) NOT NULL,
|
||||
[RulesetName] [nvarchar](max) NOT NULL,
|
||||
[RulesetVersion] [nvarchar](max) NOT NULL,
|
||||
[Severity] [nvarchar](max) NOT NULL,
|
||||
[Message] [nvarchar](max) NOT NULL,
|
||||
[TargetPath] [nvarchar](max) NOT NULL,
|
||||
[TargetType] [nvarchar](max) NOT NULL,
|
||||
[HelpLink] [nvarchar](max) NOT NULL,
|
||||
[Timestamp] [datetimeoffset](7) NOT NULL
|
||||
)
|
||||
END
|
||||
GO
|
||||
INSERT INTO [dbo].[AssessmentResult] ([CheckName],[CheckId],[RulesetName],[RulesetVersion],[Severity],[Message],[TargetPath],[TargetType],[HelpLink],[Timestamp])
|
||||
VALUES
|
||||
('DN1','C1','Microsoft Ruleset','1.3','Information','Msg''1','proj[*]_dev','Server','HL1','2001-05-25 01:42:00.000 +00:00'),
|
||||
('D N2','C-2','Microsoft Ruleset','1.3','Warning','Msg''1','proj[*]_dev','Database','http://HL2','2001-05-25 01:42:00.000 +03:00'),
|
||||
('D''N1','C''3','Microsoft Ruleset','1.3','Critical','Msg''1','proj[*]_dev','Server','HL''1','2001-05-25 01:42:00.000 -01:30')";
|
||||
|
||||
[Test]
|
||||
public void GenerateScriptTest()
|
||||
{
|
||||
var scriptText = GenerateScriptOperation.GenerateScript(SampleParams, CancellationToken.None);
|
||||
Assert.AreEqual(SampleScript, scriptText);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ExecuteTest()
|
||||
{
|
||||
var subject = new GenerateScriptOperation(SampleParams);
|
||||
var taskMetadata = new TaskMetadata();
|
||||
using (var sqlTask = new SqlTask(taskMetadata, DummyOpFunction, DummyOpFunction))
|
||||
{
|
||||
subject.SqlTask = sqlTask;
|
||||
sqlTask.ScriptAdded += ValidateScriptAdded;
|
||||
subject.Execute(TaskExecutionMode.Script);
|
||||
}
|
||||
}
|
||||
|
||||
private void ValidateScriptAdded(object sender, TaskEventArgs<TaskScript> e)
|
||||
{
|
||||
Assert.AreEqual(SqlTaskStatus.Succeeded, e.TaskData.Status);
|
||||
Assert.AreEqual(SampleScript, e.TaskData.Script);
|
||||
}
|
||||
|
||||
private static Task<TaskResult> DummyOpFunction(SqlTask _)
|
||||
{
|
||||
return Task.FromResult(new TaskResult() {TaskStatus = SqlTaskStatus.Succeeded});
|
||||
}
|
||||
}
|
||||
}
|
||||
//
|
||||
// Copyright (c) Microsoft. All rights reserved.
|
||||
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
||||
//
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.SqlServer.Management.Assessment;
|
||||
using Microsoft.SqlTools.ServiceLayer.SqlAssessment;
|
||||
using Microsoft.SqlTools.ServiceLayer.SqlAssessment.Contracts;
|
||||
using Microsoft.SqlTools.ServiceLayer.TaskServices;
|
||||
using Moq;
|
||||
using NUnit.Framework;
|
||||
|
||||
namespace Microsoft.SqlTools.ServiceLayer.UnitTests.SqlAssessment
|
||||
{
|
||||
public class GenerateScriptOperationTests
|
||||
{
|
||||
private static readonly GenerateScriptParams SampleParams = new GenerateScriptParams
|
||||
{
|
||||
Items = new List<AssessmentResultItem>
|
||||
{
|
||||
new AssessmentResultItem
|
||||
{
|
||||
CheckId = "C1",
|
||||
Description = "Desc1",
|
||||
DisplayName = "DN1",
|
||||
HelpLink = "HL1",
|
||||
Kind = AssessmentResultItemKind.Note,
|
||||
Level = "Information",
|
||||
Message = "Msg'1",
|
||||
TargetName = "proj[*]_dev",
|
||||
TargetType = SqlObjectType.Server,
|
||||
Timestamp = new DateTimeOffset(2001, 5, 25, 13, 42, 00, TimeSpan.Zero),
|
||||
RulesetName = "Microsoft Ruleset",
|
||||
RulesetVersion = "1.3"
|
||||
},
|
||||
new AssessmentResultItem
|
||||
{
|
||||
CheckId = "C-2",
|
||||
Description = "Desc2",
|
||||
DisplayName = "D N2",
|
||||
HelpLink = "http://HL2",
|
||||
Kind = AssessmentResultItemKind.Warning,
|
||||
Level = "Warning",
|
||||
Message = "Msg'1",
|
||||
TargetName = "proj[*]_devW",
|
||||
TargetType = SqlObjectType.Database,
|
||||
Timestamp = new DateTimeOffset(2001, 5, 25, 13, 42, 00, TimeSpan.FromHours(3)),
|
||||
RulesetName = "Microsoft Ruleset",
|
||||
RulesetVersion = "1.3"
|
||||
},
|
||||
new AssessmentResultItem
|
||||
{
|
||||
CheckId = "C'3",
|
||||
Description = "Des'c3",
|
||||
DisplayName = "D'N1",
|
||||
HelpLink = "HL'1",
|
||||
Kind = AssessmentResultItemKind.Error,
|
||||
Level = "Critical",
|
||||
Message = "Msg'1",
|
||||
TargetName = "proj[*]_devE",
|
||||
TargetType = SqlObjectType.Server,
|
||||
Timestamp = new DateTimeOffset(2001, 5, 25, 13, 42, 00, TimeSpan.FromMinutes(-90)),
|
||||
RulesetName = "Microsoft Ruleset",
|
||||
RulesetVersion = "1.3"
|
||||
},
|
||||
new AssessmentResultItem
|
||||
{
|
||||
CheckId = "C-2",
|
||||
Description = "Desc2",
|
||||
DisplayName = "D N2",
|
||||
HelpLink = "http://HL2",
|
||||
Kind = AssessmentResultItemKind.Note,
|
||||
Level = "Warning",
|
||||
Message = "Msg'1",
|
||||
TargetName = "proj[*]_dev",
|
||||
TargetType = SqlObjectType.Database,
|
||||
Timestamp = new DateTimeOffset(2001, 5, 25, 13, 42, 00, TimeSpan.FromHours(3)),
|
||||
RulesetName = "Microsoft Ruleset",
|
||||
RulesetVersion = "1.3"
|
||||
},
|
||||
new AssessmentResultItem
|
||||
{
|
||||
CheckId = "C'3",
|
||||
Description = "Des'c3",
|
||||
DisplayName = "D'N1",
|
||||
HelpLink = "HL'1",
|
||||
Kind = AssessmentResultItemKind.Note,
|
||||
Level = "Critical",
|
||||
Message = "Msg'1",
|
||||
TargetName = "proj[*]_dev",
|
||||
TargetType = SqlObjectType.Server,
|
||||
Timestamp = new DateTimeOffset(2001, 5, 25, 13, 42, 00, TimeSpan.FromMinutes(-90)),
|
||||
RulesetName = "Microsoft Ruleset",
|
||||
RulesetVersion = "1.3"
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
private const string SampleScript =
|
||||
@"IF (NOT EXISTS(SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = 'dbo' AND TABLE_NAME = 'AssessmentResult'))
|
||||
BEGIN
|
||||
CREATE TABLE [dbo].[AssessmentResult](
|
||||
[CheckName] [nvarchar](max),
|
||||
[CheckId] [nvarchar](max),
|
||||
[RulesetName] [nvarchar](max),
|
||||
[RulesetVersion] [nvarchar](max),
|
||||
[Severity] [nvarchar](max),
|
||||
[Message] [nvarchar](max),
|
||||
[TargetPath] [nvarchar](max),
|
||||
[TargetType] [nvarchar](max),
|
||||
[HelpLink] [nvarchar](max),
|
||||
[Timestamp] [datetimeoffset](7)
|
||||
)
|
||||
END
|
||||
GO
|
||||
INSERT INTO [dbo].[AssessmentResult] ([CheckName],[CheckId],[RulesetName],[RulesetVersion],[Severity],[Message],[TargetPath],[TargetType],[HelpLink],[Timestamp])
|
||||
SELECT rpt.[CheckName],rpt.[CheckId],rpt.[RulesetName],rpt.[RulesetVersion],rpt.[Severity],rpt.[Message],rpt.[TargetPath],rpt.[TargetType],rpt.[HelpLink],rpt.[Timestamp]
|
||||
FROM (VALUES
|
||||
('DN1','C1','Microsoft Ruleset','1.3','Information','Msg''1','proj[*]_dev','Server','HL1','2001-05-25 01:42:00.000 +00:00'),
|
||||
('D N2','C-2','Microsoft Ruleset','1.3','Warning','Msg''1','proj[*]_dev','Database','http://HL2','2001-05-25 01:42:00.000 +03:00'),
|
||||
('D''N1','C''3','Microsoft Ruleset','1.3','Critical','Msg''1','proj[*]_dev','Server','HL''1','2001-05-25 01:42:00.000 -01:30')
|
||||
) rpt([CheckName],[CheckId],[RulesetName],[RulesetVersion],[Severity],[Message],[TargetPath],[TargetType],[HelpLink],[Timestamp])";
|
||||
|
||||
[Test]
|
||||
public void GenerateScriptTest()
|
||||
{
|
||||
var scriptText = GenerateScriptOperation.GenerateScript(SampleParams, CancellationToken.None);
|
||||
Assert.AreEqual(SampleScript, scriptText);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ExecuteTest()
|
||||
{
|
||||
var subject = new GenerateScriptOperation(SampleParams);
|
||||
var taskMetadata = new TaskMetadata();
|
||||
using (var sqlTask = new SqlTask(taskMetadata, DummyOpFunction, DummyOpFunction))
|
||||
{
|
||||
subject.SqlTask = sqlTask;
|
||||
sqlTask.ScriptAdded += ValidateScriptAdded;
|
||||
subject.Execute(TaskExecutionMode.Script);
|
||||
}
|
||||
}
|
||||
|
||||
private void ValidateScriptAdded(object sender, TaskEventArgs<TaskScript> e)
|
||||
{
|
||||
Assert.AreEqual(SqlTaskStatus.Succeeded, e.TaskData.Status);
|
||||
Assert.AreEqual(SampleScript, e.TaskData.Script);
|
||||
}
|
||||
|
||||
private static Task<TaskResult> DummyOpFunction(SqlTask _)
|
||||
{
|
||||
return Task.FromResult(new TaskResult() {TaskStatus = SqlTaskStatus.Succeeded});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user