Task/script refactor (#446)

* scripting working with race conditions

* new service works with no race conditions

* use new scripting service and commented out tests

* refactored peek definition to use mssql-scripter

* fixed peek definition tests

* removed auto gen comment

* fixed peek definition highlighting bug

* made scripting async and fixed event handlers

* fixed tests (without cancel and plan notifs)

* removed dead code

* added nuget package

* CR comments + select script service implementation

* minor fixes and added test

* CR comments and script select

* added unit tests

* code review comments and cleanup
This commit is contained in:
Aditya Bist
2017-10-02 12:02:43 -07:00
committed by GitHub
parent 9d898f0d0c
commit e7756b0bf1
24 changed files with 718 additions and 993 deletions

View File

@@ -75,7 +75,7 @@ CREATE SYNONYM [dbo].[pd_testTable] FOR master.dbo.spt_monitor
GO";
private const string TableValuedFunctionTypeName = "TableValuedFunction";
private const string ScalarValuedFunctionTypeName = "ScalarValuedFunction";
private const string ScalarValuedFunctionTypeName = "UserDefinedFunction";
private const string UserDefinedDataTypeTypeName = "UserDefinedDataType";
private const string UserDefinedTableTypeTypeName = "UserDefinedTableType";
private const string SynonymTypeName = "Synonym";
@@ -100,7 +100,7 @@ GO";
string objectType = "TABLE";
// Get locations for valid table object
Location[] locations = scripter.GetSqlObjectDefinition(scripter.GetTableScripts, objectName, schemaName, objectType);
Location[] locations = scripter.GetSqlObjectDefinition(objectName, schemaName, objectType);
Assert.NotNull(locations);
Cleanup(locations);
}
@@ -121,7 +121,7 @@ GO";
string objectType = "TABLE";
// Get locations for invalid table object
Location[] locations = scripter.GetSqlObjectDefinition(scripter.GetTableScripts, objectName, schemaName, objectType);
Location[] locations = scripter.GetSqlObjectDefinition(objectName, schemaName, objectType);
Assert.Null(locations);
}
@@ -142,7 +142,7 @@ GO";
string objectType = "TABLE";
// Get locations for valid table object with schema name
Location[] locations = scripter.GetSqlObjectDefinition(scripter.GetTableScripts, objectName, schemaName, objectType);
Location[] locations = scripter.GetSqlObjectDefinition(objectName, schemaName, objectType);
Assert.NotNull(locations);
Cleanup(locations);
}
@@ -262,7 +262,7 @@ GO";
string schemaName = "sys";
string objectType = "VIEW";
Location[] locations = scripter.GetSqlObjectDefinition(scripter.GetViewScripts, objectName, schemaName, objectType);
Location[] locations = scripter.GetSqlObjectDefinition(objectName, schemaName, objectType);
Assert.NotNull(locations);
Cleanup(locations);
}
@@ -282,7 +282,7 @@ GO";
string schemaName = null;
string objectType = "VIEW";
Location[] locations = scripter.GetSqlObjectDefinition(scripter.GetViewScripts, objectName, schemaName, objectType);
Location[] locations = scripter.GetSqlObjectDefinition(objectName, schemaName, objectType);
Assert.Null(locations);
}
@@ -300,9 +300,9 @@ GO";
string objectName = "sp_columns";
string schemaName = "sys";
string objectType = "PROCEDURE";
string objectType = "StoredProcedure";
Location[] locations = scripter.GetSqlObjectDefinition(scripter.GetStoredProcedureScripts, objectName, schemaName, objectType);
Location[] locations = scripter.GetSqlObjectDefinition(objectName, schemaName, objectType);
Assert.NotNull(locations);
Cleanup(locations);
}
@@ -322,7 +322,7 @@ GO";
string schemaName = "dbo";
string objectType = "PROCEDURE";
Location[] locations = scripter.GetSqlObjectDefinition(scripter.GetStoredProcedureScripts, objectName, schemaName, objectType);
Location[] locations = scripter.GetSqlObjectDefinition(objectName, schemaName, objectType);
Assert.Null(locations);
}
@@ -339,9 +339,9 @@ GO";
Scripter scripter = new Scripter(serverConnection, connInfo);
string objectName = "sp_MSrepl_startup";
string schemaName = null;
string objectType = "PROCEDURE";
string objectType = "StoredProcedure";
Location[] locations = scripter.GetSqlObjectDefinition(scripter.GetStoredProcedureScripts, objectName, schemaName, objectType);
Location[] locations = scripter.GetSqlObjectDefinition(objectName, schemaName, objectType);
Assert.NotNull(locations);
Cleanup(locations);
}
@@ -377,40 +377,7 @@ GO";
Scripter scripter = new Scripter(serverConnection, connInfo);
Scripter.ScriptGetter sqlScriptGetter = null;
switch (objectType)
{
case SynonymTypeName:
sqlScriptGetter = scripter.GetSynonymScripts;
break;
case ScalarValuedFunctionTypeName:
sqlScriptGetter = scripter.GetScalarValuedFunctionScripts;
objectType = "Function";
break;
case TableValuedFunctionTypeName:
sqlScriptGetter = scripter.GetTableValuedFunctionScripts;
objectType = "Function";
break;
case TableTypeName:
sqlScriptGetter = scripter.GetTableScripts;
break;
case ViewTypeName:
sqlScriptGetter = scripter.GetViewScripts;
break;
case StoredProcedureTypeName:
sqlScriptGetter = scripter.GetStoredProcedureScripts;
break;
case UserDefinedDataTypeTypeName:
sqlScriptGetter = scripter.GetUserDefinedDataTypeScripts;
objectType = "Type";
break;
case UserDefinedTableTypeTypeName:
sqlScriptGetter = scripter.GetUserDefinedTableTypeScripts;
objectType = "Type";
break;
}
Location[] locations = scripter.GetSqlObjectDefinition(sqlScriptGetter, objectName, schemaName, objectType);
Location[] locations = scripter.GetSqlObjectDefinition(objectName, schemaName, objectType);
if (shouldReturnValidResult)
{
Assert.NotNull(locations);

View File

@@ -27,11 +27,12 @@
<ProjectReference Include="..\Microsoft.SqlTools.ServiceLayer.UnitTests\Microsoft.SqlTools.ServiceLayer.UnitTests.csproj" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="System.Net.Http" Version="4.3.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.3.0" />
<PackageReference Include="xunit" Version="2.2.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.2.0" />
<PackageReference Include="System.Data.SqlClient" Version="4.4.0" />
<PackageReference Include="Microsoft.SqlServer.Smo" Version="140.2.5" />
<PackageReference Include="Microsoft.SqlServer.Smo" Version="140.2.6" />
</ItemGroup>
<ItemGroup>
<Service Include="{82a7f48d-3b50-4b1e-b82e-3ada8210c358}" />

View File

@@ -3,15 +3,17 @@
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//
using Microsoft.SqlTools.ServiceLayer.Workspace.Contracts;
using Xunit;
using Microsoft.SqlTools.ServiceLayer.IntegrationTests.Utility;
using Microsoft.SqlTools.ServiceLayer.Metadata.Contracts;
using Microsoft.SqlTools.ServiceLayer.Scripting;
using Moq;
using Microsoft.SqlTools.Hosting.Protocol;
using Microsoft.SqlTools.ServiceLayer.Scripting.Contracts;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.SqlTools.Hosting.Protocol;
using Microsoft.SqlTools.ServiceLayer.Connection;
using Microsoft.SqlTools.ServiceLayer.IntegrationTests.Utility;
using Microsoft.SqlTools.ServiceLayer.Workspace.Contracts;
using Microsoft.SqlTools.ServiceLayer.Scripting;
using Microsoft.SqlTools.ServiceLayer.Scripting.Contracts;
using Moq;
using Xunit;
namespace Microsoft.SqlTools.ServiceLayer.IntegrationTests.Scripting
{
@@ -26,6 +28,7 @@ namespace Microsoft.SqlTools.ServiceLayer.IntegrationTests.Scripting
private const string DatabaseName = "test-db";
private const string StoredProcName = "test-sp";
private string[] objects = new string[5] {"Table", "View", "Schema", "Database", "SProc"};
private string[] selectObjects = new string[2] { "Table", "View" };
private LiveConnectionHelper.TestConnectionResult GetLiveAutoCompleteTestObjects()
{
@@ -44,118 +47,39 @@ namespace Microsoft.SqlTools.ServiceLayer.IntegrationTests.Scripting
return result;
}
private static ObjectMetadata GenerateMetadata(string objectType)
{
var metadata = new ObjectMetadata()
{
Schema = SchemaName,
Name = objectType
};
switch(objectType)
{
case("Table"):
metadata.MetadataType = MetadataType.Table;
metadata.Name = TableName;
break;
case("View"):
metadata.MetadataType = MetadataType.View;
metadata.Name = ViewName;
break;
case("Database"):
metadata.MetadataType = MetadataType.Database;
metadata.Name = DatabaseName;
break;
case("Schema"):
metadata.MetadataType = MetadataType.Schema;
metadata.MetadataTypeName = SchemaName;
break;
case("SProc"):
metadata.MetadataType = MetadataType.SProc;
metadata.MetadataTypeName = StoredProcName;
break;
default:
metadata.MetadataType = MetadataType.Table;
metadata.Name = TableName;
break;
}
return metadata;
}
private async Task<Mock<RequestContext<ScriptingScriptAsResult>>> SendAndValidateScriptRequest(ScriptOperation operation, string objectType)
private async Task<Mock<RequestContext<ScriptingResult>>> SendAndValidateScriptRequest(bool isSelectScript)
{
var result = GetLiveAutoCompleteTestObjects();
var requestContext = new Mock<RequestContext<ScriptingScriptAsResult>>();
requestContext.Setup(x => x.SendResult(It.IsAny<ScriptingScriptAsResult>())).Returns(Task.FromResult(new object()));
var requestContext = new Mock<RequestContext<ScriptingResult>>();
requestContext.Setup(x => x.SendResult(It.IsAny<ScriptingResult>())).Returns(Task.FromResult(new object()));
var scriptingParams = new ScriptingScriptAsParams
var scriptingParams = new ScriptingParams
{
OwnerUri = result.ConnectionInfo.OwnerUri,
Operation = operation,
Metadata = GenerateMetadata(objectType)
OwnerUri = ConnectionService.BuildConnectionString(result.ConnectionInfo.ConnectionDetails)
};
await ScriptingService.HandleScriptingScriptAsRequest(scriptingParams, requestContext.Object);
if (isSelectScript)
{
scriptingParams.ScriptOptions = new ScriptOptions { ScriptCreateDrop = "ScriptSelect" };
List<ScriptingObject> scriptingObjects = new List<ScriptingObject>();
scriptingObjects.Add(new ScriptingObject { Type = "View", Name = "sysobjects", Schema = "sys" });
scriptingParams.ScriptingObjects = scriptingObjects;
}
ScriptingService service = new ScriptingService();
await service.HandleScriptExecuteRequest(scriptingParams, requestContext.Object);
return requestContext;
}
/// <summary>
/// Verify the script as select request
/// Verify the script object request
/// </summary>
[Fact]
public async void ScriptingScriptAsSelect()
public async void ScriptingScript()
{
foreach (string obj in objects)
{
Assert.NotNull(await SendAndValidateScriptRequest(ScriptOperation.Select, obj));
}
}
/// <summary>
/// Verify the script as create request
/// </summary>
[Fact]
public async void ScriptingScriptAsCreate()
{
foreach (string obj in objects)
{
Assert.NotNull(await SendAndValidateScriptRequest(ScriptOperation.Create, obj));
}
}
/// <summary>
/// Verify the script as insert request
/// </summary>
[Fact]
public async void ScriptingScriptAsInsert()
{
foreach (string obj in objects)
{
Assert.NotNull(await SendAndValidateScriptRequest(ScriptOperation.Insert, obj));
}
}
/// <summary>
/// Verify the script as update request
/// </summary>
[Fact]
public async void ScriptingScriptAsUpdate()
{
foreach (string obj in objects)
{
Assert.NotNull(await SendAndValidateScriptRequest(ScriptOperation.Select, obj));
}
}
/// <summary>
/// Verify the script as delete request
/// </summary>
[Fact]
public async void ScriptingScriptAsDelete()
{
foreach (string obj in objects)
{
Assert.NotNull(await SendAndValidateScriptRequest(ScriptOperation.Select, obj));
Assert.NotNull(await SendAndValidateScriptRequest(false));
Assert.NotNull(await SendAndValidateScriptRequest(true));
}
}
}