// // 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 { /// /// Tests for the Metadata service component /// 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(); } } /// /// Verify that the metadata service correctly returns details for user tables /// [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(); 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.Setup(x => x.SendResult(It.IsAny())).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.Setup(x => x.SendResult(It.IsAny())).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(); } } }