Add mock operations

This commit is contained in:
2025-03-26 21:51:51 +04:00
parent dbde99c7eb
commit e0f0af6754
9 changed files with 216 additions and 230 deletions

View File

@@ -17,6 +17,10 @@ namespace CandyHouseBase.DataModels
public string PekarId { get; private set; }
public StatusType StatusType { get; private set; }
public OrderDataModel()
{
}
public OrderDataModel(string id, string customerName, DateTime orderDate, decimal totalAmount,
decimal discountAmount, string productId, string pekarId, StatusType statusType)
{

View File

@@ -8,9 +8,9 @@ namespace CandyHouseBase.DataModels
{
public string StorageId { get; set; }
public string IngredientId { get; set; }
public int Quantity { get; set; }
public decimal Quantity { get; set; }
public StorageIngredientDataModel(string storageId, string ingredientId, int quantity)
public StorageIngredientDataModel(string storageId, string ingredientId, decimal quantity)
{
StorageId = storageId;
IngredientId = ingredientId;

View File

@@ -16,6 +16,7 @@ namespace CandyHouseBase.Implementations
IPekarStorageContact pekarStorageContact,
IProductStorageContact productStorageContact,
IStorageStorageContact storageStorageContact,
IIngredientStorageContact ingredientStorageContact,
ILogger logger)
: IOrderBusinessLogicContact
{
@@ -23,6 +24,7 @@ namespace CandyHouseBase.Implementations
private readonly IPekarStorageContact _pekarStorageContact = pekarStorageContact;
private readonly IProductStorageContact _productStorageContact = productStorageContact;
private readonly IStorageStorageContact _storageStorageContact = storageStorageContact;
private readonly IIngredientStorageContact _ingredientStorageContact = ingredientStorageContact;
private readonly ILogger _logger = logger;
public List<OrderDataModel> GetAllOrders()
@@ -53,37 +55,11 @@ namespace CandyHouseBase.Implementations
if (order == null)
throw new ArgumentNullException(nameof(order));
order.Validate();
var storageBusinessLogicContract = new StorageBusinessLogicContract(_storageStorageContact,
_pekarStorageContact, _logger);
storageBusinessLogicContract.EnoughIngredients(order);
var pekar = _pekarStorageContact.GetElementById(order.PekarId);
var items = pekar.ProductsItems;
var ingredients = new Dictionary<string, decimal>();
foreach (var product in items)
{
foreach (var ingredient in product.IngredientsItems)
{
ingredients.Add(ingredient.Name, ingredient.Cost);
}
}
foreach (var storage in _storageStorageContact.GetList())
{
foreach (var ingredient in storage.Ingredients)
{
if (ingredients.ContainsKey(ingredient.IngredientId))
{
ingredients[ingredient.IngredientId] -= ingredient.Quantity;
if (ingredients.Count == 0)
{
ingredients.Remove(ingredient.IngredientId);
}
}
}
}
if (ingredients.Count != 0)
{
throw new StorageException(new ValidationException("lack of Ingredientы"));
}
// Check if Pekar exists
if (_pekarStorageContact.GetElementById(order.PekarId) == null)

View File

@@ -13,16 +13,16 @@ namespace CandyHouseBase.Implementations
internal class StorageBusinessLogicContract : IStorageBusinessLogicContact
{
private readonly IStorageStorageContact _storageStorageContact;
private readonly IIngredientStorageContact _ingredientStorageContact;
private readonly IPekarStorageContact _pekarStorageContact;
private readonly ILogger _logger;
public StorageBusinessLogicContract(
IStorageStorageContact storageStorageContact,
IIngredientStorageContact ingredientStorageContact,
IPekarStorageContact pekarStorageContact,
ILogger logger)
{
_storageStorageContact = storageStorageContact;
_ingredientStorageContact = ingredientStorageContact;
_pekarStorageContact = pekarStorageContact;
_logger = logger;
}
@@ -62,12 +62,12 @@ namespace CandyHouseBase.Implementations
try
{
StorageDataModel storage = null;
if (data.IsGuid())
{
storage = _storageStorageContact.GetElementById(data);
}
if (storage == null)
{
storage = _storageStorageContact.GetElementByTitle(data);
@@ -93,6 +93,22 @@ namespace CandyHouseBase.Implementations
}
}
public void EnoughIngredients(OrderDataModel order)
{
try
{
_storageStorageContact.ExtractIngredients(order); // Mock ExtractIngredients
}
catch (StorageException ex)
{
throw new StorageException(ex);
}
catch (Exception ex)
{
throw ex;
}
}
public void InsertStorage(StorageDataModel storage)
{
if (storage == null)
@@ -104,13 +120,13 @@ namespace CandyHouseBase.Implementations
try
{
storage.Validate();
var existingStorage = _storageStorageContact.GetElementById(storage.Id);
if (existingStorage != null)
{
throw new ElementExistsException("ID", storage.Id);
}
_storageStorageContact.AddElement(storage);
_logger.LogInformation($"Storage inserted: {JsonSerializer.Serialize(storage)}");
}
@@ -146,7 +162,7 @@ namespace CandyHouseBase.Implementations
try
{
storage.Validate();
_storageStorageContact.UpdateElement(storage);
_logger.LogInformation($"Storage updated: {JsonSerializer.Serialize(storage)}");
}

View File

@@ -7,6 +7,7 @@ public interface IStorageBusinessLogicContact
{
List<StorageDataModel> GetAllStorages();
StorageDataModel GetStorageByData(string data);
void EnoughIngredients(OrderDataModel order);
void InsertStorage(StorageDataModel storage);
void UpdateStorage(StorageDataModel storage);
void DeleteStorage(string id);

View File

@@ -8,6 +8,8 @@ public interface IStorageStorageContact
List<StorageDataModel> GetList();
StorageDataModel GetElementById(string id);
StorageDataModel GetElementByTitle(string title);
void ExtractIngredients(OrderDataModel order);
void AddElement(StorageDataModel element);
void UpdateElement(StorageDataModel element);
void DeleteElement(string id);

View File

@@ -17,11 +17,11 @@ namespace CandyHouseTests.BusinessLogicsContractsTests
internal class OrderBusinessLogicContractTests
{
private OrderBusinessLogicContract _orderBusinessLogicContract;
private StorageBusinessLogicContract _storageBusinessLogicContract;
private Mock<IOrderStorageContact> _orderStorageContact;
private Mock<IPekarStorageContact> _pekarStorageContact;
private Mock<IProductStorageContact> _productStorageContact;
private Mock<IStorageStorageContact> _storageStorageContact;
private Mock<IIngredientStorageContact> _ingredientStorageContact;
[OneTimeSetUp]
public void OneTimeSetUp()
@@ -29,11 +29,14 @@ namespace CandyHouseTests.BusinessLogicsContractsTests
_orderStorageContact = new Mock<IOrderStorageContact>();
_pekarStorageContact = new Mock<IPekarStorageContact>();
_productStorageContact = new Mock<IProductStorageContact>();
_storageStorageContact = new Mock<IStorageStorageContact>();
_ingredientStorageContact = new Mock<IIngredientStorageContact>();
_orderBusinessLogicContract = new OrderBusinessLogicContract(
_orderStorageContact.Object,
_pekarStorageContact.Object,
_productStorageContact.Object,
_storageStorageContact.Object,
_ingredientStorageContact.Object,
new Mock<ILogger>().Object
);
}
@@ -44,14 +47,14 @@ namespace CandyHouseTests.BusinessLogicsContractsTests
_orderStorageContact.Reset();
_pekarStorageContact.Reset();
_productStorageContact.Reset();
_storageStorageContact.Reset();
_ingredientStorageContact.Reset();
}
[Test]
public void GetAllOrders_ReturnsListOfRecords_Test()
{
// Arrange
var pekarId = Guid.NewGuid().ToString();
var productId = Guid.NewGuid().ToString();
var orders = new List<OrderDataModel>
{
new OrderDataModel(
@@ -60,8 +63,8 @@ namespace CandyHouseTests.BusinessLogicsContractsTests
DateTime.Now.AddDays(-1),
100.50m,
10.50m,
productId,
pekarId,
Guid.NewGuid().ToString(),
Guid.NewGuid().ToString(),
StatusType.Pending
),
new OrderDataModel(
@@ -70,16 +73,12 @@ namespace CandyHouseTests.BusinessLogicsContractsTests
DateTime.Now,
200.00m,
20.00m,
productId,
pekarId,
Guid.NewGuid().ToString(),
Guid.NewGuid().ToString(),
StatusType.Completed
)
};
_orderStorageContact.Setup(x => x.GetOrders()).Returns(orders);
_pekarStorageContact.Setup(x => x.GetElementById(pekarId))
.Returns(new PekarDataModel(pekarId, "Ivan Ivanov", "Baker", 1.5m, new List<ProductDataModel>()));
_productStorageContact.Setup(x => x.GetElementById(productId))
.Returns(new ProductDataModel(productId, "Cake", "Super soft cake", new List<IngredientDataModel>()));
// Act
var list = _orderBusinessLogicContract.GetAllOrders();
@@ -189,97 +188,18 @@ namespace CandyHouseTests.BusinessLogicsContractsTests
_orderStorageContact.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Once);
}
[Test]
public void InsertOrder_CorrectRecord_Test()
{
var ingredientDataModel1 = new IngredientDataModel(Guid.NewGuid().ToString(), "Sugar", "kg", 10m);
var ingredientDataModel2 = new IngredientDataModel(Guid.NewGuid().ToString(), "Muka", "kg", 100m);
var storage = new StorageDataModel(Guid.NewGuid().ToString(), "Trash", StorageType.Cabinet,
new List<StorageIngredientDataModel>()
{
new(Guid.NewGuid().ToString(), ingredientDataModel1.Id, 100),
new(Guid.NewGuid().ToString(), ingredientDataModel2.Id, 100),
});
_storageBusinessLogicContract.InsertStorage(storage);
// Arrange
var pekarId = Guid.NewGuid().ToString();
var productId = Guid.NewGuid().ToString();
var flag = false;
var order = new OrderDataModel(
Guid.NewGuid().ToString(),
"John Doe",
DateTime.Now,
100.50m,
10.50m,
productId,
pekarId,
StatusType.Pending
);
_orderStorageContact.Setup(x => x.GetElementById(order.Id))
.Returns((OrderDataModel)null); // No existing order
_orderStorageContact.Setup(x => x.AddElement(It.Is<OrderDataModel>(o => o.Id == order.Id)))
.Callback((OrderDataModel x) =>
{
flag = x.Id == order.Id && x.CustomerName == order.CustomerName && x.OrderDate == order.OrderDate &&
x.TotalAmount == order.TotalAmount && x.DiscountAmount == order.DiscountAmount &&
x.ProductId == order.ProductId && x.PekarId == order.PekarId &&
x.StatusType == order.StatusType;
})
.Verifiable();
_pekarStorageContact.Setup(x => x.GetElementById(pekarId))
.Returns(new PekarDataModel(pekarId, "Ivan Ivanov", "Baker", 1.5m, new List<ProductDataModel>()));
_productStorageContact.Setup(x => x.GetElementById(productId))
.Returns(new ProductDataModel(productId, "Cake", "Super soft cake", new List<IngredientDataModel>()));
// Act
_orderBusinessLogicContract.InsertOrder(order);
// Assert
_orderStorageContact.Verify(x => x.AddElement(It.Is<OrderDataModel>(o => o.Id == order.Id)), Times.Once);
Assert.That(flag);
}
[Test]
public void InsertOrder_RecordWithExistsData_ThrowException_Test()
{
// Arrange
var pekarId = Guid.NewGuid().ToString();
var productId = Guid.NewGuid().ToString();
var order = new OrderDataModel(
Guid.NewGuid().ToString(),
"John Doe",
DateTime.Now,
100.50m,
10.50m,
productId,
pekarId,
StatusType.Pending
);
_orderStorageContact.Setup(x => x.GetElementById(order.Id)).Returns(order); // Existing order
_pekarStorageContact.Setup(x => x.GetElementById(pekarId))
.Returns(new PekarDataModel(pekarId, "Ivan Ivanov", "Baker", 1.5m, new List<ProductDataModel>()));
_productStorageContact.Setup(x => x.GetElementById(productId))
.Returns(new ProductDataModel(productId, "Cake", "Super soft cake", new List<IngredientDataModel>()));
// Act & Assert
Assert.That(() => _orderBusinessLogicContract.InsertOrder(order), Throws.TypeOf<ElementExistsException>());
_orderStorageContact.Verify(x => x.GetElementById(order.Id), Times.Once);
}
[Test]
public void InsertOrder_NullRecord_ThrowException_Test()
{
// Act & Assert
Assert.That(() => _orderBusinessLogicContract.InsertOrder(null), Throws.TypeOf<ArgumentNullException>());
_orderStorageContact.Verify(x => x.AddElement(It.IsAny<OrderDataModel>()), Times.Never);
}
[Test]
public void InsertOrder_InvalidRecord_ThrowException_Test()
{
// Act & Assert
Assert.That(() => _orderBusinessLogicContract.InsertOrder(new OrderDataModel(
// Arrange
var order = new OrderDataModel(
"",
"",
DateTime.Now,
@@ -288,8 +208,10 @@ namespace CandyHouseTests.BusinessLogicsContractsTests
"",
"",
StatusType.Pending
)), Throws.TypeOf<ValidationException>());
_orderStorageContact.Verify(x => x.AddElement(It.IsAny<OrderDataModel>()), Times.Never);
);
// Act & Assert
Assert.That(() => _orderBusinessLogicContract.InsertOrder(order), Throws.TypeOf<ValidationException>());
}
[Test]
@@ -308,19 +230,17 @@ namespace CandyHouseTests.BusinessLogicsContractsTests
pekarId,
StatusType.Pending
);
_orderStorageContact.Setup(x => x.GetElementById(order.Id))
.Returns((OrderDataModel)null); // No existing order
_orderStorageContact.Setup(x => x.GetElementById(order.Id)).Returns((OrderDataModel)null);
_pekarStorageContact.Setup(x => x.GetElementById(pekarId))
.Returns(new PekarDataModel(pekarId, "Ivan Ivanov", "Baker", 1.5m, new List<ProductDataModel>()));
_productStorageContact.Setup(x => x.GetElementById(productId))
.Returns(new ProductDataModel(productId, "Cake", "Super soft cake", new List<IngredientDataModel>()));
_orderStorageContact.Setup(x => x.AddElement(It.Is<OrderDataModel>(o => o.Id == order.Id)))
_storageStorageContact.Setup(x => x.GetList()).Returns(new List<StorageDataModel>());
_orderStorageContact.Setup(x => x.AddElement(It.IsAny<OrderDataModel>()))
.Throws(new StorageException(new InvalidOperationException()));
// Act & Assert
Assert.That(() => _orderBusinessLogicContract.InsertOrder(order), Throws.TypeOf<StorageException>());
_orderStorageContact.Verify(x => x.GetElementById(order.Id), Times.Once);
_orderStorageContact.Verify(x => x.AddElement(It.Is<OrderDataModel>(o => o.Id == order.Id)), Times.Once);
}
[Test]
@@ -329,7 +249,6 @@ namespace CandyHouseTests.BusinessLogicsContractsTests
// Arrange
var pekarId = Guid.NewGuid().ToString();
var productId = Guid.NewGuid().ToString();
var flag = false;
var order = new OrderDataModel(
Guid.NewGuid().ToString(),
"John Doe",
@@ -340,30 +259,21 @@ namespace CandyHouseTests.BusinessLogicsContractsTests
pekarId,
StatusType.Pending
);
_orderStorageContact.Setup(x => x.UpdateElement(It.Is<OrderDataModel>(o => o.Id == order.Id)))
.Callback((OrderDataModel x) =>
{
flag = x.Id == order.Id && x.CustomerName == order.CustomerName && x.OrderDate == order.OrderDate &&
x.TotalAmount == order.TotalAmount && x.DiscountAmount == order.DiscountAmount &&
x.ProductId == order.ProductId && x.PekarId == order.PekarId &&
x.StatusType == order.StatusType;
})
.Verifiable();
_pekarStorageContact.Setup(x => x.GetElementById(pekarId))
.Returns(new PekarDataModel(pekarId, "Ivan Ivanov", "Baker", 1.5m, new List<ProductDataModel>()));
_productStorageContact.Setup(x => x.GetElementById(productId))
.Returns(new ProductDataModel(productId, "Cake", "Super soft cake", new List<IngredientDataModel>()));
_orderStorageContact.Setup(x => x.UpdateElement(It.IsAny<OrderDataModel>())).Verifiable();
// Act
_orderBusinessLogicContract.UpdateOrder(order);
// Assert
_orderStorageContact.Verify(x => x.UpdateElement(It.Is<OrderDataModel>(o => o.Id == order.Id)), Times.Once);
Assert.That(flag);
_orderStorageContact.Verify(x => x.UpdateElement(It.IsAny<OrderDataModel>()), Times.Once);
}
[Test]
public void UpdateOrder_RecordNotFound_ThrowException_Test()
public void UpdateOrder_PekarNotFound_ThrowException_Test()
{
// Arrange
var pekarId = Guid.NewGuid().ToString();
@@ -378,16 +288,13 @@ namespace CandyHouseTests.BusinessLogicsContractsTests
pekarId,
StatusType.Pending
);
_pekarStorageContact.Setup(x => x.GetElementById(pekarId))
.Returns((PekarDataModel)null); // Trigger not found
_orderStorageContact.Setup(x => x.UpdateElement(It.Is<OrderDataModel>(o => o.Id == order.Id)))
.Verifiable();
_pekarStorageContact.Setup(x => x.GetElementById(pekarId)).Returns((PekarDataModel)null);
_productStorageContact.Setup(x => x.GetElementById(productId))
.Returns(new ProductDataModel(productId, "Cake", "Super soft cake", new List<IngredientDataModel>()));
// Act & Assert
Assert.That(() => _orderBusinessLogicContract.UpdateOrder(order),
Throws.TypeOf<ElementNotFoundException>());
_orderStorageContact.Verify(x => x.UpdateElement(It.Is<OrderDataModel>(o => o.Id == order.Id)),
Times.Never);
}
[Test]
@@ -395,14 +302,13 @@ namespace CandyHouseTests.BusinessLogicsContractsTests
{
// Act & Assert
Assert.That(() => _orderBusinessLogicContract.UpdateOrder(null), Throws.TypeOf<ArgumentNullException>());
_orderStorageContact.Verify(x => x.UpdateElement(It.IsAny<OrderDataModel>()), Times.Never);
}
[Test]
public void UpdateOrder_InvalidRecord_ThrowException_Test()
{
// Act & Assert
Assert.That(() => _orderBusinessLogicContract.UpdateOrder(new OrderDataModel(
// Arrange
var order = new OrderDataModel(
"",
"",
DateTime.Now,
@@ -411,8 +317,10 @@ namespace CandyHouseTests.BusinessLogicsContractsTests
"",
"",
StatusType.Pending
)), Throws.TypeOf<ValidationException>());
_orderStorageContact.Verify(x => x.UpdateElement(It.IsAny<OrderDataModel>()), Times.Never);
);
// Act & Assert
Assert.That(() => _orderBusinessLogicContract.UpdateOrder(order), Throws.TypeOf<ValidationException>());
}
[Test]
@@ -435,12 +343,11 @@ namespace CandyHouseTests.BusinessLogicsContractsTests
.Returns(new PekarDataModel(pekarId, "Ivan Ivanov", "Baker", 1.5m, new List<ProductDataModel>()));
_productStorageContact.Setup(x => x.GetElementById(productId))
.Returns(new ProductDataModel(productId, "Cake", "Super soft cake", new List<IngredientDataModel>()));
_orderStorageContact.Setup(x => x.UpdateElement(It.Is<OrderDataModel>(o => o.Id == order.Id)))
_orderStorageContact.Setup(x => x.UpdateElement(It.IsAny<OrderDataModel>()))
.Throws(new StorageException(new InvalidOperationException()));
// Act & Assert
Assert.That(() => _orderBusinessLogicContract.UpdateOrder(order), Throws.TypeOf<StorageException>());
_orderStorageContact.Verify(x => x.UpdateElement(It.Is<OrderDataModel>(o => o.Id == order.Id)), Times.Once);
}
[Test]
@@ -458,20 +365,14 @@ namespace CandyHouseTests.BusinessLogicsContractsTests
Guid.NewGuid().ToString(),
StatusType.Pending
);
var flag = false;
_orderStorageContact.Setup(x => x.GetElementById(id)).Returns(order);
_orderStorageContact.Setup(x => x.DeleteElement(order)).Callback(() => { flag = true; });
_orderStorageContact.Setup(x => x.DeleteElement(order)).Verifiable();
// Act
_orderBusinessLogicContract.DeleteOrder(id);
// Assert
_orderStorageContact.Verify(x => x.DeleteElement(order), Times.Once);
Assert.That(flag);
Assert.That(Guid.TryParse(order.Id, out _), Is.True);
Assert.That(!order.CustomerName.IsEmpty());
Assert.That(order.TotalAmount >= 0);
Assert.That(order.DiscountAmount >= 0);
}
[Test]
@@ -483,7 +384,6 @@ namespace CandyHouseTests.BusinessLogicsContractsTests
// Act & Assert
Assert.That(() => _orderBusinessLogicContract.DeleteOrder(id), Throws.TypeOf<ElementNotFoundException>());
_orderStorageContact.Verify(x => x.GetElementById(id), Times.Once);
}
[Test]
@@ -493,7 +393,6 @@ namespace CandyHouseTests.BusinessLogicsContractsTests
Assert.That(() => _orderBusinessLogicContract.DeleteOrder(null), Throws.TypeOf<ArgumentNullException>());
Assert.That(() => _orderBusinessLogicContract.DeleteOrder(string.Empty),
Throws.TypeOf<ArgumentNullException>());
_orderStorageContact.Verify(x => x.DeleteElement(It.IsAny<OrderDataModel>()), Times.Never);
}
[Test]
@@ -501,7 +400,6 @@ namespace CandyHouseTests.BusinessLogicsContractsTests
{
// Act & Assert
Assert.That(() => _orderBusinessLogicContract.DeleteOrder("invalid"), Throws.TypeOf<ValidationException>());
_orderStorageContact.Verify(x => x.DeleteElement(It.IsAny<OrderDataModel>()), Times.Never);
}
[Test]
@@ -509,16 +407,22 @@ namespace CandyHouseTests.BusinessLogicsContractsTests
{
// Arrange
var id = Guid.NewGuid().ToString();
var order = new OrderDataModel(id, "John Doe", DateTime.Now, 100.50m, 10.50m, Guid.NewGuid().ToString(),
Guid.NewGuid().ToString(), StatusType.Pending);
var order = new OrderDataModel(
id,
"John Doe",
DateTime.Now,
100.50m,
10.50m,
Guid.NewGuid().ToString(),
Guid.NewGuid().ToString(),
StatusType.Pending
);
_orderStorageContact.Setup(x => x.GetElementById(id)).Returns(order);
_orderStorageContact.Setup(x => x.DeleteElement(order))
.Throws(new StorageException(new InvalidOperationException()));
// Act & Assert
Assert.That(() => _orderBusinessLogicContract.DeleteOrder(id), Throws.TypeOf<StorageException>());
_orderStorageContact.Verify(x => x.GetElementById(id), Times.Once);
_orderStorageContact.Verify(x => x.DeleteElement(order), Times.Once);
}
}
}

