Skip to content

Commit

Permalink
fix: fix test issues
Browse files Browse the repository at this point in the history
  • Loading branch information
meysamhadeli committed Dec 19, 2024
1 parent 68a9185 commit 6c9d183
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 25 deletions.
16 changes: 8 additions & 8 deletions src/BuildingBlocks/EFCore/SeedManagers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ IServiceProvider serviceProvider
{
public async Task ExecuteSeedAsync()
{
await using var scope = serviceProvider.CreateAsyncScope();
var dataSeeders = scope.ServiceProvider.GetServices<IDataSeeder>();

if (!env.IsEnvironment("test"))
{
await using var scope = serviceProvider.CreateAsyncScope();
var dataSeeders = scope.ServiceProvider.GetServices<IDataSeeder>();

foreach (var seeder in dataSeeders)
{
logger.LogInformation("Seed {SeederName} is started.", seeder.GetType().Name);
Expand All @@ -30,13 +30,13 @@ public async Task ExecuteSeedAsync()
public async Task ExecuteTestSeedAsync()
{
await using var scope = serviceProvider.CreateAsyncScope();
var dataSeeders = scope.ServiceProvider.GetServices<ITestDataSeeder>();
var testDataSeeders = scope.ServiceProvider.GetServices<ITestDataSeeder>();

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);
}
}
}
38 changes: 21 additions & 17 deletions src/BuildingBlocks/TestBase/TestBase.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -21,24 +18,23 @@
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;
using ILogger = Serilog.ILogger;

namespace BuildingBlocks.TestBase;

using System.Globalization;
using Npgsql;
using Testcontainers.EventStoreDb;
using Testcontainers.MongoDb;
using Testcontainers.PostgreSql;
using Testcontainers.RabbitMq;

public class TestFixture<TEntryPoint> : IAsyncLifetime
where TEntryPoint : class
Expand All @@ -54,8 +50,8 @@ public class TestFixture<TEntryPoint> : IAsyncLifetime
public EventStoreDbContainer EventStoreDbTestContainer;
public CancellationTokenSource CancellationTokenSource;

public PersistMessageBackgroundService PersistMessageBackgroundService => ServiceProvider.GetRequiredService<PersistMessageBackgroundService>();
public ISeedManager SeedManager => ServiceProvider.GetRequiredService<ISeedManager>();
public PersistMessageBackgroundService PersistMessageBackgroundService =>
ServiceProvider.GetRequiredService<PersistMessageBackgroundService>();

public HttpClient HttpClient
{
Expand Down Expand Up @@ -100,10 +96,8 @@ protected TestFixture()
TestRegistrationServices?.Invoke(services);
services.ReplaceSingleton(AddHttpContextAccessorMock);

services.AddSingleton<PersistMessageBackgroundService>();
services.RemoveHostedService<PersistMessageBackgroundService>();
services.AddSingleton<PersistMessageBackgroundService>(); // Register as a singleton
services.AddHostedService(provider => provider.GetRequiredService<PersistMessageBackgroundService>()); // Use the same instance for hosted service


// Register all ITestDataSeeder implementations dynamically
services.Scan(scan => scan
Expand Down Expand Up @@ -214,7 +208,9 @@ public async Task<bool> WaitForPublishing<TMessage>(
var result = await WaitUntilConditionMet(
async () =>
{
var published = await TestHarness.Published.Any<TMessage>(cancellationToken);
var published =
await TestHarness.Published.Any<TMessage>(cancellationToken);

return published;
});

Expand Down Expand Up @@ -617,7 +613,7 @@ await Fixture.PersistMessageBackgroundService.StartAsync(
DefaultDbConnection,
new RespawnerOptions { DbAdapter = DbAdapter.Postgres });

await Fixture.SeedManager.ExecuteTestSeedAsync();
await SeedDataAsync();
}
}

Expand Down Expand Up @@ -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<ISeedManager>();
await seedManager.ExecuteTestSeedAsync();
}
}

public abstract class TestReadBase<TEntryPoint, TRContext> : TestFixtureCore<TEntryPoint>
Expand Down

0 comments on commit 6c9d183

Please sign in to comment.