pgLabII/pgLabII.PgUtils.Tests/ConnectionStrings/ConnectionStringServiceTests.cs

82 lines
2.6 KiB
C#
Raw Normal View History

2025-08-30 20:38:59 +02:00
using pgLabII.PgUtils.ConnectionStrings;
namespace pgLabII.PgUtils.Tests.ConnectionStrings;
public class ConnectionStringServiceTests
{
private readonly ConnectionStringService svc = ConnectionStringService.CreateDefault();
[Fact]
public void DetectFormat_Url()
{
var r = svc.DetectFormat("postgresql://user@localhost/db");
Assert.True(r.IsSuccess);
Assert.Equal(ConnStringFormat.Url, r.Value);
2025-08-31 10:22:08 +02:00
}
[Fact]
public void DetectFormat_Jdbc()
{
var r = svc.DetectFormat("jdbc:postgresql://localhost/db");
Assert.True(r.IsSuccess);
Assert.Equal(ConnStringFormat.Jdbc, r.Value);
2025-08-30 20:38:59 +02:00
}
[Fact]
public void DetectFormat_Npgsql()
{
var r = svc.DetectFormat("Host=localhost;Database=db;Username=u");
Assert.True(r.IsSuccess);
Assert.Equal(ConnStringFormat.Npgsql, r.Value);
}
[Fact]
public void DetectFormat_Libpq()
{
var r = svc.DetectFormat("host=localhost dbname=db user=u");
Assert.True(r.IsSuccess);
Assert.Equal(ConnStringFormat.Libpq, r.Value);
}
[Fact]
public void ParseToDescriptor_DispatchesByFormat()
{
var r1 = svc.ParseToDescriptor("host=localhost dbname=db user=u");
Assert.True(r1.IsSuccess);
var r2 = svc.ParseToDescriptor("Host=localhost;Database=db;Username=u");
Assert.True(r2.IsSuccess);
var r3 = svc.ParseToDescriptor("postgresql://u@localhost/db");
Assert.True(r3.IsSuccess);
}
[Fact]
public void Convert_Libpq_to_Npgsql()
{
var input = "host=localhost port=5433 dbname=mydb user=alice password=secret sslmode=require";
var r = svc.Convert(input, ConnStringFormat.Npgsql);
Assert.True(r.IsSuccess);
var s = r.Value;
Assert.Contains("Host=localhost", s);
Assert.Contains("Port=5433", s);
Assert.Contains("Database=mydb", s);
Assert.Contains("Username=alice", s);
Assert.Contains("Password=secret", s);
Assert.Contains("SSL Mode=Require", s);
}
[Fact]
public void Convert_Url_to_Libpq()
{
var input = "postgresql://bob:pwd@host1:5432,host2:5433/db?application_name=cli&sslmode=prefer";
var r = svc.Convert(input, ConnStringFormat.Libpq);
Assert.True(r.IsSuccess);
var s = r.Value;
Assert.Contains("host=host1,host2", s);
// Ports differ (5432 vs 5433), libpq formatter emits a single port only when all ports are the same
Assert.DoesNotContain("port=", s);
Assert.Contains("dbname=db", s);
Assert.Contains("user=bob", s);
Assert.Contains("sslmode=prefer", s);
}
}