mirror of
https://github.com/ckaczor/sqltoolsservice.git
synced 2026-02-03 09:35:39 -05:00
Add error handling for Azure Exceptions (#177)
* Add error handling for Azure Exceptions * Add SRGen for string * Add specific exception messages * Move DefinitionResult class * Add SqlLogin constant * Add error scenarios * revert timeout duration * Modify tests * Modify tests * Add tests * Revert live connection definition * Modify DefinitionsHandlerWithNoConnectionTest * fix test after merge * Code review changes * Code review changes * Code review changes
This commit is contained in:
@@ -3,11 +3,14 @@
|
||||
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
||||
//
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using System.Runtime.InteropServices;
|
||||
using Microsoft.SqlServer.Management.Common;
|
||||
using Microsoft.SqlServer.Management.SqlParser.Binder;
|
||||
using Microsoft.SqlServer.Management.SqlParser.Intellisense;
|
||||
using Microsoft.SqlServer.Management.SqlParser.MetadataProvider;
|
||||
using Microsoft.SqlServer.Management.SqlParser.Parser;
|
||||
using Microsoft.SqlTools.ServiceLayer.Connection;
|
||||
@@ -91,8 +94,9 @@ namespace Microsoft.SqlTools.ServiceLayer.Test.LanguageServices
|
||||
requestContext = new Mock<RequestContext<Location[]>>();
|
||||
requestContext.Setup(rc => rc.SendResult(It.IsAny<Location[]>()))
|
||||
.Returns(Task.FromResult(0));
|
||||
requestContext.Setup(r => r.SendEvent(It.IsAny<EventType<TelemetryParams>>(), It.IsAny<TelemetryParams>()));
|
||||
requestContext.Setup(r => r.SendEvent(It.IsAny<EventType<StatusChangeParams>>(), It.IsAny<StatusChangeParams>()));
|
||||
requestContext.Setup(rc => rc.SendError(It.IsAny<DefinitionError>())).Returns(Task.FromResult(0));;
|
||||
requestContext.Setup(r => r.SendEvent(It.IsAny<EventType<TelemetryParams>>(), It.IsAny<TelemetryParams>())).Returns(Task.FromResult(0));;
|
||||
requestContext.Setup(r => r.SendEvent(It.IsAny<EventType<StatusChangeParams>>(), It.IsAny<StatusChangeParams>())).Returns(Task.FromResult(0));;
|
||||
|
||||
// setup the IBinder mock
|
||||
binder = new Mock<IBinder>();
|
||||
@@ -103,7 +107,7 @@ namespace Microsoft.SqlTools.ServiceLayer.Test.LanguageServices
|
||||
|
||||
var testScriptParseInfo = new ScriptParseInfo();
|
||||
LanguageService.Instance.AddOrUpdateScriptParseInfo(this.testScriptUri, testScriptParseInfo);
|
||||
testScriptParseInfo.IsConnected = true;
|
||||
testScriptParseInfo.IsConnected = false;
|
||||
testScriptParseInfo.ConnectionKey = LanguageService.Instance.BindingQueue.AddConnectionContext(connectionInfo);
|
||||
|
||||
// setup the binding context object
|
||||
@@ -115,18 +119,19 @@ namespace Microsoft.SqlTools.ServiceLayer.Test.LanguageServices
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Tests the definition event handler. When called with no active connection, no definition is sent
|
||||
/// Tests the definition event handler. When called with no active connection, an error is sent
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public async Task DefinitionsHandlerWithNoConnectionTest()
|
||||
{
|
||||
TestObjects.InitializeTestServices();
|
||||
InitializeTestObjects();
|
||||
// request the completion list
|
||||
await Task.WhenAny(LanguageService.HandleDefinitionRequest(textDocument, requestContext.Object), Task.Delay(TaskTimeout));
|
||||
|
||||
// verify that send result was not called
|
||||
// request definition
|
||||
var definitionTask = await Task.WhenAny(LanguageService.HandleDefinitionRequest(textDocument, requestContext.Object), Task.Delay(TaskTimeout));
|
||||
await definitionTask;
|
||||
// verify that send result was not called and send error was called
|
||||
requestContext.Verify(m => m.SendResult(It.IsAny<Location[]>()), Times.Never());
|
||||
requestContext.Verify(m => m.SendError(It.IsAny<DefinitionError>()), Times.Once());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -136,7 +141,7 @@ namespace Microsoft.SqlTools.ServiceLayer.Test.LanguageServices
|
||||
public void GetLocationFromFileForValidFilePathTest()
|
||||
{
|
||||
String filePath = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "C:\\test\\script.sql" : "/test/script.sql";
|
||||
PeekDefinition peekDefinition = new PeekDefinition(null);
|
||||
PeekDefinition peekDefinition = new PeekDefinition(null, null);
|
||||
Location[] locations = peekDefinition.GetLocationFromFile(filePath, 0);
|
||||
|
||||
String expectedFilePath = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "file:///C:/test/script.sql" : "file:/test/script.sql";
|
||||
@@ -149,11 +154,11 @@ namespace Microsoft.SqlTools.ServiceLayer.Test.LanguageServices
|
||||
[Fact]
|
||||
public void GetSchemaFromDatabaseQualifiedNameWithValidNameTest()
|
||||
{
|
||||
PeekDefinition peekDefinition = new PeekDefinition(null);
|
||||
PeekDefinition peekDefinition = new PeekDefinition(null, null);
|
||||
string validDatabaseQualifiedName = "master.test.test_table";
|
||||
string objectName = "test_table";
|
||||
string expectedSchemaName = "test";
|
||||
|
||||
|
||||
string actualSchemaName = peekDefinition.GetSchemaFromDatabaseQualifiedName(validDatabaseQualifiedName, objectName);
|
||||
Assert.Equal(actualSchemaName, expectedSchemaName);
|
||||
}
|
||||
@@ -165,11 +170,11 @@ namespace Microsoft.SqlTools.ServiceLayer.Test.LanguageServices
|
||||
[Fact]
|
||||
public void GetSchemaFromDatabaseQualifiedNameWithNoSchemaTest()
|
||||
{
|
||||
PeekDefinition peekDefinition = new PeekDefinition(null);
|
||||
PeekDefinition peekDefinition = new PeekDefinition(null, null);
|
||||
string validDatabaseQualifiedName = "test_table";
|
||||
string objectName = "test_table";
|
||||
string expectedSchemaName = "dbo";
|
||||
|
||||
|
||||
string actualSchemaName = peekDefinition.GetSchemaFromDatabaseQualifiedName(validDatabaseQualifiedName, objectName);
|
||||
Assert.Equal(actualSchemaName, expectedSchemaName);
|
||||
}
|
||||
@@ -180,15 +185,15 @@ namespace Microsoft.SqlTools.ServiceLayer.Test.LanguageServices
|
||||
[Fact]
|
||||
public void GetSchemaFromDatabaseQualifiedNameWithInvalidNameTest()
|
||||
{
|
||||
PeekDefinition peekDefinition = new PeekDefinition(null);
|
||||
PeekDefinition peekDefinition = new PeekDefinition(null, null);
|
||||
string validDatabaseQualifiedName = "x.y.z";
|
||||
string objectName = "test_table";
|
||||
string expectedSchemaName = "dbo";
|
||||
|
||||
|
||||
string actualSchemaName = peekDefinition.GetSchemaFromDatabaseQualifiedName(validDatabaseQualifiedName, objectName);
|
||||
Assert.Equal(actualSchemaName, expectedSchemaName);
|
||||
}
|
||||
|
||||
|
||||
#if LIVE_CONNECTION_TESTS
|
||||
/// <summary>
|
||||
/// Test get definition for a table object with active connection
|
||||
@@ -196,9 +201,13 @@ namespace Microsoft.SqlTools.ServiceLayer.Test.LanguageServices
|
||||
[Fact]
|
||||
public void GetValidTableDefinitionTest()
|
||||
{
|
||||
// Get live connectionInfo
|
||||
PeekDefinition peekDefinition = new PeekDefinition(TestObjects.InitLiveConnectionInfoForDefinition());
|
||||
// Get live connectionInfo and serverConnection
|
||||
ConnectionInfo connInfo = TestObjects.InitLiveConnectionInfoForDefinition();
|
||||
ServerConnection serverConnection = TestObjects.InitLiveServerConnectionForDefinition(connInfo);
|
||||
|
||||
PeekDefinition peekDefinition = new PeekDefinition(serverConnection, connInfo);
|
||||
string objectName = "spt_monitor";
|
||||
|
||||
string schemaName = null;
|
||||
string objectType = "TABLE";
|
||||
|
||||
@@ -214,8 +223,11 @@ namespace Microsoft.SqlTools.ServiceLayer.Test.LanguageServices
|
||||
[Fact]
|
||||
public void GetTableDefinitionInvalidObjectTest()
|
||||
{
|
||||
// Get live connectionInfo
|
||||
PeekDefinition peekDefinition = new PeekDefinition(TestObjects.InitLiveConnectionInfoForDefinition());
|
||||
// Get live connectionInfo and serverConnection
|
||||
ConnectionInfo connInfo = TestObjects.InitLiveConnectionInfoForDefinition();
|
||||
ServerConnection serverConnection = TestObjects.InitLiveServerConnectionForDefinition(connInfo);
|
||||
|
||||
PeekDefinition peekDefinition = new PeekDefinition(serverConnection, connInfo);
|
||||
string objectName = "test_invalid";
|
||||
string schemaName = null;
|
||||
string objectType = "TABLE";
|
||||
@@ -231,24 +243,102 @@ namespace Microsoft.SqlTools.ServiceLayer.Test.LanguageServices
|
||||
[Fact]
|
||||
public void GetTableDefinitionWithSchemaTest()
|
||||
{
|
||||
// Get live connectionInfo
|
||||
PeekDefinition peekDefinition = new PeekDefinition(TestObjects.InitLiveConnectionInfoForDefinition());
|
||||
// Get live connectionInfo and serverConnection
|
||||
ConnectionInfo connInfo = TestObjects.InitLiveConnectionInfoForDefinition();
|
||||
ServerConnection serverConnection = TestObjects.InitLiveServerConnectionForDefinition(connInfo);
|
||||
|
||||
PeekDefinition peekDefinition = new PeekDefinition(serverConnection, connInfo);
|
||||
string objectName = "spt_monitor";
|
||||
|
||||
string schemaName = "dbo";
|
||||
string objectType = "TABLE";
|
||||
|
||||
// Get locations for valid table object with schema name
|
||||
Location[] locations = peekDefinition.GetSqlObjectDefinition(peekDefinition.GetTableScripts, objectName, schemaName, objectType);
|
||||
Assert.NotNull(locations);
|
||||
Cleanup(locations);
|
||||
Cleanup(locations);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test GetDefinition with an unsupported type(function)
|
||||
/// Test GetDefinition with an unsupported type(schema - dbo). Expect a error result.
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void GetUnsupportedDefinitionForFullScript()
|
||||
public void GetUnsupportedDefinitionErrorTest()
|
||||
{
|
||||
ScriptFile scriptFile;
|
||||
TextDocumentPosition textDocument = new TextDocumentPosition
|
||||
{
|
||||
TextDocument = new TextDocumentIdentifier { Uri = OwnerUri },
|
||||
Position = new Position
|
||||
{
|
||||
Line = 0,
|
||||
// test for 'dbo'
|
||||
Character = 16
|
||||
}
|
||||
};
|
||||
ConnectionInfo connInfo = TestObjects.InitLiveConnectionInfo(out scriptFile);
|
||||
scriptFile.Contents = "select * from dbo.func ()";
|
||||
var languageService = new LanguageService();
|
||||
ScriptParseInfo scriptInfo = new ScriptParseInfo { IsConnected = true };
|
||||
languageService.ScriptParseInfoMap.Add(OwnerUri, scriptInfo);
|
||||
|
||||
// When I call the language service
|
||||
var result = languageService.GetDefinition(textDocument, scriptFile, connInfo);
|
||||
|
||||
// Then I expect null locations and an error to be reported
|
||||
Assert.NotNull(result);
|
||||
Assert.True(result.IsErrorResult);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get Definition for a object with no definition. Expect a error result
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void GetDefinitionWithNoResultsFoundError()
|
||||
{
|
||||
ConnectionInfo connInfo = TestObjects.InitLiveConnectionInfoForDefinition();
|
||||
ServerConnection serverConnection = TestObjects.InitLiveServerConnectionForDefinition(connInfo);
|
||||
|
||||
PeekDefinition peekDefinition = new PeekDefinition(serverConnection, connInfo);
|
||||
string objectName = "from";
|
||||
|
||||
List<Declaration> declarations = new List<Declaration>();
|
||||
DefinitionResult result = peekDefinition.GetScript(declarations, objectName, null);
|
||||
|
||||
Assert.NotNull(result);
|
||||
Assert.True(result.IsErrorResult);
|
||||
Assert.Equal(SR.PeekDefinitionNoResultsError, result.Message);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test GetDefinition with a forced timeout. Expect a error result.
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void GetDefinitionTimeoutTest()
|
||||
{
|
||||
// Given a binding queue that will automatically time out
|
||||
var languageService = new LanguageService();
|
||||
Mock<ConnectedBindingQueue> queueMock = new Mock<ConnectedBindingQueue>();
|
||||
languageService.BindingQueue = queueMock.Object;
|
||||
ManualResetEvent mre = new ManualResetEvent(true); // Do not block
|
||||
Mock<QueueItem> itemMock = new Mock<QueueItem>();
|
||||
itemMock.Setup(i => i.ItemProcessed).Returns(mre);
|
||||
|
||||
DefinitionResult timeoutResult = null;
|
||||
|
||||
queueMock.Setup(q => q.QueueBindingOperation(
|
||||
It.IsAny<string>(),
|
||||
It.IsAny<Func<IBindingContext, CancellationToken, object>>(),
|
||||
It.IsAny<Func<IBindingContext, object>>(),
|
||||
It.IsAny<int?>(),
|
||||
It.IsAny<int?>()))
|
||||
.Callback<string, Func<IBindingContext, CancellationToken, object>, Func<IBindingContext, object>, int?, int?>(
|
||||
(key, bindOperation, timeoutOperation, blah, blah2) =>
|
||||
{
|
||||
timeoutResult = (DefinitionResult) timeoutOperation((IBindingContext)null);
|
||||
itemMock.Object.Result = timeoutResult;
|
||||
})
|
||||
.Returns(() => itemMock.Object);
|
||||
|
||||
ScriptFile scriptFile;
|
||||
TextDocumentPosition textDocument = new TextDocumentPosition
|
||||
@@ -263,12 +353,17 @@ namespace Microsoft.SqlTools.ServiceLayer.Test.LanguageServices
|
||||
ConnectionInfo connInfo = TestObjects.InitLiveConnectionInfo(out scriptFile);
|
||||
scriptFile.Contents = "select * from dbo.func ()";
|
||||
|
||||
var languageService = LanguageService.Instance;
|
||||
ScriptParseInfo scriptInfo = new ScriptParseInfo { IsConnected = true };
|
||||
languageService.ScriptParseInfoMap.Add(OwnerUri, scriptInfo);
|
||||
|
||||
var locations = languageService.GetDefinition(textDocument, scriptFile, connInfo);
|
||||
Assert.Null(locations);
|
||||
// When I call the language service
|
||||
var result = languageService.GetDefinition(textDocument, scriptFile, connInfo);
|
||||
|
||||
// Then I expect null locations and an error to be reported
|
||||
Assert.NotNull(result);
|
||||
Assert.True(result.IsErrorResult);
|
||||
// Check timeout message
|
||||
Assert.Equal(SR.PeekDefinitionTimedoutError, result.Message);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -276,8 +371,11 @@ namespace Microsoft.SqlTools.ServiceLayer.Test.LanguageServices
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void GetValidViewDefinitionTest()
|
||||
{
|
||||
PeekDefinition peekDefinition = new PeekDefinition(TestObjects.InitLiveConnectionInfoForDefinition());
|
||||
{
|
||||
ConnectionInfo connInfo = TestObjects.InitLiveConnectionInfoForDefinition();
|
||||
ServerConnection serverConnection = TestObjects.InitLiveServerConnectionForDefinition(connInfo);
|
||||
|
||||
PeekDefinition peekDefinition = new PeekDefinition(serverConnection, connInfo);
|
||||
string objectName = "objects";
|
||||
string schemaName = "sys";
|
||||
string objectType = "VIEW";
|
||||
@@ -293,7 +391,11 @@ namespace Microsoft.SqlTools.ServiceLayer.Test.LanguageServices
|
||||
[Fact]
|
||||
public void GetViewDefinitionInvalidObjectTest()
|
||||
{
|
||||
PeekDefinition peekDefinition = new PeekDefinition(TestObjects.InitLiveConnectionInfoForDefinition());
|
||||
// Get live connectionInfo and serverConnection
|
||||
ConnectionInfo connInfo = TestObjects.InitLiveConnectionInfoForDefinition();
|
||||
ServerConnection serverConnection = TestObjects.InitLiveServerConnectionForDefinition(connInfo);
|
||||
|
||||
PeekDefinition peekDefinition = new PeekDefinition(serverConnection, connInfo);
|
||||
string objectName = "objects";
|
||||
string schemaName = null;
|
||||
string objectType = "VIEW";
|
||||
@@ -308,8 +410,13 @@ namespace Microsoft.SqlTools.ServiceLayer.Test.LanguageServices
|
||||
[Fact]
|
||||
public void GetStoredProcedureDefinitionTest()
|
||||
{
|
||||
PeekDefinition peekDefinition = new PeekDefinition(TestObjects.InitLiveConnectionInfoForDefinition());
|
||||
// Get live connectionInfo and serverConnection
|
||||
ConnectionInfo connInfo = TestObjects.InitLiveConnectionInfoForDefinition();
|
||||
ServerConnection serverConnection = TestObjects.InitLiveServerConnectionForDefinition(connInfo);
|
||||
|
||||
PeekDefinition peekDefinition = new PeekDefinition(serverConnection, connInfo);
|
||||
string objectName = "sp_MSrepl_startup";
|
||||
|
||||
string schemaName = "dbo";
|
||||
string objectType = "PROCEDURE";
|
||||
|
||||
@@ -324,7 +431,11 @@ namespace Microsoft.SqlTools.ServiceLayer.Test.LanguageServices
|
||||
[Fact]
|
||||
public void GetStoredProcedureDefinitionFailureTest()
|
||||
{
|
||||
PeekDefinition peekDefinition = new PeekDefinition(TestObjects.InitLiveConnectionInfoForDefinition());
|
||||
// Get live connectionInfo and serverConnection
|
||||
ConnectionInfo connInfo = TestObjects.InitLiveConnectionInfoForDefinition();
|
||||
ServerConnection serverConnection = TestObjects.InitLiveServerConnectionForDefinition(connInfo);
|
||||
|
||||
PeekDefinition peekDefinition = new PeekDefinition(serverConnection, connInfo);
|
||||
string objectName = "SP2";
|
||||
string schemaName = "dbo";
|
||||
string objectType = "PROCEDURE";
|
||||
@@ -339,7 +450,11 @@ namespace Microsoft.SqlTools.ServiceLayer.Test.LanguageServices
|
||||
[Fact]
|
||||
public void GetStoredProcedureDefinitionWithoutSchemaTest()
|
||||
{
|
||||
PeekDefinition peekDefinition = new PeekDefinition(TestObjects.InitLiveConnectionInfoForDefinition());
|
||||
// Get live connectionInfo and serverConnection
|
||||
ConnectionInfo connInfo = TestObjects.InitLiveConnectionInfoForDefinition();
|
||||
ServerConnection serverConnection = TestObjects.InitLiveServerConnectionForDefinition(connInfo);
|
||||
|
||||
PeekDefinition peekDefinition = new PeekDefinition(serverConnection, connInfo);
|
||||
string objectName = "sp_MSrepl_startup";
|
||||
string schemaName = null;
|
||||
string objectType = "PROCEDURE";
|
||||
|
||||
Reference in New Issue
Block a user