ConnectionStringService
This commit is contained in:
parent
0ad39f6ae0
commit
b7631ecdd0
2 changed files with 163 additions and 0 deletions
|
|
@ -0,0 +1,73 @@
|
|||
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);
|
||||
}
|
||||
|
||||
[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);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue