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);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
<?xml version="1.0" ?>
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net10.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
<LangVersion>preview</LangVersion>
|
||||
<IsPackable>false</IsPackable>
|
||||
<IsTestProject>true</IsTestProject>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="FluentAssertions" Version="6.12.0" />
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.1" />
|
||||
<PackageReference Include="Moq" Version="4.20.70" />
|
||||
<PackageReference Include="Testcontainers.PostgreSql" Version="4.1.0" />
|
||||
<PackageReference Include="xunit" Version="2.9.2" />
|
||||
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2">
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="coverlet.collector" Version="6.0.4">
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
</PackageReference>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\StellaOps.Infrastructure.Postgres\StellaOps.Infrastructure.Postgres.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
Reference in New Issue
Block a user