up
This commit is contained in:
@@ -0,0 +1,101 @@
|
||||
using FluentAssertions;
|
||||
using StellaOps.Infrastructure.Postgres.Testing;
|
||||
using Testcontainers.PostgreSql;
|
||||
using Xunit;
|
||||
|
||||
namespace StellaOps.Infrastructure.Postgres.Tests;
|
||||
|
||||
/// <summary>
|
||||
/// Integration tests for PostgresFixture.
|
||||
/// Uses Testcontainers to spin up a real PostgreSQL instance.
|
||||
/// </summary>
|
||||
public sealed class PostgresFixtureTests : IAsyncLifetime
|
||||
{
|
||||
private PostgreSqlContainer? _container;
|
||||
|
||||
public async Task InitializeAsync()
|
||||
{
|
||||
_container = new PostgreSqlBuilder()
|
||||
.WithImage("postgres:16-alpine")
|
||||
.Build();
|
||||
|
||||
await _container.StartAsync();
|
||||
}
|
||||
|
||||
public async Task DisposeAsync()
|
||||
{
|
||||
if (_container != null)
|
||||
{
|
||||
await _container.DisposeAsync();
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Initialize_CreatesSchema()
|
||||
{
|
||||
// Arrange
|
||||
var connectionString = _container!.GetConnectionString();
|
||||
await using var fixture = PostgresFixtureFactory.Create(connectionString, nameof(Initialize_CreatesSchema));
|
||||
|
||||
// Act
|
||||
await fixture.InitializeAsync();
|
||||
|
||||
// Assert
|
||||
var options = fixture.CreateOptions();
|
||||
options.SchemaName.Should().StartWith("test_initialize_createsschema_");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task TruncateAllTables_ClearsTables()
|
||||
{
|
||||
// Arrange
|
||||
var connectionString = _container!.GetConnectionString();
|
||||
await using var fixture = PostgresFixtureFactory.CreateRandom(connectionString);
|
||||
await fixture.InitializeAsync();
|
||||
|
||||
// Create a test table and insert data
|
||||
await fixture.ExecuteSqlAsync($"""
|
||||
CREATE TABLE {fixture.SchemaName}.test_table (
|
||||
id SERIAL PRIMARY KEY,
|
||||
name TEXT NOT NULL
|
||||
);
|
||||
INSERT INTO {fixture.SchemaName}.test_table (name) VALUES ('test1'), ('test2');
|
||||
""");
|
||||
|
||||
// Act
|
||||
await fixture.TruncateAllTablesAsync();
|
||||
|
||||
// Assert - table should be empty
|
||||
await using var conn = new Npgsql.NpgsqlConnection(connectionString);
|
||||
await conn.OpenAsync();
|
||||
await using var cmd = new Npgsql.NpgsqlCommand(
|
||||
$"SELECT COUNT(*) FROM {fixture.SchemaName}.test_table", conn);
|
||||
var count = await cmd.ExecuteScalarAsync();
|
||||
count.Should().Be(0L);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Dispose_DropsSchema()
|
||||
{
|
||||
// Arrange
|
||||
var connectionString = _container!.GetConnectionString();
|
||||
string schemaName;
|
||||
|
||||
// Create and dispose fixture
|
||||
{
|
||||
await using var fixture = PostgresFixtureFactory.CreateRandom(connectionString);
|
||||
await fixture.InitializeAsync();
|
||||
schemaName = fixture.SchemaName;
|
||||
}
|
||||
|
||||
// Assert - schema should not exist
|
||||
await using var conn = new Npgsql.NpgsqlConnection(connectionString);
|
||||
await conn.OpenAsync();
|
||||
await using var cmd = new Npgsql.NpgsqlCommand(
|
||||
"SELECT EXISTS(SELECT 1 FROM information_schema.schemata WHERE schema_name = @name)",
|
||||
conn);
|
||||
cmd.Parameters.AddWithValue("name", schemaName);
|
||||
var exists = await cmd.ExecuteScalarAsync();
|
||||
exists.Should().Be(false);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user