mirror of
https://github.com/ckaczor/sqltoolsservice.git
synced 2026-02-01 01:25:43 -05:00
Initial metadata and scripting services (#280)
* Initial metadata service and scripting service files * Simple metadata lookup with SMO objects * Add metadata type class * Remove SMO from metadata service. * Cleanup metadata service SQL * Initial MetadataService test * Add scripting commands * Add metadata test case * Remove sleep used for testing * Use random table name in metadata test * Add scripting tests
This commit is contained in:
@@ -0,0 +1,96 @@
|
||||
//
|
||||
// Copyright (c) Microsoft. All rights reserved.
|
||||
// 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.Test.Common;
|
||||
using Microsoft.SqlTools.ServiceLayer.IntegrationTests.Utility;
|
||||
using Microsoft.SqlTools.ServiceLayer.Metadata;
|
||||
using System.Collections.Generic;
|
||||
using Microsoft.SqlTools.ServiceLayer.Metadata.Contracts;
|
||||
using System.Data.SqlClient;
|
||||
using System;
|
||||
|
||||
namespace Microsoft.SqlTools.ServiceLayer.IntegrationTests.Metadata
|
||||
{
|
||||
/// <summary>
|
||||
/// Tests for the Metadata service component
|
||||
/// </summary>
|
||||
public class MetadataServiceTests
|
||||
{
|
||||
private string testTableSchema = "dbo";
|
||||
private string testTableName = "MetadataTestTable";
|
||||
|
||||
private LiveConnectionHelper.TestConnectionResult GetLiveAutoCompleteTestObjects()
|
||||
{
|
||||
var textDocument = new TextDocumentPosition
|
||||
{
|
||||
TextDocument = new TextDocumentIdentifier { Uri = Constants.OwnerUri },
|
||||
Position = new Position
|
||||
{
|
||||
Line = 0,
|
||||
Character = 0
|
||||
}
|
||||
};
|
||||
|
||||
var result = LiveConnectionHelper.InitLiveConnectionInfo();
|
||||
result.TextDocumentPosition = textDocument;
|
||||
return result;
|
||||
}
|
||||
|
||||
private void CreateTestTable(SqlConnection sqlConn)
|
||||
{
|
||||
string sql = string.Format("IF OBJECT_ID('{0}.{1}', 'U') IS NULL CREATE TABLE {0}.{1}(id int)",
|
||||
this.testTableSchema, this.testTableName);
|
||||
using (var sqlCommand = new SqlCommand(sql, sqlConn))
|
||||
{
|
||||
sqlCommand.ExecuteNonQuery();
|
||||
}
|
||||
}
|
||||
|
||||
private void DeleteTestTable(SqlConnection sqlConn)
|
||||
{
|
||||
string sql = string.Format("IF OBJECT_ID('{0}.{1}', 'U') IS NOT NULL DROP TABLE {0}.{1}",
|
||||
this.testTableSchema, this.testTableName);
|
||||
using (var sqlCommand = new SqlCommand(sql, sqlConn))
|
||||
{
|
||||
sqlCommand.ExecuteNonQuery();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Verify that the metadata service correctly returns details for user tables
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void MetadataReturnsUserTable()
|
||||
{
|
||||
this.testTableName += new Random().Next(1000000, 9999999).ToString();
|
||||
|
||||
var result = GetLiveAutoCompleteTestObjects();
|
||||
var sqlConn = MetadataService.OpenMetadataConnection(result.ConnectionInfo);
|
||||
Assert.NotNull(sqlConn);
|
||||
|
||||
CreateTestTable(sqlConn);
|
||||
|
||||
var metadata = new List<ObjectMetadata>();
|
||||
MetadataService.ReadMetadata(sqlConn, metadata);
|
||||
Assert.NotNull(metadata.Count > 0);
|
||||
|
||||
bool foundTestTable = false;
|
||||
foreach (var item in metadata)
|
||||
{
|
||||
if (string.Equals(item.Schema, this.testTableSchema, StringComparison.OrdinalIgnoreCase)
|
||||
&& string.Equals(item.Name, this.testTableName, StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
foundTestTable = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
Assert.True(foundTestTable);
|
||||
|
||||
DeleteTestTable(sqlConn);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,118 @@
|
||||
//
|
||||
// Copyright (c) Microsoft. All rights reserved.
|
||||
// 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.Threading.Tasks;
|
||||
|
||||
namespace Microsoft.SqlTools.ServiceLayer.IntegrationTests.Scripting
|
||||
{
|
||||
/// <summary>
|
||||
/// Tests for the scripting service component
|
||||
/// </summary>
|
||||
public class ScriptingServiceTests
|
||||
{
|
||||
private const string SchemaName = "sys";
|
||||
private const string TableName = "all_objects";
|
||||
|
||||
private LiveConnectionHelper.TestConnectionResult GetLiveAutoCompleteTestObjects()
|
||||
{
|
||||
var textDocument = new TextDocumentPosition
|
||||
{
|
||||
TextDocument = new TextDocumentIdentifier { Uri = Test.Common.Constants.OwnerUri },
|
||||
Position = new Position
|
||||
{
|
||||
Line = 0,
|
||||
Character = 0
|
||||
}
|
||||
};
|
||||
|
||||
var result = LiveConnectionHelper.InitLiveConnectionInfo();
|
||||
result.TextDocumentPosition = textDocument;
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
private async Task<Mock<RequestContext<ScriptingScriptAsResult>>> SendAndValidateScriptRequest(ScriptOperation operation)
|
||||
{
|
||||
var result = GetLiveAutoCompleteTestObjects();
|
||||
var requestContext = new Mock<RequestContext<ScriptingScriptAsResult>>();
|
||||
requestContext.Setup(x => x.SendResult(It.IsAny<ScriptingScriptAsResult>())).Returns(Task.FromResult(new object()));
|
||||
|
||||
var scriptingParams = new ScriptingScriptAsParams
|
||||
{
|
||||
OwnerUri = Test.Common.Constants.OwnerUri,
|
||||
Operation = operation,
|
||||
Metadata = new ObjectMetadata()
|
||||
{
|
||||
MetadataType = MetadataType.Table,
|
||||
MetadataTypeName = "View",
|
||||
Schema = SchemaName,
|
||||
Name = TableName
|
||||
}
|
||||
};
|
||||
|
||||
await ScriptingService.HandleScriptingScriptAsRequest(scriptingParams, requestContext.Object);
|
||||
|
||||
requestContext.Verify(x => x.SendResult(It.Is<ScriptingScriptAsResult>(
|
||||
i => i.Script.Contains(operation.ToString().ToUpper())
|
||||
&& i.Script.Contains(TableName)
|
||||
&& i.Script.Contains(SchemaName))));
|
||||
|
||||
return requestContext;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Verify the script as select request
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public async void ScriptingScriptAsSelect()
|
||||
{
|
||||
await SendAndValidateScriptRequest(ScriptOperation.Select);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Verify the script as create request
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public async void ScriptingScriptAsCreate()
|
||||
{
|
||||
await SendAndValidateScriptRequest(ScriptOperation.Create);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Verify the script as insert request
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public async void ScriptingScriptAsInsert()
|
||||
{
|
||||
await SendAndValidateScriptRequest(ScriptOperation.Insert);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Verify the script as update request
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public async void ScriptingScriptAsUpdate()
|
||||
{
|
||||
await SendAndValidateScriptRequest(ScriptOperation.Update);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Verify the script as delete request
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public async void ScriptingScriptAsDelete()
|
||||
{
|
||||
await SendAndValidateScriptRequest(ScriptOperation.Delete);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user