// // 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 { /// /// Tests for the language service autocomplete component /// public class AutocompleteTests { private const int TaskTimeout = 60000; private readonly string testScriptUri = TestObjects.ScriptUri; private readonly string testConnectionKey = "testdbcontextkey"; private Mock bindingQueue; private Mock> workspaceService; private Mock> requestContext; private Mock 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.Instance.CurrentSettings = new SqlToolsSettings(); // set up file for returning the query var fileMock = new Mock(); fileMock.SetupGet(file => file.Contents).Returns(Common.StandardQuery); fileMock.SetupGet(file => file.ClientFilePath).Returns(this.testScriptUri); // set up workspace mock workspaceService = new Mock>(); workspaceService.Setup(service => service.Workspace.GetFile(It.IsAny())) .Returns(fileMock.Object); // setup binding queue mock bindingQueue = new Mock(); bindingQueue.Setup(q => q.AddConnectionContext(It.IsAny())) .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.Setup(rc => rc.SendResult(It.IsAny())) .Returns(Task.FromResult(0)); // setup the IBinder mock binder = new Mock(); binder.Setup(b => b.Bind( It.IsAny>(), It.IsAny(), It.IsAny())); 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); } /// /// Tests the primary completion list event handler /// [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()), Times.Once()); } /// /// Test the service initialization code path and verify nothing throws /// [Fact] public async void UpdateLanguageServiceOnConnection() { InitializeTestObjects(); AutoCompleteHelper.WorkspaceServiceInstance = workspaceService.Object; ConnectionInfo connInfo = TestObjects.GetTestConnectionInfo(); await LanguageService.Instance.UpdateLanguageServiceOnConnection(connInfo); } } }