Files
jdescopingtool/NEW/tests/Utils/JdeScoping.ConfigManager.Ui.Tests/ViewModels/Forms/ConnectionStringEntryViewModelTests.cs
T
Joseph Doherty 1fc7792cd1 refactor(configmanager): rename UI project and split test projects
Rename ConfigManager to ConfigManager.Ui to match the Core/CLI/UI project
structure, and split the monolithic test project into Core.Tests,
Cli.Tests, and Ui.Tests to align with the source project organization.
2026-01-28 10:24:36 -05:00

220 lines
6.2 KiB
C#

using JdeScoping.ConfigManager.Core.Models;
using JdeScoping.ConfigManager.Ui.ViewModels.Forms;
namespace JdeScoping.ConfigManager.Ui.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");
}
}