mirror of
https://github.com/meysamhadeli/booking-microservices.git
synced 2026-05-02 19:02:55 +08:00
fix: fix test issues
This commit is contained in:
parent
68a9185070
commit
6c9d183970
@ -13,11 +13,11 @@ public class SeedManager(
|
|||||||
{
|
{
|
||||||
public async Task ExecuteSeedAsync()
|
public async Task ExecuteSeedAsync()
|
||||||
{
|
{
|
||||||
await using var scope = serviceProvider.CreateAsyncScope();
|
|
||||||
var dataSeeders = scope.ServiceProvider.GetServices<IDataSeeder>();
|
|
||||||
|
|
||||||
if (!env.IsEnvironment("test"))
|
if (!env.IsEnvironment("test"))
|
||||||
{
|
{
|
||||||
|
await using var scope = serviceProvider.CreateAsyncScope();
|
||||||
|
var dataSeeders = scope.ServiceProvider.GetServices<IDataSeeder>();
|
||||||
|
|
||||||
foreach (var seeder in dataSeeders)
|
foreach (var seeder in dataSeeders)
|
||||||
{
|
{
|
||||||
logger.LogInformation("Seed {SeederName} is started.", seeder.GetType().Name);
|
logger.LogInformation("Seed {SeederName} is started.", seeder.GetType().Name);
|
||||||
@ -30,13 +30,13 @@ public class SeedManager(
|
|||||||
public async Task ExecuteTestSeedAsync()
|
public async Task ExecuteTestSeedAsync()
|
||||||
{
|
{
|
||||||
await using var scope = serviceProvider.CreateAsyncScope();
|
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);
|
logger.LogInformation("Seed {SeederName} is started.", testSeeder.GetType().Name);
|
||||||
await seeder.SeedAllAsync();
|
await testSeeder.SeedAllAsync();
|
||||||
logger.LogInformation("Seed {SeederName} is completed.", seeder.GetType().Name);
|
logger.LogInformation("Seed {SeederName} is completed.", testSeeder.GetType().Name);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,12 +1,9 @@
|
|||||||
using System.Globalization;
|
|
||||||
using System.Net;
|
using System.Net;
|
||||||
using System.Reflection;
|
|
||||||
using System.Security.Claims;
|
using System.Security.Claims;
|
||||||
using Ardalis.GuardClauses;
|
using Ardalis.GuardClauses;
|
||||||
using BuildingBlocks.Core.Event;
|
using BuildingBlocks.Core.Event;
|
||||||
using BuildingBlocks.Core.Model;
|
using BuildingBlocks.Core.Model;
|
||||||
using BuildingBlocks.EFCore;
|
using BuildingBlocks.EFCore;
|
||||||
using BuildingBlocks.MassTransit;
|
|
||||||
using BuildingBlocks.Mongo;
|
using BuildingBlocks.Mongo;
|
||||||
using BuildingBlocks.PersistMessageProcessor;
|
using BuildingBlocks.PersistMessageProcessor;
|
||||||
using BuildingBlocks.Web;
|
using BuildingBlocks.Web;
|
||||||
@ -21,17 +18,10 @@ using Microsoft.AspNetCore.Mvc.Testing;
|
|||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
using Microsoft.Extensions.Configuration;
|
using Microsoft.Extensions.Configuration;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
using Microsoft.Extensions.DependencyInjection.Extensions;
|
|
||||||
using Microsoft.Extensions.Hosting;
|
|
||||||
using MongoDB.Driver;
|
using MongoDB.Driver;
|
||||||
using Npgsql;
|
|
||||||
using NSubstitute;
|
using NSubstitute;
|
||||||
using Respawn;
|
using Respawn;
|
||||||
using Serilog;
|
using Serilog;
|
||||||
using Testcontainers.EventStoreDb;
|
|
||||||
using Testcontainers.MongoDb;
|
|
||||||
using Testcontainers.PostgreSql;
|
|
||||||
using Testcontainers.RabbitMq;
|
|
||||||
using WebMotions.Fake.Authentication.JwtBearer;
|
using WebMotions.Fake.Authentication.JwtBearer;
|
||||||
using Xunit;
|
using Xunit;
|
||||||
using Xunit.Abstractions;
|
using Xunit.Abstractions;
|
||||||
@ -39,6 +29,12 @@ using ILogger = Serilog.ILogger;
|
|||||||
|
|
||||||
namespace BuildingBlocks.TestBase;
|
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
|
public class TestFixture<TEntryPoint> : IAsyncLifetime
|
||||||
where TEntryPoint : class
|
where TEntryPoint : class
|
||||||
@ -54,8 +50,8 @@ where TEntryPoint : class
|
|||||||
public EventStoreDbContainer EventStoreDbTestContainer;
|
public EventStoreDbContainer EventStoreDbTestContainer;
|
||||||
public CancellationTokenSource CancellationTokenSource;
|
public CancellationTokenSource CancellationTokenSource;
|
||||||
|
|
||||||
public PersistMessageBackgroundService PersistMessageBackgroundService => ServiceProvider.GetRequiredService<PersistMessageBackgroundService>();
|
public PersistMessageBackgroundService PersistMessageBackgroundService =>
|
||||||
public ISeedManager SeedManager => ServiceProvider.GetRequiredService<ISeedManager>();
|
ServiceProvider.GetRequiredService<PersistMessageBackgroundService>();
|
||||||
|
|
||||||
public HttpClient HttpClient
|
public HttpClient HttpClient
|
||||||
{
|
{
|
||||||
@ -100,10 +96,8 @@ where TEntryPoint : class
|
|||||||
TestRegistrationServices?.Invoke(services);
|
TestRegistrationServices?.Invoke(services);
|
||||||
services.ReplaceSingleton(AddHttpContextAccessorMock);
|
services.ReplaceSingleton(AddHttpContextAccessorMock);
|
||||||
|
|
||||||
|
services.AddSingleton<PersistMessageBackgroundService>();
|
||||||
services.RemoveHostedService<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
|
// Register all ITestDataSeeder implementations dynamically
|
||||||
services.Scan(scan => scan
|
services.Scan(scan => scan
|
||||||
@ -214,7 +208,9 @@ where TEntryPoint : class
|
|||||||
var result = await WaitUntilConditionMet(
|
var result = await WaitUntilConditionMet(
|
||||||
async () =>
|
async () =>
|
||||||
{
|
{
|
||||||
var published = await TestHarness.Published.Any<TMessage>(cancellationToken);
|
var published =
|
||||||
|
await TestHarness.Published.Any<TMessage>(cancellationToken);
|
||||||
|
|
||||||
return published;
|
return published;
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -617,7 +613,7 @@ where TEntryPoint : class
|
|||||||
DefaultDbConnection,
|
DefaultDbConnection,
|
||||||
new RespawnerOptions { DbAdapter = DbAdapter.Postgres });
|
new RespawnerOptions { DbAdapter = DbAdapter.Postgres });
|
||||||
|
|
||||||
await Fixture.SeedManager.ExecuteTestSeedAsync();
|
await SeedDataAsync();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -685,6 +681,14 @@ where TEntryPoint : class
|
|||||||
protected virtual void RegisterTestsServices(IServiceCollection services)
|
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>
|
public abstract class TestReadBase<TEntryPoint, TRContext> : TestFixtureCore<TEntryPoint>
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user