From 5ce7112b6225024ba1619af3f5ffad7a5d57d545 Mon Sep 17 00:00:00 2001 From: aestene Date: Tue, 21 Jan 2025 13:46:34 +0100 Subject: [PATCH] Use postgreSQL for mission eventh handler tests --- .../EventHandlers/TestMissionEventHandler.cs | 24 +++++++++++-------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/backend/api.test/EventHandlers/TestMissionEventHandler.cs b/backend/api.test/EventHandlers/TestMissionEventHandler.cs index a62ed13b..a90b9f4d 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.ConfigureSqLiteDatabase(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.ConfigureSqLiteContext(ConnectionString); + return TestSetupHelpers.ConfigurePostgreSqlContext(ConnectionString); } [Fact] @@ -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,