View File

@@ -1,16 +1,13 @@
using Moq;
using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.Linq;
using CandyHouseBase.DataModels;
using CandyHouseBase.Enums;
using CandyHouseBase.Exceptions;
using CandyHouseBase.Extensions;
using CandyHouseBase.Implementations;
using CandyHouseBase.Interfaces.BusinessLogicsContracts;
using CandyHouseBase.Interfaces.StoragesContracts;
using Microsoft.Extensions.Logging;
namespace CandyHouseTests.BusinessLogicsContractsTests
{
[TestFixture]
@@ -19,15 +16,19 @@ namespace CandyHouseTests.BusinessLogicsContractsTests
private StorageBusinessLogicContract _storageBusinessLogicContract;
private Mock<IStorageStorageContact> _storageStorageContact;
private Mock<IIngredientStorageContact> _ingredientStorageContact;
private Mock<IPekarStorageContact> _pekarStorageContact;
private Mock<IOrderStorageContact> _orderStorageContact;
[OneTimeSetUp]
public void OneTimeSetUp()
{
_storageStorageContact = new Mock<IStorageStorageContact>();
_ingredientStorageContact = new Mock<IIngredientStorageContact>();
_pekarStorageContact = new Mock<IPekarStorageContact>();
_orderStorageContact = new Mock<IOrderStorageContact>();
_storageBusinessLogicContract = new StorageBusinessLogicContract(
_storageStorageContact.Object,
_ingredientStorageContact.Object,
_pekarStorageContact.Object,
new Mock<ILogger>().Object
);
}
@@ -37,30 +38,34 @@ namespace CandyHouseTests.BusinessLogicsContractsTests
{
_storageStorageContact.Reset();
_ingredientStorageContact.Reset();
_pekarStorageContact.Reset();
_orderStorageContact.Reset();
}
[Test]
public void GetAllStorages_ReturnsListOfRecords_Test()
{
// Arrange
var storageId1 = Guid.NewGuid().ToString();
var storageId2 = Guid.NewGuid().ToString();
var storages = new List<StorageDataModel>
{
new StorageDataModel(
Guid.NewGuid().ToString(),
storageId1,
"Fridge 1",
StorageType.Fridge,
new List<IngredientDataModel>
new List<StorageIngredientDataModel>
{
new IngredientDataModel(Guid.NewGuid().ToString(), "Sugar", "kg", 100)
new StorageIngredientDataModel(storageId1, Guid.NewGuid().ToString(), 100)
}
),
new StorageDataModel(
Guid.NewGuid().ToString(),
"Cabninet 1",
storageId2,
"Cabinet 1",
StorageType.Cabinet,
new List<IngredientDataModel>
new List<StorageIngredientDataModel>
{
new IngredientDataModel(Guid.NewGuid().ToString(), "Milk", "l", 200)
new StorageIngredientDataModel(storageId2, Guid.NewGuid().ToString(), 200)
}
)
};
@@ -128,9 +133,9 @@ namespace CandyHouseTests.BusinessLogicsContractsTests
id,
"Fridge 1",
StorageType.Fridge,
new List<IngredientDataModel>
new List<StorageIngredientDataModel>
{
new IngredientDataModel(Guid.NewGuid().ToString(), "Sugar", "kg", 100)
new StorageIngredientDataModel(id, Guid.NewGuid().ToString(), 100)
}
);
_storageStorageContact.Setup(x => x.GetElementById(id)).Returns(storage);
@@ -153,13 +158,14 @@ namespace CandyHouseTests.BusinessLogicsContractsTests
{
// Arrange
var title = "Fridge 1";
var storageId = Guid.NewGuid().ToString();
var storage = new StorageDataModel(
Guid.NewGuid().ToString(),
storageId,
title,
StorageType.Fridge,
new List<IngredientDataModel>
new List<StorageIngredientDataModel>
{
new IngredientDataModel(Guid.NewGuid().ToString(), "Sugar", "kg", 100)
new StorageIngredientDataModel(storageId, Guid.NewGuid().ToString(), 100)
}
);
_storageStorageContact.Setup(x => x.GetElementByTitle(title)).Returns(storage);
@@ -221,13 +227,14 @@ namespace CandyHouseTests.BusinessLogicsContractsTests
{
// Arrange
var flag = false;
var storageId = Guid.NewGuid().ToString();
var storage = new StorageDataModel(
Guid.NewGuid().ToString(),
storageId,
"Fridge 1",
StorageType.Fridge,
new List<IngredientDataModel>
new List<StorageIngredientDataModel>
{
new IngredientDataModel(Guid.NewGuid().ToString(), "Sugar", "kg", 100)
new StorageIngredientDataModel(storageId, Guid.NewGuid().ToString(), 100)
}
);
_storageStorageContact.Setup(x => x.AddElement(It.IsAny<StorageDataModel>()))
@@ -254,13 +261,14 @@ namespace CandyHouseTests.BusinessLogicsContractsTests
public void InsertStorage_RecordWithExistsData_ThrowException_Test()
{
// Arrange
var storageId = Guid.NewGuid().ToString();
var storage = new StorageDataModel(
Guid.NewGuid().ToString(),
storageId,
"Fridge 1",
StorageType.Fridge,
new List<IngredientDataModel>
new List<StorageIngredientDataModel>
{
new IngredientDataModel(Guid.NewGuid().ToString(), "Sugar", "kg", 100)
new StorageIngredientDataModel(storageId, Guid.NewGuid().ToString(), 100)
}
);
_storageStorageContact.Setup(x => x.AddElement(It.IsAny<StorageDataModel>()))
@@ -298,13 +306,14 @@ namespace CandyHouseTests.BusinessLogicsContractsTests
public void InsertStorage_StorageThrowError_ThrowException_Test()
{
// Arrange
var storageId = Guid.NewGuid().ToString();
var storage = new StorageDataModel(
Guid.NewGuid().ToString(),
storageId,
"Fridge 1",
StorageType.Fridge,
new List<IngredientDataModel>
new List<StorageIngredientDataModel>
{
new IngredientDataModel(Guid.NewGuid().ToString(), "Sugar", "kg", 100)
new StorageIngredientDataModel(storageId, Guid.NewGuid().ToString(), 100)
}
);
_storageStorageContact.Setup(x => x.AddElement(It.IsAny<StorageDataModel>()))
@@ -321,13 +330,14 @@ namespace CandyHouseTests.BusinessLogicsContractsTests
{
// Arrange
var flag = false;
var storageId = Guid.NewGuid().ToString();
var storage = new StorageDataModel(
Guid.NewGuid().ToString(),
storageId,
"Fridge 1",
StorageType.Fridge,
new List<IngredientDataModel>
new List<StorageIngredientDataModel>
{
new IngredientDataModel(Guid.NewGuid().ToString(), "Sugar", "kg", 100)
new StorageIngredientDataModel(storageId, Guid.NewGuid().ToString(), 100)
}
);
_storageStorageContact.Setup(x => x.UpdateElement(It.IsAny<StorageDataModel>()))
@@ -354,13 +364,14 @@ namespace CandyHouseTests.BusinessLogicsContractsTests
public void UpdateStorage_RecordNotFound_ThrowException_Test()
{
// Arrange
var storageId = Guid.NewGuid().ToString();
var storage = new StorageDataModel(
Guid.NewGuid().ToString(),
storageId,
"Fridge 1",
StorageType.Fridge,
new List<IngredientDataModel>
new List<StorageIngredientDataModel>
{
new IngredientDataModel(Guid.NewGuid().ToString(), "Sugar", "kg", 100)
new StorageIngredientDataModel(storageId, Guid.NewGuid().ToString(), 100)
}
);
_storageStorageContact.Setup(x => x.UpdateElement(It.IsAny<StorageDataModel>()))
@@ -398,13 +409,14 @@ namespace CandyHouseTests.BusinessLogicsContractsTests
public void UpdateStorage_StorageThrowError_ThrowException_Test()
{
// Arrange
var storageId = Guid.NewGuid().ToString();
var storage = new StorageDataModel(
Guid.NewGuid().ToString(),
storageId,
"Fridge 1",
StorageType.Fridge,
new List<IngredientDataModel>
new List<StorageIngredientDataModel>
{
new IngredientDataModel(Guid.NewGuid().ToString(), "Sugar", "kg", 100)
new StorageIngredientDataModel(storageId, Guid.NewGuid().ToString(), 100)
}
);
_storageStorageContact.Setup(x => x.UpdateElement(It.IsAny<StorageDataModel>()))
@@ -425,9 +437,9 @@ namespace CandyHouseTests.BusinessLogicsContractsTests
id,
"Fridge 1",
StorageType.Fridge,
new List<IngredientDataModel>
new List<StorageIngredientDataModel>
{
new IngredientDataModel(Guid.NewGuid().ToString(), "Sugar", "kg", 100)
new StorageIngredientDataModel(id, Guid.NewGuid().ToString(), 100)
}
);
var flag = false;
@@ -491,9 +503,9 @@ namespace CandyHouseTests.BusinessLogicsContractsTests
id,
"Fridge 1",
StorageType.Fridge,
new List<IngredientDataModel>
new List<StorageIngredientDataModel>
{
new IngredientDataModel(Guid.NewGuid().ToString(), "Sugar", "kg", 100)
new StorageIngredientDataModel(id, Guid.NewGuid().ToString(), 100)
}
);
_storageStorageContact.Setup(x => x.GetElementById(id)).Returns(storage);
@@ -506,5 +518,55 @@ namespace CandyHouseTests.BusinessLogicsContractsTests
_storageStorageContact.Verify(x => x.GetElementById(id), Times.Once);
_storageStorageContact.Verify(x => x.DeleteElement(id), Times.Once);
}
[Test]
public void EnoughIngredients_SufficientIngredients_ReturnsTrue_Test()
{
// Arrange
var order = new OrderDataModel(
Guid.NewGuid().ToString(),
"John Doe",
DateTime.Now,
100.50m,
10.50m,
"",
"",
StatusType.Pending
);
_storageStorageContact.Setup(s => s.ExtractIngredients(order))
.Verifiable();
// Act
_storageBusinessLogicContract.EnoughIngredients(order);
// Assert
_storageStorageContact.Verify(s => s.ExtractIngredients(order), Times.Once);
}
[Test]
public void EnoughIngredients_SufficientIngredients_ReturnsFalse_Test()
{
// Arrange
var orderId = Guid.NewGuid().ToString();
var order = new OrderDataModel(
orderId,
"John Doe",
DateTime.Now,
100.50m,
10.50m,
"",
"",
StatusType.Pending
);
_storageStorageContact.Setup(s => s.ExtractIngredients(order))
.Throws(new StorageException(new Exception("Not enough ingredients")));
// Act & Assert
Assert.That(() => _storageBusinessLogicContract.EnoughIngredients(order),
Throws.TypeOf<StorageException>());
}
}
}

View File

@@ -16,9 +16,9 @@ namespace CandyHouseTests.DataModelsTests
var id = Guid.NewGuid().ToString();
var title = "Main Warehouse";
var storageType = StorageType.Fridge;
var ingredients = new List<IngredientDataModel>
var ingredients = new List<StorageIngredientDataModel>
{
new IngredientDataModel(Guid.NewGuid().ToString(), "Sugar", "kg", 10)
new StorageIngredientDataModel(id, Guid.NewGuid().ToString(), 10)
};
var storage = new StorageDataModel(id, title, storageType, ingredients);
@@ -32,11 +32,15 @@ namespace CandyHouseTests.DataModelsTests
[Test]
public void Validate_ValidData_ShouldNotThrowException()
{
var id = Guid.NewGuid().ToString();
var storage = new StorageDataModel(
Guid.NewGuid().ToString(),
id,
"Main Warehouse",
StorageType.Fridge,
new List<IngredientDataModel>());
new List<StorageIngredientDataModel>
{
new StorageIngredientDataModel(id, Guid.NewGuid().ToString(), 10)
});
Assert.DoesNotThrow(() => storage.Validate());
}
@@ -48,9 +52,10 @@ namespace CandyHouseTests.DataModelsTests
"",
"Main Warehouse",
StorageType.Fridge,
new List<IngredientDataModel>());
new List<StorageIngredientDataModel>());
Assert.Throws<ValidationException>(() => storage.Validate());
var ex = Assert.Throws<ValidationException>(() => storage.Validate());
Assert.AreEqual("Field Id is empty", ex.Message);
}
[Test]
@@ -60,9 +65,10 @@ namespace CandyHouseTests.DataModelsTests
"invalid-guid",
"Main Warehouse",
StorageType.Fridge,
new List<IngredientDataModel>());
new List<StorageIngredientDataModel>());
Assert.Throws<ValidationException>(() => storage.Validate());
var ex = Assert.Throws<ValidationException>(() => storage.Validate());
Assert.AreEqual("Invalid Id format", ex.Message);
}
[Test]
@@ -72,9 +78,10 @@ namespace CandyHouseTests.DataModelsTests
Guid.NewGuid().ToString(),
"",
StorageType.Fridge,
new List<IngredientDataModel>());
new List<StorageIngredientDataModel>());
Assert.Throws<ValidationException>(() => storage.Validate());
var ex = Assert.Throws<ValidationException>(() => storage.Validate());
Assert.AreEqual("Field Title is empty", ex.Message);
}
[Test]
@@ -84,9 +91,23 @@ namespace CandyHouseTests.DataModelsTests
Guid.NewGuid().ToString(),
"Main Warehouse",
(StorageType)999,
new List<IngredientDataModel>());
new List<StorageIngredientDataModel>());
Assert.Throws<ValidationException>(() => storage.Validate());
var ex = Assert.Throws<ValidationException>(() => storage.Validate());
Assert.AreEqual($"Invalid StorageType: {(StorageType)999}", ex.Message);
}
[Test]
public void Validate_NullIngredients_ShouldThrowValidationException()
{
var storage = new StorageDataModel(
Guid.NewGuid().ToString(),
"Main Warehouse",
StorageType.Fridge,
null);
var ex = Assert.Throws<ValidationException>(() => storage.Validate());
Assert.AreEqual("Ingredients is null", ex.Message);
}
}
}