Introduced `CfgProfileDto` for testing and added a `FakeDataGenerator` utility for generating test data. Updated `ECMJobRunner.Tests.csproj` to support both `.NET Framework 4.8` and `.NET 8.0`, enabling nullable reference types and adding dependencies for testing frameworks, DI, and AutoMapper. Added `TestFixture` to set up dependency injection and in-memory databases for tests. Created `TestMappingProfile` for AutoMapper configurations. Implemented `CfgProfileRepositoryTests` to validate repository methods, including `GetByIdAsync`, `GetAllAsync`, `AddAsync`, and `FindAsync`. Enhanced test maintainability with `FluentAssertions` and realistic data generation using `Bogus`.
82 lines
2.7 KiB
C#
82 lines
2.7 KiB
C#
using AutoMapper;
|
|
using ECMJobRunner.Domain.Interfaces;
|
|
using ECMJobRunner.Infrastructure;
|
|
using ECMJobRunner.Infrastructure.Data;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Microsoft.Extensions.Hosting;
|
|
|
|
namespace ECMJobRunner.Tests.Infrastructure;
|
|
|
|
/// <summary>
|
|
/// Base test fixture with Dependency Injection and In-Memory Database
|
|
/// Supports both .NET Framework 4.8 (EF6 with Effort) and .NET 8 (EF Core InMemory)
|
|
/// </summary>
|
|
public class TestFixture : IDisposable
|
|
{
|
|
private readonly IHost _host;
|
|
private IServiceScope? _scope;
|
|
|
|
public IServiceProvider Services => _scope?.ServiceProvider ?? _host.Services;
|
|
public JobRunnerDbContext DbContext => Services.GetRequiredService<JobRunnerDbContext>();
|
|
public IMapper Mapper => Services.GetRequiredService<IMapper>();
|
|
public ICfgProfileRepository ProfileRepository => Services.GetRequiredService<ICfgProfileRepository>();
|
|
public IProfileSqlJobRepository SqlJobRepository => Services.GetRequiredService<IProfileSqlJobRepository>();
|
|
public IProfileHistoryRepository HistoryRepository => Services.GetRequiredService<IProfileHistoryRepository>();
|
|
|
|
public TestFixture()
|
|
{
|
|
_host = Host.CreateDefaultBuilder()
|
|
.ConfigureServices((context, services) =>
|
|
{
|
|
// Use AddInfrastructureInMemory extension for both frameworks
|
|
services.AddInfrastructureInMemory();
|
|
|
|
// Register test-specific AutoMapper profiles
|
|
services.AddAutoMapper(typeof(TestFixture).Assembly);
|
|
})
|
|
.Build();
|
|
|
|
// Create a scope for scoped services
|
|
_scope = _host.Services.CreateScope();
|
|
|
|
#if !NET48
|
|
// Ensure database is created (EF Core only)
|
|
DbContext.Database.EnsureCreated();
|
|
#endif
|
|
}
|
|
|
|
/// <summary>
|
|
/// Create a new service scope (useful for testing scoped lifetime)
|
|
/// </summary>
|
|
public IServiceScope CreateScope()
|
|
{
|
|
return _host.Services.CreateScope();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Reset the current scope (creates a new DbContext)
|
|
/// </summary>
|
|
public void ResetScope()
|
|
{
|
|
_scope?.Dispose();
|
|
_scope = _host.Services.CreateScope();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Clear all data from database
|
|
/// </summary>
|
|
public async Task ClearDatabaseAsync()
|
|
{
|
|
DbContext.ProfileHistories.RemoveRange(DbContext.ProfileHistories);
|
|
DbContext.ProfileSqlJobs.RemoveRange(DbContext.ProfileSqlJobs);
|
|
DbContext.CfgProfiles.RemoveRange(DbContext.CfgProfiles);
|
|
await DbContext.SaveChangesAsync();
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
_scope?.Dispose();
|
|
_host?.Dispose();
|
|
}
|
|
}
|