using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc.Testing;
using Microsoft.Extensions.Configuration;
using Testcontainers.PostgreSql;
using Xunit;
namespace SCM.IntegrationTests;
///
/// Sobe a API inteira (TestServer) apontando para um PostgreSQL descartável (Testcontainers).
/// As migrations rodam no startup (ambiente Development), criando schema + seed do admin.
///
public sealed class ScmApiFactory : WebApplicationFactory, IAsyncLifetime
{
private readonly PostgreSqlContainer _postgres = new PostgreSqlBuilder()
.WithImage("postgres:16")
.Build();
public string ConnectionString => _postgres.GetConnectionString();
protected override void ConfigureWebHost(IWebHostBuilder builder)
{
builder.UseEnvironment("Development");
builder.ConfigureAppConfiguration((_, config) =>
{
config.AddInMemoryCollection(new Dictionary
{
["ConnectionStrings:Postgres"] = _postgres.GetConnectionString(),
["Migrations:RunOnStartup"] = "true",
});
});
}
async Task IAsyncLifetime.InitializeAsync() => await _postgres.StartAsync();
async Task IAsyncLifetime.DisposeAsync()
{
await _postgres.DisposeAsync();
await base.DisposeAsync();
}
}