Added tests, need to go through them manually to check if they make sense and are correct later
This commit is contained in:
@@ -0,0 +1,51 @@
|
||||
using Lutra.Application.Interfaces;
|
||||
using Lutra.Infrastructure.Sql;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
namespace Lutra.API.IntegrationTests.Infrastructure;
|
||||
|
||||
/// <summary>
|
||||
/// Base class for integration tests. Provides a shared factory and helper methods
|
||||
/// to seed and reset the database between tests.
|
||||
/// </summary>
|
||||
public abstract class IntegrationTestBase : IClassFixture<LutraApiFactory>, IAsyncLifetime
|
||||
{
|
||||
protected readonly LutraApiFactory Factory;
|
||||
protected readonly HttpClient Client;
|
||||
|
||||
protected IntegrationTestBase(LutraApiFactory factory)
|
||||
{
|
||||
Factory = factory;
|
||||
Client = factory.CreateClient();
|
||||
}
|
||||
|
||||
/// <summary>Seed data or perform setup before each test.</summary>
|
||||
public virtual Task InitializeAsync()
|
||||
{
|
||||
Factory.EnsureSchemaCreated();
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
/// <summary>Reset database state after each test.</summary>
|
||||
public async Task DisposeAsync()
|
||||
{
|
||||
using var scope = Factory.Services.CreateScope();
|
||||
var db = scope.ServiceProvider.GetRequiredService<ILutraDbContext>() 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<T> SeedAsync<T>(T entity) where T : class
|
||||
{
|
||||
using var scope = Factory.Services.CreateScope();
|
||||
var db = scope.ServiceProvider.GetRequiredService<ILutraDbContext>() as LutraDbContext;
|
||||
db!.Set<T>().Add(entity);
|
||||
await db.SaveChangesAsync(CancellationToken.None);
|
||||
return entity;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
using Lutra.Application.Interfaces;
|
||||
using Lutra.Infrastructure.Sql;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.AspNetCore.Mvc.Testing;
|
||||
using Microsoft.Data.Sqlite;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.DependencyInjection.Extensions;
|
||||
|
||||
namespace Lutra.API.IntegrationTests.Infrastructure;
|
||||
|
||||
/// <summary>
|
||||
/// Custom WebApplicationFactory that replaces the PostgreSQL database with SQLite in-memory
|
||||
/// so that integration tests can run without a live database server.
|
||||
/// A single SqliteConnection is kept open for the lifetime of the factory so that
|
||||
/// all DI scopes share the same in-memory database.
|
||||
/// </summary>
|
||||
public class LutraApiFactory : WebApplicationFactory<Program>
|
||||
{
|
||||
// Opened immediately so it is ready when ConfigureWebHost runs.
|
||||
private readonly SqliteConnection _connection = new("Data Source=:memory:");
|
||||
private bool _schemaCreated;
|
||||
|
||||
public LutraApiFactory()
|
||||
{
|
||||
_connection.Open();
|
||||
}
|
||||
|
||||
/// <summary>Ensures the SQLite schema is created. Call once before the first test.</summary>
|
||||
public void EnsureSchemaCreated()
|
||||
{
|
||||
if (_schemaCreated) return;
|
||||
|
||||
using var scope = Services.CreateScope();
|
||||
var db = (LutraDbContext)scope.ServiceProvider.GetRequiredService<ILutraDbContext>();
|
||||
db.Database.EnsureCreated();
|
||||
_schemaCreated = true;
|
||||
}
|
||||
|
||||
protected override void ConfigureWebHost(IWebHostBuilder builder)
|
||||
{
|
||||
builder.ConfigureServices(services =>
|
||||
{
|
||||
// EF Core 10 stores provider configuration in IDbContextOptionsConfiguration<T>
|
||||
// descriptors (one per AddDbContext call). All four registration types must be
|
||||
// removed so neither Npgsql options nor its provider services survive into the
|
||||
// SQLite registration.
|
||||
services.RemoveAll<ILutraDbContext>();
|
||||
services.RemoveAll<LutraDbContext>();
|
||||
services.RemoveAll<DbContextOptions<LutraDbContext>>();
|
||||
services.RemoveAll<DbContextOptions>();
|
||||
services.RemoveAll(typeof(IDbContextOptionsConfiguration<LutraDbContext>));
|
||||
|
||||
// Register SQLite using the shared open connection.
|
||||
services.AddDbContext<ILutraDbContext, LutraDbContext>(options =>
|
||||
options.UseSqlite(_connection));
|
||||
});
|
||||
|
||||
builder.UseEnvironment("Testing");
|
||||
}
|
||||
|
||||
protected override void Dispose(bool disposing)
|
||||
{
|
||||
base.Dispose(disposing);
|
||||
if (disposing)
|
||||
_connection.Dispose();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user