using JdeScoping.ConfigManager.Core.Models; namespace JdeScoping.ConfigManager.Core.Tests.Models; public class ConnectionStringEntryTests { [Fact] public void GenerateConnectionString_SqlServer_ProducesCorrectFormat() { // Arrange var entry = new ConnectionStringEntry { Provider = ConnectionProvider.SqlServer, Server = "localhost\\SQLEXPRESS", Database = "TestDb", UserId = "sa", Password = "secret123", Encrypt = "True", TrustServerCertificate = true, ConnectionTimeout = 60, ApplicationName = "TestApp" }; // Act var result = entry.GenerateConnectionString(); // Assert result.ShouldContain("Server=localhost\\SQLEXPRESS"); result.ShouldContain("Database=TestDb"); result.ShouldContain("User Id=sa"); result.ShouldContain("Password=secret123"); result.ShouldContain("Encrypt=True"); result.ShouldContain("TrustServerCertificate=True"); result.ShouldContain("Connection Timeout=60"); result.ShouldContain("Application Name=TestApp"); } [Fact] public void GenerateConnectionString_SqlServer_OmitsDefaultTimeout() { // Arrange var entry = new ConnectionStringEntry { Provider = ConnectionProvider.SqlServer, Server = "localhost", Database = "TestDb", UserId = "user", Password = "pass", ConnectionTimeout = 30 // Default value }; // Act var result = entry.GenerateConnectionString(); // Assert result.ShouldNotContain("Connection Timeout="); } [Fact] public void GenerateConnectionString_Oracle_ProducesEZConnectFormat() { // Arrange var entry = new ConnectionStringEntry { Provider = ConnectionProvider.Oracle, Host = "oracle-server.example.com", Port = 1522, ServiceName = "ORCL", UserId = "scott", Password = "tiger" }; // Act var result = entry.GenerateConnectionString(); // Assert result.ShouldContain("Data Source=//oracle-server.example.com:1522/ORCL"); result.ShouldContain("User Id=scott"); result.ShouldContain("Password=tiger"); } [Fact] public void GenerateConnectionString_Generic_ReturnsRawString() { // Arrange var rawConnString = "Driver={ODBC Driver};Server=myserver;Database=mydb;"; var entry = new ConnectionStringEntry { Provider = ConnectionProvider.Generic, RawConnectionString = rawConnString }; // Act var result = entry.GenerateConnectionString(); // Assert result.ShouldBe(rawConnString); } [Fact] public void DefaultValues_AreCorrect() { // Act var entry = new ConnectionStringEntry(); // Assert entry.Name.ShouldBe(string.Empty); entry.Provider.ShouldBe(ConnectionProvider.Generic); entry.Server.ShouldBeNull(); entry.SqlServerPort.ShouldBeNull(); entry.Database.ShouldBeNull(); entry.UserId.ShouldBeNull(); entry.Password.ShouldBeNull(); entry.Encrypt.ShouldBe("True"); entry.TrustServerCertificate.ShouldBeFalse(); entry.ConnectionTimeout.ShouldBe(30); entry.ApplicationName.ShouldBeNull(); entry.Host.ShouldBeNull(); entry.Port.ShouldBe(1521); entry.ServiceName.ShouldBeNull(); entry.RawConnectionString.ShouldBeNull(); } [Fact] public void GenerateConnectionString_SqlServer_WithPort_IncludesPortInServer() { // Arrange var entry = new ConnectionStringEntry { Provider = ConnectionProvider.SqlServer, Server = "localhost", SqlServerPort = 1434, Database = "TestDb", UserId = "sa", Password = "secret123" }; // Act var result = entry.GenerateConnectionString(); // Assert result.ShouldContain("Server=localhost,1434"); result.ShouldContain("Database=TestDb"); } [Fact] public void GenerateConnectionString_SqlServer_WithoutPort_NoPortInServer() { // Arrange var entry = new ConnectionStringEntry { Provider = ConnectionProvider.SqlServer, Server = "localhost", SqlServerPort = null, Database = "TestDb", UserId = "sa", Password = "secret123" }; // Act var result = entry.GenerateConnectionString(); // Assert result.ShouldContain("Server=localhost;"); result.ShouldNotContain("Server=localhost,"); } [Fact] public void GenerateConnectionString_SqlServer_WithZeroPort_NoPortInServer() { // Arrange var entry = new ConnectionStringEntry { Provider = ConnectionProvider.SqlServer, Server = "localhost", SqlServerPort = 0, Database = "TestDb", UserId = "sa", Password = "secret123" }; // Act var result = entry.GenerateConnectionString(); // Assert result.ShouldContain("Server=localhost;"); result.ShouldNotContain("Server=localhost,"); } }