Centralize Postgres connection string policy across all modules
Extract connection string building into PostgresConnectionStringPolicy so all services use consistent pooling, application_name, and timeout settings. Adopt the new policy in 20+ module DataSource/ServiceCollectionExtensions classes. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,66 @@
|
||||
using FluentAssertions;
|
||||
using Npgsql;
|
||||
using StellaOps.Infrastructure.Postgres.Connections;
|
||||
using StellaOps.Infrastructure.Postgres.Options;
|
||||
using StellaOps.TestKit;
|
||||
using Xunit;
|
||||
|
||||
namespace StellaOps.Infrastructure.Postgres.Tests;
|
||||
|
||||
[Trait("Category", TestCategories.Unit)]
|
||||
public sealed class PostgresConnectionStringPolicyTests
|
||||
{
|
||||
[Fact]
|
||||
public void Build_applies_runtime_attribution_and_pooling_policy()
|
||||
{
|
||||
var options = new PostgresOptions
|
||||
{
|
||||
ConnectionString = "Host=localhost;Database=stellaops;Username=stellaops;Password=stellaops",
|
||||
ApplicationName = "custom-ledger-app",
|
||||
Pooling = false,
|
||||
MinPoolSize = 3,
|
||||
MaxPoolSize = 12,
|
||||
ConnectionIdleLifetimeSeconds = 900,
|
||||
};
|
||||
|
||||
var connectionString = PostgresConnectionStringPolicy.Build(options, "stellaops-findings-ledger");
|
||||
var builder = new NpgsqlConnectionStringBuilder(connectionString);
|
||||
|
||||
builder.ApplicationName.Should().Be("custom-ledger-app");
|
||||
builder.Pooling.Should().BeFalse();
|
||||
builder.MinPoolSize.Should().Be(3);
|
||||
builder.MaxPoolSize.Should().Be(12);
|
||||
builder.ConnectionIdleLifetime.Should().Be(900);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Build_uses_default_application_name_when_none_is_configured()
|
||||
{
|
||||
var options = new PostgresOptions
|
||||
{
|
||||
ConnectionString = "Host=localhost;Database=stellaops;Username=stellaops;Password=stellaops",
|
||||
MinPoolSize = 0,
|
||||
MaxPoolSize = 1,
|
||||
};
|
||||
|
||||
var connectionString = PostgresConnectionStringPolicy.Build(options, "stellaops-policy");
|
||||
var builder = new NpgsqlConnectionStringBuilder(connectionString);
|
||||
|
||||
builder.ApplicationName.Should().Be("stellaops-policy");
|
||||
builder.MinPoolSize.Should().Be(0);
|
||||
builder.MaxPoolSize.Should().Be(1);
|
||||
builder.ConnectionIdleLifetime.Should().Be(300);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("Policy", "stellaops-policy")]
|
||||
[InlineData("PacksRegistry", "stellaops-packs-registry")]
|
||||
[InlineData("IssuerDirectory", "stellaops-issuer-directory")]
|
||||
[InlineData("ReachGraph", "stellaops-reach-graph")]
|
||||
public void BuildDefaultApplicationName_normalizes_module_names(string moduleName, string expected)
|
||||
{
|
||||
var result = PostgresConnectionStringPolicy.BuildDefaultApplicationName(moduleName);
|
||||
|
||||
result.Should().Be(expected);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user