Files
sqltoolsservice/test/Microsoft.SqlTools.ServiceLayer.UnitTests/ObjectExplorer/SmoQueryModelTests.cs
Aasim Khan 73c2a75fba Creating a new Sql Core project that stores OE classes. (#2165)
* init

* More fixes

* moving filters from contracts to core OE classes

* Fixing some tests

* More fixes and added doc comments

* Fixing tests

* Quick refactoring

* more cleanups

* cleanup

* Adding stateless OE

* Adding null checks

* Making group by schema independent of settings

* Fixing tests

* Removing node info from core oe code

* Fixing tests and moving OE code to its own project

* moving oe to own project

* Removing changes to Kusto

* Removing azure access token from service layer

* Fixing project description and title

* Fixing file name typo

* Removing unused  strings from service layer

* Fixing localized strings in tests
Adding comments to stateless OE

* Fixing stuff

* Update src/Microsoft.SqlTools.SqlCore/Microsoft.SqlTools.SqlCore.csproj

Co-authored-by: Charles Gagnon <chgagnon@microsoft.com>

* Fixing project nesting

* Fixing more stuff and removing OE class

* Cleanup

* Code cleanup

* fixing oe service provider

* Fixing test name

* Remove using

* Update src/Microsoft.SqlTools.SqlCore/ObjectExplorer/SmoModel/SmoQueryContext.cs

Co-authored-by: Charles Gagnon <chgagnon@microsoft.com>

* Fixing syntax error

* Adding project to locproject

* Fixing stuff

* Fixing errors

* sorting usings

---------

Co-authored-by: Charles Gagnon <chgagnon@microsoft.com>
2023-08-16 22:11:35 -07:00

129 lines
4.8 KiB
C#

//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//
#nullable disable
using System;
using System.IO;
using System.Linq;
using Microsoft.SqlServer.Management.Smo;
using Microsoft.SqlTools.Extensibility;
using Microsoft.SqlTools.SqlCore.ObjectExplorer;
using Microsoft.SqlTools.SqlCore.ObjectExplorer.SmoModel;
using Microsoft.SqlTools.SqlCore.ObjectExplorer.Nodes;
using NUnit.Framework;
namespace Microsoft.SqlTools.ServiceLayer.UnitTests.ObjectExplorer
{
public class SmoQueryModelTests
{
IMultiServiceProvider serviceProvider;
[SetUp]
public void Setup()
{
// Given the extension type loader is set to find SmoCollectionQuerier objects
var assembly = typeof(SmoQuerier).Assembly;
serviceProvider = ExtensionServiceProvider.CreateFromAssembliesInDirectory(Path.GetDirectoryName(assembly.Location), new string[] { Path.GetFileName(assembly.Location) });
}
[Test]
public void ShouldFindDatabaseQuerierFromRealPath()
{
// When I request a database compatible querier
SmoQuerier querier = serviceProvider.GetService<SmoQuerier>(q => q.SupportedObjectTypes.Contains(typeof(Database)));
// Then I expect to get back the SqlDatabaseQuerier
Assert.NotNull(querier);
Assert.AreEqual(typeof(SqlDatabaseQuerier), querier.GetType());
// And I expect the service provider to have been set by the extension code
Assert.NotNull(querier.ServiceProvider);
}
[Test]
public void ShouldFindQuerierIfInExtensionList()
{
VerifyQuerierLookup(typeof(Table), typeof(SqlTableQuerier), expectExists: true);
}
[Test]
public void ShouldNotFindQuerierIfNotInExtensionList()
{
VerifyQuerierLookup(typeof(Database), null, expectExists: false);
}
[Test]
public void SqlServerDdlTriggerQuerierShouldNotBeAvailableForSqlDw()
{
SmoQuerier querier = GetSmoQuerier(typeof(ServerDdlTrigger));
Assert.False(querier.ValidFor.HasFlag(ValidForFlag.SqlDw));
}
[Test]
public void SqlSynonymQuerierShouldNotBeAvailableForSqlDw()
{
SmoQuerier querier = GetSmoQuerier(typeof(Synonym));
Assert.False(querier.ValidFor.HasFlag(ValidForFlag.SqlDw));
}
[Test]
public void SqlTriggerQuerierShouldNotBeAvailableForSqlDw()
{
SmoQuerier querier = GetSmoQuerier(typeof(Trigger));
Assert.False(querier.ValidFor.HasFlag(ValidForFlag.SqlDw));
}
[Test]
public void SqlFullTextIndexQuerierShouldNotBeAvailableForSqlDw()
{
SmoQuerier querier = GetSmoQuerier(typeof(FullTextIndex));
Assert.False(querier.ValidFor.HasFlag(ValidForFlag.SqlDw));
}
[Test]
public void TableValuedFunctionsIncludeInlineFunctions()
{
var tableFactory = new TableValuedFunctionsChildFactory();
var filters = tableFactory.Filters;
Assert.True(filters.ToList().Any(filter => {
var f = filter as NodePropertyFilter;
return f.Values.Contains(UserDefinedFunctionType.Table) && f.Values.Contains(UserDefinedFunctionType.Inline);
}));
}
private SmoQuerier GetSmoQuerier(Type querierType)
{
// When I request a compatible querier
SmoQuerier querier = serviceProvider.GetService<SmoQuerier>(q => q.SupportedObjectTypes.Contains(querierType));
// Then I expect to get back the Querier
Assert.NotNull(querier);
// And I expect the service provider to have been set by the extension code
Assert.NotNull(querier.ServiceProvider);
return querier;
}
private static void VerifyQuerierLookup(Type smoType, Type querierType, bool expectExists)
{
ExtensionServiceProvider serviceProvider = ExtensionServiceProvider.Create(new Type[] {
typeof(SqlTableQuerier),
typeof(SqlLinkedServerQuerier)
});
SmoQuerier querier = serviceProvider.GetService<SmoQuerier>(q => q.SupportedObjectTypes.Contains(smoType));
if (expectExists)
{
Assert.NotNull(querier);
Assert.AreEqual(querierType, querier.GetType());
Assert.NotNull(querier.ServiceProvider);
}
else
{
Assert.Null(querier);
}
}
}
}