tests(infra,api): refactor to allow further config of TestApi
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user