From cc8989a6b34ccf3bcb8faddc46fd935797334337 Mon Sep 17 00:00:00 2001 From: Meysam Hadeli <35596795+meysamhadeli@users.noreply.github.com> Date: Thu, 19 Dec 2024 03:09:50 +0330 Subject: [PATCH] fix: fix db-update exception in tests --- src/BuildingBlocks/EFCore/Extensions.cs | 2 +- src/BuildingBlocks/EFCore/ISeedManager.cs | 3 +- src/BuildingBlocks/EFCore/SeedManagers.cs | 41 +++++++++---------- .../PersistMessageProcessor/Extensions.cs | 5 +-- src/BuildingBlocks/TestBase/TestBase.cs | 5 ++- 5 files changed, 28 insertions(+), 28 deletions(-) diff --git a/src/BuildingBlocks/EFCore/Extensions.cs b/src/BuildingBlocks/EFCore/Extensions.cs index 282de70b..17c98d33 100644 --- a/src/BuildingBlocks/EFCore/Extensions.cs +++ b/src/BuildingBlocks/EFCore/Extensions.cs @@ -137,6 +137,6 @@ private static async Task SeedAsync(IServiceProvider serviceProvider) var seedersManager = scope.ServiceProvider.GetRequiredService(); - await seedersManager.ExecuteAsync(); + await seedersManager.ExecuteSeedAsync(); } } diff --git a/src/BuildingBlocks/EFCore/ISeedManager.cs b/src/BuildingBlocks/EFCore/ISeedManager.cs index c6f0e790..0ded6aa4 100644 --- a/src/BuildingBlocks/EFCore/ISeedManager.cs +++ b/src/BuildingBlocks/EFCore/ISeedManager.cs @@ -2,5 +2,6 @@ namespace BuildingBlocks.EFCore; public interface ISeedManager { - Task ExecuteAsync(); + Task ExecuteSeedAsync(); + Task ExecuteTestSeedAsync(); } diff --git a/src/BuildingBlocks/EFCore/SeedManagers.cs b/src/BuildingBlocks/EFCore/SeedManagers.cs index cf91b0de..6602d4f4 100644 --- a/src/BuildingBlocks/EFCore/SeedManagers.cs +++ b/src/BuildingBlocks/EFCore/SeedManagers.cs @@ -1,39 +1,38 @@ using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; namespace BuildingBlocks.EFCore; public class SeedManager( -IServiceProvider serviceProvider -) - : ISeedManager + ILogger logger, + IWebHostEnvironment env, + IServiceProvider serviceProvider +) : ISeedManager { - public async Task ExecuteAsync() + public async Task ExecuteSeedAsync() { await using var scope = serviceProvider.CreateAsyncScope(); - var logger = scope.ServiceProvider.GetRequiredService>(); - var env = scope.ServiceProvider.GetRequiredService(); var dataSeeders = scope.ServiceProvider.GetServices(); - if (env.IsEnvironment("test")) + foreach (var seeder in dataSeeders.Where(x => x is not ITestDataSeeder)) { - foreach (var seeder in dataSeeders.Where(x => x is ITestDataSeeder)) - { - logger.LogInformation("Test Seed {SeederName} is started.", seeder.GetType().Name); - await seeder.SeedAllAsync(); - logger.LogInformation("Test Seed {SeederName} is completed.", seeder.GetType().Name); - } + logger.LogInformation("Seed {SeederName} is started.", seeder.GetType().Name); + await seeder.SeedAllAsync(); + logger.LogInformation("Seed {SeederName} is completed.", seeder.GetType().Name); } - else + } + + public async Task ExecuteTestSeedAsync() + { + await using var scope = serviceProvider.CreateAsyncScope(); + var dataSeeders = scope.ServiceProvider.GetServices(); + + foreach (var seeder in dataSeeders.Where(x => x is not ITestDataSeeder)) { - foreach (var seeder in dataSeeders.Where(x => x is not ITestDataSeeder)) - { - logger.LogInformation("Seed {SeederName} is started.", seeder.GetType().Name); - await seeder.SeedAllAsync(); - logger.LogInformation("Seed {SeederName} is completed.", seeder.GetType().Name); - } + logger.LogInformation("Seed {SeederName} is started.", seeder.GetType().Name); + await seeder.SeedAllAsync(); + logger.LogInformation("Seed {SeederName} is completed.", seeder.GetType().Name); } } } diff --git a/src/BuildingBlocks/PersistMessageProcessor/Extensions.cs b/src/BuildingBlocks/PersistMessageProcessor/Extensions.cs index 1efc84a4..5aef3604 100644 --- a/src/BuildingBlocks/PersistMessageProcessor/Extensions.cs +++ b/src/BuildingBlocks/PersistMessageProcessor/Extensions.cs @@ -51,10 +51,7 @@ IWebHostEnvironment env services.AddScoped(); - if (env.EnvironmentName != "test") - { - services.AddHostedService(); - } + services.AddHostedService(); return services; } diff --git a/src/BuildingBlocks/TestBase/TestBase.cs b/src/BuildingBlocks/TestBase/TestBase.cs index 20c3c0b0..ba54b4a4 100644 --- a/src/BuildingBlocks/TestBase/TestBase.cs +++ b/src/BuildingBlocks/TestBase/TestBase.cs @@ -52,6 +52,7 @@ public class TestFixture : IAsyncLifetime public CancellationTokenSource CancellationTokenSource; public PersistMessageBackgroundService PersistMessageBackgroundService => ServiceProvider.GetRequiredService(); + public ISeedManager SeedManager => ServiceProvider.GetRequiredService(); public HttpClient HttpClient { @@ -95,7 +96,7 @@ protected TestFixture() { TestRegistrationServices?.Invoke(services); services.ReplaceSingleton(AddHttpContextAccessorMock); - // services.RemoveAll(); + services.RemoveAll(); services.AddSingleton(); // Register all ITestDataSeeder implementations dynamically @@ -609,6 +610,8 @@ await Fixture.PersistMessageBackgroundService.StartAsync( _reSpawnerDefaultDb = await Respawner.CreateAsync( DefaultDbConnection, new RespawnerOptions { DbAdapter = DbAdapter.Postgres }); + + await Fixture.SeedManager.ExecuteTestSeedAsync(); } }