Connect should return actual DB Name (#42)

- On Connecting to a server with no DB specified, we will actually get a connection to Master or some default DB.
- This DB should be used when notifying others of a new connection, and when returning information to the caller so that the correct name can be displayed in the UI.
- Added basic unit tests to cover this scenario
This commit is contained in:
Kevin Cunnane
2016-09-12 11:32:02 -07:00
committed by GitHub
parent 9e492f19f9
commit 14b6348b20
3 changed files with 53 additions and 0 deletions

View File

@@ -52,6 +52,7 @@ namespace Microsoft.SqlTools.ServiceLayer.Test.Connection
return connectionMock.Object;
}
/// <summary>
/// Verify that we can connect to the default database when no database name is
/// provided as a parameter.
/// </summary>
@@ -75,6 +76,43 @@ namespace Microsoft.SqlTools.ServiceLayer.Test.Connection
Assert.NotEmpty(connectionResult.ConnectionId);
}
/// <summary>
/// Verify that we can connect to the default database when no database name is
/// provided as a parameter.
/// </summary>
[Theory]
[InlineDataAttribute("master")]
[InlineDataAttribute("nonMasterDb")]
public void ConnectToDefaultDatabaseRespondsWithActualDbName(string expectedDbName)
{
// Given connecting with empty database name will return the expected DB name
var connectionMock = new Mock<DbConnection> { CallBase = true };
connectionMock.Setup(c => c.Database).Returns(expectedDbName);
var mockFactory = new Mock<ISqlConnectionFactory>();
mockFactory.Setup(factory => factory.CreateSqlConnection(It.IsAny<string>()))
.Returns(connectionMock.Object);
var connectionService = new ConnectionService(mockFactory.Object);
// When I connect with an empty DB name
var connectionDetails = TestObjects.GetTestConnectionDetails();
connectionDetails.DatabaseName = string.Empty;
var connectionResult =
connectionService
.Connect(new ConnectParams()
{
OwnerUri = "file:///my/test/file.sql",
Connection = connectionDetails
});
// Then I expect connection to succeed and the Summary to include the correct DB name
Assert.NotEmpty(connectionResult.ConnectionId);
Assert.NotNull(connectionResult.ConnectionSummary);
Assert.Equal(expectedDbName, connectionResult.ConnectionSummary.DatabaseName);
}
/// <summary>
/// Verify that when a connection is started for a URI with an already existing
/// connection, we disconnect first before connecting.