mirror of
https://github.com/ckaczor/sqltoolsservice.git
synced 2026-01-14 09:35:43 -05:00
* Add LanguageFlavorNotification handling and refactor LanguageService - Added new notification handler for language flavor changed - Refactored the LanguageService so that it no longer relies on so many intertwined static calls, which meant it was impossible to test without modifying the static instance. This will help with test reliability in the future, and prep for replacing the instance with a service provider. * Skip if not an MSSQL doc and add test * Handle definition requests * Fix diagnostics handling
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.Instance.ConnectionServiceInstance != null);
|
|
Assert.True(LanguageService.Instance.CurrentWorkspaceSettings != 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 };
|
|
|
|
LanguageService.Instance.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));
|
|
}
|
|
}
|
|
}
|