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 { /// /// Performs one-time setup for integration tests by resetting the patients_appointments collection and seeding it with two test patient appointments: one with minimal fields and another extended with visit number, appointment reason, and resource groups. /// [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 { { "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 _optionsApiSettings; private static readonly DateTime Now = DateTime.Now; private static readonly ObjectId PatientId = ObjectId.GenerateNewId(); /// /// Verifies that .GetByPatient returns a non-null, non-empty list when a valid patient identifier is provided. /// [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); } /// /// Verifies that returns a non-null, empty list when no records are found for the specified patient identifier. /// [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); } /// /// Verifies that the repository returns null when no patient is found matching the specified patient identifier and visit number. /// [Test] public async Task FindByPatientAndVisitNumber_Not_Find_Patient_Return_Null() { var result = await _repository.FindByPatientAndVisitNumber(PatientId, "visitNumber"); Assert.That(result, Is.Null); } /// /// Verifies that the repository's FindByPatientAndVisitNumber method successfully retrieves /// the corresponding appointment when a valid patient identifier and an existing visit number are supplied. /// [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")); } /// /// Verifies that the repository returns null when searching for an appointment by patient ID and a reason that does not match any existing appointment. /// [Test] public async Task FindByPatientAndReason_Not_Find_Patient_Return_Null() { var result = await _repository.FindByPatientAndReason(PatientId, "NotappointmentReason"); Assert.That(result, Is.Null); } /// /// Tests that FindByPatientAndReason returns a non-null appointment containing the expected AppointmentReason for the specified patient. /// [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")); } /// /// Verifies that .FindByLocation returns an empty (non-null) collection when no patient is associated with the specified location. /// [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)); } /// /// Verifies that successfully retrieves the appointment associated with a patient located at the specified . /// [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)); } }