mirror of
https://github.com/ckaczor/sqltoolsservice.git
synced 2026-01-14 01:25:40 -05:00
The goal of this make sure that test code is correctly organized to ensure that test suites aren't dependent on each other.
* UnitTests get their own project now (renaming Microsoft.SqlTools.ServiceLayer.Test to Microsoft.SqlTools.ServiceLayer.UnitTests) which is about 90% of the changes to the files.
* IntegrationTests no longer depends on UnitTests, only Test.Common
* Any shared components from TestObjects that spins up a "live" connection has been moved to IntegrationTests Utility/LiveConnectionHelper.cs
* The dictionary-based mock file stream factory has been moved to Test.Common since it is used by UnitTests and IntegrationTests
* Added a overload that doesn't take a dictionary for when we don't care about monitoring the storage (about 90% of the time)
* The RunIf* wrapper methods have been moved to Test.Common
* OwnerUri and StandardQuery constants have been moved to Test.Common Constants file
* Updating to latest SDK version available at https://www.microsoft.com/net/core#windowscmd
* Moving unit tests to unit test folder
* Changing namespaces to UnitTests
* Moving some constants and shared functionality into common project, making the UnitTests reference it
* Unit tests are working!
* Integration tests are working
* Updating automated test runs
* Fixing one last broken unit test
* Exposing internals for other projects
* Moving edit data tests to UnitTest project
* Applying refactor fixes to unit tests
* Fixing flaky test that wasn't awaiting completion
159 lines
6.3 KiB
C#
159 lines
6.3 KiB
C#
//
|
|
// Copyright (c) Microsoft. All rights reserved.
|
|
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
|
//
|
|
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using Microsoft.SqlTools.ServiceLayer.IntegrationTests.Utility;
|
|
using Microsoft.SqlTools.ServiceLayer.LanguageServices;
|
|
using Microsoft.SqlTools.ServiceLayer.LanguageServices.Contracts;
|
|
using Microsoft.SqlTools.ServiceLayer.Test.Common;
|
|
using Microsoft.SqlTools.ServiceLayer.Workspace.Contracts;
|
|
using Xunit;
|
|
|
|
namespace Microsoft.SqlTools.ServiceLayer.IntegrationTests.LanguageServer
|
|
{
|
|
/// <summary>
|
|
/// Tests for the ServiceHost Language Service tests
|
|
/// </summary>
|
|
public class LanguageServiceTests
|
|
{
|
|
private LiveConnectionHelper.TestConnectionResult GetLiveAutoCompleteTestObjects()
|
|
{
|
|
var textDocument = new TextDocumentPosition
|
|
{
|
|
TextDocument = new TextDocumentIdentifier { Uri = Constants.OwnerUri },
|
|
Position = new Position
|
|
{
|
|
Line = 0,
|
|
Character = 0
|
|
}
|
|
};
|
|
|
|
var result = LiveConnectionHelper.InitLiveConnectionInfo();
|
|
result.TextDocumentPosition = textDocument;
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Test the service initialization code path and verify nothing throws
|
|
/// </summary>
|
|
[Fact]
|
|
public void ServiceInitialization()
|
|
{
|
|
try
|
|
{
|
|
TestServiceProvider serviceProvider = TestServiceProvider.Instance;
|
|
Assert.NotNull(serviceProvider);
|
|
}
|
|
catch (System.ArgumentException)
|
|
{
|
|
|
|
}
|
|
Assert.True(LanguageService.Instance.Context != null);
|
|
Assert.True(LanguageService.ConnectionServiceInstance != null);
|
|
Assert.True(LanguageService.Instance.CurrentSettings != null);
|
|
Assert.True(LanguageService.Instance.CurrentWorkspace != null);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Test the service initialization code path and verify nothing throws
|
|
/// </summary>
|
|
[Fact]
|
|
public void PrepopulateCommonMetadata()
|
|
{
|
|
var result = LiveConnectionHelper.InitLiveConnectionInfo();
|
|
var connInfo = result.ConnectionInfo;
|
|
|
|
ScriptParseInfo scriptInfo = new ScriptParseInfo { IsConnected = true };
|
|
|
|
AutoCompleteHelper.PrepopulateCommonMetadata(connInfo, scriptInfo, null);
|
|
}
|
|
|
|
// This test currently requires a live database connection to initialize
|
|
// SMO connected metadata provider. Since we don't want a live DB dependency
|
|
// in the CI unit tests this scenario is currently disabled.
|
|
[Fact]
|
|
public void AutoCompleteFindCompletions()
|
|
{
|
|
var result = GetLiveAutoCompleteTestObjects();
|
|
|
|
result.TextDocumentPosition.Position.Character = 7;
|
|
result.ScriptFile.Contents = "select ";
|
|
|
|
var autoCompleteService = LanguageService.Instance;
|
|
var completions = autoCompleteService.GetCompletionItems(
|
|
result.TextDocumentPosition,
|
|
result.ScriptFile,
|
|
result.ConnectionInfo);
|
|
|
|
Assert.True(completions.Length > 0);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Verify that GetSignatureHelp returns not null when the provided TextDocumentPosition
|
|
/// has an associated ScriptParseInfo and the provided query has a function that should
|
|
/// provide signature help.
|
|
/// </summary>
|
|
[Fact]
|
|
public async Task GetSignatureHelpReturnsNotNullIfParseInfoInitialized()
|
|
{
|
|
// When we make a connection to a live database
|
|
Hosting.ServiceHost.SendEventIgnoreExceptions = true;
|
|
var result = LiveConnectionHelper.InitLiveConnectionInfo();
|
|
|
|
// And we place the cursor after a function that should prompt for signature help
|
|
string queryWithFunction = "EXEC sys.fn_isrolemember ";
|
|
result.ScriptFile.Contents = queryWithFunction;
|
|
TextDocumentPosition textDocument = new TextDocumentPosition
|
|
{
|
|
TextDocument = new TextDocumentIdentifier
|
|
{
|
|
Uri = result.ScriptFile.ClientFilePath
|
|
},
|
|
Position = new Position
|
|
{
|
|
Line = 0,
|
|
Character = queryWithFunction.Length
|
|
}
|
|
};
|
|
|
|
// If the SQL has already been parsed
|
|
var service = LanguageService.Instance;
|
|
await service.UpdateLanguageServiceOnConnection(result.ConnectionInfo);
|
|
Thread.Sleep(2000);
|
|
|
|
// We should get back a non-null ScriptParseInfo
|
|
ScriptParseInfo parseInfo = service.GetScriptParseInfo(result.ScriptFile.ClientFilePath);
|
|
Assert.NotNull(parseInfo);
|
|
|
|
// And we should get back a non-null SignatureHelp
|
|
SignatureHelp signatureHelp = service.GetSignatureHelp(textDocument, result.ScriptFile);
|
|
Assert.NotNull(signatureHelp);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Test overwriting the binding queue context
|
|
/// </summary>
|
|
[Fact]
|
|
public void OverwriteBindingContext()
|
|
{
|
|
var result = LiveConnectionHelper.InitLiveConnectionInfo();
|
|
|
|
// add a new connection context
|
|
var connectionKey = LanguageService.Instance.BindingQueue.AddConnectionContext(result.ConnectionInfo, overwrite: true);
|
|
Assert.True(LanguageService.Instance.BindingQueue.BindingContextMap.ContainsKey(connectionKey));
|
|
|
|
// cache the server connection
|
|
var orgServerConnection = LanguageService.Instance.BindingQueue.BindingContextMap[connectionKey].ServerConnection;
|
|
Assert.NotNull(orgServerConnection);
|
|
|
|
// add a new connection context
|
|
connectionKey = LanguageService.Instance.BindingQueue.AddConnectionContext(result.ConnectionInfo, overwrite: true);
|
|
Assert.True(LanguageService.Instance.BindingQueue.BindingContextMap.ContainsKey(connectionKey));
|
|
Assert.False(object.ReferenceEquals(LanguageService.Instance.BindingQueue.BindingContextMap[connectionKey].ServerConnection, orgServerConnection));
|
|
}
|
|
}
|
|
}
|