2026-02-01 08:07:23 -06:00
|
|
|
namespace FiscalOS.API.Tests.Integration;
|
|
|
|
|
|
|
|
|
|
public abstract class IntegrationTest(TestApi testApi) : IClassFixture<TestApi>, IAsyncLifetime
|
|
|
|
|
{
|
2026-02-01 11:31:46 -06:00
|
|
|
public HttpClient Client => testApi.CreateClient();
|
2026-02-01 08:07:23 -06:00
|
|
|
|
2026-02-01 11:31:46 -06:00
|
|
|
public async ValueTask InitializeAsync()
|
2026-02-01 08:07:23 -06:00
|
|
|
{
|
2026-02-12 20:47:40 -06:00
|
|
|
await ExecuteAsync(static async (context, ct) =>
|
2026-02-01 08:07:23 -06:00
|
|
|
{
|
2026-02-12 20:47:40 -06:00
|
|
|
await context.Database.EnsureCreatedAsync(ct);
|
|
|
|
|
}, TestContext.Current.CancellationToken);
|
2026-02-01 08:07:23 -06:00
|
|
|
}
|
|
|
|
|
|
2026-02-12 20:47:40 -06:00
|
|
|
protected async Task ExecuteAsync(Func<DbContext, CancellationToken, Task> action, CancellationToken ct)
|
2026-02-01 08:07:23 -06:00
|
|
|
{
|
2026-02-01 11:31:46 -06:00
|
|
|
await using var scope = testApi.Services.CreateAsyncScope();
|
|
|
|
|
var context = scope.ServiceProvider.GetRequiredService<AppDbContext>();
|
2026-02-12 20:47:40 -06:00
|
|
|
await action(context, ct);
|
2026-02-01 08:07:23 -06:00
|
|
|
}
|
|
|
|
|
|
2026-02-12 20:47:40 -06:00
|
|
|
protected async Task ExecuteAsync(Func<DbContext, CancellationToken, IServiceProvider, Task> action, CancellationToken ct)
|
2026-02-02 04:49:00 -06:00
|
|
|
{
|
|
|
|
|
await using var scope = testApi.Services.CreateAsyncScope();
|
|
|
|
|
var context = scope.ServiceProvider.GetRequiredService<AppDbContext>();
|
2026-02-12 20:47:40 -06:00
|
|
|
await action(context, ct, scope.ServiceProvider);
|
2026-02-02 04:49:00 -06:00
|
|
|
}
|
|
|
|
|
|
2026-02-12 20:47:40 -06:00
|
|
|
protected async Task<T> ExecuteAsync<T>(Func<DbContext, CancellationToken, Task<T>> action, CancellationToken ct)
|
2026-02-01 08:07:23 -06:00
|
|
|
{
|
2026-02-01 11:31:46 -06:00
|
|
|
await using var scope = testApi.Services.CreateAsyncScope();
|
|
|
|
|
var context = scope.ServiceProvider.GetRequiredService<AppDbContext>();
|
2026-02-12 20:47:40 -06:00
|
|
|
return await action(context, ct);
|
2026-02-01 11:31:46 -06:00
|
|
|
}
|
|
|
|
|
|
2026-02-12 20:47:40 -06:00
|
|
|
protected async Task<T> ExecuteAsync<T>(Func<DbContext, CancellationToken, IServiceProvider, Task<T>> action, CancellationToken ct)
|
2026-02-02 04:49:00 -06:00
|
|
|
{
|
|
|
|
|
await using var scope = testApi.Services.CreateAsyncScope();
|
|
|
|
|
var context = scope.ServiceProvider.GetRequiredService<AppDbContext>();
|
2026-02-12 20:47:40 -06:00
|
|
|
return await action(context, ct, scope.ServiceProvider);
|
2026-02-02 04:49:00 -06:00
|
|
|
}
|
|
|
|
|
|
2026-02-01 11:31:46 -06:00
|
|
|
public async ValueTask DisposeAsync()
|
|
|
|
|
{
|
2026-02-12 20:47:40 -06:00
|
|
|
await ExecuteAsync(static async (context, ct) =>
|
2026-02-01 11:31:46 -06:00
|
|
|
{
|
2026-02-12 20:47:40 -06:00
|
|
|
await context.Database.EnsureDeletedAsync(ct);
|
|
|
|
|
}, TestContext.Current.CancellationToken);
|
2026-02-01 11:31:46 -06:00
|
|
|
|
2026-02-01 08:07:23 -06:00
|
|
|
GC.SuppressFinalize(this);
|
|
|
|
|
}
|
|
|
|
|
}
|