Files
fiscalos/tests/FiscalOS.API.Tests/Integration/IntegrationTest.cs
T

39 lines
1.1 KiB
C#
Raw Normal View History

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-01 11:31:46 -06:00
await ExecuteDbContextAsync(static async context =>
2026-02-01 08:07:23 -06:00
{
2026-02-01 11:31:46 -06:00
await context.Database.EnsureCreatedAsync();
});
2026-02-01 08:07:23 -06:00
}
2026-02-01 11:31:46 -06:00
protected async Task ExecuteDbContextAsync(Func<DbContext, Task> action)
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>();
await action(context);
2026-02-01 08:07:23 -06:00
}
2026-02-01 11:31:46 -06:00
protected async Task<T> ExecuteDbContextAsync<T>(Func<DbContext, Task<T>> action)
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>();
return await action(context);
}
public async ValueTask DisposeAsync()
{
await ExecuteDbContextAsync(static async context =>
{
await context.Database.EnsureDeletedAsync();
});
2026-02-01 08:07:23 -06:00
GC.SuppressFinalize(this);
}
}