Creado apartir del commit b888de6c92056de294456f581a56e25e734d4ab7 de develop
This commit is contained in:
@@ -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));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user