diff --git a/src/BuildingBlocks/EFCore/SeedManagers.cs b/src/BuildingBlocks/EFCore/SeedManagers.cs index e50715aa..75e72b04 100644 --- a/src/BuildingBlocks/EFCore/SeedManagers.cs +++ b/src/BuildingBlocks/EFCore/SeedManagers.cs @@ -13,11 +13,11 @@ IServiceProvider serviceProvider { public async Task ExecuteSeedAsync() { - await using var scope = serviceProvider.CreateAsyncScope(); - var dataSeeders = scope.ServiceProvider.GetServices(); - if (!env.IsEnvironment("test")) { + await using var scope = serviceProvider.CreateAsyncScope(); + var dataSeeders = scope.ServiceProvider.GetServices(); + foreach (var seeder in dataSeeders) { logger.LogInformation("Seed {SeederName} is started.", seeder.GetType().Name); @@ -30,13 +30,13 @@ public async Task ExecuteSeedAsync() public async Task ExecuteTestSeedAsync() { await using var scope = serviceProvider.CreateAsyncScope(); - var dataSeeders = scope.ServiceProvider.GetServices(); + var testDataSeeders = scope.ServiceProvider.GetServices(); - foreach (var seeder in dataSeeders) + foreach (var testSeeder in testDataSeeders) { - 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.", testSeeder.GetType().Name); + await testSeeder.SeedAllAsync(); + logger.LogInformation("Seed {SeederName} is completed.", testSeeder.GetType().Name); } } } diff --git a/src/BuildingBlocks/TestBase/TestBase.cs b/src/BuildingBlocks/TestBase/TestBase.cs index ee5513fa..9c22a974 100644 --- a/src/BuildingBlocks/TestBase/TestBase.cs +++ b/src/BuildingBlocks/TestBase/TestBase.cs @@ -1,12 +1,9 @@ -using System.Globalization; using System.Net; -using System.Reflection; using System.Security.Claims; using Ardalis.GuardClauses; using BuildingBlocks.Core.Event; using BuildingBlocks.Core.Model; using BuildingBlocks.EFCore; -using BuildingBlocks.MassTransit; using BuildingBlocks.Mongo; using BuildingBlocks.PersistMessageProcessor; using BuildingBlocks.Web; @@ -21,17 +18,10 @@ using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.DependencyInjection.Extensions; -using Microsoft.Extensions.Hosting; using MongoDB.Driver; -using Npgsql; using NSubstitute; using Respawn; using Serilog; -using Testcontainers.EventStoreDb; -using Testcontainers.MongoDb; -using Testcontainers.PostgreSql; -using Testcontainers.RabbitMq; using WebMotions.Fake.Authentication.JwtBearer; using Xunit; using Xunit.Abstractions; @@ -39,6 +29,12 @@ namespace BuildingBlocks.TestBase; +using System.Globalization; +using Npgsql; +using Testcontainers.EventStoreDb; +using Testcontainers.MongoDb; +using Testcontainers.PostgreSql; +using Testcontainers.RabbitMq; public class TestFixture : IAsyncLifetime where TEntryPoint : class @@ -54,8 +50,8 @@ public class TestFixture : IAsyncLifetime public EventStoreDbContainer EventStoreDbTestContainer; public CancellationTokenSource CancellationTokenSource; - public PersistMessageBackgroundService PersistMessageBackgroundService => ServiceProvider.GetRequiredService(); - public ISeedManager SeedManager => ServiceProvider.GetRequiredService(); + public PersistMessageBackgroundService PersistMessageBackgroundService => + ServiceProvider.GetRequiredService(); public HttpClient HttpClient { @@ -100,10 +96,8 @@ protected TestFixture() TestRegistrationServices?.Invoke(services); services.ReplaceSingleton(AddHttpContextAccessorMock); + services.AddSingleton(); services.RemoveHostedService(); - services.AddSingleton(); // Register as a singleton - services.AddHostedService(provider => provider.GetRequiredService()); // Use the same instance for hosted service - // Register all ITestDataSeeder implementations dynamically services.Scan(scan => scan @@ -214,7 +208,9 @@ public async Task WaitForPublishing( var result = await WaitUntilConditionMet( async () => { - var published = await TestHarness.Published.Any(cancellationToken); + var published = + await TestHarness.Published.Any(cancellationToken); + return published; }); @@ -617,7 +613,7 @@ await Fixture.PersistMessageBackgroundService.StartAsync( DefaultDbConnection, new RespawnerOptions { DbAdapter = DbAdapter.Postgres }); - await Fixture.SeedManager.ExecuteTestSeedAsync(); + await SeedDataAsync(); } } @@ -685,6 +681,14 @@ private async Task ResetRabbitMqAsync(CancellationToken cancellationToken = defa protected virtual void RegisterTestsServices(IServiceCollection services) { } + + private async Task SeedDataAsync() + { + using var scope = Fixture.ServiceProvider.CreateScope(); + + var seedManager = scope.ServiceProvider.GetService(); + await seedManager.ExecuteTestSeedAsync(); + } } public abstract class TestReadBase : TestFixtureCore