using Lutra.Application.Interfaces; using Lutra.Infrastructure.Sql; using Microsoft.Extensions.DependencyInjection; namespace Lutra.API.IntegrationTests.Infrastructure; /// /// Base class for integration tests. Provides a shared factory and helper methods /// to seed and reset the database between tests. /// public abstract class IntegrationTestBase : IClassFixture, IAsyncLifetime { protected readonly LutraApiFactory Factory; protected readonly HttpClient Client; protected IntegrationTestBase(LutraApiFactory factory) { Factory = factory; Client = factory.CreateClient(); } /// Seed data or perform setup before each test. public virtual Task InitializeAsync() { Factory.EnsureSchemaCreated(); return Task.CompletedTask; } /// Reset database state after each test. public async Task DisposeAsync() { using var scope = Factory.Services.CreateScope(); var db = scope.ServiceProvider.GetRequiredService() as LutraDbContext; if (db is not null) { db.Beoordelingen.RemoveRange(db.Beoordelingen); db.Verspaketten.RemoveRange(db.Verspaketten); db.Supermarkten.RemoveRange(db.Supermarkten); await db.SaveChangesAsync(CancellationToken.None); } } protected async Task SeedAsync(T entity) where T : class { using var scope = Factory.Services.CreateScope(); var db = scope.ServiceProvider.GetRequiredService() as LutraDbContext; db!.Set().Add(entity); await db.SaveChangesAsync(CancellationToken.None); return entity; } }