mirror of
https://github.com/ckaczor/sqltoolsservice.git
synced 2026-01-14 09:35:43 -05:00
140 lines
5.8 KiB
C#
140 lines
5.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.
|
|
//
|
|
|
|
using System.Collections.Generic;
|
|
using System.Threading.Tasks;
|
|
using Microsoft.SqlServer.Management.SqlParser.Binder;
|
|
using Microsoft.SqlServer.Management.SqlParser.MetadataProvider;
|
|
using Microsoft.SqlServer.Management.SqlParser.Parser;
|
|
using Microsoft.SqlTools.ServiceLayer.Connection;
|
|
using Microsoft.SqlTools.ServiceLayer.Hosting.Protocol;
|
|
using Microsoft.SqlTools.ServiceLayer.LanguageServices;
|
|
using Microsoft.SqlTools.ServiceLayer.LanguageServices.Contracts;
|
|
using Microsoft.SqlTools.ServiceLayer.SqlContext;
|
|
using Microsoft.SqlTools.ServiceLayer.Test.QueryExecution;
|
|
using Microsoft.SqlTools.ServiceLayer.Workspace;
|
|
using Microsoft.SqlTools.ServiceLayer.Workspace.Contracts;
|
|
using Microsoft.SqlTools.Test.Utility;
|
|
using Moq;
|
|
using Xunit;
|
|
|
|
namespace Microsoft.SqlTools.ServiceLayer.Test.LanguageServices
|
|
{
|
|
/// <summary>
|
|
/// Tests for the language service autocomplete component
|
|
/// </summary>
|
|
public class AutocompleteTests
|
|
{
|
|
private const int TaskTimeout = 60000;
|
|
|
|
private readonly string testScriptUri = TestObjects.ScriptUri;
|
|
|
|
private readonly string testConnectionKey = "testdbcontextkey";
|
|
|
|
private Mock<ConnectedBindingQueue> bindingQueue;
|
|
|
|
private Mock<WorkspaceService<SqlToolsSettings>> workspaceService;
|
|
|
|
private Mock<RequestContext<CompletionItem[]>> requestContext;
|
|
|
|
private Mock<IBinder> binder;
|
|
|
|
private TextDocumentPosition textDocument;
|
|
|
|
private void InitializeTestObjects()
|
|
{
|
|
// initial cursor position in the script file
|
|
textDocument = new TextDocumentPosition
|
|
{
|
|
TextDocument = new TextDocumentIdentifier {Uri = this.testScriptUri},
|
|
Position = new Position
|
|
{
|
|
Line = 0,
|
|
Character = 0
|
|
}
|
|
};
|
|
|
|
// default settings are stored in the workspace service
|
|
WorkspaceService<SqlToolsSettings>.Instance.CurrentSettings = new SqlToolsSettings();
|
|
|
|
// set up file for returning the query
|
|
var fileMock = new Mock<ScriptFile>();
|
|
fileMock.SetupGet(file => file.Contents).Returns(Common.StandardQuery);
|
|
fileMock.SetupGet(file => file.ClientFilePath).Returns(this.testScriptUri);
|
|
|
|
// set up workspace mock
|
|
workspaceService = new Mock<WorkspaceService<SqlToolsSettings>>();
|
|
workspaceService.Setup(service => service.Workspace.GetFile(It.IsAny<string>()))
|
|
.Returns(fileMock.Object);
|
|
|
|
// setup binding queue mock
|
|
bindingQueue = new Mock<ConnectedBindingQueue>();
|
|
bindingQueue.Setup(q => q.AddConnectionContext(It.IsAny<ConnectionInfo>()))
|
|
.Returns(this.testConnectionKey);
|
|
|
|
// inject mock instances into the Language Service
|
|
LanguageService.WorkspaceServiceInstance = workspaceService.Object;
|
|
LanguageService.ConnectionServiceInstance = TestObjects.GetTestConnectionService();
|
|
ConnectionInfo connectionInfo = TestObjects.GetTestConnectionInfo();
|
|
LanguageService.ConnectionServiceInstance.OwnerToConnectionMap.Add(this.testScriptUri, connectionInfo);
|
|
LanguageService.Instance.BindingQueue = bindingQueue.Object;
|
|
|
|
// setup the mock for SendResult
|
|
requestContext = new Mock<RequestContext<CompletionItem[]>>();
|
|
requestContext.Setup(rc => rc.SendResult(It.IsAny<CompletionItem[]>()))
|
|
.Returns(Task.FromResult(0));
|
|
|
|
// setup the IBinder mock
|
|
binder = new Mock<IBinder>();
|
|
binder.Setup(b => b.Bind(
|
|
It.IsAny<IEnumerable<ParseResult>>(),
|
|
It.IsAny<string>(),
|
|
It.IsAny<BindMode>()));
|
|
|
|
var testScriptParseInfo = new ScriptParseInfo();
|
|
LanguageService.Instance.AddOrUpdateScriptParseInfo(this.testScriptUri, testScriptParseInfo);
|
|
testScriptParseInfo.IsConnected = true;
|
|
testScriptParseInfo.ConnectionKey = LanguageService.Instance.BindingQueue.AddConnectionContext(connectionInfo);
|
|
|
|
// setup the binding context object
|
|
ConnectedBindingContext bindingContext = new ConnectedBindingContext();
|
|
bindingContext.Binder = binder.Object;
|
|
bindingContext.MetadataDisplayInfoProvider = new MetadataDisplayInfoProvider();
|
|
LanguageService.Instance.BindingQueue.BindingContextMap.Add(testScriptParseInfo.ConnectionKey, bindingContext);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Tests the primary completion list event handler
|
|
/// </summary>
|
|
[Fact]
|
|
public void GetCompletionsHandlerTest()
|
|
{
|
|
InitializeTestObjects();
|
|
|
|
// request the completion list
|
|
Task handleCompletion = LanguageService.HandleCompletionRequest(textDocument, requestContext.Object);
|
|
handleCompletion.Wait(TaskTimeout);
|
|
|
|
// verify that send result was called with a completion array
|
|
requestContext.Verify(m => m.SendResult(It.IsAny<CompletionItem[]>()), Times.Once());
|
|
}
|
|
|
|
/// <summary>
|
|
/// Test the service initialization code path and verify nothing throws
|
|
/// </summary>
|
|
[Fact]
|
|
public async void UpdateLanguageServiceOnConnection()
|
|
{
|
|
InitializeTestObjects();
|
|
|
|
AutoCompleteHelper.WorkspaceServiceInstance = workspaceService.Object;
|
|
|
|
ConnectionInfo connInfo = TestObjects.GetTestConnectionInfo();
|
|
|
|
await LanguageService.Instance.UpdateLanguageServiceOnConnection(connInfo);
|
|
}
|
|
}
|
|
}
|