From 984a20f5a83a2be305636330f1d10bc846a0eb96 Mon Sep 17 00:00:00 2001 From: yoyocaleb Date: Wed, 7 Jan 2026 11:17:38 -0500 Subject: [PATCH 1/7] SoilStatusRepositoryTest Created --- .../SoilStatusRepositoryTest.cs | 62 +++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 tests/FMS.Infrastructure.Tests/SoilStatusRepositoryTest.cs diff --git a/tests/FMS.Infrastructure.Tests/SoilStatusRepositoryTest.cs b/tests/FMS.Infrastructure.Tests/SoilStatusRepositoryTest.cs new file mode 100644 index 00000000..6f6196df --- /dev/null +++ b/tests/FMS.Infrastructure.Tests/SoilStatusRepositoryTest.cs @@ -0,0 +1,62 @@ +using Microsoft.EntityFrameworkCore; +using NUnit.Framework; +using NSubstitute; +using System; +using System.Linq; +using System.Threading.Tasks; +using FMS.Infrastructure.Contexts; +using FMS.Infrastructure.Repositories; +using FMS.Domain.Entities; +using FMS.Domain.Dto; +using Microsoft.AspNetCore.Http; +using FluentAssertions; +using System.Collections.Generic; + +namespace FMS.Infrastructure.Tests +{ + [TestFixture] + public class SoilStatusRepositoryTests + { + private FmsDbContext _context; + private SoilStatusRepository _repository; + private bool _disposed = false; + + [SetUp] + public void SetUp() + { + var options = new DbContextOptionsBuilder() + .UseInMemoryDatabase(databaseName: $"TestDatabase_{Guid.NewGuid()}") + .Options; + var httpContextAccessor = Substitute.For(); + _context = new FmsDbContext(options, httpContextAccessor); + _repository = new SoilStatusRepository(_context); + + _context.SoilStatuses.Add(new SoilStatus + { + Id = Guid.NewGuid(), + Name = "VALID_NAME", + Description = "VALID_DESCRIPTION", + Active = true + }); + _context.SaveChanges(); + } + + [TearDown] + public void TearDown() + { + Dispose(); + } + public void Dispose() + { + if (!_disposed) + { + _context.Database.EnsureCreated(); + _context.Dispose(); + _repository.Dispose(); + _disposed = true; + } + } + + + } +} From fb7b3c22784b4e7a516477d659e1fc7f92382e6e Mon Sep 17 00:00:00 2001 From: yoyocaleb Date: Thu, 8 Jan 2026 10:22:56 -0500 Subject: [PATCH 2/7] SoilStatusDescriptionExistsAsync and SoilStatusNameExistsAsync test created --- .../SoilStatusRepositoryTest.cs | 71 +++++++++++++++++++ 1 file changed, 71 insertions(+) diff --git a/tests/FMS.Infrastructure.Tests/SoilStatusRepositoryTest.cs b/tests/FMS.Infrastructure.Tests/SoilStatusRepositoryTest.cs index 6f6196df..4a2ae0d4 100644 --- a/tests/FMS.Infrastructure.Tests/SoilStatusRepositoryTest.cs +++ b/tests/FMS.Infrastructure.Tests/SoilStatusRepositoryTest.cs @@ -57,6 +57,77 @@ public void Dispose() } } + // SoilStatusExistsAsync + [Test] + public async Task SoilStatusExistAsync_ReturnsTrue_WhenSoilStatusExist() + { + var existingSS = await _context.SoilStatuses.Select(e => e.Id).FirstAsync(); + var results = await _repository.SoilStatusExistsAsync(existingSS); + results.Should().BeTrue(); + } + [Test] + public async Task SoilStatusExistAsync_ReturnsFalse_WhenSoilStatusDoesNotExist() + { + var nonexistingSS = Guid.NewGuid(); + var results = await _repository.SoilStatusExistsAsync(nonexistingSS); + results.Should().BeFalse(); + } + + // SoilStatusNameExistsAsync + [Test] + public async Task SoilStatusNameExistsAsync_ReturnsTrue_WhenNameExist() + { + var existingSS = new SoilStatus { Id = Guid.NewGuid(), Name = "NEW_NAME" }; + _context.SoilStatuses.Add(existingSS); + await _context.SaveChangesAsync(); + + var results = await _repository.SoilStatusNameExistsAsync(existingSS.Name); + results.Should().BeTrue(); + } + [Test] + public async Task SoilStatusNameExistsAsync_ReturnsFalse_WhenNameDoesNotExist() + { + var nonexistingSS = "NONEXISTING_NAME"; + + var results = await _repository.SoilStatusNameExistsAsync(nonexistingSS); + results.Should().BeFalse(); + } + + // SoilStatusDescriptionExistsAsync + [Test] + public async Task SoilStatusDescriptionExistsAsync_ReturnsTrue_WhenDescriptionExist() + { + var existingSS = new SoilStatus { Id = Guid.NewGuid(), Description = "NEW_DESCRIPTION" }; + _context.SoilStatuses.Add(existingSS); + await _context.SaveChangesAsync(); + + var results = await _repository.SoilStatusDescriptionExistsAsync(existingSS.Description); + results.Should().BeTrue(); + } + [Test] + public async Task SoilStatusDescriptionExistsAsync_ReturnsFalse_WhenDescriptionDoesNotExist() + { + var nonexistingSS = "NONEXISTING_DESCRIPTION"; + + var results = await _repository.SoilStatusDescriptionExistsAsync(nonexistingSS); + results.Should().BeFalse(); + } + + // GetSoilStatusAsync + + + // GetSoilStatusByNameAsync + + + // GetSoilStatusListAsync + + + // CreateSoilStatusAsync + + + // UpdateSoilStatusAsync + + // UpdateSoilStatusStatusAsync } } From bee7bf4828b7c929f4e57feda571574cb68fe90d Mon Sep 17 00:00:00 2001 From: yoyocaleb Date: Thu, 8 Jan 2026 11:12:19 -0500 Subject: [PATCH 3/7] GetSoilStatusAsync and GetSoilStatusByNameAsync test created --- .../SoilStatusRepositoryTest.cs | 30 +++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/tests/FMS.Infrastructure.Tests/SoilStatusRepositoryTest.cs b/tests/FMS.Infrastructure.Tests/SoilStatusRepositoryTest.cs index 4a2ae0d4..1574aa48 100644 --- a/tests/FMS.Infrastructure.Tests/SoilStatusRepositoryTest.cs +++ b/tests/FMS.Infrastructure.Tests/SoilStatusRepositoryTest.cs @@ -114,10 +114,36 @@ public async Task SoilStatusDescriptionExistsAsync_ReturnsFalse_WhenDescriptionD } // GetSoilStatusAsync - + [Test] + public async Task GetSoilStatusAsync_ReturnsSoilStatusEditDto_WhenSoilStatusExist() + { + var existingSS = await _context.SoilStatuses.Select(e => e.Id).FirstAsync(); + var results = await _repository.GetSoilStatusAsync(existingSS); + results.Should().BeOfType(); + } + [Test] + public async Task GetSoilStatusAsync_ReturnsNull_WhenSoilStatusDoesNotExist() + { + var invalidId = Guid.NewGuid(); + var results = await _repository.GetSoilStatusAsync(invalidId); + results.Should().BeNull(); + } // GetSoilStatusByNameAsync - + [Test] + public async Task GetSoilStatusByNameAsync_ReturnsSoilStatusEditDto_WhenSoilStatusNameExist() + { + var existingSS = await _context.SoilStatuses.Select(e => e.Name).FirstAsync(); + var results = await _repository.GetSoilStatusByNameAsync(existingSS); + results.Should().BeOfType(); + } + [Test] + public async Task GetSoilStatusByNameAsync_ReturnsNull_WhenSoilStatusNameDoesNotExist() + { + var nonexistingName = "NONEXISTING_NAME"; + var results = await _repository.GetSoilStatusByNameAsync(nonexistingName); + results.Should().BeNull(); + } // GetSoilStatusListAsync From 17aff8c9958f2348a135012f78bbf2ed417ee176 Mon Sep 17 00:00:00 2001 From: yoyocaleb Date: Fri, 9 Jan 2026 11:21:29 -0500 Subject: [PATCH 4/7] GetSoilStatusListAsync and CreateSoilStatusAsync test created --- .../SoilStatusRepositoryTest.cs | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/tests/FMS.Infrastructure.Tests/SoilStatusRepositoryTest.cs b/tests/FMS.Infrastructure.Tests/SoilStatusRepositoryTest.cs index 1574aa48..5dce3175 100644 --- a/tests/FMS.Infrastructure.Tests/SoilStatusRepositoryTest.cs +++ b/tests/FMS.Infrastructure.Tests/SoilStatusRepositoryTest.cs @@ -146,10 +146,26 @@ public async Task GetSoilStatusByNameAsync_ReturnsNull_WhenSoilStatusNameDoesNot } // GetSoilStatusListAsync - + [Test] + public async Task GetSoilStatusListAsync_ReturnsAllFacilityStatuses() + { + var results = await _repository.GetSoilStatusListAsync(); + results.Should().NotBeNullOrEmpty(); + } // CreateSoilStatusAsync + [Test] + public async Task CreateSoilStatusAsync_CreateNewFacilityStatus_WhenDataIsValid() + { + var dto = new SoilStatusCreateDto { Name = "NEW_NAME", Description = "NEW_DESCRIPTION"}; + var newId = await _repository.CreateSoilStatusAsync(dto); + var createdSoilStatus = await _context.SoilStatuses.FindAsync(newId); + + createdSoilStatus.Should().NotBeNull(); + createdSoilStatus.Name.Should().Be("NEW_NAME"); + createdSoilStatus.Description.Should().Be("NEW_DESCRIPTION"); + } // UpdateSoilStatusAsync From 4a6ef9ff69186ffb936124f227aa0ee8b57c6051 Mon Sep 17 00:00:00 2001 From: yoyocaleb Date: Mon, 12 Jan 2026 11:22:59 -0500 Subject: [PATCH 5/7] UpdateSoilStatusAsync created. Fixed function UpdateSoilStatusAsync --- .../Repositories/SoilStatusRepository.cs | 8 +-- .../SoilStatusRepositoryTest.cs | 55 +++++++++++++++++++ 2 files changed, 59 insertions(+), 4 deletions(-) diff --git a/FMS.Infrastructure/Repositories/SoilStatusRepository.cs b/FMS.Infrastructure/Repositories/SoilStatusRepository.cs index 3348c9b1..b46b8b9b 100644 --- a/FMS.Infrastructure/Repositories/SoilStatusRepository.cs +++ b/FMS.Infrastructure/Repositories/SoilStatusRepository.cs @@ -92,14 +92,14 @@ private async Task UpdateSoilStatusInternalAsync(Guid id, SoilStatusEditDto soil throw new KeyNotFoundException($"Soil Status with ID {id} not found."); } - if (await SoilStatusNameExistsAsync(soilStatus.Name, id)) + if (await SoilStatusNameExistsAsync(soilStatusUpdates.Name, id)) { - throw new ArgumentException($"The Soil Status Name: '{soilStatus.Name}' already exists."); + throw new ArgumentException($"The Soil Status Name: '{soilStatusUpdates.Name}' already exists."); } - if (await SoilStatusDescriptionExistsAsync(soilStatus.Description, id)) + if (await SoilStatusDescriptionExistsAsync(soilStatusUpdates.Description, id)) { - throw new ArgumentException($"The Soil Status Description: '{soilStatus.Description}' already exists."); + throw new ArgumentException($"The Soil Status Description: '{soilStatusUpdates.Description}' already exists."); } soilStatus.Name = soilStatusUpdates.Name; diff --git a/tests/FMS.Infrastructure.Tests/SoilStatusRepositoryTest.cs b/tests/FMS.Infrastructure.Tests/SoilStatusRepositoryTest.cs index 5dce3175..dbdfb759 100644 --- a/tests/FMS.Infrastructure.Tests/SoilStatusRepositoryTest.cs +++ b/tests/FMS.Infrastructure.Tests/SoilStatusRepositoryTest.cs @@ -168,7 +168,62 @@ public async Task CreateSoilStatusAsync_CreateNewFacilityStatus_WhenDataIsValid( } // UpdateSoilStatusAsync + [Test] + public async Task UpdateSoilStatusAsync_UpdatesExistingSoilStatus_WhenDataIsValid() + { + var existingSS= new SoilStatus { Id = Guid.NewGuid(), Name = "ORIGINAL_NAME", Description = "ORIGINAL_DESCRIPTION" }; + _context.SoilStatuses.Add(existingSS); + await _context.SaveChangesAsync(); + + var updateDto = new SoilStatusEditDto { Id = existingSS.Id, Name = "UPDATED_NAME", Description = "UPDATED_DESCRIPTION" }; + await _repository.UpdateSoilStatusAsync(existingSS.Id, updateDto); + + var updatedSS= await _context.SoilStatuses.FindAsync(existingSS.Id); + updatedSS.Name.Should().Be("UPDATED_NAME"); + updatedSS.Description.Should().Be("UPDATED_DESCRIPTION"); + + } + [Test] + public async Task UpdateSoilStatusAsync_ThrowsKeyNotFoundException_WhenIdDoesNotExist() + { + var nonExistingId = Guid.NewGuid(); + var updateDto = new SoilStatusEditDto { Id = Guid.NewGuid(), Name = "ORIGINAL_NAME", Description = "ORIGINAL_DESCRIPTION" }; + + Func action = async () => await _repository.UpdateSoilStatusAsync(nonExistingId, updateDto); + await action.Should().ThrowAsync(); + } + [Test] + public async Task UpdateSoilStatusAsync_ThrowsKeyNotFoundException_WhenNameAlreadyExist() + { + var existingSS = new SoilStatus { Id = Guid.NewGuid(), Name = "DUPLICATE_NAME", Description = "ORIGINAL_DESCRIPTION" }; + var existingSS2 = new SoilStatus { Id = Guid.NewGuid(), Name = "DUPLICATE_NAME2", Description = "ORIGINAL_DESCRIPTION2" }; + _context.SoilStatuses.Add(existingSS); + _context.SoilStatuses.Add(existingSS2); + await _context.SaveChangesAsync(); + _context.ChangeTracker.Clear(); + + var updateDto = new SoilStatusEditDto { Id = existingSS2.Id, Name = "DUPLICATE_NAME", Description = "UPDATED_DESCRIPTION" }; + + Func action = async () => await _repository.UpdateSoilStatusAsync(existingSS2.Id, updateDto); + await action.Should().ThrowAsync(); + } + [Test] + public async Task UpdateSoilStatusAsync_ThrowsKeyNotFoundException_WhenDescriptionAlreadyExist() + { + var existingSS = new SoilStatus { Id = Guid.NewGuid(), Name = "ORIGINAL_NAME", Description = "DUPLICATE_DESCRIPTION" }; + var existingSS2 = new SoilStatus { Id = Guid.NewGuid(), Name = "ORIGINAL_NAME2", Description = "DUPLICATE_DESCRIPTION2" }; + + _context.SoilStatuses.Add(existingSS); + _context.SoilStatuses.Add(existingSS2); + await _context.SaveChangesAsync(); + _context.ChangeTracker.Clear(); + + var updateDto = new SoilStatusEditDto { Id = existingSS2.Id, Name = "UPDATED_NAME", Description = "DUPLICATE_DESCRIPTION" }; + + Func action = async () => await _repository.UpdateSoilStatusAsync(existingSS2.Id, updateDto); + await action.Should().ThrowAsync(); + } // UpdateSoilStatusStatusAsync } From 6620951e3a236b90ebe5b1763005428b541ecd95 Mon Sep 17 00:00:00 2001 From: yoyocaleb Date: Mon, 12 Jan 2026 11:28:40 -0500 Subject: [PATCH 6/7] CreateSoilStatusAsync test passes --- .../SoilStatusRepositoryTest.cs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/tests/FMS.Infrastructure.Tests/SoilStatusRepositoryTest.cs b/tests/FMS.Infrastructure.Tests/SoilStatusRepositoryTest.cs index dbdfb759..b250b02a 100644 --- a/tests/FMS.Infrastructure.Tests/SoilStatusRepositoryTest.cs +++ b/tests/FMS.Infrastructure.Tests/SoilStatusRepositoryTest.cs @@ -147,7 +147,7 @@ public async Task GetSoilStatusByNameAsync_ReturnsNull_WhenSoilStatusNameDoesNot // GetSoilStatusListAsync [Test] - public async Task GetSoilStatusListAsync_ReturnsAllFacilityStatuses() + public async Task GetSoilStatusListAsync_ReturnsAllSoilStatuses() { var results = await _repository.GetSoilStatusListAsync(); results.Should().NotBeNullOrEmpty(); @@ -155,16 +155,16 @@ public async Task GetSoilStatusListAsync_ReturnsAllFacilityStatuses() // CreateSoilStatusAsync [Test] - public async Task CreateSoilStatusAsync_CreateNewFacilityStatus_WhenDataIsValid() + public async Task CreateSoilStatusAsync_CreateNewSoilStatus_WhenDataIsValid() { var dto = new SoilStatusCreateDto { Name = "NEW_NAME", Description = "NEW_DESCRIPTION"}; var newId = await _repository.CreateSoilStatusAsync(dto); - var createdSoilStatus = await _context.SoilStatuses.FindAsync(newId); + var result = await _repository.GetSoilStatusByNameAsync(dto.Name); - createdSoilStatus.Should().NotBeNull(); - createdSoilStatus.Name.Should().Be("NEW_NAME"); - createdSoilStatus.Description.Should().Be("NEW_DESCRIPTION"); + result.Should().NotBeNull(); + result.Name.Should().Be("NEW_NAME"); + result.Description.Should().Be("NEW_DESCRIPTION"); } // UpdateSoilStatusAsync From a828719cf1eaf329623fc8ccdcd795e74481708b Mon Sep 17 00:00:00 2001 From: yoyocaleb Date: Mon, 12 Jan 2026 11:53:22 -0500 Subject: [PATCH 7/7] UpdateSoilStatusStatusAsync test created and pass --- .../SoilStatusRepositoryTest.cs | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/tests/FMS.Infrastructure.Tests/SoilStatusRepositoryTest.cs b/tests/FMS.Infrastructure.Tests/SoilStatusRepositoryTest.cs index b250b02a..56e57c49 100644 --- a/tests/FMS.Infrastructure.Tests/SoilStatusRepositoryTest.cs +++ b/tests/FMS.Infrastructure.Tests/SoilStatusRepositoryTest.cs @@ -226,5 +226,29 @@ public async Task UpdateSoilStatusAsync_ThrowsKeyNotFoundException_WhenDescripti } // UpdateSoilStatusStatusAsync + [Test] + public async Task UpdateSoilStatusTypeStatusAsync_UpdatesStatusCorrectly() + { + var SoilStatus = new SoilStatus + { + Id = Guid.NewGuid(), + Name = "ORIGINAL_NAME", + Description = "ORIGINAL_DESCRIPTION", + Active = true + }; + _context.SoilStatuses.Add(SoilStatus); + await _context.SaveChangesAsync(); + + await _repository.UpdateSoilStatusStatusAsync(SoilStatus.Id, false); + + var updatedSoilStatus = await _context.SoilStatuses.FindAsync(SoilStatus.Id); + updatedSoilStatus.Active.Should().BeFalse(); + } + [Test] + public async Task UpdateSoilStatusStatusAsync_ThrowsKeyNotFoundException_WhenIdDoesNotExist() + { + Func action = async () => await _repository.UpdateSoilStatusStatusAsync(Guid.NewGuid(), false); + await action.Should().ThrowAsync(); + } } }