diff --git a/backend/api.test/Controllers/AreaControllerTests.cs b/backend/api.test/Controllers/AreaControllerTests.cs index f354ffe2..74590ff0 100644 --- a/backend/api.test/Controllers/AreaControllerTests.cs +++ b/backend/api.test/Controllers/AreaControllerTests.cs @@ -9,6 +9,7 @@ using Api.Services; using Api.Test.Database; using Microsoft.Extensions.DependencyInjection; +using Testcontainers.PostgreSql; using Xunit; namespace Api.Test.Controllers @@ -16,6 +17,7 @@ namespace Api.Test.Controllers public class AreaControllerTests : IAsyncLifetime { public required DatabaseUtilities DatabaseUtilities; + public required PostgreSqlContainer Container; public required HttpClient Client; public required JsonSerializerOptions SerializerOptions; @@ -23,24 +25,24 @@ public class AreaControllerTests : IAsyncLifetime public async Task InitializeAsync() { - string databaseName = Guid.NewGuid().ToString(); - (string connectionString, var connection) = await TestSetupHelpers.ConfigureDatabase( - databaseName + (Container, string connectionString, var connection) = + await TestSetupHelpers.ConfigurePostgreSqlDatabase(); + var factory = TestSetupHelpers.ConfigureWebApplicationFactory( + postgreSqlConnectionString: connectionString ); - var factory = TestSetupHelpers.ConfigureWebApplicationFactory(databaseName); var serviceProvider = TestSetupHelpers.ConfigureServiceProvider(factory); Client = TestSetupHelpers.ConfigureHttpClient(factory); SerializerOptions = TestSetupHelpers.ConfigureJsonSerializerOptions(); DatabaseUtilities = new DatabaseUtilities( - TestSetupHelpers.ConfigureFlotillaDbContext(connectionString) + TestSetupHelpers.ConfigurePostgreSqlContext(connectionString) ); AreaService = serviceProvider.GetRequiredService(); } - public Task DisposeAsync() => Task.CompletedTask; + public async Task DisposeAsync() => await Task.CompletedTask; [Fact] public async Task CheckThatAreaIsCorrectlyCreatedThroughEndpoint() diff --git a/backend/api.test/Controllers/EmergencyActionControllerTests.cs b/backend/api.test/Controllers/EmergencyActionControllerTests.cs index 5977a6dc..9c813d0d 100644 --- a/backend/api.test/Controllers/EmergencyActionControllerTests.cs +++ b/backend/api.test/Controllers/EmergencyActionControllerTests.cs @@ -1,10 +1,7 @@ -using System; -using System.Net.Http; -using System.Text.Json; +using System.Net.Http; using System.Threading.Tasks; -using Api.Services; using Api.Test.Database; -using Microsoft.Extensions.DependencyInjection; +using Testcontainers.PostgreSql; using Xunit; namespace Api.Test.Controllers @@ -12,19 +9,20 @@ namespace Api.Test.Controllers public class EmergencyActionControllerTests : IAsyncLifetime { public required DatabaseUtilities DatabaseUtilities; + public required PostgreSqlContainer Container; public required HttpClient Client; public async Task InitializeAsync() { - string databaseName = Guid.NewGuid().ToString(); - (string connectionString, var connection) = await TestSetupHelpers.ConfigureDatabase( - databaseName + (Container, string connectionString, var connection) = + await TestSetupHelpers.ConfigurePostgreSqlDatabase(); + var factory = TestSetupHelpers.ConfigureWebApplicationFactory( + postgreSqlConnectionString: connectionString ); - var factory = TestSetupHelpers.ConfigureWebApplicationFactory(databaseName); Client = TestSetupHelpers.ConfigureHttpClient(factory); DatabaseUtilities = new DatabaseUtilities( - TestSetupHelpers.ConfigureFlotillaDbContext(connectionString) + TestSetupHelpers.ConfigurePostgreSqlContext(connectionString) ); } diff --git a/backend/api.test/Controllers/InspectionAreaControllerTests.cs b/backend/api.test/Controllers/InspectionAreaControllerTests.cs index fb82983c..507211e7 100644 --- a/backend/api.test/Controllers/InspectionAreaControllerTests.cs +++ b/backend/api.test/Controllers/InspectionAreaControllerTests.cs @@ -10,6 +10,7 @@ using Api.Services; using Api.Test.Database; using Microsoft.Extensions.DependencyInjection; +using Testcontainers.PostgreSql; using Xunit; namespace Api.Test.Controllers @@ -17,6 +18,7 @@ namespace Api.Test.Controllers public class InspectionAreaControllerTests : IAsyncLifetime { public required DatabaseUtilities DatabaseUtilities; + public required PostgreSqlContainer Container; public required HttpClient Client; public required JsonSerializerOptions SerializerOptions; @@ -26,18 +28,18 @@ public class InspectionAreaControllerTests : IAsyncLifetime public async Task InitializeAsync() { - string databaseName = Guid.NewGuid().ToString(); - (string connectionString, var connection) = await TestSetupHelpers.ConfigureDatabase( - databaseName + (Container, string connectionString, var connection) = + await TestSetupHelpers.ConfigurePostgreSqlDatabase(); + var factory = TestSetupHelpers.ConfigureWebApplicationFactory( + postgreSqlConnectionString: connectionString ); - var factory = TestSetupHelpers.ConfigureWebApplicationFactory(databaseName); var serviceProvider = TestSetupHelpers.ConfigureServiceProvider(factory); Client = TestSetupHelpers.ConfigureHttpClient(factory); SerializerOptions = TestSetupHelpers.ConfigureJsonSerializerOptions(); DatabaseUtilities = new DatabaseUtilities( - TestSetupHelpers.ConfigureFlotillaDbContext(connectionString) + TestSetupHelpers.ConfigurePostgreSqlContext(connectionString) ); InspectionAreaService = serviceProvider.GetRequiredService(); diff --git a/backend/api.test/Controllers/InstallationControllerTests.cs b/backend/api.test/Controllers/InstallationControllerTests.cs index f167dad9..4a7462b0 100644 --- a/backend/api.test/Controllers/InstallationControllerTests.cs +++ b/backend/api.test/Controllers/InstallationControllerTests.cs @@ -4,25 +4,26 @@ using System.Threading.Tasks; using Api.Controllers.Models; using Api.Services; -using Api.Test.Database; using Microsoft.Extensions.DependencyInjection; +using Testcontainers.PostgreSql; using Xunit; namespace Api.Test.Controllers { public class InstallationControllerTests : IAsyncLifetime { + public required PostgreSqlContainer Container; public required HttpClient Client; public required IInstallationService InstallationService; public async Task InitializeAsync() { - string databaseName = Guid.NewGuid().ToString(); - (string connectionString, var connection) = await TestSetupHelpers.ConfigureDatabase( - databaseName + (Container, string connectionString, var connection) = + await TestSetupHelpers.ConfigurePostgreSqlDatabase(); + var factory = TestSetupHelpers.ConfigureWebApplicationFactory( + postgreSqlConnectionString: connectionString ); - var factory = TestSetupHelpers.ConfigureWebApplicationFactory(databaseName); var serviceProvider = TestSetupHelpers.ConfigureServiceProvider(factory); Client = TestSetupHelpers.ConfigureHttpClient(factory); diff --git a/backend/api.test/Controllers/MissionSchedulingControllerTests.cs b/backend/api.test/Controllers/MissionSchedulingControllerTests.cs index 1985a1b9..572c37d2 100644 --- a/backend/api.test/Controllers/MissionSchedulingControllerTests.cs +++ b/backend/api.test/Controllers/MissionSchedulingControllerTests.cs @@ -11,6 +11,7 @@ using Api.Services; using Api.Test.Database; using Microsoft.Extensions.DependencyInjection; +using Testcontainers.PostgreSql; using Xunit; namespace Api.Test.Controllers @@ -18,6 +19,7 @@ namespace Api.Test.Controllers public class MissionSchedulingControllerTests : IAsyncLifetime { public required DatabaseUtilities DatabaseUtilities; + public required PostgreSqlContainer Container; public required HttpClient Client; public required JsonSerializerOptions SerializerOptions; @@ -25,18 +27,18 @@ public class MissionSchedulingControllerTests : IAsyncLifetime public async Task InitializeAsync() { - string databaseName = Guid.NewGuid().ToString(); - (string connectionString, var connection) = await TestSetupHelpers.ConfigureDatabase( - databaseName + (Container, string connectionString, var connection) = + await TestSetupHelpers.ConfigurePostgreSqlDatabase(); + var factory = TestSetupHelpers.ConfigureWebApplicationFactory( + postgreSqlConnectionString: connectionString ); - var factory = TestSetupHelpers.ConfigureWebApplicationFactory(databaseName); var serviceProvider = TestSetupHelpers.ConfigureServiceProvider(factory); Client = TestSetupHelpers.ConfigureHttpClient(factory); SerializerOptions = TestSetupHelpers.ConfigureJsonSerializerOptions(); DatabaseUtilities = new DatabaseUtilities( - TestSetupHelpers.ConfigureFlotillaDbContext(connectionString) + TestSetupHelpers.ConfigurePostgreSqlContext(connectionString) ); MissionDefinitionService = serviceProvider.GetRequiredService(); @@ -272,7 +274,7 @@ await missionRunThreeResponse.Content.ReadFromJsonAsync( Assert.Equal(missionRunOne!.MissionId, activeMissionRun.MissionId); Assert.Equal(missionRunTwo!.MissionId, activeMissionRun.MissionId); Assert.Equal(missionRunThree!.MissionId, activeMissionRun.MissionId); - Assert.True(nextMissionRun.Id == missionRunOne.Id); + Assert.True(nextMissionRun.Id == missionRunTwo.Id); } [Fact] @@ -286,9 +288,7 @@ public async Task CheckThatMissionDoesNotStartIfRobotIsNotInSameInstallationAsMi plant.PlantCode ); - var otherInstallation = await DatabaseUtilities.NewInstallation( - "otherInstallationCode" - ); + var otherInstallation = await DatabaseUtilities.NewInstallation("OtherCode"); var robot = await DatabaseUtilities.NewRobot(RobotStatus.Available, otherInstallation); var query = CreateDefaultCustomMissionQuery( diff --git a/backend/api.test/Controllers/PlantControllerTests.cs b/backend/api.test/Controllers/PlantControllerTests.cs index 3e497222..91824257 100644 --- a/backend/api.test/Controllers/PlantControllerTests.cs +++ b/backend/api.test/Controllers/PlantControllerTests.cs @@ -6,6 +6,7 @@ using Api.Services; using Api.Test.Database; using Microsoft.Extensions.DependencyInjection; +using Testcontainers.PostgreSql; using Xunit; namespace Api.Test.Controllers @@ -13,22 +14,23 @@ namespace Api.Test.Controllers public class PlantControllerTests : IAsyncLifetime { public required DatabaseUtilities DatabaseUtilities; + public required PostgreSqlContainer Container; public required HttpClient Client; public required IPlantService PlantService; public async Task InitializeAsync() { - string databaseName = Guid.NewGuid().ToString(); - (string connectionString, var connection) = await TestSetupHelpers.ConfigureDatabase( - databaseName + (Container, string connectionString, var connection) = + await TestSetupHelpers.ConfigurePostgreSqlDatabase(); + var factory = TestSetupHelpers.ConfigureWebApplicationFactory( + postgreSqlConnectionString: connectionString ); - var factory = TestSetupHelpers.ConfigureWebApplicationFactory(databaseName); var serviceProvider = TestSetupHelpers.ConfigureServiceProvider(factory); Client = TestSetupHelpers.ConfigureHttpClient(factory); DatabaseUtilities = new DatabaseUtilities( - TestSetupHelpers.ConfigureFlotillaDbContext(connectionString) + TestSetupHelpers.ConfigurePostgreSqlContext(connectionString) ); PlantService = serviceProvider.GetRequiredService(); diff --git a/backend/api.test/Controllers/RobotControllerTests.cs b/backend/api.test/Controllers/RobotControllerTests.cs index ba982dc0..46bbff73 100644 --- a/backend/api.test/Controllers/RobotControllerTests.cs +++ b/backend/api.test/Controllers/RobotControllerTests.cs @@ -9,6 +9,7 @@ using Api.Database.Models; using Api.Test.Database; using Microsoft.EntityFrameworkCore; +using Testcontainers.PostgreSql; using Xunit; namespace Api.Test.Controllers @@ -16,22 +17,22 @@ namespace Api.Test.Controllers public class RobotControllerTests : IAsyncLifetime { public required DatabaseUtilities DatabaseUtilities; + public required PostgreSqlContainer Container; public required HttpClient Client; public required JsonSerializerOptions SerializerOptions; public async Task InitializeAsync() { - string databaseName = Guid.NewGuid().ToString(); - (string connectionString, var connection) = await TestSetupHelpers.ConfigureDatabase( - databaseName + (Container, string connectionString, var connection) = + await TestSetupHelpers.ConfigurePostgreSqlDatabase(); + var factory = TestSetupHelpers.ConfigureWebApplicationFactory( + postgreSqlConnectionString: connectionString ); - var factory = TestSetupHelpers.ConfigureWebApplicationFactory(databaseName); - Client = TestSetupHelpers.ConfigureHttpClient(factory); SerializerOptions = TestSetupHelpers.ConfigureJsonSerializerOptions(); DatabaseUtilities = new DatabaseUtilities( - TestSetupHelpers.ConfigureFlotillaDbContext(connectionString) + TestSetupHelpers.ConfigurePostgreSqlContext(connectionString) ); } @@ -90,7 +91,7 @@ public async Task CheckThatRobotIsNotCreatedWhenInspectionAreaIsNotInInstallatio // Arrange var installation = await DatabaseUtilities.NewInstallation(); - var wrongInstallation = await DatabaseUtilities.NewInstallation("wrongInstallation"); + var wrongInstallation = await DatabaseUtilities.NewInstallation("wrongCode"); var wrongPlant = await DatabaseUtilities.NewPlant(wrongInstallation.InstallationCode); var wrongInspectionArea = await DatabaseUtilities.NewInspectionArea( wrongInstallation.InstallationCode, diff --git a/backend/api.test/Controllers/RoleAccessTests.cs b/backend/api.test/Controllers/RoleAccessTests.cs index fbdcb050..02dae77f 100644 --- a/backend/api.test/Controllers/RoleAccessTests.cs +++ b/backend/api.test/Controllers/RoleAccessTests.cs @@ -9,6 +9,7 @@ using Api.Test.Mocks; using Microsoft.AspNetCore.Http; using Microsoft.Extensions.DependencyInjection; +using Testcontainers.PostgreSql; using Xunit; namespace Api.Test.Controllers @@ -16,17 +17,18 @@ namespace Api.Test.Controllers public class RoleAccessTests : IAsyncLifetime { public required DatabaseUtilities DatabaseUtilities; + public required PostgreSqlContainer Container; public required HttpClient Client; public required JsonSerializerOptions SerializerOptions; public required MockHttpContextAccessor HttpContextAccessor; public async Task InitializeAsync() { - string databaseName = Guid.NewGuid().ToString(); - (string connectionString, var connection) = await TestSetupHelpers.ConfigureDatabase( - databaseName + (Container, string connectionString, var connection) = + await TestSetupHelpers.ConfigurePostgreSqlDatabase(); + var factory = TestSetupHelpers.ConfigureWebApplicationFactory( + postgreSqlConnectionString: connectionString ); - var factory = TestSetupHelpers.ConfigureWebApplicationFactory(databaseName); var serviceProvider = TestSetupHelpers.ConfigureServiceProvider(factory); Client = TestSetupHelpers.ConfigureHttpClient(factory); @@ -35,7 +37,7 @@ public async Task InitializeAsync() serviceProvider.GetService()!; DatabaseUtilities = new DatabaseUtilities( - TestSetupHelpers.ConfigureFlotillaDbContext(connectionString) + TestSetupHelpers.ConfigurePostgreSqlContext(connectionString) ); } @@ -45,7 +47,7 @@ public async Task InitializeAsync() public async Task CheckThatRequestingPlantsWithUnauthorizedUserFails() { // Arrange - var installation = await DatabaseUtilities.NewInstallation("TestInstallationCode"); + var installation = await DatabaseUtilities.NewInstallation(); var plant = await DatabaseUtilities.NewPlant(installation.InstallationCode); var accessRoleQuery = new CreateAccessRoleQuery diff --git a/backend/api.test/Database/DatabaseUtilities.cs b/backend/api.test/Database/DatabaseUtilities.cs index 7042f0e5..cafb22b7 100644 --- a/backend/api.test/Database/DatabaseUtilities.cs +++ b/backend/api.test/Database/DatabaseUtilities.cs @@ -24,11 +24,11 @@ public class DatabaseUtilities private readonly RobotService _robotService; private readonly UserInfoService _userInfoService; private readonly SourceService _sourceService; - private readonly string _testInstallationCode = "testInstallationCode"; - private readonly string _testInstallationName = "testInstallation"; - private readonly string _testPlantCode = "testPlantCode"; - private readonly string _testInspectionAreaName = "testInspectionArea"; - private readonly string _testAreaName = "testArea"; + private readonly string _testInstallationCode = "InstCode"; + private readonly string _testInstallationName = "Installation"; + private readonly string _testPlantCode = "PlantCode"; + private readonly string _testInspectionAreaName = "InspectionArea"; + private readonly string _testAreaName = "Area"; public DatabaseUtilities(FlotillaDbContext context) { @@ -106,7 +106,7 @@ public async Task NewMissionRun( IsarMissionId = isarMissionId, MissionRunType = missionRunType, Status = missionStatus, - DesiredStartTime = DateTime.Now, + DesiredStartTime = DateTime.UtcNow, InspectionArea = inspectionArea, Tasks = [], InstallationCode = installationCode, diff --git a/backend/api.test/EventHandlers/TestMissionEventHandler.cs b/backend/api.test/EventHandlers/TestMissionEventHandler.cs index 5f438a2f..685cd362 100644 --- a/backend/api.test/EventHandlers/TestMissionEventHandler.cs +++ b/backend/api.test/EventHandlers/TestMissionEventHandler.cs @@ -14,6 +14,7 @@ using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using Moq; +using Testcontainers.PostgreSql; using Xunit; namespace Api.Test.EventHandlers @@ -24,6 +25,7 @@ public class TestMissionEventHandler : IAsyncLifetime, IAsyncDisposable public required TestWebApplicationFactory Factory; public required IServiceProvider ServiceProvider; + public required PostgreSqlContainer Container; public required string ConnectionString; public required DatabaseUtilities DatabaseUtilities; @@ -36,14 +38,15 @@ public class TestMissionEventHandler : IAsyncLifetime, IAsyncDisposable public async Task InitializeAsync() { - string databaseName = Guid.NewGuid().ToString(); - (ConnectionString, _) = await TestSetupHelpers.ConfigureDatabase(databaseName); - - DatabaseUtilities = new DatabaseUtilities(Context); + (Container, ConnectionString, var connection) = + await TestSetupHelpers.ConfigurePostgreSqlDatabase(); + Factory = TestSetupHelpers.ConfigureWebApplicationFactory( + postgreSqlConnectionString: ConnectionString + ); - Factory = TestSetupHelpers.ConfigureWebApplicationFactory(databaseName); ServiceProvider = TestSetupHelpers.ConfigureServiceProvider(Factory); + DatabaseUtilities = new DatabaseUtilities(Context); MissionRunService = ServiceProvider.GetRequiredService(); RobotService = ServiceProvider.GetRequiredService(); MissionSchedulingService = @@ -55,8 +58,7 @@ public async Task InitializeAsync() public async Task DisposeAsync() { - await Context.Database.EnsureDeletedAsync(); - await Context.Database.EnsureCreatedAsync(); + await Task.CompletedTask; } async ValueTask IAsyncDisposable.DisposeAsync() @@ -66,7 +68,7 @@ async ValueTask IAsyncDisposable.DisposeAsync() private FlotillaDbContext CreateContext() { - return TestSetupHelpers.ConfigureFlotillaDbContext(ConnectionString); + return TestSetupHelpers.ConfigurePostgreSqlContext(ConnectionString); } [Fact] @@ -92,7 +94,7 @@ public async Task ScheduledMissionStartedWhenSystemIsAvailable() // Act await MissionRunService.Create(missionRun); - Thread.Sleep(100); + Thread.Sleep(1000); // Assert var postTestMissionRun = await MissionRunService.ReadById( @@ -270,7 +272,7 @@ public async Task MissionRunIsStartedForOtherAvailableRobotIfOneRobotHasAnOngoin // Act (Ensure first mission is started) await MissionRunService.Create(missionRunOne); - Thread.Sleep(100); + Thread.Sleep(1000); // Assert var postStartMissionRunOne = await MissionRunService.ReadById( @@ -282,7 +284,7 @@ public async Task MissionRunIsStartedForOtherAvailableRobotIfOneRobotHasAnOngoin // Act (Ensure second mission is started for second robot) await MissionRunService.Create(missionRunTwo); - Thread.Sleep(100); + Thread.Sleep(1000); // Assert var postStartMissionRunTwo = await MissionRunService.ReadById( @@ -353,6 +355,8 @@ public async Task QueuedMissionsAreNotAbortedWhenRobotAvailableHappensAtTheSameT robotAvailableEventArgs ); + Thread.Sleep(100); + // Assert var postTestMissionRunOne = await MissionRunService.ReadById( missionRunOne.Id, diff --git a/backend/api.test/Services/MissionRunService.cs b/backend/api.test/Services/MissionRunService.cs index fe88fa7c..2b326273 100644 --- a/backend/api.test/Services/MissionRunService.cs +++ b/backend/api.test/Services/MissionRunService.cs @@ -6,6 +6,7 @@ using Api.Services; using Api.Test.Database; using Microsoft.Extensions.DependencyInjection; +using Testcontainers.PostgreSql; using Xunit; namespace Api.Test.Services @@ -13,19 +14,20 @@ namespace Api.Test.Services public class MissionServiceTest : IAsyncLifetime { public required DatabaseUtilities DatabaseUtilities; + public required PostgreSqlContainer Container; public required IMissionRunService MissionRunService; public async Task InitializeAsync() { - string databaseName = Guid.NewGuid().ToString(); - (string connectionString, var connection) = await TestSetupHelpers.ConfigureDatabase( - databaseName + (Container, string connectionString, var connection) = + await TestSetupHelpers.ConfigurePostgreSqlDatabase(); + var factory = TestSetupHelpers.ConfigureWebApplicationFactory( + postgreSqlConnectionString: connectionString ); - var factory = TestSetupHelpers.ConfigureWebApplicationFactory(databaseName); var serviceProvider = TestSetupHelpers.ConfigureServiceProvider(factory); DatabaseUtilities = new DatabaseUtilities( - TestSetupHelpers.ConfigureFlotillaDbContext(connectionString) + TestSetupHelpers.ConfigurePostgreSqlContext(connectionString) ); MissionRunService = serviceProvider.GetRequiredService(); } diff --git a/backend/api.test/Services/MissionSchedulingService.cs b/backend/api.test/Services/MissionSchedulingService.cs index 7800657b..ba3b5dba 100644 --- a/backend/api.test/Services/MissionSchedulingService.cs +++ b/backend/api.test/Services/MissionSchedulingService.cs @@ -1,11 +1,10 @@ -using System; -using System.Threading; using System.Threading.Tasks; using Api.Controllers.Models; using Api.Database.Models; using Api.Services; using Api.Test.Database; using Microsoft.Extensions.DependencyInjection; +using Testcontainers.PostgreSql; using Xunit; namespace Api.Test.Services @@ -13,21 +12,22 @@ namespace Api.Test.Services public class MissionSchedulingServiceTest : IAsyncLifetime { public required DatabaseUtilities DatabaseUtilities; + public required PostgreSqlContainer Container; public required IMissionSchedulingService MissionSchedulingService; public required IMissionRunService MissionRunService; public async Task InitializeAsync() { - string databaseName = Guid.NewGuid().ToString(); - (string connectionString, var connection) = await TestSetupHelpers.ConfigureDatabase( - databaseName + (Container, string connectionString, var connection) = + await TestSetupHelpers.ConfigurePostgreSqlDatabase(); + var factory = TestSetupHelpers.ConfigureWebApplicationFactory( + postgreSqlConnectionString: connectionString ); - var factory = TestSetupHelpers.ConfigureWebApplicationFactory(databaseName); var serviceProvider = TestSetupHelpers.ConfigureServiceProvider(factory); DatabaseUtilities = new DatabaseUtilities( - TestSetupHelpers.ConfigureFlotillaDbContext(connectionString) + TestSetupHelpers.ConfigurePostgreSqlContext(connectionString) ); MissionSchedulingService = serviceProvider.GetRequiredService(); diff --git a/backend/api.test/Services/RobotService.cs b/backend/api.test/Services/RobotService.cs index 6e4672b6..446069fe 100644 --- a/backend/api.test/Services/RobotService.cs +++ b/backend/api.test/Services/RobotService.cs @@ -6,6 +6,7 @@ using Api.Services; using Api.Test.Database; using Microsoft.Extensions.DependencyInjection; +using Testcontainers.PostgreSql; using Xunit; namespace Api.Test.Services @@ -13,20 +14,21 @@ namespace Api.Test.Services public class RobotServiceTest : IAsyncLifetime { public required DatabaseUtilities DatabaseUtilities; + public required PostgreSqlContainer Container; public required IRobotService RobotService; public required IInstallationService InstallationService; public async Task InitializeAsync() { - string databaseName = Guid.NewGuid().ToString(); - (string connectionString, var connection) = await TestSetupHelpers.ConfigureDatabase( - databaseName + (Container, string connectionString, var connection) = + await TestSetupHelpers.ConfigurePostgreSqlDatabase(); + var factory = TestSetupHelpers.ConfigureWebApplicationFactory( + postgreSqlConnectionString: connectionString ); - var factory = TestSetupHelpers.ConfigureWebApplicationFactory(databaseName); var serviceProvider = TestSetupHelpers.ConfigureServiceProvider(factory); DatabaseUtilities = new DatabaseUtilities( - TestSetupHelpers.ConfigureFlotillaDbContext(connectionString) + TestSetupHelpers.ConfigurePostgreSqlContext(connectionString) ); RobotService = serviceProvider.GetRequiredService(); diff --git a/backend/api.test/TestSetupHelpers.cs b/backend/api.test/TestSetupHelpers.cs index 8c8af5a3..9a4b7308 100644 --- a/backend/api.test/TestSetupHelpers.cs +++ b/backend/api.test/TestSetupHelpers.cs @@ -9,12 +9,13 @@ using Microsoft.AspNetCore.Mvc.Testing; using Microsoft.Data.Sqlite; using Microsoft.EntityFrameworkCore; +using Testcontainers.PostgreSql; namespace Api.Test; public static class TestSetupHelpers { - public static async Task<(string, DbConnection)> ConfigureDatabase(string databaseName) + public static async Task<(string, DbConnection)> ConfigureSqLiteDatabase(string databaseName) { string connectionString = new SqliteConnectionStringBuilder { @@ -22,7 +23,7 @@ public static class TestSetupHelpers Cache = SqliteCacheMode.Shared, }.ToString(); - var context = ConfigureFlotillaDbContext(connectionString); + var context = ConfigureSqLiteContext(connectionString); await context.Database.EnsureCreatedAsync(); var connection = context.Database.GetDbConnection(); @@ -31,6 +32,25 @@ public static class TestSetupHelpers return (connectionString, connection); } + public static async Task<( + PostgreSqlContainer, + string, + DbConnection + )> ConfigurePostgreSqlDatabase() + { + var container = new PostgreSqlBuilder().Build(); + await container.StartAsync(); + + string? connectionString = container.GetConnectionString(); + var context = ConfigurePostgreSqlContext(connectionString); + await context.Database.MigrateAsync(); + + var connection = context.Database.GetDbConnection(); + await connection.OpenAsync(); + + return (container, connectionString, connection); + } + public static JsonSerializerOptions ConfigureJsonSerializerOptions() { return new JsonSerializerOptions @@ -47,7 +67,7 @@ TestWebApplicationFactory factory return factory.Services; } - public static FlotillaDbContext ConfigureFlotillaDbContext(string connectionString) + public static FlotillaDbContext ConfigureSqLiteContext(string connectionString) { var optionsBuilder = new DbContextOptionsBuilder(); optionsBuilder.EnableSensitiveDataLogging(); @@ -57,11 +77,23 @@ public static FlotillaDbContext ConfigureFlotillaDbContext(string connectionStri return context; } + public static FlotillaDbContext ConfigurePostgreSqlContext(string connectionString) + { + var optionsBuilder = new DbContextOptionsBuilder(); + optionsBuilder.UseNpgsql( + connectionString, + o => o.UseQuerySplittingBehavior(QuerySplittingBehavior.SingleQuery) + ); + var context = new FlotillaDbContext(optionsBuilder.Options); + return context; + } + public static TestWebApplicationFactory ConfigureWebApplicationFactory( - string databaseName + string? databaseName = null, + string? postgreSqlConnectionString = null ) { - return new TestWebApplicationFactory(databaseName); + return new TestWebApplicationFactory(databaseName, postgreSqlConnectionString); } public static HttpClient ConfigureHttpClient(TestWebApplicationFactory factory) diff --git a/backend/api.test/TestWebApplicationFactory.cs b/backend/api.test/TestWebApplicationFactory.cs index 1a69e57f..afcced13 100644 --- a/backend/api.test/TestWebApplicationFactory.cs +++ b/backend/api.test/TestWebApplicationFactory.cs @@ -15,8 +15,10 @@ namespace Api.Test { - public class TestWebApplicationFactory(string databaseName) - : WebApplicationFactory + public class TestWebApplicationFactory( + string? sqLiteDatabaseName = null, + string? postgresConnectionString = null + ) : WebApplicationFactory where TProgram : class { public IConfiguration? Configuration; @@ -35,18 +37,36 @@ protected override void ConfigureWebHost(IWebHostBuilder builder) ); builder.ConfigureTestServices(services => { - string sqlLiteConnectionString = new SqliteConnectionStringBuilder + if (sqLiteDatabaseName != null) { - DataSource = $"file:{databaseName}?mode=memory", - Cache = SqliteCacheMode.Shared, - }.ToString(); + string sqlLiteConnectionString = new SqliteConnectionStringBuilder + { + DataSource = $"file:{sqLiteDatabaseName}?mode=memory", + Cache = SqliteCacheMode.Shared, + }.ToString(); - services.AddDbContext(options => - options.UseSqlite( - sqlLiteConnectionString, - o => o.UseQuerySplittingBehavior(QuerySplittingBehavior.SingleQuery) - ) - ); + services.AddDbContext(options => + options.UseSqlite( + sqlLiteConnectionString, + o => o.UseQuerySplittingBehavior(QuerySplittingBehavior.SingleQuery) + ) + ); + } + else if (postgresConnectionString != null) + { + services.AddDbContext( + options => + options.UseNpgsql( + postgresConnectionString, + o => + { + o.UseQuerySplittingBehavior(QuerySplittingBehavior.SingleQuery); + o.EnableRetryOnFailure(); + } + ), + ServiceLifetime.Transient + ); + } services.AddScoped(); services.AddScoped(); diff --git a/backend/api.test/api.test.csproj b/backend/api.test/api.test.csproj index 58647026..75b7bc7a 100644 --- a/backend/api.test/api.test.csproj +++ b/backend/api.test/api.test.csproj @@ -13,6 +13,7 @@ + all diff --git a/backend/api/appsettings.Test.json b/backend/api/appsettings.Test.json index e9e96baf..865dac6c 100644 --- a/backend/api/appsettings.Test.json +++ b/backend/api/appsettings.Test.json @@ -36,6 +36,6 @@ "ShouldFailOnMaxRetries": true }, "Database": { - "UseInMemoryDatabase": true + "UseInMemoryDatabase": false } }