2025-08-31 06:49:37 +02:00
|
|
|
|
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()
|
|
|
|
|
|
{
|
2025-08-31 14:25:27 +02:00
|
|
|
|
ServerConfigurationEntity cfg = new()
|
2025-08-31 06:49:37 +02:00
|
|
|
|
{
|
|
|
|
|
|
Name = "Prod",
|
|
|
|
|
|
Host = "db.example.com",
|
|
|
|
|
|
Port = 5433,
|
|
|
|
|
|
InitialDatabase = "appdb",
|
2025-08-31 14:25:27 +02:00
|
|
|
|
SslMode = SslMode.Require,
|
2025-08-31 19:34:27 +02:00
|
|
|
|
UserName = "alice",
|
|
|
|
|
|
Password = "secret"
|
2025-08-31 06:49:37 +02:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
var extra = new Dictionary<string,string>{{"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()
|
|
|
|
|
|
{
|
2025-08-31 14:25:27 +02:00
|
|
|
|
ServerConfigurationEntity cfg = new ()
|
2025-08-31 06:49:37 +02:00
|
|
|
|
{
|
|
|
|
|
|
Name = "Empty",
|
|
|
|
|
|
Host = "",
|
|
|
|
|
|
InitialDatabase = "",
|
2025-08-31 19:34:27 +02:00
|
|
|
|
UserName = "",
|
|
|
|
|
|
Password = "",
|
2025-08-31 06:49:37 +02:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2025-08-31 14:25:27 +02:00
|
|
|
|
ServerConfigurationEntity cfg = ServerConfigurationMapping.FromDescriptor(desc);
|
2025-08-31 06:49:37 +02:00
|
|
|
|
|
|
|
|
|
|
Assert.Equal("host1", cfg.Host);
|
|
|
|
|
|
Assert.Equal((ushort)5432, cfg.Port);
|
|
|
|
|
|
Assert.Equal("stagedb", cfg.InitialDatabase);
|
2025-08-31 14:25:27 +02:00
|
|
|
|
Assert.Equal(SslMode.VerifyFull, cfg.SslMode);
|
2025-08-31 19:34:27 +02:00
|
|
|
|
Assert.Equal("bob", cfg.UserName);
|
|
|
|
|
|
Assert.Equal("pwd", cfg.Password);
|
2025-08-31 06:49:37 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
|
public void FromDescriptor_UpdatesExisting_PreservesMissing()
|
|
|
|
|
|
{
|
2025-08-31 14:25:27 +02:00
|
|
|
|
ServerConfigurationEntity existing = new()
|
2025-08-31 06:49:37 +02:00
|
|
|
|
{
|
|
|
|
|
|
Name = "Existing",
|
|
|
|
|
|
Host = "keep-host",
|
|
|
|
|
|
Port = 5432,
|
|
|
|
|
|
InitialDatabase = "keepdb",
|
2025-08-31 14:25:27 +02:00
|
|
|
|
SslMode = SslMode.Prefer,
|
2025-08-31 19:34:27 +02:00
|
|
|
|
UserName = "keepuser",
|
|
|
|
|
|
Password = "keeppwd",
|
2025-08-31 06:49:37 +02:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// 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
|
2025-08-31 14:25:27 +02:00
|
|
|
|
Assert.Equal(SslMode.Prefer, cfg.SslMode); // preserved
|
2025-08-31 19:34:27 +02:00
|
|
|
|
Assert.Equal("keepuser", cfg.UserName); // preserved
|
|
|
|
|
|
Assert.Equal("keeppwd", cfg.Password); // preserved
|
2025-08-31 06:49:37 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
|
public void Roundtrip_Basic()
|
|
|
|
|
|
{
|
2025-08-31 14:25:27 +02:00
|
|
|
|
ServerConfigurationEntity cfg = new()
|
2025-08-31 06:49:37 +02:00
|
|
|
|
{
|
|
|
|
|
|
Name = "Round",
|
|
|
|
|
|
Host = "localhost",
|
|
|
|
|
|
Port = 5432,
|
|
|
|
|
|
InitialDatabase = "postgres",
|
2025-08-31 14:25:27 +02:00
|
|
|
|
SslMode = SslMode.Allow,
|
2025-08-31 19:34:27 +02:00
|
|
|
|
UserName = "me",
|
|
|
|
|
|
Password = "pw",
|
2025-08-31 06:49:37 +02:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
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);
|
2025-08-31 14:25:27 +02:00
|
|
|
|
Assert.Equal(cfg.SslMode, cfg2.SslMode);
|
2025-08-31 19:34:27 +02:00
|
|
|
|
Assert.Equal(cfg.UserName, cfg2.UserName);
|
|
|
|
|
|
Assert.Equal(cfg.Password, cfg2.Password);
|
2025-08-31 06:49:37 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|