Creado apartir del commit b888de6c92056de294456f581a56e25e734d4ab7 de develop
This commit is contained in:
@@ -0,0 +1,390 @@
|
||||
using adas_core.Domain.Enums;
|
||||
using adas_core.Domain.Models;
|
||||
using adas_core.Domain.Models.AppSettings;
|
||||
using adas_core.Domain.Models.MongoModels;
|
||||
using adas_core.Infrastructure.Repositories;
|
||||
using adas_core.Test.Utilities;
|
||||
using Microsoft.Extensions.Options;
|
||||
using MongoDB.Bson;
|
||||
using Options = Microsoft.Extensions.Options.Options;
|
||||
|
||||
namespace adas_core.Test.Repositories;
|
||||
|
||||
[TestFixture]
|
||||
[Category("Integration")]
|
||||
public class AdmissionRepositoryTest
|
||||
{
|
||||
[OneTimeSetUp]
|
||||
public async Task Init()
|
||||
{
|
||||
_optionsApiSettings = Options.Create(_apiSettings);
|
||||
|
||||
|
||||
await IntegrationDb.Database.DropCollectionAsync("admissions");
|
||||
await IntegrationDb.Database.CreateCollectionAsync("admissions");
|
||||
|
||||
_repository = new AdmissionRepository(_optionsApiSettings, IntegrationDb.Database);
|
||||
|
||||
var admissions = new List<Admission>
|
||||
{
|
||||
TestUtilities.CreateValidAdmission(),
|
||||
TestUtilities.CreateValidAdmission(),
|
||||
TestUtilities.CreateValidAdmission()
|
||||
};
|
||||
|
||||
foreach (var admission in admissions) await _repository.InsertOneAsync(admission);
|
||||
}
|
||||
|
||||
private AdmissionRepository _repository;
|
||||
private IOptions<ApiSettings> _optionsApiSettings;
|
||||
|
||||
private readonly ApiSettings _apiSettings = new()
|
||||
{
|
||||
Admissions = "admissions"
|
||||
};
|
||||
|
||||
[Test]
|
||||
public async Task InsertOneAsync_Success()
|
||||
{
|
||||
// Arrange
|
||||
var admission = TestUtilities.CreateValidAdmission();
|
||||
|
||||
// Act
|
||||
await _repository.InsertOneAsync(admission);
|
||||
|
||||
// Assert
|
||||
var result = await _repository.FindById(admission.Id);
|
||||
|
||||
Assert.That(result, Is.Not.Null);
|
||||
Assert.That(result?.Id, Is.EqualTo(admission.Id));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Delete_Success()
|
||||
{
|
||||
// Arrange
|
||||
var admission = TestUtilities.CreateValidAdmission();
|
||||
await _repository.InsertOneAsync(admission);
|
||||
|
||||
// Act
|
||||
await _repository.Delete(admission.Id);
|
||||
|
||||
// Assert
|
||||
var result = await _repository.FindById(admission.Id);
|
||||
Assert.That(result, Is.Null);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Update_Success()
|
||||
{
|
||||
// Arrange
|
||||
var admission = TestUtilities.CreateValidAdmission();
|
||||
await _repository.InsertOneAsync(admission);
|
||||
|
||||
// Modify the admission
|
||||
admission.DiagnosisAux = "Updated value";
|
||||
|
||||
// Act
|
||||
await _repository.Update(admission);
|
||||
|
||||
// Assert
|
||||
var updatedAdmission = await _repository.FindById(admission.Id);
|
||||
Assert.That(updatedAdmission, Is.Not.Null);
|
||||
Assert.That(updatedAdmission?.DiagnosisAux, Is.EqualTo(admission.DiagnosisAux));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task UpdateLocation_Success()
|
||||
{
|
||||
// Arrange
|
||||
var admission = TestUtilities.CreateValidAdmission();
|
||||
var originalLocationId = ObjectId.GenerateNewId();
|
||||
var newLocationId = ObjectId.GenerateNewId();
|
||||
|
||||
admission.PointOfCareId = originalLocationId;
|
||||
await _repository.InsertOneAsync(admission);
|
||||
|
||||
// Act
|
||||
await _repository.UpdateLocation(admission.Id, newLocationId);
|
||||
|
||||
// Assert
|
||||
var updatedAdmission = await _repository.FindById(admission.Id);
|
||||
Assert.That(updatedAdmission, Is.Not.Null);
|
||||
Assert.That(newLocationId, Is.EqualTo(updatedAdmission?.PointOfCareId));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task UpdatePatient_Success()
|
||||
{
|
||||
// Arrange
|
||||
var admission = TestUtilities.CreateValidAdmission();
|
||||
var newPatient = new Person
|
||||
{
|
||||
LastName = "Doe",
|
||||
FirstName = "John",
|
||||
BirthDate = new DateTime(1990, 1, 1),
|
||||
Gender = PatientEnum.Gender.Male
|
||||
};
|
||||
|
||||
await _repository.InsertOneAsync(admission);
|
||||
|
||||
// Act
|
||||
await _repository.UpdatePatient(admission.Id, newPatient);
|
||||
|
||||
// Assert
|
||||
var updatedAdmission = await _repository.FindById(admission.Id);
|
||||
Assert.That(updatedAdmission, Is.Not.Null);
|
||||
Assert.That(updatedAdmission?.Person, Is.Not.Null);
|
||||
Assert.That(updatedAdmission?.Person?.FirstName, Is.EqualTo(newPatient.FirstName));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task FindAll_Success()
|
||||
{
|
||||
// Arrange
|
||||
|
||||
|
||||
// Act
|
||||
var result = await _repository.FindAll();
|
||||
|
||||
// Assert
|
||||
var admissions = result as Admission[] ?? result.ToArray();
|
||||
Assert.That(admissions, Is.Not.Null);
|
||||
Assert.That(admissions, Is.Not.Empty);
|
||||
Assert.That(admissions.Count(), Is.EqualTo(3));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task FindById_Success()
|
||||
{
|
||||
// Arrange
|
||||
var admission = TestUtilities.CreateValidAdmission();
|
||||
await _repository.InsertOneAsync(admission);
|
||||
|
||||
// Act
|
||||
var result = await _repository.FindById(admission.Id);
|
||||
|
||||
// Assert
|
||||
Assert.That(result, Is.Not.Null);
|
||||
Assert.That(admission.Id, Is.EqualTo(result?.Id));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task FindById_NotFound()
|
||||
{
|
||||
// Arrange
|
||||
var nonExistingId = ObjectId.GenerateNewId();
|
||||
|
||||
// Act
|
||||
var result = await _repository.FindById(nonExistingId);
|
||||
|
||||
// Assert
|
||||
Assert.That(result, Is.Null);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task FindByNhc_Success()
|
||||
{
|
||||
// Arrange
|
||||
var admission = TestUtilities.CreateValidAdmission();
|
||||
await _repository.InsertOneAsync(admission);
|
||||
|
||||
// Act
|
||||
var result = await _repository.FindByNhc(admission.Nhc);
|
||||
|
||||
// Assert
|
||||
Assert.That(result, Is.Not.Null);
|
||||
Assert.That(admission.Nhc, Is.EqualTo(result?.Nhc));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task FindByNhc_NotFound()
|
||||
{
|
||||
// Arrange
|
||||
var nonExistingNhc = "non-existing-nhc";
|
||||
|
||||
// Act
|
||||
var result = await _repository.FindByNhc(nonExistingNhc);
|
||||
|
||||
// Assert
|
||||
Assert.That(result, Is.Null);
|
||||
}
|
||||
|
||||
// [Test]
|
||||
// public async Task FindByLocation_Success()
|
||||
// {
|
||||
// // Arrange
|
||||
// var location = new PatientLocation
|
||||
// {
|
||||
// UnitName = "TestUnit",
|
||||
// Bed = "TestBed",
|
||||
// Room = "TestRoom"
|
||||
// };
|
||||
//
|
||||
// var admission1 = TestUtilities.CreateValidAdmission();
|
||||
// admission1.PatientLocation = location;
|
||||
//
|
||||
// var admission2 = TestUtilities.CreateValidAdmission();
|
||||
// admission2.PatientLocation = location;
|
||||
//
|
||||
// await _repository.InsertOneAsync(admission1);
|
||||
// await _repository.InsertOneAsync(admission2);
|
||||
//
|
||||
// // Act
|
||||
// var result = await _repository.FindByLocation(location);
|
||||
//
|
||||
// // Assert
|
||||
// Assert.That(result, Is.Not.Null);
|
||||
// Assert.That(result, Is.Not.Empty);
|
||||
// Assert.That(result.Count, Is.EqualTo(2));
|
||||
//
|
||||
// foreach (var admission in result)
|
||||
// {
|
||||
// Assert.That(admission.PatientLocation, Is.Not.Null);
|
||||
// Assert.That(location.UnitName, Is.EqualTo(admission?.PatientLocation?.UnitName));
|
||||
// Assert.That(location.Bed, Is.EqualTo(admission?.PatientLocation?.Bed));
|
||||
// Assert.That(location.Room, Is.EqualTo(admission?.PatientLocation?.Room));
|
||||
// }
|
||||
// }
|
||||
|
||||
// [Test]
|
||||
// public async Task FindByOrigin_Success()
|
||||
// {
|
||||
// // Arrange
|
||||
// var origin = "TestOrigin";
|
||||
// var admission1 = TestUtilities.CreateValidAdmission();
|
||||
// admission1.Origin = TestUtilities.CreateValidOptionList();
|
||||
//
|
||||
// var admission2 = TestUtilities.CreateValidAdmission();
|
||||
// admission2.Origin = TestUtilities.CreateValidOptionList();
|
||||
//
|
||||
// await _repository.InsertOneAsync(admission1);
|
||||
// await _repository.InsertOneAsync(admission2);
|
||||
//
|
||||
// // Act
|
||||
// var result = await _repository.FindByOrigin(origin);
|
||||
// var resultList = result?.ToList();
|
||||
//
|
||||
// // Assert
|
||||
// Assert.That(resultList, Is.Not.Null);
|
||||
// Assert.That(resultList, Is.Not.Empty);
|
||||
// Assert.That(resultList?.Count(), Is.EqualTo(2));
|
||||
// }
|
||||
|
||||
[Test]
|
||||
public async Task InsertOneAsyncAndReturn_Success()
|
||||
{
|
||||
// Arrange
|
||||
var admission = TestUtilities.CreateValidAdmission();
|
||||
|
||||
// Act
|
||||
var result = await _repository.InsertOneAsyncAndReturn(admission);
|
||||
|
||||
// Assert
|
||||
Assert.That(result, Is.Not.Null);
|
||||
Assert.That(result, Is.EqualTo(admission));
|
||||
var retrievedAdmission = await _repository.FindById(admission.Id);
|
||||
Assert.That(retrievedAdmission, Is.Not.Null);
|
||||
Assert.That(retrievedAdmission?.Id, Is.EqualTo(admission.Id));
|
||||
}
|
||||
|
||||
|
||||
[Test]
|
||||
public async Task GetAdmissionByUnitIdWithOutPoC_Success()
|
||||
{
|
||||
// Arrange
|
||||
var unitId = ObjectId.GenerateNewId();
|
||||
var admission1 = TestUtilities.CreateValidAdmission();
|
||||
admission1.UnitId = unitId;
|
||||
admission1.PointOfCareId = null;
|
||||
|
||||
var admission2 = TestUtilities.CreateValidAdmission();
|
||||
admission2.UnitId = ObjectId.GenerateNewId();
|
||||
admission2.PointOfCareId = ObjectId.GenerateNewId();
|
||||
|
||||
await _repository.InsertOneAsync(admission1);
|
||||
await _repository.InsertOneAsync(admission2);
|
||||
|
||||
// Act
|
||||
var result = await _repository.GetAdmissionByUnitIdWithOutPoC(unitId);
|
||||
|
||||
// Assert
|
||||
Assert.That(result, Is.Not.Null);
|
||||
Assert.That(result.Count, Is.EqualTo(1));
|
||||
Assert.That(result[0].UnitId, Is.EqualTo(unitId));
|
||||
Assert.That(result[0].PointOfCareId, Is.Null);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task GetAdmissionByUnitIdWithOutPoC_NoMatch()
|
||||
{
|
||||
// Arrange
|
||||
var unitId = ObjectId.GenerateNewId();
|
||||
|
||||
// Act
|
||||
var result = await _repository.GetAdmissionByUnitIdWithOutPoC(unitId);
|
||||
|
||||
// Assert
|
||||
Assert.That(result, Is.Not.Null);
|
||||
Assert.That(result, Is.Empty);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task FindByPointOfCareId_Success()
|
||||
{
|
||||
var pocId = ObjectId.GenerateNewId();
|
||||
var admission1 = TestUtilities.CreateValidAdmission();
|
||||
admission1.PointOfCareId = pocId;
|
||||
|
||||
var admission2 = TestUtilities.CreateValidAdmission();
|
||||
admission2.PointOfCareId = ObjectId.GenerateNewId();
|
||||
|
||||
await _repository.InsertOneAsync(admission1);
|
||||
await _repository.InsertOneAsync(admission2);
|
||||
|
||||
var result = await _repository.FindByPointOfCareId(pocId);
|
||||
|
||||
Assert.That(result, Is.Not.Null);
|
||||
Assert.That(result.Count, Is.EqualTo(1));
|
||||
Assert.That(result[0].PointOfCareId, Is.EqualTo(pocId));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task FindByPointOfCareId_NoMatch()
|
||||
{
|
||||
var pocId = ObjectId.GenerateNewId();
|
||||
|
||||
var result = await _repository.FindByPointOfCareId(pocId);
|
||||
|
||||
Assert.That(result, Is.Not.Null);
|
||||
Assert.That(result, Is.Empty);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task FindByDiagnosis_Success()
|
||||
{
|
||||
var diagnosis = "SomeDiagnosis";
|
||||
var admission1 = TestUtilities.CreateValidAdmission();
|
||||
admission1.Diagnosis = TestUtilities.CreateValidOptionList();
|
||||
|
||||
var admission2 = TestUtilities.CreateValidAdmission();
|
||||
admission2.Diagnosis = TestUtilities.CreateValidOptionList();
|
||||
|
||||
await _repository.InsertOneAsync(admission1);
|
||||
await _repository.InsertOneAsync(admission2);
|
||||
|
||||
var result = await _repository.FindByDiagnosis(diagnosis);
|
||||
|
||||
Assert.That(result, Is.Not.Null);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task FindByDiagnosis_NoMatch()
|
||||
{
|
||||
var diagnosis = "NonExistentDiagnosis";
|
||||
|
||||
var result = await _repository.FindByDiagnosis(diagnosis);
|
||||
|
||||
Assert.That(result, Is.Empty);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,233 @@
|
||||
using adas_core.Domain.Models;
|
||||
using adas_core.Domain.Models.AppSettings;
|
||||
using adas_core.Domain.Models.GroupedObservations;
|
||||
using adas_core.Infrastructure.Repositories;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Options;
|
||||
using MongoDB.Bson;
|
||||
using Moq;
|
||||
using Options = Microsoft.Extensions.Options.Options;
|
||||
|
||||
namespace adas_core.Test.Repositories;
|
||||
|
||||
[TestFixture]
|
||||
[Order(1)]
|
||||
[Category("Integration")]
|
||||
public class AlarmRepositoryTest
|
||||
{
|
||||
[OneTimeSetUp]
|
||||
public async Task Init()
|
||||
{
|
||||
_optionsApiSettings = Options.Create(_apiSettings);
|
||||
|
||||
|
||||
var pObsAlarm1 = new PatientObservationAlarm
|
||||
{
|
||||
Id = Id,
|
||||
PatientId = PatientId,
|
||||
Code = "code1",
|
||||
CodingSystem = "SNM",
|
||||
Name = "name1",
|
||||
Time = Now,
|
||||
Value = "value1",
|
||||
SystemId = "systemId1"
|
||||
};
|
||||
|
||||
var pObsAlarm2 = new PatientObservationAlarm
|
||||
{
|
||||
Id = ObjectId.GenerateNewId(),
|
||||
PatientId = PatientId,
|
||||
Code = "code2",
|
||||
CodingSystem = "SNM",
|
||||
Name = "name2",
|
||||
Time = Now,
|
||||
Value = "value2",
|
||||
SystemId = "systemId2"
|
||||
};
|
||||
|
||||
await IntegrationDb.Database.DropCollectionAsync("patients_alarms");
|
||||
await IntegrationDb.Database.CreateCollectionAsync("patients_alarms");
|
||||
|
||||
_logger = new Mock<ILogger<AlarmRepository>>();
|
||||
|
||||
_repository = new AlarmRepository(_optionsApiSettings, IntegrationDb.Database, _logger.Object);
|
||||
|
||||
await _repository.InsertOneAsync(pObsAlarm1);
|
||||
await _repository.InsertOneAsync(pObsAlarm2);
|
||||
}
|
||||
|
||||
private AlarmRepository _repository;
|
||||
|
||||
private readonly ApiSettings _apiSettings = new()
|
||||
{
|
||||
PatientsAlarms = "patients_alarms"
|
||||
};
|
||||
|
||||
private IOptions<ApiSettings>? _optionsApiSettings;
|
||||
private Mock<ILogger<AlarmRepository>> _logger;
|
||||
|
||||
private static readonly DateTime Now = DateTime.UtcNow;
|
||||
private static readonly ObjectId PatientId = ObjectId.GenerateNewId();
|
||||
private static readonly ObjectId Id = ObjectId.GenerateNewId();
|
||||
|
||||
[Test]
|
||||
public async Task AggregatedPatientLastObservationsByField_Not_Found_Returns_Empty_List()
|
||||
{
|
||||
var filter = new List<Field>
|
||||
{
|
||||
new()
|
||||
{
|
||||
Name = "name100",
|
||||
Last = 2,
|
||||
OnlyExpired = false
|
||||
}
|
||||
};
|
||||
|
||||
var result = await _repository.AggregatedPatientLastObservationsByField(PatientId, filter);
|
||||
|
||||
Assert.That(result, Is.Not.Null);
|
||||
Assert.That(result, Is.Empty);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task AggregatedPatientLastObservationsByField_Found_Returns_1()
|
||||
{
|
||||
var filter = new List<Field>
|
||||
{
|
||||
new()
|
||||
{
|
||||
Name = "name1",
|
||||
Last = 2,
|
||||
OnlyExpired = false
|
||||
}
|
||||
};
|
||||
|
||||
var result = await _repository.AggregatedPatientLastObservationsByField(PatientId, filter);
|
||||
|
||||
Assert.That(result, Is.Not.Null);
|
||||
Assert.That(result.Count(), Is.EqualTo(1));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task AggregatedPatientLastObservationsByField_Found_Returns_2()
|
||||
{
|
||||
var filter = new List<Field>
|
||||
{
|
||||
new()
|
||||
{
|
||||
Name = "name1",
|
||||
Last = 1,
|
||||
OnlyExpired = false
|
||||
},
|
||||
new()
|
||||
{
|
||||
Name = "name2",
|
||||
Last = 1,
|
||||
OnlyExpired = false
|
||||
}
|
||||
};
|
||||
|
||||
var result = await _repository.AggregatedPatientLastObservationsByField(PatientId, filter);
|
||||
|
||||
Assert.That(result, Is.Not.Null);
|
||||
Assert.That(result.Count(), Is.EqualTo(2));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task AggregatedPatientLastObservationsByField_AllExpired_Returns_Empty_List()
|
||||
{
|
||||
var expiredTime = Now.AddDays(-1); // Set a time in the past to simulate expiration
|
||||
|
||||
var expiredObs = new PatientObservationAlarm
|
||||
{
|
||||
Id = ObjectId.GenerateNewId(),
|
||||
PatientId = PatientId,
|
||||
Code = "code3",
|
||||
CodingSystem = "SNM",
|
||||
Name = "name3",
|
||||
Time = expiredTime,
|
||||
Value = "value3",
|
||||
SystemId = "systemId3",
|
||||
Expired = true
|
||||
};
|
||||
|
||||
await _repository.InsertOneAsync(expiredObs);
|
||||
|
||||
var filter = new List<Field>
|
||||
{
|
||||
new()
|
||||
{
|
||||
Name = "name3",
|
||||
Last = 2,
|
||||
OnlyExpired = true
|
||||
}
|
||||
};
|
||||
|
||||
var result = await _repository.AggregatedPatientLastObservationsByField(PatientId, filter);
|
||||
|
||||
Assert.That(result, Is.Not.Null);
|
||||
Assert.That(result, Is.Empty);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task AggregatedPatientLastObservationsByField_MultipleObservations_Returns_Most_Recent()
|
||||
{
|
||||
var oldTime = Now.AddMinutes(-10); // Set an older time
|
||||
|
||||
var oldObs = new PatientObservationAlarm
|
||||
{
|
||||
Id = ObjectId.GenerateNewId(),
|
||||
PatientId = PatientId,
|
||||
Code = "code4",
|
||||
CodingSystem = "SNM",
|
||||
Name = "name4",
|
||||
Time = oldTime,
|
||||
Value = "value4",
|
||||
SystemId = "systemId4"
|
||||
};
|
||||
|
||||
var newObs = new PatientObservationAlarm
|
||||
{
|
||||
Id = ObjectId.GenerateNewId(),
|
||||
PatientId = PatientId,
|
||||
Code = "code4",
|
||||
CodingSystem = "SNM",
|
||||
Name = "name4",
|
||||
Time = Now,
|
||||
Value = "value5",
|
||||
SystemId = "systemId5"
|
||||
};
|
||||
|
||||
await _repository.InsertOneAsync(oldObs);
|
||||
await _repository.InsertOneAsync(newObs);
|
||||
|
||||
var filter = new List<Field>
|
||||
{
|
||||
new()
|
||||
{
|
||||
Name = "name4",
|
||||
Last = 1,
|
||||
OnlyExpired = false
|
||||
}
|
||||
};
|
||||
|
||||
var result = await _repository.AggregatedPatientLastObservationsByField(PatientId, filter);
|
||||
|
||||
Assert.That(result, Is.Not.Null);
|
||||
using (Assert.EnterMultipleScope())
|
||||
{
|
||||
|
||||
Assert.That(result, Has.Count.EqualTo(1));
|
||||
Assert.That(result.First().Value, Is.EqualTo("value5")); // Ensure the most recent observation is returned
|
||||
};
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task AggregatedPatientLastObservationsByField_FilterObservations_Null_Returns_All()
|
||||
{
|
||||
var result = await _repository.AggregatedPatientLastObservationsByField(PatientId);
|
||||
|
||||
Assert.That(result, Is.Not.Null);
|
||||
Assert.That(result, Is.Not.Empty); // Ensure it returns all observations for the patient
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,93 @@
|
||||
using adas_core.Domain.Enums;
|
||||
using adas_core.Domain.Models;
|
||||
using adas_core.Domain.Models.AppSettings;
|
||||
using adas_core.Infrastructure.Repositories;
|
||||
using Microsoft.Extensions.Options;
|
||||
using MongoDB.Bson;
|
||||
using MongoDB.Driver;
|
||||
using Options = Microsoft.Extensions.Options.Options;
|
||||
|
||||
namespace adas_core.Test.Repositories;
|
||||
|
||||
[TestFixture]
|
||||
[Category("Integration")]
|
||||
public class AppointmentArchiveRepositoryTest
|
||||
{
|
||||
[OneTimeSetUp]
|
||||
public async Task Init()
|
||||
{
|
||||
_optionsApiSettings = Options.Create(_apiSettings);
|
||||
|
||||
var patient = new Person
|
||||
{
|
||||
FirstName = "firstName",
|
||||
SecondName = "secondName",
|
||||
BirthDate = Now.AddYears(-10),
|
||||
Gender = PatientEnum.Gender.Female,
|
||||
Ids = new Dictionary<string, string> { { "MR", "123456" }, { "VN", "654321" } }
|
||||
};
|
||||
|
||||
PatientAppointment testPatientAppointment = new()
|
||||
{
|
||||
PatientId = PatientId,
|
||||
Patient = patient,
|
||||
CreateTime = Now.AddHours(-2),
|
||||
UpdateTime = Now.AddHours(-2)
|
||||
};
|
||||
|
||||
PatientAppointment testPatientAppointment2 = new()
|
||||
{
|
||||
PatientId = PatientId,
|
||||
Patient = patient,
|
||||
CreateTime = Now.AddHours(-3),
|
||||
UpdateTime = Now.AddHours(-3),
|
||||
VisitNumber = "654321",
|
||||
AppointmentReason = "appointmentReason",
|
||||
ResourceGroups =
|
||||
[
|
||||
new PatientAppointmentResourceGroup
|
||||
{
|
||||
Locations = [new PatientLocation("UCI5C", "Box4")]
|
||||
}
|
||||
]
|
||||
};
|
||||
|
||||
await IntegrationDb.Database.DropCollectionAsync("patients_appointments");
|
||||
await IntegrationDb.Database.CreateCollectionAsync("patients_appointments");
|
||||
|
||||
_repository = new AppointmentArchiveRepository(_optionsApiSettings, IntegrationDb.Database);
|
||||
|
||||
await _repository.InsertOneAsync(testPatientAppointment);
|
||||
await _repository.InsertOneAsync(testPatientAppointment2);
|
||||
}
|
||||
|
||||
private AppointmentArchiveRepository _repository;
|
||||
|
||||
private readonly ApiSettings _apiSettings = new()
|
||||
{
|
||||
ArchivePatientsAppointments = "archive_patients_appointments"
|
||||
};
|
||||
|
||||
private IOptions<ApiSettings> _optionsApiSettings;
|
||||
//Mock<ILogger<ObservationRepository>> logger;
|
||||
|
||||
private static readonly DateTime Now = DateTime.Now;
|
||||
private static readonly ObjectId PatientId = ObjectId.GenerateNewId();
|
||||
|
||||
|
||||
[Test]
|
||||
public async Task DeleteBeforeDate()
|
||||
{
|
||||
var result = await _repository.Collection.FindAsync(p => p.PatientId == PatientId);
|
||||
|
||||
Assert.That(result, Is.Not.Null);
|
||||
Assert.That(result.ToList(), Has.Count.GreaterThan(1));
|
||||
|
||||
await _repository.DeleteBeforeDate(Now.AddHours(-2));
|
||||
|
||||
var resultDelete = await _repository.Collection.FindAsync(p => p.PatientId == PatientId);
|
||||
|
||||
Assert.That(resultDelete, Is.Not.Null);
|
||||
Assert.That(resultDelete.ToList(), Has.Count.EqualTo(1));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,148 @@
|
||||
using adas_core.Domain.Enums;
|
||||
using adas_core.Domain.Models;
|
||||
using adas_core.Domain.Models.AppSettings;
|
||||
using adas_core.Infrastructure.Repositories;
|
||||
using Microsoft.Extensions.Options;
|
||||
using MongoDB.Bson;
|
||||
using Options = Microsoft.Extensions.Options.Options;
|
||||
|
||||
namespace adas_core.Test.Repositories;
|
||||
|
||||
[TestFixture]
|
||||
[Category("Integration")]
|
||||
public class AppointmentRepositoryTest
|
||||
{
|
||||
[OneTimeSetUp]
|
||||
public async Task Init()
|
||||
{
|
||||
_optionsApiSettings = Options.Create(_apiSettings);
|
||||
|
||||
var patient = new Person
|
||||
{
|
||||
FirstName = "firstName",
|
||||
SecondName = "secondName",
|
||||
BirthDate = Now.AddYears(-10),
|
||||
Gender = PatientEnum.Gender.Female,
|
||||
Ids = new Dictionary<string, string> { { "MR", "123456" }, { "VN", "654321" } }
|
||||
};
|
||||
|
||||
var testPatientAppointment = new PatientAppointment
|
||||
{
|
||||
PatientId = PatientId,
|
||||
Patient = patient,
|
||||
CreateTime = Now.AddHours(-2),
|
||||
UpdateTime = Now.AddHours(-2)
|
||||
};
|
||||
|
||||
var testPatientAppointment2 = new PatientAppointment
|
||||
{
|
||||
PatientId = PatientId,
|
||||
Patient = patient,
|
||||
CreateTime = Now.AddHours(-3),
|
||||
UpdateTime = Now.AddHours(-3),
|
||||
VisitNumber = "654321",
|
||||
AppointmentReason = "appointmentReason",
|
||||
ResourceGroups =
|
||||
[
|
||||
new PatientAppointmentResourceGroup
|
||||
{
|
||||
Locations = [new PatientLocation("UCI5C", "Box4")]
|
||||
}
|
||||
]
|
||||
};
|
||||
|
||||
await IntegrationDb.Database.DropCollectionAsync("patients_appointments");
|
||||
await IntegrationDb.Database.CreateCollectionAsync("patients_appointments");
|
||||
|
||||
_repository = new AppointmentRepository(_optionsApiSettings, IntegrationDb.Database);
|
||||
|
||||
await _repository.InsertOneAsync(testPatientAppointment);
|
||||
await _repository.InsertOneAsync(testPatientAppointment2);
|
||||
}
|
||||
|
||||
private AppointmentRepository _repository;
|
||||
|
||||
private readonly ApiSettings _apiSettings = new()
|
||||
{
|
||||
PatientsAppointments = "patients_appointments"
|
||||
};
|
||||
|
||||
private IOptions<ApiSettings> _optionsApiSettings;
|
||||
|
||||
private static readonly DateTime Now = DateTime.Now;
|
||||
private static readonly ObjectId PatientId = ObjectId.GenerateNewId();
|
||||
|
||||
[Test]
|
||||
public async Task GetByPatient_Find_Patient_Return_List()
|
||||
{
|
||||
var result = await _repository.GetByPatient(PatientId);
|
||||
|
||||
Assert.That(result, Is.Not.Null);
|
||||
Assert.That(result, Is.Not.Empty);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task GetByPatient_not_Find_Patient_Return_Empty_List()
|
||||
{
|
||||
var result = await _repository.GetByPatient(ObjectId.GenerateNewId());
|
||||
|
||||
Assert.That(result, Is.Not.Null);
|
||||
Assert.That(result, Is.Empty);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task FindByPatientAndVisitNumber_Not_Find_Patient_Return_Null()
|
||||
{
|
||||
var result = await _repository.FindByPatientAndVisitNumber(PatientId, "visitNumber");
|
||||
|
||||
Assert.That(result, Is.Null);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task FindByPatientAndVisitNumber_Find_Patient_Return_Appointment()
|
||||
{
|
||||
var result = await _repository.FindByPatientAndVisitNumber(PatientId, "654321");
|
||||
|
||||
Assert.That(result, Is.Not.Null);
|
||||
Assert.That(result!.VisitNumber, Is.EqualTo("654321"));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task FindByPatientAndReason_Not_Find_Patient_Return_Null()
|
||||
{
|
||||
var result = await _repository.FindByPatientAndReason(PatientId, "NotappointmentReason");
|
||||
|
||||
Assert.That(result, Is.Null);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task FindByPatientAndReason_Find_Patient_Return_Appointment()
|
||||
{
|
||||
var result = await _repository.FindByPatientAndReason(PatientId, "appointmentReason");
|
||||
|
||||
Assert.That(result, Is.Not.Null);
|
||||
Assert.That(result!.AppointmentReason, Is.EqualTo("appointmentReason"));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task FindByLocation_Not_Find_Patient_Return_Null()
|
||||
{
|
||||
var location = new PatientLocation("UCI5C", "Box3");
|
||||
|
||||
var result = await _repository.FindByLocation(location);
|
||||
|
||||
Assert.That(result, Is.Not.Null);
|
||||
Assert.That(result.Count(), Is.EqualTo(0));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task FindByLocation_Find_Patient_Return_Appointment()
|
||||
{
|
||||
var location = new PatientLocation("UCI5C", "Box4");
|
||||
|
||||
var result = await _repository.FindByLocation(location);
|
||||
|
||||
Assert.That(result, Is.Not.Null);
|
||||
Assert.That(result.Count(), Is.EqualTo(1));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,190 @@
|
||||
using adas_core.Application.Services.Interfaces;
|
||||
using adas_core.Domain.Models.AppSettings;
|
||||
using adas_core.Domain.Models.MongoModels;
|
||||
using adas_core.Infrastructure.Repositories;
|
||||
using Microsoft.Extensions.Options;
|
||||
using MongoDB.Bson;
|
||||
using Moq;
|
||||
using Options = Microsoft.Extensions.Options.Options;
|
||||
|
||||
namespace adas_core.Test.Repositories;
|
||||
|
||||
[TestFixture]
|
||||
[Category("Integration")]
|
||||
public class ConfigObservationRepositoryTest
|
||||
{
|
||||
[OneTimeSetUp]
|
||||
public async Task Init()
|
||||
{
|
||||
_id = ObjectId.GenerateNewId();
|
||||
_optionsApiSettings = Options.Create(_apiSettings);
|
||||
_masterListMock = new Mock<IMasterListServiceFactory>();
|
||||
|
||||
var testConfigObservation = new List<ConfigObservation>
|
||||
{
|
||||
new()
|
||||
{
|
||||
Id = _id,
|
||||
Name = "Hemoglobina",
|
||||
Code = "12345",
|
||||
CodingSystem = "SNM"
|
||||
},
|
||||
|
||||
new()
|
||||
{
|
||||
Id = ObjectId.GenerateNewId(),
|
||||
Name = "Glucemia",
|
||||
Code = "54321",
|
||||
CodingSystem = "SNM",
|
||||
OriginalName = "GLU"
|
||||
},
|
||||
|
||||
new()
|
||||
{
|
||||
Id = ObjectId.GenerateNewId(),
|
||||
Name = "Sodio",
|
||||
OriginalName = "Sodio"
|
||||
},
|
||||
|
||||
new()
|
||||
{
|
||||
Id = ObjectId.GenerateNewId(),
|
||||
Name = "ph",
|
||||
Code = "555",
|
||||
CodingSystem = "MG4",
|
||||
ParentCode = "3333",
|
||||
ParentCodingSystem = "SNM"
|
||||
},
|
||||
|
||||
|
||||
new()
|
||||
{
|
||||
Id = ObjectId.GenerateNewId(),
|
||||
Name = "SOFA",
|
||||
Code = "278061009",
|
||||
OriginalName = "SOFA",
|
||||
CodingSystem = "SNM",
|
||||
MinAlert = 10,
|
||||
MinWarn = 14
|
||||
},
|
||||
|
||||
|
||||
new()
|
||||
{
|
||||
Id = ObjectId.GenerateNewId(),
|
||||
Name = "FC",
|
||||
Code = "147842",
|
||||
//originalName = "MDC_ECG_CARD_BEAT_RATE",
|
||||
CodingSystem = "MDC",
|
||||
ParentCode = "69965",
|
||||
ParentName = "MDC_DEV_MON_PHYSIO_MULTI_PARAM_MDS",
|
||||
MinAlert = 60,
|
||||
MaxAlert = 100,
|
||||
ForceAlert = true
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
await IntegrationDb.Database.DropCollectionAsync("config_observations");
|
||||
await IntegrationDb.Database.CreateCollectionAsync("config_observations");
|
||||
|
||||
_repository =
|
||||
new ConfigObservationRepository(_optionsApiSettings, IntegrationDb.Database, _masterListMock.Object);
|
||||
|
||||
await _repository.InsertManyAsync(testConfigObservation);
|
||||
}
|
||||
|
||||
private ConfigObservationRepository _repository;
|
||||
private IMock<IMasterListServiceFactory> _masterListMock;
|
||||
private ObjectId _id;
|
||||
|
||||
private readonly ApiSettings _apiSettings = new()
|
||||
{
|
||||
ConfigObservations = "config_observations"
|
||||
};
|
||||
|
||||
private IOptions<ApiSettings> _optionsApiSettings;
|
||||
|
||||
|
||||
[Test]
|
||||
public async Task FindById_Find_Id_Return_Configs()
|
||||
{
|
||||
var result = await _repository.FindById(_id);
|
||||
|
||||
Assert.That(result, Is.Not.Null);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task FindById_Not_Find_Id_Return_Empty()
|
||||
{
|
||||
var result = await _repository.FindById(ObjectId.GenerateNewId());
|
||||
|
||||
Assert.That(result, Is.Null);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Update_Find_Id_Return_Update_Configs()
|
||||
{
|
||||
var updateConfigObservation = new ConfigObservation
|
||||
{
|
||||
Id = _id,
|
||||
Name = "Hemoglobina 2",
|
||||
Code = "12345",
|
||||
CodingSystem = "SNM"
|
||||
};
|
||||
|
||||
var result = await _repository.FindById(_id);
|
||||
|
||||
Assert.That(result, Is.Not.Null);
|
||||
Assert.That(result.Name, Is.EqualTo("Hemoglobina"));
|
||||
|
||||
var resultUpdate = await _repository.Update(updateConfigObservation);
|
||||
|
||||
Assert.That(resultUpdate, Is.Not.Null);
|
||||
Assert.That(resultUpdate.Name, Is.EqualTo("Hemoglobina 2"));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Update_Not_Find_Id_Create_It()
|
||||
{
|
||||
var id2 = ObjectId.GenerateNewId();
|
||||
var updateConfigObservation = new ConfigObservation
|
||||
{
|
||||
Id = id2,
|
||||
Name = "Hemoglobina",
|
||||
Code = "12345",
|
||||
CodingSystem = "SNM"
|
||||
};
|
||||
|
||||
await _repository.Update(updateConfigObservation);
|
||||
var resultfind = await _repository.FindById(id2);
|
||||
Assert.That(resultfind, Is.Not.Null);
|
||||
}
|
||||
|
||||
|
||||
[Test]
|
||||
public async Task Delete_With_Base_DeleteAsync()
|
||||
{
|
||||
var iid = ObjectId.GenerateNewId();
|
||||
var deleteConfigObservation = new ConfigObservation
|
||||
{
|
||||
Id = iid,
|
||||
Name = "Hemoglobina3",
|
||||
Code = "123455",
|
||||
CodingSystem = "SNMM"
|
||||
};
|
||||
|
||||
var result = await _repository.FindById(iid);
|
||||
Assert.That(result, Is.Null);
|
||||
|
||||
await _repository.InsertOneAsync(deleteConfigObservation);
|
||||
|
||||
var resultInsert = await _repository.FindById(iid);
|
||||
Assert.That(resultInsert, Is.Not.Null);
|
||||
|
||||
await _repository.Delete(iid);
|
||||
|
||||
var resultDelete = await _repository.FindById(iid);
|
||||
Assert.That(resultDelete, Is.Null);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
using System.Diagnostics;
|
||||
using adas_core.Domain.Models.AppSettings;
|
||||
using adas_core.Domain.Models.MongoModels;
|
||||
using adas_core.Infrastructure.Repositories;
|
||||
using Microsoft.Extensions.Options;
|
||||
using Options = Microsoft.Extensions.Options.Options;
|
||||
|
||||
namespace adas_core.Test.Repositories;
|
||||
|
||||
[TestFixture]
|
||||
[Category("Integration")]
|
||||
public class ConfigPumpsRepositoryTest
|
||||
{
|
||||
[OneTimeSetUp]
|
||||
public async Task Init()
|
||||
{
|
||||
_optionsApiSettings = Options.Create(_apiSettings);
|
||||
|
||||
var configPumpItem1 = new ConfigPumpItem();
|
||||
var configPumpItem2 = new ConfigPumpItem();
|
||||
|
||||
var configPumps = new ConfigPumps
|
||||
{
|
||||
Id = "PV1",
|
||||
Items = [configPumpItem1, configPumpItem2]
|
||||
};
|
||||
|
||||
await IntegrationDb.Database.DropCollectionAsync("config_pumps");
|
||||
await IntegrationDb.Database.CreateCollectionAsync("config_pumps");
|
||||
|
||||
_repository = new ConfigPumpsRepository(_optionsApiSettings, IntegrationDb.Database);
|
||||
|
||||
await _repository.InsertOneAsync(configPumps);
|
||||
}
|
||||
|
||||
private ConfigPumpsRepository _repository;
|
||||
|
||||
private readonly ApiSettings _apiSettings = new()
|
||||
{
|
||||
ConfigPumps = "config_pumps"
|
||||
};
|
||||
|
||||
private IOptions<ApiSettings> _optionsApiSettings;
|
||||
|
||||
|
||||
[Test]
|
||||
public async Task FindById_Find_id_Return_List()
|
||||
{
|
||||
var result = await _repository.FindById("PV1");
|
||||
|
||||
Assert.That(result, Is.Not.Null);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task FindAsync_not_Find_id_Return_Empty_List()
|
||||
{
|
||||
var result = await _repository.FindById("PV2");
|
||||
|
||||
Assert.That(result, Is.Null);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Insert_PumpConfig_returns2()
|
||||
{
|
||||
var configPumpItem1 = new ConfigPumpItem();
|
||||
var configPumpItem2 = new ConfigPumpItem();
|
||||
|
||||
var newPumpConfig = new ConfigPumps
|
||||
{
|
||||
Id = "PV2",
|
||||
Items = [configPumpItem1, configPumpItem2]
|
||||
};
|
||||
|
||||
await _repository.InsertOneAsync(newPumpConfig);
|
||||
|
||||
var result = await _repository.GetAllConfigs();
|
||||
|
||||
Assert.That(result, Has.Count.EqualTo(2));
|
||||
|
||||
var pumpCfgInserted = await _repository.FindById(newPumpConfig.Id);
|
||||
|
||||
Assert.That(pumpCfgInserted, Is.Not.EqualTo(null));
|
||||
|
||||
Debug.Assert(pumpCfgInserted != null, nameof(pumpCfgInserted) + " != null");
|
||||
|
||||
await _repository.DeleteConfig(pumpCfgInserted);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
using adas_core.Domain.Models.AppSettings;
|
||||
using adas_core.Domain.Models.MongoModels;
|
||||
using adas_core.Infrastructure.Repositories;
|
||||
using Microsoft.Extensions.Options;
|
||||
using Options = Microsoft.Extensions.Options.Options;
|
||||
|
||||
namespace adas_core.Test.Repositories;
|
||||
|
||||
[TestFixture]
|
||||
[Category("Integration")]
|
||||
public class ConfigUnitsRepositoryTest
|
||||
{
|
||||
[OneTimeSetUp]
|
||||
public async Task Init()
|
||||
{
|
||||
_optionsApiSettings = Options.Create(_apiSettings);
|
||||
|
||||
var configUnitItem1 = new ConfigUnitItem();
|
||||
var configUnitItem2 = new ConfigUnitItem();
|
||||
|
||||
var configUnits = new ConfigUnits
|
||||
{
|
||||
Id = "PV1",
|
||||
Items = [configUnitItem1, configUnitItem2]
|
||||
};
|
||||
|
||||
await IntegrationDb.Database.DropCollectionAsync("config_units");
|
||||
await IntegrationDb.Database.CreateCollectionAsync("config_units");
|
||||
|
||||
_repository = new ConfigUnitsRepository(_optionsApiSettings, IntegrationDb.Database);
|
||||
|
||||
await _repository.InsertOneAsync(configUnits);
|
||||
}
|
||||
|
||||
private ConfigUnitsRepository _repository;
|
||||
|
||||
private readonly ApiSettings _apiSettings = new()
|
||||
{
|
||||
ConfigUnits = "config_units"
|
||||
};
|
||||
|
||||
private IOptions<ApiSettings> _optionsApiSettings;
|
||||
|
||||
|
||||
[Test]
|
||||
public async Task FindById_Find_id_Return_List()
|
||||
{
|
||||
var result = await _repository.FindById("PV1");
|
||||
|
||||
Assert.That(result, Is.Not.Null);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task FindAsync_not_Find_id_Return_Empty_List()
|
||||
{
|
||||
var result = await _repository.FindById("PV2");
|
||||
|
||||
Assert.That(result, Is.Null);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,86 @@
|
||||
using adas_core.Domain.Models;
|
||||
using adas_core.Domain.Models.AppSettings;
|
||||
using adas_core.Infrastructure.Repositories;
|
||||
using Microsoft.Extensions.Options;
|
||||
using MongoDB.Bson;
|
||||
using MongoDB.Driver;
|
||||
using Options = Microsoft.Extensions.Options.Options;
|
||||
|
||||
namespace adas_core.Test.Repositories;
|
||||
|
||||
[TestFixture]
|
||||
[Category("Integration")]
|
||||
public class DiagnosisArchiveRepositoryTest
|
||||
{
|
||||
[OneTimeSetUp]
|
||||
public async Task Init()
|
||||
{
|
||||
_optionsApiSettings = Options.Create(_apiSettings);
|
||||
|
||||
var patientDiagnosis1 = new PatientDiagnosis
|
||||
{
|
||||
Id = Id,
|
||||
PatientId = PatientId,
|
||||
Time = Now.AddDays(-1),
|
||||
Code = "code1",
|
||||
DiagnosisSystem = "diagnosisSystem"
|
||||
};
|
||||
|
||||
var patientDiagnosis2 = new PatientDiagnosis
|
||||
{
|
||||
Id = ObjectId.GenerateNewId(),
|
||||
PatientId = PatientId,
|
||||
Time = Now.AddDays(-2),
|
||||
Code = "code2",
|
||||
DiagnosisSystem = "diagnosisSystem"
|
||||
};
|
||||
|
||||
var patientDiagnosis3 = new PatientDiagnosis
|
||||
{
|
||||
Id = ObjectId.GenerateNewId(),
|
||||
PatientId = PatientId,
|
||||
Time = Now.AddDays(-3),
|
||||
Code = "code3",
|
||||
DiagnosisSystem = "diagnosisSystem"
|
||||
};
|
||||
|
||||
await IntegrationDb.Database.DropCollectionAsync("archive_patients_diagnosis");
|
||||
await IntegrationDb.Database.CreateCollectionAsync("archive_patients_diagnosis");
|
||||
|
||||
_repository = new DiagnosisArchiveRepository(_optionsApiSettings, IntegrationDb.Database);
|
||||
|
||||
await _repository.InsertOneAsync(patientDiagnosis1);
|
||||
await _repository.InsertOneAsync(patientDiagnosis2);
|
||||
await _repository.InsertOneAsync(patientDiagnosis3);
|
||||
}
|
||||
|
||||
private DiagnosisArchiveRepository _repository;
|
||||
|
||||
private readonly ApiSettings _apiSettings = new()
|
||||
{
|
||||
ArchivePatientsDiagnosis = "archive_patients_diagnosis"
|
||||
};
|
||||
|
||||
private IOptions<ApiSettings> _optionsApiSettings;
|
||||
|
||||
private static readonly DateTime Now = DateTime.Now;
|
||||
private static readonly ObjectId PatientId = ObjectId.GenerateNewId();
|
||||
private static readonly ObjectId Id = ObjectId.GenerateNewId();
|
||||
|
||||
|
||||
[Test]
|
||||
public async Task DeleteBeforeDate()
|
||||
{
|
||||
var result = await _repository.Collection.FindAsync(p => p.PatientId == PatientId);
|
||||
|
||||
Assert.That(result, Is.Not.Null);
|
||||
Assert.That(result.ToList(), Has.Count.GreaterThan(1));
|
||||
|
||||
await _repository.DeleteBeforeDate(Now.AddDays(-1));
|
||||
|
||||
var resultDelete = await _repository.Collection.FindAsync(p => p.PatientId == PatientId);
|
||||
|
||||
Assert.That(resultDelete, Is.Not.Null);
|
||||
Assert.That(resultDelete.ToList(), Has.Count.EqualTo(1));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,197 @@
|
||||
using adas_core.Domain.Models;
|
||||
using adas_core.Domain.Models.AppSettings;
|
||||
using adas_core.Infrastructure.Repositories;
|
||||
using Microsoft.Extensions.Options;
|
||||
using MongoDB.Bson;
|
||||
using MongoDB.Driver;
|
||||
using Options = Microsoft.Extensions.Options.Options;
|
||||
|
||||
namespace adas_core.Test.Repositories;
|
||||
|
||||
[TestFixture]
|
||||
[Category("Integration")]
|
||||
public class DiagnosisRepositoryTest
|
||||
{
|
||||
[OneTimeSetUp]
|
||||
public async Task Init()
|
||||
{
|
||||
_optionsApiSettings = Options.Create(_apiSettings);
|
||||
|
||||
var patientDiagnosis1 = new PatientDiagnosis
|
||||
{
|
||||
Id = Id,
|
||||
PatientId = PatientId,
|
||||
Time = Now,
|
||||
Code = "code1",
|
||||
DiagnosisSystem = "diagnosisSystem"
|
||||
};
|
||||
|
||||
var patientDiagnosis2 = new PatientDiagnosis
|
||||
{
|
||||
Id = ObjectId.GenerateNewId(),
|
||||
PatientId = ObjectId.GenerateNewId(),
|
||||
Time = Now,
|
||||
Code = "code2",
|
||||
DiagnosisSystem = "diagnosisSystem"
|
||||
};
|
||||
|
||||
var patientDiagnosis3 = new PatientDiagnosis
|
||||
{
|
||||
Id = ObjectId.GenerateNewId(),
|
||||
PatientId = ObjectId.GenerateNewId(),
|
||||
Time = Now,
|
||||
Code = "code3",
|
||||
DiagnosisSystem = "diagnosisSystem"
|
||||
};
|
||||
|
||||
await IntegrationDb.Database.DropCollectionAsync("patients_diagnosis");
|
||||
await IntegrationDb.Database.CreateCollectionAsync("patients_diagnosis");
|
||||
|
||||
_repository = new DiagnosisRepository(_optionsApiSettings, IntegrationDb.Database);
|
||||
|
||||
await _repository.InsertOneAsync(patientDiagnosis1);
|
||||
await _repository.InsertOneAsync(patientDiagnosis2);
|
||||
await _repository.InsertOneAsync(patientDiagnosis3);
|
||||
}
|
||||
|
||||
private DiagnosisRepository _repository;
|
||||
|
||||
private readonly ApiSettings _apiSettings = new()
|
||||
{
|
||||
PatientsDiagnosis = "patients_diagnosis"
|
||||
};
|
||||
|
||||
private IOptions<ApiSettings> _optionsApiSettings;
|
||||
|
||||
private static readonly DateTime Now = DateTime.Now;
|
||||
private static readonly ObjectId PatientId = ObjectId.GenerateNewId();
|
||||
private static readonly ObjectId Id = ObjectId.GenerateNewId();
|
||||
|
||||
|
||||
[Test]
|
||||
public async Task GetByPatient_Find_Patient_Return_List()
|
||||
{
|
||||
var result = await _repository.GetByPatient(PatientId);
|
||||
|
||||
Assert.That(result, Is.Not.Null);
|
||||
Assert.That(result, Is.Not.Empty);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task GetByPatient_not_Find_Patient_Return_Empty_List()
|
||||
{
|
||||
var result = await _repository.GetByPatient(ObjectId.GenerateNewId());
|
||||
|
||||
Assert.That(result, Is.Not.Null);
|
||||
Assert.That(result, Is.Empty);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task FindByPatientIdAndCode_Find_Patient_Return_Diagnois()
|
||||
{
|
||||
var result = await _repository.FindByPatientIdAndCode(PatientId, "code1", "diagnosisSystem");
|
||||
|
||||
Assert.That(result, Is.Not.Null);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task FindByPatientIdAndCode_not_Find_Patient_Return_null()
|
||||
{
|
||||
var result = await _repository.FindByPatientIdAndCode(ObjectId.GenerateNewId(), "code4", "diagnosisSystem");
|
||||
|
||||
Assert.That(result, Is.Null);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task FindByPatientIdAsync_Find_Patient_Return_List()
|
||||
{
|
||||
var result = await _repository.FindByPatientIdAsync(PatientId);
|
||||
|
||||
Assert.That(result, Is.Not.Null);
|
||||
Assert.That(result.ToList(), Is.Not.Empty);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task FindByPatientIdAsync_not_Find_Patient_Return_Empty_List()
|
||||
{
|
||||
var result = await _repository.FindByPatientIdAsync(ObjectId.GenerateNewId());
|
||||
|
||||
Assert.That(result, Is.Not.Null);
|
||||
Assert.That(result.ToList(), Is.Empty);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task DeleteAsync_Find_id()
|
||||
{
|
||||
var patientDiagnosis = new PatientDiagnosis
|
||||
{
|
||||
Id = ObjectId.GenerateNewId(),
|
||||
PatientId = ObjectId.GenerateNewId(),
|
||||
Time = Now,
|
||||
Code = "code1",
|
||||
DiagnosisSystem = "diagnosisSystem"
|
||||
};
|
||||
|
||||
await _repository.InsertOneAsync(patientDiagnosis);
|
||||
|
||||
var result = await _repository.FindByPatientIdAsync(patientDiagnosis.PatientId);
|
||||
Assert.That(result.ToList(), Has.Count.EqualTo(1));
|
||||
|
||||
await _repository.DeleteAsync(patientDiagnosis.Id);
|
||||
|
||||
var resultDelete = await _repository.FindByPatientIdAsync(patientDiagnosis.PatientId);
|
||||
Assert.That(resultDelete.ToList(), Is.Empty);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task DeleteByPatientId_Find_patientId()
|
||||
{
|
||||
var patientDiagnosis = new PatientDiagnosis
|
||||
{
|
||||
Id = ObjectId.GenerateNewId(),
|
||||
PatientId = ObjectId.GenerateNewId(),
|
||||
Time = Now,
|
||||
Code = "code1",
|
||||
DiagnosisSystem = "diagnosisSystem"
|
||||
};
|
||||
|
||||
await _repository.InsertOneAsync(patientDiagnosis);
|
||||
|
||||
var result = await _repository.FindByPatientIdAsync(patientDiagnosis.PatientId);
|
||||
Assert.That(result.ToList(), Has.Count.EqualTo(1));
|
||||
|
||||
await _repository.DeleteByPatientId(patientDiagnosis.PatientId);
|
||||
|
||||
var resultDelete = await _repository.FindByPatientIdAsync(patientDiagnosis.PatientId);
|
||||
Assert.That(resultDelete.ToList(), Is.Empty);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task UpdateManyObjectId_With_base_UpdateManyObjectIdAsync()
|
||||
{
|
||||
var patientDiagnosis = new PatientDiagnosis
|
||||
{
|
||||
Id = ObjectId.GenerateNewId(),
|
||||
PatientId = ObjectId.GenerateNewId(),
|
||||
Time = Now,
|
||||
Code = "code1",
|
||||
DiagnosisSystem = "diagnosisSystem"
|
||||
};
|
||||
|
||||
await _repository.InsertOneAsync(patientDiagnosis);
|
||||
|
||||
var resultInsert = await _repository.FindByPatientIdAsync(patientDiagnosis.PatientId);
|
||||
|
||||
var resultList = resultInsert.ToList();
|
||||
|
||||
Assert.That(resultList, Has.Count.EqualTo(1));
|
||||
|
||||
var newPatientId = ObjectId.GenerateNewId();
|
||||
|
||||
await _repository.UpdateManyObjectId("patientId", newPatientId, patientDiagnosis.PatientId);
|
||||
|
||||
var resultUpdate = await _repository.FindByPatientIdAsync(newPatientId);
|
||||
Assert.That(resultUpdate, Is.Not.Null);
|
||||
Assert.That(resultUpdate.ToList().Count(f => f.PatientId == newPatientId), Is.EqualTo(1));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,373 @@
|
||||
using adas_core.Domain.Models;
|
||||
using adas_core.Domain.Models.AppSettings;
|
||||
using adas_core.Domain.Models.MongoModels;
|
||||
using adas_core.Infrastructure.Repositories;
|
||||
using adas_core.Test.Utilities;
|
||||
using Microsoft.Extensions.Options;
|
||||
using MongoDB.Bson;
|
||||
using Options = Microsoft.Extensions.Options.Options;
|
||||
|
||||
namespace adas_core.Test.Repositories;
|
||||
|
||||
[TestFixture]
|
||||
[Category("Integration")]
|
||||
public class DischargeRepositoryTest
|
||||
{
|
||||
[OneTimeSetUp]
|
||||
public async Task Init()
|
||||
{
|
||||
// Initialize options and repository
|
||||
_optionsApiSettings = Options.Create(_apiSettings);
|
||||
|
||||
await IntegrationDb.Database.DropCollectionAsync("discharge");
|
||||
await IntegrationDb.Database.CreateCollectionAsync("discharge");
|
||||
|
||||
_dischargeRepository = new DischargeRepository(_optionsApiSettings, IntegrationDb.Database);
|
||||
}
|
||||
|
||||
private DischargeRepository _dischargeRepository;
|
||||
private readonly ApiSettings _apiSettings = new();
|
||||
private IOptions<ApiSettings> _optionsApiSettings;
|
||||
|
||||
[Test]
|
||||
public async Task InsertOneAsync_ShouldInsertDischarge()
|
||||
{
|
||||
// Arrange
|
||||
var discharge = TestUtilities.CreateValidDischarge();
|
||||
|
||||
// Act
|
||||
await _dischargeRepository.InsertOneAsync(discharge);
|
||||
|
||||
var result = await _dischargeRepository.FindById(discharge.Id);
|
||||
Assert.That(result, Is.Not.Null);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Delete_ShouldDeleteDischarge()
|
||||
{
|
||||
// Arrange
|
||||
var discharge = TestUtilities.CreateValidDischarge();
|
||||
await _dischargeRepository.InsertOneAsync(discharge);
|
||||
|
||||
// Act
|
||||
await _dischargeRepository.Delete(discharge.Id);
|
||||
|
||||
var result = await _dischargeRepository.FindById(discharge.Id);
|
||||
Assert.That(result, Is.Null);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Update_DischargeSuccessfullyUpdated()
|
||||
{
|
||||
// Arrange
|
||||
var pocId = new ObjectId();
|
||||
var discharge = TestUtilities.CreateValidDischarge();
|
||||
discharge.PointOfCareId = pocId;
|
||||
await _dischargeRepository.InsertOneAsync(discharge);
|
||||
|
||||
// Act
|
||||
discharge.Service = "UpdatedService";
|
||||
await _dischargeRepository.Update(discharge);
|
||||
|
||||
// Retrieve the discharge from the database
|
||||
var updatedDischarge = await _dischargeRepository.GetDischargeByPointOfCareId(pocId);
|
||||
|
||||
// Assert
|
||||
Assert.That(updatedDischarge, Is.Not.Null);
|
||||
Assert.That(updatedDischarge?.Service, Is.EqualTo("UpdatedService"));
|
||||
}
|
||||
|
||||
// [Test]
|
||||
// public async Task UpdateUnit_UnitNameSuccessfullyUpdated()
|
||||
// {
|
||||
// // Arrange
|
||||
// var discharge = TestUtilities.CreateValidDischarge();
|
||||
// await _dischargeRepository.InsertOneAsync(discharge);
|
||||
//
|
||||
// // Act
|
||||
// var newUnitName = "NewUnitName";
|
||||
// await _dischargeRepository.UpdateUnit(discharge.Id, newUnitName);
|
||||
//
|
||||
// // Retrieve the discharge from the database
|
||||
// var updatedDischarge = await _dischargeRepository.FindById(discharge.Id);
|
||||
//
|
||||
// // Assert
|
||||
// Assert.That(updatedDischarge, Is.Not.Null);
|
||||
// Assert.That(updatedDischarge?.PatientLocation, Is.Not.Null);
|
||||
// Assert.That(updatedDischarge?.PatientLocation?.UnitName, Is.EqualTo(newUnitName));
|
||||
// }
|
||||
|
||||
// [Test]
|
||||
// public async Task UpdatePatient_PatientSuccessfullyUpdated()
|
||||
// {
|
||||
// // Arrange
|
||||
// var discharge = TestUtilities.CreateValidDischarge();
|
||||
// await _dischargeRepository.InsertOneAsync(discharge);
|
||||
//
|
||||
// // Create a new patient object with updated information
|
||||
// var updatedPatient = TestUtilities.CreateValidPatient();
|
||||
//
|
||||
// // Act
|
||||
// await _dischargeRepository.UpdatePatient(discharge.Id, updatedPatient);
|
||||
//
|
||||
// // Retrieve the discharge from the database
|
||||
// var updatedDischarge = await _dischargeRepository.FindById(discharge.Id);
|
||||
//
|
||||
// // Assert
|
||||
// Assert.That(updatedDischarge, Is.Not.Null);
|
||||
// Assert.That(updatedDischarge?.Patient, Is.Not.Null);
|
||||
// Assert.That(updatedDischarge?.Patient?.Id, Is.EqualTo(updatedPatient.Id));
|
||||
// }
|
||||
|
||||
[Test]
|
||||
public async Task FindAll_ReturnsAllDischarges()
|
||||
{
|
||||
// Arrange: Prepare test data
|
||||
var expectedDischarges = new List<Discharge>
|
||||
{
|
||||
new() { Id = ObjectId.GenerateNewId(), DischargeDate = DateTime.UtcNow },
|
||||
new() { Id = ObjectId.GenerateNewId(), DischargeDate = DateTime.UtcNow },
|
||||
new() { Id = ObjectId.GenerateNewId(), DischargeDate = DateTime.UtcNow }
|
||||
};
|
||||
|
||||
await _dischargeRepository.InsertManyAsync(expectedDischarges);
|
||||
|
||||
// Act: Call the method under test
|
||||
var actualDischarges = await _dischargeRepository.FindAll();
|
||||
|
||||
var discharges = actualDischarges.ToList();
|
||||
Assert.That(discharges, Is.Not.Null);
|
||||
Assert.That(discharges, Is.Not.Empty);
|
||||
Assert.That(discharges.Count(), Is.EqualTo(expectedDischarges.Count()));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task FindById_ExistingId_ReturnsDischarge()
|
||||
{
|
||||
var id = ObjectId.GenerateNewId();
|
||||
var expectedDischarge = new Discharge { Id = id, DischargeDate = DateTime.UtcNow };
|
||||
await _dischargeRepository.InsertOneAsync(expectedDischarge);
|
||||
|
||||
var actualDischarge = await _dischargeRepository.FindById(id);
|
||||
|
||||
Assert.That(actualDischarge, Is.Not.Null);
|
||||
Assert.That(actualDischarge?.Id, Is.EqualTo(expectedDischarge.Id));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task FindById_NonExistentId_ReturnsNull()
|
||||
{
|
||||
var id = ObjectId.GenerateNewId();
|
||||
|
||||
var actualDischarge = await _dischargeRepository.FindById(id);
|
||||
|
||||
Assert.That(actualDischarge, Is.Null);
|
||||
}
|
||||
|
||||
// [Test]
|
||||
// public async Task FindByUnit_ExistingUnit_ReturnsDischarges()
|
||||
// {
|
||||
// // Arrange: Prepare test data
|
||||
// var unitName = "TestUnit";
|
||||
// var expectedDischarges =
|
||||
// new Discharge
|
||||
// { Id = ObjectId.GenerateNewId(), PatientLocation = new PatientLocation { UnitName = unitName } };
|
||||
// await _dischargeRepository.InsertOneAsync(expectedDischarges);
|
||||
//
|
||||
// // Act: Call the method under test
|
||||
// var actualDischarges = await _dischargeRepository.FindByUnit(unitName);
|
||||
//
|
||||
// // Assert: Verify the result
|
||||
// Assert.That(actualDischarges, Is.Not.Null);
|
||||
// Assert.That(actualDischarges?.ToList().First().PatientLocation?.UnitName, Is.EquivalentTo(expectedDischarges.PatientLocation.UnitName));
|
||||
// }
|
||||
|
||||
[Test]
|
||||
public async Task FindByDestination_ExistingDestination_ReturnsDischarges()
|
||||
{
|
||||
// Arrange: Prepare test data
|
||||
var destination = "TestDestination";
|
||||
var expectedDischarges =
|
||||
new Discharge
|
||||
{ Id = ObjectId.GenerateNewId(), Destination = destination };
|
||||
await _dischargeRepository.InsertOneAsync(expectedDischarges);
|
||||
|
||||
// Act: Call the method under test
|
||||
var actualDischarges = await _dischargeRepository.FindByDestination(destination);
|
||||
|
||||
// Assert: Verify the result
|
||||
var discharges = actualDischarges?.ToList() ?? [];
|
||||
Assert.That(discharges, Is.Not.Null);
|
||||
Assert.That(discharges.First().Id, Is.EqualTo(expectedDischarges.Id));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task FindByDestination_NonExistentDestination_ReturnsEmptyList()
|
||||
{
|
||||
// Arrange: Use a non-existent destination
|
||||
var destination = "NonExistentDestination";
|
||||
|
||||
// Act: Call the method under test
|
||||
var actualDischarges = await _dischargeRepository.FindByDestination(destination);
|
||||
|
||||
// Assert: Verify that the result is an empty list
|
||||
Assert.That(actualDischarges, Is.Empty);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task FindByPoCId_ExistingPoCId_ReturnsDischarges()
|
||||
{
|
||||
// Arrange: Prepare test data
|
||||
var pocId = ObjectId.GenerateNewId();
|
||||
var expectedDischarges =
|
||||
new Discharge
|
||||
{ Id = ObjectId.GenerateNewId(), PointOfCareId = pocId };
|
||||
await _dischargeRepository.InsertOneAsync(expectedDischarges);
|
||||
|
||||
// Act: Call the method under test
|
||||
var actualDischarges = await _dischargeRepository.FindByPoCId(pocId);
|
||||
|
||||
// Assert: Verify the result
|
||||
var discharges = actualDischarges?.ToList() ?? [];
|
||||
Assert.That(discharges, Is.Not.Null);
|
||||
Assert.That(discharges.First().Id, Is.EqualTo(expectedDischarges.Id));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task FindByPoCId_NonExistentPoCId_ReturnsEmptyList()
|
||||
{
|
||||
// Arrange: Use a non-existent PoCId
|
||||
var pocId = ObjectId.GenerateNewId();
|
||||
|
||||
// Act: Call the method under test
|
||||
var actualDischarges = await _dischargeRepository.FindByPoCId(pocId);
|
||||
|
||||
// Assert: Verify that the result is an empty list
|
||||
Assert.That(actualDischarges, Is.Empty);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task FindByService_ExistingService_ReturnsDischarges()
|
||||
{
|
||||
// Arrange: Prepare test data
|
||||
var service = "TestService";
|
||||
var expectedDischarges =
|
||||
new Discharge
|
||||
{ Id = ObjectId.GenerateNewId(), Service = service };
|
||||
await _dischargeRepository.InsertOneAsync(expectedDischarges);
|
||||
|
||||
// Act: Call the method under test
|
||||
var actualDischarges = await _dischargeRepository.FindByService(service);
|
||||
|
||||
// Assert: Verify the result
|
||||
var discharges = actualDischarges?.ToList() ?? [];
|
||||
Assert.That(discharges, Is.Not.Null);
|
||||
Assert.That(discharges.First().Id, Is.EqualTo(expectedDischarges.Id));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task FindByService_NonExistentService_ReturnsEmptyList()
|
||||
{
|
||||
// Arrange: Use a non-existent service
|
||||
var service = "NonExistentService";
|
||||
|
||||
// Act: Call the method under test
|
||||
var actualDischarges = await _dischargeRepository.FindByService(service);
|
||||
|
||||
// Assert: Verify that the result is an empty list
|
||||
Assert.That(actualDischarges, Is.Empty);
|
||||
}
|
||||
|
||||
// [Test]
|
||||
// public async Task GetDischargeByLocation_ExistingLocation_ReturnsDischarge()
|
||||
// {
|
||||
// // Arrange: Prepare test data
|
||||
// var location = new PatientLocation("TestUnit", "TestBed", "TestRoom");
|
||||
// var expectedDischarges =
|
||||
// new Discharge
|
||||
// { Id = ObjectId.GenerateNewId(), PatientLocation = location};
|
||||
// await _dischargeRepository.InsertOneAsync(expectedDischarges);
|
||||
//
|
||||
// // Act: Call the method under test
|
||||
// var actualDischarge = await _dischargeRepository.GetDischargeByLocation(location);
|
||||
//
|
||||
// // Assert: Verify the result
|
||||
// Assert.That(actualDischarge, Is.Not.Null);
|
||||
// Assert.That(actualDischarge?.Id, Is.EqualTo(expectedDischarges.Id));
|
||||
// }
|
||||
|
||||
[Test]
|
||||
public async Task GetDischargeByLocation_NonExistentLocation_ReturnsNull()
|
||||
{
|
||||
// Arrange: Use a non-existent location
|
||||
var location = new PatientLocation("NonExistentUnit", "NonExistentBed", "NonExistentRoom");
|
||||
|
||||
// Act: Call the method under test
|
||||
var actualDischarge = await _dischargeRepository.GetDischargeByLocation(location);
|
||||
|
||||
// Assert: Verify that the result is null
|
||||
Assert.That(actualDischarge, Is.Null);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task GetDischargeByPointOfCareId_ExistingId_ReturnsDischarge()
|
||||
{
|
||||
// Arrange: Prepare test data
|
||||
var expectedId = ObjectId.GenerateNewId();
|
||||
var expectedDischarge =
|
||||
new Discharge
|
||||
{ Id = ObjectId.GenerateNewId(), PointOfCareId = expectedId };
|
||||
await _dischargeRepository.InsertOneAsync(expectedDischarge);
|
||||
|
||||
// Act: Call the method under test
|
||||
var actualDischarge = await _dischargeRepository.GetDischargeByPointOfCareId(expectedId);
|
||||
|
||||
// Assert: Verify the result
|
||||
Assert.That(actualDischarge, Is.Not.Null);
|
||||
Assert.That(actualDischarge?.Id, Is.EqualTo(expectedDischarge.Id));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task GetDischargeByPointOfCareId_NonExistentId_ReturnsNull()
|
||||
{
|
||||
// Arrange: Use a non-existent ID
|
||||
var nonExistentId = ObjectId.GenerateNewId();
|
||||
|
||||
// Act: Call the method under test
|
||||
var actualDischarge = await _dischargeRepository.GetDischargeByPointOfCareId(nonExistentId);
|
||||
|
||||
// Assert: Verify that the result is null
|
||||
Assert.That(actualDischarge, Is.Null);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task GetByPatientId_ExistingPatientId_ReturnsDischarge()
|
||||
{
|
||||
// Arrange: Prepare test data
|
||||
var expectedId = ObjectId.GenerateNewId();
|
||||
var expectedDischarge =
|
||||
new Discharge
|
||||
{ Id = ObjectId.GenerateNewId(), PatientId = expectedId };
|
||||
await _dischargeRepository.InsertOneAsync(expectedDischarge);
|
||||
|
||||
// Act: Call the method under test
|
||||
var actualDischarge = await _dischargeRepository.GetByPatientId(expectedId);
|
||||
|
||||
// Assert: Verify the result
|
||||
Assert.That(actualDischarge, Is.Not.Null);
|
||||
Assert.That(actualDischarge?.Id, Is.EqualTo(expectedDischarge.Id));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task GetByPatientId_NonExistentPatientId_ReturnsNull()
|
||||
{
|
||||
// Arrange: Use a non-existent patient ID
|
||||
var nonExistentId = ObjectId.GenerateNewId();
|
||||
|
||||
// Act: Call the method under test
|
||||
var actualDischarge = await _dischargeRepository.GetByPatientId(nonExistentId);
|
||||
|
||||
// Assert: Verify that the result is null
|
||||
Assert.That(actualDischarge, Is.Null);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
using adas_core.Application.Repositories.Interfaces;
|
||||
using adas_core.Domain.Enums;
|
||||
using adas_core.Domain.Models.AppSettings;
|
||||
using adas_core.Infrastructure.Repositories;
|
||||
using adas_core.Test.Utilities;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Options;
|
||||
using MongoDB.Bson;
|
||||
using Moq;
|
||||
|
||||
namespace adas_core.Test.Repositories;
|
||||
|
||||
[TestFixture]
|
||||
public class DisplayConfigTest
|
||||
{
|
||||
[OneTimeSetUp]
|
||||
public async Task SetUp()
|
||||
{
|
||||
Options.Create(new ApiSettings());
|
||||
_unitRepository = new Mock<IUnitRepository>();
|
||||
_displayCardConfigRepository =
|
||||
new DisplayCardConfigRepository(
|
||||
IntegrationDb.Database,
|
||||
new ApiSettings(),
|
||||
new Mock<ILogger<DisplayCardConfigRepository>>().Object
|
||||
);
|
||||
_displayConfigRepository =
|
||||
new DisplayConfigRepository(
|
||||
IntegrationDb.Database,
|
||||
new ApiSettings(),
|
||||
new Mock<ILogger<DisplayConfigRepository>>().Object,
|
||||
_unitRepository.Object
|
||||
);
|
||||
_displayDetailConfigRepository =
|
||||
new DisplayDetailConfigRepository(
|
||||
IntegrationDb.Database,
|
||||
new ApiSettings(),
|
||||
new Mock<ILogger<DisplayDetailConfigRepository>>().Object
|
||||
);
|
||||
|
||||
await _displayDetailConfigRepository.InsertOneAsync(
|
||||
TestUtilities.CreateValidCardDetailsNurseConfig(_configDisplayCardDetailsId));
|
||||
await _displayCardConfigRepository.InsertOneAsync(TestUtilities.CreateValidCardConfig(_configDisplayCardId));
|
||||
await _displayConfigRepository.InsertOneAsync(TestUtilities.CreateValidDisplayConfig(
|
||||
DisplayConfigEnums.DisplayType.DisplayNurse, _configDisplayId, _configDisplayCardId,
|
||||
_configDisplayCardDetailsId));
|
||||
}
|
||||
|
||||
[OneTimeTearDown]
|
||||
public async Task Cleanup()
|
||||
{
|
||||
// Clean up database after tests
|
||||
await IntegrationDb.Database.DropCollectionAsync("config_displays");
|
||||
await IntegrationDb.Database.DropCollectionAsync("config_displays_card");
|
||||
await IntegrationDb.Database.DropCollectionAsync("config_displays_detail");
|
||||
}
|
||||
|
||||
private DisplayConfigRepository _displayConfigRepository;
|
||||
private DisplayCardConfigRepository _displayCardConfigRepository;
|
||||
private DisplayDetailConfigRepository _displayDetailConfigRepository;
|
||||
private Mock<IUnitRepository> _unitRepository;
|
||||
private readonly ObjectId _configDisplayId = ObjectId.GenerateNewId();
|
||||
private readonly ObjectId _configDisplayCardId = ObjectId.GenerateNewId();
|
||||
private readonly ObjectId _configDisplayCardDetailsId = ObjectId.GenerateNewId();
|
||||
|
||||
[Test]
|
||||
public async Task GetByIdShouldReturnAllConfigsAssociated()
|
||||
{
|
||||
var result = await _displayConfigRepository.GetById(_configDisplayId);
|
||||
Assert.That(result, Is.Not.Null);
|
||||
using (Assert.EnterMultipleScope())
|
||||
{
|
||||
Assert.That(result.CardConfigId, Is.Not.Null);
|
||||
Assert.That(result.CardConfig, Is.Not.Null);
|
||||
Assert.That(result.DetailConfigId, Is.Not.Null);
|
||||
Assert.That(result.DetailConfig, Is.Not.Null);
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
namespace adas_core.Test.Repositories;
|
||||
|
||||
[TestFixture]
|
||||
public class DisplayRepisitoryTest
|
||||
{
|
||||
//private Mock<ILogger<DisplayRepository>> _mockLogger ;
|
||||
//private Mock<IOptions<ApiSettings>> _mockOptionsApiSettings ;
|
||||
|
||||
[OneTimeSetUp]
|
||||
public async Task OneTimeSetUp()
|
||||
{
|
||||
//_mockLogger = new Mock<ILogger<DisplayRepository>>();
|
||||
//_mockOptionsApiSettings = new Mock<IOptions<ApiSettings>>();
|
||||
|
||||
await IntegrationDb.Database.DropCollectionAsync("displays");
|
||||
await IntegrationDb.Database.CreateCollectionAsync("displays");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,212 @@
|
||||
using System.Diagnostics;
|
||||
using adas_core.Domain.Enums;
|
||||
using adas_core.Domain.Models.AppSettings;
|
||||
using adas_core.Domain.Models.MongoModels;
|
||||
using adas_core.Infrastructure.Repositories;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Options;
|
||||
using MongoDB.Bson;
|
||||
using Moq;
|
||||
using Options = Microsoft.Extensions.Options.Options;
|
||||
|
||||
namespace adas_core.Test.Repositories;
|
||||
|
||||
[TestFixture]
|
||||
[Category("Integration")]
|
||||
public class HistoricalConfigChangesRepositoryTest
|
||||
{
|
||||
[OneTimeSetUp]
|
||||
public void Init()
|
||||
{
|
||||
_optionsApiSettings = Options.Create(_apiSettings);
|
||||
_logger = new Mock<ILogger<HistoricalConfigChangesRepository>>();
|
||||
|
||||
|
||||
_repository =
|
||||
new HistoricalConfigChangesRepository(_optionsApiSettings, IntegrationDb.Database, _logger.Object);
|
||||
}
|
||||
|
||||
[TearDown]
|
||||
public async Task Cleanup()
|
||||
{
|
||||
await IntegrationDb.Database.DropCollectionAsync("historicalConfigChanges");
|
||||
await IntegrationDb.Database.CreateCollectionAsync("historicalConfigChanges");
|
||||
|
||||
await InitializeData();
|
||||
}
|
||||
|
||||
private HistoricalConfigChangesRepository _repository;
|
||||
|
||||
private readonly ApiSettings _apiSettings = new()
|
||||
{
|
||||
HistoricalConfigChanges = "historicalConfigChanges"
|
||||
};
|
||||
|
||||
private IOptions<ApiSettings> _optionsApiSettings;
|
||||
|
||||
private Mock<ILogger<HistoricalConfigChangesRepository>> _logger;
|
||||
|
||||
private async Task InitializeData()
|
||||
{
|
||||
var id = ObjectId.GenerateNewId();
|
||||
HistoricalConfigChanges historicalConfigChanges1 = new()
|
||||
{
|
||||
//Id = ObjectId.GenerateNewId(),
|
||||
ConfigType = DisplayConfigEnums.ConfigTypes.ObservationCfg,
|
||||
Time = DateTime.UtcNow,
|
||||
Username = "adas",
|
||||
OldConfig = new ConfigObservation
|
||||
{
|
||||
Id = id,
|
||||
Name = "Hemoglobina",
|
||||
Code = "54321",
|
||||
CodingSystem = "SNM"
|
||||
}.ToJson(),
|
||||
NewConfig = new ConfigObservation
|
||||
{
|
||||
Id = id,
|
||||
Name = "Hemoglobina",
|
||||
Code = "xxxxx",
|
||||
CodingSystem = "SNM"
|
||||
}.ToJson()
|
||||
};
|
||||
|
||||
await _repository.InsertOneAsync(historicalConfigChanges1);
|
||||
|
||||
HistoricalConfigChanges historicalConfigChanges2 = new()
|
||||
{
|
||||
//Id = ObjectId.GenerateNewId(),
|
||||
ConfigType = DisplayConfigEnums.ConfigTypes.ObservationCfg,
|
||||
Time = DateTime.UtcNow,
|
||||
Username = "adas",
|
||||
OldConfig = new ConfigObservation
|
||||
{
|
||||
Id = id,
|
||||
Name = "Hemoglobina",
|
||||
Code = "xxxxx",
|
||||
CodingSystem = "SNM"
|
||||
}.ToJson(),
|
||||
NewConfig = new ConfigObservation
|
||||
{
|
||||
Id = id,
|
||||
Name = "Hemoglobina",
|
||||
Code = "11111",
|
||||
CodingSystem = "SNM"
|
||||
}.ToJson()
|
||||
};
|
||||
|
||||
await _repository.InsertOneAsync(historicalConfigChanges2);
|
||||
|
||||
HistoricalConfigChanges historicalConfigChanges3 = new()
|
||||
{
|
||||
//Id = ObjectId.GenerateNewId(),
|
||||
ConfigType = DisplayConfigEnums.ConfigTypes.ObservationCfg,
|
||||
Time = DateTime.UtcNow,
|
||||
Username = "adas",
|
||||
OldConfig = new ConfigObservation
|
||||
{
|
||||
Id = id,
|
||||
Name = "Hemoglobina",
|
||||
Code = "111111",
|
||||
CodingSystem = "SNM"
|
||||
}.ToJson(),
|
||||
NewConfig = new ConfigObservation
|
||||
{
|
||||
Id = id,
|
||||
Name = "Hemoglobina",
|
||||
Code = "99999",
|
||||
CodingSystem = "SNM"
|
||||
}.ToJson()
|
||||
};
|
||||
|
||||
await _repository.InsertOneAsync(historicalConfigChanges3);
|
||||
}
|
||||
|
||||
|
||||
[Test]
|
||||
public async Task Get_HistoricalConfigChanges_returns_count_3()
|
||||
{
|
||||
await Cleanup();
|
||||
|
||||
var result = await _repository.FindAll();
|
||||
|
||||
Assert.That(result, Is.Not.Null);
|
||||
Assert.That(result, Has.Count.EqualTo(3));
|
||||
}
|
||||
|
||||
|
||||
[Test]
|
||||
public async Task HistoricalConfigChanges_returns_diferent_document()
|
||||
{
|
||||
await Cleanup();
|
||||
var id = ObjectId.GenerateNewId();
|
||||
var resultList = await _repository.FindAll();
|
||||
var result = resultList.FirstOrDefault();
|
||||
|
||||
Assert.That(result, Is.Not.Null);
|
||||
|
||||
Debug.Assert(result != null, nameof(result) + " != null");
|
||||
HistoricalConfigChanges updatedHistoricalConfigChanges = new()
|
||||
{
|
||||
Id = result.Id,
|
||||
ConfigType = DisplayConfigEnums.ConfigTypes.ObservationCfg,
|
||||
Time = DateTime.UtcNow,
|
||||
Username = "admin",
|
||||
OldConfig = new ConfigObservation
|
||||
{
|
||||
Id = id,
|
||||
Name = "Hemoglobina",
|
||||
Code = "12345",
|
||||
CodingSystem = "SNM"
|
||||
}.ToJson(),
|
||||
NewConfig = new ConfigObservation
|
||||
{
|
||||
Id = id,
|
||||
Name = "Hemoglobina",
|
||||
Code = "54321",
|
||||
CodingSystem = "SNM"
|
||||
}.ToJson()
|
||||
};
|
||||
|
||||
|
||||
var updated = await _repository.Update(updatedHistoricalConfigChanges);
|
||||
using (Assert.EnterMultipleScope())
|
||||
{
|
||||
Assert.That(updated, Is.Not.Null);
|
||||
Assert.That(result.Time, Is.EqualTo(updated?.Time));
|
||||
Assert.That(result.Id, Is.EqualTo(updated?.Id));
|
||||
Assert.That(result.Username, Is.EqualTo(updated?.Username));
|
||||
Assert.That(result.ConfigType, Is.EqualTo(updated?.ConfigType));
|
||||
Assert.That(result.OldConfig, Is.EqualTo(updated?.OldConfig));
|
||||
Assert.That(result.NewConfig, Is.EqualTo(updated?.NewConfig));
|
||||
};
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task FindLastHistoricalConfigChangesByUser_returns_3()
|
||||
{
|
||||
await Cleanup();
|
||||
var result = await _repository.FindLastHistoricalConfigChangesByUser("adas");
|
||||
|
||||
Assert.That(result, Is.Not.Null);
|
||||
Assert.That(result, Has.Count.EqualTo(3));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Delete_HistoricalConfigChanges_returns_count_2()
|
||||
{
|
||||
await Cleanup();
|
||||
var resultList = await _repository.FindAll();
|
||||
Assert.That(resultList, Has.Count.EqualTo(3));
|
||||
|
||||
var result = resultList.FirstOrDefault();
|
||||
Assert.That(result, Is.Not.Null);
|
||||
Debug.Assert(result != null, nameof(result) + " != null");
|
||||
var deleted = await _repository.DeleteAsync(result.Id);
|
||||
|
||||
Assert.That(deleted, Is.Not.Null);
|
||||
|
||||
var resultAfterDelete = await _repository.FindAll();
|
||||
Assert.That(resultAfterDelete, Has.Count.EqualTo(2));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
using adas_core.Infrastructure.Utils;
|
||||
using Mongo2Go;
|
||||
using MongoDB.Bson;
|
||||
using MongoDB.Driver;
|
||||
|
||||
namespace adas_core.Test.Repositories;
|
||||
|
||||
[SetUpFixture]
|
||||
[Category("Integration")]
|
||||
public class IntegrationDb
|
||||
{
|
||||
private const int TimeoutInSeconds = 60; // Set the desired timeout in seconds.
|
||||
public static MongoDbRunner? Runner { get; private set; }
|
||||
|
||||
private static MongoClient? Client { get; set; }
|
||||
|
||||
public static IMongoDatabase Database { get; private set; } = null!;
|
||||
public static string DatabaseName { get; } = "IntegrationTestDb";
|
||||
|
||||
[OneTimeSetUp]
|
||||
public void InitIntegrationTests()
|
||||
{
|
||||
StartMongoDbRunner().Wait();
|
||||
MongoDbHostBuilderExtension.ConfigureMongoDbConventions();
|
||||
MongoDbHostBuilderExtension.ConfigureRegisterMapClass();
|
||||
Client = new MongoClient(Runner?.ConnectionString);
|
||||
Database = Client.GetDatabase(DatabaseName);
|
||||
}
|
||||
|
||||
private static async Task StartMongoDbRunner()
|
||||
{
|
||||
Runner = MongoDbRunner.Start();
|
||||
|
||||
// Wait for the MongoDB server to become available or timeout.
|
||||
var startTime = DateTime.UtcNow;
|
||||
while (DateTime.UtcNow - startTime < TimeSpan.FromSeconds(TimeoutInSeconds))
|
||||
try
|
||||
{
|
||||
var testClient = new MongoClient(Runner.ConnectionString);
|
||||
var adminDb = testClient.GetDatabase("admin");
|
||||
await adminDb.RunCommandAsync((Command<BsonDocument>)"{ping:1}");
|
||||
return; // MongoDB server is available, continue.
|
||||
}
|
||||
catch
|
||||
{
|
||||
// Retry after a short delay.
|
||||
await Task.Delay(500);
|
||||
}
|
||||
|
||||
// Timeout reached, dispose the runner and throw an exception.
|
||||
Runner.Dispose();
|
||||
throw new TimeoutException("Timeout while starting MongoDB server.");
|
||||
}
|
||||
|
||||
[OneTimeTearDown]
|
||||
public void TeardownIntegrationTests()
|
||||
{
|
||||
Client?.Dispose();
|
||||
Runner?.Dispose();
|
||||
//_runner = null;
|
||||
//_client = null;
|
||||
//_fakeDb = null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,374 @@
|
||||
using adas_core.Domain.Models.AppSettings;
|
||||
using adas_core.Domain.Models.Filter;
|
||||
using adas_core.Domain.Models.Masters;
|
||||
using adas_core.Infrastructure.Repositories;
|
||||
using Microsoft.Extensions.Options;
|
||||
using MongoDB.Bson;
|
||||
|
||||
namespace adas_core.Test.Repositories;
|
||||
|
||||
[TestFixture]
|
||||
public class MasterListRepositoryTest
|
||||
{
|
||||
[OneTimeSetUp]
|
||||
public async Task Init()
|
||||
{
|
||||
// Set up MongoDB connection
|
||||
// var client = new MongoClient("mongodb://localhost:27017");
|
||||
// _database = client.GetDatabase("TestDatabase");
|
||||
|
||||
// Initialize ApiSettings
|
||||
_apiSettings = new ApiSettings
|
||||
{
|
||||
AltableOptionList = "altableOptionList",
|
||||
AllergyList = "allergyList",
|
||||
DestinationList = "destinationList",
|
||||
DiagnosisList = "diagnosisList",
|
||||
DischargeStatusList = "dischargeStatusList",
|
||||
DoctorList = "doctorList",
|
||||
DoctorTypeList = "doctorTypeList",
|
||||
InternalDestinationList = "internalDestinationList",
|
||||
InsulationList = "insulationList",
|
||||
LanguageBarrierList = "languageBarrierList",
|
||||
MobilityOptionList = "mobilityOptionList",
|
||||
OriginList = "originList",
|
||||
PatientStatusList = "patientStatusList",
|
||||
ProcedureList = "procedureList",
|
||||
TestList = "testList",
|
||||
ServiceList = "serviceList",
|
||||
TherapeuticCeilingList = "therapeuticCeilingList",
|
||||
TreatmentList = "treatmentList",
|
||||
VisitOptionList = "visitOptionList",
|
||||
AccessControlList = "accessControlList"
|
||||
};
|
||||
_optionsApiSettings = Options.Create(_apiSettings);
|
||||
|
||||
|
||||
// Insert sample data
|
||||
await IntegrationDb.Database.DropCollectionAsync("MasterLists");
|
||||
await IntegrationDb.Database.CreateCollectionAsync("MasterLists");
|
||||
|
||||
// Create the repository instance
|
||||
_repository = new MasterListRepository<MasterList>(_optionsApiSettings, IntegrationDb.Database);
|
||||
|
||||
// var masterListCollection = _database.GetCollection<MasterList>("MasterLists");
|
||||
// var sampleData = new List<MasterList>
|
||||
// {
|
||||
// new MasterList { Id = ObjectId.GenerateNewId(), Name = "Sample MasterList 1" },
|
||||
// new MasterList { Id = ObjectId.GenerateNewId(), Name = "Sample MasterList 2" }
|
||||
// };
|
||||
// await masterListCollection.InsertManyAsync(sampleData);
|
||||
}
|
||||
|
||||
[OneTimeTearDown]
|
||||
public async Task Cleanup()
|
||||
{
|
||||
// Clean up database after tests
|
||||
await IntegrationDb.Database.DropCollectionAsync("MasterLists");
|
||||
}
|
||||
|
||||
private MasterListRepository<MasterList> _repository;
|
||||
private ApiSettings _apiSettings;
|
||||
private IOptions<ApiSettings> _optionsApiSettings;
|
||||
|
||||
[Test]
|
||||
public async Task InsertOneAsync_ShouldInsertEntity()
|
||||
{
|
||||
// Arrange
|
||||
var masterList = new MasterList { Id = ObjectId.GenerateNewId(), Name = "Test MasterList" };
|
||||
|
||||
// Act
|
||||
await _repository.InsertOneAsync(masterList);
|
||||
await _repository.GetAll();
|
||||
var result = await _repository.FindById(masterList.Id, LocaleEnum.Default);
|
||||
|
||||
// Assert
|
||||
Assert.That(result, Is.Not.Null);
|
||||
Assert.That(result?.Name, Is.EqualTo("Test MasterList"));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Delete_ShouldDeleteEntity()
|
||||
{
|
||||
// Arrange
|
||||
var masterList = new MasterList { Id = ObjectId.GenerateNewId(), Name = "ToDelete MasterList" };
|
||||
await _repository.InsertOneAsync(masterList);
|
||||
|
||||
// Act
|
||||
await _repository.Delete(masterList.Id);
|
||||
var result = await _repository.FindById(masterList.Id, LocaleEnum.Default);
|
||||
|
||||
// Assert
|
||||
Assert.That(result, Is.Null);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Update_ShouldUpdateEntity()
|
||||
{
|
||||
// Arrange
|
||||
var masterList = new MasterList { Id = ObjectId.GenerateNewId(), Name = "Original MasterList" };
|
||||
await _repository.InsertOneAsync(masterList);
|
||||
masterList.Name = "Updated MasterList";
|
||||
|
||||
// Act
|
||||
await _repository.Update(masterList);
|
||||
var result = await _repository.FindById(masterList.Id, LocaleEnum.Default);
|
||||
|
||||
// Assert
|
||||
Assert.That(result, Is.Not.Null);
|
||||
Assert.That(result?.Name, Is.EqualTo("Updated MasterList"));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task FindById_ShouldReturnEntity_WhenFound()
|
||||
{
|
||||
// Arrange
|
||||
var masterList = new MasterList { Id = ObjectId.GenerateNewId(), Name = "FindById MasterList" };
|
||||
await _repository.InsertOneAsync(masterList);
|
||||
|
||||
// Act
|
||||
var result = await _repository.FindById(masterList.Id, LocaleEnum.Default);
|
||||
|
||||
// Assert
|
||||
Assert.That(result, Is.Not.Null);
|
||||
Assert.That(result?.Name, Is.EqualTo("FindById MasterList"));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task FindById_WithDifferentLocale_ShouldReturnEntity_WhenFound()
|
||||
{
|
||||
// Arrange
|
||||
var masterList = new MasterList
|
||||
{
|
||||
Id = ObjectId.GenerateNewId(), Name = "FindById MasterList",
|
||||
DefaultLocale = LocaleEnum.Es,
|
||||
Options = new List<OptionList>
|
||||
{
|
||||
new()
|
||||
{
|
||||
Id = ObjectId.GenerateNewId(),
|
||||
Name = "Opción 1",
|
||||
Description = "Descripción 1",
|
||||
LocaleItems = new Locale
|
||||
{
|
||||
Eng = new LocaleItem { Name = "Option eng" },
|
||||
Ca = new LocaleItem { Name = "Opciò cat" },
|
||||
Pt = new LocaleItem { Name = "Opçao por" }
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
await _repository.InsertOneAsync(masterList);
|
||||
|
||||
// Act
|
||||
var resultZh = await _repository.FindById(masterList.Id, LocaleEnum.Zh);
|
||||
var resultPt = await _repository.FindById(masterList.Id, LocaleEnum.Pt);
|
||||
var resultEnUs = await _repository.FindById(masterList.Id, LocaleEnum.Eng);
|
||||
var resultCa = await _repository.FindById(masterList.Id, LocaleEnum.Ca);
|
||||
var resultEs = await _repository.FindById(masterList.Id, LocaleEnum.Es);
|
||||
|
||||
// Assert ZH return default
|
||||
Assert.That(resultZh, Is.Not.Null);
|
||||
Assert.That(resultZh?.Options[0].Name, Is.EqualTo("Opción 1"));
|
||||
Assert.That(resultZh?.Options[0].LocaleItems, Is.Null);
|
||||
// Assert Pt
|
||||
Assert.That(resultPt, Is.Not.Null);
|
||||
Assert.That(resultPt?.Options[0].Name, Is.EqualTo("Opçao por"));
|
||||
Assert.That(resultPt?.Options[0].LocaleItems, Is.Null);
|
||||
// Assert Eng
|
||||
Assert.That(resultEnUs, Is.Not.Null);
|
||||
Assert.That(resultEnUs?.Options[0].Name, Is.EqualTo("Option eng"));
|
||||
Assert.That(resultEnUs?.Options[0].LocaleItems, Is.Null);
|
||||
// Assert resultCa
|
||||
Assert.That(resultCa, Is.Not.Null);
|
||||
Assert.That(resultCa?.Options[0].Name, Is.EqualTo("Opciò cat"));
|
||||
Assert.That(resultCa?.Options[0].LocaleItems, Is.Null);
|
||||
// Assert resultEs
|
||||
Assert.That(resultEs, Is.Not.Null);
|
||||
Assert.That(resultEs?.Options[0].Name, Is.EqualTo("Opción 1"));
|
||||
Assert.That(resultEs?.Options[0].LocaleItems, Is.Null);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task FindById_WithDifferentLocale_ShouldReturnEntity_WhenFound_AndDefaultLocale()
|
||||
{
|
||||
// Arrange
|
||||
var masterList = new MasterList
|
||||
{
|
||||
Id = ObjectId.GenerateNewId(), Name = "FindById MasterList",
|
||||
DefaultLocale = LocaleEnum.Es,
|
||||
Options = new List<OptionList>
|
||||
{
|
||||
new()
|
||||
{
|
||||
Id = ObjectId.GenerateNewId(),
|
||||
Name = "Opción 1",
|
||||
Description = "Descripción 1"
|
||||
}
|
||||
}
|
||||
};
|
||||
await _repository.InsertOneAsync(masterList);
|
||||
|
||||
// Act
|
||||
var resultZh = await _repository.FindById(masterList.Id, LocaleEnum.Zh);
|
||||
var resultPt = await _repository.FindById(masterList.Id, LocaleEnum.Pt);
|
||||
var resultEnUs = await _repository.FindById(masterList.Id, LocaleEnum.Eng);
|
||||
var resultCa = await _repository.FindById(masterList.Id, LocaleEnum.Ca);
|
||||
var resultEs = await _repository.FindById(masterList.Id, LocaleEnum.Es);
|
||||
|
||||
// Assert ZH return default
|
||||
Assert.That(resultZh, Is.Not.Null);
|
||||
Assert.That(resultZh?.Options[0].Name, Is.EqualTo("Opción 1"));
|
||||
// Assert Pt
|
||||
Assert.That(resultPt, Is.Not.Null);
|
||||
Assert.That(resultPt?.Options[0].Name, Is.EqualTo("Opción 1"));
|
||||
// Assert Eng
|
||||
Assert.That(resultEnUs, Is.Not.Null);
|
||||
Assert.That(resultEnUs?.Options[0].Name, Is.EqualTo("Opción 1"));
|
||||
// Assert resultCa
|
||||
Assert.That(resultCa, Is.Not.Null);
|
||||
Assert.That(resultCa?.Options[0].Name, Is.EqualTo("Opción 1"));
|
||||
// Assert resultEs
|
||||
Assert.That(resultEs, Is.Not.Null);
|
||||
Assert.That(resultEs?.Options[0].Name, Is.EqualTo("Opción 1"));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task FindByName_ShouldReturnEntity_WhenFound()
|
||||
{
|
||||
// Arrange
|
||||
var masterList = new MasterList { Id = ObjectId.GenerateNewId(), Name = "FindByName MasterList" };
|
||||
await _repository.InsertOneAsync(masterList);
|
||||
|
||||
// Act
|
||||
var result = await _repository.FindByName(masterList.Name);
|
||||
|
||||
// Assert
|
||||
Assert.That(result, Is.Not.Null);
|
||||
Assert.That(result?.Name, Is.EqualTo("FindByName MasterList"));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task GetAll_ShouldReturnAllEntities()
|
||||
{
|
||||
// Act
|
||||
var result = await _repository.GetAll();
|
||||
|
||||
// Assert
|
||||
Assert.That(result.Count(), Is.GreaterThanOrEqualTo(2));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task GetMasterListByIdAndSearchOptiionsByLocale()
|
||||
{
|
||||
// Arrange
|
||||
var masterList = new MasterList
|
||||
{
|
||||
Id = ObjectId.GenerateNewId(), Name = "FindById MasterList",
|
||||
DefaultLocale = LocaleEnum.Es,
|
||||
Options = new List<OptionList>
|
||||
{
|
||||
new()
|
||||
{
|
||||
Id = ObjectId.GenerateNewId(),
|
||||
Name = "Opción 1",
|
||||
Description = "AC0.01",
|
||||
LocaleItems = new Locale
|
||||
{
|
||||
Eng = new LocaleItem { Name = "Option eng" },
|
||||
Ca = new LocaleItem { Name = "Opciò cat" },
|
||||
Pt = new LocaleItem { Name = "Opçao por" }
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
await _repository.InsertOneAsync(masterList);
|
||||
var resultPt = await _repository.GetMasterListByIdAndSearchOptions(masterList.Id,
|
||||
new FilterOptionListElement { Locale = LocaleEnum.Pt, Text = "Op" });
|
||||
Assert.That(resultPt, Is.Not.Null);
|
||||
Assert.That(resultPt[0].Name, Is.EqualTo("Opçao por"));
|
||||
var resultEng = await _repository.GetMasterListByIdAndSearchOptions(masterList.Id,
|
||||
new FilterOptionListElement { Locale = LocaleEnum.Eng, Description = "AC0.01" });
|
||||
Assert.That(resultEng, Is.Not.Null);
|
||||
Assert.That(resultEng[0].Name, Is.EqualTo("Option eng"));
|
||||
var resultEs = await _repository.GetMasterListByIdAndSearchOptions(masterList.Id,
|
||||
new FilterOptionListElement { Locale = LocaleEnum.Es, Text = "Op" });
|
||||
Assert.That(resultEs, Is.Not.Null);
|
||||
Assert.That(resultEs[0].Name, Is.EqualTo("Opción 1"));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task UpdateMasterListOptionByLocale()
|
||||
{
|
||||
// Arrange
|
||||
var listId = ObjectId.GenerateNewId();
|
||||
var optionId = ObjectId.GenerateNewId();
|
||||
var masterList = new MasterList
|
||||
{
|
||||
Id = listId, Name = "FindById MasterList",
|
||||
DefaultLocale = LocaleEnum.Es,
|
||||
OptionListDetails = new OptionListDetails { Name = new Element { IsRequired = true } },
|
||||
Options = new List<OptionList>
|
||||
{
|
||||
new()
|
||||
{
|
||||
Id = optionId,
|
||||
Name = "Opción 1",
|
||||
Description = "AC0.01",
|
||||
LocaleItems = new Locale
|
||||
{
|
||||
Eng = new LocaleItem { Name = "Option eng" },
|
||||
Ca = new LocaleItem { Name = "Opciò cat" },
|
||||
Pt = new LocaleItem { Name = "Opçao por" }
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
await _repository.InsertOneAsync(masterList);
|
||||
var optionPt = new OptionList
|
||||
{
|
||||
Id = optionId,
|
||||
Name = "Opçao çambiada"
|
||||
};
|
||||
var resultPt = await _repository.UpdateMasterListOption(masterList.Id, optionPt, LocaleEnum.Pt);
|
||||
Assert.That(resultPt, Is.Not.Null);
|
||||
Assert.That(resultPt.Name, Is.EqualTo("Opçao çambiada"));
|
||||
var realResult = await _repository.FindById(listId);
|
||||
Assert.That(realResult?.Options[0].Name, Is.EqualTo("Opción 1"));
|
||||
await _repository.UpdateMasterListOption(masterList.Id, optionPt, LocaleEnum.Pt);
|
||||
// _pointOfCareRepositoryMock.Verify(m => m.FindById(patientLocationId), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task FindOptionItemByLocaleAndId()
|
||||
{
|
||||
var masterId = ObjectId.GenerateNewId();
|
||||
var optionId = ObjectId.GenerateNewId();
|
||||
var masterList = new MasterList
|
||||
{
|
||||
Id = masterId, Name = "FindById MasterList",
|
||||
DefaultLocale = LocaleEnum.Es,
|
||||
Options = new List<OptionList>
|
||||
{
|
||||
new()
|
||||
{
|
||||
Id = optionId,
|
||||
Name = "Opción 1",
|
||||
Description = "AC0.01",
|
||||
LocaleItems = new Locale
|
||||
{
|
||||
Eng = new LocaleItem { Name = "Option eng" },
|
||||
Ca = new LocaleItem { Name = "Opciò cat" },
|
||||
Pt = new LocaleItem { Name = "Opçao por" }
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
await _repository.InsertOneAsync(masterList);
|
||||
var result = await _repository.FindOptionItemById(masterId, optionId, LocaleEnum.Pt);
|
||||
|
||||
Assert.That(result, Is.Not.Null);
|
||||
Assert.That(result.Id!, Is.EqualTo(optionId));
|
||||
Assert.That(result.Name, Is.EqualTo("Opçao por"));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,132 @@
|
||||
using adas_core.Domain.Enums;
|
||||
using adas_core.Domain.Models;
|
||||
using adas_core.Domain.Models.AppSettings;
|
||||
using adas_core.Infrastructure.Repositories;
|
||||
using Microsoft.Extensions.Options;
|
||||
using MongoDB.Bson;
|
||||
using Options = Microsoft.Extensions.Options.Options;
|
||||
|
||||
namespace adas_core.Test.Repositories;
|
||||
|
||||
[TestFixture]
|
||||
[Category("Integration")]
|
||||
public class MedicineRepositoryTest
|
||||
{
|
||||
[OneTimeSetUp]
|
||||
public async Task Init()
|
||||
{
|
||||
_optionsApiSettings = Options.Create(_apiSettings);
|
||||
|
||||
_testMedicine = new Medicine
|
||||
{
|
||||
Id = ObjectId.GenerateNewId(),
|
||||
Codes = ["12345"],
|
||||
Notes = ["note1"],
|
||||
Name = "test",
|
||||
Group = [MedicineEnum.Group.Metabolic.ToString()]
|
||||
};
|
||||
|
||||
await IntegrationDb.Database.DropCollectionAsync("medicines");
|
||||
await IntegrationDb.Database.CreateCollectionAsync("medicines");
|
||||
|
||||
_repository = new MedicineRepository(_optionsApiSettings, IntegrationDb.Database);
|
||||
|
||||
await _repository.InsertOneAsync(_testMedicine);
|
||||
|
||||
var result = _repository.GetMedicineByCodeOrNote(["12345"]);
|
||||
|
||||
Assert.That(result, Is.Not.Null);
|
||||
}
|
||||
|
||||
private MedicineRepository _repository = null!;
|
||||
|
||||
private readonly ApiSettings _apiSettings = new()
|
||||
{
|
||||
Medicines = "medicines"
|
||||
};
|
||||
|
||||
private Medicine _testMedicine = null!;
|
||||
|
||||
private IOptions<ApiSettings> _optionsApiSettings = null!;
|
||||
|
||||
[Test]
|
||||
public async Task Get_Medicines_Of_Treatments_Return_Empty_List()
|
||||
{
|
||||
var result = await _repository.GetMedicineByCodeOrNote([""]);
|
||||
|
||||
Assert.That(result, Is.Not.Null);
|
||||
Assert.That(result, Is.Empty);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Get_Medicines_Of_Treatments_By_Code_When_Have_Code_And_Notes_Return_Paracetamol()
|
||||
{
|
||||
var id = ObjectId.GenerateNewId();
|
||||
var paracetamolMedicine = new Medicine
|
||||
{
|
||||
Id = id,
|
||||
Codes = ["322236009", "322246006", "370151002", "605677", "605779"],
|
||||
Type = ["ParacetamolOpiates"],
|
||||
Name = "Paracetamol"
|
||||
};
|
||||
|
||||
await _repository.InsertOneAsync(paracetamolMedicine);
|
||||
|
||||
var result = await _repository.GetMedicineByCodeOrNote([
|
||||
"Medicación", "Formulary Drug Intake", "1.12 kg", "605677"
|
||||
]);
|
||||
using (Assert.EnterMultipleScope())
|
||||
{
|
||||
Assert.That(result, Is.Not.Null);
|
||||
Assert.That(id, Is.EqualTo(result.First().Id));
|
||||
};
|
||||
Assert.That(result, Has.Count.EqualTo(1));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task GetMedicine_Find_Code_Return_Medicine()
|
||||
{
|
||||
var result = await _repository.GetMedicine("12345");
|
||||
|
||||
Assert.That(result, Is.Not.Null);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task GetMedicine_Not_Find_Code_Return_Empty()
|
||||
{
|
||||
var result = await _repository.GetMedicine("123456");
|
||||
|
||||
Assert.That(result, Is.Null);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task GetMedicine_Find_Note_Return_Medicine()
|
||||
{
|
||||
var result = await _repository.GetMedicine("note1");
|
||||
|
||||
Assert.That(result, Is.Not.Null);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task UpdateMedicine_Return_Medicine()
|
||||
{
|
||||
var updateMedicine = new Medicine
|
||||
{
|
||||
Id = _testMedicine.Id,
|
||||
Codes = ["12345"],
|
||||
Notes = ["note1", "note2", "note3"],
|
||||
Name = "test",
|
||||
Group = [MedicineEnum.Group.Metabolic.ToString()]
|
||||
};
|
||||
|
||||
var result = await _repository.UpdateMedicine(updateMedicine);
|
||||
var resultUpdate = await _repository.GetMedicineById(_testMedicine.Id);
|
||||
using (Assert.EnterMultipleScope())
|
||||
{
|
||||
|
||||
Assert.That(result, Is.Not.Null);
|
||||
Assert.That(resultUpdate, Is.Not.Null);
|
||||
Assert.That(resultUpdate?.Notes != null && resultUpdate.Notes.Contains("note3"), Is.True);
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,156 @@
|
||||
using adas_core.Domain.Enums;
|
||||
using adas_core.Domain.Models;
|
||||
using adas_core.Domain.Models.AppSettings;
|
||||
using adas_core.Domain.Models.MongoModels;
|
||||
using adas_core.Infrastructure.Migrations.MongoMigrations;
|
||||
using adas_core.Infrastructure.Repositories;
|
||||
using Microsoft.Extensions.Options;
|
||||
using MongoDB.Bson;
|
||||
using MongoDB.Driver;
|
||||
using MongoMigrations.Core;
|
||||
using Options = Microsoft.Extensions.Options.Options;
|
||||
|
||||
namespace adas_core.Test.Repositories;
|
||||
|
||||
[TestFixture]
|
||||
[Category("Integration")]
|
||||
public class MongodbMigrationTest
|
||||
{
|
||||
private PatientRepository _repository;
|
||||
private IOptions<ApiSettings> _optionsApiSettings;
|
||||
|
||||
private readonly ApiSettings _apiSettings = new()
|
||||
{
|
||||
ArchivePatientsDiagnosis = "patients"
|
||||
};
|
||||
|
||||
private static readonly DateTime Now = DateTime.Now;
|
||||
private static readonly ObjectId PatientId = ObjectId.GenerateNewId();
|
||||
|
||||
[OneTimeSetUp]
|
||||
public async Task Init()
|
||||
{
|
||||
_optionsApiSettings = Options.Create(_apiSettings);
|
||||
|
||||
await IntegrationDb.Database.DropCollectionAsync("patients");
|
||||
await IntegrationDb.Database.CreateCollectionAsync("patients");
|
||||
|
||||
_repository = new PatientRepository(_optionsApiSettings, IntegrationDb.Database);
|
||||
|
||||
var patients = new List<Patient>
|
||||
{
|
||||
new ()
|
||||
{
|
||||
Id = PatientId,
|
||||
PatientId = "patientId1",
|
||||
PointOfCareId = ObjectId.GenerateNewId(),
|
||||
Bed = "bed1",
|
||||
PatientNumber = "patientNumber1",
|
||||
Person = new Person
|
||||
{
|
||||
FirstName = "firstName1",
|
||||
LastName = "lastName1",
|
||||
BirthDate = new DateTime(),
|
||||
Gender = PatientEnum.Gender.Male
|
||||
},
|
||||
DisTime = Now
|
||||
},
|
||||
new ()
|
||||
{
|
||||
PatientId = "patientId2",
|
||||
PointOfCareId = ObjectId.GenerateNewId(),
|
||||
Bed = "bed2",
|
||||
PatientNumber = "patientNumber2",
|
||||
Person = new Person
|
||||
{
|
||||
FirstName = "firstName2",
|
||||
LastName = "lastName2",
|
||||
BirthDate = new DateTime(),
|
||||
Gender = PatientEnum.Gender.Male
|
||||
}
|
||||
},
|
||||
new ()
|
||||
{
|
||||
PatientId = "patientId3",
|
||||
PointOfCareId = ObjectId.GenerateNewId(),
|
||||
Bed = "bed3",
|
||||
PatientNumber = "patientNumber3",
|
||||
Person = new Person
|
||||
{
|
||||
FirstName = "firstName3",
|
||||
LastName = "lastName3",
|
||||
BirthDate = new DateTime(),
|
||||
Gender = PatientEnum.Gender.Male
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
await _repository.InsertManyAsync(patients);
|
||||
Assert.That(await _repository.Collection.CountDocumentsAsync(_ => true), Is.EqualTo(3));
|
||||
}
|
||||
|
||||
[OneTimeTearDown]
|
||||
public void Teardown()
|
||||
{
|
||||
IntegrationDb.Database.DropCollection("patients");
|
||||
IntegrationDb.Database.DropCollection("__migrations");
|
||||
}
|
||||
|
||||
[Test]
|
||||
[Order(1)]
|
||||
public void IsDatabaseOutdated_ShouldReturnTrue()
|
||||
{
|
||||
var runner = CreateRunner();
|
||||
|
||||
var isUpToDate = runner.IsDatabaseUpToDate(ReadPreference.Primary);
|
||||
|
||||
Assert.That(isUpToDate, Is.False);
|
||||
}
|
||||
|
||||
|
||||
[Test]
|
||||
[Order(2)]
|
||||
public void AfterUpdateToLatest_MigrationShouldBeApplied()
|
||||
{
|
||||
var runner = CreateRunner();
|
||||
|
||||
runner.UpdateToLatest();
|
||||
var ids = GetAppliedMigrationIds();
|
||||
Assert.That(ids, Does.Contain(10)); // 0.1.0
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void RunningMigrationsTwice_ShouldNotFail()
|
||||
{
|
||||
var runner = CreateRunner();
|
||||
|
||||
runner.UpdateToLatest();
|
||||
runner.UpdateToLatest(); // segunda ejecución
|
||||
|
||||
var ids = GetAppliedMigrationIds();
|
||||
Assert.That(ids, Does.Contain(10));
|
||||
}
|
||||
|
||||
// Helpers
|
||||
private MigrationRunner CreateRunner()
|
||||
{
|
||||
var locator = new MigrationLocator();
|
||||
locator.LookForMigrationsInAssembly(typeof(U_0_1_0_UpdateDataPatien).Assembly);
|
||||
return new MigrationRunner(
|
||||
IntegrationDb.Database,
|
||||
collectionName: "__migrations",
|
||||
migrationLocator: locator
|
||||
);
|
||||
}
|
||||
|
||||
private List<int> GetAppliedMigrationIds()
|
||||
{
|
||||
var collection = IntegrationDb.Database.GetCollection<BsonDocument>("__migrations");
|
||||
|
||||
return collection
|
||||
.Find(FilterDefinition<BsonDocument>.Empty)
|
||||
.ToList()
|
||||
.Select(d => d["_id"].AsInt32)
|
||||
.ToList();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
using adas_core.Domain.Enums;
|
||||
using adas_core.Domain.Models;
|
||||
using adas_core.Domain.Models.AppSettings;
|
||||
using adas_core.Infrastructure.Repositories;
|
||||
using Microsoft.Extensions.Options;
|
||||
using MongoDB.Bson;
|
||||
using MongoDB.Driver;
|
||||
using Options = Microsoft.Extensions.Options.Options;
|
||||
|
||||
namespace adas_core.Test.Repositories;
|
||||
|
||||
[TestFixture]
|
||||
[Category("Integration")]
|
||||
public class ObservationArchiveRepositoryTest
|
||||
{
|
||||
[OneTimeSetUp]
|
||||
public async Task Init()
|
||||
{
|
||||
_optionsApiSettings = Options.Create(_apiSettings);
|
||||
|
||||
var patientObservation1 = new PatientObservation
|
||||
{
|
||||
PatientId = PatientId,
|
||||
Code = "263490005",
|
||||
CodingSystem = "SNM",
|
||||
Name = "Estado",
|
||||
Status = StatusEnum.Type.Ok,
|
||||
Time = Now,
|
||||
Value = "Sin alergias conocidas"
|
||||
};
|
||||
|
||||
var patientObservation2 = new PatientObservation
|
||||
{
|
||||
PatientId = PatientId,
|
||||
Code = "300916003",
|
||||
CodingSystem = "SNM",
|
||||
Name = "¿Alergia al látex?",
|
||||
Status = StatusEnum.Type.Ok,
|
||||
Time = Now.AddDays(-1),
|
||||
Value = "No"
|
||||
};
|
||||
|
||||
await IntegrationDb.Database.DropCollectionAsync("archive_patients_observations");
|
||||
await IntegrationDb.Database.CreateCollectionAsync("archive_patients_observations");
|
||||
|
||||
_repository = new ObservationArchiveRepository(_optionsApiSettings, IntegrationDb.Database);
|
||||
|
||||
await _repository.InsertOneAsync(patientObservation1);
|
||||
await _repository.InsertOneAsync(patientObservation2);
|
||||
}
|
||||
|
||||
private ObservationArchiveRepository _repository;
|
||||
|
||||
private readonly ApiSettings _apiSettings = new()
|
||||
{
|
||||
ArchivePatientsDiagnosis = "archive_patients_observations"
|
||||
};
|
||||
|
||||
private IOptions<ApiSettings> _optionsApiSettings;
|
||||
|
||||
private static readonly DateTime Now = DateTime.Now;
|
||||
private static readonly ObjectId PatientId = ObjectId.GenerateNewId();
|
||||
|
||||
|
||||
[Test]
|
||||
public async Task DeleteBeforeDate()
|
||||
{
|
||||
var filter = Builders<PatientObservation>.Filter.Eq(p => p.PatientId, PatientId);
|
||||
|
||||
var result = await _repository.Collection.FindAsync(filter);
|
||||
|
||||
Assert.That(result, Is.Not.Null);
|
||||
Assert.That(result.ToList(), Has.Count.GreaterThan(1));
|
||||
|
||||
await _repository.DeleteBeforeDate(Now);
|
||||
|
||||
var resultDelete = await _repository.Collection.FindAsync(filter);
|
||||
|
||||
Assert.That(resultDelete, Is.Not.Null);
|
||||
Assert.That(resultDelete.ToList(), Has.Count.EqualTo(1));
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,106 @@
|
||||
using adas_core.Domain.Enums;
|
||||
using adas_core.Domain.Models;
|
||||
using adas_core.Domain.Models.AppSettings;
|
||||
using adas_core.Domain.Models.MongoModels;
|
||||
using adas_core.Infrastructure.Repositories;
|
||||
using Microsoft.Extensions.Options;
|
||||
using Options = Microsoft.Extensions.Options.Options;
|
||||
|
||||
namespace adas_core.Test.Repositories;
|
||||
|
||||
[TestFixture]
|
||||
[Category("Integration")]
|
||||
public class PatientArchiveRepositoryTest
|
||||
{
|
||||
[OneTimeSetUp]
|
||||
public async Task Init()
|
||||
{
|
||||
_optionsApiSettings = Options.Create(_apiSettings);
|
||||
|
||||
var patient1 = new Patient
|
||||
{
|
||||
UnitString = "PointOfCare",
|
||||
Bed = "bed1",
|
||||
PatientNumber = "patientNumber1",
|
||||
Person = new Person
|
||||
{
|
||||
FirstName = "firstName1",
|
||||
LastName = "lastName1",
|
||||
BirthDate = new DateTime(),
|
||||
Gender = PatientEnum.Gender.Male
|
||||
}
|
||||
};
|
||||
|
||||
var patient2 = new Patient
|
||||
{
|
||||
UnitString = "PointOfCare",
|
||||
Bed = "bed2",
|
||||
PatientNumber = "patientNumber2",
|
||||
Person = new Person
|
||||
{
|
||||
FirstName = "firstName2",
|
||||
LastName = "lastName2",
|
||||
BirthDate = new DateTime(),
|
||||
Gender = PatientEnum.Gender.Male
|
||||
}
|
||||
};
|
||||
|
||||
var patient3 = new Patient
|
||||
{
|
||||
UnitString = "PointOfCare",
|
||||
Bed = "bed3",
|
||||
PatientNumber = "patientNumber3",
|
||||
Person = new Person
|
||||
{
|
||||
FirstName = "firstName3",
|
||||
LastName = "lastName3",
|
||||
BirthDate = new DateTime(),
|
||||
Gender = PatientEnum.Gender.Male
|
||||
}
|
||||
};
|
||||
|
||||
await IntegrationDb.Database.DropCollectionAsync("archive_patient");
|
||||
await IntegrationDb.Database.CreateCollectionAsync("archive_patient");
|
||||
|
||||
_repository = new PatientArchiveRepository(_optionsApiSettings, IntegrationDb.Database);
|
||||
|
||||
await _repository.InsertOneAsync(patient1);
|
||||
await _repository.InsertOneAsync(patient2);
|
||||
await _repository.InsertOneAsync(patient3);
|
||||
}
|
||||
|
||||
private PatientArchiveRepository _repository;
|
||||
|
||||
private readonly ApiSettings _apiSettings = new()
|
||||
{
|
||||
ArchivePatientsDiagnosis = "archive_patients"
|
||||
};
|
||||
|
||||
private IOptions<ApiSettings> _optionsApiSettings;
|
||||
|
||||
[Test]
|
||||
public async Task FindByPatientNumber_Not_Find_Return_null()
|
||||
{
|
||||
var result = await _repository.FindByPatientNumber("");
|
||||
|
||||
Assert.That(result, Is.Null);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task FindByPatientNumber_Find_Return_Patient()
|
||||
{
|
||||
var result = await _repository.FindByPatientNumber("patientNumber1");
|
||||
|
||||
Assert.That(result, Is.Not.Null);
|
||||
Assert.That(result.PatientNumber, Is.EqualTo("patientNumber1"));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task FindAll_Return_List_Patients()
|
||||
{
|
||||
var result = await _repository.FindAll();
|
||||
|
||||
Assert.That(result, Is.Not.Null);
|
||||
Assert.That(result.Count, Is.EqualTo(3));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,624 @@
|
||||
using adas_core.Domain.Enums;
|
||||
using adas_core.Domain.Models;
|
||||
using adas_core.Domain.Models.AppSettings;
|
||||
using adas_core.Domain.Models.Masters;
|
||||
using adas_core.Domain.Models.MongoModels;
|
||||
using adas_core.Infrastructure.Repositories;
|
||||
using Microsoft.Extensions.Options;
|
||||
using MongoDB.Bson;
|
||||
using MongoDB.Driver;
|
||||
using Options = Microsoft.Extensions.Options.Options;
|
||||
|
||||
namespace adas_core.Test.Repositories;
|
||||
|
||||
[TestFixture]
|
||||
[Category("Integration")]
|
||||
public class PatientRepositoryTest
|
||||
{
|
||||
//private static readonly ObjectId id = ObjectId.GenerateNewId();
|
||||
|
||||
[OneTimeSetUp]
|
||||
public async Task Init()
|
||||
{
|
||||
_optionsApiSettings = Options.Create(_apiSettings);
|
||||
|
||||
var patient1 = new Patient
|
||||
{
|
||||
Id = PatientId,
|
||||
PatientId = "patientId1",
|
||||
UnitId = _unit1,
|
||||
PointOfCareId = _poc1.Id,
|
||||
PatientNumber = "patientNumber1",
|
||||
Person = new Person
|
||||
{
|
||||
FirstName = "firstName1",
|
||||
LastName = "lastName1",
|
||||
BirthDate = new DateTime(),
|
||||
Gender = PatientEnum.Gender.Male
|
||||
},
|
||||
DisTime = Now,
|
||||
Origin = new OptionList
|
||||
{
|
||||
Id = OriginListOptionId,
|
||||
Name = "Urlogy"
|
||||
}
|
||||
};
|
||||
|
||||
var patient2 = new Patient
|
||||
{
|
||||
UnitId = _unit1,
|
||||
PatientId = "patientId2",
|
||||
PointOfCareId = _poc2.Id,
|
||||
PatientNumber = "patientNumber2",
|
||||
Person = new Person
|
||||
{
|
||||
FirstName = "firstName2",
|
||||
LastName = "lastName2",
|
||||
BirthDate = new DateTime(),
|
||||
Gender = PatientEnum.Gender.Male
|
||||
}
|
||||
};
|
||||
|
||||
var patient3 = new Patient
|
||||
{
|
||||
UnitId = _unit1,
|
||||
PatientId = "patientId3",
|
||||
PointOfCareId = _poc3.Id,
|
||||
PatientNumber = "patientNumber3",
|
||||
Person = new Person
|
||||
{
|
||||
FirstName = "firstName3",
|
||||
LastName = "lastName3",
|
||||
BirthDate = new DateTime(),
|
||||
Gender = PatientEnum.Gender.Male
|
||||
}
|
||||
};
|
||||
var patient4 = new Patient
|
||||
{
|
||||
UnitId = _pocMoved.UnitId,
|
||||
PatientId = "patientId4",
|
||||
PointOfCareId = _pocMoved.Id,
|
||||
PatientNumber = "patientNumber4",
|
||||
Person = new Person
|
||||
{
|
||||
FirstName = "firstName4",
|
||||
LastName = "lastName4",
|
||||
BirthDate = new DateTime(),
|
||||
Gender = PatientEnum.Gender.Male
|
||||
}
|
||||
};
|
||||
var patient5 = new Patient
|
||||
{
|
||||
UnitId = _pocPushed.UnitId,
|
||||
PatientId = "patientId5",
|
||||
PointOfCareId = _pocPushed.Id,
|
||||
PatientNumber = "patientNumber5",
|
||||
Person = new Person
|
||||
{
|
||||
FirstName = "firstName5",
|
||||
LastName = "lastName5",
|
||||
BirthDate = new DateTime(),
|
||||
Gender = PatientEnum.Gender.Male
|
||||
}
|
||||
};
|
||||
|
||||
await IntegrationDb.Database.DropCollectionAsync("patient");
|
||||
await IntegrationDb.Database.CreateCollectionAsync("patient");
|
||||
await IntegrationDb.Database.DropCollectionAsync("pointOfCares");
|
||||
await IntegrationDb.Database.CreateCollectionAsync("pointOfCares");
|
||||
await IntegrationDb.Database.DropCollectionAsync("units");
|
||||
await IntegrationDb.Database.CreateCollectionAsync("units");
|
||||
await IntegrationDb.Database.DropCollectionAsync("list_origin");
|
||||
await IntegrationDb.Database.CreateCollectionAsync("list_origin");
|
||||
|
||||
_repository = new PatientRepository(_optionsApiSettings, IntegrationDb.Database);
|
||||
_repositoryPoc = new PointOfCareRepository(_optionsApiSettings, IntegrationDb.Database);
|
||||
_repositoryUnit = new UnitRepository(_optionsApiSettings, IntegrationDb.Database);
|
||||
_repositoryList = new MasterListRepository<OriginList>(_optionsApiSettings, IntegrationDb.Database);
|
||||
|
||||
await _repositoryPoc.InsertOneAsync(_poc1);
|
||||
await _repositoryPoc.InsertOneAsync(_poc2);
|
||||
await _repositoryPoc.InsertOneAsync(_poc3);
|
||||
await _repositoryPoc.InsertOneAsync(_pocMoved);
|
||||
await _repositoryPoc.InsertOneAsync(_pocPushed);
|
||||
await _repository.InsertOneAsync(patient1);
|
||||
await _repository.InsertOneAsync(patient2);
|
||||
await _repository.InsertOneAsync(patient3);
|
||||
await _repository.InsertOneAsync(patient4);
|
||||
await _repository.InsertOneAsync(patient5);
|
||||
await _repositoryUnit.InsertOneAsync(_unit);
|
||||
await _repositoryList.InsertOneAsync(_originList);
|
||||
|
||||
await _repository.FindByPatientNumber("patientNumber1");
|
||||
|
||||
await _repository.InsertManyAsync([patient1, patient2, patient3, patient4, patient5]);
|
||||
Assert.That(await _repository.Collection.CountDocumentsAsync(_ => true), Is.EqualTo(5));
|
||||
}
|
||||
|
||||
private PatientRepository _repository;
|
||||
private PointOfCareRepository _repositoryPoc;
|
||||
private UnitRepository _repositoryUnit;
|
||||
private MasterListRepository<OriginList> _repositoryList;
|
||||
|
||||
private readonly ApiSettings _apiSettings = new()
|
||||
{
|
||||
ArchivePatientsDiagnosis = "patients"
|
||||
};
|
||||
|
||||
private IOptions<ApiSettings> _optionsApiSettings;
|
||||
|
||||
private static readonly DateTime Now = DateTime.Now;
|
||||
private static readonly ObjectId PatientId = ObjectId.GenerateNewId();
|
||||
private static readonly ObjectId OriginListId = ObjectId.GenerateNewId();
|
||||
private static readonly ObjectId OriginListOptionId = ObjectId.GenerateNewId();
|
||||
|
||||
private readonly ObjectId _unit1 = new("662760a96bdc7150b49fe29c");
|
||||
|
||||
private readonly Unit _unit = new()
|
||||
{
|
||||
Id = ObjectId.Parse("662760a96bdc7150b49fe29c"),
|
||||
Name = "unit1",
|
||||
OriginListId = OriginListId
|
||||
};
|
||||
|
||||
private readonly OriginList _originList = new()
|
||||
{
|
||||
Id = OriginListId,
|
||||
Name = "OriginList",
|
||||
Options = new List<OptionList>
|
||||
{
|
||||
new()
|
||||
{
|
||||
Id = OriginListOptionId,
|
||||
Name = "Urlogy",
|
||||
LocaleItems = new Locale
|
||||
{
|
||||
Es = new LocaleItem { Name = "Urologia" }
|
||||
}
|
||||
}
|
||||
},
|
||||
CanAddElement = true,
|
||||
ListType = MasterListType.OriginList,
|
||||
DefaultLocale = LocaleEnum.Eng
|
||||
};
|
||||
|
||||
private readonly PointOfCare _poc1 = new()
|
||||
{
|
||||
Bed = "Bed1",
|
||||
Id = ObjectId.GenerateNewId(),
|
||||
Room = "Room1",
|
||||
UnitId = ObjectId.GenerateNewId()
|
||||
};
|
||||
|
||||
private readonly PointOfCare _poc2 = new()
|
||||
{
|
||||
Bed = "Bed1",
|
||||
Id = ObjectId.GenerateNewId(),
|
||||
Room = "Room1",
|
||||
UnitId = ObjectId.GenerateNewId()
|
||||
};
|
||||
|
||||
private readonly PointOfCare _poc3 = new()
|
||||
{
|
||||
Bed = "Bed1",
|
||||
Id = ObjectId.GenerateNewId(),
|
||||
Room = "Room1",
|
||||
UnitId = ObjectId.GenerateNewId()
|
||||
};
|
||||
|
||||
private readonly PointOfCare _pocMoved = new()
|
||||
{
|
||||
Bed = VirtualPointOfCare.Moved.ToString(),
|
||||
Id = ObjectId.GenerateNewId(),
|
||||
Room = VirtualPointOfCare.Moved.ToString(),
|
||||
UnitId = ObjectId.GenerateNewId()
|
||||
};
|
||||
|
||||
private readonly PointOfCare _pocPushed = new()
|
||||
{
|
||||
Bed = VirtualPointOfCare.Pushed.ToString(),
|
||||
Id = ObjectId.GenerateNewId(),
|
||||
Room = VirtualPointOfCare.Pushed.ToString(),
|
||||
UnitId = ObjectId.GenerateNewId()
|
||||
};
|
||||
|
||||
[Test]
|
||||
public async Task FindInActivePoC()
|
||||
{
|
||||
var result = await _repository.FindInActivePoC();
|
||||
Assert.That(result, Is.Not.Null);
|
||||
Assert.That(result.Count, Is.EqualTo(3));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task FindInInactivePoC()
|
||||
{
|
||||
var result = await _repository.FindInInactivePoC();
|
||||
Assert.That(result, Is.Not.Null);
|
||||
Assert.That(result.Count, Is.EqualTo(2));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task FindById_Find_Return_Patient()
|
||||
{
|
||||
var result = await _repository.FindById(PatientId);
|
||||
|
||||
Assert.That(result, Is.Not.Null);
|
||||
Assert.That(result.PatientNumber, Is.EqualTo("patientNumber1"));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task FindByPatientNumber_Not_Find_Return_Patient()
|
||||
{
|
||||
var result = await _repository.FindByPatientNumber("");
|
||||
|
||||
Assert.That(result, Is.Null);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task FindByPatientNumber_Find_Return_Patient()
|
||||
{
|
||||
var result = await _repository.FindByPatientNumber("patientNumber1");
|
||||
|
||||
Assert.That(result, Is.Not.Null);
|
||||
Assert.That(result.PatientNumber, Is.EqualTo("patientNumber1"));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task FindAll_Return_List_Patients()
|
||||
{
|
||||
var result = await _repository.FindAll();
|
||||
|
||||
Assert.That(result, Is.Not.Null);
|
||||
Assert.That(result, Has.Count.EqualTo(5));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Update_with_base_UpdateOneAsync()
|
||||
{
|
||||
var patient = new Patient
|
||||
{
|
||||
Id = ObjectId.GenerateNewId(),
|
||||
UnitString = "PointOfCare",
|
||||
Bed = "bed",
|
||||
PatientNumber = "patientNumber",
|
||||
Person = new Person
|
||||
{
|
||||
FirstName = "firstName",
|
||||
LastName = "lastName",
|
||||
BirthDate = new DateTime(),
|
||||
Gender = PatientEnum.Gender.Male
|
||||
}
|
||||
};
|
||||
|
||||
var patientUpdate = new Patient
|
||||
{
|
||||
Id = patient.Id,
|
||||
UnitString = "PointOfCare",
|
||||
Bed = "bed",
|
||||
PatientNumber = "patientNumberUpdate",
|
||||
Person = new Person
|
||||
{
|
||||
FirstName = "firstName",
|
||||
LastName = "lastName",
|
||||
BirthDate = new DateTime(),
|
||||
Gender = PatientEnum.Gender.Male
|
||||
}
|
||||
};
|
||||
|
||||
await _repository.InsertOneAsync(patient);
|
||||
|
||||
var result = await _repository.FindByPatientNumber("patientNumberUpdate");
|
||||
|
||||
Assert.That(result, Is.Null);
|
||||
|
||||
await _repository.Update(patientUpdate);
|
||||
|
||||
var resultUpdate = await _repository.FindByPatientNumber("patientNumberUpdate");
|
||||
|
||||
Assert.That(resultUpdate, Is.Not.Null);
|
||||
Assert.That(resultUpdate.PatientNumber, Is.EqualTo("patientNumberUpdate"));
|
||||
|
||||
await _repository.Delete(patient.Id);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Delete()
|
||||
{
|
||||
var patientDelete = new Patient
|
||||
{
|
||||
Id = ObjectId.GenerateNewId(),
|
||||
UnitString = "PointOfCare",
|
||||
Bed = "bedDelete",
|
||||
PatientNumber = "patientNumberDelete",
|
||||
Person = new Person
|
||||
{
|
||||
FirstName = "firstName",
|
||||
LastName = "lastName",
|
||||
BirthDate = new DateTime(),
|
||||
Gender = PatientEnum.Gender.Male
|
||||
}
|
||||
};
|
||||
|
||||
await _repository.InsertOneAsync(patientDelete);
|
||||
|
||||
var result = await _repository.FindByPatientNumber("patientNumberDelete");
|
||||
|
||||
Assert.That(result, Is.Not.Null);
|
||||
|
||||
await _repository.Delete(patientDelete.Id);
|
||||
|
||||
var resultDelete = await _repository.FindByPatientNumber("patientNumberDelete");
|
||||
|
||||
Assert.That(resultDelete, Is.Null);
|
||||
}
|
||||
|
||||
|
||||
// [Test]
|
||||
// public async Task UpdateLocation()
|
||||
// {
|
||||
// var patient = new Patient()
|
||||
// {
|
||||
// Id = ObjectId.GenerateNewId(),
|
||||
// UnitId = _unit1,
|
||||
// PointOfCareId = ObjectId.GenerateNewId(),
|
||||
// PatientNumber = "patientNumberLocation",
|
||||
// Person = new Person
|
||||
// {
|
||||
// FirstName = "firstName",
|
||||
// LastName = "lastName",
|
||||
// BirthDate = new DateTime(),
|
||||
// Gender = Gender.Male,
|
||||
// }
|
||||
// };
|
||||
//
|
||||
// var location = new PatientLocation("PointOfCare", "BedLocationUpdate");
|
||||
//
|
||||
// await _repository.InsertOneAsync(patient);
|
||||
//
|
||||
// var result = await _repository.FindByPointOfCare(_poc1.Id);
|
||||
//
|
||||
// // Assert.That(result, Is.Null);
|
||||
//
|
||||
// await _repository.UpdateLocation(result.FirstOrDefault().Id,location);
|
||||
//
|
||||
// var resultUpdate = await _repository.FindByPointOfCare(_poc1.Id);
|
||||
//
|
||||
// Assert.That(resultUpdate, Is.Not.Null);
|
||||
// Assert.That(resultUpdate.FirstOrDefault().Bed, Is.EqualTo("BedLocationUpdate"));
|
||||
//
|
||||
// await _repository.Delete(patient.Id);
|
||||
// }
|
||||
|
||||
[Test]
|
||||
public async Task UpdateAttendingDoctor()
|
||||
{
|
||||
var doctor = new Person
|
||||
{
|
||||
FirstName = "firstNameDoctor2",
|
||||
LastName = "lastNameDoctor2",
|
||||
Gender = PatientEnum.Gender.Female
|
||||
};
|
||||
|
||||
var result = await _repository.FindByPointOfCare(_poc1.Id);
|
||||
|
||||
await _repository.UpdateAttendingDoctor(result.First().Id, doctor);
|
||||
|
||||
var resultUpdate = await _repository.FindByPointOfCare(_poc1.Id);
|
||||
|
||||
Assert.That(resultUpdate, Is.Not.Null);
|
||||
Assert.That(resultUpdate.First().AttendingDoctor?.FirstName, Is.EqualTo("firstNameDoctor2"));
|
||||
}
|
||||
|
||||
// [Test]
|
||||
// public async Task UpdatePatientData_UpdatePatientNumber_true()
|
||||
// {
|
||||
// var patient1 = new Person
|
||||
// {
|
||||
// FirstName = "firstNamePatient1",
|
||||
// LastName = "lastNamePatient1",
|
||||
// BirthDate = new DateTime(),
|
||||
// Gender = Gender.Male
|
||||
// };
|
||||
//
|
||||
// var patient2 = new Person
|
||||
// {
|
||||
// FirstName = "firstNamePatient2",
|
||||
// LastName = "lastNamePatient2",
|
||||
// BirthDate = new DateTime(),
|
||||
// Gender = Gender.Female
|
||||
// };
|
||||
//
|
||||
// var patient = new Patient()
|
||||
// {
|
||||
// Id = ObjectId.GenerateNewId(),
|
||||
// UnitId = _unit1,
|
||||
// PointOfCareId = ObjectId.GenerateNewId(),
|
||||
// PatientNumber = "patientNumber",
|
||||
// Person = patient1
|
||||
// };
|
||||
//
|
||||
// var location = new PatientLocation("PointOfCare", "bed");
|
||||
//
|
||||
// await _repository.InsertOneAsync(patient);
|
||||
//
|
||||
// var result = await _repository.FindByLocation(location);
|
||||
//
|
||||
// Assert.That(result, Is.Not.Null);
|
||||
// Assert.That(result.Person?.FirstName, Is.EqualTo("firstNamePatient1"));
|
||||
//
|
||||
// await _repository.UpdatePatientData(patient.Id, "patientNumberUpdate", patient2, true);
|
||||
//
|
||||
// var resultUpdate = await _repository.FindByLocation(location);
|
||||
//
|
||||
// Assert.That(resultUpdate, Is.Not.Null);
|
||||
// Assert.That(resultUpdate.Person?.FirstName, Is.EqualTo("firstNamePatient2"));
|
||||
//
|
||||
// await _repository.Delete(patient.Id);
|
||||
// }
|
||||
|
||||
// [Test]
|
||||
// public async Task UpdatePatientData_UpdatePatientNumber_false()
|
||||
// {
|
||||
// var patient1 = new Person
|
||||
// {
|
||||
// FirstName = "firstNamePatient1",
|
||||
// LastName = "lastNamePatient1",
|
||||
// BirthDate = new DateTime(),
|
||||
// Gender = Gender.Male
|
||||
// };
|
||||
//
|
||||
// var patient2 = new Person
|
||||
// {
|
||||
// FirstName = "firstNamePatient2",
|
||||
// LastName = "lastNamePatient2",
|
||||
// BirthDate = new DateTime(),
|
||||
// Gender = Gender.Female
|
||||
// };
|
||||
//
|
||||
// var patient = new Patient()
|
||||
// {
|
||||
// Id = ObjectId.GenerateNewId(),
|
||||
// UnitId = _unit1,
|
||||
// PointOfCareId = ObjectId.GenerateNewId(),
|
||||
// PatientNumber = "patientNumber",
|
||||
// Person = patient1
|
||||
// };
|
||||
//
|
||||
// var location = new PatientLocation("PointOfCare", "bed");
|
||||
//
|
||||
// await _repository.InsertOneAsync(patient);
|
||||
//
|
||||
// var result = await _repository.FindByLocation(location);
|
||||
//
|
||||
// Assert.That(result, Is.Not.Null);
|
||||
// Assert.Multiple(() =>
|
||||
// {
|
||||
// Assert.That(result.Person?.FirstName, Is.EqualTo("firstNamePatient1"));
|
||||
// Assert.That(result.PatientNumber, Is.EqualTo("patientNumber"));
|
||||
// });
|
||||
// await _repository.UpdatePatientData(patient.Id, "patientNumberUpdate", patient2, false);
|
||||
//
|
||||
// var resultUpdate = await _repository.FindByLocation(location);
|
||||
//
|
||||
// Assert.That(resultUpdate, Is.Not.Null);
|
||||
// Assert.Multiple(() =>
|
||||
// {
|
||||
// Assert.That(resultUpdate.Person?.FirstName, Is.EqualTo("firstNamePatient2"));
|
||||
// Assert.That(result.PatientNumber, Is.EqualTo("patientNumber"));
|
||||
// });
|
||||
// await _repository.Delete(patient.Id);
|
||||
// }
|
||||
|
||||
// [Test]
|
||||
// public async Task UpdateOne()
|
||||
// {
|
||||
// var person1 = new Person
|
||||
// {
|
||||
// FirstName = "firstNamePatient1",
|
||||
// LastName = "lastNamePatient1",
|
||||
// BirthDate = new DateTime(),
|
||||
// Gender = Gender.Male
|
||||
// };
|
||||
//
|
||||
// var person2 = new Person
|
||||
// {
|
||||
// FirstName = "firstNamePatient2",
|
||||
// LastName = "lastNamePatient2",
|
||||
// BirthDate = new DateTime(),
|
||||
// Gender = Gender.Female
|
||||
// };
|
||||
//
|
||||
// var patient1 = new Patient()
|
||||
// {
|
||||
// Id = ObjectId.GenerateNewId(),
|
||||
// UnitId = _unit1,
|
||||
// PointOfCareId = ObjectId.GenerateNewId(),
|
||||
// PatientNumber = "patientNumber",
|
||||
// Person = person1
|
||||
// };
|
||||
//
|
||||
// var patient2 = new Patient()
|
||||
// {
|
||||
// Id = patient1.Id,
|
||||
// UnitId = _unit1,
|
||||
// PointOfCareId = ObjectId.GenerateNewId(),
|
||||
// PatientNumber = "patientNumberUpdate",
|
||||
// Person = person2
|
||||
// };
|
||||
//
|
||||
// var location1 = new PatientLocation("PointOfCare", "bed");
|
||||
// var location2 = new PatientLocation("PointOfCare2", "bed2");
|
||||
//
|
||||
// await _repository.InsertOneAsync(patient1);
|
||||
//
|
||||
// var result = await _repository.FindByLocation(location1);
|
||||
//
|
||||
// Assert.That(result, Is.Not.Null);
|
||||
// Assert.Multiple(() =>
|
||||
// {
|
||||
// Assert.That(result.Person?.FirstName, Is.EqualTo("firstNamePatient1"));
|
||||
// Assert.That(result.PatientNumber, Is.EqualTo("patientNumber"));
|
||||
// });
|
||||
// await _repository.UpdateOne(patient2);
|
||||
//
|
||||
// var resultLocation1 = await _repository.FindByLocation(location1);
|
||||
//
|
||||
// Assert.That(resultLocation1, Is.Null);
|
||||
//
|
||||
// var resultLocation2 = await _repository.FindByLocation(location2);
|
||||
//
|
||||
// Assert.That(resultLocation2, Is.Not.Null);
|
||||
// Assert.Multiple(() =>
|
||||
// {
|
||||
// Assert.That(resultLocation2.Person?.FirstName, Is.EqualTo("firstNamePatient2"));
|
||||
// Assert.That(resultLocation2.PatientNumber, Is.EqualTo("patientNumberUpdate"));
|
||||
// });
|
||||
// await _repository.Delete(patient1.Id);
|
||||
// }
|
||||
|
||||
[Test]
|
||||
public async Task FindByPatientId_Not_Find_Return_Patient()
|
||||
{
|
||||
var result = await _repository.FindByPatientId("");
|
||||
|
||||
Assert.That(result, Is.Null);
|
||||
}
|
||||
|
||||
// [Test]
|
||||
// public async Task FindByPatientId_Find_Return_Patient()
|
||||
// {
|
||||
// var result = await _repository.FindByPatientId(_poc1.Patient.Id);
|
||||
//
|
||||
// Assert.That(result, Is.Not.Null);
|
||||
// Assert.That(result.PatientNumber, Is.EqualTo("patientNumber1"));
|
||||
// }
|
||||
|
||||
[Test]
|
||||
public async Task FindByPointOfCare_Find_Return_Patients()
|
||||
{
|
||||
var result = await _repository.FindByPointOfCare(_poc1.Id);
|
||||
|
||||
Assert.That(result, Is.Not.Null);
|
||||
Assert.That(result, Is.Not.Empty);
|
||||
// Assert.That(result.Count.Equals(1));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task FindByPointOfCare_Not_Find_Return_Patients()
|
||||
{
|
||||
var result = await _repository.FindByPointOfCare(ObjectId.GenerateNewId());
|
||||
|
||||
Assert.That(result, Is.Not.Null);
|
||||
Assert.That(result, Is.Empty);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task FindDischargedPatients_Find_Return_Patients()
|
||||
{
|
||||
var result = await _repository.FindDischargedPatients();
|
||||
|
||||
Assert.That(result, Is.Not.Null);
|
||||
Assert.That(result, Is.Not.Empty);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
using adas_core.Domain.Models.AppSettings;
|
||||
using adas_core.Domain.Models.MongoModels;
|
||||
using adas_core.Infrastructure.Repositories;
|
||||
using Microsoft.Extensions.Options;
|
||||
using Options = Microsoft.Extensions.Options.Options;
|
||||
|
||||
namespace adas_core.Test.Repositories;
|
||||
|
||||
[TestFixture]
|
||||
[Category("Integration")]
|
||||
public class PoCMappingRepositoryTest
|
||||
{
|
||||
[OneTimeSetUp]
|
||||
public async Task Init()
|
||||
{
|
||||
_optionsApiSettings = Options.Create(_apiSettings);
|
||||
|
||||
var mappingItem1 = new PoCMappingItem
|
||||
{
|
||||
OriginalPoC = "original1",
|
||||
NewPoC = "new1",
|
||||
Beds =
|
||||
[
|
||||
new List<string> { "bed1", "bed 1" },
|
||||
new List<string> { "bed2", "bed 2" },
|
||||
new List<string> { "bed3", "bed 3" },
|
||||
new List<string> { "bed4", "bed 4" },
|
||||
new List<string> { "bed5", "bed 5" },
|
||||
new List<string> { "bed6", "bed 6" },
|
||||
new List<string> { "bed7", "bed 7" },
|
||||
new List<string> { "bed8", "bed 8" },
|
||||
new List<string> { "bed9", "bed 9" },
|
||||
new List<string> { "bed10", "bed 10" },
|
||||
new List<string> { "bed11", "bed 11" }
|
||||
]
|
||||
};
|
||||
|
||||
var pointOfCares = new List<PoCMappingItem> { mappingItem1 };
|
||||
|
||||
var mapping = new PoCMapping
|
||||
{
|
||||
Id = "PV1",
|
||||
PointOfCares = pointOfCares
|
||||
};
|
||||
|
||||
await IntegrationDb.Database.DropCollectionAsync("mappings");
|
||||
await IntegrationDb.Database.CreateCollectionAsync("mappings");
|
||||
|
||||
_repository = new PoCMappingRepository(_optionsApiSettings, IntegrationDb.Database);
|
||||
|
||||
await _repository.InsertOneAsync(mapping);
|
||||
}
|
||||
|
||||
private PoCMappingRepository _repository;
|
||||
|
||||
private readonly ApiSettings _apiSettings = new();
|
||||
|
||||
private IOptions<ApiSettings> _optionsApiSettings;
|
||||
|
||||
|
||||
[Test]
|
||||
public async Task FindByKey_Find_Return_Mapping()
|
||||
{
|
||||
var result = await _repository.FindByKey("PV1");
|
||||
|
||||
Assert.That(result, Is.Not.Null);
|
||||
Assert.That(result!.PointOfCares, Has.Count.GreaterThan(0));
|
||||
Assert.That(result.PointOfCares[0].Beds, Has.Count.GreaterThan(0));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task FindByKey_Not_Find_Return_Mapping()
|
||||
{
|
||||
var result = await _repository.FindByKey("PV2");
|
||||
|
||||
Assert.That(result, Is.Null);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,117 @@
|
||||
using adas_core.Domain.Models;
|
||||
using adas_core.Domain.Models.AppSettings;
|
||||
using adas_core.Domain.Models.MongoModels;
|
||||
using adas_core.Infrastructure.Repositories;
|
||||
using Microsoft.Extensions.Options;
|
||||
using MongoDB.Bson;
|
||||
using Options = Microsoft.Extensions.Options.Options;
|
||||
|
||||
namespace adas_core.Test.Repositories;
|
||||
|
||||
[TestFixture]
|
||||
public class PoCSettingsRepositoryTest
|
||||
{
|
||||
[OneTimeSetUp]
|
||||
public async Task Init()
|
||||
{
|
||||
_optionsApiSettings = Options.Create(_apiSettings);
|
||||
|
||||
await IntegrationDb.Database.DropCollectionAsync(_apiSettings.PoCSettings);
|
||||
await IntegrationDb.Database.CreateCollectionAsync(_apiSettings.PoCSettings);
|
||||
|
||||
_repository = new PoCSettingsRepository(_optionsApiSettings, IntegrationDb.Database);
|
||||
|
||||
await _repository.InsertOneAsync(TestPoCSettings);
|
||||
}
|
||||
|
||||
private PoCSettingsRepository _repository;
|
||||
|
||||
private readonly ApiSettings _apiSettings = new()
|
||||
{
|
||||
PoCSettings = "poc_settings"
|
||||
};
|
||||
|
||||
private IOptions<ApiSettings> _optionsApiSettings;
|
||||
|
||||
private static readonly ObjectId TestObjectId = ObjectId.GenerateNewId();
|
||||
private static readonly PatientLocation TestLocation = new("POC1", "Bed1");
|
||||
|
||||
private static readonly PoCSettings TestPoCSettings = new()
|
||||
{
|
||||
Id = TestObjectId,
|
||||
PatientLocation = TestLocation
|
||||
};
|
||||
|
||||
[Test]
|
||||
public async Task Delete_WhenCalled_ShouldRemovePoCSettings()
|
||||
{
|
||||
// Arrange
|
||||
var newObjectId = ObjectId.GenerateNewId();
|
||||
var newPoCSettings = new PoCSettings
|
||||
{
|
||||
Id = newObjectId,
|
||||
PatientLocation = new PatientLocation("poc2", "bed2")
|
||||
};
|
||||
await _repository.InsertOneAsync(newPoCSettings);
|
||||
|
||||
// Act
|
||||
await _repository.Delete(newObjectId);
|
||||
|
||||
// Assert
|
||||
var result = await _repository.FindById(newObjectId);
|
||||
Assert.That(result, Is.Null);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task FindAll_WhenCalled_ShouldReturnAllPoCSettings()
|
||||
{
|
||||
// Act
|
||||
var result = await _repository.FindAll();
|
||||
|
||||
// Assert
|
||||
Assert.That(result, Is.Not.Null);
|
||||
Assert.That(result, Is.Not.Empty);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task FindById_WhenCalled_ShouldReturnPoCSettings()
|
||||
{
|
||||
// Act
|
||||
var result = await _repository.FindById(TestObjectId);
|
||||
|
||||
// Assert
|
||||
Assert.That(result, Is.Not.Null);
|
||||
Assert.That(result?.Id, Is.EqualTo(TestObjectId));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task FindByLocation_WhenCalled_ShouldReturnPoCSettings()
|
||||
{
|
||||
// Act
|
||||
var result = await _repository.FindByLocation(TestLocation);
|
||||
|
||||
// Assert
|
||||
Assert.That(result, Is.Not.Null);
|
||||
Assert.That(result?.PatientLocation, Is.EqualTo(TestLocation));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Update_WhenCalled_ShouldUpdatePoCSettings()
|
||||
{
|
||||
// Arrange
|
||||
var updatedPoCSettings = new PoCSettings
|
||||
{
|
||||
Id = TestObjectId,
|
||||
PatientLocation = new PatientLocation("POC3", "Bed3")
|
||||
};
|
||||
|
||||
// Act
|
||||
await _repository.Update(updatedPoCSettings);
|
||||
|
||||
// Assert
|
||||
var result = await _repository.FindById(TestObjectId);
|
||||
Assert.That(result, Is.Not.Null);
|
||||
//Assert.That(result?.PatientLocation?.PointOfCare, Is.EqualTo("POC3"));
|
||||
Assert.That(result?.PatientLocation?.Bed, Is.EqualTo("Bed3"));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,181 @@
|
||||
using adas_core.Domain.Models.AppSettings;
|
||||
using adas_core.Domain.Models.MongoModels;
|
||||
using adas_core.Infrastructure.Repositories;
|
||||
using adas_core.Test.Utilities;
|
||||
using Microsoft.Extensions.Options;
|
||||
using MongoDB.Bson;
|
||||
using MongoDB.Driver;
|
||||
using Moq;
|
||||
using Options = Microsoft.Extensions.Options.Options;
|
||||
|
||||
namespace adas_core.Test.Repositories;
|
||||
|
||||
[TestFixture]
|
||||
[Category("Integration")]
|
||||
public class PointOfCareRepositoryTests
|
||||
{
|
||||
[OneTimeSetUp]
|
||||
public async Task Init()
|
||||
{
|
||||
_optionsApiSettings = Options.Create(new ApiSettings());
|
||||
_mockCollection = new Mock<IMongoCollection<PointOfCare>>();
|
||||
_mockDatabase = new Mock<IMongoDatabase>();
|
||||
_mockDatabase.Setup(db => db.GetCollection<PointOfCare>(It.IsAny<string>(), null))
|
||||
.Returns(_mockCollection.Object);
|
||||
|
||||
await IntegrationDb.Database.DropCollectionAsync("pointOfCares");
|
||||
await IntegrationDb.Database.CreateCollectionAsync("pointOfCares");
|
||||
_repository = new PointOfCareRepository(_optionsApiSettings, IntegrationDb.Database);
|
||||
}
|
||||
|
||||
private PointOfCareRepository _repository;
|
||||
private Mock<IMongoCollection<PointOfCare>> _mockCollection;
|
||||
private Mock<IMongoDatabase> _mockDatabase;
|
||||
private IOptions<ApiSettings> _optionsApiSettings;
|
||||
|
||||
[Test]
|
||||
public async Task InsertOneAsync_ValidPointOfCare_InsertsSuccessfully()
|
||||
{
|
||||
// Arrange
|
||||
var pointOfCare = TestUtilities.CreateValidPointOfCare();
|
||||
|
||||
// Act
|
||||
await _repository.InsertOneAsync(pointOfCare);
|
||||
|
||||
// Assert
|
||||
var insertedPointOfCare = await _repository.FindById(pointOfCare.Id);
|
||||
Assert.That(insertedPointOfCare, Is.Not.Null, "Inserted point of care should not be null");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Delete_ValidId_DeletesSuccessfully()
|
||||
{
|
||||
var pointOfCare = TestUtilities.CreateValidPointOfCare();
|
||||
await _repository.InsertOneAsync(pointOfCare);
|
||||
|
||||
await _repository.Delete(pointOfCare.Id);
|
||||
|
||||
// Assert
|
||||
var deletedPointOfCare = await _repository.FindById(pointOfCare.Id);
|
||||
Assert.That(deletedPointOfCare, Is.Null, "Deleted point of care should be null");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Update_ValidPointOfCare_UpdatesSuccessfully()
|
||||
{
|
||||
// Arrange
|
||||
var originalPointOfCare = TestUtilities.CreateValidPointOfCare();
|
||||
await _repository.InsertOneAsync(originalPointOfCare); // Insert the original point of care
|
||||
|
||||
// Modify some properties to update
|
||||
originalPointOfCare.Room = "Updated Room";
|
||||
originalPointOfCare.Bed = "Updated Bed";
|
||||
|
||||
// Act
|
||||
await _repository.Update(originalPointOfCare); // Update the point of care
|
||||
|
||||
// Retrieve the updated point of care
|
||||
var updatedPointOfCare = await _repository.FindById(originalPointOfCare.Id);
|
||||
|
||||
// Assert
|
||||
Assert.That(updatedPointOfCare, Is.Not.Null, "Updated point of care should not be null");
|
||||
Assert.That(updatedPointOfCare?.Room, Is.EqualTo(originalPointOfCare.Room), "Room should be updated");
|
||||
Assert.That(updatedPointOfCare?.Bed, Is.EqualTo(originalPointOfCare.Bed), "Bed should be updated");
|
||||
}
|
||||
|
||||
|
||||
[Test]
|
||||
public async Task FindById_ExistingId_ReturnsPointOfCare()
|
||||
{
|
||||
// Arrange
|
||||
var pointOfCareId = ObjectId.GenerateNewId();
|
||||
var expectedPointOfCare = TestUtilities.CreateValidPointOfCare();
|
||||
expectedPointOfCare.Id = pointOfCareId;
|
||||
|
||||
// Insert a PointOfCare document into the database
|
||||
await _repository.InsertOneAsync(expectedPointOfCare);
|
||||
|
||||
// Act
|
||||
var result = await _repository.FindById(pointOfCareId);
|
||||
|
||||
// Assert
|
||||
Assert.That(result, Is.Not.Null, "Returned PointOfCare should not be null");
|
||||
Assert.That(result!.Id, Is.EqualTo(expectedPointOfCare.Id), "Returned PointOfCare should have the expected ID");
|
||||
// Add additional assertions to compare other properties if needed
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task FindByUnitAndStatus_ExistingUnitAndStatus_ReturnsMatchingPointOfCares()
|
||||
{
|
||||
// Arrange
|
||||
var expectedPointOfCare = TestUtilities.CreateValidPointOfCare();
|
||||
|
||||
// Insert PointOfCare documents into the database with the specified unit ID and status
|
||||
|
||||
await _repository.InsertOneAsync(expectedPointOfCare);
|
||||
|
||||
// Act
|
||||
var result = await _repository.FindByUnitAndStatus(expectedPointOfCare.UnitId, expectedPointOfCare.Status);
|
||||
|
||||
var resultList = result.ToList();
|
||||
// Assert
|
||||
Assert.That(resultList, Is.Not.Null, "Returned collection should not be null");
|
||||
Assert.That(resultList.First().Id, Is.EqualTo(expectedPointOfCare.Id),
|
||||
"Returned collection should contain expected PointOfCare object");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task FindByRoom_ValidRoom_ReturnsMatchingPointOfCares()
|
||||
{
|
||||
var expectedPointOfCare = TestUtilities.CreateValidPointOfCare();
|
||||
await _repository.InsertOneAsync(expectedPointOfCare);
|
||||
|
||||
// Act
|
||||
if (expectedPointOfCare.Unit != null)
|
||||
{
|
||||
var result = await _repository.FindByRoom(expectedPointOfCare.Room);
|
||||
|
||||
var resultList = result?.ToList();
|
||||
|
||||
// Assert
|
||||
Assert.That(resultList, Is.Not.Null, "Returned collection should not be null");
|
||||
|
||||
Assert.That(resultList?.Any(p => p.Id == expectedPointOfCare.Id), Is.True,
|
||||
"Returned collection should contain expected PointOfCare object");
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task FindByBed_ValidBed_ReturnsMatchingPointOfCares()
|
||||
{
|
||||
var expectedPointOfCare = TestUtilities.CreateValidPointOfCare();
|
||||
await _repository.InsertOneAsync(expectedPointOfCare);
|
||||
|
||||
// Act
|
||||
var result = await _repository.FindByBed(expectedPointOfCare.Bed);
|
||||
|
||||
// Convert the result to a list or an array
|
||||
var resultList = result?.ToList();
|
||||
|
||||
// Assert
|
||||
Assert.That(resultList, Is.Not.Null, "Returned collection should not be null");
|
||||
|
||||
// Check if the expected point of care is contained within the result
|
||||
Assert.That(resultList?.First().Bed, Is.EqualTo(expectedPointOfCare.Bed),
|
||||
"Returned collection should contain expected PointOfCare object");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task GetAll_ReturnsAllPointOfCares()
|
||||
{
|
||||
var poc = TestUtilities.CreateValidPointOfCare();
|
||||
await _repository.InsertOneAsync(poc);
|
||||
|
||||
var result = await _repository.GetAll();
|
||||
|
||||
var resultList = result?.ToList();
|
||||
// Assert
|
||||
Assert.That(resultList?.Any(p => p.Id == poc.Id), Is.True,
|
||||
"Returned collection should contain the expected PointOfCare object with the specified Id");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,395 @@
|
||||
using adas_core.Domain.Enums;
|
||||
using adas_core.Domain.Models.AppSettings;
|
||||
using adas_core.Domain.Models.Pumps;
|
||||
using adas_core.Infrastructure.Repositories;
|
||||
using Microsoft.Extensions.Options;
|
||||
using MongoDB.Bson;
|
||||
using MongoDB.Driver;
|
||||
|
||||
namespace adas_core.Test.Repositories;
|
||||
|
||||
[TestFixture]
|
||||
[Category("Integration")]
|
||||
public class PumpAlarmEventRepositoryTest
|
||||
{
|
||||
private PumpAlarmEventRepository _repo;
|
||||
private IOptions<ApiSettings> _apiSettings;
|
||||
|
||||
private static readonly DateTime Now = DateTime.UtcNow;
|
||||
private const string DeviceA = "Device-A";
|
||||
private const string DeviceB = "Device-B";
|
||||
private static readonly ObjectId PatientId = ObjectId.GenerateNewId();
|
||||
|
||||
// -------------------------------------------------------------------
|
||||
// INIT – Setup inicial de colección con índices y datos de prueba
|
||||
// -------------------------------------------------------------------
|
||||
[OneTimeSetUp]
|
||||
public async Task Init()
|
||||
{
|
||||
_apiSettings = Options.Create(new ApiSettings
|
||||
{
|
||||
PumpAlarmEvent = "pump_alarm_event"
|
||||
});
|
||||
|
||||
await IntegrationDb.Database.DropCollectionAsync("pump_alarm_event");
|
||||
await IntegrationDb.Database.CreateCollectionAsync("pump_alarm_event");
|
||||
|
||||
_repo = new PumpAlarmEventRepository(
|
||||
_apiSettings,
|
||||
IntegrationDb.Database
|
||||
);
|
||||
|
||||
await _repo.CreateIndexes();
|
||||
|
||||
var initialEvents = new List<PumpAlarmEvent>
|
||||
{
|
||||
new()
|
||||
{
|
||||
Id = ObjectId.GenerateNewId(),
|
||||
DeviceId = DeviceA,
|
||||
PatientId = PatientId,
|
||||
AlarmType = PumpEnum.AlarmType.Attention,
|
||||
Time = Now.AddSeconds(-1)
|
||||
},
|
||||
new()
|
||||
{
|
||||
Id = ObjectId.GenerateNewId(),
|
||||
DeviceId = DeviceA,
|
||||
PatientId = PatientId,
|
||||
AlarmType = PumpEnum.AlarmType.Occlusion,
|
||||
Time = Now.AddSeconds(-3)
|
||||
},
|
||||
new()
|
||||
{
|
||||
Id = ObjectId.GenerateNewId(),
|
||||
DeviceId = DeviceB,
|
||||
PatientId = PatientId,
|
||||
AlarmType = PumpEnum.AlarmType.Occlusion,
|
||||
Time = Now.AddSeconds(-2)
|
||||
}
|
||||
};
|
||||
|
||||
await IntegrationDb.Database
|
||||
.GetCollection<PumpAlarmEvent>("pump_alarm_event")
|
||||
.InsertManyAsync(initialEvents);
|
||||
|
||||
var count = await _repo.Collection.CountDocumentsAsync(_ => true);
|
||||
Assert.That(count, Is.EqualTo(3));
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------
|
||||
// INSERT
|
||||
// -------------------------------------------------------------------
|
||||
[Test]
|
||||
public async Task InsertAsync_Works()
|
||||
{
|
||||
var evt = new PumpAlarmEvent
|
||||
{
|
||||
Id = ObjectId.GenerateNewId(),
|
||||
DeviceId = "Device-C",
|
||||
PatientId = PatientId,
|
||||
AlarmType = PumpEnum.AlarmType.Occlusion,
|
||||
Time = Now
|
||||
};
|
||||
|
||||
await _repo.InsertAsync(evt);
|
||||
|
||||
var found = await _repo.Collection.FindAsync(x => x.Id == evt.Id);
|
||||
Assert.That(found.FirstOrDefault(), Is.Not.Null);
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------
|
||||
// FIND BY DEVICE ID
|
||||
// -------------------------------------------------------------------
|
||||
[Test]
|
||||
public async Task FindByDeviceIdAsync_ReturnsOrdered()
|
||||
{
|
||||
var result = await _repo.FindByDeviceIdAsync(DeviceA);
|
||||
|
||||
var list = result.ToList();
|
||||
|
||||
using (Assert.EnterMultipleScope())
|
||||
{
|
||||
Assert.That(list, Is.Not.Empty);
|
||||
Assert.That(list.First().Time, Is.GreaterThanOrEqualTo(list.Last().Time));
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task FindByDeviceIdAsync_WithDateRange_Works()
|
||||
{
|
||||
var from = Now.AddSeconds(-2);
|
||||
var to = Now;
|
||||
|
||||
var result = await _repo.FindByDeviceIdAsync(DeviceA, from, to);
|
||||
|
||||
using (Assert.EnterMultipleScope())
|
||||
{
|
||||
var pumpAlarmEvents = result.ToList();
|
||||
Assert.That(pumpAlarmEvents, Is.Not.Empty);
|
||||
Assert.That(pumpAlarmEvents.All(x => x.Time >= from && x.Time <= to), Is.True);
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task FindByDeviceIdAsync_WithLimit_Works()
|
||||
{
|
||||
var result = await _repo.FindByDeviceIdAsync(DeviceA, limit: 1);
|
||||
|
||||
var list = result.ToList();
|
||||
Assert.That(list, Has.Count.EqualTo(1));
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------
|
||||
// FIND LAST BY DEVICE
|
||||
// -------------------------------------------------------------------
|
||||
[Test]
|
||||
public async Task FindLastByDeviceIdAsync_Works()
|
||||
{
|
||||
// Arrange: dataset ya insertado en Init()
|
||||
|
||||
var events = await _repo.FindByDeviceIdAsync("Device-A");
|
||||
var expected = events.OrderByDescending(e => e.Time).First();
|
||||
|
||||
// Act
|
||||
var evt = await _repo.FindLastByDeviceIdAsync("Device-A");
|
||||
|
||||
// Assert
|
||||
Assert.That(evt, Is.Not.Null);
|
||||
Assert.That(evt!.Id, Is.EqualTo(expected.Id)); // comparación robusta
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------
|
||||
// DELETE BY PATIENT ID
|
||||
// -------------------------------------------------------------------
|
||||
[Test]
|
||||
public async Task DeleteByPatientId_Works()
|
||||
{
|
||||
var patient2 = ObjectId.GenerateNewId();
|
||||
|
||||
var evt = new PumpAlarmEvent
|
||||
{
|
||||
Id = ObjectId.GenerateNewId(),
|
||||
DeviceId = "Device-X",
|
||||
PatientId = patient2,
|
||||
AlarmType = PumpEnum.AlarmType.Occlusion,
|
||||
Time = Now
|
||||
};
|
||||
|
||||
await _repo.InsertAsync(evt);
|
||||
|
||||
await _repo.DeleteByPatientId(patient2);
|
||||
|
||||
var found = await _repo.Collection.FindAsync(x => x.PatientId == patient2);
|
||||
Assert.That(await found.AnyAsync(), Is.False);
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------
|
||||
// UPDATE MANY (CAMBIO DE PATIENTID O SIMILAR)
|
||||
// -------------------------------------------------------------------
|
||||
|
||||
[Test]
|
||||
public async Task UpdateManyObjectIdByFiledNameAsync_Works()
|
||||
{
|
||||
var oldId = ObjectId.GenerateNewId();
|
||||
var newId = ObjectId.GenerateNewId();
|
||||
|
||||
var evt = new PumpAlarmEvent
|
||||
{
|
||||
Id = ObjectId.GenerateNewId(),
|
||||
DeviceId = "Device-U",
|
||||
PatientId = oldId,
|
||||
AlarmType = PumpEnum.AlarmType.Attention,
|
||||
Time = Now
|
||||
};
|
||||
|
||||
await _repo.InsertAsync(evt);
|
||||
|
||||
var updated = await _repo.UpdateManyObjectIdByFiledNameAsync("PatientId", newId, oldId);
|
||||
|
||||
Assert.That(updated, Is.EqualTo(1));
|
||||
|
||||
var found = await _repo.Collection.FindAsync(x => x.PatientId == newId);
|
||||
Assert.That(await found.AnyAsync(), Is.True);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task FindByDeviceIdAsync_ReturnsEmpty_WhenOutOfRange()
|
||||
{
|
||||
var from = Now.AddYears(-2);
|
||||
var to = Now.AddYears(-2).AddDays(1);
|
||||
|
||||
var result = await _repo.FindByDeviceIdAsync("Device-A", from, to);
|
||||
|
||||
Assert.That(result, Is.Empty);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task FindByDeviceIdAsync_RespectsLimitAndOrder()
|
||||
{
|
||||
// Semilla adicional para asegurar > 1
|
||||
var events = new[]
|
||||
{
|
||||
new PumpAlarmEvent { Id = ObjectId.GenerateNewId(), DeviceId = DeviceA, PatientId = PatientId, AlarmType = PumpEnum.AlarmType.AirInLine, Time = Now.AddMilliseconds(-10) },
|
||||
new PumpAlarmEvent { Id = ObjectId.GenerateNewId(), DeviceId = DeviceA, PatientId = PatientId, AlarmType = PumpEnum.AlarmType.Occlusion, Time = Now.AddMilliseconds(-5) }
|
||||
};
|
||||
await _repo.Collection.InsertManyAsync(events);
|
||||
|
||||
var result = await _repo.FindByDeviceIdAsync(DeviceA, limit: 1);
|
||||
var list = result.ToList();
|
||||
|
||||
using (Assert.EnterMultipleScope())
|
||||
{
|
||||
Assert.That(list, Has.Count.EqualTo(1));
|
||||
var last = await _repo.FindLastByDeviceIdAsync(DeviceA);
|
||||
Assert.That(list[0].Id, Is.EqualTo(last!.Id));
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task InsertAsync_AllowsNullOptionalFields()
|
||||
{
|
||||
var evt = new PumpAlarmEvent
|
||||
{
|
||||
Id = ObjectId.GenerateNewId(),
|
||||
DeviceId = "Device-Nulls",
|
||||
PatientId = null, // opcional
|
||||
AlarmType = null, // opcional
|
||||
Time = Now
|
||||
};
|
||||
|
||||
await _repo.InsertAsync(evt);
|
||||
|
||||
var found = await _repo.Collection.FindAsync(x => x.Id == evt.Id);
|
||||
Assert.That(found.FirstOrDefault(), Is.Not.Null);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task DeleteByPatientId_DeletesMany_AndIsIdempotent()
|
||||
{
|
||||
var pid = ObjectId.GenerateNewId();
|
||||
|
||||
var docs = Enumerable.Range(0, 3).Select(i => new PumpAlarmEvent
|
||||
{
|
||||
Id = ObjectId.GenerateNewId(),
|
||||
DeviceId = "Device-Del",
|
||||
PatientId = pid,
|
||||
AlarmType = PumpEnum.AlarmType.Attention,
|
||||
Time = Now.AddSeconds(-i)
|
||||
});
|
||||
|
||||
await _repo.Collection.InsertManyAsync(docs);
|
||||
|
||||
// Primera vez: borra 3
|
||||
await _repo.DeleteByPatientId(pid);
|
||||
var left = await _repo.Collection.FindAsync(x => x.PatientId == pid);
|
||||
Assert.That(left.ToList(), Is.Empty);
|
||||
|
||||
Func<Task> act = async () => await _repo.DeleteByPatientId(pid);
|
||||
Assert.That(act, Throws.Nothing);
|
||||
}
|
||||
|
||||
|
||||
[Test]
|
||||
public async Task UpdateManyObjectIdByFiledNameAsync_UpdatesMultiple()
|
||||
{
|
||||
var oldId = ObjectId.GenerateNewId();
|
||||
var newId = ObjectId.GenerateNewId();
|
||||
|
||||
var events = Enumerable.Range(0, 4).Select(i => new PumpAlarmEvent
|
||||
{
|
||||
Id = ObjectId.GenerateNewId(),
|
||||
DeviceId = "Device-U",
|
||||
PatientId = oldId,
|
||||
AlarmType = PumpEnum.AlarmType.AirInLine,
|
||||
Time = Now.AddMilliseconds(-i)
|
||||
});
|
||||
|
||||
await _repo.Collection.InsertManyAsync(events);
|
||||
|
||||
var modified = await _repo.UpdateManyObjectIdByFiledNameAsync("PatientId", newId, oldId);
|
||||
|
||||
using (Assert.EnterMultipleScope())
|
||||
{
|
||||
Assert.That(modified, Is.EqualTo(4));
|
||||
var foundNew = await _repo.Collection.FindAsync(x => x.PatientId == newId);
|
||||
Assert.That(foundNew.ToList(), Has.Count.EqualTo(4));
|
||||
}
|
||||
}
|
||||
[Test]
|
||||
public async Task FindByDeviceIdAsync_InclusiveRange_BoundsRespected()
|
||||
{
|
||||
var start = Now.AddMinutes(-30);
|
||||
var end = Now.AddMinutes(-29);
|
||||
|
||||
var exactEvt = new PumpAlarmEvent
|
||||
{
|
||||
Id = ObjectId.GenerateNewId(),
|
||||
DeviceId = "Device-Range",
|
||||
PatientId = PatientId,
|
||||
AlarmType = PumpEnum.AlarmType.Attention,
|
||||
Time = end // exactamente igual al to
|
||||
};
|
||||
|
||||
await _repo.InsertAsync(exactEvt);
|
||||
|
||||
var result = await _repo.FindByDeviceIdAsync("Device-Range", from: start, to: end);
|
||||
|
||||
using (Assert.EnterMultipleScope())
|
||||
{
|
||||
var pumpAlarmEvents = result as PumpAlarmEvent[] ?? result.ToArray();
|
||||
Assert.That(pumpAlarmEvents, Is.Not.Empty);
|
||||
Assert.That(pumpAlarmEvents.All(r => r.Time >= start && r.Time <= end), Is.True);
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task FindLastByDeviceIdAsync_TracksNewest()
|
||||
{
|
||||
const string dev = "Device-LastCheck";
|
||||
|
||||
var e1 = new PumpAlarmEvent { Id = ObjectId.GenerateNewId(), DeviceId = dev, PatientId = PatientId, AlarmType = PumpEnum.AlarmType.AirInLine, Time = Now.AddSeconds(-10) };
|
||||
var e2 = new PumpAlarmEvent { Id = ObjectId.GenerateNewId(), DeviceId = dev, PatientId = PatientId, AlarmType = PumpEnum.AlarmType.Occlusion, Time = Now.AddSeconds(-5) };
|
||||
|
||||
await _repo.Collection.InsertManyAsync([e1, e2]);
|
||||
|
||||
var last1 = await _repo.FindLastByDeviceIdAsync(dev);
|
||||
Assert.That(last1!.Id, Is.EqualTo(e2.Id));
|
||||
|
||||
var e3 = new PumpAlarmEvent { Id = ObjectId.GenerateNewId(), DeviceId = dev, PatientId = PatientId, AlarmType = PumpEnum.AlarmType.Attention, Time = Now };
|
||||
await _repo.InsertAsync(e3);
|
||||
|
||||
var last2 = await _repo.FindLastByDeviceIdAsync(dev);
|
||||
Assert.That(last2!.Id, Is.EqualTo(e3.Id));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task FindByDeviceIdAsync_MultipleOverlappingRanges()
|
||||
{
|
||||
const string dev = "Device-Timeline";
|
||||
|
||||
var batch = new[]
|
||||
{
|
||||
new PumpAlarmEvent { Id = ObjectId.GenerateNewId(), DeviceId = dev, PatientId = PatientId, AlarmType = PumpEnum.AlarmType.AirInLine, Time = Now.AddMinutes(-20) },
|
||||
new PumpAlarmEvent { Id = ObjectId.GenerateNewId(), DeviceId = dev, PatientId = PatientId, AlarmType = PumpEnum.AlarmType.Occlusion, Time = Now.AddMinutes(-10) },
|
||||
new PumpAlarmEvent { Id = ObjectId.GenerateNewId(), DeviceId = dev, PatientId = PatientId, AlarmType = PumpEnum.AlarmType.Attention, Time = Now.AddMinutes(-5) }
|
||||
};
|
||||
|
||||
await _repo.Collection.InsertManyAsync(batch);
|
||||
|
||||
var r1 = await _repo.FindByDeviceIdAsync(dev, from: Now.AddMinutes(-30), to: Now.AddMinutes(-15));
|
||||
var r2 = await _repo.FindByDeviceIdAsync(dev, from: Now.AddMinutes(-12), to: Now.AddMinutes(-8));
|
||||
var r3 = await _repo.FindByDeviceIdAsync(dev, from: Now.AddMinutes(-6), to: Now);
|
||||
|
||||
using (Assert.EnterMultipleScope())
|
||||
{
|
||||
var r1List = r1.ToList();
|
||||
Assert.That(r1List, Has.Count.EqualTo(1)); // -20
|
||||
var r2List = r2.ToList();
|
||||
Assert.That(r2List, Has.Count.EqualTo(1)); // -10
|
||||
var r3List = r3.ToList();
|
||||
Assert.That(r3List, Has.Count.EqualTo(1)); // -5
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,323 @@
|
||||
using adas_core.Domain.Enums;
|
||||
using adas_core.Domain.Models.AppSettings;
|
||||
using adas_core.Domain.Models.Pumps;
|
||||
using adas_core.Infrastructure.Repositories;
|
||||
using Microsoft.Extensions.Options;
|
||||
using MongoDB.Bson;
|
||||
using MongoDB.Driver;
|
||||
|
||||
namespace adas_core.Test.Repositories;
|
||||
|
||||
[TestFixture]
|
||||
[Category("Integration")]
|
||||
public class PumpAlarmStateRepositoryTest
|
||||
{
|
||||
private PumpAlarmStateRepository _repo;
|
||||
private IOptions<ApiSettings> _apiSettings;
|
||||
|
||||
private static readonly DateTime Now = DateTime.UtcNow;
|
||||
private const string DeviceA = "Device-A";
|
||||
private const string DeviceB = "Device-B";
|
||||
private static readonly ObjectId PatientId = ObjectId.GenerateNewId();
|
||||
|
||||
// -------------------------------------------------------------------
|
||||
// INIT: preparar colección, índices y datos iniciales
|
||||
// -------------------------------------------------------------------
|
||||
[OneTimeSetUp]
|
||||
public async Task Init()
|
||||
{
|
||||
_apiSettings = Options.Create(new ApiSettings
|
||||
{
|
||||
PumpAlarmState = "pump_alarm_state"
|
||||
});
|
||||
|
||||
await IntegrationDb.Database.DropCollectionAsync("pump_alarm_state");
|
||||
await IntegrationDb.Database.CreateCollectionAsync("pump_alarm_state");
|
||||
|
||||
_repo = new PumpAlarmStateRepository(
|
||||
_apiSettings,
|
||||
IntegrationDb.Database
|
||||
);
|
||||
|
||||
await _repo.CreateIndexes();
|
||||
|
||||
// datos iniciales
|
||||
var initial = new List<PumpAlarmState>
|
||||
{
|
||||
new()
|
||||
{
|
||||
Id = ObjectId.GenerateNewId(),
|
||||
DeviceId = DeviceA,
|
||||
PatientId = PatientId,
|
||||
AlarmType = PumpEnum.AlarmType.Attention,
|
||||
AlarmCodeMdc = "AC001",
|
||||
LastUpdated = Now.AddSeconds(-3)
|
||||
},
|
||||
new()
|
||||
{
|
||||
Id = ObjectId.GenerateNewId(),
|
||||
DeviceId = DeviceA,
|
||||
PatientId = PatientId,
|
||||
AlarmType = PumpEnum.AlarmType.Occlusion,
|
||||
AlarmCodeMdc = "AC002",
|
||||
LastUpdated = Now.AddSeconds(-1)
|
||||
},
|
||||
new()
|
||||
{
|
||||
Id = ObjectId.GenerateNewId(),
|
||||
DeviceId = DeviceB,
|
||||
PatientId = PatientId,
|
||||
AlarmType = PumpEnum.AlarmType.Occlusion,
|
||||
AlarmCodeMdc = "AC003",
|
||||
LastUpdated = Now.AddSeconds(-2)
|
||||
}
|
||||
};
|
||||
|
||||
await _repo.Collection.InsertManyAsync(initial);
|
||||
|
||||
var count = await _repo.Collection.CountDocumentsAsync(_ => true);
|
||||
Assert.That(count, Is.EqualTo(3));
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------
|
||||
// FIND ACTIVE
|
||||
// -------------------------------------------------------------------
|
||||
[Test]
|
||||
public async Task FindActiveAsync_ReturnsCorrectAlarm()
|
||||
{
|
||||
var alarm = await _repo.FindActiveAsync(DeviceA, PumpEnum.AlarmType.Occlusion, "AC002");
|
||||
|
||||
using (Assert.EnterMultipleScope())
|
||||
{
|
||||
Assert.That(alarm, Is.Not.Null);
|
||||
Assert.That(alarm!.DeviceId, Is.EqualTo(DeviceA));
|
||||
Assert.That(alarm.AlarmType, Is.EqualTo(PumpEnum.AlarmType.Occlusion));
|
||||
Assert.That(alarm.AlarmCodeMdc, Is.EqualTo("AC002"));
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task FindActiveAsync_ReturnsNull_WhenNotFound()
|
||||
{
|
||||
var alarm = await _repo.FindActiveAsync(DeviceA, PumpEnum.AlarmType.Occlusion, "XXX");
|
||||
Assert.That(alarm, Is.Null);
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------
|
||||
// UPSERT ACTIVE
|
||||
// -------------------------------------------------------------------
|
||||
[Test]
|
||||
public async Task UpsertActiveAsync_InsertsNew()
|
||||
{
|
||||
var alarm = new PumpAlarmState
|
||||
{
|
||||
Id = ObjectId.GenerateNewId(),
|
||||
DeviceId = "Device-C",
|
||||
AlarmType = PumpEnum.AlarmType.Attention,
|
||||
AlarmCodeMdc = "NEW",
|
||||
PatientId = PatientId,
|
||||
LastUpdated = Now
|
||||
};
|
||||
|
||||
await _repo.UpsertActiveAsync(alarm);
|
||||
|
||||
var found = await _repo.FindActiveAsync("Device-C", PumpEnum.AlarmType.Attention, "NEW");
|
||||
|
||||
Assert.That(found, Is.Not.Null);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task UpsertActiveAsync_UpdatesExisting()
|
||||
{
|
||||
var updated = new PumpAlarmState
|
||||
{
|
||||
Id = ObjectId.GenerateNewId(), // será ignorado por replace
|
||||
DeviceId = DeviceA,
|
||||
AlarmType = PumpEnum.AlarmType.Occlusion,
|
||||
AlarmCodeMdc = "AC002",
|
||||
PatientId = PatientId,
|
||||
LastUpdated = Now.AddMinutes(1)
|
||||
};
|
||||
|
||||
await _repo.UpsertActiveAsync(updated);
|
||||
|
||||
var found = await _repo.FindActiveAsync(DeviceA, PumpEnum.AlarmType.Occlusion, "AC002");
|
||||
|
||||
Assert.That(found, Is.Not.Null);
|
||||
Assert.That(found!.LastUpdated, Is.EqualTo(updated.LastUpdated).Within(TimeSpan.FromMilliseconds(2)));
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------
|
||||
// REMOVE ACTIVE ALARM
|
||||
// -------------------------------------------------------------------
|
||||
[Test]
|
||||
public async Task RemoveAsync_Works()
|
||||
{
|
||||
var alarm = new PumpAlarmState
|
||||
{
|
||||
Id = ObjectId.GenerateNewId(),
|
||||
DeviceId = "Device-D",
|
||||
AlarmType = PumpEnum.AlarmType.Attention,
|
||||
AlarmCodeMdc = "D1",
|
||||
PatientId = PatientId,
|
||||
LastUpdated = Now
|
||||
};
|
||||
|
||||
await _repo.UpsertActiveAsync(alarm);
|
||||
|
||||
await _repo.RemoveAsync("Device-D", PumpEnum.AlarmType.Attention, "D1");
|
||||
|
||||
var found = await _repo.FindActiveAsync("Device-D", PumpEnum.AlarmType.Attention, "D1");
|
||||
Assert.That(found, Is.Null);
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------
|
||||
// DELETE BY PATIENT ID
|
||||
// -------------------------------------------------------------------
|
||||
[Test]
|
||||
public async Task DeleteByPatientId_Works()
|
||||
{
|
||||
var pid = ObjectId.GenerateNewId();
|
||||
|
||||
var alarm = new PumpAlarmState
|
||||
{
|
||||
Id = ObjectId.GenerateNewId(),
|
||||
DeviceId = "Device-E",
|
||||
AlarmType = PumpEnum.AlarmType.Occlusion,
|
||||
AlarmCodeMdc = "DDD",
|
||||
PatientId = pid,
|
||||
LastUpdated = Now
|
||||
};
|
||||
|
||||
await _repo.UpsertActiveAsync(alarm);
|
||||
|
||||
await _repo.DeleteByPatientId(pid);
|
||||
|
||||
var list = await _repo.Collection.FindAsync(x => x.PatientId == pid);
|
||||
Assert.That(await list.AnyAsync(), Is.False);
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------
|
||||
// FIND ALL ACTIVE BY DEVICE
|
||||
// -------------------------------------------------------------------
|
||||
[Test]
|
||||
public async Task FindAllActiveByDeviceAsync_ReturnsCorrect()
|
||||
{
|
||||
var result = await _repo.FindAllActiveByDeviceAsync(DeviceA);
|
||||
|
||||
var pumpAlarmStates = result.ToList();
|
||||
Assert.That(pumpAlarmStates, Is.Not.Empty);
|
||||
Assert.That(pumpAlarmStates.All(x => x.DeviceId == DeviceA), Is.True);
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------
|
||||
// UPDATE MANY BY FIELD
|
||||
// -------------------------------------------------------------------
|
||||
[Test]
|
||||
public async Task UpdateManyObjectIdByFieldNameAsync_Works()
|
||||
{
|
||||
var oldId = ObjectId.GenerateNewId();
|
||||
var newId = ObjectId.GenerateNewId();
|
||||
|
||||
// Insert one alarm to update
|
||||
var alarm = new PumpAlarmState
|
||||
{
|
||||
Id = ObjectId.GenerateNewId(),
|
||||
DeviceId = "Device-F",
|
||||
AlarmType = PumpEnum.AlarmType.Occlusion,
|
||||
AlarmCodeMdc = "FFF",
|
||||
PatientId = oldId,
|
||||
LastUpdated = Now
|
||||
};
|
||||
|
||||
await _repo.UpsertActiveAsync(alarm);
|
||||
|
||||
var updated = await _repo.UpdateManyObjectIdByFieldNameAsync("PatientId", newId, oldId);
|
||||
|
||||
using (Assert.EnterMultipleScope())
|
||||
{
|
||||
Assert.That(updated, Is.EqualTo(1));
|
||||
|
||||
var found = await _repo.Collection.FindAsync(x => x.PatientId == newId);
|
||||
Assert.That(await found.AnyAsync(), Is.True);
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task UpsertActiveAsync_NoDuplicateOnRepeatedCalls()
|
||||
{
|
||||
var alarm = new PumpAlarmState
|
||||
{
|
||||
Id = ObjectId.GenerateNewId(),
|
||||
DeviceId = "Device-Repeat",
|
||||
AlarmType = PumpEnum.AlarmType.Attention,
|
||||
AlarmCodeMdc = "R1",
|
||||
PatientId = PatientId,
|
||||
LastUpdated = Now
|
||||
};
|
||||
|
||||
await _repo.UpsertActiveAsync(alarm);
|
||||
await _repo.UpsertActiveAsync(alarm); // segunda llamada idéntica
|
||||
|
||||
var alarmsEnum = await _repo.FindAllActiveByDeviceAsync("Device-Repeat");
|
||||
var list = alarmsEnum.ToList();
|
||||
Assert.That(list, Has.Count.EqualTo(1));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task UpsertActiveAsync_InsertsWhenAlarmCodeIsNull()
|
||||
{
|
||||
var alarm = new PumpAlarmState
|
||||
{
|
||||
Id = ObjectId.GenerateNewId(),
|
||||
DeviceId = "Device-NullCode",
|
||||
AlarmType = PumpEnum.AlarmType.Occlusion,
|
||||
AlarmCodeMdc = null,
|
||||
PatientId = PatientId,
|
||||
LastUpdated = Now
|
||||
};
|
||||
|
||||
await _repo.UpsertActiveAsync(alarm);
|
||||
|
||||
var found = await _repo.FindActiveAsync("Device-NullCode", PumpEnum.AlarmType.Occlusion, null);
|
||||
|
||||
Assert.That(found, Is.Not.Null);
|
||||
Assert.That(found!.AlarmCodeMdc, Is.Null);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task RemoveAsync_RemovesAllMatching()
|
||||
{
|
||||
var alarms = new[]
|
||||
{
|
||||
new PumpAlarmState
|
||||
{
|
||||
Id = ObjectId.GenerateNewId(),
|
||||
DeviceId = "Device-Multi",
|
||||
AlarmType = PumpEnum.AlarmType.AirInLine,
|
||||
AlarmCodeMdc = "XX1",
|
||||
PatientId = PatientId,
|
||||
LastUpdated = Now
|
||||
},
|
||||
new PumpAlarmState
|
||||
{
|
||||
Id = ObjectId.GenerateNewId(),
|
||||
DeviceId = "Device-Multi",
|
||||
AlarmType = PumpEnum.AlarmType.AirInLine,
|
||||
AlarmCodeMdc = "XX2",
|
||||
PatientId = PatientId,
|
||||
LastUpdated = Now
|
||||
}
|
||||
};
|
||||
|
||||
foreach (var a in alarms)
|
||||
await _repo.UpsertActiveAsync(a);
|
||||
|
||||
await _repo.RemoveAsync("Device-Multi", PumpEnum.AlarmType.AirInLine);
|
||||
|
||||
var remaining = await _repo.FindAllActiveByDeviceAsync("Device-Multi");
|
||||
|
||||
Assert.That(remaining, Is.Empty);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,193 @@
|
||||
using adas_core.Domain.Enums;
|
||||
using adas_core.Domain.Models;
|
||||
using adas_core.Domain.Models.AppSettings;
|
||||
using adas_core.Domain.Models.Pumps;
|
||||
using adas_core.Infrastructure.Repositories;
|
||||
using Microsoft.Extensions.Options;
|
||||
using MongoDB.Driver;
|
||||
using Options = Microsoft.Extensions.Options.Options;
|
||||
|
||||
namespace adas_core.Test.Repositories;
|
||||
|
||||
[TestFixture]
|
||||
[Category("Integration")]
|
||||
public class PumpArchiveRepositoryTest
|
||||
{
|
||||
[OneTimeSetUp]
|
||||
public async Task Init()
|
||||
{
|
||||
_optionsApiSettings = Options.Create(_apiSettings);
|
||||
|
||||
var pump1 = new PumpObservation
|
||||
{
|
||||
Code = "Tile-6",
|
||||
Time = Now,
|
||||
Name = "g-1-pump-Tile-6",
|
||||
Number = 2,
|
||||
Total = 7,
|
||||
TotalAux = 0,
|
||||
Status = PumpEnum.Status.Infusing,
|
||||
PumpMode = PumpEnum.Mode.Infusing,
|
||||
InfusingStatus = PumpEnum.InfusingStatus.Infusing,
|
||||
Pressure = new CommonPumpTypes.PumpValue
|
||||
{
|
||||
Value = 6,
|
||||
Units = "mmHg"
|
||||
},
|
||||
DrugName = "Nutrición Parenteral",
|
||||
Concentration = new CommonPumpTypes.PumpValue
|
||||
{
|
||||
Value = 1,
|
||||
Units = "ml/ml"
|
||||
},
|
||||
DrugAmount = new CommonPumpTypes.PumpValue
|
||||
{
|
||||
Value = 1,
|
||||
Units = "ml"
|
||||
},
|
||||
DiluentVolume = new CommonPumpTypes.PumpValue
|
||||
{
|
||||
Value = 1,
|
||||
Units = "ml"
|
||||
},
|
||||
DoseRate = new CommonPumpTypes.PumpValue
|
||||
{
|
||||
Value = 27,
|
||||
Units = "ml/h"
|
||||
},
|
||||
Rate = new CommonPumpTypes.PumpValue
|
||||
{
|
||||
Value = 27,
|
||||
Units = "ml/h"
|
||||
},
|
||||
VolumeInfused = new CommonPumpTypes.PumpValue
|
||||
{
|
||||
Value = 127.52,
|
||||
Units = "ml"
|
||||
},
|
||||
VolumeRemaining = new CommonPumpTypes.PumpValue
|
||||
{
|
||||
Value = 0.00001
|
||||
},
|
||||
TimeRemaining = new CommonPumpTypes.PumpValue
|
||||
{
|
||||
Value = 0,
|
||||
Units = "min"
|
||||
},
|
||||
PatientWeight = new CommonPumpTypes.PumpValue
|
||||
{
|
||||
Value = 1,
|
||||
Units = "kg"
|
||||
},
|
||||
AlarmType = PumpEnum.AlarmType.Attention,
|
||||
GatewayNumber = 1,
|
||||
IsAux = false,
|
||||
IsInfusing = true
|
||||
};
|
||||
|
||||
var pump2 = new PumpObservation
|
||||
{
|
||||
Code = "Tile-6",
|
||||
Time = Now.AddDays(-10),
|
||||
Name = "g-1-pump-Tile-6",
|
||||
Number = 2,
|
||||
Total = 7,
|
||||
TotalAux = 0,
|
||||
Status = PumpEnum.Status.Infusing,
|
||||
PumpMode = PumpEnum.Mode.Infusing,
|
||||
InfusingStatus = PumpEnum.InfusingStatus.Infusing,
|
||||
Pressure = new CommonPumpTypes.PumpValue
|
||||
{
|
||||
Value = 6,
|
||||
Units = "mmHg"
|
||||
},
|
||||
DrugName = "Nutrición Parenteral",
|
||||
Concentration = new CommonPumpTypes.PumpValue
|
||||
{
|
||||
Value = 1,
|
||||
Units = "ml/ml"
|
||||
},
|
||||
DrugAmount = new CommonPumpTypes.PumpValue
|
||||
{
|
||||
Value = 1,
|
||||
Units = "ml"
|
||||
},
|
||||
DiluentVolume = new CommonPumpTypes.PumpValue
|
||||
{
|
||||
Value = 1,
|
||||
Units = "ml"
|
||||
},
|
||||
DoseRate = new CommonPumpTypes.PumpValue
|
||||
{
|
||||
Value = 27,
|
||||
Units = "ml/h"
|
||||
},
|
||||
Rate = new CommonPumpTypes.PumpValue
|
||||
{
|
||||
Value = 27,
|
||||
Units = "ml/h"
|
||||
},
|
||||
VolumeInfused = new CommonPumpTypes.PumpValue
|
||||
{
|
||||
Value = 127.52,
|
||||
Units = "ml"
|
||||
},
|
||||
VolumeRemaining = new CommonPumpTypes.PumpValue
|
||||
{
|
||||
Value = 0.00001
|
||||
},
|
||||
TimeRemaining = new CommonPumpTypes.PumpValue
|
||||
{
|
||||
Value = 0,
|
||||
Units = "min"
|
||||
},
|
||||
PatientWeight = new CommonPumpTypes.PumpValue
|
||||
{
|
||||
Value = 1,
|
||||
Units = "kg"
|
||||
},
|
||||
AlarmType = PumpEnum.AlarmType.Attention,
|
||||
GatewayNumber = 1,
|
||||
IsAux = false,
|
||||
IsInfusing = true
|
||||
};
|
||||
|
||||
await IntegrationDb.Database.DropCollectionAsync("archive_patients_pumpobservations");
|
||||
await IntegrationDb.Database.CreateCollectionAsync("archive_patients_pumpobservations");
|
||||
|
||||
_repository = new PumpArchiveRepository(_optionsApiSettings, IntegrationDb.Database);
|
||||
|
||||
await _repository.InsertOneAsync(pump1);
|
||||
await _repository.InsertOneAsync(pump2);
|
||||
|
||||
Assert.That(await _repository.Collection.CountDocumentsAsync(_ => true), Is.EqualTo(2));
|
||||
}
|
||||
|
||||
private PumpArchiveRepository _repository;
|
||||
|
||||
private readonly ApiSettings _apiSettings = new()
|
||||
{
|
||||
ArchivePatientsPumpobservations = "archive_patients_pumpobservations"
|
||||
};
|
||||
|
||||
private IOptions<ApiSettings> _optionsApiSettings;
|
||||
|
||||
private static readonly DateTime Now = DateTime.Now;
|
||||
|
||||
|
||||
[Test]
|
||||
public async Task DeleteBeforeDate()
|
||||
{
|
||||
var result = await _repository.Collection.FindAsync(_ => true);
|
||||
|
||||
Assert.That(result, Is.Not.Null);
|
||||
Assert.That(result.ToList(), Has.Count.GreaterThan(1));
|
||||
|
||||
await _repository.DeleteBeforeDate(Now.AddDays(-1));
|
||||
|
||||
var resultDelete = await _repository.Collection.FindAsync(_ => true);
|
||||
|
||||
Assert.That(resultDelete, Is.Not.Null);
|
||||
Assert.That(resultDelete.ToList(), Has.Count.EqualTo(1));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,416 @@
|
||||
using adas_core.Domain.Enums;
|
||||
using adas_core.Domain.Models.AppSettings;
|
||||
using adas_core.Domain.Models.Pumps;
|
||||
using adas_core.Infrastructure.Repositories;
|
||||
using Microsoft.Extensions.Options;
|
||||
using MongoDB.Bson;
|
||||
using MongoDB.Driver;
|
||||
|
||||
namespace adas_core.Test.Repositories;
|
||||
|
||||
[TestFixture]
|
||||
[Category("Integration")]
|
||||
public class PumpObservationRepositoryTest
|
||||
{
|
||||
private PumpObservationRepository _repo;
|
||||
private IOptions<ApiSettings> _apiSettings;
|
||||
|
||||
private static readonly DateTime Now = DateTime.UtcNow;
|
||||
private static readonly ObjectId PatientId = ObjectId.GenerateNewId();
|
||||
|
||||
// -------------------------------------------------------------------
|
||||
// INIT – SETUP DE LA COLECCIÓN CON DATOS DE PRUEBA
|
||||
// -------------------------------------------------------------------
|
||||
[OneTimeSetUp]
|
||||
public async Task Init()
|
||||
{
|
||||
_apiSettings = Options.Create(new ApiSettings
|
||||
{
|
||||
PumpObservations = "pump_observations"
|
||||
});
|
||||
|
||||
await IntegrationDb.Database.DropCollectionAsync("pump_observations");
|
||||
await IntegrationDb.Database.CreateCollectionAsync("pump_observations");
|
||||
|
||||
_repo = new PumpObservationRepository(
|
||||
_apiSettings,
|
||||
IntegrationDb.Database
|
||||
);
|
||||
|
||||
await _repo.CreateIndexes();
|
||||
|
||||
var samples = new List<PumpObservation>
|
||||
{
|
||||
new()
|
||||
{
|
||||
Id = ObjectId.GenerateNewId(),
|
||||
PatientId = PatientId,
|
||||
DeviceId = "Device-A",
|
||||
Name = "PumpX",
|
||||
Time = Now.AddSeconds(-1),
|
||||
MessageType = PumpEnum.PumpMessageType.Observation
|
||||
},
|
||||
new()
|
||||
{
|
||||
Id = ObjectId.GenerateNewId(),
|
||||
PatientId = PatientId,
|
||||
DeviceId = "Device-A",
|
||||
Name = "PumpY",
|
||||
Time = Now.AddSeconds(-2),
|
||||
MessageType = PumpEnum.PumpMessageType.Observation
|
||||
},
|
||||
new()
|
||||
{
|
||||
Id = ObjectId.GenerateNewId(),
|
||||
PatientId = PatientId,
|
||||
DeviceId = "Device-A",
|
||||
Name = "PumpX",
|
||||
Time = Now.AddSeconds(-3),
|
||||
MessageType = PumpEnum.PumpMessageType.Observation
|
||||
}
|
||||
};
|
||||
|
||||
await _repo.InsertManyAsync(samples);
|
||||
|
||||
var count = await _repo.Collection.CountDocumentsAsync(_ => true);
|
||||
Assert.That(count, Is.EqualTo(3));
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------
|
||||
// FIND LAST OBSERVATIONS (LÍMITE = 2)
|
||||
// -------------------------------------------------------------------
|
||||
[Test]
|
||||
public async Task FindLastObservations_ReturnsTwo()
|
||||
{
|
||||
var list = await _repo.FindLastObservations(PatientId, "PumpX");
|
||||
|
||||
Assert.That(list, Is.Not.Null);
|
||||
Assert.That(list, Has.Count.EqualTo(2));
|
||||
Assert.That(list[0].Time, Is.GreaterThanOrEqualTo(list[1].Time));
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------
|
||||
// INSERT
|
||||
// -------------------------------------------------------------------
|
||||
[Test]
|
||||
public async Task InsertAsync_Works()
|
||||
{
|
||||
var pump = new PumpObservation
|
||||
{
|
||||
Id = ObjectId.GenerateNewId(),
|
||||
PatientId = ObjectId.GenerateNewId(),
|
||||
DeviceId = "Device-B",
|
||||
Time = Now
|
||||
};
|
||||
|
||||
await _repo.InsertAsync(pump);
|
||||
|
||||
var found = await _repo.Collection.FindAsync(x => x.Id == pump.Id);
|
||||
Assert.That(found.FirstOrDefault(), Is.Not.Null);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task InsertManyAsync_Works()
|
||||
{
|
||||
var list = new List<PumpObservation>
|
||||
{
|
||||
new() { Id = ObjectId.GenerateNewId(), DeviceId="D", Time=Now },
|
||||
new() { Id = ObjectId.GenerateNewId(), DeviceId="D", Time=Now }
|
||||
};
|
||||
|
||||
await _repo.InsertManyAsync(list);
|
||||
|
||||
var count = await _repo.Collection.CountDocumentsAsync(x => list.Select(y => y.Id).Contains(x.Id));
|
||||
Assert.That(count, Is.EqualTo(2));
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------
|
||||
// FIND METHODS
|
||||
// -------------------------------------------------------------------
|
||||
[Test]
|
||||
public async Task FindByDeviceIdAsync_ReturnsOrdered()
|
||||
{
|
||||
var result = await _repo.FindByDeviceIdAsync("Device-A");
|
||||
|
||||
var pumpObservations = result.ToList();
|
||||
using (Assert.EnterMultipleScope())
|
||||
{
|
||||
Assert.That(pumpObservations, Is.Not.Empty);
|
||||
Assert.That(pumpObservations.First().Time, Is.GreaterThanOrEqualTo(pumpObservations.Last().Time));
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task FindByPatientAsync_ReturnsOrdered()
|
||||
{
|
||||
var result = await _repo.FindByPatientAsync(PatientId);
|
||||
|
||||
var pumpObservations = result.ToList();
|
||||
using (Assert.EnterMultipleScope())
|
||||
{
|
||||
Assert.That(pumpObservations, Is.Not.Empty);
|
||||
Assert.That(pumpObservations.First().Time, Is.GreaterThanOrEqualTo(pumpObservations.Last().Time));
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task FindLastByDeviceIdAsync_Works()
|
||||
{
|
||||
var obs = await _repo.FindLastByDeviceIdAsync("Device-A");
|
||||
|
||||
Assert.That(obs, Is.Not.Null);
|
||||
Assert.That(obs.DeviceId, Is.EqualTo("Device-A"));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task FindByPatientId_Works()
|
||||
{
|
||||
var list = await _repo.FindByPatientId(PatientId);
|
||||
|
||||
Assert.That(list.Any(), Is.True);
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------
|
||||
// AGGREGATED LAST OBSERVATIONS
|
||||
// -------------------------------------------------------------------
|
||||
[Test]
|
||||
public async Task AggregatedPatientLastObservations_DistinctByName()
|
||||
{
|
||||
var result = await _repo.AggregatedPatientLastObservations(PatientId);
|
||||
|
||||
using (Assert.EnterMultipleScope())
|
||||
{
|
||||
Assert.That(result, Is.Not.Empty);
|
||||
Assert.That(result, Has.Count.LessThanOrEqualTo(2));
|
||||
}
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------
|
||||
// DELETE METHODS
|
||||
// -------------------------------------------------------------------
|
||||
[Test]
|
||||
public async Task DeleteByPatientId_Works()
|
||||
{
|
||||
var pid = ObjectId.GenerateNewId();
|
||||
|
||||
await _repo.InsertAsync(new PumpObservation
|
||||
{
|
||||
Id = ObjectId.GenerateNewId(),
|
||||
PatientId = pid,
|
||||
DeviceId = "Z",
|
||||
Time = Now
|
||||
});
|
||||
|
||||
await _repo.DeleteByPatientId(pid);
|
||||
|
||||
var list = await _repo.FindByPatientId(pid);
|
||||
Assert.That(list.Any(), Is.False);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task DeleteOlderThanDaysAsync_Works()
|
||||
{
|
||||
var pid = ObjectId.GenerateNewId();
|
||||
|
||||
await _repo.InsertAsync(new PumpObservation
|
||||
{
|
||||
Id = ObjectId.GenerateNewId(),
|
||||
PatientId = pid,
|
||||
Name = "TestOld",
|
||||
Time = Now.AddDays(-10)
|
||||
});
|
||||
|
||||
var deleted = await _repo.DeleteOlderThanDaysAsync(5);
|
||||
|
||||
Assert.That(deleted, Is.GreaterThanOrEqualTo(1));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task DeleteOlderNumberAsync_Works()
|
||||
{
|
||||
const string name = "HistoryPump";
|
||||
|
||||
var obs = Enumerable.Range(0, 5).Select(i =>
|
||||
new PumpObservation
|
||||
{
|
||||
Id = ObjectId.GenerateNewId(),
|
||||
DeviceId = "H",
|
||||
Name = name,
|
||||
Time = Now.AddSeconds(-i)
|
||||
});
|
||||
|
||||
await _repo.InsertManyAsync(obs);
|
||||
|
||||
var deleted = await _repo.DeleteOlderNumberAsync(name, 2);
|
||||
|
||||
Assert.That(deleted, Is.EqualTo(3));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task DeleteKeepLastNAsync_Works()
|
||||
{
|
||||
var items = Enumerable.Range(0, 8).Select(i =>
|
||||
new PumpObservation
|
||||
{
|
||||
Id = ObjectId.GenerateNewId(),
|
||||
DeviceId = "Device-Clean",
|
||||
Time = Now.AddSeconds(-i)
|
||||
});
|
||||
|
||||
await _repo.InsertManyAsync(items);
|
||||
|
||||
var deleted = await _repo.DeleteKeepLastNAsync(3);
|
||||
|
||||
Assert.That(deleted, Is.EqualTo(5));
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------
|
||||
// UPDATE METHOD
|
||||
// -------------------------------------------------------------------
|
||||
[Test]
|
||||
public async Task UpdateManyObjectIdByFieldAsync_Works()
|
||||
{
|
||||
var oldId = ObjectId.GenerateNewId();
|
||||
var newId = ObjectId.GenerateNewId();
|
||||
|
||||
await _repo.InsertAsync(new PumpObservation
|
||||
{
|
||||
Id = ObjectId.GenerateNewId(),
|
||||
PatientId = oldId,
|
||||
DeviceId = "UpdateTest",
|
||||
Time = Now
|
||||
});
|
||||
|
||||
var updated = await _repo.UpdateManyObjectIdByFieldAsync("PatientId", newId, oldId);
|
||||
|
||||
Assert.That(updated, Is.EqualTo(1));
|
||||
|
||||
var found = await _repo.FindByPatientId(newId);
|
||||
Assert.That(found.Any(), Is.True);
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------
|
||||
// AGGREGATION: LAST OBSERVATION TIME PER PATIENT
|
||||
// -------------------------------------------------------------------
|
||||
[Test]
|
||||
public async Task FindAllLastPatientObservationTimeAsync_Works()
|
||||
{
|
||||
var dict = await _repo.FindAllLastPatientObservationTimeAsync();
|
||||
|
||||
Assert.That(dict, Is.Not.Null);
|
||||
using (Assert.EnterMultipleScope())
|
||||
{
|
||||
Assert.That(dict.ContainsKey(PatientId), Is.True);
|
||||
Assert.That(dict[PatientId], Is.LessThanOrEqualTo(DateTime.UtcNow));
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task FindByDeviceIdAsync_EmptyWhenOutOfDateRange()
|
||||
{
|
||||
var from = Now.AddYears(-1);
|
||||
var to = Now.AddYears(-1).AddHours(1);
|
||||
|
||||
var result = await _repo.FindByDeviceIdAsync("Device-A", from, to);
|
||||
|
||||
Assert.That(result, Is.Empty);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task FindByPatientAsync_RespectsLimit()
|
||||
{
|
||||
var result = await _repo.FindByPatientAsync(PatientId, limit: 1);
|
||||
var list = result.ToList();
|
||||
Assert.That(list, Has.Count.EqualTo(1));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task FindLastByDeviceIdAsync_ReturnsNullWhenNotExists()
|
||||
{
|
||||
var obs = await _repo.FindLastByDeviceIdAsync("Device-DoesNotExist");
|
||||
|
||||
Assert.That(obs, Is.Null);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task InsertManyAsync_IgnoresEmptyList()
|
||||
{
|
||||
var empty = new List<PumpObservation>();
|
||||
|
||||
Func<Task> act = () => _repo.InsertManyAsync(empty);
|
||||
|
||||
Assert.That(act, Throws.Nothing);
|
||||
}
|
||||
|
||||
|
||||
[Test]
|
||||
public async Task DeleteOlderNumberAsync_DoesNothingWhenCountBelowLimit()
|
||||
{
|
||||
const string name = "LimitTestPump";
|
||||
|
||||
var obs = new PumpObservation
|
||||
{
|
||||
Id = ObjectId.GenerateNewId(),
|
||||
DeviceId = "Device-X",
|
||||
Name = name,
|
||||
Time = Now
|
||||
};
|
||||
|
||||
await _repo.InsertAsync(obs);
|
||||
|
||||
var deleted = await _repo.DeleteOlderNumberAsync(name, maxCount: 5);
|
||||
|
||||
Assert.That(deleted, Is.Zero);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task DeleteOlderThanDaysAsync_DoesNotDeleteRecentObservations()
|
||||
{
|
||||
var obs = new PumpObservation
|
||||
{
|
||||
Id = ObjectId.GenerateNewId(),
|
||||
PatientId = PatientId,
|
||||
DeviceId = "Device-Recent",
|
||||
Name = "RecentPump",
|
||||
Time = Now.AddMinutes(-1) // reciente
|
||||
};
|
||||
|
||||
await _repo.InsertAsync(obs);
|
||||
|
||||
var deleted = await _repo.DeleteOlderThanDaysAsync(10);
|
||||
|
||||
Assert.That(deleted, Is.Zero);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task AggregatedPatientLastObservations_RemovesDuplicatesByCodeAndName()
|
||||
{
|
||||
var patient = ObjectId.GenerateNewId();
|
||||
|
||||
var p1 = new PumpObservation
|
||||
{
|
||||
Id = ObjectId.GenerateNewId(),
|
||||
PatientId = patient,
|
||||
DeviceId = "DevA",
|
||||
Code = "C1",
|
||||
Name = "PumpA",
|
||||
Time = Now
|
||||
};
|
||||
|
||||
var p2 = new PumpObservation
|
||||
{
|
||||
Id = ObjectId.GenerateNewId(),
|
||||
PatientId = patient,
|
||||
DeviceId = "DevA",
|
||||
Code = "C1",
|
||||
Name = "PumpA",
|
||||
Time = Now.AddSeconds(-1)
|
||||
};
|
||||
|
||||
await _repo.InsertManyAsync([p1, p2]);
|
||||
|
||||
var result = await _repo.AggregatedPatientLastObservations(patient);
|
||||
|
||||
Assert.That(result, Has.Count.EqualTo(1));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,185 @@
|
||||
using adas_core.Domain.Models.AppSettings;
|
||||
using adas_core.Domain.Models.Pumps;
|
||||
using adas_core.Infrastructure.Repositories;
|
||||
using Microsoft.Extensions.Options;
|
||||
using MongoDB.Bson;
|
||||
using MongoDB.Driver;
|
||||
using adas_core.Domain.Enums;
|
||||
|
||||
namespace adas_core.Test.Repositories;
|
||||
|
||||
[TestFixture]
|
||||
[Category("Integration")]
|
||||
public class PumpStateRepositoryTest
|
||||
{
|
||||
private PumpStateRepository _repo;
|
||||
private IOptions<ApiSettings> _apiSettings;
|
||||
|
||||
private static readonly DateTime Now = DateTime.UtcNow;
|
||||
private const string DeviceA = "Device-A";
|
||||
private const string DeviceB = "Device-B";
|
||||
private static readonly ObjectId PatientId = ObjectId.GenerateNewId();
|
||||
|
||||
// ============================================================
|
||||
// INIT
|
||||
// ============================================================
|
||||
[OneTimeSetUp]
|
||||
public async Task Init()
|
||||
{
|
||||
_apiSettings = Options.Create(new ApiSettings
|
||||
{
|
||||
PumpStates = "pump_states"
|
||||
});
|
||||
|
||||
await IntegrationDb.Database.DropCollectionAsync("pump_states");
|
||||
await IntegrationDb.Database.CreateCollectionAsync("pump_states");
|
||||
|
||||
_repo = new PumpStateRepository(
|
||||
_apiSettings,
|
||||
IntegrationDb.Database
|
||||
);
|
||||
|
||||
await _repo.CreateIndexes();
|
||||
|
||||
// Insertar estados iniciales
|
||||
var initialStates = new List<PumpState>
|
||||
{
|
||||
new()
|
||||
{
|
||||
Id = ObjectId.GenerateNewId(),
|
||||
DeviceId = DeviceA,
|
||||
PatientId = PatientId,
|
||||
LastUpdated = Now,
|
||||
IsInfusing = true,
|
||||
Status = PumpEnum.Status.Infusing,
|
||||
PumpMode = PumpEnum.Mode.Infusing
|
||||
},
|
||||
new()
|
||||
{
|
||||
Id = ObjectId.GenerateNewId(),
|
||||
DeviceId = DeviceB,
|
||||
LastUpdated = Now.AddSeconds(-10),
|
||||
IsInfusing = false,
|
||||
Status = PumpEnum.Status.NotInfusing
|
||||
}
|
||||
};
|
||||
|
||||
await IntegrationDb.Database.GetCollection<PumpState>("pump_states")
|
||||
.InsertManyAsync(initialStates);
|
||||
|
||||
var count = await _repo.Collection.CountDocumentsAsync(_ => true);
|
||||
Assert.That(count, Is.EqualTo(2));
|
||||
}
|
||||
|
||||
// ============================================================
|
||||
// FIND BY DEVICE ID
|
||||
// ============================================================
|
||||
[Test]
|
||||
public async Task FindByDeviceIdAsync_ReturnsCorrectState()
|
||||
{
|
||||
var state = await _repo.FindByDeviceIdAsync(DeviceA);
|
||||
|
||||
Assert.That(state, Is.Not.Null);
|
||||
using (Assert.EnterMultipleScope())
|
||||
{
|
||||
Assert.That(state!.DeviceId, Is.EqualTo(DeviceA));
|
||||
Assert.That(state.IsInfusing, Is.True);
|
||||
}
|
||||
}
|
||||
|
||||
// ============================================================
|
||||
// UPSERT (INSERT + UPDATE)
|
||||
// ============================================================
|
||||
[Test]
|
||||
public async Task UpsertAsync_InsertsNewWhenNotExists()
|
||||
{
|
||||
var newState = new PumpState
|
||||
{
|
||||
DeviceId = "Device-C",
|
||||
PatientId = ObjectId.GenerateNewId(),
|
||||
LastUpdated = Now,
|
||||
IsInfusing = false
|
||||
};
|
||||
|
||||
await _repo.UpsertAsync(newState);
|
||||
|
||||
var found = await _repo.FindByDeviceIdAsync("Device-C");
|
||||
Assert.That(found, Is.Not.Null);
|
||||
Assert.That(found!.IsInfusing, Is.False);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task UpsertAsync_UpdatesExistingState()
|
||||
{
|
||||
var updated = new PumpState
|
||||
{
|
||||
DeviceId = DeviceA,
|
||||
PatientId = PatientId,
|
||||
LastUpdated = Now.AddMinutes(1),
|
||||
IsInfusing = false,
|
||||
Status = PumpEnum.Status.NotInfusing
|
||||
};
|
||||
|
||||
await _repo.UpsertAsync(updated);
|
||||
|
||||
var found = await _repo.FindByDeviceIdAsync(DeviceA);
|
||||
|
||||
Assert.That(found, Is.Not.Null);
|
||||
using (Assert.EnterMultipleScope())
|
||||
{
|
||||
Assert.That(found!.IsInfusing, Is.False);
|
||||
Assert.That(found.Status, Is.EqualTo(PumpEnum.Status.NotInfusing));
|
||||
Assert.That(
|
||||
found.LastUpdated,
|
||||
Is.EqualTo(updated.LastUpdated).Within(TimeSpan.FromMilliseconds(1))
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// ============================================================
|
||||
// GET ALL
|
||||
// ============================================================
|
||||
[Test]
|
||||
public async Task GetAllAsync_ReturnsAllStates()
|
||||
{
|
||||
var list = await _repo.GetAllAsync();
|
||||
|
||||
var pumpStates = list.ToList();
|
||||
Assert.That(pumpStates, Is.Not.Null);
|
||||
Assert.That(pumpStates, Has.Count.GreaterThanOrEqualTo(2));
|
||||
|
||||
var containsA = pumpStates.Any(x => x.DeviceId == DeviceA);
|
||||
var containsB = pumpStates.Any(x => x.DeviceId == DeviceB);
|
||||
|
||||
using (Assert.EnterMultipleScope())
|
||||
{
|
||||
Assert.That(containsA, Is.True);
|
||||
Assert.That(containsB, Is.True);
|
||||
}
|
||||
}
|
||||
|
||||
// ============================================================
|
||||
// UNIQUE INDEX: UPSERT OVERWRITES (NO DUPLICADOS)
|
||||
// ============================================================
|
||||
[Test]
|
||||
public async Task UpsertAsync_DoesNotCreateDuplicates()
|
||||
{
|
||||
var before = await _repo.GetAllAsync();
|
||||
var countBefore = before.Count();
|
||||
|
||||
// Insert (upsert) for existing DeviceA
|
||||
var state = new PumpState
|
||||
{
|
||||
DeviceId = DeviceA,
|
||||
LastUpdated = Now.AddMinutes(5)
|
||||
};
|
||||
|
||||
await _repo.UpsertAsync(state);
|
||||
|
||||
var after = await _repo.GetAllAsync();
|
||||
var countAfter = after.Count();
|
||||
|
||||
// No debe aumentar, porque es un update, no un insert
|
||||
Assert.That(countAfter, Is.EqualTo(countBefore));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
using adas_core.Domain.Models;
|
||||
using adas_core.Domain.Models.AppSettings;
|
||||
using adas_core.Infrastructure.Repositories;
|
||||
using Microsoft.Extensions.Options;
|
||||
using MongoDB.Bson;
|
||||
using MongoDB.Driver;
|
||||
using Options = Microsoft.Extensions.Options.Options;
|
||||
|
||||
namespace adas_core.Test.Repositories;
|
||||
|
||||
[TestFixture]
|
||||
[Category("Integration")]
|
||||
public class RecordingAlertArchiveRepositoryTest
|
||||
{
|
||||
[OneTimeSetUp]
|
||||
public async Task Init()
|
||||
{
|
||||
_optionsApiSettings = Options.Create(_apiSettings);
|
||||
|
||||
var recordingAlert1 = new PatientRecordingAlert
|
||||
{
|
||||
PatientId = PatientId,
|
||||
IsRecording = true,
|
||||
Time = Now
|
||||
};
|
||||
|
||||
var recordingAlert2 = new PatientRecordingAlert
|
||||
{
|
||||
PatientId = ObjectId.GenerateNewId(),
|
||||
IsRecording = true,
|
||||
Time = Now.AddDays(-10)
|
||||
};
|
||||
|
||||
await IntegrationDb.Database.DropCollectionAsync("archive_patients_recordingalerts");
|
||||
await IntegrationDb.Database.CreateCollectionAsync("archive_patients_recordingalerts");
|
||||
|
||||
_repository = new RecordingAlertArchiveRepository(_optionsApiSettings, IntegrationDb.Database);
|
||||
|
||||
await _repository.InsertOneAsync(recordingAlert1);
|
||||
await _repository.InsertOneAsync(recordingAlert2);
|
||||
|
||||
Assert.That(await _repository.Collection.CountDocumentsAsync(_ => true), Is.EqualTo(2));
|
||||
}
|
||||
|
||||
private RecordingAlertArchiveRepository _repository;
|
||||
|
||||
private readonly ApiSettings _apiSettings = new()
|
||||
{
|
||||
ArchivePatientsRecordingalerts = "archive_patients_recordingalerts"
|
||||
};
|
||||
|
||||
private IOptions<ApiSettings> _optionsApiSettings;
|
||||
|
||||
private static readonly DateTime Now = DateTime.Now;
|
||||
private static readonly ObjectId PatientId = ObjectId.GenerateNewId();
|
||||
|
||||
|
||||
[Test]
|
||||
public async Task DeleteBeforeDate()
|
||||
{
|
||||
var result = await _repository.Collection.FindAsync(_ => true);
|
||||
|
||||
Assert.That(result, Is.Not.Null);
|
||||
Assert.That(result.ToList(), Has.Count.GreaterThan(1));
|
||||
|
||||
await _repository.DeleteBeforeDate(Now.AddDays(-1));
|
||||
|
||||
var resultDelete = await _repository.Collection.FindAsync(_ => true);
|
||||
|
||||
Assert.That(resultDelete, Is.Not.Null);
|
||||
Assert.That(resultDelete.ToList(), Has.Count.EqualTo(1));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,281 @@
|
||||
using adas_core.Domain.Models;
|
||||
using adas_core.Domain.Models.AppSettings;
|
||||
using adas_core.Infrastructure.Repositories;
|
||||
using Microsoft.Extensions.Options;
|
||||
using MongoDB.Bson;
|
||||
using MongoDB.Driver;
|
||||
using Options = Microsoft.Extensions.Options.Options;
|
||||
|
||||
namespace adas_core.Test.Repositories;
|
||||
|
||||
[TestFixture]
|
||||
[Category("Integration")]
|
||||
public class RecordingAlertRepositoryTest
|
||||
{
|
||||
[OneTimeSetUp]
|
||||
public async Task Init()
|
||||
{
|
||||
_optionsApiSettings = Options.Create(_apiSettings);
|
||||
|
||||
var recordingAlert1 = new PatientRecordingAlert
|
||||
{
|
||||
PatientId = PatientId,
|
||||
IsRecording = true,
|
||||
Time = Now
|
||||
};
|
||||
|
||||
var recordingAlert2 = new PatientRecordingAlert
|
||||
{
|
||||
PatientId = ObjectId.GenerateNewId(),
|
||||
IsRecording = true,
|
||||
Time = Now.AddDays(-10)
|
||||
};
|
||||
|
||||
await IntegrationDb.Database.DropCollectionAsync("archive_patients_recordingalerts");
|
||||
await IntegrationDb.Database.CreateCollectionAsync("archive_patients_recordingalerts");
|
||||
|
||||
_repository = new RecordingAlertRepository(_optionsApiSettings, IntegrationDb.Database);
|
||||
|
||||
await _repository.InsertOneAsync(recordingAlert1);
|
||||
await _repository.InsertOneAsync(recordingAlert2);
|
||||
|
||||
Assert.That(await _repository.Collection.CountDocumentsAsync(_ => true), Is.EqualTo(2));
|
||||
}
|
||||
|
||||
private RecordingAlertRepository _repository;
|
||||
|
||||
private readonly ApiSettings _apiSettings = new()
|
||||
{
|
||||
ArchivePatientsRecordingalerts = "archive_patients_recordingalerts"
|
||||
};
|
||||
|
||||
private IOptions<ApiSettings> _optionsApiSettings;
|
||||
|
||||
private static readonly DateTime Now = DateTime.Now;
|
||||
private static readonly ObjectId PatientId = ObjectId.GenerateNewId();
|
||||
|
||||
|
||||
[Test]
|
||||
public async Task DeleteAsync()
|
||||
{
|
||||
var recordingAlert = new PatientRecordingAlert
|
||||
{
|
||||
Id = ObjectId.GenerateNewId(),
|
||||
PatientId = ObjectId.GenerateNewId(),
|
||||
IsRecording = true,
|
||||
Time = Now
|
||||
};
|
||||
|
||||
await _repository.InsertOneAsync(recordingAlert);
|
||||
|
||||
var result = await _repository.Collection.FindAsync(p => p.Id == recordingAlert.Id);
|
||||
|
||||
Assert.That(result.FirstOrDefault(), Is.Not.Null);
|
||||
|
||||
await _repository.DeleteAsync(recordingAlert.Id);
|
||||
|
||||
var resultDelete = await _repository.Collection.FindAsync(p => p.Id == recordingAlert.Id);
|
||||
|
||||
Assert.That(resultDelete.FirstOrDefault(), Is.Null);
|
||||
}
|
||||
|
||||
|
||||
[Test]
|
||||
public async Task DeleteOlderDaysAsync()
|
||||
{
|
||||
var recordingAlert = new PatientRecordingAlert
|
||||
{
|
||||
Id = ObjectId.GenerateNewId(),
|
||||
PatientId = ObjectId.GenerateNewId(),
|
||||
IsRecording = true,
|
||||
Time = Now,
|
||||
Name = "recordingName1"
|
||||
};
|
||||
|
||||
var recordingAlertOld = new PatientRecordingAlert
|
||||
{
|
||||
Id = ObjectId.GenerateNewId(),
|
||||
PatientId = recordingAlert.PatientId,
|
||||
IsRecording = true,
|
||||
Time = Now.AddDays(-5),
|
||||
Name = "recordingName1"
|
||||
};
|
||||
|
||||
await _repository.InsertOneAsync(recordingAlert);
|
||||
await _repository.InsertOneAsync(recordingAlertOld);
|
||||
|
||||
var result = await _repository.Collection.FindAsync(p => p.PatientId == recordingAlert.PatientId);
|
||||
|
||||
Assert.That(result.ToList(), Has.Count.EqualTo(2));
|
||||
|
||||
await _repository.DeleteOlderDaysAsync("recordingName1", 2);
|
||||
|
||||
var resultDelete = await _repository.Collection.FindAsync(p => p.PatientId == recordingAlert.PatientId);
|
||||
|
||||
Assert.That(resultDelete.ToList(), Has.Count.EqualTo(1));
|
||||
|
||||
await _repository.Collection.DeleteManyAsync(d => d.PatientId == recordingAlert.PatientId);
|
||||
|
||||
var resultClear = await _repository.Collection.FindAsync(p => p.PatientId == recordingAlert.PatientId);
|
||||
|
||||
Assert.That(resultClear.ToList(), Has.Count.EqualTo(0));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task DeleteOlderNumberAsync()
|
||||
{
|
||||
var recordingAlert = new PatientRecordingAlert
|
||||
{
|
||||
Id = ObjectId.GenerateNewId(),
|
||||
PatientId = ObjectId.GenerateNewId(),
|
||||
IsRecording = true,
|
||||
Time = Now,
|
||||
Name = "recordingName1"
|
||||
};
|
||||
|
||||
var recordingAlertOld = new PatientRecordingAlert
|
||||
{
|
||||
Id = ObjectId.GenerateNewId(),
|
||||
PatientId = recordingAlert.PatientId,
|
||||
IsRecording = true,
|
||||
Time = Now.AddDays(-5),
|
||||
Name = "recordingName1"
|
||||
};
|
||||
|
||||
await _repository.InsertOneAsync(recordingAlert);
|
||||
await _repository.InsertOneAsync(recordingAlertOld);
|
||||
|
||||
var result = await _repository.Collection.FindAsync(p => p.Name == "recordingName1");
|
||||
|
||||
Assert.That(result.ToList(), Has.Count.EqualTo(2));
|
||||
|
||||
await _repository.DeleteOlderNumberAsync("recordingName1", 1);
|
||||
|
||||
var resultDelete = await _repository.Collection.FindAsync(p => p.Name == "recordingName1");
|
||||
|
||||
Assert.That(resultDelete.ToList(), Has.Count.EqualTo(1));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task DeleteByPatientId()
|
||||
{
|
||||
var recordingAlert = new PatientRecordingAlert
|
||||
{
|
||||
Id = ObjectId.GenerateNewId(),
|
||||
PatientId = ObjectId.GenerateNewId(),
|
||||
IsRecording = true,
|
||||
Time = Now
|
||||
};
|
||||
|
||||
var recordingAlertOld = new PatientRecordingAlert
|
||||
{
|
||||
Id = ObjectId.GenerateNewId(),
|
||||
PatientId = recordingAlert.PatientId,
|
||||
IsRecording = true,
|
||||
Time = Now.AddDays(-5)
|
||||
};
|
||||
|
||||
await _repository.InsertOneAsync(recordingAlert);
|
||||
await _repository.InsertOneAsync(recordingAlertOld);
|
||||
|
||||
var result = await _repository.Collection.FindAsync(p => p.PatientId == recordingAlert.PatientId);
|
||||
|
||||
Assert.That(result.FirstOrDefault(), Is.Not.Null);
|
||||
|
||||
await _repository.DeleteByPatientId(recordingAlert.PatientId);
|
||||
|
||||
var resultDelete = await _repository.Collection.FindAsync(p => p.PatientId == recordingAlert.PatientId);
|
||||
|
||||
Assert.That(resultDelete.FirstOrDefault(), Is.Null);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task FindByPatientIdAsync()
|
||||
{
|
||||
var result = await _repository.FindByPatientIdAsync(PatientId);
|
||||
|
||||
Assert.That(result.FirstOrDefault(), Is.Not.Null);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task FindLastObservations()
|
||||
{
|
||||
var recordingAlert = new PatientRecordingAlert
|
||||
{
|
||||
Id = ObjectId.GenerateNewId(),
|
||||
PatientId = ObjectId.GenerateNewId(),
|
||||
IsRecording = true,
|
||||
Time = Now,
|
||||
Name = "recordingName1"
|
||||
};
|
||||
|
||||
var recordingAlertOld = new PatientRecordingAlert
|
||||
{
|
||||
Id = ObjectId.GenerateNewId(),
|
||||
PatientId = recordingAlert.PatientId,
|
||||
IsRecording = true,
|
||||
Time = Now.AddDays(-5),
|
||||
Name = "recordingName1"
|
||||
};
|
||||
|
||||
await _repository.InsertOneAsync(recordingAlert);
|
||||
await _repository.InsertOneAsync(recordingAlertOld);
|
||||
|
||||
var result = await _repository.Collection.FindAsync(_ => true);
|
||||
|
||||
Assert.That(result, Is.Not.Null);
|
||||
Assert.That(result.ToList(), Has.Count.GreaterThan(2));
|
||||
|
||||
var resultFind = await _repository.FindLastObservations(recordingAlert.PatientId, "recordingName1", 1);
|
||||
|
||||
Assert.That(resultFind, Is.Not.Null);
|
||||
using (Assert.EnterMultipleScope())
|
||||
{
|
||||
Assert.That(resultFind.ToList(), Has.Count.EqualTo(1));
|
||||
Assert.That(resultFind.FirstOrDefault()?.Time.Day, Is.EqualTo(Now.Day));
|
||||
Assert.That(resultFind.FirstOrDefault()?.Time.Month, Is.EqualTo(Now.Month));
|
||||
Assert.That(resultFind.FirstOrDefault()?.Time.Year, Is.EqualTo(Now.Year));
|
||||
};
|
||||
await _repository.DeleteByPatientId(recordingAlert.PatientId);
|
||||
|
||||
var resultDelete = await _repository.Collection.FindAsync(p => p.PatientId == recordingAlert.PatientId);
|
||||
|
||||
Assert.That(resultDelete.FirstOrDefault(), Is.Null);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task UpdateManyObjectId()
|
||||
{
|
||||
var recordingAlert = new PatientRecordingAlert
|
||||
{
|
||||
Id = ObjectId.GenerateNewId(),
|
||||
PatientId = ObjectId.GenerateNewId(),
|
||||
IsRecording = true,
|
||||
Time = Now
|
||||
};
|
||||
|
||||
var newPatientId = ObjectId.GenerateNewId();
|
||||
|
||||
await _repository.InsertOneAsync(recordingAlert);
|
||||
|
||||
var result = await _repository.FindByPatientIdAsync(recordingAlert.PatientId);
|
||||
|
||||
Assert.That(result.FirstOrDefault(), Is.Not.Null);
|
||||
|
||||
await _repository.UpdateManyObjectId("patientid", newPatientId, recordingAlert.PatientId);
|
||||
|
||||
var resultOldId = await _repository.FindByPatientIdAsync(recordingAlert.PatientId);
|
||||
|
||||
Assert.That(resultOldId.FirstOrDefault(), Is.Null);
|
||||
|
||||
var resultNewId = await _repository.FindByPatientIdAsync(newPatientId);
|
||||
|
||||
Assert.That(resultNewId.FirstOrDefault(), Is.Not.Null);
|
||||
|
||||
await _repository.DeleteByPatientId(newPatientId);
|
||||
|
||||
var resultDelete = await _repository.FindByPatientIdAsync(newPatientId);
|
||||
|
||||
Assert.That(resultDelete.FirstOrDefault(), Is.Null);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,261 @@
|
||||
using adas_core.Domain.Models;
|
||||
using adas_core.Domain.Models.AppSettings;
|
||||
using adas_core.Domain.Models.MongoModels;
|
||||
using adas_core.Infrastructure.Repositories;
|
||||
using Microsoft.Extensions.Options;
|
||||
using MongoDB.Bson;
|
||||
using MongoDB.Driver;
|
||||
using Serilog;
|
||||
using static adas_core.Domain.Models.MongoModels.Section;
|
||||
using Options = Microsoft.Extensions.Options.Options;
|
||||
|
||||
namespace adas_core.Test.Repositories;
|
||||
|
||||
[TestFixture]
|
||||
[Category("Integration")]
|
||||
public class SectionRepositoryTest
|
||||
{
|
||||
[OneTimeSetUp]
|
||||
public async Task Init()
|
||||
{
|
||||
_optionsApiSettings = Options.Create(_apiSettings);
|
||||
|
||||
var boxes = new List<Box>
|
||||
{
|
||||
new() { Bed = "bed1", IsActive = true },
|
||||
new() { Bed = "bed2", IsActive = true },
|
||||
new() { Bed = "bed3", IsActive = true },
|
||||
new() { Bed = "bed4", IsActive = true },
|
||||
new() { Bed = "bed5", IsActive = true },
|
||||
new() { Bed = "bed6", IsActive = true }
|
||||
};
|
||||
|
||||
var section1 = new Section
|
||||
{
|
||||
_id = SectionId1,
|
||||
Id = "UCIP1",
|
||||
SectionTitle = "UCI PEDIÁTRICA 1",
|
||||
PointOfCare = "UCIP1",
|
||||
Items =
|
||||
[
|
||||
new SectionItem
|
||||
{
|
||||
Group = "Pasillo 1",
|
||||
Boxes = boxes
|
||||
}
|
||||
]
|
||||
};
|
||||
|
||||
var section2 = new Section
|
||||
{
|
||||
_id = SectionId2,
|
||||
Id = "UCIP2",
|
||||
SectionTitle = "UCI PEDIÁTRICA 2",
|
||||
PointOfCare = "UCIP2",
|
||||
Items =
|
||||
[
|
||||
new SectionItem
|
||||
{
|
||||
Group = "Pasillo 1",
|
||||
Boxes = boxes
|
||||
}
|
||||
]
|
||||
};
|
||||
|
||||
//await IntegrationDb.Database.DropCollectionAsync("config_sections");
|
||||
await IntegrationDb.Database.CreateCollectionAsync("config_sections");
|
||||
|
||||
_repository = new SectionRepository(_optionsApiSettings, IntegrationDb.Database);
|
||||
|
||||
await _repository.InsertOneAsync(section1);
|
||||
await _repository.InsertOneAsync(section2);
|
||||
}
|
||||
|
||||
private SectionRepository _repository;
|
||||
|
||||
private readonly ApiSettings _apiSettings = new()
|
||||
{
|
||||
ConfigSections = "config_sections"
|
||||
};
|
||||
|
||||
private IOptions<ApiSettings> _optionsApiSettings;
|
||||
|
||||
private static readonly ObjectId SectionId1 = ObjectId.GenerateNewId();
|
||||
private static readonly ObjectId SectionId2 = ObjectId.GenerateNewId();
|
||||
|
||||
[Test]
|
||||
public async Task GetAll_Return_List_Sections()
|
||||
{
|
||||
var result = await _repository.GetAll();
|
||||
|
||||
Assert.That(result, Is.Not.Null);
|
||||
Assert.That(result, Has.Count.EqualTo(2));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task FindBySection_Not_Find_Return_null()
|
||||
{
|
||||
var result = await _repository.FindBySection("");
|
||||
|
||||
Assert.That(result, Is.Null);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task FindBySection_Find_Return_Section()
|
||||
{
|
||||
var result = await _repository.FindBySection("UCI PEDIÁTRICA 2");
|
||||
|
||||
Assert.That(result, Is.Not.Null);
|
||||
Assert.That(result!.SectionTitle, Is.EqualTo("UCI PEDIÁTRICA 2"));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task FindByPointOfCare_Not_Find_Return_null()
|
||||
{
|
||||
var result = await _repository.FindByPointOfCare("");
|
||||
|
||||
Assert.That(result, Is.Null);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task FindByPointOfCare_Find_Return_Section()
|
||||
{
|
||||
var result = await _repository.FindByPointOfCare("UCIP1");
|
||||
|
||||
Assert.That(result, Is.Not.Null);
|
||||
Assert.That(result!.SectionTitle, Is.EqualTo("UCI PEDIÁTRICA 1"));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task FindById_Not_Find_Return_null()
|
||||
{
|
||||
var result = await _repository.FindById("");
|
||||
|
||||
Assert.That(result, Is.Null);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task FindById_Find_Return_Section()
|
||||
{
|
||||
var result = await _repository.FindById("UCIP1");
|
||||
|
||||
Assert.That(result, Is.Not.Null);
|
||||
Assert.That(result!.SectionTitle, Is.EqualTo("UCI PEDIÁTRICA 1"));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task FindById_Not_Find_Id_Return_null()
|
||||
{
|
||||
var result = await _repository.FindById(ObjectId.GenerateNewId());
|
||||
|
||||
Assert.That(result, Is.Null);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task FindById_Find_Id_Return_Section()
|
||||
{
|
||||
var result = await _repository.FindById(SectionId2);
|
||||
|
||||
Assert.That(result, Is.Not.Null);
|
||||
Assert.That(result!.SectionTitle, Is.EqualTo("UCI PEDIÁTRICA 2"));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task FindByLocation_Not_Find_Return_Emty_List()
|
||||
{
|
||||
PatientLocation location = new("UCIP3", "bed1");
|
||||
var result = await _repository.FindByLocation(location);
|
||||
|
||||
Assert.That(result, Is.Not.Null);
|
||||
Assert.That(result, Is.Empty);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task FindByLocation_Find_Return_List()
|
||||
{
|
||||
PatientLocation location = new("UCIP1", "bed1");
|
||||
var result = await _repository.FindByLocation(location);
|
||||
|
||||
Assert.That(result, Is.Not.Null);
|
||||
Assert.That(result, Has.Count.EqualTo(1));
|
||||
Assert.That(result[0].SectionTitle, Is.EqualTo("UCI PEDIÁTRICA 1"));
|
||||
}
|
||||
|
||||
|
||||
[Test]
|
||||
public async Task InsertUpdateAndDeleteSection()
|
||||
{
|
||||
var updateId = ObjectId.GenerateNewId();
|
||||
|
||||
var boxes = new List<Box>
|
||||
{
|
||||
new() { Bed = "bed1", IsActive = true },
|
||||
new() { Bed = "bed2", IsActive = true },
|
||||
new() { Bed = "bed3", IsActive = true },
|
||||
new() { Bed = "bed4", IsActive = true },
|
||||
new() { Bed = "bed5", IsActive = true },
|
||||
new() { Bed = "bed6", IsActive = true }
|
||||
};
|
||||
|
||||
var section1 = new Section
|
||||
{
|
||||
_id = ObjectId.GenerateNewId(),
|
||||
Id = "UCIP_U_Id",
|
||||
SectionTitle = "UCI PEDIÁTRICA U",
|
||||
PointOfCare = "UCIP_U",
|
||||
Items =
|
||||
[
|
||||
new SectionItem
|
||||
{
|
||||
Group = "Pasillo 1",
|
||||
Boxes = boxes
|
||||
}
|
||||
]
|
||||
};
|
||||
|
||||
var sectionUpdate = new Section
|
||||
{
|
||||
_id = updateId,
|
||||
Id = "UCIP_U_Id",
|
||||
SectionTitle = "UCI PEDIÁTRICA UPDATE",
|
||||
PointOfCare = "UCIP_U",
|
||||
Items =
|
||||
[
|
||||
new SectionItem
|
||||
{
|
||||
Group = "Pasillo 1",
|
||||
Boxes = boxes
|
||||
}
|
||||
]
|
||||
};
|
||||
|
||||
|
||||
await _repository.InsertOneAsync(section1);
|
||||
try
|
||||
{
|
||||
await _repository.Collection.CountDocumentsAsync(_ => true);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Log.Debug(ex.ToString());
|
||||
throw;
|
||||
}
|
||||
|
||||
var resultInsert = await _repository.FindById("UCIP_U_Id");
|
||||
|
||||
Assert.That(resultInsert, Is.Not.Null);
|
||||
Assert.That(resultInsert.SectionTitle, Is.EqualTo("UCI PEDIÁTRICA U"));
|
||||
|
||||
var result = await _repository.UpdateSection(sectionUpdate);
|
||||
|
||||
Assert.That(result, Is.Not.Null);
|
||||
Assert.That(result.SectionTitle, Is.Not.EqualTo("UCI PEDIÁTRICA U"));
|
||||
Assert.That(result.SectionTitle, Is.EqualTo("UCI PEDIÁTRICA UPDATE"));
|
||||
|
||||
await _repository.Collection.DeleteOneAsync(s => s.Id == "UCIP_U_Id");
|
||||
|
||||
var resultDelete = await _repository.FindById("UCIP_U_Id");
|
||||
|
||||
Assert.That(resultDelete, Is.Null);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
using adas_core.Domain.Models.AppSettings;
|
||||
using adas_core.Domain.Models.MongoModels;
|
||||
using adas_core.Infrastructure.Repositories;
|
||||
using Microsoft.Extensions.Options;
|
||||
using MongoDB.Bson;
|
||||
using Options = Microsoft.Extensions.Options.Options;
|
||||
|
||||
namespace adas_core.Test.Repositories;
|
||||
|
||||
[TestFixture]
|
||||
[Category("Integration")]
|
||||
public class ServiceConfigRepositoryTest
|
||||
{
|
||||
[OneTimeSetUp]
|
||||
public async Task Init()
|
||||
{
|
||||
_optionsApiSettings = Options.Create(_apiSettings);
|
||||
|
||||
var serviceConfig = new ServiceConfig
|
||||
{
|
||||
Id = _id,
|
||||
StrId = Id.ToString()
|
||||
};
|
||||
|
||||
await IntegrationDb.Database.DropCollectionAsync("service_config");
|
||||
await IntegrationDb.Database.CreateCollectionAsync("service_config");
|
||||
|
||||
_repository = new ServiceConfigRepository(_optionsApiSettings, IntegrationDb.Database);
|
||||
|
||||
await _repository.InsertOneAsync(serviceConfig);
|
||||
}
|
||||
|
||||
private ServiceConfigRepository _repository;
|
||||
|
||||
private readonly ApiSettings _apiSettings = new()
|
||||
{
|
||||
ServiceConfig = "service_config"
|
||||
};
|
||||
|
||||
private IOptions<ApiSettings> _optionsApiSettings;
|
||||
|
||||
private readonly ObjectId _id = ObjectId.GenerateNewId();
|
||||
private static readonly ObjectId Id = ObjectId.GenerateNewId();
|
||||
|
||||
|
||||
[Test]
|
||||
public async Task FindById_Find_string()
|
||||
{
|
||||
var result = await _repository.FindById(Id.ToString());
|
||||
|
||||
Assert.That(result, Is.Not.Null);
|
||||
Assert.That(result!.StrId, Is.EqualTo(Id.ToString()));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task FindById_Find_ObjectId()
|
||||
{
|
||||
var result = await _repository.FindById(_id);
|
||||
|
||||
Assert.That(result, Is.Not.Null);
|
||||
Assert.That(result!.Id, Is.EqualTo(_id));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,95 @@
|
||||
using adas_core.Domain.Enums;
|
||||
using adas_core.Domain.Models;
|
||||
using adas_core.Domain.Models.AppSettings;
|
||||
using adas_core.Infrastructure.Repositories;
|
||||
using Microsoft.Extensions.Options;
|
||||
using MongoDB.Bson;
|
||||
using MongoDB.Driver;
|
||||
using Options = Microsoft.Extensions.Options.Options;
|
||||
|
||||
namespace adas_core.Test.Repositories;
|
||||
|
||||
[TestFixture]
|
||||
[Category("Integration")]
|
||||
public class TreatmentArchiveRepositoryTest
|
||||
{
|
||||
[OneTimeSetUp]
|
||||
public async Task Init()
|
||||
{
|
||||
_optionsApiSettings = Options.Create(_apiSettings);
|
||||
|
||||
var treatment1 = new PatientTreatment
|
||||
{
|
||||
Id = ObjectId.GenerateNewId(),
|
||||
PatientId = PatientId,
|
||||
OrderControl = OrderControlType.Nw,
|
||||
PlacerOrder = new Entity { EntityIdentifier = "3690", NamespaceId = "CareVue" },
|
||||
FillerOrder = new Entity { EntityIdentifier = "3690", NamespaceId = "CareVue" },
|
||||
OrderStatus = "A",
|
||||
OrderTime = DateTime.Now,
|
||||
Notes = [],
|
||||
Routes = []
|
||||
};
|
||||
|
||||
var treatment2 = new PatientTreatment
|
||||
{
|
||||
Id = ObjectId.GenerateNewId(),
|
||||
PatientId = PatientId,
|
||||
OrderControl = OrderControlType.Dc,
|
||||
PlacerOrder = new Entity { EntityIdentifier = "3690", NamespaceId = "CareVue" },
|
||||
FillerOrder = new Entity { EntityIdentifier = "3690", NamespaceId = "CareVue" },
|
||||
OrderStatus = "A",
|
||||
OrderTime = DateTime.Now.AddDays(-5),
|
||||
Notes = [],
|
||||
Routes = []
|
||||
};
|
||||
|
||||
await IntegrationDb.Database.DropCollectionAsync("archive_patients_treatments");
|
||||
await IntegrationDb.Database.CreateCollectionAsync("archive_patients_treatments");
|
||||
|
||||
_repository = new TreatmentArchiveRepository(_optionsApiSettings, IntegrationDb.Database);
|
||||
|
||||
await _repository.InsertOneAsync(treatment1);
|
||||
await _repository.InsertOneAsync(treatment2);
|
||||
|
||||
Assert.That(_repository.Collection.FindAsync(_ => true).Result.ToList(), Has.Count.EqualTo(2));
|
||||
}
|
||||
|
||||
private TreatmentArchiveRepository _repository;
|
||||
|
||||
private readonly ApiSettings _apiSettings = new()
|
||||
{
|
||||
ArchivePatientsTreatments = "archive_patients_treatments"
|
||||
};
|
||||
|
||||
private IOptions<ApiSettings> _optionsApiSettings;
|
||||
|
||||
private static readonly DateTime Now = DateTime.Now;
|
||||
private static readonly ObjectId PatientId = ObjectId.GenerateNewId();
|
||||
|
||||
|
||||
[Test]
|
||||
public async Task DeleteBeforeDate()
|
||||
{
|
||||
var result = await _repository.Collection.FindAsync(p => p.PatientId == PatientId);
|
||||
|
||||
Assert.That(result, Is.Not.Null);
|
||||
Assert.That(result.ToList(), Has.Count.GreaterThan(1));
|
||||
|
||||
await _repository.DeleteBeforeDate(Now.AddHours(-2));
|
||||
|
||||
var resultDelete = await _repository.Collection.FindAsync(p => p.PatientId == PatientId);
|
||||
|
||||
Assert.That(resultDelete, Is.Not.Null);
|
||||
Assert.That(resultDelete.ToList(), Has.Count.EqualTo(1));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task FindAllFromPatient()
|
||||
{
|
||||
var result = await _repository.FindAllFromPatient(PatientId);
|
||||
|
||||
Assert.That(result, Is.Not.Null);
|
||||
Assert.That(result.ToList(), Has.Count.GreaterThan(0));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,186 @@
|
||||
using adas_core.Domain.Enums;
|
||||
using adas_core.Domain.Models;
|
||||
using adas_core.Domain.Models.AppSettings;
|
||||
using adas_core.Infrastructure.Repositories;
|
||||
using Microsoft.Extensions.Options;
|
||||
using MongoDB.Bson;
|
||||
using MongoDB.Driver;
|
||||
using Options = Microsoft.Extensions.Options.Options;
|
||||
|
||||
namespace adas_core.Test.Repositories;
|
||||
|
||||
[TestFixture]
|
||||
[Category("Integration")]
|
||||
public class TreatmentRepositoryTest
|
||||
{
|
||||
[OneTimeSetUp]
|
||||
public async Task Init()
|
||||
{
|
||||
_optionsApiSettings = Options.Create(_apiSettings);
|
||||
|
||||
_treatment1 = new PatientTreatment
|
||||
{
|
||||
Id = ObjectId.GenerateNewId(),
|
||||
PatientId = PatientId,
|
||||
OrderControl = OrderControlType.Nw,
|
||||
PlacerOrder = new Entity { EntityIdentifier = "3690", NamespaceId = "CareVue" },
|
||||
FillerOrder = new Entity { EntityIdentifier = "3690", NamespaceId = "CareVue" },
|
||||
OrderStatus = "A",
|
||||
OrderTime = DateTime.Now,
|
||||
Notes = [],
|
||||
Routes = []
|
||||
};
|
||||
|
||||
_treatment2 = new PatientTreatment
|
||||
{
|
||||
Id = ObjectId.GenerateNewId(),
|
||||
PatientId = PatientId,
|
||||
OrderControl = OrderControlType.Dc,
|
||||
PlacerOrder = new Entity { EntityIdentifier = "3690", NamespaceId = "CareVue" },
|
||||
FillerOrder = new Entity { EntityIdentifier = "3690", NamespaceId = "CareVue" },
|
||||
OrderStatus = "A",
|
||||
OrderTime = DateTime.Now.AddDays(-5),
|
||||
Notes = [],
|
||||
Routes = [],
|
||||
RequestedGiveCodesStatus =
|
||||
[
|
||||
new CodeStatus
|
||||
{ Code = new Code { CodingSystem = "CS", Identifier = "I", Text = "T" }, Status = "status" }
|
||||
]
|
||||
};
|
||||
|
||||
await IntegrationDb.Database.DropCollectionAsync("archive_patients_treatments");
|
||||
await IntegrationDb.Database.CreateCollectionAsync("archive_patients_treatments");
|
||||
|
||||
_repository = new TreatmentRepository(_optionsApiSettings, IntegrationDb.Database);
|
||||
|
||||
await _repository.InsertOneAsync(_treatment1);
|
||||
await _repository.InsertOneAsync(_treatment2);
|
||||
|
||||
Assert.That(_repository.Collection.FindAsync(_ => true).Result.ToList(), Has.Count.EqualTo(2));
|
||||
}
|
||||
|
||||
private TreatmentRepository _repository = null!;
|
||||
|
||||
private readonly ApiSettings _apiSettings = new()
|
||||
{
|
||||
ArchivePatientsTreatments = "archive_patients_treatments"
|
||||
};
|
||||
|
||||
private IOptions<ApiSettings> _optionsApiSettings = null!;
|
||||
|
||||
//private static readonly DateTime now = DateTime.Now;
|
||||
private static readonly ObjectId PatientId = ObjectId.GenerateNewId();
|
||||
private PatientTreatment _treatment1 = null!;
|
||||
private PatientTreatment _treatment2 = null!;
|
||||
|
||||
|
||||
[Test]
|
||||
public async Task GetByPatientId_Not_Find_Return_Null()
|
||||
{
|
||||
var result = await _repository.GetByPatientId(ObjectId.GenerateNewId());
|
||||
|
||||
var patientTreatments = result.ToList();
|
||||
Assert.That(patientTreatments, Is.Not.Null);
|
||||
Assert.That(patientTreatments.ToList(), Is.Empty);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task GetByPatientId_Find_Return_Null()
|
||||
{
|
||||
var result = await _repository.GetByPatientId(PatientId);
|
||||
|
||||
var patientTreatments = result.ToList();
|
||||
Assert.That(patientTreatments, Is.Not.Null);
|
||||
Assert.That(patientTreatments.ToList(), Has.Count.EqualTo(2));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task DeleteAsync()
|
||||
{
|
||||
var treatment = new PatientTreatment
|
||||
{
|
||||
Id = ObjectId.GenerateNewId(),
|
||||
PatientId = PatientId,
|
||||
OrderControl = OrderControlType.Nw,
|
||||
PlacerOrder = new Entity { EntityIdentifier = "3690", NamespaceId = "CareVue" },
|
||||
FillerOrder = new Entity { EntityIdentifier = "3690", NamespaceId = "CareVue" },
|
||||
OrderStatus = "A",
|
||||
OrderTime = DateTime.Now,
|
||||
Notes = [],
|
||||
Routes = []
|
||||
};
|
||||
|
||||
await _repository.InsertOneAsync(treatment);
|
||||
|
||||
var result = await _repository.Collection.FindAsync(t => t.Id == treatment.Id);
|
||||
|
||||
Assert.That(result, Is.Not.Null);
|
||||
Assert.That(result.ToList(), Has.Count.EqualTo(1));
|
||||
|
||||
await _repository.DeleteAsync(treatment.Id);
|
||||
|
||||
var resultDelete = await _repository.Collection.FindAsync(t => t.Id == treatment.Id);
|
||||
|
||||
Assert.That(resultDelete, Is.Not.Null);
|
||||
Assert.That(resultDelete.ToList(), Has.Count.EqualTo(0));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task FindByPatientIdAsync_Find_Return_List_Traetments()
|
||||
{
|
||||
var result = await _repository.FindByPatientIdAsync(PatientId);
|
||||
|
||||
Assert.That(result, Is.Not.Null);
|
||||
Assert.That(result.ToList(), Has.Count.EqualTo(2));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task DeleteByPatientId()
|
||||
{
|
||||
var treatment = new PatientTreatment
|
||||
{
|
||||
Id = ObjectId.GenerateNewId(),
|
||||
PatientId = ObjectId.GenerateNewId(),
|
||||
OrderControl = OrderControlType.Nw,
|
||||
PlacerOrder = new Entity { EntityIdentifier = "3690", NamespaceId = "CareVue" },
|
||||
FillerOrder = new Entity { EntityIdentifier = "3690", NamespaceId = "CareVue" },
|
||||
OrderStatus = "A",
|
||||
OrderTime = DateTime.Now,
|
||||
Notes = [],
|
||||
Routes = []
|
||||
};
|
||||
|
||||
await _repository.InsertOneAsync(treatment);
|
||||
|
||||
var result = await _repository.Collection.FindAsync(t => t.PatientId == treatment.PatientId);
|
||||
|
||||
Assert.That(result, Is.Not.Null);
|
||||
Assert.That(result.ToList(), Has.Count.EqualTo(1));
|
||||
|
||||
await _repository.DeleteByPatientId(treatment.PatientId);
|
||||
|
||||
var resultDelete = await _repository.Collection.FindAsync(t => t.PatientId == treatment.PatientId);
|
||||
|
||||
Assert.That(resultDelete, Is.Not.Null);
|
||||
Assert.That(resultDelete.ToList(), Has.Count.EqualTo(0));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task FindBolusTreatments_Find_Return_Treatment()
|
||||
{
|
||||
var result = await _repository.FindBolusTreatments(PatientId);
|
||||
|
||||
Assert.That(result, Is.Not.Null);
|
||||
Assert.That(result.ToList(), Has.Count.EqualTo(1));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task FindByPatientId_Find_Return_List_Traetments()
|
||||
{
|
||||
var result = await _repository.FindByPatientIdAsync(PatientId);
|
||||
|
||||
Assert.That(result, Is.Not.Null);
|
||||
Assert.That(result.ToList(), Has.Count.EqualTo(2));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,377 @@
|
||||
using adas_core.Domain.Enums;
|
||||
using adas_core.Domain.Models.AppSettings;
|
||||
using adas_core.Domain.Models.MongoModels;
|
||||
using adas_core.Infrastructure.Repositories;
|
||||
using Microsoft.Extensions.Options;
|
||||
using MongoDB.Bson;
|
||||
using Options = Microsoft.Extensions.Options.Options;
|
||||
|
||||
namespace adas_core.Test.Repositories;
|
||||
|
||||
[TestFixture]
|
||||
[Category("Integration")]
|
||||
public class UnitRepositoryTest
|
||||
{
|
||||
[OneTimeSetUp]
|
||||
public async Task Init()
|
||||
{
|
||||
// Set up your test environment, including database connection
|
||||
|
||||
// Mocking ApiSettings
|
||||
_optionsApiSettings = Options.Create(_apiSettings);
|
||||
|
||||
// Creating sample unit data
|
||||
var unit1 = new Unit
|
||||
{
|
||||
Id = ObjectId.GenerateNewId(),
|
||||
Name = "unit1"
|
||||
// Add other properties as needed
|
||||
};
|
||||
|
||||
var unit2 = new Unit
|
||||
{
|
||||
Id = ObjectId.GenerateNewId(),
|
||||
Name = "unit2"
|
||||
// Add other properties as needed
|
||||
};
|
||||
|
||||
// Insert sample data into the database
|
||||
await IntegrationDb.Database.DropCollectionAsync("units");
|
||||
await IntegrationDb.Database.CreateCollectionAsync("units");
|
||||
_repository = new UnitRepository(_optionsApiSettings, IntegrationDb.Database);
|
||||
await _repository.InsertOneUnit(unit1);
|
||||
await _repository.InsertOneUnit(unit2);
|
||||
}
|
||||
|
||||
private UnitRepository _repository;
|
||||
|
||||
private readonly ApiSettings _apiSettings = new()
|
||||
{
|
||||
Units = "units"
|
||||
};
|
||||
|
||||
private IOptions<ApiSettings> _optionsApiSettings;
|
||||
|
||||
[Test]
|
||||
public async Task InsertOneUnit_ValidUnit_ReturnsUnit()
|
||||
{
|
||||
// Arrange
|
||||
var unit = new Unit
|
||||
{
|
||||
Id = ObjectId.GenerateNewId(),
|
||||
Title = "Test Unit",
|
||||
Name = "Test Unit",
|
||||
LastUpdate = DateTime.UtcNow,
|
||||
PointOfCareIds = [ObjectId.GenerateNewId()],
|
||||
Status = StatusEnum.Type.Ok,
|
||||
Configuration = new UnitConfiguration
|
||||
{
|
||||
AutoAdt = true,
|
||||
},
|
||||
AltableOptionListId = ObjectId.GenerateNewId(),
|
||||
AllergyListId = ObjectId.GenerateNewId()
|
||||
};
|
||||
|
||||
// Act
|
||||
var insertResult = await _repository.InsertOneUnit(unit);
|
||||
var findResult = await _repository.FindById(unit.Id);
|
||||
|
||||
// Assert
|
||||
Assert.That(insertResult, Is.Not.Null);
|
||||
Assert.That(findResult, Is.Not.Null);
|
||||
Assert.That(findResult?.Id, Is.EqualTo(unit.Id));
|
||||
Assert.That(findResult?.Title, Is.EqualTo(unit.Title));
|
||||
Assert.That(findResult?.Name, Is.EqualTo(unit.Name));
|
||||
}
|
||||
|
||||
// [Test]
|
||||
// public async Task FindByLocation_ValidLocation_ReturnsUnit()
|
||||
// {
|
||||
// // Arrange
|
||||
// var location = new PatientLocation
|
||||
// {
|
||||
// Bed = "TestBed",
|
||||
// UnitName = "TestUnit"
|
||||
// };
|
||||
//
|
||||
// var unit = new Unit
|
||||
// {
|
||||
// Id = ObjectId.GenerateNewId(),
|
||||
// Title = "Test Unit",
|
||||
// Name = "Test Unit",
|
||||
// PointOfCares = new List<PointOfCare>
|
||||
// {
|
||||
// new PointOfCare { Bed = "TestBed", UnitName = "TestUnit" }
|
||||
// }
|
||||
// };
|
||||
//
|
||||
// // Insert the unit into the MongoDB database
|
||||
// await _repository.InsertOneUnit(unit);
|
||||
//
|
||||
// // Act
|
||||
// var result = await _repository.FindByLocation(location);
|
||||
//
|
||||
// // Assert
|
||||
// Assert.That(result, Is.Not.Null);
|
||||
// Assert.That(result?.Id, Is.EqualTo(unit.Id));
|
||||
// }
|
||||
|
||||
[Test]
|
||||
public async Task FindById_ValidId_ReturnsUnit()
|
||||
{
|
||||
// Arrange
|
||||
var unitId = ObjectId.GenerateNewId();
|
||||
var unit = new Unit
|
||||
{
|
||||
Id = unitId,
|
||||
Title = "Test Unit",
|
||||
Name = "Test Unit"
|
||||
};
|
||||
|
||||
await _repository.InsertOneUnit(unit);
|
||||
|
||||
// Act
|
||||
var result = await _repository.FindById(unitId);
|
||||
|
||||
// Assert
|
||||
Assert.That(result, Is.Not.Null);
|
||||
Assert.That(result?.Id, Is.EqualTo(unitId));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task FindById_InvalidId_ReturnsNull()
|
||||
{
|
||||
// Arrange
|
||||
var invalidId = ObjectId.GenerateNewId();
|
||||
|
||||
// Act
|
||||
var result = await _repository.FindById(invalidId);
|
||||
|
||||
// Assert
|
||||
Assert.That(result, Is.Null);
|
||||
}
|
||||
|
||||
// [Test]
|
||||
// public async Task FindByMasterListId_ValidId_ReturnsMatchingUnits()
|
||||
// {
|
||||
// // Arrange
|
||||
// var objectId = ObjectId.GenerateNewId();
|
||||
// var masterListType = MasterListType.DestinationList;
|
||||
//
|
||||
// // Insert units associated with the given ObjectId and MasterListType
|
||||
// var units = new List<Unit>
|
||||
// {
|
||||
// new Unit
|
||||
// {
|
||||
// Id = ObjectId.GenerateNewId(),
|
||||
// // Set other properties
|
||||
// DestinationList = objectId // Set property dynamically based on masterListType
|
||||
// },
|
||||
// new Unit
|
||||
// {
|
||||
// Id = ObjectId.GenerateNewId(),
|
||||
// // Set other properties
|
||||
// DestinationList = ObjectId.GenerateNewId() // Different ObjectId
|
||||
// },
|
||||
// // Add more units as needed
|
||||
// };
|
||||
//
|
||||
// foreach (var unit in units)
|
||||
// {
|
||||
// await _repository.InsertOneUnit(unit);
|
||||
// }
|
||||
//
|
||||
// // Act
|
||||
// var result = await _repository.FindByMasterListId(objectId, masterListType);
|
||||
//
|
||||
// // Assert
|
||||
// Assert.That(result, Is.Not.Null);
|
||||
// Assert.That(result,
|
||||
// Has.All.Property("SomeTypeId").EqualTo(objectId)); // Check if all returned units have the expected ObjectId
|
||||
// // Add more assertions as needed
|
||||
// }
|
||||
|
||||
[Test]
|
||||
public async Task FindByName_ValidName_ReturnsUnit()
|
||||
{
|
||||
// Arrange
|
||||
var unitName = "TestUnit";
|
||||
var unit = new Unit
|
||||
{
|
||||
Id = ObjectId.GenerateNewId(),
|
||||
Title = "Test Unit",
|
||||
Name = unitName
|
||||
};
|
||||
|
||||
await _repository.InsertOneUnit(unit);
|
||||
|
||||
// Act
|
||||
var result = await _repository.FindByName(unitName);
|
||||
|
||||
// Assert
|
||||
Assert.That(result, Is.Not.Null);
|
||||
Assert.That(result?.Name, Is.EqualTo(unitName));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task FindByName_InvalidName_ReturnsNull()
|
||||
{
|
||||
// Arrange
|
||||
var invalidName = "NonExistentUnitName";
|
||||
|
||||
// Act
|
||||
var result = await _repository.FindByName(invalidName);
|
||||
|
||||
// Assert
|
||||
Assert.That(result, Is.Null);
|
||||
}
|
||||
|
||||
// [Test]
|
||||
// public async Task FindByPointOfCare_ValidPointOfCare_ReturnsUnit()
|
||||
// {
|
||||
// // Arrange
|
||||
// var pointOfCare = new PointOfCare
|
||||
// {
|
||||
// Bed = "TestBed",
|
||||
// Room = "TestRoom",
|
||||
// UnitName = "TestUnit"
|
||||
// };
|
||||
//
|
||||
// var unit = new Unit
|
||||
// {
|
||||
// Id = ObjectId.GenerateNewId(),
|
||||
// Title = "Test Unit",
|
||||
// Name = "Test Unit",
|
||||
// PointOfCares = new System.Collections.Generic.List<PointOfCare>
|
||||
// {
|
||||
// pointOfCare
|
||||
// }
|
||||
// };
|
||||
//
|
||||
// await _repository.InsertOneUnit(unit);
|
||||
//
|
||||
// // Act
|
||||
// var result = await _repository.FindByPointOfCare(pointOfCare);
|
||||
//
|
||||
// // Assert
|
||||
// Assert.That(result, Is.Not.Null);
|
||||
// Assert.That(result?.PointOfCares, Contains.Item(pointOfCare));
|
||||
// }
|
||||
|
||||
// [Test]
|
||||
// public async Task FindByPointOfCare_InvalidPointOfCare_ReturnsNull()
|
||||
// {
|
||||
// // Arrange
|
||||
// var invalidPointOfCare = new PointOfCare
|
||||
// {
|
||||
// Bed = "NonExistentBed",
|
||||
// Room = "NonExistentRoom",
|
||||
// UnitName = "NonExistentUnitName"
|
||||
// };
|
||||
//
|
||||
// // Act
|
||||
// var result = await _repository.FindByPointOfCare(invalidPointOfCare);
|
||||
//
|
||||
// // Assert
|
||||
// Assert.That(result, Is.Null);
|
||||
// }
|
||||
|
||||
// [Test]
|
||||
// public async Task FindByUnitName_ValidUnitName_ReturnsMatchingUnits()
|
||||
// {
|
||||
// // Arrange
|
||||
// var unitName = "TestUnit";
|
||||
//
|
||||
// var units = new List<Unit>
|
||||
// {
|
||||
// new Unit
|
||||
// {
|
||||
// Id = ObjectId.GenerateNewId(),
|
||||
// Name = unitName,
|
||||
// },
|
||||
// new Unit
|
||||
// {
|
||||
// Id = ObjectId.GenerateNewId(),
|
||||
// Name = unitName,
|
||||
// },
|
||||
// };
|
||||
//
|
||||
// foreach (var unit in units)
|
||||
// {
|
||||
// await _repository.InsertOneUnit(unit);
|
||||
// }
|
||||
//
|
||||
// // Act
|
||||
// var result = await _repository.FindByUnitName(unitName);
|
||||
//
|
||||
// // Assert
|
||||
// Assert.That(result, Is.Not.Null);
|
||||
// Assert.That(result, Has.Count.EqualTo(units.Count));
|
||||
// }
|
||||
|
||||
// [Test]
|
||||
// public async Task FindByUnitName_InvalidUnitName_ReturnsEmptyList()
|
||||
// {
|
||||
// // Arrange
|
||||
// var invalidUnitName = "NonExistentUnitName";
|
||||
//
|
||||
// // Act
|
||||
// var result = await _repository.FindByUnitName(invalidUnitName);
|
||||
//
|
||||
// // Assert
|
||||
// Assert.That(result, Is.Not.Null);
|
||||
// Assert.That(result, Is.Empty);
|
||||
// }
|
||||
|
||||
[Test]
|
||||
public async Task GetAll_ReturnsAllUnits()
|
||||
{
|
||||
// Arrange
|
||||
|
||||
var units = new List<Unit>();
|
||||
if (units == null) throw new ArgumentNullException(nameof(units));
|
||||
for (var i = 0; i < 5; i++)
|
||||
{
|
||||
var unit = new Unit
|
||||
{
|
||||
Id = ObjectId.GenerateNewId()
|
||||
};
|
||||
units.Add(unit);
|
||||
await _repository.InsertOneUnit(unit);
|
||||
}
|
||||
|
||||
// Act
|
||||
var result = await _repository.GetAll();
|
||||
|
||||
// Assert
|
||||
Assert.That(result, Is.Not.Null);
|
||||
Assert.That(result, Has.Count.Not.EqualTo(0));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task UpdateUnit_ValidUnit_ReturnsUpdatedUnit()
|
||||
{
|
||||
// Arrange
|
||||
var originalUnit = new Unit
|
||||
{
|
||||
Id = ObjectId.GenerateNewId(),
|
||||
Title = "Original Title"
|
||||
};
|
||||
|
||||
var updatedUnit = new Unit
|
||||
{
|
||||
Id = originalUnit.Id,
|
||||
Title = "Updated Title"
|
||||
};
|
||||
|
||||
await _repository.InsertOneUnit(originalUnit);
|
||||
|
||||
// Act
|
||||
var result = await _repository.UpdateUnit(updatedUnit);
|
||||
|
||||
// Assert
|
||||
Assert.That(result, Is.Not.Null);
|
||||
Assert.That(result?.Id, Is.EqualTo(originalUnit.Id));
|
||||
Assert.That(result?.Title, Is.EqualTo(updatedUnit.Title));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
using adas_core.Domain.Models.AppSettings;
|
||||
using adas_core.Domain.Models.MongoModels;
|
||||
using adas_core.Infrastructure.Repositories;
|
||||
using Microsoft.Extensions.Options;
|
||||
using MongoDB.Bson;
|
||||
using Options = Microsoft.Extensions.Options.Options;
|
||||
|
||||
namespace adas_core.Test.Repositories;
|
||||
|
||||
[TestFixture]
|
||||
[Category("Integration")]
|
||||
public class UserRepositoryTest
|
||||
{
|
||||
[OneTimeSetUp]
|
||||
public async Task Init()
|
||||
{
|
||||
_optionsApiSettings = Options.Create(_apiSettings);
|
||||
|
||||
var user1 = new User
|
||||
{
|
||||
Id = ObjectId.GenerateNewId(),
|
||||
UserName = "username1",
|
||||
Password = "password1"
|
||||
//Rol = "rol1"
|
||||
};
|
||||
|
||||
var user2 = new User
|
||||
{
|
||||
Id = ObjectId.GenerateNewId(),
|
||||
UserName = "username2",
|
||||
Password = "password2"
|
||||
//Rol = "rol2"
|
||||
};
|
||||
|
||||
await IntegrationDb.Database.DropCollectionAsync("users");
|
||||
await IntegrationDb.Database.CreateCollectionAsync("users");
|
||||
|
||||
_repository = new UserRepository(_optionsApiSettings, IntegrationDb.Database);
|
||||
|
||||
await _repository.InsertOneAsync(user1);
|
||||
await _repository.InsertOneAsync(user2);
|
||||
}
|
||||
|
||||
private UserRepository _repository = null!;
|
||||
|
||||
private readonly ApiSettings _apiSettings = new()
|
||||
{
|
||||
Users = "users"
|
||||
};
|
||||
|
||||
private IOptions<ApiSettings> _optionsApiSettings = null!;
|
||||
|
||||
|
||||
[Test]
|
||||
public async Task GetUser()
|
||||
{
|
||||
var result = await _repository.GetUser("username1", "password1");
|
||||
|
||||
Assert.That(result, Is.Not.Null);
|
||||
using (Assert.EnterMultipleScope())
|
||||
{
|
||||
Assert.That(result!.UserName, Is.EqualTo("username1"));
|
||||
Assert.That(result.Password, Is.EqualTo("password1"));
|
||||
//Assert.That(result.Rol, Is.EqualTo("rol1"));
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user