Files
sqltoolsservice/test/Microsoft.SqlTools.ServiceLayer.IntegrationTests/Metadata/MetadataServiceTests.cs
Karl Burtram 7ba2011a1e 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
2017-03-14 22:35:17 -07:00

97 lines
3.3 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.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);
}
}
}