mirror of
https://github.com/ckaczor/sqltoolsservice.git
synced 2026-01-14 09:59:48 -05:00
* Add SMO table property lookup event * Fix a couple bugs in column metadata * Add GetView metadata message
146 lines
5.1 KiB
C#
146 lines
5.1 KiB
C#
//
|
|
// 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;
|
|
using System.Collections.Generic;
|
|
using Microsoft.SqlTools.ServiceLayer.Metadata.Contracts;
|
|
using System.Data.SqlClient;
|
|
using System;
|
|
using Moq;
|
|
using Microsoft.SqlTools.Hosting.Protocol;
|
|
using System.Threading.Tasks;
|
|
|
|
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 = Test.Common.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);
|
|
}
|
|
|
|
[Fact]
|
|
public async void GetTableInfoReturnsValidResults()
|
|
{
|
|
this.testTableName += new Random().Next(1000000, 9999999).ToString();
|
|
|
|
var result = GetLiveAutoCompleteTestObjects();
|
|
var sqlConn = MetadataService.OpenMetadataConnection(result.ConnectionInfo);
|
|
|
|
CreateTestTable(sqlConn);
|
|
|
|
var requestContext = new Mock<RequestContext<TableMetadataResult>>();
|
|
requestContext.Setup(x => x.SendResult(It.IsAny<TableMetadataResult>())).Returns(Task.FromResult(new object()));
|
|
|
|
var metadataParmas = new TableMetadataParams
|
|
{
|
|
OwnerUri = result.ConnectionInfo.OwnerUri,
|
|
Schema = this.testTableSchema,
|
|
ObjectName = this.testTableName
|
|
};
|
|
|
|
await MetadataService.HandleGetTableRequest(metadataParmas, requestContext.Object);
|
|
|
|
DeleteTestTable(sqlConn);
|
|
|
|
requestContext.VerifyAll();
|
|
}
|
|
|
|
[Fact]
|
|
public async void GetViewInfoReturnsValidResults()
|
|
{
|
|
var result = GetLiveAutoCompleteTestObjects();
|
|
var requestContext = new Mock<RequestContext<TableMetadataResult>>();
|
|
requestContext.Setup(x => x.SendResult(It.IsAny<TableMetadataResult>())).Returns(Task.FromResult(new object()));
|
|
|
|
var metadataParmas = new TableMetadataParams
|
|
{
|
|
OwnerUri = result.ConnectionInfo.OwnerUri,
|
|
Schema = "sys",
|
|
ObjectName = "all_objects"
|
|
};
|
|
|
|
await MetadataService.HandleGetViewRequest(metadataParmas, requestContext.Object);
|
|
|
|
requestContext.VerifyAll();
|
|
}
|
|
|
|
}
|
|
}
|