mirror of
https://github.com/ckaczor/sqltoolsservice.git
synced 2026-01-17 09:35:37 -05:00
Add tests to improve code coverage (#187)
* DbColumn and ReliableConnection tests * More retry connection tests * More tests * Fix broken peek definition integration tests * Fix test bug * Add a couple batch tests * Add some more tests * More tests for code coverage. * Validation and Diagnostic tests * A few more tests * A few mote test changes. * Update file path tests to run on Windows only
This commit is contained in:
@@ -39,8 +39,12 @@ namespace Microsoft.SqlTools.ServiceLayer.Test.LanguageServices
|
||||
|
||||
private Mock<RequestContext<CompletionItem[]>> requestContext;
|
||||
|
||||
private Mock<ScriptFile> scriptFile;
|
||||
|
||||
private Mock<IBinder> binder;
|
||||
|
||||
private ScriptParseInfo scriptParseInfo;
|
||||
|
||||
private TextDocumentPosition textDocument;
|
||||
|
||||
private void InitializeTestObjects()
|
||||
@@ -60,14 +64,14 @@ namespace Microsoft.SqlTools.ServiceLayer.Test.LanguageServices
|
||||
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);
|
||||
scriptFile = new Mock<ScriptFile>();
|
||||
scriptFile.SetupGet(file => file.Contents).Returns(Common.StandardQuery);
|
||||
scriptFile.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);
|
||||
.Returns(scriptFile.Object);
|
||||
|
||||
// setup binding queue mock
|
||||
bindingQueue = new Mock<ConnectedBindingQueue>();
|
||||
@@ -93,16 +97,113 @@ namespace Microsoft.SqlTools.ServiceLayer.Test.LanguageServices
|
||||
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);
|
||||
scriptParseInfo = new ScriptParseInfo();
|
||||
LanguageService.Instance.AddOrUpdateScriptParseInfo(this.testScriptUri, scriptParseInfo);
|
||||
scriptParseInfo.IsConnected = true;
|
||||
scriptParseInfo.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);
|
||||
LanguageService.Instance.BindingQueue.BindingContextMap.Add(scriptParseInfo.ConnectionKey, bindingContext);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void HandleCompletionRequestDisabled()
|
||||
{
|
||||
InitializeTestObjects();
|
||||
WorkspaceService<SqlToolsSettings>.Instance.CurrentSettings.SqlTools.IntelliSense.EnableIntellisense = false;
|
||||
Assert.NotNull(LanguageService.HandleCompletionRequest(null, null));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void HandleCompletionResolveRequestDisabled()
|
||||
{
|
||||
InitializeTestObjects();
|
||||
WorkspaceService<SqlToolsSettings>.Instance.CurrentSettings.SqlTools.IntelliSense.EnableIntellisense = false;
|
||||
Assert.NotNull(LanguageService.HandleCompletionResolveRequest(null, null));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void HandleSignatureHelpRequestDisabled()
|
||||
{
|
||||
InitializeTestObjects();
|
||||
WorkspaceService<SqlToolsSettings>.Instance.CurrentSettings.SqlTools.IntelliSense.EnableIntellisense = false;
|
||||
Assert.NotNull(LanguageService.HandleSignatureHelpRequest(null, null));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void AddOrUpdateScriptParseInfoNullUri()
|
||||
{
|
||||
InitializeTestObjects();
|
||||
LanguageService.Instance.AddOrUpdateScriptParseInfo("abracadabra", scriptParseInfo);
|
||||
Assert.True(LanguageService.Instance.ScriptParseInfoMap.ContainsKey("abracadabra"));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetDefinitionInvalidTextDocument()
|
||||
{
|
||||
InitializeTestObjects();
|
||||
textDocument.TextDocument.Uri = "invaliduri";
|
||||
Assert.Null(LanguageService.Instance.GetDefinition(textDocument, null, null));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void RemoveScriptParseInfoNullUri()
|
||||
{
|
||||
InitializeTestObjects();
|
||||
Assert.False(LanguageService.Instance.RemoveScriptParseInfo("abc123"));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void IsPreviewWindowNullScriptFileTest()
|
||||
{
|
||||
InitializeTestObjects();
|
||||
Assert.False(LanguageService.Instance.IsPreviewWindow(null));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetCompletionItemsInvalidTextDocument()
|
||||
{
|
||||
InitializeTestObjects();
|
||||
textDocument.TextDocument.Uri = "somethinggoeshere";
|
||||
Assert.True(LanguageService.Instance.GetCompletionItems(textDocument, scriptFile.Object, null).Length > 0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetDiagnosticFromMarkerTest()
|
||||
{
|
||||
var scriptFileMarker = new ScriptFileMarker()
|
||||
{
|
||||
Message = "Message",
|
||||
Level = ScriptFileMarkerLevel.Error,
|
||||
ScriptRegion = new ScriptRegion()
|
||||
{
|
||||
File = "file://nofile.sql",
|
||||
StartLineNumber = 1,
|
||||
StartColumnNumber = 1,
|
||||
StartOffset = 0,
|
||||
EndLineNumber = 1,
|
||||
EndColumnNumber = 1,
|
||||
EndOffset = 0
|
||||
}
|
||||
};
|
||||
var diagnostic = DiagnosticsHelper.GetDiagnosticFromMarker(scriptFileMarker);
|
||||
Assert.Equal(diagnostic.Message, scriptFileMarker.Message);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void MapDiagnosticSeverityTest()
|
||||
{
|
||||
var level = ScriptFileMarkerLevel.Error;
|
||||
Assert.Equal(DiagnosticsHelper.MapDiagnosticSeverity(level), DiagnosticSeverity.Error);
|
||||
level = ScriptFileMarkerLevel.Warning;
|
||||
Assert.Equal(DiagnosticsHelper.MapDiagnosticSeverity(level), DiagnosticSeverity.Warning);
|
||||
level = ScriptFileMarkerLevel.Information;
|
||||
Assert.Equal(DiagnosticsHelper.MapDiagnosticSeverity(level), DiagnosticSeverity.Information);
|
||||
level = (ScriptFileMarkerLevel)100;
|
||||
Assert.Equal(DiagnosticsHelper.MapDiagnosticSeverity(level), DiagnosticSeverity.Error);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -3,8 +3,10 @@
|
||||
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
||||
//
|
||||
|
||||
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.Workspace.Contracts;
|
||||
using Microsoft.SqlTools.Test.Utility;
|
||||
@@ -143,6 +145,56 @@ namespace Microsoft.SqlTools.ServiceLayer.Test.LanguageServer
|
||||
Assert.Null(signatureHelp);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void EmptyCompletionListTest()
|
||||
{
|
||||
Assert.Equal(AutoCompleteHelper.EmptyCompletionList.Length, 0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SetWorkspaceServiceInstanceTest()
|
||||
{
|
||||
AutoCompleteHelper.WorkspaceServiceInstance = null;
|
||||
// workspace will be recreated if it's set to null
|
||||
Assert.NotNull(AutoCompleteHelper.WorkspaceServiceInstance);
|
||||
}
|
||||
|
||||
internal class TestScriptDocumentInfo : ScriptDocumentInfo
|
||||
{
|
||||
public TestScriptDocumentInfo(TextDocumentPosition textDocumentPosition, ScriptFile scriptFile, ScriptParseInfo scriptParseInfo)
|
||||
:base(textDocumentPosition, scriptFile, scriptParseInfo)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public override string TokenText
|
||||
{
|
||||
get
|
||||
{
|
||||
return "doesntmatchanythingintheintellisensedefaultlist";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetDefaultCompletionListWithNoMatchesTest()
|
||||
{
|
||||
var scriptFile = new ScriptFile();
|
||||
scriptFile.SetFileContents("koko wants a bananas");
|
||||
|
||||
ScriptParseInfo scriptInfo = new ScriptParseInfo { IsConnected = false };
|
||||
|
||||
var scriptDocumentInfo = new TestScriptDocumentInfo(
|
||||
new TextDocumentPosition()
|
||||
{
|
||||
TextDocument = new TextDocumentIdentifier() { Uri = TestObjects.ScriptUri },
|
||||
Position = new Position() { Line = 0, Character = 0 }
|
||||
}, scriptFile, scriptInfo);
|
||||
|
||||
AutoCompleteHelper.GetDefaultCompletionItems(scriptDocumentInfo, false);
|
||||
|
||||
}
|
||||
|
||||
private TextDocumentPosition CreateDummyDocPosition()
|
||||
{
|
||||
return new TextDocumentPosition
|
||||
@@ -180,11 +232,10 @@ namespace Microsoft.SqlTools.ServiceLayer.Test.LanguageServer
|
||||
|
||||
connInfo = TestObjects.InitLiveConnectionInfo(out scriptFile);
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Test the service initialization code path and verify nothing throws
|
||||
/// </summary>
|
||||
// Test is causing failures in build lab..investigating to reenable
|
||||
[Fact]
|
||||
public void ServiceInitialization()
|
||||
{
|
||||
@@ -200,12 +251,11 @@ namespace Microsoft.SqlTools.ServiceLayer.Test.LanguageServer
|
||||
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>
|
||||
// Test is causing failures in build lab..investigating to reenable
|
||||
[Fact]
|
||||
public void PrepopulateCommonMetadata()
|
||||
{
|
||||
|
||||
@@ -197,9 +197,8 @@ namespace Microsoft.SqlTools.ServiceLayer.Test.LanguageServices
|
||||
public void GetValidTableDefinitionTest()
|
||||
{
|
||||
// Get live connectionInfo
|
||||
ConnectionInfo connInfo = TestObjects.InitLiveConnectionInfoForDefinition();
|
||||
PeekDefinition peekDefinition = new PeekDefinition(connInfo);
|
||||
string objectName = "test_table";
|
||||
PeekDefinition peekDefinition = new PeekDefinition(TestObjects.InitLiveConnectionInfoForDefinition());
|
||||
string objectName = "spt_monitor";
|
||||
string schemaName = null;
|
||||
string objectType = "TABLE";
|
||||
|
||||
@@ -216,8 +215,7 @@ namespace Microsoft.SqlTools.ServiceLayer.Test.LanguageServices
|
||||
public void GetTableDefinitionInvalidObjectTest()
|
||||
{
|
||||
// Get live connectionInfo
|
||||
ConnectionInfo connInfo = TestObjects.InitLiveConnectionInfoForDefinition();
|
||||
PeekDefinition peekDefinition = new PeekDefinition(connInfo);
|
||||
PeekDefinition peekDefinition = new PeekDefinition(TestObjects.InitLiveConnectionInfoForDefinition());
|
||||
string objectName = "test_invalid";
|
||||
string schemaName = null;
|
||||
string objectType = "TABLE";
|
||||
@@ -234,9 +232,8 @@ namespace Microsoft.SqlTools.ServiceLayer.Test.LanguageServices
|
||||
public void GetTableDefinitionWithSchemaTest()
|
||||
{
|
||||
// Get live connectionInfo
|
||||
ConnectionInfo connInfo = TestObjects.InitLiveConnectionInfoForDefinition();
|
||||
PeekDefinition peekDefinition = new PeekDefinition(connInfo);
|
||||
string objectName = "test_table";
|
||||
PeekDefinition peekDefinition = new PeekDefinition(TestObjects.InitLiveConnectionInfoForDefinition());
|
||||
string objectName = "spt_monitor";
|
||||
string schemaName = "dbo";
|
||||
string objectType = "TABLE";
|
||||
|
||||
@@ -279,9 +276,8 @@ namespace Microsoft.SqlTools.ServiceLayer.Test.LanguageServices
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void GetValidViewDefinitionTest()
|
||||
{
|
||||
ConnectionInfo connInfo = TestObjects.InitLiveConnectionInfoForDefinition();
|
||||
PeekDefinition peekDefinition = new PeekDefinition(connInfo);
|
||||
{
|
||||
PeekDefinition peekDefinition = new PeekDefinition(TestObjects.InitLiveConnectionInfoForDefinition());
|
||||
string objectName = "objects";
|
||||
string schemaName = "sys";
|
||||
string objectType = "VIEW";
|
||||
@@ -297,8 +293,7 @@ namespace Microsoft.SqlTools.ServiceLayer.Test.LanguageServices
|
||||
[Fact]
|
||||
public void GetViewDefinitionInvalidObjectTest()
|
||||
{
|
||||
ConnectionInfo connInfo = TestObjects.InitLiveConnectionInfoForDefinition();
|
||||
PeekDefinition peekDefinition = new PeekDefinition(connInfo);
|
||||
PeekDefinition peekDefinition = new PeekDefinition(TestObjects.InitLiveConnectionInfoForDefinition());
|
||||
string objectName = "objects";
|
||||
string schemaName = null;
|
||||
string objectType = "VIEW";
|
||||
@@ -313,9 +308,8 @@ namespace Microsoft.SqlTools.ServiceLayer.Test.LanguageServices
|
||||
[Fact]
|
||||
public void GetStoredProcedureDefinitionTest()
|
||||
{
|
||||
ConnectionInfo connInfo = TestObjects.InitLiveConnectionInfoForDefinition();
|
||||
PeekDefinition peekDefinition = new PeekDefinition(connInfo);
|
||||
string objectName = "SP1";
|
||||
PeekDefinition peekDefinition = new PeekDefinition(TestObjects.InitLiveConnectionInfoForDefinition());
|
||||
string objectName = "sp_MSrepl_startup";
|
||||
string schemaName = "dbo";
|
||||
string objectType = "PROCEDURE";
|
||||
|
||||
@@ -330,8 +324,7 @@ namespace Microsoft.SqlTools.ServiceLayer.Test.LanguageServices
|
||||
[Fact]
|
||||
public void GetStoredProcedureDefinitionFailureTest()
|
||||
{
|
||||
ConnectionInfo connInfo = TestObjects.InitLiveConnectionInfoForDefinition();
|
||||
PeekDefinition peekDefinition = new PeekDefinition(connInfo);
|
||||
PeekDefinition peekDefinition = new PeekDefinition(TestObjects.InitLiveConnectionInfoForDefinition());
|
||||
string objectName = "SP2";
|
||||
string schemaName = "dbo";
|
||||
string objectType = "PROCEDURE";
|
||||
@@ -346,9 +339,8 @@ namespace Microsoft.SqlTools.ServiceLayer.Test.LanguageServices
|
||||
[Fact]
|
||||
public void GetStoredProcedureDefinitionWithoutSchemaTest()
|
||||
{
|
||||
ConnectionInfo connInfo = TestObjects.InitLiveConnectionInfoForDefinition();
|
||||
PeekDefinition peekDefinition = new PeekDefinition(connInfo);
|
||||
string objectName = "SP1";
|
||||
PeekDefinition peekDefinition = new PeekDefinition(TestObjects.InitLiveConnectionInfoForDefinition());
|
||||
string objectName = "sp_MSrepl_startup";
|
||||
string schemaName = null;
|
||||
string objectType = "PROCEDURE";
|
||||
|
||||
|
||||
Reference in New Issue
Block a user