mirror of
https://github.com/ckaczor/sqltoolsservice.git
synced 2026-01-14 01:25:40 -05:00
Added functions to metadata list result (#568)
* added functions to be included in metadata list result
This commit is contained in:
@@ -3,18 +3,22 @@
|
||||
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
||||
//
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data.SqlClient;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.SqlTools.Hosting.Protocol;
|
||||
using Microsoft.SqlTools.ServiceLayer.Connection;
|
||||
using Microsoft.SqlTools.ServiceLayer.IntegrationTests.Utility;
|
||||
using Microsoft.SqlTools.ServiceLayer.Metadata;
|
||||
using Microsoft.SqlTools.ServiceLayer.Metadata.Contracts;
|
||||
using Microsoft.SqlTools.ServiceLayer.Test.Common;
|
||||
using Microsoft.SqlTools.ServiceLayer.Workspace.Contracts;
|
||||
using Moq;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data.SqlClient;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Xunit;
|
||||
using static Microsoft.SqlTools.ServiceLayer.IntegrationTests.Utility.LiveConnectionHelper;
|
||||
|
||||
namespace Microsoft.SqlTools.ServiceLayer.IntegrationTests.Metadata
|
||||
{
|
||||
@@ -142,5 +146,124 @@ namespace Microsoft.SqlTools.ServiceLayer.IntegrationTests.Metadata
|
||||
requestContext.VerifyAll();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async void VerifyMetadataList()
|
||||
{
|
||||
string query = @"CREATE TABLE testTable1 (c1 int)
|
||||
GO
|
||||
CREATE PROCEDURE testSp1 @StartProductID [int] AS BEGIN Select * from sys.all_columns END
|
||||
GO
|
||||
CREATE VIEW testView1 AS SELECT * from sys.all_columns
|
||||
GO
|
||||
CREATE FUNCTION testFun1() RETURNS [int] AS BEGIN RETURN 1 END
|
||||
GO
|
||||
CREATE FUNCTION [testFun2](@CityID int)
|
||||
RETURNS TABLE
|
||||
WITH SCHEMABINDING
|
||||
AS
|
||||
RETURN SELECT 1 AS AccessResult
|
||||
GO";
|
||||
|
||||
List<ObjectMetadata> expectedMetadataList = new List<ObjectMetadata>
|
||||
{
|
||||
new ObjectMetadata
|
||||
{
|
||||
MetadataType = MetadataType.Table,
|
||||
MetadataTypeName = "Table",
|
||||
Name = "testTable1",
|
||||
Schema = "dbo"
|
||||
},
|
||||
new ObjectMetadata
|
||||
{
|
||||
MetadataType = MetadataType.SProc,
|
||||
MetadataTypeName = "StoredProcedure",
|
||||
Name = "testSp1",
|
||||
Schema = "dbo"
|
||||
},
|
||||
new ObjectMetadata
|
||||
{
|
||||
MetadataType = MetadataType.View,
|
||||
MetadataTypeName = "View",
|
||||
Name = "testView1",
|
||||
Schema = "dbo"
|
||||
},
|
||||
new ObjectMetadata
|
||||
{
|
||||
MetadataType = MetadataType.Function,
|
||||
MetadataTypeName = "UserDefinedFunction",
|
||||
Name = "testFun1",
|
||||
Schema = "dbo"
|
||||
},
|
||||
new ObjectMetadata
|
||||
{
|
||||
MetadataType = MetadataType.Function,
|
||||
MetadataTypeName = "UserDefinedFunction",
|
||||
Name = "testFun2",
|
||||
Schema = "dbo"
|
||||
}
|
||||
};
|
||||
|
||||
await VerifyMetadataList(query, expectedMetadataList);
|
||||
}
|
||||
|
||||
private async Task VerifyMetadataList(string query, List<ObjectMetadata> expectedMetadataList)
|
||||
{
|
||||
var testDb = await SqlTestDb.CreateNewAsync(TestServerType.OnPrem, false, null, query, "MetadataTests");
|
||||
try
|
||||
{
|
||||
var requestContext = new Mock<RequestContext<MetadataQueryResult>>();
|
||||
requestContext.Setup(x => x.SendResult(It.IsAny<MetadataQueryResult>())).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);
|
||||
|
||||
MetadataService service = new MetadataService();
|
||||
await service.HandleMetadataListRequest(new MetadataQueryParams
|
||||
{
|
||||
OwnerUri = queryTempFile.FilePath
|
||||
}, requestContext.Object);
|
||||
Thread.Sleep(2000);
|
||||
await service.MetadataListTask;
|
||||
|
||||
requestContext.Verify(x => x.SendResult(It.Is<MetadataQueryResult>(r => VerifyResult(r, expectedMetadataList))));
|
||||
connectionService.Disconnect(new ServiceLayer.Connection.Contracts.DisconnectParams
|
||||
{
|
||||
OwnerUri = queryTempFile.FilePath
|
||||
});
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
throw;
|
||||
}
|
||||
finally
|
||||
{
|
||||
await testDb.CleanupAsync();
|
||||
}
|
||||
}
|
||||
|
||||
private static bool VerifyResult(MetadataQueryResult result, List<ObjectMetadata> expectedMetadataList)
|
||||
{
|
||||
if (expectedMetadataList == null)
|
||||
{
|
||||
return result.Metadata == null;
|
||||
}
|
||||
|
||||
if(expectedMetadataList.Count() != result.Metadata.Count())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
foreach (ObjectMetadata expected in expectedMetadataList)
|
||||
{
|
||||
if (!result.Metadata.Any(x => x.MetadataType == expected.MetadataType && x.MetadataTypeName == expected.MetadataTypeName && x.Name == expected.Name && x.Schema == expected.Schema))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user