mirror of
https://github.com/ckaczor/sqltoolsservice.git
synced 2026-02-16 10:58:30 -05:00
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:
@@ -169,6 +169,16 @@ namespace Microsoft.SqlTools.ServiceLayer.Connection
|
|||||||
|
|
||||||
ownerToConnectionMap[connectionParams.OwnerUri] = connectionInfo;
|
ownerToConnectionMap[connectionParams.OwnerUri] = connectionInfo;
|
||||||
|
|
||||||
|
// Update with the actual database name in connectionInfo and result
|
||||||
|
// Doing this here as we know the connection is open - expect to do this only on connecting
|
||||||
|
connectionInfo.ConnectionDetails.DatabaseName = connectionInfo.SqlConnection.Database;
|
||||||
|
response.ConnectionSummary = new ConnectionSummary()
|
||||||
|
{
|
||||||
|
ServerName = connectionInfo.ConnectionDetails.ServerName,
|
||||||
|
DatabaseName = connectionInfo.ConnectionDetails.DatabaseName,
|
||||||
|
UserName = connectionInfo.ConnectionDetails.UserName,
|
||||||
|
};
|
||||||
|
|
||||||
// invoke callback notifications
|
// invoke callback notifications
|
||||||
foreach (var activity in this.onConnectionActivities)
|
foreach (var activity in this.onConnectionActivities)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -19,5 +19,10 @@ namespace Microsoft.SqlTools.ServiceLayer.Connection.Contracts
|
|||||||
/// Gets or sets any connection error messages
|
/// Gets or sets any connection error messages
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public string Messages { get; set; }
|
public string Messages { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets or sets the actual Connection established, including Database Name
|
||||||
|
/// </summary>
|
||||||
|
public ConnectionSummary ConnectionSummary { get; set; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -52,6 +52,7 @@ namespace Microsoft.SqlTools.ServiceLayer.Test.Connection
|
|||||||
return connectionMock.Object;
|
return connectionMock.Object;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
/// Verify that we can connect to the default database when no database name is
|
/// Verify that we can connect to the default database when no database name is
|
||||||
/// provided as a parameter.
|
/// provided as a parameter.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -75,6 +76,43 @@ namespace Microsoft.SqlTools.ServiceLayer.Test.Connection
|
|||||||
Assert.NotEmpty(connectionResult.ConnectionId);
|
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>
|
/// <summary>
|
||||||
/// Verify that when a connection is started for a URI with an already existing
|
/// Verify that when a connection is started for a URI with an already existing
|
||||||
/// connection, we disconnect first before connecting.
|
/// connection, we disconnect first before connecting.
|
||||||
|
|||||||
Reference in New Issue
Block a user