rama creada apartir de master en j
This commit is contained in:
@@ -12,36 +12,39 @@ namespace adas_core.Test.Repositories;
|
||||
[Category("Integration")]
|
||||
public class UnitRepositoryTest
|
||||
{
|
||||
/// <summary>
|
||||
/// One-time setup method that prepares the integration test environment by configuring API settings, creating a fresh "units" collection in the database, and seeding it with two sample <see cref="Unit"/> records via the <see cref="UnitRepository"/>.
|
||||
/// </summary>
|
||||
[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
|
||||
public async Task Init()
|
||||
{
|
||||
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);
|
||||
}
|
||||
// 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;
|
||||
|
||||
@@ -52,37 +55,40 @@ public class UnitRepositoryTest
|
||||
|
||||
private IOptions<ApiSettings> _optionsApiSettings;
|
||||
|
||||
/// <summary>
|
||||
/// Verifies that inserting a valid <see cref="Unit"/> into the repository returns the unit and that the inserted unit can be successfully retrieved by its identifier, with key properties preserved.
|
||||
/// </summary>
|
||||
[Test]
|
||||
public async Task InsertOneUnit_ValidUnit_ReturnsUnit()
|
||||
{
|
||||
// Arrange
|
||||
var unit = new Unit
|
||||
public async Task InsertOneUnit_ValidUnit_ReturnsUnit()
|
||||
{
|
||||
Id = ObjectId.GenerateNewId(),
|
||||
Title = "Test Unit",
|
||||
Name = "Test Unit",
|
||||
LastUpdate = DateTime.UtcNow,
|
||||
PointOfCareIds = [ObjectId.GenerateNewId()],
|
||||
Status = StatusEnum.Type.Ok,
|
||||
Configuration = new UnitConfiguration
|
||||
// Arrange
|
||||
var unit = new Unit
|
||||
{
|
||||
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));
|
||||
}
|
||||
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()
|
||||
@@ -116,40 +122,46 @@ public class UnitRepositoryTest
|
||||
// Assert.That(result?.Id, Is.EqualTo(unit.Id));
|
||||
// }
|
||||
|
||||
/// <summary>
|
||||
/// Verifies that FindById retrieves the matching <see cref="Unit"/> from the repository when a valid identifier is provided.
|
||||
/// </summary>
|
||||
[Test]
|
||||
public async Task FindById_ValidId_ReturnsUnit()
|
||||
{
|
||||
// Arrange
|
||||
var unitId = ObjectId.GenerateNewId();
|
||||
var unit = new Unit
|
||||
public async Task FindById_ValidId_ReturnsUnit()
|
||||
{
|
||||
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));
|
||||
}
|
||||
// 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));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Verifies that FindById returns <c>null</c> when queried with an ID that does not exist in the repository.
|
||||
/// </summary>
|
||||
[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);
|
||||
}
|
||||
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()
|
||||
@@ -191,40 +203,47 @@ public class UnitRepositoryTest
|
||||
// // Add more assertions as needed
|
||||
// }
|
||||
|
||||
/// <summary>
|
||||
/// Verifies that the repository's <c>FindByName</c> method returns the matching unit when called with a valid unit name.
|
||||
/// The test inserts a unit, retrieves it by name, and asserts that the returned entity is not null and has the expected name.
|
||||
/// </summary>
|
||||
[Test]
|
||||
public async Task FindByName_ValidName_ReturnsUnit()
|
||||
{
|
||||
// Arrange
|
||||
var unitName = "TestUnit";
|
||||
var unit = new Unit
|
||||
public async Task FindByName_ValidName_ReturnsUnit()
|
||||
{
|
||||
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));
|
||||
}
|
||||
// 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));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Verifies that the repository's FindByName method returns null when invoked with a name that does not match any existing entity.
|
||||
/// </summary>
|
||||
[Test]
|
||||
public async Task FindByName_InvalidName_ReturnsNull()
|
||||
{
|
||||
// Arrange
|
||||
var invalidName = "NonExistentUnitName";
|
||||
|
||||
// Act
|
||||
var result = await _repository.FindByName(invalidName);
|
||||
|
||||
// Assert
|
||||
Assert.That(result, Is.Null);
|
||||
}
|
||||
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()
|
||||
@@ -323,55 +342,62 @@ public class UnitRepositoryTest
|
||||
// Assert.That(result, Is.Empty);
|
||||
// }
|
||||
|
||||
/// <summary>
|
||||
/// Verifies that <c>GetAll</c> returns a non-null collection containing the units that have been inserted into the repository.
|
||||
/// </summary>
|
||||
/// <exception cref="ArgumentNullException">Thrown when the <c>units</c> collection used to seed the repository is <c>null</c>.</exception>
|
||||
[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++)
|
||||
public async Task GetAll_ReturnsAllUnits()
|
||||
{
|
||||
var unit = new Unit
|
||||
// Arrange
|
||||
|
||||
var units = new List<Unit>();
|
||||
if (units == null) throw new ArgumentNullException(nameof(units));
|
||||
for (var i = 0; i < 5; i++)
|
||||
{
|
||||
Id = ObjectId.GenerateNewId()
|
||||
};
|
||||
units.Add(unit);
|
||||
await _repository.InsertOneUnit(unit);
|
||||
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));
|
||||
}
|
||||
|
||||
// Act
|
||||
var result = await _repository.GetAll();
|
||||
|
||||
// Assert
|
||||
Assert.That(result, Is.Not.Null);
|
||||
Assert.That(result, Has.Count.Not.EqualTo(0));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Verifies that UpdateUnit correctly updates an existing unit in the repository and returns the updated entity with the new title and the original identifier.
|
||||
/// </summary>
|
||||
[Test]
|
||||
public async Task UpdateUnit_ValidUnit_ReturnsUpdatedUnit()
|
||||
{
|
||||
// Arrange
|
||||
var originalUnit = new Unit
|
||||
public async Task UpdateUnit_ValidUnit_ReturnsUpdatedUnit()
|
||||
{
|
||||
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));
|
||||
}
|
||||
// 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