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
{
///
/// One-time test setup that prepares the integration database for patient diagnosis tests by dropping and recreating the diagnoses collection, inserting three sample records (one with predefined test identifiers and two with auto-generated identifiers), and initializing the under test.
///
[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 _optionsApiSettings;
private static readonly DateTime Now = DateTime.Now;
private static readonly ObjectId PatientId = ObjectId.GenerateNewId();
private static readonly ObjectId Id = ObjectId.GenerateNewId();
///
/// Verifies that the repository returns a non-null and non-empty collection when retrieving records for an existing patient.
///
[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 an 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 successfully retrieves a diagnosis for an existing patient using the provided patient identifier, diagnosis code, and diagnosis system.
///
/// A task that represents the asynchronous test execution.
[Test]
public async Task FindByPatientIdAndCode_Find_Patient_Return_Diagnois()
{
var result = await _repository.FindByPatientIdAndCode(PatientId, "code1", "diagnosisSystem");
Assert.That(result, Is.Not.Null);
}
///
/// Verifies that returns null when no patient is found
/// for the provided patient identifier, code, and diagnosis system.
///
[Test]
public async Task FindByPatientIdAndCode_not_Find_Patient_Return_null()
{
var result = await _repository.FindByPatientIdAndCode(ObjectId.GenerateNewId(), "code4", "diagnosisSystem");
Assert.That(result, Is.Null);
}
///
/// Verifies that returns a non-null, non-empty list when looking up an existing patient by their identifier.
///
[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);
}
///
/// Verifies that FindByPatientIdAsync returns a non-null empty list when no patient matches the provided patient ID.
///
[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);
}
///
/// Verifies that DeleteAsync removes a patient diagnosis record by its identifier: after insertion, the record is retrievable by patient id, and once deleted, the search by patient id returns no results.
///
[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);
}
///
/// Verifies that DeleteByPatientId removes all diagnoses associated with the specified patient
/// by inserting a diagnosis, confirming it can be found, deleting it by patient id, and then
/// asserting that no diagnoses remain for that patient.
///
[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);
}
///
/// Tests that UpdateManyObjectId correctly updates the PatientId field across all matching documents by replacing the original ObjectId with a new one.
/// Verifies that after the update, the document can be retrieved using the new patient identifier.
///
[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));
}
}