tests(infra,api): refactor to allow further config of TestApi

This commit is contained in:
Stevan Freeborn
2026-03-08 09:15:45 -05:00
parent 870baf7b6c
commit 1a4f7fac8d
12 changed files with 123 additions and 83 deletions
@@ -2,51 +2,17 @@ namespace FiscalOS.API.Tests.Integration;
public abstract class IntegrationTest(TestApi testApi) : IClassFixture<TestApi>, IAsyncLifetime
{
public HttpClient Client => testApi.CreateClient();
protected TestApi Api => testApi;
protected HttpClient Client => testApi.CreateClient();
public async ValueTask InitializeAsync()
{
await ExecuteAsync(static async (context, ct) =>
{
await context.Database.EnsureCreatedAsync(ct);
}, TestContext.Current.CancellationToken);
}
protected async Task ExecuteAsync(Func<DbContext, CancellationToken, Task> action, CancellationToken ct)
{
await using var scope = testApi.Services.CreateAsyncScope();
var context = scope.ServiceProvider.GetRequiredService<AppDbContext>();
await action(context, ct);
}
protected async Task ExecuteAsync(Func<DbContext, CancellationToken, IServiceProvider, Task> action, CancellationToken ct)
{
await using var scope = testApi.Services.CreateAsyncScope();
var context = scope.ServiceProvider.GetRequiredService<AppDbContext>();
await action(context, ct, scope.ServiceProvider);
}
protected async Task<T> ExecuteAsync<T>(Func<DbContext, CancellationToken, Task<T>> action, CancellationToken ct)
{
await using var scope = testApi.Services.CreateAsyncScope();
var context = scope.ServiceProvider.GetRequiredService<AppDbContext>();
return await action(context, ct);
}
protected async Task<T> ExecuteAsync<T>(Func<DbContext, CancellationToken, IServiceProvider, Task<T>> action, CancellationToken ct)
{
await using var scope = testApi.Services.CreateAsyncScope();
var context = scope.ServiceProvider.GetRequiredService<AppDbContext>();
return await action(context, ct, scope.ServiceProvider);
await Api.EnsureDbCreatedAsync();
}
public async ValueTask DisposeAsync()
{
await ExecuteAsync(static async (context, ct) =>
{
await context.Database.EnsureDeletedAsync(ct);
}, TestContext.Current.CancellationToken);
await Api.EnsureDbDeletedAsync();
GC.SuppressFinalize(this);
}
}