Лабораторная работа 2. Исправлена логика изменения количества компонента на складе, Добавлен тест на успешное добавление компонента в товар, если его хватает на складе

This commit is contained in:
Есения Андрианова 2025-02-27 21:37:19 +04:00
parent 44691c91e7
commit fea322f601
5 changed files with 55 additions and 1 deletions

View File

@ -110,7 +110,7 @@ internal class ProductBusinessLogicContract(
if (component == null)
throw new ArgumentNullException($"Component {componentOnStorage.ComponentId} not found");
if (componentOnStorage.Count == 0)
if (componentOnStorage.Count <= 0)
throw new OutOfStockException(component.ComponentName);
ComponentInProductDataModel componentInProduct = new ComponentInProductDataModel(component.Id, productId);

View File

@ -73,6 +73,23 @@ namespace NorthBridgeBusinessLogic.Implementations
_storageContract.UpdateComponentCount(storageId, componentId, newCount);
}
public int GetComponentCountFromStorage(string componentId, string storageId)
{
var storage = _storageContract.GetStorageById(storageId);
var component = _storageContract.GetComponentOnStorage(storageId, componentId);
if (storage == null)
{
throw new ArgumentNullException("Storage not found");
}
if (component == null)
{
throw new ArgumentNullException("Component not found");
}
return component.Count;
}
public List<ComponentOnStorageDataModel> GetComponentsFromStorage(string storageId)
{
var storage = _storageContract.GetStorageById(storageId);

View File

@ -17,6 +17,8 @@ namespace NorthBridgeContract.BusinessLogicsContracts
void UpdateComponentCount(string storageId, string componentId, int newCount);
int GetComponentCountFromStorage(string componentId, string storageId);
List<ComponentOnStorageDataModel> GetComponentsFromStorage(string storageId);
StorageDataModel GetStorageById(string storageId);

View File

@ -21,6 +21,8 @@ namespace NorthBridgeContract.StoragesContracts
void UpdateComponentCount(string storageId, string componentId, int newCount);
ComponentOnStorageDataModel GetComponentOnStorage(string storageId, string componentId);
List<ComponentOnStorageDataModel> GetComponentsOnStorage(string storageId);
}

View File

@ -199,6 +199,39 @@ namespace NorthBridgeTest.BusinessLogicsContractsTests
Assert.That(() => _productBusinessLogicContract.DeleteProduct(id), Throws.TypeOf<ArgumentNullException>());
_productStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Never);
}
[Test]
public void AddComponentToProduct_SuccessfullyAddsComponent()
{
// Arrange
string productId = Guid.NewGuid().ToString();
string componentId = Guid.NewGuid().ToString();
string storageId = Guid.NewGuid().ToString();
var count = 5;
var componentOnStorage = new ComponentOnStorageDataModel(storageId, componentId, count);
var component = new ComponentDataModel(
componentId,
"RAM",
ComponentType.RAM,
Guid.NewGuid().ToString(),
100.50,
false
);
_componentStorageContract
.Setup(cs => cs.GetElementById(componentId))
.Returns(component);
// Act
_productBusinessLogicContract.AddComponentToProduct(componentOnStorage, productId);
// Assert
_productStorageContract.Verify(ps => ps.AddComponentToProduct(
It.Is<ComponentInProductDataModel>(c => c.ComponentId == componentId && c.ProductId == productId)
), Times.Once);
}
}