mirror of
https://github.com/ckaczor/sqltoolsservice.git
synced 2026-01-25 17:24:17 -05:00
Fixing bug in GetOrOpenConnection (#234)
Fixes a bug where the GetOrOpenConnection method of the ConnectionService would throw if the connection didn't exist. It would yield a very unhelpful "key not found in collection" exception. The code has been updated to be much more helpful and fix the bug.
This commit is contained in:
@@ -1139,5 +1139,53 @@ namespace Microsoft.SqlTools.ServiceLayer.Test.Connection
|
||||
await service.Connect(connectParamsQuery);
|
||||
Assert.Equal(2, connectionInfo.CountConnections);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(null)]
|
||||
[InlineData("")]
|
||||
public async Task GetOrOpenNullOwnerUri(string ownerUri)
|
||||
{
|
||||
// If: I have a connection service and I ask for a connection with an invalid ownerUri
|
||||
// Then: An exception should be thrown
|
||||
var service = TestObjects.GetTestConnectionService();
|
||||
await Assert.ThrowsAsync<ArgumentException>(
|
||||
() => service.GetOrOpenConnection(ownerUri, ConnectionType.Default));
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(null)]
|
||||
[InlineData("")]
|
||||
public async Task GetOrOpenNullConnectionType(string connType)
|
||||
{
|
||||
// If: I have a connection service and I ask for a connection with an invalid connectionType
|
||||
// Then: An exception should be thrown
|
||||
var service = TestObjects.GetTestConnectionService();
|
||||
await Assert.ThrowsAsync<ArgumentException>(
|
||||
() => service.GetOrOpenConnection(TestObjects.ScriptUri, connType));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetOrOpenNoConnection()
|
||||
{
|
||||
// If: I have a connection service and I ask for a connection for an unconnected uri
|
||||
// Then: An exception should be thrown
|
||||
var service = TestObjects.GetTestConnectionService();
|
||||
await Assert.ThrowsAsync<ArgumentOutOfRangeException>(
|
||||
() => service.GetOrOpenConnection(TestObjects.ScriptUri, ConnectionType.Query));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetOrOpenNoDefaultConnection()
|
||||
{
|
||||
// Setup: Create a connection service with an empty connection info obj
|
||||
var service = TestObjects.GetTestConnectionService();
|
||||
var connInfo = new ConnectionInfo(null, null, null);
|
||||
service.OwnerToConnectionMap[TestObjects.ScriptUri] = connInfo;
|
||||
|
||||
// If: I ask for a connection on a connection that doesn't have a default connection
|
||||
// Then: An exception should be thrown
|
||||
await Assert.ThrowsAsync<InvalidOperationException>(
|
||||
() => service.GetOrOpenConnection(TestObjects.ScriptUri, ConnectionType.Query));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -206,7 +206,11 @@ namespace Microsoft.SqlTools.ServiceLayer.Test.QueryExecution
|
||||
|
||||
public static ConnectionInfo CreateTestConnectionInfo(TestResultSet[] data, bool throwOnRead)
|
||||
{
|
||||
return new ConnectionInfo(CreateMockFactory(data, throwOnRead), OwnerUri, StandardConnectionDetails);
|
||||
// Create a connection info and add the default connection to it
|
||||
ISqlConnectionFactory factory = CreateMockFactory(data, throwOnRead);
|
||||
ConnectionInfo ci = new ConnectionInfo(factory, OwnerUri, StandardConnectionDetails);
|
||||
ci.ConnectionTypeToConnectionMap[ConnectionType.Default] = factory.CreateSqlConnection(null);
|
||||
return ci;
|
||||
}
|
||||
|
||||
public static ConnectionInfo CreateConnectedConnectionInfo(TestResultSet[] data, bool throwOnRead, string type = ConnectionType.Default)
|
||||
|
||||
Reference in New Issue
Block a user