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,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);
}
}
}

View File

@@ -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);
}
}
}