using ColaFlow.Modules.Identity.Infrastructure.Persistence;
using Microsoft.Extensions.DependencyInjection;
namespace ColaFlow.Modules.Identity.IntegrationTests.Infrastructure;
///
/// 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
///
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();
// 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();
db.Database.EnsureDeleted();
}
}
catch
{
// Ignore cleanup errors
}
Client?.Dispose();
Factory?.Dispose();
GC.SuppressFinalize(this);
}
}