mirror of
https://github.com/ckaczor/sqltoolsservice.git
synced 2026-01-14 09:59:48 -05:00
137 lines
5.1 KiB
C#
137 lines
5.1 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.Tasks;
|
|
using Microsoft.SqlServer.Management.SqlParser.Parser;
|
|
using Microsoft.SqlTools.ServiceLayer.Connection;
|
|
using Microsoft.SqlTools.ServiceLayer.LanguageServices;
|
|
using Microsoft.SqlTools.ServiceLayer.LanguageServices.Completion;
|
|
using Microsoft.SqlTools.ServiceLayer.LanguageServices.Contracts;
|
|
using Microsoft.SqlTools.ServiceLayer.Test.Common;
|
|
using Microsoft.SqlTools.ServiceLayer.Workspace.Contracts;
|
|
using Microsoft.SqlTools.Test.Utility;
|
|
using Xunit;
|
|
|
|
namespace Microsoft.SqlTools.ServiceLayer.IntegrationTests.LanguageServer
|
|
{
|
|
/// <summary>
|
|
/// Tests for the ServiceHost Language Service tests
|
|
/// </summary>
|
|
public class LanguageServiceTests
|
|
{
|
|
private async static Task<TestConnectionResult> GetLiveAutoCompleteTestObjects()
|
|
{
|
|
var textDocument = new TextDocumentPosition
|
|
{
|
|
TextDocument = new TextDocumentIdentifier { Uri = TestObjects.ScriptUri },
|
|
Position = new Position
|
|
{
|
|
Line = 0,
|
|
Character = 0
|
|
}
|
|
};
|
|
|
|
var result = await TestObjects.InitLiveConnectionInfo();
|
|
result.TextDocumentPosition = textDocument;
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Test the service initialization code path and verify nothing throws
|
|
/// </summary>
|
|
[Fact]
|
|
public void ServiceInitialization()
|
|
{
|
|
try
|
|
{
|
|
TestServiceProvider.InitializeTestServices();
|
|
}
|
|
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 async Task PrepopulateCommonMetadata()
|
|
{
|
|
var result = await TestObjects.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 async Task AutoCompleteFindCompletions()
|
|
{
|
|
var result = await 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 = await TestObjects.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);
|
|
|
|
// 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);
|
|
}
|
|
}
|
|
}
|