3278 Kusto Unit Tests - Part 2 (#1063)

* 3278 Added unit tests in MetadataFactoryTests and Microsoft.Kusto.ServiceLayer.UnitTests project

* 3278 Removed todo and changed unit test to validate megabytes

* 3278 Added file and unit tests in AutoCompleteHelperTests.cs

* 3278 Removed unused functions from Kusto > ScriptAsScriptingOperation

* 3278 Added unit tests for DataSourceFactory

* 3278 Refactored AdminService to pass in ConnectionService rather than through instance variable. Added unit test for AdminServiceTests

* 3278 Refactored DataSourceFactory to not have static functions for future unit tests

* 3278 Re-added properties that were flagged as unused but are being used by ADS in ReliableDataSourceConnection.cs

* 3278 Re-added properties that were flagged as unused but are being used by ADS in ReliableDataSourceConnection.cs

* adding pipeline to execute tests (#1062)

* 3278 Converted GetDefaultAutoComplete and GetDefaultSemanticMarkers to static functions in DataSourceFactory. Removed unused constructor in ScriptFile. Added positive unit tests for both functions

* undoing release version bump

* adding additional configs

* 3278 Minor refactors in ConnectionInfo, BindingQueue, DiagnosticsHelper, MetadataService, and HostLoader. Changed AssemblyInfo to only allow Kusto Unit Tests for internal access. Added lots of unit tests.

* 3278 Commented out bindingContext.IsConnected in AddConnectionContext_Sets_BindingContext

* 3278 Reversed order of unit tests in ConnectedBindingQueueTests and added throw to Catch block.

* 3278 Reverted change to ConnectedBindingQueue. Removed unit test from AddConnectionContext for NeedsMetaData True

Co-authored-by: Jorge Berumen <52225468+joberume@users.noreply.github.com>
Co-authored-by: joberume <jberumen3@miners.utep.edu>
This commit is contained in:
Justin M
2020-09-03 16:17:39 -07:00
committed by GitHub
parent 5cf5b59a0d
commit c932ef8613
16 changed files with 877 additions and 96 deletions

View File

@@ -0,0 +1,45 @@
using System;
using System.Threading;
using Microsoft.Kusto.ServiceLayer.LanguageServices;
using Microsoft.Kusto.ServiceLayer.LanguageServices.Contracts;
using NUnit.Framework;
namespace Microsoft.Kusto.ServiceLayer.UnitTests.LanguageServices
{
public class BindingQueueTests
{
[Test]
public void QueueBindingOperation_Returns_Null_For_NullBindOperation()
{
var bindingQueue = new BindingQueue<ConnectedBindingContext>();
var queueItem = bindingQueue.QueueBindingOperation("", null);
Assert.IsNull(queueItem);
}
[Test]
public void QueueBindingOperation_Returns_QueueItem()
{
var key = "key";
var bindOperation = new Func<IBindingContext, CancellationToken, object>((context, token) => new Hover());
Func<IBindingContext, object> timeoutOperation = (context) => LanguageService.HoverTimeout;
Func<Exception, object> errorHandler = exception => new Exception();
var bindingTimeout = 30;
var waitForLockTimeout = 45;
var bindingQueue = new BindingQueue<ConnectedBindingContext>();
var queueItem = bindingQueue.QueueBindingOperation(key,
bindOperation,
timeoutOperation,
errorHandler,
bindingTimeout,
waitForLockTimeout);
Assert.AreEqual(key, queueItem.Key);
Assert.AreEqual(bindOperation, queueItem.BindOperation);
Assert.AreEqual(timeoutOperation, queueItem.TimeoutOperation);
Assert.AreEqual(errorHandler, queueItem.ErrorHandler);
Assert.AreEqual(bindingTimeout, queueItem.BindingTimeout);
Assert.AreEqual(waitForLockTimeout, queueItem.WaitForLockTimeout);
}
}
}

View File

@@ -0,0 +1,144 @@
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 connectionOpenerMock = new Mock<ISqlConnectionOpener>();
var dataSourceFactory = new Mock<IDataSourceFactory>();
var connectedBindingQueue = new ConnectedBindingQueue(connectionOpenerMock.Object, 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 connectionOpenerMock = new Mock<ISqlConnectionOpener>();
var dataSourceFactory = new Mock<IDataSourceFactory>();
var connectedBindingQueue = new ConnectedBindingQueue(connectionOpenerMock.Object, dataSourceFactory.Object);
var connectionKey = connectedBindingQueue.AddConnectionContext(connectionInfo, false, "featureName");
Assert.AreEqual("NULL_NULL_NULL_NULL", connectionKey);
}
[TestCase(false)]
public void AddConnectionContext_Sets_BindingContext(bool needsMetadata)
{
var connectionDetails = new ConnectionDetails();
var connectionFactory = new Mock<IDataSourceConnectionFactory>();
var connectionInfo = new ConnectionInfo(connectionFactory.Object, "ownerUri", connectionDetails);
var connectionOpenerMock = new Mock<ISqlConnectionOpener>();
var fakeServerConnection = new ServerConnection();
connectionOpenerMock
.Setup(x => x.OpenServerConnection(It.IsAny<ConnectionInfo>(), It.IsAny<string>()))
.Returns(fakeServerConnection);
var dataSourceFactory = new Mock<IDataSourceFactory>();
var dataSourceMock = new Mock<IDataSource>();
dataSourceFactory
.Setup(x => x.Create(It.IsAny<DataSourceType>(), It.IsAny<string>(), It.IsAny<string>()))
.Returns(dataSourceMock.Object);
var connectedBindingQueue =
new ConnectedBindingQueue(connectionOpenerMock.Object, dataSourceFactory.Object);
var connectionKey =
connectedBindingQueue.AddConnectionContext(connectionInfo, needsMetadata, "featureName");
var bindingContext = connectedBindingQueue.GetOrCreateBindingContext(connectionKey);
Assert.AreEqual(fakeServerConnection, bindingContext.ServerConnection);
Assert.AreEqual(dataSourceMock.Object, bindingContext.DataSource);
Assert.AreEqual(500, bindingContext.BindingTimeout);
Assert.AreEqual(true, bindingContext.IsConnected);
Assert.AreEqual(CasingStyle.Uppercase, bindingContext.MetadataDisplayInfoProvider.BuiltInCasing);
Assert.IsNull(bindingContext.SmoMetadataProvider);
Assert.IsNull(bindingContext.Binder);
}
[Test]
public void RemoveBindingContext_Removes_Context()
{
var connectionDetails = new ConnectionDetails();
var connectionFactory = new Mock<IDataSourceConnectionFactory>();
var connectionInfo = new ConnectionInfo(connectionFactory.Object, "ownerUri", connectionDetails);
var connectionOpenerMock = new Mock<ISqlConnectionOpener>();
var dataSourceFactory = new Mock<IDataSourceFactory>();
var connectedBindingQueue = new ConnectedBindingQueue(connectionOpenerMock.Object, dataSourceFactory.Object);
var connectionKey = connectedBindingQueue.AddConnectionContext(connectionInfo, false, "featureName");
connectedBindingQueue.RemoveBindingContext(connectionInfo);
Assert.IsFalse(connectedBindingQueue.BindingContextMap.ContainsKey(connectionKey));
}
}
}

