Add test infrastructure and repository tests
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`.
This commit is contained in:
153
ECMJobRunner.Tests/Repositories/CfgProfileRepositoryTests.cs
Normal file
153
ECMJobRunner.Tests/Repositories/CfgProfileRepositoryTests.cs
Normal file
@@ -0,0 +1,153 @@
|
||||
using ECMJobRunner.Domain.Entities;
|
||||
using ECMJobRunner.Tests.DTOs;
|
||||
using ECMJobRunner.Tests.Infrastructure;
|
||||
using FluentAssertions;
|
||||
|
||||
namespace ECMJobRunner.Tests.Repositories;
|
||||
|
||||
/// <summary>
|
||||
/// Tests for CfgProfileRepository
|
||||
/// </summary>
|
||||
public class CfgProfileRepositoryTests : IClassFixture<TestFixture>
|
||||
{
|
||||
private readonly TestFixture _fixture;
|
||||
|
||||
public CfgProfileRepositoryTests(TestFixture fixture)
|
||||
{
|
||||
_fixture = fixture;
|
||||
FakeDataGenerator.ResetIdCounters();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetByIdAsync_ExistingProfile_ReturnsProfile()
|
||||
{
|
||||
// Arrange
|
||||
await _fixture.ClearDatabaseAsync();
|
||||
var dto = new CfgProfileDto
|
||||
{
|
||||
Active = true,
|
||||
ProfileName = "Test Profile for GetById",
|
||||
TypeId = (byte)1,
|
||||
Schedule = "0 0 * * *",
|
||||
Comment = "Test",
|
||||
AddedWho = "TestUser",
|
||||
AddedWhen = DateTime.Now,
|
||||
ChangedWho = "TestUser",
|
||||
ChangedWhen = DateTime.Now
|
||||
};
|
||||
var addedProfile = await _fixture.ProfileRepository.AddAsync(dto);
|
||||
|
||||
// Act
|
||||
var result = await _fixture.ProfileRepository.GetByIdAsync(addedProfile.Id);
|
||||
|
||||
// Assert
|
||||
result.Should().NotBeNull();
|
||||
result!.Id.Should().Be(addedProfile.Id);
|
||||
result.ProfileName.Should().Be("Test Profile for GetById");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetAllAsync_ReturnsAllProfiles()
|
||||
{
|
||||
// Arrange
|
||||
await _fixture.ClearDatabaseAsync();
|
||||
|
||||
for (int i = 0; i < 5; i++)
|
||||
{
|
||||
var dto = new CfgProfileDto
|
||||
{
|
||||
Active = true,
|
||||
ProfileName = $"Profile {i}",
|
||||
TypeId = (byte)1,
|
||||
Schedule = "0 0 * * *",
|
||||
AddedWho = "TestUser",
|
||||
AddedWhen = DateTime.Now,
|
||||
ChangedWho = "TestUser",
|
||||
ChangedWhen = DateTime.Now
|
||||
};
|
||||
await _fixture.ProfileRepository.AddAsync(dto);
|
||||
}
|
||||
|
||||
// Act
|
||||
var result = await _fixture.ProfileRepository.GetAllAsync();
|
||||
|
||||
// Assert
|
||||
result.Should().HaveCount(5);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task AddAsync_NewProfile_AddsToDatabase()
|
||||
{
|
||||
// Arrange
|
||||
await _fixture.ClearDatabaseAsync();
|
||||
var dto = new CfgProfileDto
|
||||
{
|
||||
Active = true,
|
||||
ProfileName = "Test Profile",
|
||||
TypeId = (byte)1,
|
||||
Schedule = "0 0 * * *",
|
||||
Comment = "Test comment",
|
||||
AddedWho = "TestUser",
|
||||
AddedWhen = DateTime.Now,
|
||||
ChangedWho = "TestUser",
|
||||
ChangedWhen = DateTime.Now
|
||||
};
|
||||
|
||||
// Act
|
||||
var addedProfile = await _fixture.ProfileRepository.AddAsync(dto);
|
||||
|
||||
// Assert
|
||||
var result = await _fixture.ProfileRepository.GetByIdAsync(addedProfile.Id);
|
||||
result.Should().NotBeNull();
|
||||
result!.ProfileName.Should().Be("Test Profile");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task FindAsync_WithPredicate_ReturnsMatchingProfiles()
|
||||
{
|
||||
// Arrange
|
||||
await _fixture.ClearDatabaseAsync();
|
||||
|
||||
// Add active profiles
|
||||
for (int i = 0; i < 3; i++)
|
||||
{
|
||||
var dto = new CfgProfileDto
|
||||
{
|
||||
Active = true,
|
||||
ProfileName = $"Active Profile {i}",
|
||||
TypeId = (byte)1,
|
||||
Schedule = "0 0 * * *",
|
||||
AddedWho = "TestUser",
|
||||
AddedWhen = DateTime.Now,
|
||||
ChangedWho = "TestUser",
|
||||
ChangedWhen = DateTime.Now
|
||||
};
|
||||
await _fixture.ProfileRepository.AddAsync(dto);
|
||||
}
|
||||
|
||||
// Add inactive profiles
|
||||
for (int i = 0; i < 2; i++)
|
||||
{
|
||||
var dto = new CfgProfileDto
|
||||
{
|
||||
Active = false,
|
||||
ProfileName = $"Inactive Profile {i}",
|
||||
TypeId = (byte)1,
|
||||
Schedule = "0 0 * * *",
|
||||
AddedWho = "TestUser",
|
||||
AddedWhen = DateTime.Now,
|
||||
ChangedWho = "TestUser",
|
||||
ChangedWhen = DateTime.Now
|
||||
};
|
||||
await _fixture.ProfileRepository.AddAsync(dto);
|
||||
}
|
||||
|
||||
// Act
|
||||
var result = await _fixture.ProfileRepository.FindAsync(p => p.Active);
|
||||
|
||||
// Assert
|
||||
result.Should().HaveCount(3);
|
||||
result.Should().OnlyContain(p => p.Active);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user