3278 Kusto Unit Tests - Part 1 (#1057)

* 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

* adressing feedback

* Correcting path in csproj

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-08-31 11:11:12 -07:00
committed by GitHub
parent 07700560a6
commit 1577177153
26 changed files with 565 additions and 379 deletions

View File

@@ -0,0 +1,91 @@
using System;
using System.Collections.Generic;
using Microsoft.Kusto.ServiceLayer.DataSource;
using Microsoft.Kusto.ServiceLayer.DataSource.DataSourceIntellisense;
using Microsoft.Kusto.ServiceLayer.LanguageServices.Completion;
using Microsoft.Kusto.ServiceLayer.Workspace.Contracts;
using NUnit.Framework;
namespace Microsoft.Kusto.ServiceLayer.UnitTests.DataSource
{
public class DataSourceFactoryTests
{
[TestCase(typeof(ArgumentNullException), "", "AzureAccountToken")]
[TestCase(typeof(ArgumentNullException), "ConnectionString", "")]
[TestCase(typeof(ArgumentException), "ConnectionString", "AzureAccountToken")]
public void Create_Throws_Exceptions_For_InvalidParams(Type exceptionType,
string connectionString,
string azureAccountToken)
{
var dataSourceFactory = new DataSourceFactory();
Assert.Throws(exceptionType,
() => dataSourceFactory.Create(DataSourceType.None, connectionString, azureAccountToken));
}
[Test]
public void GetDefaultAutoComplete_Throws_ArgumentException_For_InvalidDataSourceType()
{
Assert.Throws<ArgumentException>(() =>
DataSourceFactory.GetDefaultAutoComplete(DataSourceType.None, null, null));
}
[Test]
public void GetDefaultAutoComplete_Returns_CompletionItems()
{
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 = DataSourceFactory.GetDefaultAutoComplete(DataSourceType.Kusto, documentInfo, position);
Assert.AreNotEqual(0, completionItems.Length);
}
[Test]
public void GetDefaultSemanticMarkers_Throws_ArgumentException_For_InvalidDataSourceType()
{
Assert.Throws<ArgumentException>(() =>
DataSourceFactory.GetDefaultSemanticMarkers(DataSourceType.None, null, null, null));
}
[Test]
public void GetDefaultSemanticMarkers_Returns_ScriptFileMarker()
{
var parseInfo = new ScriptParseInfo();
var file = new ScriptFile("", "", "");
var queryText = ".show databases";
var semanticMarkers = DataSourceFactory.GetDefaultSemanticMarkers(DataSourceType.Kusto, parseInfo, file, queryText);
Assert.AreNotEqual(0, semanticMarkers.Length);
}
[Test]
public void ConvertToServerInfoFormat_Throws_ArgumentException_For_InvalidDataSourceType()
{
Assert.Throws<ArgumentException>(() =>
DataSourceFactory.ConvertToServerInfoFormat(DataSourceType.None, null));
}
[Test]
public void ConvertToServerInfoFormat_Returns_ServerInfo_With_Options()
{
var diagnosticsInfo = new DiagnosticsInfo
{
Options = new Dictionary<string, object>
{
{"Key", "Object"}
}
};
var serverInfo = DataSourceFactory.ConvertToServerInfoFormat(DataSourceType.Kusto, diagnosticsInfo);
Assert.IsNotNull(serverInfo.Options);
Assert.AreEqual(diagnosticsInfo.Options["Key"], serverInfo.Options["Key"]);
}
}
}

View File

@@ -0,0 +1,164 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Kusto.ServiceLayer.DataSource.Metadata;
using Microsoft.Kusto.ServiceLayer.Metadata.Contracts;
using NUnit.Framework;
namespace Microsoft.Kusto.ServiceLayer.UnitTests.DataSource.Metadata
{
public class MetadataFactoryTests
{
[Test]
public void CreateClusterMetadata_ThrowsNullException_For_NullClusterName()
{
Assert.Throws<ArgumentNullException>(() => MetadataFactory.CreateClusterMetadata(null));
}
[Test]
[TestCase("")]
[TestCase(null)]
[TestCase(" ")]
public void CreateDatabaseMetadata_ThrowsNullException_For_InvalidDatabaseName(string databaseName)
{
var testMetadata = new DataSourceObjectMetadata
{
MetadataType = DataSourceMetadataType.Cluster
};
Assert.Throws<ArgumentNullException>(() => MetadataFactory.CreateDatabaseMetadata(testMetadata, databaseName));
}
[Test]
public void CreateDatabaseMetadata_ThrowsNullException_For_InvalidMetadataType()
{
var testMetadata = new DataSourceObjectMetadata
{
MetadataType = DataSourceMetadataType.Database
};
Assert.Throws<ArgumentException>(() => MetadataFactory.CreateDatabaseMetadata(testMetadata, "FakeDatabaseName"));
}
[Test]
public void CreateFolderMetadata_ThrowsNullException_For_NullMetadata()
{
Assert.Throws<InvalidOperationException>(() => MetadataFactory.CreateFolderMetadata(null, "", ""));
}
[Test]
public void CreateClusterMetadata_Returns_DataSourceObjectMetadata()
{
string clusterName = "FakeClusterName";
var objectMetadata = MetadataFactory.CreateClusterMetadata(clusterName);
Assert.AreEqual(DataSourceMetadataType.Cluster, objectMetadata.MetadataType);
Assert.AreEqual(DataSourceMetadataType.Cluster.ToString(), objectMetadata.MetadataTypeName);
Assert.AreEqual(clusterName, objectMetadata.Name);
Assert.AreEqual(clusterName, objectMetadata.PrettyName);
Assert.AreEqual(clusterName, objectMetadata.Urn);
}
[Test]
public void CreateDatabaseMetadata_Returns_DataSourceObjectMetadata()
{
string databaseName = "FakeDatabaseName";
var clusterMetadata = new DataSourceObjectMetadata
{
MetadataType = DataSourceMetadataType.Cluster,
Name = "FakeClusterName",
Urn = "FakeClusterName"
};
var objectMetadata = MetadataFactory.CreateDatabaseMetadata(clusterMetadata, databaseName);
Assert.AreEqual(DataSourceMetadataType.Database, objectMetadata.MetadataType);
Assert.AreEqual(DataSourceMetadataType.Database.ToString(), objectMetadata.MetadataTypeName);
Assert.AreEqual(databaseName, objectMetadata.Name);
Assert.AreEqual(databaseName, objectMetadata.PrettyName);
Assert.AreEqual($"{clusterMetadata.Urn}.{databaseName}", objectMetadata.Urn);
}
[Test]
public void CreateFolderMetadata_Returns_FolderMetadata()
{
string path = "FakeCluster.FakeDatabase.FakeFolder";
string name = "FakeFolderName";
var parentMetadata = new DataSourceObjectMetadata();
var objectMetadata = MetadataFactory.CreateFolderMetadata(parentMetadata, path, name);
Assert.AreEqual(DataSourceMetadataType.Folder, objectMetadata.MetadataType);
Assert.AreEqual(DataSourceMetadataType.Folder.ToString(), objectMetadata.MetadataTypeName);
Assert.AreEqual(name, objectMetadata.Name);
Assert.AreEqual(name, objectMetadata.PrettyName);
Assert.AreEqual($"{path}.{name}", objectMetadata.Urn);
Assert.AreEqual(parentMetadata, objectMetadata.ParentMetadata);
}
[Test]
public void ConvertToDatabaseInfo_Returns_EmptyList_For_NonDatabaseMetadata()
{
var inputList = new List<DataSourceObjectMetadata>
{
new DataSourceObjectMetadata
{
Name = "FakeClusterName"
}
};
var databaseInfos = MetadataFactory.ConvertToDatabaseInfo(inputList);
Assert.AreEqual(0, databaseInfos.Count);
}
[Test]
public void ConvertToDatabaseInfo_Returns_DatabaseInfoList()
{
var databaseMetadata = new DatabaseMetadata
{
Name = "FakeDatabaseName",
SizeInMB = "2097152" // stored in bytes
};
var inputList = new List<DatabaseMetadata>
{
databaseMetadata
};
var databaseInfos = MetadataFactory.ConvertToDatabaseInfo(inputList);
Assert.AreEqual(1, databaseInfos.Count);
var databaseInfo = databaseInfos.Single();
Assert.AreEqual(databaseMetadata.Name, databaseInfo.Options["name"]);
Assert.AreEqual("2", databaseInfo.Options["sizeInMB"]);
}
[Test]
public void ConvertToObjectMetadata_Returns_ListObjectMetadata()
{
var databaseMetadata = new DataSourceObjectMetadata
{
PrettyName = "FakeDatabaseName",
MetadataTypeName = "Table"
};
var inputList = new List<DataSourceObjectMetadata>
{
databaseMetadata
};
var objectMetadatas = MetadataFactory.ConvertToObjectMetadata(inputList);
Assert.AreEqual(1, objectMetadatas.Count);
var objectMetadata = objectMetadatas.Single();
Assert.AreEqual(databaseMetadata.PrettyName, objectMetadata.Name);
Assert.AreEqual(databaseMetadata.MetadataTypeName, objectMetadata.MetadataTypeName);
Assert.AreEqual(MetadataType.Table, objectMetadata.MetadataType);
}
}
}