Refactor
Some checks failed
Code Coverage / Generate Coverage Report (push) Has been cancelled
Tests / Run Tests (9.0.x) (push) Has been cancelled
Tests / Docker Build Test (push) Has been cancelled
Tests / Test Summary (push) Has been cancelled

This commit is contained in:
Yaojia Wang
2025-11-03 21:02:14 +01:00
parent 5c541ddb79
commit a220e5d5d7
64 changed files with 3867 additions and 732 deletions

View File

@@ -14,16 +14,10 @@ namespace ColaFlow.Modules.Identity.IntegrationTests.Infrastructure;
/// Custom WebApplicationFactory for ColaFlow Integration Tests
/// Supports both In-Memory and Real PostgreSQL databases
/// </summary>
public class ColaFlowWebApplicationFactory : WebApplicationFactory<Program>
public class ColaFlowWebApplicationFactory(bool useInMemoryDatabase = true, string? testDatabaseName = null)
: WebApplicationFactory<Program>
{
private readonly bool _useInMemoryDatabase;
private readonly string? _testDatabaseName;
public ColaFlowWebApplicationFactory(bool useInMemoryDatabase = true, string? testDatabaseName = null)
{
_useInMemoryDatabase = useInMemoryDatabase;
_testDatabaseName = testDatabaseName ?? $"TestDb_{Guid.NewGuid()}";
}
private readonly string? _testDatabaseName = testDatabaseName ?? $"TestDb_{Guid.NewGuid()}";
protected override void ConfigureWebHost(IWebHostBuilder builder)
{
@@ -52,7 +46,7 @@ public class ColaFlowWebApplicationFactory : WebApplicationFactory<Program>
builder.ConfigureServices(services =>
{
// Register test databases (modules won't register PostgreSQL due to Testing environment)
if (_useInMemoryDatabase)
if (useInMemoryDatabase)
{
// Use In-Memory Database for fast, isolated tests
// IMPORTANT: Share the same database name for cross-context data consistency
@@ -101,7 +95,7 @@ public class ColaFlowWebApplicationFactory : WebApplicationFactory<Program>
{
// Initialize Identity database
var identityDb = services.GetRequiredService<IdentityDbContext>();
if (_useInMemoryDatabase)
if (useInMemoryDatabase)
{
identityDb.Database.EnsureCreated();
}
@@ -112,7 +106,7 @@ public class ColaFlowWebApplicationFactory : WebApplicationFactory<Program>
// Initialize ProjectManagement database
var pmDb = services.GetRequiredService<PMDbContext>();
if (_useInMemoryDatabase)
if (useInMemoryDatabase)
{
pmDb.Database.EnsureCreated();
}

View File

@@ -7,17 +7,13 @@ namespace ColaFlow.Modules.Identity.IntegrationTests.Infrastructure;
/// </summary>
public class DatabaseFixture : IDisposable
{
public ColaFlowWebApplicationFactory Factory { get; }
public ColaFlowWebApplicationFactory Factory { get; } = new(useInMemoryDatabase: true);
// Note: Client property is kept for backward compatibility but creates new instances
// Tests should call CreateClient() for isolation to avoid shared state issues
public HttpClient Client => CreateClient();
public DatabaseFixture()
{
// Use In-Memory Database for fast, isolated tests
Factory = new ColaFlowWebApplicationFactory(useInMemoryDatabase: true);
}
// Use In-Memory Database for fast, isolated tests
/// <summary>
/// Creates a new HttpClient for each test to ensure test isolation