mirror of
https://github.com/ckaczor/sqltoolsservice.git
synced 2026-01-14 01:25:40 -05:00
Fixing the bug with connections on database make restore fail (#473)
* closing the connections that don't need to be open and keeping track of the connections that should stay open
This commit is contained in:
@@ -7,11 +7,9 @@ using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data.SqlClient;
|
||||
using System.Globalization;
|
||||
using System.Linq;
|
||||
using Microsoft.SqlServer.Management.Common;
|
||||
using Microsoft.SqlServer.Management.Smo;
|
||||
using Microsoft.SqlTools.Extensibility;
|
||||
using Microsoft.SqlTools.ServiceLayer.Connection;
|
||||
using Microsoft.SqlTools.ServiceLayer.Connection.Contracts;
|
||||
using Microsoft.SqlTools.ServiceLayer.ObjectExplorer;
|
||||
using Microsoft.SqlTools.ServiceLayer.ObjectExplorer.Contracts;
|
||||
@@ -20,6 +18,7 @@ using Microsoft.SqlTools.ServiceLayer.ObjectExplorer.SmoModel;
|
||||
using Microsoft.SqlTools.ServiceLayer.UnitTests.Utility;
|
||||
using Moq;
|
||||
using Xunit;
|
||||
using Microsoft.SqlTools.ServiceLayer.Connection;
|
||||
|
||||
namespace Microsoft.SqlTools.ServiceLayer.UnitTests.ObjectExplorer
|
||||
{
|
||||
@@ -33,10 +32,12 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.ObjectExplorer
|
||||
private ConnectionDetails defaultConnectionDetails;
|
||||
private ConnectionCompleteParams defaultConnParams;
|
||||
private string fakeConnectionString = "Data Source=server;Initial Catalog=database;Integrated Security=False;User Id=user";
|
||||
private ServerConnection serverConnection = null;
|
||||
|
||||
public NodeTests()
|
||||
{
|
||||
defaultServerInfo = TestObjects.GetTestServerInfo();
|
||||
serverConnection = new ServerConnection(new SqlConnection(fakeConnectionString));
|
||||
|
||||
defaultConnectionDetails = new ConnectionDetails()
|
||||
{
|
||||
@@ -59,15 +60,15 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.ObjectExplorer
|
||||
[Fact]
|
||||
public void ServerNodeConstructorValidatesFields()
|
||||
{
|
||||
Assert.Throws<ArgumentNullException>(() => new ServerNode(null, ServiceProvider));
|
||||
Assert.Throws<ArgumentNullException>(() => new ServerNode(defaultConnParams, null));
|
||||
Assert.Throws<ArgumentNullException>(() => new ServerNode(null, ServiceProvider, serverConnection));
|
||||
Assert.Throws<ArgumentNullException>(() => new ServerNode(defaultConnParams, null, serverConnection));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ServerNodeConstructorShouldSetValuesCorrectly()
|
||||
{
|
||||
// Given a server node with valid inputs
|
||||
ServerNode node = new ServerNode(defaultConnParams, ServiceProvider);
|
||||
ServerNode node = new ServerNode(defaultConnParams, ServiceProvider, serverConnection);
|
||||
// Then expect all fields set correctly
|
||||
Assert.False(node.IsAlwaysLeaf, "Server node should never be a leaf");
|
||||
Assert.Equal(defaultConnectionDetails.ServerName, node.NodeValue);
|
||||
@@ -99,7 +100,7 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.ObjectExplorer
|
||||
OwnerUri = defaultOwnerUri
|
||||
};
|
||||
// When querying label
|
||||
string label = new ServerNode(connParams, ServiceProvider).Label;
|
||||
string label = new ServerNode(connParams, ServiceProvider, serverConnection).Label;
|
||||
// Then only server name and version shown
|
||||
string expectedLabel = defaultConnectionDetails.ServerName + " (SQL Server " + defaultServerInfo.ServerVersion + ")";
|
||||
Assert.Equal(expectedLabel, label);
|
||||
@@ -111,7 +112,7 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.ObjectExplorer
|
||||
defaultServerInfo.IsCloud = true;
|
||||
|
||||
// Given a server node for a cloud DB, with master name
|
||||
ServerNode node = new ServerNode(defaultConnParams, ServiceProvider);
|
||||
ServerNode node = new ServerNode(defaultConnParams, ServiceProvider, serverConnection);
|
||||
// Then expect label to not include db name
|
||||
string expectedLabel = defaultConnectionDetails.ServerName + " (SQL Server " + defaultServerInfo.ServerVersion + " - "
|
||||
+ defaultConnectionDetails.UserName + ")";
|
||||
@@ -120,7 +121,7 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.ObjectExplorer
|
||||
// But given a server node for a cloud DB that's not master
|
||||
defaultConnectionDetails.DatabaseName = "NotMaster";
|
||||
defaultConnParams.ConnectionSummary.DatabaseName = defaultConnectionDetails.DatabaseName;
|
||||
node = new ServerNode(defaultConnParams, ServiceProvider);
|
||||
node = new ServerNode(defaultConnParams, ServiceProvider, serverConnection);
|
||||
|
||||
// Then expect label to include db name
|
||||
expectedLabel = defaultConnectionDetails.ServerName + " (SQL Server " + defaultServerInfo.ServerVersion + " - "
|
||||
@@ -132,7 +133,7 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.ObjectExplorer
|
||||
public void ToNodeInfoIncludeAllFields()
|
||||
{
|
||||
// Given a server connection
|
||||
ServerNode node = new ServerNode(defaultConnParams, ServiceProvider);
|
||||
ServerNode node = new ServerNode(defaultConnParams, ServiceProvider, serverConnection);
|
||||
// When converting to NodeInfo
|
||||
NodeInfo info = node.ToNodeInfo();
|
||||
// Then all fields should match
|
||||
@@ -204,7 +205,6 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.ObjectExplorer
|
||||
public void ServerNodeContextShouldIncludeServer()
|
||||
{
|
||||
// given a successful Server creation
|
||||
SetupAndRegisterTestConnectionService();
|
||||
Server smoServer = new Server(new ServerConnection(new SqlConnection(fakeConnectionString)));
|
||||
ServerNode node = SetupServerNodeWithServer(smoServer);
|
||||
|
||||
@@ -223,10 +223,8 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.ObjectExplorer
|
||||
public void ServerNodeContextShouldSetErrorMessageIfSqlConnectionIsNull()
|
||||
{
|
||||
// given a connectionInfo with no SqlConnection to use for queries
|
||||
ConnectionService connService = SetupAndRegisterTestConnectionService();
|
||||
connService.OwnerToConnectionMap.Remove(defaultOwnerUri);
|
||||
|
||||
Server smoServer = new Server(new ServerConnection(new SqlConnection(fakeConnectionString)));
|
||||
Server smoServer = null;
|
||||
ServerNode node = SetupServerNodeWithServer(smoServer);
|
||||
|
||||
// When I get the context for a ServerNode
|
||||
@@ -234,17 +232,12 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.ObjectExplorer
|
||||
|
||||
// Then I expect it to be in an error state
|
||||
Assert.Null(context);
|
||||
Assert.Equal(
|
||||
string.Format(CultureInfo.CurrentCulture, SR.ServerNodeConnectionError, defaultConnectionDetails.ServerName),
|
||||
node.ErrorStateMessage);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ServerNodeContextShouldSetErrorMessageIfConnFailureExceptionThrown()
|
||||
{
|
||||
// given a connectionInfo with no SqlConnection to use for queries
|
||||
SetupAndRegisterTestConnectionService();
|
||||
|
||||
Server smoServer = new Server(new ServerConnection(new SqlConnection(fakeConnectionString)));
|
||||
string expectedMsg = "ConnFailed!";
|
||||
ServerNode node = SetupServerNodeWithExceptionCreator(new ConnectionFailureException(expectedMsg));
|
||||
@@ -263,8 +256,6 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.ObjectExplorer
|
||||
public void ServerNodeContextShouldSetErrorMessageIfExceptionThrown()
|
||||
{
|
||||
// given a connectionInfo with no SqlConnection to use for queries
|
||||
SetupAndRegisterTestConnectionService();
|
||||
|
||||
Server smoServer = new Server(new ServerConnection(new SqlConnection(fakeConnectionString)));
|
||||
string expectedMsg = "Failed!";
|
||||
ServerNode node = SetupServerNodeWithExceptionCreator(new Exception(expectedMsg));
|
||||
@@ -283,7 +274,6 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.ObjectExplorer
|
||||
public void QueryContextShouldNotCallOpenOnAlreadyOpenConnection()
|
||||
{
|
||||
// given a server connection that will state its connection is open
|
||||
SetupAndRegisterTestConnectionService();
|
||||
Server smoServer = new Server(new ServerConnection(new SqlConnection(fakeConnectionString)));
|
||||
Mock<SmoWrapper> wrapper = SetupSmoWrapperForIsOpenTest(smoServer, isOpen: true);
|
||||
|
||||
@@ -301,7 +291,7 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.ObjectExplorer
|
||||
{
|
||||
Mock<SmoWrapper> wrapper = new Mock<SmoWrapper>();
|
||||
int count = 0;
|
||||
wrapper.Setup(c => c.CreateServer(It.IsAny<SqlConnection>()))
|
||||
wrapper.Setup(c => c.CreateServer(It.IsAny<ServerConnection>()))
|
||||
.Returns(() => smoServer);
|
||||
wrapper.Setup(c => c.IsConnectionOpen(It.IsAny<Server>()))
|
||||
.Returns(() => isOpen);
|
||||
@@ -315,7 +305,6 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.ObjectExplorer
|
||||
public void QueryContextShouldReopenClosedConnectionWhenGettingServer()
|
||||
{
|
||||
// given a server connection that will state its connection is closed
|
||||
SetupAndRegisterTestConnectionService();
|
||||
Server smoServer = new Server(new ServerConnection(new SqlConnection(fakeConnectionString)));
|
||||
Mock<SmoWrapper> wrapper = SetupSmoWrapperForIsOpenTest(smoServer, isOpen: false);
|
||||
|
||||
@@ -333,7 +322,6 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.ObjectExplorer
|
||||
public void QueryContextShouldReopenClosedConnectionWhenGettingParent()
|
||||
{
|
||||
// given a server connection that will state its connection is closed
|
||||
SetupAndRegisterTestConnectionService();
|
||||
Server smoServer = new Server(new ServerConnection(new SqlConnection(fakeConnectionString)));
|
||||
Mock<SmoWrapper> wrapper = SetupSmoWrapperForIsOpenTest(smoServer, isOpen: false);
|
||||
|
||||
@@ -362,7 +350,7 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.ObjectExplorer
|
||||
private ServerNode SetupServerNodeWithServer(Server smoServer)
|
||||
{
|
||||
Mock<SmoWrapper> creator = new Mock<SmoWrapper>();
|
||||
creator.Setup(c => c.CreateServer(It.IsAny<SqlConnection>()))
|
||||
creator.Setup(c => c.CreateServer(It.IsAny<ServerConnection>()))
|
||||
.Returns(() => smoServer);
|
||||
creator.Setup(c => c.IsConnectionOpen(It.IsAny<Server>()))
|
||||
.Returns(() => true);
|
||||
@@ -373,7 +361,7 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.ObjectExplorer
|
||||
private ServerNode SetupServerNodeWithExceptionCreator(Exception ex)
|
||||
{
|
||||
Mock<SmoWrapper> creator = new Mock<SmoWrapper>();
|
||||
creator.Setup(c => c.CreateServer(It.IsAny<SqlConnection>()))
|
||||
creator.Setup(c => c.CreateServer(It.IsAny<ServerConnection>()))
|
||||
.Throws(ex);
|
||||
creator.Setup(c => c.IsConnectionOpen(It.IsAny<Server>()))
|
||||
.Returns(() => false);
|
||||
@@ -384,7 +372,7 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.ObjectExplorer
|
||||
|
||||
private ServerNode SetupServerNodeWithCreator(SmoWrapper creator)
|
||||
{
|
||||
ServerNode node = new ServerNode(defaultConnParams, ServiceProvider);
|
||||
ServerNode node = new ServerNode(defaultConnParams, ServiceProvider, new ServerConnection(new SqlConnection(fakeConnectionString)));
|
||||
node.SmoWrapper = creator;
|
||||
return node;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user