Files
jdescopingtool/NEW/tests/JdeScoping.ConfigManager.Tests/ViewModels/Forms/ConnectionStringEntryViewModelTests.cs
T
Joseph Doherty 5ee710d330 feat(configmanager): add dedicated SQL Server port control
Add SqlServerPort property to connection string editor for explicit port
configuration, replacing the need to embed port in server name (e.g.,
localhost,1434). The port control generates the comma-separated format in
the connection string but does not parse it back when loading existing
connection strings.
2026-01-28 09:24:49 -05:00

220 lines
6.1 KiB
C#

using JdeScoping.ConfigManager.Models;
using JdeScoping.ConfigManager.ViewModels.Forms;
namespace JdeScoping.ConfigManager.Tests.ViewModels.Forms;
public class ConnectionStringEntryViewModelTests
{
[Fact]
public void Constructor_InitializesFromModel()
{
// Arrange
var model = new ConnectionStringEntry
{
Name = "TestConnection",
Provider = ConnectionProvider.SqlServer,
Server = "localhost",
SqlServerPort = 1434,
Database = "TestDb",
UserId = "testuser",
Password = "testpass",
Encrypt = "Strict",
TrustServerCertificate = true,
ConnectionTimeout = 45,
ApplicationName = "TestApp"
};
// Act
var sut = new ConnectionStringEntryViewModel(model, () => { });
// Assert
sut.Name.ShouldBe("TestConnection");
sut.Provider.ShouldBe(ConnectionProvider.SqlServer);
sut.Server.ShouldBe("localhost");
sut.SqlServerPort.ShouldBe(1434);
sut.Database.ShouldBe("TestDb");
sut.UserId.ShouldBe("testuser");
sut.Password.ShouldBe("testpass");
sut.Encrypt.ShouldBe("Strict");
sut.TrustServerCertificate.ShouldBeTrue();
sut.ConnectionTimeout.ShouldBe(45);
sut.ApplicationName.ShouldBe("TestApp");
}
[Fact]
public void Constructor_ThrowsOnNullModel()
{
// Act & Assert
Should.Throw<ArgumentNullException>(() => new ConnectionStringEntryViewModel(null!, () => { }));
}
[Fact]
public void Constructor_ThrowsOnNullOnChanged()
{
// Arrange
var model = new ConnectionStringEntry();
// Act & Assert
Should.Throw<ArgumentNullException>(() => new ConnectionStringEntryViewModel(model, null!));
}
[Fact]
public void PropertyChange_UpdatesModel()
{
// Arrange
var model = new ConnectionStringEntry();
var sut = new ConnectionStringEntryViewModel(model, () => { });
// Act
sut.Name = "UpdatedName";
sut.Server = "newserver";
sut.Database = "newdb";
// Assert
model.Name.ShouldBe("UpdatedName");
model.Server.ShouldBe("newserver");
model.Database.ShouldBe("newdb");
}
[Fact]
public void PropertyChange_InvokesOnChanged()
{
// Arrange
var model = new ConnectionStringEntry();
var changedInvoked = false;
var sut = new ConnectionStringEntryViewModel(model, () => changedInvoked = true);
// Act
sut.Name = "NewName";
// Assert
changedInvoked.ShouldBeTrue();
}
[Fact]
public void TogglePasswordVisibility_TogglesIsPasswordVisible()
{
// Arrange
var model = new ConnectionStringEntry();
var sut = new ConnectionStringEntryViewModel(model, () => { });
// Assert initial state
sut.IsPasswordVisible.ShouldBeFalse();
// Act - first toggle
sut.TogglePasswordVisibilityCommand.Execute(null);
// Assert
sut.IsPasswordVisible.ShouldBeTrue();
// Act - second toggle
sut.TogglePasswordVisibilityCommand.Execute(null);
// Assert
sut.IsPasswordVisible.ShouldBeFalse();
}
[Fact]
public void ProviderDisplay_ReturnsCorrectString()
{
// Arrange
var model = new ConnectionStringEntry { Provider = ConnectionProvider.SqlServer };
var sut = new ConnectionStringEntryViewModel(model, () => { });
// Assert
sut.ProviderDisplay.ShouldBe("SqlServer");
// Act
sut.Provider = ConnectionProvider.Oracle;
// Assert
sut.ProviderDisplay.ShouldBe("Oracle");
// Act
sut.Provider = ConnectionProvider.Generic;
// Assert
sut.ProviderDisplay.ShouldBe("Generic");
}
[Fact]
public void ServerDisplay_ReturnsServerForSqlServer()
{
// Arrange
var model = new ConnectionStringEntry
{
Provider = ConnectionProvider.SqlServer,
Server = "sql-server-host"
};
var sut = new ConnectionStringEntryViewModel(model, () => { });
// Assert
sut.ServerDisplay.ShouldBe("sql-server-host");
}
[Fact]
public void ServerDisplay_ReturnsHostForOracle()
{
// Arrange
var model = new ConnectionStringEntry
{
Provider = ConnectionProvider.Oracle,
Host = "oracle-host"
};
var sut = new ConnectionStringEntryViewModel(model, () => { });
// Assert
sut.ServerDisplay.ShouldBe("oracle-host");
}
[Fact]
public void ServerDisplay_ReturnsDashForGeneric()
{
// Arrange
var model = new ConnectionStringEntry
{
Provider = ConnectionProvider.Generic,
RawConnectionString = "some connection string"
};
var sut = new ConnectionStringEntryViewModel(model, () => { });
// Assert
sut.ServerDisplay.ShouldBe("-");
}
[Fact]
public void SqlServerPort_PropertyChange_UpdatesModel()
{
// Arrange
var model = new ConnectionStringEntry();
var changedInvoked = false;
var sut = new ConnectionStringEntryViewModel(model, () => changedInvoked = true);
// Act
sut.SqlServerPort = 1434;
// Assert
model.SqlServerPort.ShouldBe(1434);
changedInvoked.ShouldBeTrue();
}
[Fact]
public void SqlServerPort_PropertyChange_UpdatesGeneratedConnectionString()
{
// Arrange
var model = new ConnectionStringEntry
{
Provider = ConnectionProvider.SqlServer,
Server = "localhost",
Database = "TestDb"
};
var sut = new ConnectionStringEntryViewModel(model, () => { });
// Act
sut.SqlServerPort = 1434;
// Assert
sut.GeneratedConnectionString.ShouldContain("Server=localhost,1434");
}
}