Files
vigilcare-records/VigilCareRecordsAPI.Tests/Fixtures/ApiFixture.cs
T
Trent 5a175e3fba
CI / changes (push) Successful in 1m4s
CI / backend (push) Failing after 0s
CI / frontend (push) Failing after 0s
Align CI/CD with shared infra/ci-workflows and drop compose-based CI.
Replace hand-rolled jobs with @v1 callers, capped bake/migrate CD, and remove the docker-compose.ci.yml bridge workaround.
2026-10-06 19:10:58 +08:00

54 lines
2.3 KiB
C#

using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc.Testing;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using StackExchange.Redis;
public class ApiFixture : WebApplicationFactory<Program>, IAsyncLifetime
{
// Override configuration to point at a test database — never run tests against
// the development database; a botched rollback could corrupt seed data.
//
// Defaults assume Postgres/Redis on loopback (local `docker compose up -d
// postgres redis`). CI (infra/ci-workflows dotnet8-ci) sets
// CONNECTIONSTRINGS__DEFAULTCONNECTION / REDIS__CONNECTIONSTRING to the
// service-container DNS names (Host=postgres, redis:6379).
protected override void ConfigureWebHost(IWebHostBuilder builder)
{
builder.UseEnvironment("Testing");
builder.ConfigureAppConfiguration((_, config) =>
{
var connectionString = Environment.GetEnvironmentVariable("CONNECTIONSTRINGS__DEFAULTCONNECTION")
?? "Host=localhost;Port=5437;Database=vigilcare_records_test;Username=postgres;Password=password";
var redisConnectionString = Environment.GetEnvironmentVariable("REDIS__CONNECTIONSTRING")
?? "localhost:6383,defaultDatabase=1,allowAdmin=true";
var fhirBaseUrl = Environment.GetEnvironmentVariable("FHIR__BASEURL")
?? "http://localhost/fhir";
config.AddInMemoryCollection(new Dictionary<string, string?>
{
["ConnectionStrings:DefaultConnection"] = connectionString,
["Redis:ConnectionString"] = redisConnectionString,
["Fhir:BaseUrl"] = fhirBaseUrl
});
});
}
public async Task InitializeAsync()
{
// Apply migrations against the test database on first run
using var scope = Services.CreateScope();
var db = scope.ServiceProvider.GetRequiredService<AppDbContext>();
await db.Database.MigrateAsync();
// Flush the test Redis database (db=1) to avoid cross-test cache pollution
var redis = scope.ServiceProvider.GetRequiredService<IConnectionMultiplexer>();
await redis.GetDatabase(1).ExecuteAsync("FLUSHDB");
}
public new async Task DisposeAsync()
{
await base.DisposeAsync();
}
}