Kusto Auth Refactor Tests (#1148)

* Refactored Kusto.ServiceLayer to pass ConnectionDetails to DataSourceFactory instead of connection string. Created KustoConnectionDetails to map needed details to KustoClient.

* Removed unused ScriptingScriptOperation from KustoServiceLayer.

* Created DstsAuthenticationManager and moved logic for getting DstsToken. Updated error message for failing to create KustoConnection.

* Removed DstsAuthenticationManager.cs. Refactored DataSourceFactory to retrieve UserToken from ConnectionDetails.

* Renamed AzureAccountToken in ConnectionDetails to AccountToken. Changed mapping to KustoConnectionDetails based on the AccountToken.

* Removed Kusto.Data reference from ConnectionService and ScriptingListObjectsOperation. Moved creation of KustoConnectionStringBuilder to DataSourceFactory

* Added accountToken validation to DataSourceFactory Create.

* Renamed KustoConnectionDetails to DataSourceConnectionDetails. Renamed AzureToken to AuthToken.

* Refactored SchemaState and intellisense out of KustoClient to KustoIntellisenseClient. Added IIntellisenseClient. Added unit tests for KustoIntellisenseClient and KustoClient.

* Removed unused property dataSourceFactory from LanguageService > InitializeService. Moved KustoIntellisense functions from KustoIntellisenseHelper to KustoIntellisenseClient and made SchemaState private. Added IIntellisenseClient to IDataSource.

* Renamed directory from DataSourceIntellisense to Intellisense and updated namespace. Fixed namespace in ScriptDocumentInfo.
This commit is contained in:
Justin M
2021-01-29 11:25:23 -08:00
committed by GitHub
parent df595ab425
commit 6f22dcf241
20 changed files with 651 additions and 395 deletions

View File

@@ -2,8 +2,8 @@ using System;
using System.Collections.Generic;
using Microsoft.Kusto.ServiceLayer.Connection.Contracts;
using Microsoft.Kusto.ServiceLayer.DataSource;
using Microsoft.Kusto.ServiceLayer.DataSource.DataSourceIntellisense;
using Microsoft.Kusto.ServiceLayer.LanguageServices.Completion;
using Microsoft.Kusto.ServiceLayer.DataSource.Intellisense;
using Microsoft.Kusto.ServiceLayer.LanguageServices;
using Microsoft.Kusto.ServiceLayer.Workspace.Contracts;
using NUnit.Framework;

View File

@@ -0,0 +1,174 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Kusto.ServiceLayer.DataSource;
using Microsoft.Kusto.ServiceLayer.DataSource.Intellisense;
using Microsoft.Kusto.ServiceLayer.LanguageServices;
using Microsoft.Kusto.ServiceLayer.Workspace.Contracts;
using Moq;
using NUnit.Framework;
namespace Microsoft.Kusto.ServiceLayer.UnitTests.DataSource.DataSourceIntellisense
{
public class KustoIntellisenseClientTests
{
private Mock<IKustoClient> GetMockKustoClient()
{
var kustoClientMock = new Mock<IKustoClient>();
kustoClientMock.Setup(x => x.ClusterName).Returns("https://fake.url.com");
kustoClientMock.Setup(x => x.DatabaseName).Returns("FakeDatabaseName");
var databaseSchema = new ShowDatabaseSchemaResult
{
DatabaseName = "FakeDatabaseName",
TableName = "FakeTableName",
ColumnName = "FakeColumnName",
ColumnType = "bool",
IsDefaultTable = false,
IsDefaultColumn = false,
PrettyName = "Fake Table Name",
Version = "",
Folder = "FakeTableFolder",
DocName = ""
};
var databaseSchemaResults = new List<ShowDatabaseSchemaResult> {databaseSchema} as IEnumerable<ShowDatabaseSchemaResult>;
kustoClientMock.Setup(x => x.ExecuteQueryAsync<ShowDatabaseSchemaResult>(It.IsAny<string>(), It.IsAny<CancellationToken>(), "FakeDatabaseName"))
.Returns(Task.FromResult(databaseSchemaResults));
var functionSchema = new ShowFunctionsResult
{
Name = "FakeFunctionName",
Parameters = "a:real, b:real",
Body = "a+b",
Folder = "FakeFunctionFolder",
DocString = ""
};
var functionSchemaResults = new List<ShowFunctionsResult> {functionSchema} as IEnumerable<ShowFunctionsResult>;
kustoClientMock.Setup(x => x.ExecuteQueryAsync<ShowFunctionsResult>(It.IsAny<string>(), It.IsAny<CancellationToken>(), "FakeDatabaseName"))
.Returns(Task.FromResult(functionSchemaResults));
return kustoClientMock;
}
[Test]
public void GetSemanticMarkers_Returns_Error_For_InvalidText()
{
var kustoClientMock = GetMockKustoClient();
var client = new KustoIntellisenseClient(kustoClientMock.Object);
var semanticMarkers = client.GetSemanticMarkers(new ScriptParseInfo(), new ScriptFile("", "", ""), "InvalidText");
var semanticMarker = semanticMarkers.Single();
Assert.AreEqual(ScriptFileMarkerLevel.Error, semanticMarker.Level);
Assert.AreEqual("The name 'InvalidText' does not refer to any known column, table, variable or function.", semanticMarker.Message);
Assert.IsNotNull(semanticMarker.ScriptRegion);
}
[Test]
public void GetSemanticMarkers_Returns_Zero_SemanticMarkers_For_ValidQueryText()
{
var kustoClientMock = GetMockKustoClient();
var client = new KustoIntellisenseClient(kustoClientMock.Object);
var queryText = @".show commands";
var semanticMarkers = client.GetSemanticMarkers(new ScriptParseInfo(), new ScriptFile("", "", ""), queryText);
Assert.AreEqual(0, semanticMarkers.Length);
}
[Test]
public void GetDefinition_Returns_Null()
{
var kustoClientMock = GetMockKustoClient();
var client = new KustoIntellisenseClient(kustoClientMock.Object);
var definition = client.GetDefinition("queryText", 0, 1, 1);
// finish these assertions once the function is implemented
Assert.IsNull(definition);
}
[Test]
public void GetHoverHelp_Returns_Hover()
{
var kustoClientMock = GetMockKustoClient();
var client = new KustoIntellisenseClient(kustoClientMock.Object);
var textDocumentPosition = new TextDocumentPosition
{
Position = new Position()
};
var scriptFile = new ScriptFile("", "", "");
var scriptParseInfo = new ScriptParseInfo();
var documentInfo = new ScriptDocumentInfo(textDocumentPosition, scriptFile, scriptParseInfo);
var hover = client.GetHoverHelp(documentInfo, new Position());
Assert.IsNotNull(hover);
}
[Test]
public void GetAutoCompleteSuggestions_Returns_CompletionItems()
{
var kustoClientMock = GetMockKustoClient();
var client = new KustoIntellisenseClient(kustoClientMock.Object);
var position = new Position();
var textDocumentPosition = new TextDocumentPosition
{
Position = position
};
var scriptFile = new ScriptFile("", "", "");
var scriptParseInfo = new ScriptParseInfo();
var documentInfo = new ScriptDocumentInfo(textDocumentPosition, scriptFile, scriptParseInfo);
var items = client.GetAutoCompleteSuggestions(documentInfo, position);
Assert.AreEqual(20, items.Length);
}
[Test]
public void UpdateDatabase_Updates_SchemaState()
{
var kustoClientMock = GetMockKustoClient();
var databaseSchema = new ShowDatabaseSchemaResult
{
DatabaseName = "NewDatabaseName",
TableName = "NewTableName",
ColumnName = "NewColumnName",
ColumnType = "bool",
IsDefaultTable = false,
IsDefaultColumn = false,
PrettyName = "New Table Name",
Version = "",
Folder = "NewTableFolder",
DocName = ""
};
var databaseSchemaResults = new List<ShowDatabaseSchemaResult> {databaseSchema} as IEnumerable<ShowDatabaseSchemaResult>;
kustoClientMock.Setup(x => x.ExecuteQueryAsync<ShowDatabaseSchemaResult>(It.IsAny<string>(), It.IsAny<CancellationToken>(), "NewDatabaseName"))
.Returns(Task.FromResult(databaseSchemaResults));
var client = new KustoIntellisenseClient(kustoClientMock.Object);
client.UpdateDatabase("NewDatabaseName");
var position = new Position();
var textDocumentPosition = new TextDocumentPosition
{
Position = position
};
var scriptFile = new ScriptFile("", "", "");
var scriptParseInfo = new ScriptParseInfo();
var documentInfo = new ScriptDocumentInfo(textDocumentPosition, scriptFile, scriptParseInfo);
var items = client.GetAutoCompleteSuggestions(documentInfo, position);
Assert.AreEqual(19, items.Length);
var tableItem = items.FirstOrDefault(x => x.Detail == "Table");
// assert new table is being returned to show database has changed
Assert.IsNotNull(tableItem);
Assert.AreEqual("NewTableName", tableItem.InsertText);
Assert.AreEqual("NewTableName", tableItem.Label);
}
}
}

View File

@@ -1,8 +1,5 @@
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.DataSource.Intellisense;
using Microsoft.Kusto.ServiceLayer.LanguageServices;
using Microsoft.Kusto.ServiceLayer.Workspace.Contracts;
using NUnit.Framework;
@@ -10,37 +7,6 @@ namespace Microsoft.Kusto.ServiceLayer.UnitTests.DataSource.DataSourceIntellisen
{
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()
{

View File

@@ -0,0 +1,58 @@
using System;
using Microsoft.Kusto.ServiceLayer.DataSource;
using Microsoft.Kusto.ServiceLayer.DataSource.Contracts;
using NUnit.Framework;
namespace Microsoft.Kusto.ServiceLayer.UnitTests.DataSource
{
public class KustoClientTests
{
[Test]
public void Constructor_Throws_ArgumentException_For_MissingToken()
{
var connectionDetails = new DataSourceConnectionDetails
{
UserToken = ""
};
Assert.Throws<ArgumentException>(() => new KustoClient(connectionDetails, "ownerUri"));
}
[Test]
public void Constructor_Sets_ClusterName_With_DefaultDatabaseName()
{
string clusterName = "https://fake.url.com";
var connectionDetails = new DataSourceConnectionDetails
{
UserToken = "UserToken",
ServerName = clusterName,
DatabaseName = "",
AuthenticationType = "AzureMFA"
};
var client = new KustoClient(connectionDetails, "ownerUri");
Assert.AreEqual(clusterName, client.ClusterName);
Assert.AreEqual("NetDefaultDB", client.DatabaseName);
}
[TestCase("dstsAuth")]
[TestCase("AzureMFA")]
public void Constructor_Creates_Client_With_Valid_AuthenticationType(string authenticationType)
{
string clusterName = "https://fake.url.com";
var connectionDetails = new DataSourceConnectionDetails
{
UserToken = "UserToken",
ServerName = clusterName,
DatabaseName = "FakeDatabaseName",
AuthenticationType = authenticationType
};
var client = new KustoClient(connectionDetails, "ownerUri");
Assert.AreEqual(clusterName, client.ClusterName);
Assert.AreEqual("FakeDatabaseName", client.DatabaseName);
}
}
}