// // 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.Hosting.Protocol; using Microsoft.SqlTools.Hosting.Protocol.Contracts; using Microsoft.SqlTools.ServiceLayer.Connection; using Microsoft.SqlTools.ServiceLayer.LanguageServices; using Microsoft.SqlTools.ServiceLayer.LanguageServices.Contracts; using Microsoft.SqlTools.ServiceLayer.SqlContext; using Microsoft.SqlTools.ServiceLayer.UnitTests.Utility; using Microsoft.SqlTools.ServiceLayer.Workspace; using Microsoft.SqlTools.ServiceLayer.Workspace.Contracts; using GlobalCommon = Microsoft.SqlTools.ServiceLayer.Test.Common; using Moq; namespace Microsoft.SqlTools.ServiceLayer.UnitTests.LanguageServer { /// /// Tests for the language service autocomplete component /// public abstract class LanguageServiceTestBase { protected const int TaskTimeout = 60000; protected readonly string testScriptUri = TestObjects.ScriptUri; protected readonly string testConnectionKey = "testdbcontextkey"; protected LanguageService langService; protected Mock bindingQueue; protected Mock> workspaceService; protected Mock> requestContext; protected Mock scriptFile; protected Mock binder; internal ScriptParseInfo scriptParseInfo; protected TextDocumentPosition textDocument; protected void InitializeTestObjects() { // initial cursor position in the script file textDocument = new TextDocumentPosition { TextDocument = new TextDocumentIdentifier { Uri = this.testScriptUri }, Position = new Position { Line = 0, Character = 23 } }; // default settings are stored in the workspace service WorkspaceService.Instance.CurrentSettings = new SqlToolsSettings(); // set up file for returning the query scriptFile = new Mock(); scriptFile.SetupGet(file => file.Contents).Returns(GlobalCommon.Constants.StandardQuery); scriptFile.SetupGet(file => file.ClientUri).Returns(this.testScriptUri); // set up workspace mock workspaceService = new Mock>(); workspaceService.Setup(service => service.Workspace.GetFile(It.IsAny())) .Returns((string filePath) => { return filePath == this.testScriptUri ? scriptFile.Object : null; }); // setup binding queue mock bindingQueue = new Mock(); bindingQueue.Setup(q => q.AddConnectionContext(It.IsAny(), It.IsAny(), It.IsAny())) .Returns(this.testConnectionKey); langService = new LanguageService(); // inject mock instances into the Language Service langService.WorkspaceServiceInstance = workspaceService.Object; langService.ConnectionServiceInstance = TestObjects.GetTestConnectionService(); ConnectionInfo connectionInfo = TestObjects.GetTestConnectionInfo(); langService.ConnectionServiceInstance.OwnerToConnectionMap.Add(this.testScriptUri, connectionInfo); langService.BindingQueue = bindingQueue.Object; // setup the mock for SendResult requestContext = new Mock>(); requestContext.Setup(rc => rc.SendResult(It.IsAny())) .Returns(Task.FromResult(0)); requestContext.Setup(rc => rc.SendError(It.IsAny(), It.IsAny())).Returns(Task.FromResult(0)); requestContext.Setup(r => r.SendEvent(It.IsAny>(), It.IsAny())).Returns(Task.FromResult(0)); requestContext.Setup(r => r.SendEvent(It.IsAny>(), It.IsAny())).Returns(Task.FromResult(0)); // setup the IBinder mock binder = new Mock(); binder.Setup(b => b.Bind( It.IsAny>(), It.IsAny(), It.IsAny())); scriptParseInfo = new ScriptParseInfo(); langService.AddOrUpdateScriptParseInfo(this.testScriptUri, scriptParseInfo); scriptParseInfo.IsConnected = true; scriptParseInfo.ConnectionKey = langService.BindingQueue.AddConnectionContext(connectionInfo); // setup the binding context object ConnectedBindingContext bindingContext = new ConnectedBindingContext(); bindingContext.Binder = binder.Object; bindingContext.MetadataDisplayInfoProvider = new MetadataDisplayInfoProvider(); langService.BindingQueue.BindingContextMap.Add(scriptParseInfo.ConnectionKey, bindingContext); } } }