Files
sqltoolsservice/test/Microsoft.Kusto.ServiceLayer.UnitTests/LanguageServices/ConnectedBindingQueueTests.cs
Justin M 1f7da97e6c Added AzureMonitor to Microsoft.Kusto.ServiceLayer (#1208)
* Added AzureMonitor to Microsoft.Kusto.ServiceLayer.

* Added Intellisense for AzureMonitor. Moved Intellisense logic from KustoIntellisenseClient to IntellisenseClientBase.

* Added ServiceName as a command parameter for starting Kusto.

* Added check to return null if connectionInfo is not in the connectionService.

* Added support for Dashboard in MetadataService and AdminService.

* Removed workspace id from databaseName for Monitor. Added logic for MetadataService and AdminService to return different information for AzureMonitor.

* Moved providerName and providerDescription to DataSourceFactory.

* Changed DatabaseName to include Name and Id. Changed ProviderName to LOGANALYTICS in DataSourceFactory

* Fixed unit tests

* Changed logic to use ServiceName instead of server to determine DataSourceType

* Code review feedback and reverted changes to ObjectExplorerService.

* Removed unused reference from HostLoader

* Changed Parallel.Invoke to await Task.Run

* Moved Kusto datasource and supporting classes to separate directory.

* Removed unused datasourceFactory from ConnectionService. Added GetDatabases and GetDatabaseInfo to IDataSource and child classes

* Renamed Instance variables in ObjectExplorerService. Removed unused attribute on TSqlFormatterService. Removed invalid comment in ConnectionService.

* Fixed warnings in build.

* Moved SizeInMB to DatabaseMetadata. Refactored ConvertToDatabaseInfo

* Fixed unit test
2021-06-25 21:40:45 -07:00

133 lines
5.6 KiB
C#

using System;
using System.Collections.Generic;
using Microsoft.Kusto.ServiceLayer.Connection;
using Microsoft.Kusto.ServiceLayer.Connection.Contracts;
using Microsoft.Kusto.ServiceLayer.DataSource;
using Microsoft.Kusto.ServiceLayer.LanguageServices;
using Microsoft.SqlServer.Management.Common;
using Microsoft.SqlServer.Management.SqlParser.MetadataProvider;
using Moq;
using NUnit.Framework;
namespace Microsoft.Kusto.ServiceLayer.UnitTests.LanguageServices
{
public class ConnectedBindingQueueTests
{
private static IEnumerable<Tuple<ConnectionDetails, string>> ConnectionDetailsSource()
{
var results = new List<Tuple<ConnectionDetails, string>>();
var details1 = new ConnectionDetails
{
ServerName = "ServerName",
DatabaseName = "DatabaseName",
UserName = "UserName",
AuthenticationType = "AuthenticationType",
DatabaseDisplayName = "DisplayName",
GroupId = "GroupId"
};
results.Add(new Tuple<ConnectionDetails, string>(details1, "ServerName_DatabaseName_UserName_AuthenticationType_DisplayName_GroupId"));
var details2 = new ConnectionDetails
{
ServerName = null,
DatabaseName = null,
UserName = null,
AuthenticationType = null,
DatabaseDisplayName = "",
GroupId = ""
};
results.Add(new Tuple<ConnectionDetails, string>(details2, "NULL_NULL_NULL_NULL"));
var details3 = new ConnectionDetails
{
ServerName = null,
DatabaseName = null,
UserName = null,
AuthenticationType = null,
DatabaseDisplayName = null,
GroupId = null
};
results.Add(new Tuple<ConnectionDetails, string>(details3, "NULL_NULL_NULL_NULL"));
return results;
}
[TestCaseSource(nameof(ConnectionDetailsSource))]
public void GetConnectionContextKey_Returns_Key(Tuple<ConnectionDetails, string> tuple)
{
var contextKey = ConnectedBindingQueue.GetConnectionContextKey(tuple.Item1);
Assert.AreEqual(tuple.Item2, contextKey);
}
[Test]
public void AddConnectionContext_Returns_EmptyString_For_NullConnectionInfo()
{
var dataSourceFactory = new Mock<IDataSourceFactory>();
var connectedBindingQueue = new ConnectedBindingQueue(dataSourceFactory.Object);
var connectionKey = connectedBindingQueue.AddConnectionContext(null, false);
Assert.AreEqual(string.Empty, connectionKey);
}
[Test]
public void AddConnectionContext_Returns_ConnectionKey()
{
var connectionDetails = new ConnectionDetails();
var connectionFactory = new Mock<IDataSourceConnectionFactory>();
var connectionInfo = new ConnectionInfo(connectionFactory.Object, "ownerUri", connectionDetails);
var dataSourceFactory = new Mock<IDataSourceFactory>();
var connectedBindingQueue = new ConnectedBindingQueue(dataSourceFactory.Object);
var connectionKey = connectedBindingQueue.AddConnectionContext(connectionInfo, false, "featureName");
Assert.AreEqual("NULL_NULL_NULL_NULL", connectionKey);
}
[Test]
public void AddConnectionContext_Sets_BindingContext()
{
var connectionDetails = new ConnectionDetails
{
AccountToken = "AzureAccountToken"
};
var connectionFactory = new Mock<IDataSourceConnectionFactory>();
var connectionInfo = new ConnectionInfo(connectionFactory.Object, "ownerUri", connectionDetails);
var dataSourceFactory = new Mock<IDataSourceFactory>();
var dataSourceMock = new Mock<IDataSource>();
dataSourceFactory
.Setup(x => x.Create(It.IsAny<ConnectionDetails>(), It.IsAny<string>()))
.Returns(dataSourceMock.Object);
var connectedBindingQueue =
new ConnectedBindingQueue(dataSourceFactory.Object);
var connectionKey =
connectedBindingQueue.AddConnectionContext(connectionInfo, false, "featureName");
var bindingContext = connectedBindingQueue.GetOrCreateBindingContext(connectionKey);
Assert.AreEqual(dataSourceMock.Object, bindingContext.DataSource);
Assert.AreEqual(500, bindingContext.BindingTimeout);
Assert.AreEqual(true, bindingContext.IsConnected);
}
[Test]
public void RemoveBindingContext_Removes_Context()
{
var connectionDetails = new ConnectionDetails();
var connectionFactory = new Mock<IDataSourceConnectionFactory>();
var connectionInfo = new ConnectionInfo(connectionFactory.Object, "ownerUri", connectionDetails);
var dataSourceFactory = new Mock<IDataSourceFactory>();
var connectedBindingQueue = new ConnectedBindingQueue(dataSourceFactory.Object);
var connectionKey = connectedBindingQueue.AddConnectionContext(connectionInfo, false, "featureName");
connectedBindingQueue.RemoveBindingContext(connectionInfo);
Assert.IsFalse(connectedBindingQueue.BindingContextMap.ContainsKey(connectionKey));
}
}
}