Files
adas-core/adas-core.Test/Repositories/MedicineRepositoryTest.cs
T
2026-06-26 10:29:23 +02:00

151 lines
5.2 KiB
C#

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
{
/// <summary>
/// Performs one-time initialization for integration tests by seeding the medicines collection with a test record and verifying it can be retrieved by code or note.
/// </summary>
[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!;
/// <summary>
/// Verifies that <c>GetMedicineByCodeOrNote</c> returns a non-null, empty list when invoked with an array containing an empty string.
/// </summary>
[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));
}
/// <summary>
/// Tests that the repository returns a non-null medicine when a valid medicine code is found.
/// </summary>
[Test]
public async Task GetMedicine_Find_Code_Return_Medicine()
{
var result = await _repository.GetMedicine("12345");
Assert.That(result, Is.Not.Null);
}
/// <summary>
/// Verifies that the repository's <c>GetMedicine</c> method returns <c>null</c> when the provided medicine code does not match any existing record.
/// </summary>
[Test]
public async Task GetMedicine_Not_Find_Code_Return_Empty()
{
var result = await _repository.GetMedicine("123456");
Assert.That(result, Is.Null);
}
/// <summary>
/// Verifies that the repository returns a non-null medicine when a medicine is found
/// by the supplied note identifier ("note1"), confirming successful lookup by note.
/// </summary>
[Test]
public async Task GetMedicine_Find_Note_Return_Medicine()
{
var result = await _repository.GetMedicine("note1");
Assert.That(result, Is.Not.Null);
}
/// <summary>
/// Verifies that UpdateMedicine returns the updated medicine and that the changes are persisted and retrievable via GetMedicineById, including the updated notes collection.
/// </summary>
[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);
};
}
}