diff --git a/src/Heimdall.Infrastructure/DependencyInjection.cs b/src/Heimdall.Infrastructure/DependencyInjection.cs index d434472..2349230 100644 --- a/src/Heimdall.Infrastructure/DependencyInjection.cs +++ b/src/Heimdall.Infrastructure/DependencyInjection.cs @@ -22,32 +22,19 @@ public static class DependencyInjection this IServiceCollection services, IConfiguration configuration) { - var dbProvider = configuration.GetValue("Database:Provider") ?? "PostgreSQL"; - // EF Core interceptor (registered as singleton — it is stateless) services.AddSingleton(); - if (dbProvider == "InMemory") - { - var dbName = configuration.GetValue("InMemoryDbName") ?? "HeimdallTestDb"; - services.AddDbContext(options => - options.UseInMemoryDatabase(dbName)); - services.AddDbContextFactory(options => - options.UseInMemoryDatabase(dbName)); - } - else - { - var connectionString = Environment.GetEnvironmentVariable("DATABASE_URL") - ?? configuration.GetConnectionString("Default") - ?? throw new InvalidOperationException( - "Database connection string is not configured. Set DATABASE_URL env var or ConnectionStrings:Default."); - services.AddDbContext(options => - options.UseNpgsql(connectionString, npgsql => - npgsql.MigrationsHistoryTable("__EFMigrationsHistory", "public"))); - services.AddDbContextFactory(options => - options.UseNpgsql(connectionString, npgsql => - npgsql.MigrationsHistoryTable("__EFMigrationsHistory", "public"))); - } + var connectionString = Environment.GetEnvironmentVariable("DATABASE_URL") + ?? configuration.GetConnectionString("Default") + ?? throw new InvalidOperationException( + "Database connection string is not configured. Set DATABASE_URL env var or ConnectionStrings:Default."); + services.AddDbContext(options => + options.UseNpgsql(connectionString, npgsql => + npgsql.MigrationsHistoryTable("__EFMigrationsHistory", "public"))); + services.AddDbContextFactory(options => + options.UseNpgsql(connectionString, npgsql => + npgsql.MigrationsHistoryTable("__EFMigrationsHistory", "public"))); // Identity services.AddIdentity(options => diff --git a/src/Heimdall.Infrastructure/Heimdall.Infrastructure.csproj b/src/Heimdall.Infrastructure/Heimdall.Infrastructure.csproj index dda5162..bf694f4 100644 --- a/src/Heimdall.Infrastructure/Heimdall.Infrastructure.csproj +++ b/src/Heimdall.Infrastructure/Heimdall.Infrastructure.csproj @@ -14,7 +14,7 @@ runtime; build; native; contentfiles; analyzers; buildtransitive all - + diff --git a/src/Heimdall.Web/appsettings.Testing.json b/src/Heimdall.Web/appsettings.Testing.json index 725a975..379bbde 100644 --- a/src/Heimdall.Web/appsettings.Testing.json +++ b/src/Heimdall.Web/appsettings.Testing.json @@ -1,12 +1,8 @@ { "Database": { - "Provider": "InMemory", "MigrateOnStartup": false }, "DataProtection": { "Mode": "Ephemeral" - }, - "Hangfire": { - "Enabled": false } } diff --git a/tests/Heimdall.Domain.Tests/Heimdall.Domain.Tests.csproj b/tests/Heimdall.Domain.Tests/Heimdall.Domain.Tests.csproj index c19cfa8..4a516c6 100644 --- a/tests/Heimdall.Domain.Tests/Heimdall.Domain.Tests.csproj +++ b/tests/Heimdall.Domain.Tests/Heimdall.Domain.Tests.csproj @@ -9,7 +9,7 @@ - + diff --git a/tests/Heimdall.Infrastructure.Tests/CameraAssignmentServiceTests.cs b/tests/Heimdall.Infrastructure.Tests/CameraAssignmentServiceTests.cs index ace3011..87c5e8b 100644 --- a/tests/Heimdall.Infrastructure.Tests/CameraAssignmentServiceTests.cs +++ b/tests/Heimdall.Infrastructure.Tests/CameraAssignmentServiceTests.cs @@ -2,6 +2,7 @@ using Heimdall.Application.Services; using Heimdall.Domain.Entities; using Heimdall.Domain.Enums; using Heimdall.Infrastructure.Data; +using Heimdall.Infrastructure.Data.Interceptors; using Heimdall.Infrastructure.Services; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Logging; @@ -10,22 +11,28 @@ using Shouldly; namespace Heimdall.Infrastructure.Tests; +[Collection("PostgresInfra")] public class CameraAssignmentServiceTests { private readonly IDbContextFactory _dbFactory; private readonly ILogger _logger = Substitute.For>(); - public CameraAssignmentServiceTests() + public CameraAssignmentServiceTests(PostgreSqlContainerFixture postgres) { - _dbFactory = CreateFactory(); + _dbFactory = CreateFactory(postgres.ConnectionString); } - private static TestDbContextFactory CreateFactory() + private static TestDbContextFactory CreateFactory(string connectionString) { var opts = new DbContextOptionsBuilder() - .UseInMemoryDatabase($"AssignTest-{Guid.NewGuid()}") + .UseNpgsql(connectionString, npgsql => + npgsql.MigrationsHistoryTable("__EFMigrationsHistory", "public")) .Options; + // Ensure tables exist + using var ctx = new AppDbContext(opts, new AuditInterceptor()); + ctx.Database.EnsureCreated(); + return new TestDbContextFactory(opts); } diff --git a/tests/Heimdall.Infrastructure.Tests/CameraEventServiceTests.cs b/tests/Heimdall.Infrastructure.Tests/CameraEventServiceTests.cs index aead998..082e091 100644 --- a/tests/Heimdall.Infrastructure.Tests/CameraEventServiceTests.cs +++ b/tests/Heimdall.Infrastructure.Tests/CameraEventServiceTests.cs @@ -3,6 +3,7 @@ using Heimdall.Application.Services; using Heimdall.Domain.Entities; using Heimdall.Domain.Enums; using Heimdall.Infrastructure.Data; +using Heimdall.Infrastructure.Data.Interceptors; using Heimdall.Infrastructure.Services; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Logging; @@ -11,23 +12,29 @@ using Shouldly; namespace Heimdall.Infrastructure.Tests; +[Collection("PostgresInfra")] public class CameraEventServiceTests { private readonly IDbContextFactory _dbFactory; private readonly ILogger _logger = Substitute.For>(); private readonly IServiceProvider _serviceProvider = Substitute.For(); - public CameraEventServiceTests() + public CameraEventServiceTests(PostgreSqlContainerFixture postgres) { - _dbFactory = CreateFactory(); + _dbFactory = CreateFactory(postgres.ConnectionString); } - private static TestDbContextFactory CreateFactory() + private static TestDbContextFactory CreateFactory(string connectionString) { var opts = new DbContextOptionsBuilder() - .UseInMemoryDatabase($"EventTest-{Guid.NewGuid()}") + .UseNpgsql(connectionString, npgsql => + npgsql.MigrationsHistoryTable("__EFMigrationsHistory", "public")) .Options; + // Ensure tables exist + using var ctx = new AppDbContext(opts, new AuditInterceptor()); + ctx.Database.EnsureCreated(); + return new TestDbContextFactory(opts); } diff --git a/tests/Heimdall.Infrastructure.Tests/Heimdall.Infrastructure.Tests.csproj b/tests/Heimdall.Infrastructure.Tests/Heimdall.Infrastructure.Tests.csproj index dfe933c..8335317 100644 --- a/tests/Heimdall.Infrastructure.Tests/Heimdall.Infrastructure.Tests.csproj +++ b/tests/Heimdall.Infrastructure.Tests/Heimdall.Infrastructure.Tests.csproj @@ -12,6 +12,7 @@ + diff --git a/tests/Heimdall.Infrastructure.Tests/PostgreSqlContainerFixture.cs b/tests/Heimdall.Infrastructure.Tests/PostgreSqlContainerFixture.cs new file mode 100644 index 0000000..39d5bc7 --- /dev/null +++ b/tests/Heimdall.Infrastructure.Tests/PostgreSqlContainerFixture.cs @@ -0,0 +1,34 @@ +using Testcontainers.PostgreSql; + +namespace Heimdall.Infrastructure.Tests; + +/// +/// xUnit collection fixture that manages a PostgreSQL Testcontainer. +/// One container per test collection — shared across all test classes. +/// +public class PostgreSqlContainerFixture : IAsyncLifetime +{ + private readonly PostgreSqlContainer _container = new PostgreSqlBuilder("postgres:17-alpine") + .WithDatabase("heimdall_test") + .WithUsername("heimdall") + .WithPassword("heimdall_test_pwd") + .WithCleanUp(true) + .Build(); + + public string ConnectionString => _container.GetConnectionString(); + + public async Task InitializeAsync() + { + await _container.StartAsync(); + } + + public async Task DisposeAsync() + { + await _container.DisposeAsync(); + } +} + +[CollectionDefinition("PostgresInfra")] +public class PostgresInfraFixture : ICollectionFixture +{ +} diff --git a/tests/Heimdall.IntegrationTests/Controllers/AuthControllerTests.cs b/tests/Heimdall.IntegrationTests/Controllers/AuthControllerTests.cs index 9b2f047..877de39 100644 --- a/tests/Heimdall.IntegrationTests/Controllers/AuthControllerTests.cs +++ b/tests/Heimdall.IntegrationTests/Controllers/AuthControllerTests.cs @@ -29,7 +29,7 @@ public class AuthControllerTests : PostgresTestBase { if (_seeded) return; - // First registration is auto-admin (no prior users in the InMemory DB) + // First registration is auto-admin (no prior users in the DB) var adminEmail = $"admin-{Guid.NewGuid()}@heimdall.test"; var regResponse = await _client.PostAsJsonAsync("api/auth/register", new { diff --git a/tests/Heimdall.IntegrationTests/Controllers/SettingsControllerTests.cs b/tests/Heimdall.IntegrationTests/Controllers/SettingsControllerTests.cs index 8d2ad9c..dc42055 100644 --- a/tests/Heimdall.IntegrationTests/Controllers/SettingsControllerTests.cs +++ b/tests/Heimdall.IntegrationTests/Controllers/SettingsControllerTests.cs @@ -96,7 +96,7 @@ public class SettingsControllerTests : PostgresTestBase var client = Factory.CreateClient(); - // First registration is auto-admin (no prior users in the InMemory DB) + // First registration is auto-admin (no prior users in the DB) var adminEmail = $"seed-admin-{Guid.NewGuid()}@heimdall.test"; var regResponse = await client.PostAsJsonAsync("api/auth/register-admin", new { diff --git a/tests/Heimdall.IntegrationTests/Factories/HeimdallWebApplicationFactory.cs b/tests/Heimdall.IntegrationTests/Factories/HeimdallWebApplicationFactory.cs index 65187a2..97aee17 100644 --- a/tests/Heimdall.IntegrationTests/Factories/HeimdallWebApplicationFactory.cs +++ b/tests/Heimdall.IntegrationTests/Factories/HeimdallWebApplicationFactory.cs @@ -5,18 +5,13 @@ namespace Heimdall.IntegrationTests.Factories; /// /// WebApplicationFactory for integration tests. -/// By default creates a factory without a connection string. -/// Use to get a factory connected to a -/// real PostgreSQL Testcontainer (see PostgreSqlContainerFixture). +/// Requires a PostgreSQL connection string from . /// public class HeimdallWebApplicationFactory : WebApplicationFactory { - private readonly string? _connectionString; + private readonly string _connectionString; - /// Creates a factory. The SUT uses InMemory unless a connection string is provided. - public HeimdallWebApplicationFactory() { } - - /// Creates a factory connected to a real PostgreSQL instance (e.g. Testcontainer). + /// Creates a factory connected to a real PostgreSQL instance via Testcontainers. public HeimdallWebApplicationFactory(string connectionString) { _connectionString = connectionString; @@ -27,13 +22,8 @@ public class HeimdallWebApplicationFactory : WebApplicationFactory builder.UseEnvironment("Testing"); builder.UseSetting("Cors:AllowedOrigins", "*"); - if (_connectionString is not null) - { - // Real PostgreSQL — override the InMemory provider from appsettings.Testing.json - builder.UseSetting("ConnectionStrings:Default", _connectionString); - builder.UseSetting("Database:Provider", "PostgreSQL"); - builder.UseSetting("Database:MigrateOnStartup", "true"); - } + builder.UseSetting("ConnectionStrings:Default", _connectionString); + builder.UseSetting("Database:MigrateOnStartup", "true"); // JWT config builder.UseSetting("Jwt:Key", "ThisIsATestKeyThatIsAtLeast32BytesLong!"); diff --git a/tests/Heimdall.IntegrationTests/Heimdall.IntegrationTests.csproj b/tests/Heimdall.IntegrationTests/Heimdall.IntegrationTests.csproj index c4b7d16..3e10e2e 100644 --- a/tests/Heimdall.IntegrationTests/Heimdall.IntegrationTests.csproj +++ b/tests/Heimdall.IntegrationTests/Heimdall.IntegrationTests.csproj @@ -10,7 +10,7 @@ - +