using System.Collections.Generic; using Npgsql; using pgLabII.Model; using pgLabII.PgUtils.ConnectionStrings; using pgLabII.Services; using Xunit; namespace pgLabII.Tests.Services; public class ServerConfigurationMappingTests { [Fact] public void ToDescriptor_Basic_MapsExpectedFields() { ServerConfigurationEntity cfg = new() { Name = "Prod", Host = "db.example.com", Port = 5433, InitialDatabase = "appdb", SslMode = SslMode.Require, UserName = "alice", Password = "secret" }; var extra = new Dictionary{{"search_path","public"}}; var d = ServerConfigurationMapping.ToDescriptor(cfg, applicationName: "pgLabII", timeoutSeconds: 15, extraProperties: extra); Assert.Single(d.Hosts); Assert.Equal("db.example.com", d.Hosts[0].Host); Assert.Equal((ushort)5433, d.Hosts[0].Port); Assert.Equal("appdb", d.Database); Assert.Equal("alice", d.Username); Assert.Equal("secret", d.Password); Assert.Equal(SslMode.Require, d.SslMode); Assert.Equal("pgLabII", d.ApplicationName); Assert.Equal(15, d.TimeoutSeconds); Assert.True(d.Properties.ContainsKey("search_path")); Assert.Equal("public", d.Properties["search_path"]); } [Fact] public void ToDescriptor_OmitsEmptyFields() { ServerConfigurationEntity cfg = new () { Name = "Empty", Host = "", InitialDatabase = "", UserName = "", Password = "", }; var d = ServerConfigurationMapping.ToDescriptor(cfg); Assert.Empty(d.Hosts); Assert.Null(d.Database); Assert.Null(d.Username); Assert.Null(d.Password); } [Fact] public void FromDescriptor_CreatesNew_UsesFirstHost() { var desc = new ConnectionDescriptor { Hosts = new [] { new HostEndpoint{ Host = "host1", Port = 5432 }, new HostEndpoint{ Host = "host2", Port = 5434 } }, Database = "stagedb", Username = "bob", Password = "pwd", SslMode = SslMode.VerifyFull }; ServerConfigurationEntity cfg = ServerConfigurationMapping.FromDescriptor(desc); Assert.Equal("host1", cfg.Host); Assert.Equal((ushort)5432, cfg.Port); Assert.Equal("stagedb", cfg.InitialDatabase); Assert.Equal(SslMode.VerifyFull, cfg.SslMode); Assert.Equal("bob", cfg.UserName); Assert.Equal("pwd", cfg.Password); } [Fact] public void FromDescriptor_UpdatesExisting_PreservesMissing() { ServerConfigurationEntity existing = new() { Name = "Existing", Host = "keep-host", Port = 5432, InitialDatabase = "keepdb", SslMode = SslMode.Prefer, UserName = "keepuser", Password = "keeppwd", }; // Descriptor missing db and user/pass and sslmode var desc = new ConnectionDescriptor { Hosts = new [] { new HostEndpoint{ Host = "new-host" } } }; var cfg = ServerConfigurationMapping.FromDescriptor(desc, existing); Assert.Equal("new-host", cfg.Host); Assert.Equal((ushort)5432, cfg.Port); // unchanged Assert.Equal("keepdb", cfg.InitialDatabase); // preserved Assert.Equal(SslMode.Prefer, cfg.SslMode); // preserved Assert.Equal("keepuser", cfg.UserName); // preserved Assert.Equal("keeppwd", cfg.Password); // preserved } [Fact] public void Roundtrip_Basic() { ServerConfigurationEntity cfg = new() { Name = "Round", Host = "localhost", Port = 5432, InitialDatabase = "postgres", SslMode = SslMode.Allow, UserName = "me", Password = "pw", }; var d = ServerConfigurationMapping.ToDescriptor(cfg); var cfg2 = ServerConfigurationMapping.FromDescriptor(d); Assert.Equal(cfg.Host, cfg2.Host); Assert.Equal(cfg.Port, cfg2.Port); Assert.Equal(cfg.InitialDatabase, cfg2.InitialDatabase); Assert.Equal(cfg.SslMode, cfg2.SslMode); Assert.Equal(cfg.UserName, cfg2.UserName); Assert.Equal(cfg.Password, cfg2.Password); } }