rama creada apartir de master en j

This commit is contained in:
jrojas
2026-06-26 10:29:23 +02:00
parent 319fd3dfb0
commit c1517fda87
2810 changed files with 1927392 additions and 25392 deletions
+389 -336
View File
@@ -15,6 +15,9 @@ using Moq;
namespace adas_core.Test.Services;
/// <summary>
/// Contains unit tests for verifying the behavior of the <see cref="AdmissionService"/> class.
/// </summary>
public class AdmissionServiceTest
{
private Mock<IAdmissionRepository> _admissionRepositoryMock;
@@ -78,97 +81,113 @@ public class AdmissionServiceTest
_masterListServiceFactoryMock.Object);
}
/// <summary>
/// Verifies that the admission service successfully deletes a valid admission by ensuring the repository's
/// Delete operation is invoked exactly once for the corresponding admission identifier.
/// </summary>
[Test]
public async Task DeleteAdmissionAsync_ValidAdmission_DeletesSuccessfully()
{
// Arrange
var admissionId = ObjectId.GenerateNewId();
var admission = new Admission { Id = admissionId };
_admissionRepositoryMock.Setup(repo => repo.FindById(admissionId))
.ReturnsAsync(admission);
// Act
await _admissionService.DeleteAdmissionAsync(admission);
// Assert
_admissionRepositoryMock.Verify(repo => repo.Delete(admissionId), Times.Once);
}
public async Task DeleteAdmissionAsync_ValidAdmission_DeletesSuccessfully()
{
// Arrange
var admissionId = ObjectId.GenerateNewId();
var admission = new Admission { Id = admissionId };
_admissionRepositoryMock.Setup(repo => repo.FindById(admissionId))
.ReturnsAsync(admission);
// Act
await _admissionService.DeleteAdmissionAsync(admission);
// Assert
_admissionRepositoryMock.Verify(repo => repo.Delete(admissionId), Times.Once);
}
/// <summary>
/// Verifies that <c>DeleteAdmissionByIdAsync</c> successfully deletes an admission when it exists in the repository, by ensuring the repository's <c>Delete</c> method is invoked exactly once for the given admission identifier.
/// </summary>
[Test]
public async Task DeleteAdmissionByIdAsync_AdmissionExists_DeletesSuccessfully()
{
// Arrange
var admissionId = ObjectId.GenerateNewId();
var admission = new Admission { Id = admissionId, PointOfCareId = ObjectId.GenerateNewId() };
_admissionRepositoryMock.Setup(repo => repo.FindById(admissionId))
.ReturnsAsync(admission);
// Act
await _admissionService.DeleteAdmissionByIdAsync(admissionId);
// Assert
_admissionRepositoryMock.Verify(repo => repo.Delete(admissionId), Times.Once);
}
public async Task DeleteAdmissionByIdAsync_AdmissionExists_DeletesSuccessfully()
{
// Arrange
var admissionId = ObjectId.GenerateNewId();
var admission = new Admission { Id = admissionId, PointOfCareId = ObjectId.GenerateNewId() };
_admissionRepositoryMock.Setup(repo => repo.FindById(admissionId))
.ReturnsAsync(admission);
// Act
await _admissionService.DeleteAdmissionByIdAsync(admissionId);
// Assert
_admissionRepositoryMock.Verify(repo => repo.Delete(admissionId), Times.Once);
}
/// <summary>
/// Verifies that <see cref="AdmissionService.GetAdmissionByIdAsync"/> returns the admission together with its
/// associated patient location (unit, bed, and room) when the admission exists in the repository and the
/// corresponding point of care is successfully resolved.
/// </summary>
[Test]
public async Task GetAdmissionByIdAsync_AdmissionExists_ReturnsAdmissionWithPatientLocation()
{
// Arrange
var admissionId = ObjectId.GenerateNewId();
var pointOfCareId = ObjectId.GenerateNewId();
var admission = new Admission { Id = admissionId, PointOfCareId = pointOfCareId };
var poc = new PointOfCare { Id = pointOfCareId, UnitName = "TestUnit", Bed = "TestBed", Room = "TestRoom" };
_admissionRepositoryMock.Setup(repo => repo.FindById(admissionId))
.ReturnsAsync(admission);
_pointOfCareServiceMock.Setup(repo => repo.GetInfo(pointOfCareId, null, false, It.IsAny<CancellationToken>()))
.ReturnsAsync(poc);
// Act
var result = await _admissionService.GetAdmissionByIdAsync(admissionId);
// Assert
Assert.That(result, Is.Not.Null);
Assert.That(result?.PatientLocation, Is.Not.Null);
Assert.That(result?.PatientLocation?.UnitName, Is.EqualTo("TestUnit"));
Assert.That(result?.PatientLocation?.Bed, Is.EqualTo("TestBed"));
Assert.That(result?.PatientLocation?.Room, Is.EqualTo("TestRoom"));
}
public async Task GetAdmissionByIdAsync_AdmissionExists_ReturnsAdmissionWithPatientLocation()
{
// Arrange
var admissionId = ObjectId.GenerateNewId();
var pointOfCareId = ObjectId.GenerateNewId();
var admission = new Admission { Id = admissionId, PointOfCareId = pointOfCareId };
var poc = new PointOfCare { Id = pointOfCareId, UnitName = "TestUnit", Bed = "TestBed", Room = "TestRoom" };
_admissionRepositoryMock.Setup(repo => repo.FindById(admissionId))
.ReturnsAsync(admission);
_pointOfCareServiceMock.Setup(repo => repo.GetInfo(pointOfCareId, null, false, It.IsAny<CancellationToken>()))
.ReturnsAsync(poc);
// Act
var result = await _admissionService.GetAdmissionByIdAsync(admissionId);
// Assert
Assert.That(result, Is.Not.Null);
Assert.That(result?.PatientLocation, Is.Not.Null);
Assert.That(result?.PatientLocation?.UnitName, Is.EqualTo("TestUnit"));
Assert.That(result?.PatientLocation?.Bed, Is.EqualTo("TestBed"));
Assert.That(result?.PatientLocation?.Room, Is.EqualTo("TestRoom"));
}
/// <summary>
/// Verifies that GetAdmissionsAsync returns admissions enriched with their associated patient location details
/// retrieved from the point of care service.
/// </summary>
[Test]
public async Task GetAdmissionsAsync_ReturnsAdmissionsWithPatientLocations()
{
// Arrange
var admission1Id = ObjectId.GenerateNewId();
var admission2Id = ObjectId.GenerateNewId();
var poc1Id = ObjectId.GenerateNewId();
var poc2Id = ObjectId.GenerateNewId();
var admission1 = new Admission { Id = admission1Id, PointOfCareId = poc1Id };
var admission2 = new Admission { Id = admission2Id, PointOfCareId = poc2Id };
var admissionsList = new List<Admission> { admission1, admission2 };
_admissionRepositoryMock.Setup(repo => repo.FindAll())
.ReturnsAsync(admissionsList);
_pointOfCareServiceMock.Setup(repo => repo.GetInfo(poc1Id, null, false, It.IsAny<CancellationToken>()))
.ReturnsAsync(TestUtilities.CreateValidPointOfCare());
_pointOfCareServiceMock.Setup(repo => repo.GetInfo(poc2Id, null, false, It.IsAny<CancellationToken>()))
.ReturnsAsync(TestUtilities.CreateValidPointOfCare());
// Act
var result = await _admissionService.GetAdmissionsAsync();
// Assert
var admissions = result.ToList();
Assert.That(admissions, Is.Not.Null);
Assert.That(admissions.Count(), Is.EqualTo(2));
var admissionArray = admissions.ToArray();
Assert.That(admissionArray.First().PatientLocation, Is.Not.Null);
Assert.That(admissionArray.First().PatientLocation?.UnitName, Is.EqualTo("Test Unit"));
Assert.That(admissionArray.First().PatientLocation?.Bed, Is.EqualTo("Test Bed"));
Assert.That(admissionArray.First().PatientLocation?.Room, Is.EqualTo("Test Room"));
}
public async Task GetAdmissionsAsync_ReturnsAdmissionsWithPatientLocations()
{
// Arrange
var admission1Id = ObjectId.GenerateNewId();
var admission2Id = ObjectId.GenerateNewId();
var poc1Id = ObjectId.GenerateNewId();
var poc2Id = ObjectId.GenerateNewId();
var admission1 = new Admission { Id = admission1Id, PointOfCareId = poc1Id };
var admission2 = new Admission { Id = admission2Id, PointOfCareId = poc2Id };
var admissionsList = new List<Admission> { admission1, admission2 };
_admissionRepositoryMock.Setup(repo => repo.FindAll())
.ReturnsAsync(admissionsList);
_pointOfCareServiceMock.Setup(repo => repo.GetInfo(poc1Id, null, false, It.IsAny<CancellationToken>()))
.ReturnsAsync(TestUtilities.CreateValidPointOfCare());
_pointOfCareServiceMock.Setup(repo => repo.GetInfo(poc2Id, null, false, It.IsAny<CancellationToken>()))
.ReturnsAsync(TestUtilities.CreateValidPointOfCare());
// Act
var result = await _admissionService.GetAdmissionsAsync();
// Assert
var admissions = result.ToList();
Assert.That(admissions, Is.Not.Null);
Assert.That(admissions.Count(), Is.EqualTo(2));
var admissionArray = admissions.ToArray();
Assert.That(admissionArray.First().PatientLocation, Is.Not.Null);
Assert.That(admissionArray.First().PatientLocation?.UnitName, Is.EqualTo("Test Unit"));
Assert.That(admissionArray.First().PatientLocation?.Bed, Is.EqualTo("Test Bed"));
Assert.That(admissionArray.First().PatientLocation?.Room, Is.EqualTo("Test Room"));
}
// [Test]
// public async Task InsertAdmission_WhenPointOfCareExists_InsertsAdmission()
@@ -205,224 +224,255 @@ public class AdmissionServiceTest
// pointOfCareServiceMock.Verify(repo => repo.Update(It.IsAny<PointOfCare>()), Times.Once);
// }
/// <summary>
/// Verifies that <see cref="_admissionService"/>.UpdateAdmissionAsync correctly updates an existing admission by
/// invoking the repository's Update method and retrieving point-of-care information for both the new and the
/// previous point of care associated with the admission.
/// </summary>
[Test]
public async Task UpdateAdmissionAsync_WhenAdmissionExists_UpdatesAdmission()
{
// Arrange
var admissionId = ObjectId.GenerateNewId();
var admission = new Admission
public async Task UpdateAdmissionAsync_WhenAdmissionExists_UpdatesAdmission()
{
Id = admissionId,
AdmissionDate = DateTime.UtcNow,
Nhc = "TestNhc",
PointOfCareId = ObjectId.GenerateNewId(),
Person = new Person(),
Origin = TestUtilities.CreateValidOptionList(),
Diagnosis = TestUtilities.CreateValidOptionList(),
Allergies = [TestUtilities.CreateValidOptionList()],
Insulation = TestUtilities.CreateValidOptionList(),
LanguageBarrier = [TestUtilities.CreateValidOptionList()]
};
var oldAdmission = new Admission
{
Id = admissionId,
AdmissionDate = DateTime.UtcNow.AddDays(-1), // Update admission date
Nhc = "OldNhc",
PointOfCareId = ObjectId.GenerateNewId(), // Change PointOfCareId
Person = new Person(),
Origin = TestUtilities.CreateValidOptionList(),
Diagnosis = TestUtilities.CreateValidOptionList(),
Allergies = [TestUtilities.CreateValidOptionList()],
Insulation = TestUtilities.CreateValidOptionList(),
LanguageBarrier = [TestUtilities.CreateValidOptionList()]
};
_admissionRepositoryMock
.Setup(repo => repo.FindById(admissionId))
.ReturnsAsync(oldAdmission);
var updatedPointOfCare = TestUtilities.CreateValidPointOfCare();
_pointOfCareServiceMock
.Setup(repo => repo.GetInfo(admission.PointOfCareId.Value, null, false, It.IsAny<CancellationToken>()))
.ReturnsAsync(updatedPointOfCare);
// Act
await _admissionService.UpdateAdmissionAsync(admission);
// Assert
_admissionRepositoryMock.Verify(repo => repo.Update(admission), Times.Once);
_pointOfCareServiceMock.Verify(repo => repo.GetInfo(admission.PointOfCareId.Value, null, false, It.IsAny<CancellationToken>()), Times.Once);
_pointOfCareServiceMock.Verify(repo => repo.GetInfo(oldAdmission.PointOfCareId.Value, null, false, It.IsAny<CancellationToken>()), Times.Once);
}
[Test]
public async Task AdmitPatient_WithValidAdmission_CreatesPatientAndDeletesAdmission()
{
// Arrange
var admission = TestUtilities.CreateValidAdmission();
var unit = TestUtilities.CreateValidUnit();
var pointOfCare = TestUtilities.CreateValidPointOfCare();
_unitServiceMock
.Setup(repo => repo.FindById(admission.UnitId))
.ReturnsAsync(unit);
_pointOfCareServiceMock
.Setup(repo => repo.FindById(It.IsAny<ObjectId>()))
.ReturnsAsync((ObjectId id) => id == admission.PointOfCareId ? pointOfCare : null);
// Act
await _admissionService.AdmitPatient(admission);
// Assert
_patientServiceMock.Verify(repo => repo.Insert(It.IsAny<Patient>()), Times.Once);
}
[Test]
public async Task GetAdmissionByLocation_LocationExists_ReturnsAdmissions()
{
// Arrange
var location = new PatientLocation
{
UnitName = "Test Unit",
Bed = "Test Bed",
Room = "Test Room"
};
var expectedAdmissions = new List<Admission>
{
new() { Id = ObjectId.GenerateNewId() },
new() { Id = ObjectId.GenerateNewId() }
};
_admissionRepositoryMock.Setup(repo => repo.FindByLocation(location))
.ReturnsAsync(expectedAdmissions);
// Act
var result = await _admissionService.GetAdmissionByLocation(location);
// Assert
Assert.That(result, Is.Not.Null);
Assert.That(result, Is.EqualTo(expectedAdmissions));
}
[Test]
public async Task GetAdmissionByLocation_LocationNotFound_ReturnsNull()
{
// Arrange
var location = new PatientLocation
{
UnitName = "Nonexistent Unit",
Bed = "Nonexistent Bed",
Room = "Nonexistent Room"
};
_admissionRepositoryMock.Setup(repo => repo.FindByLocation(location))
.ThrowsAsync(new Exception());
// Act
var result = await _admissionService.GetAdmissionByLocation(location);
// Assert
Assert.That(result, Is.Empty);
}
[Test]
public async Task GetAdmissionByPointOfCareId_POCExists_ReturnsAdmissionsWithLocation()
{
// Arrange
var pocId = ObjectId.GenerateNewId();
var expectedAdmissions = new List<Admission>
{
new() { Id = ObjectId.GenerateNewId() },
new() { Id = ObjectId.GenerateNewId() }
};
var poc = new PointOfCare
{
Id = pocId,
UnitName = "Test Unit",
Bed = "Test Bed",
Room = "Test Room"
};
_admissionRepositoryMock.Setup(repo => repo.FindByPointOfCareId(pocId))
.ReturnsAsync(expectedAdmissions);
_pointOfCareServiceMock.Setup(repo => repo.GetInfo(pocId, null, false, It.IsAny<CancellationToken>()))
.ReturnsAsync(poc);
// Act
var result = await _admissionService.GetAdmissionByPointOfCareId(pocId);
// Assert
Assert.That(result, Is.Not.Null);
Assert.That(result.Count, Is.EqualTo(expectedAdmissions.Count));
foreach (var admission in result)
{
Assert.That(admission.PatientLocation, Is.Not.Null);
Assert.That(admission.PatientLocation?.UnitName, Is.EqualTo(poc.UnitName));
Assert.That(admission.PatientLocation?.Bed, Is.EqualTo(poc.Bed));
Assert.That(admission.PatientLocation?.Room, Is.EqualTo(poc.Room));
// Arrange
var admissionId = ObjectId.GenerateNewId();
var admission = new Admission
{
Id = admissionId,
AdmissionDate = DateTime.UtcNow,
Nhc = "TestNhc",
PointOfCareId = ObjectId.GenerateNewId(),
Person = new Person(),
Origin = TestUtilities.CreateValidOptionList(),
Diagnosis = TestUtilities.CreateValidOptionList(),
Allergies = [TestUtilities.CreateValidOptionList()],
Insulation = TestUtilities.CreateValidOptionList(),
LanguageBarrier = [TestUtilities.CreateValidOptionList()]
};
var oldAdmission = new Admission
{
Id = admissionId,
AdmissionDate = DateTime.UtcNow.AddDays(-1), // Update admission date
Nhc = "OldNhc",
PointOfCareId = ObjectId.GenerateNewId(), // Change PointOfCareId
Person = new Person(),
Origin = TestUtilities.CreateValidOptionList(),
Diagnosis = TestUtilities.CreateValidOptionList(),
Allergies = [TestUtilities.CreateValidOptionList()],
Insulation = TestUtilities.CreateValidOptionList(),
LanguageBarrier = [TestUtilities.CreateValidOptionList()]
};
_admissionRepositoryMock
.Setup(repo => repo.FindById(admissionId))
.ReturnsAsync(oldAdmission);
var updatedPointOfCare = TestUtilities.CreateValidPointOfCare();
_pointOfCareServiceMock
.Setup(repo => repo.GetInfo(admission.PointOfCareId.Value, null, false, It.IsAny<CancellationToken>()))
.ReturnsAsync(updatedPointOfCare);
// Act
await _admissionService.UpdateAdmissionAsync(admission);
// Assert
_admissionRepositoryMock.Verify(repo => repo.Update(admission), Times.Once);
_pointOfCareServiceMock.Verify(repo => repo.GetInfo(admission.PointOfCareId.Value, null, false, It.IsAny<CancellationToken>()), Times.Once);
_pointOfCareServiceMock.Verify(repo => repo.GetInfo(oldAdmission.PointOfCareId.Value, null, false, It.IsAny<CancellationToken>()), Times.Once);
}
}
/// <summary>
/// Verifies that admitting a patient with a valid admission—where the associated unit is found and the point of care matches—results in a new patient being inserted.
/// </summary>
[Test]
public async Task GetAdmissionByPointOfCareId_POCNotFound_ReturnsNull()
{
// Arrange
var pocId = ObjectId.GenerateNewId();
_admissionRepositoryMock.Setup(repo => repo.FindByPointOfCareId(pocId))
.ThrowsAsync(new Exception("Repository error"));
// Act
var result = await _admissionService.GetAdmissionByPointOfCareId(pocId);
// Assert
Assert.That(result, Is.Empty);
}
[Test]
public async Task GetAdmissionByUnitIdWithOutPoC_UnitIdExists_ReturnsAdmissions()
{
// Arrange
var unitId = ObjectId.GenerateNewId();
var expectedAdmissions = new List<Admission>
public async Task AdmitPatient_WithValidAdmission_CreatesPatientAndDeletesAdmission()
{
new() { Id = ObjectId.GenerateNewId() },
new() { Id = ObjectId.GenerateNewId() }
};
_admissionRepositoryMock.Setup(repo => repo.GetAdmissionByUnitIdWithOutPoC(unitId))
.ReturnsAsync(expectedAdmissions);
// Act
var result = await _admissionService.GetAdmissionByUnitIdWithOutPoC(unitId);
// Assert
Assert.That(result, Is.Not.Null);
Assert.That(result.Count, Is.EqualTo(expectedAdmissions.Count));
Assert.That(result, Is.EqualTo(expectedAdmissions));
}
// Arrange
var admission = TestUtilities.CreateValidAdmission();
var unit = TestUtilities.CreateValidUnit();
var pointOfCare = TestUtilities.CreateValidPointOfCare();
_unitServiceMock
.Setup(repo => repo.FindById(admission.UnitId))
.ReturnsAsync(unit);
_pointOfCareServiceMock
.Setup(repo => repo.FindById(It.IsAny<ObjectId>()))
.ReturnsAsync((ObjectId id) => id == admission.PointOfCareId ? pointOfCare : null);
// Act
await _admissionService.AdmitPatient(admission);
// Assert
_patientServiceMock.Verify(repo => repo.Insert(It.IsAny<Patient>()), Times.Once);
}
/// <summary>
/// Verifies that GetAdmissionByLocation returns the expected list of admissions when a matching patient location is found, confirming the service correctly retrieves results from the repository.
/// </summary>
[Test]
public async Task GetAdmissionByUnitIdWithOutPoC_UnitIdNotFound_ReturnsNull()
{
// Arrange
var unitId = ObjectId.GenerateNewId();
public async Task GetAdmissionByLocation_LocationExists_ReturnsAdmissions()
{
// Arrange
var location = new PatientLocation
{
UnitName = "Test Unit",
Bed = "Test Bed",
Room = "Test Room"
};
var expectedAdmissions = new List<Admission>
{
new() { Id = ObjectId.GenerateNewId() },
new() { Id = ObjectId.GenerateNewId() }
};
_admissionRepositoryMock.Setup(repo => repo.FindByLocation(location))
.ReturnsAsync(expectedAdmissions);
// Act
var result = await _admissionService.GetAdmissionByLocation(location);
// Assert
Assert.That(result, Is.Not.Null);
Assert.That(result, Is.EqualTo(expectedAdmissions));
}
_admissionRepositoryMock.Setup(repo => repo.GetAdmissionByUnitIdWithOutPoC(unitId))
.ThrowsAsync(new Exception("Repository error"));
/// <summary>
/// Verifies that <see cref="AdmissionService.GetAdmissionByLocation"/> returns an empty result when the requested location does not exist, simulating the not-found scenario by having the repository throw an exception.
/// </summary>
[Test]
public async Task GetAdmissionByLocation_LocationNotFound_ReturnsNull()
{
// Arrange
var location = new PatientLocation
{
UnitName = "Nonexistent Unit",
Bed = "Nonexistent Bed",
Room = "Nonexistent Room"
};
_admissionRepositoryMock.Setup(repo => repo.FindByLocation(location))
.ThrowsAsync(new Exception());
// Act
var result = await _admissionService.GetAdmissionByLocation(location);
// Assert
Assert.That(result, Is.Empty);
}
// Act
var result = await _admissionService.GetAdmissionByUnitIdWithOutPoC(unitId);
/// <summary>
/// Verifies that when a Point of Care exists for the given identifier, the admissions returned by
/// <c>GetAdmissionByPointOfCareId</c> are enriched with patient location details (unit name, bed, and room)
/// retrieved from the corresponding Point of Care entity.
/// </summary>
[Test]
public async Task GetAdmissionByPointOfCareId_POCExists_ReturnsAdmissionsWithLocation()
{
// Arrange
var pocId = ObjectId.GenerateNewId();
var expectedAdmissions = new List<Admission>
{
new() { Id = ObjectId.GenerateNewId() },
new() { Id = ObjectId.GenerateNewId() }
};
var poc = new PointOfCare
{
Id = pocId,
UnitName = "Test Unit",
Bed = "Test Bed",
Room = "Test Room"
};
_admissionRepositoryMock.Setup(repo => repo.FindByPointOfCareId(pocId))
.ReturnsAsync(expectedAdmissions);
_pointOfCareServiceMock.Setup(repo => repo.GetInfo(pocId, null, false, It.IsAny<CancellationToken>()))
.ReturnsAsync(poc);
// Act
var result = await _admissionService.GetAdmissionByPointOfCareId(pocId);
// Assert
Assert.That(result, Is.Not.Null);
Assert.That(result.Count, Is.EqualTo(expectedAdmissions.Count));
foreach (var admission in result)
{
Assert.That(admission.PatientLocation, Is.Not.Null);
Assert.That(admission.PatientLocation?.UnitName, Is.EqualTo(poc.UnitName));
Assert.That(admission.PatientLocation?.Bed, Is.EqualTo(poc.Bed));
Assert.That(admission.PatientLocation?.Room, Is.EqualTo(poc.Room));
}
}
// Assert
Assert.That(result, Is.Empty);
}
/// <summary>
/// Verifies that <see cref="AdmissionService.GetAdmissionByPointOfCareId"/> returns an empty result when the underlying repository throws an exception while attempting to find an admission by point of care id.
/// </summary>
[Test]
public async Task GetAdmissionByPointOfCareId_POCNotFound_ReturnsNull()
{
// Arrange
var pocId = ObjectId.GenerateNewId();
_admissionRepositoryMock.Setup(repo => repo.FindByPointOfCareId(pocId))
.ThrowsAsync(new Exception("Repository error"));
// Act
var result = await _admissionService.GetAdmissionByPointOfCareId(pocId);
// Assert
Assert.That(result, Is.Empty);
}
/// <summary>
/// Verifies that <see cref="AdmissionService.GetAdmissionByUnitIdWithOutPoC"/> returns the expected list of admissions
/// when a valid unit identifier is provided.
/// </summary>
[Test]
public async Task GetAdmissionByUnitIdWithOutPoC_UnitIdExists_ReturnsAdmissions()
{
// Arrange
var unitId = ObjectId.GenerateNewId();
var expectedAdmissions = new List<Admission>
{
new() { Id = ObjectId.GenerateNewId() },
new() { Id = ObjectId.GenerateNewId() }
};
_admissionRepositoryMock.Setup(repo => repo.GetAdmissionByUnitIdWithOutPoC(unitId))
.ReturnsAsync(expectedAdmissions);
// Act
var result = await _admissionService.GetAdmissionByUnitIdWithOutPoC(unitId);
// Assert
Assert.That(result, Is.Not.Null);
Assert.That(result.Count, Is.EqualTo(expectedAdmissions.Count));
Assert.That(result, Is.EqualTo(expectedAdmissions));
}
/// <summary>
/// Verifies that <c>GetAdmissionByUnitIdWithOutPoC</c> returns an empty result when the underlying repository
/// throws an exception while looking up the given unit identifier.
/// </summary>
/// <param name="unitId">The identifier of the unit whose admission record is being requested.</param>
[Test]
public async Task GetAdmissionByUnitIdWithOutPoC_UnitIdNotFound_ReturnsNull()
{
// Arrange
var unitId = ObjectId.GenerateNewId();
_admissionRepositoryMock.Setup(repo => repo.GetAdmissionByUnitIdWithOutPoC(unitId))
.ThrowsAsync(new Exception("Repository error"));
// Act
var result = await _admissionService.GetAdmissionByUnitIdWithOutPoC(unitId);
// Assert
Assert.That(result, Is.Empty);
}
// [Test]
// public async Task SaveRequest_NewAdmission_ValidRequest_InsertsAdmission()
@@ -523,52 +573,55 @@ public class AdmissionServiceTest
// admissionServiceMock.Verify(service => service.SaveRequest(apiRequest), Times.Once);
// }
/// <summary>
/// Verifies that InsertAdmission throws a NotFoundException and performs no insert or update operations when a required associated resource is not found while processing a valid admission.
/// </summary>
[Test]
public async Task InsertAdmission_ValidAdmission_InsertsSuccessfullyWithUser()
{
// Arrange
var admission = new Admission
public async Task InsertAdmission_ValidAdmission_InsertsSuccessfullyWithUser()
{
Id = ObjectId.GenerateNewId(),
AdmissionDate = DateTime.UtcNow,
Nhc = "TestNhc",
PointOfCareId = ObjectId.GenerateNewId(),
Person = new Person(),
Origin = new OptionList(),
Diagnosis = new OptionList(),
Allergies = [new OptionList()],
Insulation = new OptionList(),
LanguageBarrier = [new OptionList()]
};
var pointOfCare = TestUtilities.CreateValidPointOfCare();
_admissionRepositoryMock
.Setup(repo => repo.FindByNhc(admission.Nhc))
.ReturnsAsync((Admission?)null);
_pointOfCareServiceMock
.Setup(repo => repo.GetInfo(admission.PointOfCareId.Value, null, false, It.IsAny<CancellationToken>()))
.ReturnsAsync(pointOfCare);
_admissionRepositoryMock
.Setup(repo => repo.InsertOneAsyncAndReturn(admission))
.ReturnsAsync(admission);
Func<Task> act = () => _admissionService.InsertAdmission(admission);
var ex = Assert.ThrowsAsync<NotFoundException>(act);
Assert.That(ex!.Message,
Is.EqualTo(((int)HttpEnum.ErrorMessage.NotFoundResourceMissing).ToString()));
// Verify no insert
_admissionRepositoryMock
.Verify(repo => repo.InsertOneAsyncAndReturn(It.IsAny<Admission>()), Times.Never);
// Verify no update
_pointOfCareServiceMock
.Verify(service => service.Update(It.IsAny<PointOfCare>()), Times.Never);
}
// Arrange
var admission = new Admission
{
Id = ObjectId.GenerateNewId(),
AdmissionDate = DateTime.UtcNow,
Nhc = "TestNhc",
PointOfCareId = ObjectId.GenerateNewId(),
Person = new Person(),
Origin = new OptionList(),
Diagnosis = new OptionList(),
Allergies = [new OptionList()],
Insulation = new OptionList(),
LanguageBarrier = [new OptionList()]
};
var pointOfCare = TestUtilities.CreateValidPointOfCare();
_admissionRepositoryMock
.Setup(repo => repo.FindByNhc(admission.Nhc))
.ReturnsAsync((Admission?)null);
_pointOfCareServiceMock
.Setup(repo => repo.GetInfo(admission.PointOfCareId.Value, null, false, It.IsAny<CancellationToken>()))
.ReturnsAsync(pointOfCare);
_admissionRepositoryMock
.Setup(repo => repo.InsertOneAsyncAndReturn(admission))
.ReturnsAsync(admission);
Func<Task> act = () => _admissionService.InsertAdmission(admission);
var ex = Assert.ThrowsAsync<NotFoundException>(act);
Assert.That(ex!.Message,
Is.EqualTo(((int)HttpEnum.ErrorMessage.NotFoundResourceMissing).ToString()));
// Verify no insert
_admissionRepositoryMock
.Verify(repo => repo.InsertOneAsyncAndReturn(It.IsAny<Admission>()), Times.Never);
// Verify no update
_pointOfCareServiceMock
.Verify(service => service.Update(It.IsAny<PointOfCare>()), Times.Never);
}
}