Skip to content

Commit

Permalink
fix: fix db-update exception in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
meysamhadeli committed Dec 18, 2024
1 parent bd345ae commit cc8989a
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 28 deletions.
2 changes: 1 addition & 1 deletion src/BuildingBlocks/EFCore/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,6 @@ private static async Task SeedAsync(IServiceProvider serviceProvider)

var seedersManager = scope.ServiceProvider.GetRequiredService<ISeedManager>();

await seedersManager.ExecuteAsync();
await seedersManager.ExecuteSeedAsync();
}
}
3 changes: 2 additions & 1 deletion src/BuildingBlocks/EFCore/ISeedManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ namespace BuildingBlocks.EFCore;

public interface ISeedManager
{
Task ExecuteAsync();
Task ExecuteSeedAsync();
Task ExecuteTestSeedAsync();
}
41 changes: 20 additions & 21 deletions src/BuildingBlocks/EFCore/SeedManagers.cs
Original file line number Diff line number Diff line change
@@ -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<SeedManager> 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<ILogger<SeedManager>>();
var env = scope.ServiceProvider.GetRequiredService<IWebHostEnvironment>();
var dataSeeders = scope.ServiceProvider.GetServices<IDataSeeder>();

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<IDataSeeder>();

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);
}
}
}
5 changes: 1 addition & 4 deletions src/BuildingBlocks/PersistMessageProcessor/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,7 @@ IWebHostEnvironment env

services.AddScoped<IPersistMessageProcessor, PersistMessageProcessor>();

if (env.EnvironmentName != "test")
{
services.AddHostedService<PersistMessageBackgroundService>();
}
services.AddHostedService<PersistMessageBackgroundService>();

return services;
}
Expand Down
5 changes: 4 additions & 1 deletion src/BuildingBlocks/TestBase/TestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public class TestFixture<TEntryPoint> : IAsyncLifetime
public CancellationTokenSource CancellationTokenSource;

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

public HttpClient HttpClient
{
Expand Down Expand Up @@ -95,7 +96,7 @@ protected TestFixture()
{
TestRegistrationServices?.Invoke(services);
services.ReplaceSingleton(AddHttpContextAccessorMock);
// services.RemoveAll<IHostedService>();
services.RemoveAll<IHostedService>();
services.AddSingleton<PersistMessageBackgroundService>();

// Register all ITestDataSeeder implementations dynamically
Expand Down Expand Up @@ -609,6 +610,8 @@ await Fixture.PersistMessageBackgroundService.StartAsync(
_reSpawnerDefaultDb = await Respawner.CreateAsync(
DefaultDbConnection,
new RespawnerOptions { DbAdapter = DbAdapter.Postgres });

await Fixture.SeedManager.ExecuteTestSeedAsync();
}
}

Expand Down

0 comments on commit cc8989a

Please sign in to comment.