44 lines
1.7 KiB
C#
44 lines
1.7 KiB
C#
using Microsoft.AspNetCore.Hosting;
|
|
using Microsoft.AspNetCore.Mvc.Testing;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.Extensions.Configuration;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using StackExchange.Redis;
|
|
|
|
public class ApiFixture : WebApplicationFactory<Program>, IAsyncLifetime
|
|
{
|
|
// Override configuration to point at a test database — never run tests against
|
|
// the development database; a botched rollback could corrupt seed data.
|
|
protected override void ConfigureWebHost(IWebHostBuilder builder)
|
|
{
|
|
builder.UseEnvironment("Testing");
|
|
builder.ConfigureAppConfiguration((_, config) =>
|
|
{
|
|
config.AddInMemoryCollection(new Dictionary<string, string?>
|
|
{
|
|
["ConnectionStrings:DefaultConnection"] =
|
|
"Host=localhost;Port=5436;Database=vigilcare_test;Username=postgres;Password=password",
|
|
["Redis:ConnectionString"] = "localhost:6382,defaultDatabase=1,allowAdmin=true"
|
|
});
|
|
});
|
|
}
|
|
|
|
public async Task InitializeAsync()
|
|
{
|
|
// Apply migrations against the test database on first run
|
|
using var scope = Services.CreateScope();
|
|
var db = scope.ServiceProvider.GetRequiredService<AppDbContext>();
|
|
await db.Database.MigrateAsync();
|
|
|
|
// Flush the test Redis database (db=1) to avoid cross-test cache pollution
|
|
var redis = scope.ServiceProvider.GetRequiredService<IConnectionMultiplexer>();
|
|
var server = redis.GetServer(redis.GetEndPoints().First());
|
|
await server.FlushDatabaseAsync(1);
|
|
}
|
|
|
|
public new async Task DisposeAsync()
|
|
{
|
|
await base.DisposeAsync();
|
|
}
|
|
}
|