Files
ColaFlow/colaflow-api/tests/Modules/Identity/ColaFlow.Modules.Identity.IntegrationTests/Infrastructure/RealDatabaseFixture.cs
Yaojia Wang 4183b10b39
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
Commit all scripts
2025-11-03 17:19:20 +01:00

66 lines
1.9 KiB
C#

using ColaFlow.Modules.Identity.Infrastructure.Persistence;
using Microsoft.Extensions.DependencyInjection;
namespace ColaFlow.Modules.Identity.IntegrationTests.Infrastructure;
/// <summary>
/// Database Fixture for Real PostgreSQL Database Tests
/// Use this for more realistic integration tests that verify actual database behavior
/// Requires PostgreSQL to be running on localhost
/// </summary>
public class RealDatabaseFixture : IDisposable
{
public ColaFlowWebApplicationFactory Factory { get; }
public HttpClient Client { get; }
private readonly string _testDatabaseName;
public RealDatabaseFixture()
{
_testDatabaseName = $"test_{Guid.NewGuid():N}";
// Use Real PostgreSQL Database
Factory = new ColaFlowWebApplicationFactory(
useInMemoryDatabase: false,
testDatabaseName: _testDatabaseName
);
Client = Factory.CreateClient();
// Clean up any existing test data
CleanupDatabase();
}
private void CleanupDatabase()
{
using var scope = Factory.Services.CreateScope();
var db = scope.ServiceProvider.GetRequiredService<IdentityDbContext>();
// Clear all data from test database
db.RefreshTokens.RemoveRange(db.RefreshTokens);
db.Users.RemoveRange(db.Users);
db.Tenants.RemoveRange(db.Tenants);
db.SaveChanges();
}
public void Dispose()
{
try
{
// Clean up test database
using (var scope = Factory.Services.CreateScope())
{
var db = scope.ServiceProvider.GetRequiredService<IdentityDbContext>();
db.Database.EnsureDeleted();
}
}
catch
{
// Ignore cleanup errors
}
Client?.Dispose();
Factory?.Dispose();
GC.SuppressFinalize(this);
}
}