232 lines
9.2 KiB
C#
232 lines
9.2 KiB
C#
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
|
|
{
|
|
/// <summary>
|
|
/// One-time test setup that prepares the integration database for patient diagnosis tests by dropping and recreating the diagnoses collection, inserting three sample <see cref="PatientDiagnosis"/> records (one with predefined test identifiers and two with auto-generated identifiers), and initializing the <see cref="DiagnosisRepository"/> under test.
|
|
/// </summary>
|
|
[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();
|
|
|
|
|
|
/// <summary>
|
|
/// Verifies that the repository returns a non-null and non-empty collection when retrieving records for an existing patient.
|
|
/// </summary>
|
|
[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);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Verifies that <see cref="DiagnosisRepository.GetByPatient"/> returns an empty list when no records are found for the specified patient identifier.
|
|
/// </summary>
|
|
[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);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Verifies that <see cref="DiagnosisRepository.FindByPatientIdAndCode"/> successfully retrieves a diagnosis for an existing patient using the provided patient identifier, diagnosis code, and diagnosis system.
|
|
/// </summary>
|
|
/// <returns>A task that represents the asynchronous test execution.</returns>
|
|
[Test]
|
|
public async Task FindByPatientIdAndCode_Find_Patient_Return_Diagnois()
|
|
{
|
|
var result = await _repository.FindByPatientIdAndCode(PatientId, "code1", "diagnosisSystem");
|
|
|
|
Assert.That(result, Is.Not.Null);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Verifies that <see cref="DiagnosisRepository.FindByPatientIdAndCode"/> returns <c>null</c> when no patient is found
|
|
/// for the provided patient identifier, code, and diagnosis system.
|
|
/// </summary>
|
|
[Test]
|
|
public async Task FindByPatientIdAndCode_not_Find_Patient_Return_null()
|
|
{
|
|
var result = await _repository.FindByPatientIdAndCode(ObjectId.GenerateNewId(), "code4", "diagnosisSystem");
|
|
|
|
Assert.That(result, Is.Null);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Verifies that <see cref="DiagnosisRepository.FindByPatientIdAsync"/> returns a non-null, non-empty list when looking up an existing patient by their identifier.
|
|
/// </summary>
|
|
[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);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Verifies that <c>FindByPatientIdAsync</c> returns a non-null empty list when no patient matches the provided patient ID.
|
|
/// </summary>
|
|
[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);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Verifies that <c>DeleteAsync</c> 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.
|
|
/// </summary>
|
|
[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);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Verifies that <c>DeleteByPatientId</c> 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.
|
|
/// </summary>
|
|
[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);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Tests that <c>UpdateManyObjectId</c> correctly updates the <c>PatientId</c> field across all matching <see cref="PatientDiagnosis"/> 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.
|
|
/// </summary>
|
|
[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));
|
|
}
|
|
} |