mirror of
https://github.com/ckaczor/sqltoolsservice.git
synced 2026-02-01 01:25:43 -05:00
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:
@@ -0,0 +1,89 @@
|
||||
using System;
|
||||
using Microsoft.Kusto.ServiceLayer.Connection;
|
||||
using Microsoft.Kusto.ServiceLayer.Connection.Contracts;
|
||||
using Microsoft.Kusto.ServiceLayer.DataSource;
|
||||
using Microsoft.SqlTools.ServiceLayer.Connection.ReliableConnection;
|
||||
using Moq;
|
||||
using NUnit.Framework;
|
||||
|
||||
namespace Microsoft.Kusto.ServiceLayer.UnitTests.Connection
|
||||
{
|
||||
public class ConnectionInfoTests
|
||||
{
|
||||
[TestCase("")]
|
||||
[TestCase(null)]
|
||||
public void AddConnection_Throws_Exception_For_Invalid_ConnectionType(string connectionType)
|
||||
{
|
||||
var datasourceFactoryMock = new Mock<IDataSourceConnectionFactory>();
|
||||
var connectionInfo = new ConnectionInfo(datasourceFactoryMock.Object, "", new ConnectionDetails());
|
||||
Assert.Throws<ArgumentException>(() => connectionInfo.AddConnection(connectionType, null));
|
||||
}
|
||||
|
||||
[TestCase("")]
|
||||
[TestCase(null)]
|
||||
public void GetConnection_Throws_Exception_For_Invalid_ConnectionType(string connectionType)
|
||||
{
|
||||
var datasourceFactoryMock = new Mock<IDataSourceConnectionFactory>();
|
||||
var connectionInfo = new ConnectionInfo(datasourceFactoryMock.Object, "", new ConnectionDetails());
|
||||
Assert.Throws<ArgumentException>(() => connectionInfo.TryGetConnection(connectionType, out var connection));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void AddConnection_And_GetConnection_AddAndGet()
|
||||
{
|
||||
var connectionFactoryMock = new Mock<IDataSourceConnectionFactory>();
|
||||
var connectionInfo = new ConnectionInfo(connectionFactoryMock.Object, "", new ConnectionDetails());
|
||||
|
||||
var dataSourceFactoryMock = new Mock<IDataSourceFactory>();
|
||||
var reliableDataSource = new ReliableDataSourceConnection("", RetryPolicyFactory.NoRetryPolicy,
|
||||
RetryPolicyFactory.NoRetryPolicy, "", dataSourceFactoryMock.Object);
|
||||
connectionInfo.AddConnection("ConnectionType", reliableDataSource);
|
||||
|
||||
connectionInfo.TryGetConnection("ConnectionType", out var connection);
|
||||
Assert.AreEqual(reliableDataSource, connection);
|
||||
}
|
||||
|
||||
[TestCase("")]
|
||||
[TestCase(null)]
|
||||
public void RemoveConnection_Throws_Exception_For_Invalid_ConnectionType(string connectionType)
|
||||
{
|
||||
var datasourceFactoryMock = new Mock<IDataSourceConnectionFactory>();
|
||||
var connectionInfo = new ConnectionInfo(datasourceFactoryMock.Object, "", new ConnectionDetails());
|
||||
Assert.Throws<ArgumentException>(() => connectionInfo.RemoveConnection(connectionType));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void RemoveConnection_Removes_Connection()
|
||||
{
|
||||
var connectionFactoryMock = new Mock<IDataSourceConnectionFactory>();
|
||||
var connectionInfo = new ConnectionInfo(connectionFactoryMock.Object, "", new ConnectionDetails());
|
||||
|
||||
var dataSourceFactoryMock = new Mock<IDataSourceFactory>();
|
||||
var reliableDataSource = new ReliableDataSourceConnection("", RetryPolicyFactory.NoRetryPolicy,
|
||||
RetryPolicyFactory.NoRetryPolicy, "", dataSourceFactoryMock.Object);
|
||||
connectionInfo.AddConnection("ConnectionType", reliableDataSource);
|
||||
|
||||
connectionInfo.RemoveConnection("ConnectionType");
|
||||
|
||||
connectionInfo.TryGetConnection("ConnectionType", out var connection);
|
||||
Assert.IsNull(connection);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void RemoveAllConnections_RemovesAllConnections()
|
||||
{
|
||||
var connectionFactoryMock = new Mock<IDataSourceConnectionFactory>();
|
||||
var connectionInfo = new ConnectionInfo(connectionFactoryMock.Object, "", new ConnectionDetails());
|
||||
|
||||
var dataSourceFactoryMock = new Mock<IDataSourceFactory>();
|
||||
var reliableDataSource = new ReliableDataSourceConnection("", RetryPolicyFactory.NoRetryPolicy,
|
||||
RetryPolicyFactory.NoRetryPolicy, "", dataSourceFactoryMock.Object);
|
||||
connectionInfo.AddConnection("ConnectionType", reliableDataSource);
|
||||
|
||||
connectionInfo.RemoveAllConnections();
|
||||
|
||||
connectionInfo.TryGetConnection("ConnectionType", out var connection);
|
||||
Assert.IsNull(connection);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
using Microsoft.Kusto.ServiceLayer.Connection;
|
||||
using NUnit.Framework;
|
||||
|
||||
namespace Microsoft.Kusto.ServiceLayer.UnitTests.Connection
|
||||
{
|
||||
public class ConnectionProviderOptionsHelperTests
|
||||
{
|
||||
[Test]
|
||||
public void BuildConnectionProviderOptions_Returns_31_Options()
|
||||
{
|
||||
var providerOptions = ConnectionProviderOptionsHelper.BuildConnectionProviderOptions();
|
||||
Assert.AreEqual(31, providerOptions.Options.Length);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
using Microsoft.Kusto.ServiceLayer.Connection;
|
||||
using Microsoft.Kusto.ServiceLayer.DataSource;
|
||||
using Moq;
|
||||
using NUnit.Framework;
|
||||
|
||||
namespace Microsoft.Kusto.ServiceLayer.UnitTests.Connection
|
||||
{
|
||||
public class DataSourceConnectionFactoryTests
|
||||
{
|
||||
[Test]
|
||||
public void CreateDataSourceConnection_Returns_Connection()
|
||||
{
|
||||
var dataSourceFactoryMock = new Mock<IDataSourceFactory>();
|
||||
var connectionFactory = new DataSourceConnectionFactory(dataSourceFactoryMock.Object);
|
||||
var connection = connectionFactory.CreateDataSourceConnection("", "");
|
||||
|
||||
Assert.IsNotNull(connection);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
using System.Linq;
|
||||
using Kusto.Language.Editor;
|
||||
using Microsoft.Kusto.ServiceLayer.DataSource.DataSourceIntellisense;
|
||||
using Microsoft.Kusto.ServiceLayer.LanguageServices.Completion;
|
||||
using Microsoft.Kusto.ServiceLayer.LanguageServices.Contracts;
|
||||
using Microsoft.Kusto.ServiceLayer.Workspace.Contracts;
|
||||
using NUnit.Framework;
|
||||
|
||||
namespace Microsoft.Kusto.ServiceLayer.UnitTests.DataSource.DataSourceIntellisense
|
||||
{
|
||||
public class KustoIntellisenseHelperTests
|
||||
{
|
||||
[TestCase(CompletionKind.Syntax, CompletionItemKind.Module)]
|
||||
[TestCase(CompletionKind.Column, CompletionItemKind.Field)]
|
||||
[TestCase(CompletionKind.Variable, CompletionItemKind.Variable)]
|
||||
[TestCase(CompletionKind.Table, CompletionItemKind.File)]
|
||||
[TestCase(CompletionKind.Database, CompletionItemKind.Method)]
|
||||
[TestCase(CompletionKind.LocalFunction, CompletionItemKind.Function)]
|
||||
[TestCase(CompletionKind.DatabaseFunction, CompletionItemKind.Function)]
|
||||
[TestCase(CompletionKind.BuiltInFunction, CompletionItemKind.Function)]
|
||||
[TestCase(CompletionKind.AggregateFunction, CompletionItemKind.Function)]
|
||||
[TestCase(CompletionKind.Unknown, CompletionItemKind.Keyword)]
|
||||
[TestCase(CompletionKind.Keyword, CompletionItemKind.Keyword)]
|
||||
[TestCase(CompletionKind.Punctuation, CompletionItemKind.Keyword)]
|
||||
[TestCase(CompletionKind.Identifier, CompletionItemKind.Keyword)]
|
||||
[TestCase(CompletionKind.Example, CompletionItemKind.Keyword)]
|
||||
[TestCase(CompletionKind.ScalarPrefix, CompletionItemKind.Keyword)]
|
||||
[TestCase(CompletionKind.TabularPrefix, CompletionItemKind.Keyword)]
|
||||
[TestCase(CompletionKind.TabularSuffix, CompletionItemKind.Keyword)]
|
||||
[TestCase(CompletionKind.QueryPrefix, CompletionItemKind.Keyword)]
|
||||
[TestCase(CompletionKind.CommandPrefix, CompletionItemKind.Keyword)]
|
||||
[TestCase(CompletionKind.ScalarInfix, CompletionItemKind.Keyword)]
|
||||
[TestCase(CompletionKind.RenderChart, CompletionItemKind.Keyword)]
|
||||
[TestCase(CompletionKind.Parameter, CompletionItemKind.Keyword)]
|
||||
[TestCase(CompletionKind.Cluster, CompletionItemKind.Keyword)]
|
||||
[TestCase(CompletionKind.MaterialiedView, CompletionItemKind.Keyword)]
|
||||
[TestCase(CompletionKind.ScalarType, CompletionItemKind.Keyword)]
|
||||
public void CreateCompletionItemKind_Returns_Kind(CompletionKind completionKind, CompletionItemKind expected)
|
||||
{
|
||||
var result = KustoIntellisenseHelper.CreateCompletionItemKind(completionKind);
|
||||
Assert.AreEqual(expected, result);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetDefaultKeywords_Returns_Keywords()
|
||||
{
|
||||
var textDocumentPosition = new TextDocumentPosition
|
||||
{
|
||||
Position = new Position()
|
||||
};
|
||||
var scriptFile = new ScriptFile("", "", "");
|
||||
var scriptParseInfo = new ScriptParseInfo();
|
||||
var documentInfo = new ScriptDocumentInfo(textDocumentPosition, scriptFile, scriptParseInfo);
|
||||
var position = new Position();
|
||||
|
||||
var completionItems = KustoIntellisenseHelper.GetDefaultKeywords(documentInfo, position);
|
||||
Assert.AreEqual(13, completionItems.Length);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetDefaultDiagnostics_Returns_Diagnostics()
|
||||
{
|
||||
var parseInfo = new ScriptParseInfo();
|
||||
var scriptFile = new ScriptFile("", "", "");
|
||||
var queryText = ".show databases";
|
||||
var completionItems = KustoIntellisenseHelper.GetDefaultDiagnostics(parseInfo, scriptFile, queryText);
|
||||
|
||||
Assert.AreEqual(6, completionItems.Length);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,188 @@
|
||||
using System.Collections.Concurrent;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Microsoft.Kusto.ServiceLayer.DataSource;
|
||||
using Microsoft.Kusto.ServiceLayer.DataSource.Metadata;
|
||||
using NUnit.Framework;
|
||||
|
||||
namespace Microsoft.Kusto.ServiceLayer.UnitTests.DataSource
|
||||
{
|
||||
public class KustoQueryUtilsTests
|
||||
{
|
||||
[TestCase("[@Table With Spaces", "[@Table With Spaces")]
|
||||
[TestCase("*", "*")]
|
||||
[TestCase("TableName", "TableName")]
|
||||
[TestCase("and", "[@\"and\"]")]
|
||||
[TestCase("anomalychart", "[@\"anomalychart\"]")]
|
||||
[TestCase("areachart", "[@\"areachart\"]")]
|
||||
[TestCase("asc", "[@\"asc\"]")]
|
||||
[TestCase("barchart", "[@\"barchart\"]")]
|
||||
[TestCase("between", "[@\"between\"]")]
|
||||
[TestCase("bool", "[@\"bool\"]")]
|
||||
[TestCase("boolean", "[@\"boolean\"]")]
|
||||
[TestCase("by", "[@\"by\"]")]
|
||||
[TestCase("columnchart", "[@\"columnchart\"]")]
|
||||
[TestCase("consume", "[@\"consume\"]")]
|
||||
[TestCase("contains", "[@\"contains\"]")]
|
||||
[TestCase("containscs", "[@\"containscs\"]")]
|
||||
[TestCase("count", "[@\"count\"]")]
|
||||
[TestCase("date", "[@\"date\"]")]
|
||||
[TestCase("datetime", "[@\"datetime\"]")]
|
||||
[TestCase("default", "[@\"default\"]")]
|
||||
[TestCase("desc", "[@\"desc\"]")]
|
||||
[TestCase("distinct", "[@\"distinct\"]")]
|
||||
[TestCase("double", "[@\"double\"]")]
|
||||
[TestCase("dynamic", "[@\"dynamic\"]")]
|
||||
[TestCase("endswith", "[@\"endswith\"]")]
|
||||
[TestCase("evaluate", "[@\"evaluate\"]")]
|
||||
[TestCase("extend", "[@\"extend\"]")]
|
||||
[TestCase("false", "[@\"false\"]")]
|
||||
[TestCase("filter", "[@\"filter\"]")]
|
||||
[TestCase("find", "[@\"find\"]")]
|
||||
[TestCase("first", "[@\"first\"]")]
|
||||
[TestCase("flags", "[@\"flags\"]")]
|
||||
[TestCase("float", "[@\"float\"]")]
|
||||
[TestCase("getschema", "[@\"getschema\"]")]
|
||||
[TestCase("has", "[@\"has\"]")]
|
||||
[TestCase("hasprefix", "[@\"hasprefix\"]")]
|
||||
[TestCase("hassuffix", "[@\"hassuffix\"]")]
|
||||
[TestCase("in", "[@\"in\"]")]
|
||||
[TestCase("int", "[@\"int\"]")]
|
||||
[TestCase("join", "[@\"join\"]")]
|
||||
[TestCase("journal", "[@\"journal\"]")]
|
||||
[TestCase("kind", "[@\"kind\"]")]
|
||||
[TestCase("ladderchart", "[@\"ladderchart\"]")]
|
||||
[TestCase("last", "[@\"last\"]")]
|
||||
[TestCase("like", "[@\"like\"]")]
|
||||
[TestCase("limit", "[@\"limit\"]")]
|
||||
[TestCase("linechart", "[@\"linechart\"]")]
|
||||
[TestCase("long", "[@\"long\"]")]
|
||||
[TestCase("materialize", "[@\"materialize\"]")]
|
||||
[TestCase("mvexpand", "[@\"mvexpand\"]")]
|
||||
[TestCase("notcontains", "[@\"notcontains\"]")]
|
||||
[TestCase("notlike", "[@\"notlike\"]")]
|
||||
[TestCase("of", "[@\"of\"]")]
|
||||
[TestCase("or", "[@\"or\"]")]
|
||||
[TestCase("order", "[@\"order\"]")]
|
||||
[TestCase("parse", "[@\"parse\"]")]
|
||||
[TestCase("piechart", "[@\"piechart\"]")]
|
||||
[TestCase("pivotchart", "[@\"pivotchart\"]")]
|
||||
[TestCase("print", "[@\"print\"]")]
|
||||
[TestCase("project", "[@\"project\"]")]
|
||||
[TestCase("queries", "[@\"queries\"]")]
|
||||
[TestCase("real", "[@\"real\"]")]
|
||||
[TestCase("regex", "[@\"regex\"]")]
|
||||
[TestCase("sample", "[@\"sample\"]")]
|
||||
[TestCase("scatterchart", "[@\"scatterchart\"]")]
|
||||
[TestCase("search", "[@\"search\"]")]
|
||||
[TestCase("set", "[@\"set\"]")]
|
||||
[TestCase("sort", "[@\"sort\"]")]
|
||||
[TestCase("stacked", "[@\"stacked\"]")]
|
||||
[TestCase("stacked100", "[@\"stacked100\"]")]
|
||||
[TestCase("stackedareachart", "[@\"stackedareachart\"]")]
|
||||
[TestCase("startswith", "[@\"startswith\"]")]
|
||||
[TestCase("string", "[@\"string\"]")]
|
||||
[TestCase("summarize", "[@\"summarize\"]")]
|
||||
[TestCase("take", "[@\"take\"]")]
|
||||
[TestCase("time", "[@\"time\"]")]
|
||||
[TestCase("timechart", "[@\"timechart\"]")]
|
||||
[TestCase("timeline", "[@\"timeline\"]")]
|
||||
[TestCase("timepivot", "[@\"timepivot\"]")]
|
||||
[TestCase("timespan", "[@\"timespan\"]")]
|
||||
[TestCase("to", "[@\"to\"]")]
|
||||
[TestCase("top", "[@\"top\"]")]
|
||||
[TestCase("toscalar", "[@\"toscalar\"]")]
|
||||
[TestCase("true", "[@\"true\"]")]
|
||||
[TestCase("union", "[@\"union\"]")]
|
||||
[TestCase("unstacked", "[@\"unstacked\"]")]
|
||||
[TestCase("viewers", "[@\"viewers\"]")]
|
||||
[TestCase("where", "[@\"where\"]")]
|
||||
[TestCase("withsource", "[@\"withsource\"]")]
|
||||
public void EscapeName_Returns_Name(string name, string expected)
|
||||
{
|
||||
var result = KustoQueryUtils.EscapeName(name);
|
||||
Assert.AreEqual(expected, result);
|
||||
}
|
||||
|
||||
[TestCase(".show databases", true)]
|
||||
[TestCase(".show schema", true)]
|
||||
[TestCase(".show tables", false)]
|
||||
public void IsClusterLevelQuery_Returns_Result(string query, bool expected)
|
||||
{
|
||||
var result = KustoQueryUtils.IsClusterLevelQuery(query);
|
||||
Assert.AreEqual(expected, result);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void SafeAdd_AddsRecord_ExistingKey()
|
||||
{
|
||||
string key = "FolderName";
|
||||
|
||||
var dictionary = new Dictionary<string, Dictionary<string, DataSourceObjectMetadata>>
|
||||
{
|
||||
[key] = new Dictionary<string, DataSourceObjectMetadata>()
|
||||
};
|
||||
var existingRecord = new DataSourceObjectMetadata
|
||||
{
|
||||
Name = "Folder 1"
|
||||
};
|
||||
dictionary[key].Add(key, existingRecord);
|
||||
|
||||
var newRecord = new DataSourceObjectMetadata
|
||||
{
|
||||
Name = "Folder 2"
|
||||
};
|
||||
dictionary.SafeAdd(key, newRecord);
|
||||
|
||||
Assert.AreEqual(2, dictionary[key].Count);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void SafeAdd_AddsRecord_NewKey()
|
||||
{
|
||||
string key = "FolderName";
|
||||
|
||||
var dictionary = new Dictionary<string, Dictionary<string, DataSourceObjectMetadata>>
|
||||
{
|
||||
[key] = new Dictionary<string, DataSourceObjectMetadata>()
|
||||
};
|
||||
|
||||
var newRecord = new DataSourceObjectMetadata
|
||||
{
|
||||
Name = "Folder 2"
|
||||
};
|
||||
dictionary.SafeAdd(key, newRecord);
|
||||
|
||||
Assert.AreEqual(1, dictionary[key].Count);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void AddRange_Keeps_Existing_Records_And_Order()
|
||||
{
|
||||
var key = "DatabaseName";
|
||||
|
||||
var existingObjectMetadata = new DataSourceObjectMetadata
|
||||
{
|
||||
PrettyName = "Ball Table"
|
||||
};
|
||||
|
||||
var dictionary = new ConcurrentDictionary<string, IEnumerable<DataSourceObjectMetadata>>
|
||||
{
|
||||
[key] = new List<DataSourceObjectMetadata> {existingObjectMetadata}
|
||||
};
|
||||
|
||||
var newMetadata = new DataSourceObjectMetadata
|
||||
{
|
||||
PrettyName = "Apple Table"
|
||||
};
|
||||
|
||||
dictionary.AddRange(key, new List<DataSourceObjectMetadata> {newMetadata});
|
||||
|
||||
Assert.AreEqual(2, dictionary[key].Count());
|
||||
|
||||
// ensure order by clause
|
||||
Assert.AreEqual(newMetadata.PrettyName, dictionary[key].First().PrettyName);
|
||||
Assert.AreEqual(existingObjectMetadata.PrettyName, dictionary[key].Last().PrettyName);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
using Microsoft.Kusto.ServiceLayer.Connection;
|
||||
using Microsoft.Kusto.ServiceLayer.Connection.Contracts;
|
||||
using Microsoft.Kusto.ServiceLayer.Metadata;
|
||||
using Microsoft.Kusto.ServiceLayer.Metadata.Contracts;
|
||||
using Microsoft.SqlTools.Hosting.Protocol;
|
||||
using Moq;
|
||||
using NUnit.Framework;
|
||||
|
||||
namespace Microsoft.Kusto.ServiceLayer.UnitTests.Metadata
|
||||
{
|
||||
public class MetadataServiceTests
|
||||
{
|
||||
[Test]
|
||||
public void HandleMetadataListRequest_Sets_MetadataListTask()
|
||||
{
|
||||
var serviceHostMock = new Mock<IProtocolEndpoint>();
|
||||
var connectionServiceMock = new Mock<ConnectionService>();
|
||||
var connectionFactoryMock = new Mock<IDataSourceConnectionFactory>();
|
||||
|
||||
var connectionInfo = new ConnectionInfo(connectionFactoryMock.Object, "", new ConnectionDetails());
|
||||
connectionServiceMock.Setup(x => x.TryFindConnection(It.IsAny<string>(), out connectionInfo));
|
||||
|
||||
var metadataService = new MetadataService();
|
||||
metadataService.InitializeService(serviceHostMock.Object, connectionServiceMock.Object);
|
||||
|
||||
Assert.IsNull(metadataService.MetadataListTask);
|
||||
|
||||
var task = metadataService.HandleMetadataListRequest(new MetadataQueryParams(),
|
||||
new RequestContext<MetadataQueryResult>());
|
||||
task.Wait();
|
||||
|
||||
Assert.IsNotNull(metadataService.MetadataListTask);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
using Microsoft.Kusto.ServiceLayer.DataSource;
|
||||
using Microsoft.Kusto.ServiceLayer.Scripting;
|
||||
using Microsoft.Kusto.ServiceLayer.Scripting.Contracts;
|
||||
using Microsoft.SqlServer.Management.Sdk.Sfc;
|
||||
using Moq;
|
||||
using NUnit.Framework;
|
||||
|
||||
namespace Microsoft.Kusto.ServiceLayer.UnitTests.Scripting
|
||||
{
|
||||
public class ScripterTests
|
||||
{
|
||||
[Test]
|
||||
public void SelectFromTableOrView_Returns_SelectQuery()
|
||||
{
|
||||
var mockDataSource = new Mock<IDataSource>();
|
||||
var urn = new Urn(@"Server[@Name = 'SERVER']/Database[@Name = 'quoted''db']/Table[@Name = 'quoted''Name' and @Schema = 'quoted''Schema']");
|
||||
var scripter = new Scripter();
|
||||
var result = scripter.SelectFromTableOrView(mockDataSource.Object, urn);
|
||||
|
||||
Assert.AreEqual("[@\"quoted'Name\"]\n | limit 1000", result);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void AlterFunction()
|
||||
{
|
||||
var expected = "AlterScript";
|
||||
var mockDataSource = new Mock<IDataSource>();
|
||||
mockDataSource.Setup(x => x.GenerateAlterFunctionScript(It.IsAny<string>())).Returns(expected);
|
||||
|
||||
var scriptingObject = new ScriptingObject
|
||||
{
|
||||
Name = "Name(a:int, b: int)"
|
||||
};
|
||||
var scripter = new Scripter();
|
||||
var result = scripter.AlterFunction(mockDataSource.Object, scriptingObject);
|
||||
|
||||
Assert.AreEqual(expected, result);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ExecuteFunction()
|
||||
{
|
||||
var expected = "ExecuteScript";
|
||||
var mockDataSource = new Mock<IDataSource>();
|
||||
mockDataSource.Setup(x => x.GenerateExecuteFunctionScript(It.IsAny<string>())).Returns(expected);
|
||||
|
||||
var scriptingObject = new ScriptingObject
|
||||
{
|
||||
Name = "Name(a:int, b: int)"
|
||||
};
|
||||
var scripter = new Scripter();
|
||||
var result = scripter.ExecuteFunction(mockDataSource.Object, scriptingObject);
|
||||
|
||||
Assert.AreEqual(expected, result);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user