using ECMJobRunner.Domain.Entities; using ECMJobRunner.Tests.DTOs; using ECMJobRunner.Tests.Infrastructure; using FluentAssertions; namespace ECMJobRunner.Tests.Repositories; /// /// Tests for CfgProfileRepository /// public class CfgProfileRepositoryTests : IClassFixture { 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); } }