View File

@@ -0,0 +1,136 @@
using System;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Kusto.ServiceLayer.LanguageServices;
using Microsoft.Kusto.ServiceLayer.LanguageServices.Contracts;
using Microsoft.Kusto.ServiceLayer.Workspace.Contracts;
using Microsoft.SqlTools.Hosting.Protocol;
using Microsoft.SqlTools.Hosting.Protocol.Contracts;
using Moq;
using NUnit.Framework;
namespace Microsoft.Kusto.ServiceLayer.UnitTests.LanguageServices
{
public class DiagnosticsHelperTests
{
[TestCase("")]
[TestCase(null)]
public void ClearScriptDiagnostics_Throws_Exception_InvalidUri(string uri)
{
Assert.ThrowsAsync<ArgumentException>(() => DiagnosticsHelper.ClearScriptDiagnostics(uri, new EventContext()));
}
[Test]
public void ClearScriptDiagnostics_Throws_Exception_InvalidEventContext()
{
Assert.ThrowsAsync<ArgumentNullException>(() => DiagnosticsHelper.ClearScriptDiagnostics("uri", null));
}
[Test]
public void ClearScriptDiagnostics_SendsEvent_ValidParams()
{
var uri = "uri";
var eventContextMock = new Mock<EventContext>();
var task = DiagnosticsHelper.ClearScriptDiagnostics(uri, eventContextMock.Object);
task.Wait();
eventContextMock.Verify(
e => e.SendEvent(PublishDiagnosticsNotification.Type,
It.Is<PublishDiagnosticsNotification>(x => x.Uri == uri && x.Diagnostics.Length == 0)), Times.Once);
}
[TestCase(ScriptFileMarkerLevel.Error, DiagnosticSeverity.Error)]
[TestCase(ScriptFileMarkerLevel.Warning, DiagnosticSeverity.Warning)]
[TestCase(ScriptFileMarkerLevel.Information, DiagnosticSeverity.Information)]
public async Task PublishScriptDiagnostics_Maps_Severity(ScriptFileMarkerLevel markerLevel, DiagnosticSeverity expected)
{
var uri = "uri";
var scriptFile = new ScriptFile("", uri, "");
ScriptFileMarker[] semanticMarkers =
{
new ScriptFileMarker
{
Level = markerLevel,
ScriptRegion = new ScriptRegion
{
StartLineNumber = 1,
StartColumnNumber = 1,
EndLineNumber = 2,
EndColumnNumber = 2
}
}
};
var actualEventType = new EventType<PublishDiagnosticsNotification>();
var actualNotification = new PublishDiagnosticsNotification();
var eventContextMock = new Mock<EventContext>();
eventContextMock.Setup(x => x.SendEvent(It.IsAny<EventType<PublishDiagnosticsNotification>>(),
It.IsAny<PublishDiagnosticsNotification>()))
.Callback<EventType<PublishDiagnosticsNotification>, PublishDiagnosticsNotification>(
(eventType, notification) =>
{
actualEventType = eventType;
actualNotification = notification;
})
.Returns(Task.FromResult(0));
await DiagnosticsHelper.PublishScriptDiagnostics(scriptFile, semanticMarkers, eventContextMock.Object);
eventContextMock.Verify(e => e.SendEvent(PublishDiagnosticsNotification.Type,
It.IsAny<PublishDiagnosticsNotification>()), Times.Once);
Assert.AreEqual(PublishDiagnosticsNotification.Type.MethodName, actualEventType.MethodName);
Assert.AreEqual(uri, actualNotification.Uri);
Assert.AreEqual(1, actualNotification.Diagnostics.Length);
var diagnostic = actualNotification.Diagnostics.First();
Assert.AreEqual(expected, diagnostic.Severity);
}
[Test]
public async Task PublishScriptDiagnostics_Creates_Diagnostic()
{
var uri = "uri";
var scriptFile = new ScriptFile("", uri, "");
var fileMarker = new ScriptFileMarker
{
Message = "Message",
Level = ScriptFileMarkerLevel.Information,
ScriptRegion = new ScriptRegion
{
StartLineNumber = 1,
StartColumnNumber = 1,
EndLineNumber = 2,
EndColumnNumber = 2
}
};
var actualEventType = new EventType<PublishDiagnosticsNotification>();
var actualNotification = new PublishDiagnosticsNotification();
var eventContextMock = new Mock<EventContext>();
eventContextMock.Setup(x => x.SendEvent(It.IsAny<EventType<PublishDiagnosticsNotification>>(),
It.IsAny<PublishDiagnosticsNotification>()))
.Callback<EventType<PublishDiagnosticsNotification>, PublishDiagnosticsNotification>(
(eventType, notification) =>
{
actualEventType = eventType;
actualNotification = notification;
})
.Returns(Task.FromResult(0));
await DiagnosticsHelper.PublishScriptDiagnostics(scriptFile, new[] {fileMarker}, eventContextMock.Object);
Assert.AreEqual(PublishDiagnosticsNotification.Type.MethodName, actualEventType.MethodName);
Assert.AreEqual(uri, actualNotification.Uri);
Assert.AreEqual(1, actualNotification.Diagnostics.Length);
var diagnostic = actualNotification.Diagnostics.First();
Assert.AreEqual(null, diagnostic.Code);
Assert.AreEqual(fileMarker.Message, diagnostic.Message);
Assert.AreEqual(0, diagnostic.Range.Start.Character);
Assert.AreEqual(0, diagnostic.Range.Start.Line);
Assert.AreEqual(1, diagnostic.Range.End.Character);
Assert.AreEqual(1, diagnostic.Range.End.Line);
Assert.AreEqual(DiagnosticSeverity.Information, diagnostic.Severity);
}
}
}