mirror of
https://github.com/ckaczor/sqltoolsservice.git
synced 2026-01-14 01:25:40 -05:00
Adding new handler for script as requests (#552)
* added new scripting handler for script as request *change the language service to get the peek definition using scripting as operatin
This commit is contained in:
@@ -3,17 +3,19 @@
|
||||
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
||||
//
|
||||
|
||||
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 Microsoft.SqlTools.ServiceLayer.Test.Common;
|
||||
using Microsoft.SqlTools.ServiceLayer.Workspace.Contracts;
|
||||
using Moq;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Xunit;
|
||||
|
||||
using static Microsoft.SqlTools.ServiceLayer.IntegrationTests.Utility.LiveConnectionHelper;
|
||||
|
||||
namespace Microsoft.SqlTools.ServiceLayer.IntegrationTests.Scripting
|
||||
{
|
||||
@@ -27,7 +29,7 @@ namespace Microsoft.SqlTools.ServiceLayer.IntegrationTests.Scripting
|
||||
private const string ViewName = "test";
|
||||
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[] objects = new string[5] { "Table", "View", "Schema", "Database", "SProc" };
|
||||
private string[] selectObjects = new string[2] { "Table", "View" };
|
||||
|
||||
private LiveConnectionHelper.TestConnectionResult GetLiveAutoCompleteTestObjects()
|
||||
@@ -82,5 +84,158 @@ namespace Microsoft.SqlTools.ServiceLayer.IntegrationTests.Scripting
|
||||
Assert.NotNull(await SendAndValidateScriptRequest(true));
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async void VerifyScriptAsCreateTable()
|
||||
{
|
||||
string query = "CREATE TABLE testTable1 (c1 int)";
|
||||
string scriptCreateDrop = "ScriptCreate";
|
||||
ScriptingObject scriptingObject = new ScriptingObject
|
||||
{
|
||||
Name = "testTable1",
|
||||
Schema = "dbo",
|
||||
Type = "Table"
|
||||
};
|
||||
string expectedScript = "CREATE TABLE [dbo].[testTable1]";
|
||||
|
||||
await VerifyScriptAs(query, scriptingObject, scriptCreateDrop, expectedScript);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async void VerifyScriptAsCreateView()
|
||||
{
|
||||
string query = "CREATE VIEW testView1 AS SELECT * from sys.all_columns";
|
||||
string scriptCreateDrop = "ScriptCreate";
|
||||
ScriptingObject scriptingObject = new ScriptingObject
|
||||
{
|
||||
Name = "testView1",
|
||||
Schema = "dbo",
|
||||
Type = "View"
|
||||
};
|
||||
string expectedScript = "CREATE VIEW [dbo].[testView1] AS";
|
||||
|
||||
await VerifyScriptAs(query, scriptingObject, scriptCreateDrop, expectedScript);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async void VerifyScriptAsCreateStoredProcedure()
|
||||
{
|
||||
string query = "CREATE PROCEDURE testSp1 AS BEGIN Select * from sys.all_columns END";
|
||||
string scriptCreateDrop = "ScriptCreate";
|
||||
ScriptingObject scriptingObject = new ScriptingObject
|
||||
{
|
||||
Name = "testSp1",
|
||||
Schema = "dbo",
|
||||
Type = "StoredProcedure"
|
||||
};
|
||||
string expectedScript = "CREATE PROCEDURE [dbo].[testSp1] AS";
|
||||
|
||||
await VerifyScriptAs(query, scriptingObject, scriptCreateDrop, expectedScript);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async void VerifyScriptAsDropTable()
|
||||
{
|
||||
string query = "CREATE TABLE testTable1 (c1 int)";
|
||||
string scriptCreateDrop = "ScriptDrop";
|
||||
ScriptingObject scriptingObject = new ScriptingObject
|
||||
{
|
||||
Name = "testTable1",
|
||||
Schema = "dbo",
|
||||
Type = "Table"
|
||||
};
|
||||
string expectedScript = "DROP TABLE [dbo].[testTable1]";
|
||||
|
||||
await VerifyScriptAs(query, scriptingObject, scriptCreateDrop, expectedScript);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async void VerifyScriptAsDropView()
|
||||
{
|
||||
string query = "CREATE VIEW testView1 AS SELECT * from sys.all_columns";
|
||||
string scriptCreateDrop = "ScriptDrop";
|
||||
ScriptingObject scriptingObject = new ScriptingObject
|
||||
{
|
||||
Name = "testView1",
|
||||
Schema = "dbo",
|
||||
Type = "View"
|
||||
};
|
||||
string expectedScript = "DROP VIEW [dbo].[testView1]";
|
||||
|
||||
await VerifyScriptAs(query, scriptingObject, scriptCreateDrop, expectedScript);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async void VerifyScriptAsDropStoredProcedure()
|
||||
{
|
||||
string query = "CREATE PROCEDURE testSp1 AS BEGIN Select * from sys.all_columns END";
|
||||
string scriptCreateDrop = "ScriptDrop";
|
||||
ScriptingObject scriptingObject = new ScriptingObject
|
||||
{
|
||||
Name = "testSp1",
|
||||
Schema = "dbo",
|
||||
Type = "StoredProcedure"
|
||||
};
|
||||
string expectedScript = "DROP PROCEDURE [dbo].[testSp1]";
|
||||
|
||||
await VerifyScriptAs(query, scriptingObject, scriptCreateDrop, expectedScript);
|
||||
}
|
||||
|
||||
private async Task VerifyScriptAs(string query, ScriptingObject scriptingObject, string scriptCreateDrop, string expectedScript)
|
||||
{
|
||||
var testDb = await SqlTestDb.CreateNewAsync(TestServerType.OnPrem, false, null, query, "ScriptingTests");
|
||||
try
|
||||
{
|
||||
var requestContext = new Mock<RequestContext<ScriptingResult>>();
|
||||
requestContext.Setup(x => x.SendResult(It.IsAny<ScriptingResult>())).Returns(Task.FromResult(new object()));
|
||||
ConnectionService connectionService = LiveConnectionHelper.GetLiveTestConnectionService();
|
||||
using (SelfCleaningTempFile queryTempFile = new SelfCleaningTempFile())
|
||||
{
|
||||
//Opening a connection to db to lock the db
|
||||
TestConnectionResult connectionResult = await LiveConnectionHelper.InitLiveConnectionInfoAsync(testDb.DatabaseName, queryTempFile.FilePath, ConnectionType.Default);
|
||||
var scriptingParams = new ScriptingParams
|
||||
{
|
||||
OwnerUri = queryTempFile.FilePath,
|
||||
ScriptDestination = "ToEditor"
|
||||
};
|
||||
|
||||
scriptingParams.ScriptOptions = new ScriptOptions
|
||||
{
|
||||
ScriptCreateDrop = scriptCreateDrop,
|
||||
|
||||
};
|
||||
|
||||
scriptingParams.ScriptingObjects = new List<ScriptingObject>
|
||||
{
|
||||
scriptingObject
|
||||
};
|
||||
|
||||
|
||||
ScriptingService service = new ScriptingService();
|
||||
await service.HandleScriptingScriptAsRequest(scriptingParams, requestContext.Object);
|
||||
Thread.Sleep(2000);
|
||||
await service.ScriptingTask;
|
||||
|
||||
requestContext.Verify(x => x.SendResult(It.Is<ScriptingResult>(r => VerifyScriptingResult(r, expectedScript))));
|
||||
connectionService.Disconnect(new ServiceLayer.Connection.Contracts.DisconnectParams
|
||||
{
|
||||
OwnerUri = queryTempFile.FilePath
|
||||
});
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
throw;
|
||||
}
|
||||
finally
|
||||
{
|
||||
await testDb.CleanupAsync();
|
||||
}
|
||||
}
|
||||
|
||||
private static bool VerifyScriptingResult(ScriptingResult result, string expected)
|
||||
{
|
||||
return !string.IsNullOrEmpty(result.Script) && result.Script.Contains(expected);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user