7 Commits

Author SHA1 Message Date
75c5ceabc0 red-line 2025-03-11 21:11:14 +04:00
514f2adefb lab1-hard merged to lab2-hard-no-logic 2025-03-11 20:45:10 +04:00
6ad09c5ff2 Merge branch 'refs/heads/lab-1-hard' into lab-2-hard-no-logic
# Conflicts:
#	CandyHouseSolution/CandyHouseBase/DataModels/StorageDataModel.cs
2025-03-11 20:37:07 +04:00
807ac64a9f fix 2025-03-11 20:36:00 +04:00
ffaeb985e5 Revert "lab1-hard"
This reverts commit b4819145
2025-03-11 20:27:46 +04:00
7b71b14c71 Merge branch 'refs/heads/lab-1-hard' into lab-2-hard-no-logic
# Conflicts:
#	CandyHouseSolution/CandyHouseBase/CandyHouseBase.csproj
2025-03-11 20:26:20 +04:00
b481914563 lab1-hard 2025-02-27 12:01:44 +04:00
19 changed files with 1792 additions and 0 deletions

View File

@@ -114,8 +114,13 @@
<Compile Include="DataModels\ProductDataModel.cs" />
<Compile Include="DataModels\RecipeDataModel.cs" />
<Compile Include="DataModels\SalaryDataModel.cs" />
<Compile Include="DataModels\StorageDataModel.cs" />
<Compile Include="DataModels\StorageIngredientDataModel.cs" />
<Compile Include="DataModels\SupplyDataModel.cs" />
<Compile Include="DataModels\SupplyIngredientDataModel.cs" />
<Compile Include="Enums\PositionType.cs" />
<Compile Include="Enums\StatusType.cs" />
<Compile Include="Enums\StorageType.cs" />
<Compile Include="Exceptions\DateTimeExtensions.cs" />
<Compile Include="Exceptions\ElementExistsException.cs" />
<Compile Include="Exceptions\ElementNotFoundException.cs" />
@@ -130,6 +135,8 @@
<Compile Include="Implementations\PositionBusinessLogicContract.cs" />
<Compile Include="Implementations\ProductBusinessLogicContract.cs" />
<Compile Include="Implementations\SalaryBusinessLogicContract.cs" />
<Compile Include="Implementations\StorageBusinessLogicContract.cs" />
<Compile Include="Implementations\SupplyBusinessLogicContract.cs" />
<Compile Include="Infrastructure\IValidation.cs" />
<Compile Include="Interfaces\BusinessLogicsContracts\IIngredientBusinessLogicContact.cs" />
<Compile Include="Interfaces\BusinessLogicsContracts\IOrderBusinessLogicContact.cs" />
@@ -137,12 +144,16 @@
<Compile Include="Interfaces\BusinessLogicsContracts\IPositionBusinessLogicContact.cs" />
<Compile Include="Interfaces\BusinessLogicsContracts\IProductBusinessLogicContact.cs" />
<Compile Include="Interfaces\BusinessLogicsContracts\ISalaryBusinessLogicContact.cs" />
<Compile Include="Interfaces\BusinessLogicsContracts\IStorageBusinessLogicContact.cs" />
<Compile Include="Interfaces\BusinessLogicsContracts\ISupplyBusinessLogicContact.cs" />
<Compile Include="Interfaces\StoragesContracts\IIngredientStorageContact.cs" />
<Compile Include="Interfaces\StoragesContracts\IOrderStorageContact.cs" />
<Compile Include="Interfaces\StoragesContracts\IPekarStorageContact.cs" />
<Compile Include="Interfaces\StoragesContracts\IPositionStorageContact.cs" />
<Compile Include="Interfaces\StoragesContracts\IProductStorageContact.cs" />
<Compile Include="Interfaces\StoragesContracts\ISalaryStorageContact.cs" />
<Compile Include="Interfaces\StoragesContracts\IStorageStorageContact.cs" />
<Compile Include="Interfaces\StoragesContracts\ISupplyStorageContact.cs" />
<Compile Include="Program.cs" />
</ItemGroup>
<ItemGroup>

View File

@@ -0,0 +1,35 @@
using System;
using System.Collections.Generic;
using CandyHouseBase.Enums;
using CandyHouseBase.Exceptions;
using CandyHouseBase.Extensions;
using CandyHouseBase.Infrastructure;
namespace CandyHouseBase.DataModels
{
public class StorageDataModel : IValidation
{
public string Id { get; set; }
public string Title { get; set; }
public StorageType StorageType { get; set; }
public List<IngredientDataModel> Ingredients { get; set; }
public StorageDataModel(string id, string title, StorageType storageType, List<IngredientDataModel> ingredients)
{
Id = id;
Title = title;
StorageType = storageType;
Ingredients = ingredients;
}
public void Validate()
{
if (Id.IsEmpty()) throw new ValidationException("Field Id is empty");
if (!Id.IsGuid()) throw new ValidationException("Invalid Id format");
if (Title.IsEmpty()) throw new ValidationException("Field Title is empty");
if (!Enum.IsDefined(typeof(StorageType), StorageType))
throw new ValidationException($"Invalid StorageType: {StorageType}");
if (Ingredients == null) throw new ValidationException($"Ingredients is null");
}
}
}

View File

@@ -0,0 +1,29 @@
using CandyHouseBase.Exceptions;
using CandyHouseBase.Extensions;
using CandyHouseBase.Infrastructure;
namespace CandyHouseBase.DataModels
{
public class StorageIngredientDataModel : IValidation
{
public string StorageId { get; set; }
public string IngredientId { get; set; }
public int Quantity { get; set; }
public StorageIngredientDataModel(string storageId, string ingredientId, int quantity)
{
StorageId = storageId;
IngredientId = ingredientId;
Quantity = quantity;
}
public void Validate()
{
if (StorageId.IsEmpty()) throw new ValidationException("Field Id is empty");
if (!StorageId.IsGuid()) throw new ValidationException("Field Id is not a valid GUID");
if (IngredientId.IsEmpty()) throw new ValidationException("Field IngredientId is empty");
if (!IngredientId.IsGuid()) throw new ValidationException("Field IngredientId is not a valid GUID");
if (Quantity <= 0) throw new ValidationException("Field Quantity must be greater than zero");
}
}
}

View File

@@ -0,0 +1,39 @@
using System;
using System.Collections.Generic;
using CandyHouseBase.Exceptions;
using CandyHouseBase.Extensions;
using CandyHouseBase.Infrastructure;
namespace CandyHouseBase.DataModels
{
public class SupplyDataModel : IValidation
{
public string Id { get; set; }
public DateTime Date { get; set; }
public int TotalQuantity { get; set; }
public decimal TotalPrice { get; set; }
public List<IngredientDataModel> Ingredients { get; set; }
public SupplyDataModel(string id, DateTime date, int quantity, decimal totalPrice,
List<IngredientDataModel> ingredients)
{
Id = id;
Date = date;
TotalQuantity = quantity;
TotalPrice = totalPrice;
Ingredients = ingredients;
}
public void Validate()
{
if (Id.IsEmpty()) throw new ValidationException("Supply ID cannot be null or empty");
if (!Id.IsGuid()) throw new ValidationException("Supply ID must be a valid GUID");
if (Date == default) throw new ValidationException("Supply date cannot be null");
if (TotalQuantity <= 0) throw new ValidationException("Supply quantity must be greater than zero");
if (TotalPrice < 0) throw new ValidationException("Supply total price cannot be negative");
if (Ingredients == null || Ingredients.Count == 0)
throw new ValidationException("Supply must have at least one ingredient");
}
}
}

View File

@@ -0,0 +1,29 @@
using CandyHouseBase.Exceptions;
using CandyHouseBase.Extensions;
using CandyHouseBase.Infrastructure;
namespace CandyHouseBase.DataModels
{
public class SupplyIngredientDataModel : IValidation
{
public string SupplyId { get; set; }
public string IngredientId { get; set; }
public int Quantity { get; set; }
public SupplyIngredientDataModel(string supplyId, string ingredientId, int quantity)
{
SupplyId = supplyId;
IngredientId = ingredientId;
Quantity = quantity;
}
public void Validate()
{
if (SupplyId.IsEmpty()) throw new ValidationException("Supply ID cannot be null or empty");
if (!SupplyId.IsGuid()) throw new ValidationException("Supply ID is not a valid GUID");
if (IngredientId.IsEmpty()) throw new ValidationException("Ingredient ID cannot be null or empty");
if (!IngredientId.IsGuid()) throw new ValidationException("Ingredient ID is not a valid GUID");
if (Quantity <= 0) throw new ValidationException("Quantity must be a positive number");
}
}
}

View File

@@ -0,0 +1,9 @@
namespace CandyHouseBase.Enums
{
public enum StorageType
{
Fridge,
Shelf,
Cabinet
}
}

View File

@@ -0,0 +1,56 @@
using System.Collections.Generic;
using CandyHouseBase.DataModels;
using CandyHouseBase.Extensions;
using CandyHouseBase.Interfaces.StoragesContracts;
using Microsoft.Extensions.Logging;
namespace CandyHouseBase.Implementations;
internal class StorageBusinessLogicContract
{
private readonly IStorageStorageContact _storageStorageContact;
private readonly IIngredientStorageContact _ingredientStorageContact;
private readonly ILogger _logger;
public StorageBusinessLogicContract(
IStorageStorageContact storageStorageContact,
IIngredientStorageContact ingredientStorageContact,
ILogger logger)
{
_storageStorageContact = storageStorageContact;
_ingredientStorageContact = ingredientStorageContact;
_logger = logger;
}
public List<StorageDataModel> GetAllStorages()
{
return new List<StorageDataModel>();
}
public StorageDataModel GetStorageByData(string data)
{
if (data.IsGuid())
{
return new StorageDataModel("", "", CandyHouseBase.Enums.StorageType.Cabinet,
new List<IngredientDataModel>());
}
else
{
// If it's not a GUID, assume it's a title
return new StorageDataModel("", data, CandyHouseBase.Enums.StorageType.Cabinet,
new List<IngredientDataModel>());
}
}
public void InsertStorage(StorageDataModel storage)
{
}
public void UpdateStorage(StorageDataModel storage)
{
}
public void DeleteStorage(string id)
{
}
}

View File

@@ -0,0 +1,46 @@
using System;
using System.Collections.Generic;
using CandyHouseBase.DataModels;
using CandyHouseBase.Interfaces.StoragesContracts;
using Microsoft.Extensions.Logging;
namespace CandyHouseBase.Implementations;
internal class SupplyBusinessLogicContract
{
private readonly ISupplyStorageContact _supplyStorageContact;
private readonly IIngredientStorageContact _ingredientStorageContact;
private readonly ILogger _logger;
public SupplyBusinessLogicContract(
ISupplyStorageContact supplyStorageContact,
IIngredientStorageContact ingredientStorageContact,
ILogger logger)
{
_supplyStorageContact = supplyStorageContact;
_ingredientStorageContact = ingredientStorageContact;
_logger = logger;
}
public List<SupplyDataModel> GetAllSupplies()
{
return new List<SupplyDataModel>();
}
public SupplyDataModel GetSupplyByData(string data)
{
return new SupplyDataModel("", new DateTime(), 0, 0, new List<IngredientDataModel>());
}
public void InsertSupply(SupplyDataModel supply)
{
}
public void UpdateSupply(SupplyDataModel supply)
{
}
public void DeleteSupply(string id)
{
}
}

View File

@@ -0,0 +1,13 @@
using System.Collections.Generic;
using CandyHouseBase.DataModels;
namespace CandyHouseBase.Interfaces.BusinessLogicsContracts;
public interface IStorageBusinessLogicContact
{
List<StorageDataModel> GetAllStorages();
StorageDataModel GetStorageByData(string data);
void InsertStorage(StorageDataModel storage);
void UpdateStorage(StorageDataModel storage);
void DeleteStorage(string id);
}

View File

@@ -0,0 +1,13 @@
using System.Collections.Generic;
using CandyHouseBase.DataModels;
namespace CandyHouseBase.Interfaces.BusinessLogicsContracts;
public interface ISupplyBusinessLogicContact
{
List<SupplyDataModel> GetAllSupplies();
SupplyDataModel GetSupplyByData(string data);
void InsertSupply(SupplyDataModel supply);
void UpdateSupply(SupplyDataModel supply);
void DeleteSupply(string id);
}

View File

@@ -0,0 +1,14 @@
using System.Collections.Generic;
using CandyHouseBase.DataModels;
namespace CandyHouseBase.Interfaces.StoragesContracts;
public interface IStorageStorageContact
{
List<StorageDataModel> GetList();
StorageDataModel GetElementById(string id);
StorageDataModel GetElementByTitle(string title);
void AddElement(StorageDataModel element);
void UpdateElement(StorageDataModel element);
void DeleteElement(string id);
}

View File

@@ -0,0 +1,13 @@
using System.Collections.Generic;
using CandyHouseBase.DataModels;
namespace CandyHouseBase.Interfaces.StoragesContracts;
public interface ISupplyStorageContact
{
List<SupplyDataModel> GetList();
SupplyDataModel GetElementById(string id);
void AddElement(SupplyDataModel element);
void UpdateElement(SupplyDataModel element);
void DeleteElement(string id);
}

View File

@@ -0,0 +1,510 @@
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]
internal class StorageBusinessLogicContractTests
{
private StorageBusinessLogicContract _storageBusinessLogicContract;
private Mock<IStorageStorageContact> _storageStorageContact;
private Mock<IIngredientStorageContact> _ingredientStorageContact;
[OneTimeSetUp]
public void OneTimeSetUp()
{
_storageStorageContact = new Mock<IStorageStorageContact>();
_ingredientStorageContact = new Mock<IIngredientStorageContact>();
_storageBusinessLogicContract = new StorageBusinessLogicContract(
_storageStorageContact.Object,
_ingredientStorageContact.Object,
new Mock<ILogger>().Object
);
}
[SetUp]
public void SetUp()
{
_storageStorageContact.Reset();
_ingredientStorageContact.Reset();
}
[Test]
public void GetAllStorages_ReturnsListOfRecords_Test()
{
// Arrange
var storages = new List<StorageDataModel>
{
new StorageDataModel(
Guid.NewGuid().ToString(),
"Fridge 1",
StorageType.Fridge,
new List<IngredientDataModel>
{
new IngredientDataModel(Guid.NewGuid().ToString(), "Sugar", "kg", 100)
}
),
new StorageDataModel(
Guid.NewGuid().ToString(),
"Cabninet 1",
StorageType.Cabinet,
new List<IngredientDataModel>
{
new IngredientDataModel(Guid.NewGuid().ToString(), "Milk", "l", 200)
}
)
};
_storageStorageContact.Setup(x => x.GetList()).Returns(storages);
// Act
var list = _storageBusinessLogicContract.GetAllStorages();
// Assert
Assert.That(list, Is.Not.Null);
Assert.That(list, Is.EquivalentTo(storages));
Assert.That(
list.All(s =>
Guid.TryParse(s.Id, out _) &&
!s.Title.IsEmpty() &&
Enum.IsDefined(typeof(StorageType), s.StorageType) &&
s.Ingredients != null),
Is.True);
}
[Test]
public void GetAllStorages_ReturnsEmptyList_Test()
{
// Arrange
_storageStorageContact.Setup(x => x.GetList()).Returns(new List<StorageDataModel>());
// Act
var list = _storageBusinessLogicContract.GetAllStorages();
// Assert
Assert.That(list, Is.Not.Null);
Assert.That(list, Has.Count.EqualTo(0));
_storageStorageContact.Verify(x => x.GetList(), Times.Once);
}
[Test]
public void GetAllStorages_ReturnsNull_ThrowException_Test()
{
// Arrange
_storageStorageContact.Setup(x => x.GetList()).Returns((List<StorageDataModel>)null);
// Act & Assert
Assert.That(() => _storageBusinessLogicContract.GetAllStorages(), Throws.TypeOf<NullListException>());
_storageStorageContact.Verify(x => x.GetList(), Times.Once);
}
[Test]
public void GetAllStorages_StorageThrowError_ThrowException_Test()
{
// Arrange
_storageStorageContact.Setup(x => x.GetList())
.Throws(new StorageException(new InvalidOperationException()));
// Act & Assert
Assert.That(() => _storageBusinessLogicContract.GetAllStorages(), Throws.TypeOf<StorageException>());
_storageStorageContact.Verify(x => x.GetList(), Times.Once);
}
[Test]
public void GetStorageByData_ReturnsStorageById_Test()
{
// Arrange
var id = Guid.NewGuid().ToString();
var storage = new StorageDataModel(
id,
"Fridge 1",
StorageType.Fridge,
new List<IngredientDataModel>
{
new IngredientDataModel(Guid.NewGuid().ToString(), "Sugar", "kg", 100)
}
);
_storageStorageContact.Setup(x => x.GetElementById(id)).Returns(storage);
// Act
var element = _storageBusinessLogicContract.GetStorageByData(id);
// Assert
Assert.That(element, Is.Not.Null);
Assert.That(element.Id, Is.EqualTo(id));
Assert.That(Guid.TryParse(element.Id, out _), Is.True);
Assert.That(!element.Title.IsEmpty());
Assert.That(Enum.IsDefined(typeof(StorageType), element.StorageType), Is.True);
Assert.That(element.Ingredients, Is.Not.Null);
_storageStorageContact.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Once);
}
[Test]
public void GetStorageByData_ReturnsStorageByTitle_Test()
{
// Arrange
var title = "Fridge 1";
var storage = new StorageDataModel(
Guid.NewGuid().ToString(),
title,
StorageType.Fridge,
new List<IngredientDataModel>
{
new IngredientDataModel(Guid.NewGuid().ToString(), "Sugar", "kg", 100)
}
);
_storageStorageContact.Setup(x => x.GetElementByTitle(title)).Returns(storage);
// Act
var element = _storageBusinessLogicContract.GetStorageByData(title);
// Assert
Assert.That(element, Is.Not.Null);
Assert.That(element.Title, Is.EqualTo(title));
Assert.That(Guid.TryParse(element.Id, out _), Is.True);
Assert.That(Enum.IsDefined(typeof(StorageType), element.StorageType), Is.True);
Assert.That(element.Ingredients, Is.Not.Null);
_storageStorageContact.Verify(x => x.GetElementByTitle(It.IsAny<string>()), Times.Once);
}
[Test]
public void GetStorageByData_EmptyData_ThrowException_Test()
{
// Act & Assert
Assert.That(() => _storageBusinessLogicContract.GetStorageByData(null),
Throws.TypeOf<ArgumentNullException>());
Assert.That(() => _storageBusinessLogicContract.GetStorageByData(string.Empty),
Throws.TypeOf<ArgumentNullException>());
_storageStorageContact.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Never);
_storageStorageContact.Verify(x => x.GetElementByTitle(It.IsAny<string>()), Times.Never);
}
[Test]
public void GetStorageByData_NotFoundStorage_ThrowException_Test()
{
// Arrange
var id = Guid.NewGuid().ToString();
_storageStorageContact.Setup(x => x.GetElementById(id)).Returns((StorageDataModel)null);
_storageStorageContact.Setup(x => x.GetElementByTitle(id)).Returns((StorageDataModel)null);
// Act & Assert
Assert.That(() => _storageBusinessLogicContract.GetStorageByData(id),
Throws.TypeOf<ElementNotFoundException>());
_storageStorageContact.Verify(x => x.GetElementById(id), Times.Once);
_storageStorageContact.Verify(x => x.GetElementByTitle(id), Times.Once);
}
[Test]
public void GetStorageByData_StorageThrowError_ThrowException_Test()
{
// Arrange
_storageStorageContact.Setup(x => x.GetElementById(It.IsAny<string>()))
.Throws(new StorageException(new InvalidOperationException()));
// Act & Assert
Assert.That(() => _storageBusinessLogicContract.GetStorageByData(Guid.NewGuid().ToString()),
Throws.TypeOf<StorageException>());
_storageStorageContact.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Once);
}
[Test]
public void InsertStorage_CorrectRecord_Test()
{
// Arrange
var flag = false;
var storage = new StorageDataModel(
Guid.NewGuid().ToString(),
"Fridge 1",
StorageType.Fridge,
new List<IngredientDataModel>
{
new IngredientDataModel(Guid.NewGuid().ToString(), "Sugar", "kg", 100)
}
);
_storageStorageContact.Setup(x => x.AddElement(It.IsAny<StorageDataModel>()))
.Callback((StorageDataModel x) =>
{
flag = x.Id == storage.Id && x.Title == storage.Title &&
x.StorageType == storage.StorageType &&
x.Ingredients.SequenceEqual(storage.Ingredients);
});
// Act
_storageBusinessLogicContract.InsertStorage(storage);
// Assert
_storageStorageContact.Verify(x => x.AddElement(It.IsAny<StorageDataModel>()), Times.Once);
Assert.That(flag);
Assert.That(Guid.TryParse(storage.Id, out _), Is.True);
Assert.That(!storage.Title.IsEmpty());
Assert.That(Enum.IsDefined(typeof(StorageType), storage.StorageType), Is.True);
Assert.That(storage.Ingredients, Is.Not.Null);
}
[Test]
public void InsertStorage_RecordWithExistsData_ThrowException_Test()
{
// Arrange
var storage = new StorageDataModel(
Guid.NewGuid().ToString(),
"Fridge 1",
StorageType.Fridge,
new List<IngredientDataModel>
{
new IngredientDataModel(Guid.NewGuid().ToString(), "Sugar", "kg", 100)
}
);
_storageStorageContact.Setup(x => x.AddElement(It.IsAny<StorageDataModel>()))
.Throws(new ElementExistsException("ID", storage.Id));
// Act & Assert
Assert.That(() => _storageBusinessLogicContract.InsertStorage(storage),
Throws.TypeOf<ElementExistsException>());
_storageStorageContact.Verify(x => x.AddElement(It.IsAny<StorageDataModel>()), Times.Once);
}
[Test]
public void InsertStorage_NullRecord_ThrowException_Test()
{
// Act & Assert
Assert.That(() => _storageBusinessLogicContract.InsertStorage(null),
Throws.TypeOf<ArgumentNullException>());
_storageStorageContact.Verify(x => x.AddElement(It.IsAny<StorageDataModel>()), Times.Never);
}
[Test]
public void InsertStorage_InvalidRecord_ThrowException_Test()
{
// Act & Assert
Assert.That(() => _storageBusinessLogicContract.InsertStorage(new StorageDataModel(
"", // Invalid ID
"", // Invalid Title
(StorageType)999, // Invalid StorageType
null // Invalid Ingredients
)), Throws.TypeOf<ValidationException>());
_storageStorageContact.Verify(x => x.AddElement(It.IsAny<StorageDataModel>()), Times.Never);
}
[Test]
public void InsertStorage_StorageThrowError_ThrowException_Test()
{
// Arrange
var storage = new StorageDataModel(
Guid.NewGuid().ToString(),
"Fridge 1",
StorageType.Fridge,
new List<IngredientDataModel>
{
new IngredientDataModel(Guid.NewGuid().ToString(), "Sugar", "kg", 100)
}
);
_storageStorageContact.Setup(x => x.AddElement(It.IsAny<StorageDataModel>()))
.Throws(new StorageException(new InvalidOperationException()));
// Act & Assert
Assert.That(() => _storageBusinessLogicContract.InsertStorage(storage),
Throws.TypeOf<StorageException>());
_storageStorageContact.Verify(x => x.AddElement(It.IsAny<StorageDataModel>()), Times.Once);
}
[Test]
public void UpdateStorage_CorrectRecord_Test()
{
// Arrange
var flag = false;
var storage = new StorageDataModel(
Guid.NewGuid().ToString(),
"Fridge 1",
StorageType.Fridge,
new List<IngredientDataModel>
{
new IngredientDataModel(Guid.NewGuid().ToString(), "Sugar", "kg", 100)
}
);
_storageStorageContact.Setup(x => x.UpdateElement(It.IsAny<StorageDataModel>()))
.Callback((StorageDataModel x) =>
{
flag = x.Id == storage.Id && x.Title == storage.Title &&
x.StorageType == storage.StorageType &&
x.Ingredients.SequenceEqual(storage.Ingredients);
});
// Act
_storageBusinessLogicContract.UpdateStorage(storage);
// Assert
_storageStorageContact.Verify(x => x.UpdateElement(It.IsAny<StorageDataModel>()), Times.Once);
Assert.That(flag);
Assert.That(Guid.TryParse(storage.Id, out _), Is.True);
Assert.That(!storage.Title.IsEmpty());
Assert.That(Enum.IsDefined(typeof(StorageType), storage.StorageType), Is.True);
Assert.That(storage.Ingredients, Is.Not.Null);
}
[Test]
public void UpdateStorage_RecordNotFound_ThrowException_Test()
{
// Arrange
var storage = new StorageDataModel(
Guid.NewGuid().ToString(),
"Fridge 1",
StorageType.Fridge,
new List<IngredientDataModel>
{
new IngredientDataModel(Guid.NewGuid().ToString(), "Sugar", "kg", 100)
}
);
_storageStorageContact.Setup(x => x.UpdateElement(It.IsAny<StorageDataModel>()))
.Throws(new ElementNotFoundException("Storage not found"));
// Act & Assert
Assert.That(() => _storageBusinessLogicContract.UpdateStorage(storage),
Throws.TypeOf<ElementNotFoundException>());
_storageStorageContact.Verify(x => x.UpdateElement(It.IsAny<StorageDataModel>()), Times.Once);
}
[Test]
public void UpdateStorage_NullRecord_ThrowException_Test()
{
// Act & Assert
Assert.That(() => _storageBusinessLogicContract.UpdateStorage(null),
Throws.TypeOf<ArgumentNullException>());
_storageStorageContact.Verify(x => x.UpdateElement(It.IsAny<StorageDataModel>()), Times.Never);
}
[Test]
public void UpdateStorage_InvalidRecord_ThrowException_Test()
{
// Act & Assert
Assert.That(() => _storageBusinessLogicContract.UpdateStorage(new StorageDataModel(
"", // Invalid ID
"", // Invalid Title
(StorageType)999, // Invalid StorageType
null // Invalid Ingredients
)), Throws.TypeOf<ValidationException>());
_storageStorageContact.Verify(x => x.UpdateElement(It.IsAny<StorageDataModel>()), Times.Never);
}
[Test]
public void UpdateStorage_StorageThrowError_ThrowException_Test()
{
// Arrange
var storage = new StorageDataModel(
Guid.NewGuid().ToString(),
"Fridge 1",
StorageType.Fridge,
new List<IngredientDataModel>
{
new IngredientDataModel(Guid.NewGuid().ToString(), "Sugar", "kg", 100)
}
);
_storageStorageContact.Setup(x => x.UpdateElement(It.IsAny<StorageDataModel>()))
.Throws(new StorageException(new InvalidOperationException()));
// Act & Assert
Assert.That(() => _storageBusinessLogicContract.UpdateStorage(storage),
Throws.TypeOf<StorageException>());
_storageStorageContact.Verify(x => x.UpdateElement(It.IsAny<StorageDataModel>()), Times.Once);
}
[Test]
public void DeleteStorage_CorrectId_Test()
{
// Arrange
var id = Guid.NewGuid().ToString();
var storage = new StorageDataModel(
id,
"Fridge 1",
StorageType.Fridge,
new List<IngredientDataModel>
{
new IngredientDataModel(Guid.NewGuid().ToString(), "Sugar", "kg", 100)
}
);
var flag = false;
_storageStorageContact.Setup(x => x.GetElementById(id)).Returns(storage);
_storageStorageContact.Setup(x => x.DeleteElement(id)).Callback(() => { flag = true; });
// Act
_storageBusinessLogicContract.DeleteStorage(id);
// Assert
_storageStorageContact.Verify(x => x.DeleteElement(id), Times.Once);
Assert.That(flag);
Assert.That(Guid.TryParse(storage.Id, out _), Is.True);
Assert.That(!storage.Title.IsEmpty());
Assert.That(Enum.IsDefined(typeof(StorageType), storage.StorageType), Is.True);
Assert.That(storage.Ingredients, Is.Not.Null);
}
[Test]
public void DeleteStorage_RecordNotFound_ThrowException_Test()
{
// Arrange
var id = Guid.NewGuid().ToString();
_storageStorageContact.Setup(x => x.GetElementById(id)).Returns((StorageDataModel)null);
// Act & Assert
Assert.That(() => _storageBusinessLogicContract.DeleteStorage(id),
Throws.TypeOf<ElementNotFoundException>());
_storageStorageContact.Verify(x => x.GetElementById(id), Times.Once);
}
[Test]
public void DeleteStorage_NullOrEmptyId_ThrowException_Test()
{
// Act & Assert
Assert.That(() => _storageBusinessLogicContract.DeleteStorage(null),
Throws.TypeOf<ArgumentNullException>());
Assert.That(() => _storageBusinessLogicContract.DeleteStorage(string.Empty),
Throws.TypeOf<ArgumentNullException>());
_storageStorageContact.Verify(x => x.DeleteElement(It.IsAny<string>()), Times.Never);
}
[Test]
public void DeleteStorage_InvalidId_ThrowException_Test()
{
// Arrange
var id = "invalid-id";
// Act & Assert
Assert.That(() => _storageBusinessLogicContract.DeleteStorage(id),
Throws.TypeOf<ValidationException>());
_storageStorageContact.Verify(x => x.GetElementById(id), Times.Never);
}
[Test]
public void DeleteStorage_StorageThrowError_ThrowException_Test()
{
// Arrange
var id = Guid.NewGuid().ToString();
var storage = new StorageDataModel(
id,
"Fridge 1",
StorageType.Fridge,
new List<IngredientDataModel>
{
new IngredientDataModel(Guid.NewGuid().ToString(), "Sugar", "kg", 100)
}
);
_storageStorageContact.Setup(x => x.GetElementById(id)).Returns(storage);
_storageStorageContact.Setup(x => x.DeleteElement(id))
.Throws(new StorageException(new InvalidOperationException()));
// Act & Assert
Assert.That(() => _storageBusinessLogicContract.DeleteStorage(id),
Throws.TypeOf<StorageException>());
_storageStorageContact.Verify(x => x.GetElementById(id), Times.Once);
_storageStorageContact.Verify(x => x.DeleteElement(id), Times.Once);
}
}
}

View File

@@ -0,0 +1,518 @@
using Moq;
using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.Linq;
using CandyHouseBase.DataModels;
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]
internal class SupplyBusinessLogicContractTests
{
private SupplyBusinessLogicContract _supplyBusinessLogicContract;
private Mock<ISupplyStorageContact> _supplyStorageContact;
private Mock<IIngredientStorageContact> _ingredientStorageContact;
[OneTimeSetUp]
public void OneTimeSetUp()
{
_supplyStorageContact = new Mock<ISupplyStorageContact>();
_ingredientStorageContact = new Mock<IIngredientStorageContact>();
_supplyBusinessLogicContract = new SupplyBusinessLogicContract(
_supplyStorageContact.Object,
_ingredientStorageContact.Object,
new Mock<ILogger>().Object
);
}
[SetUp]
public void SetUp()
{
_supplyStorageContact.Reset();
_ingredientStorageContact.Reset();
}
[Test]
public void GetAllSupplies_ReturnsListOfRecords_Test()
{
// Arrange
var supplies = new List<SupplyDataModel>
{
new SupplyDataModel(
Guid.NewGuid().ToString(),
DateTime.Now.AddDays(-7),
100,
1000.00m,
new List<IngredientDataModel>
{
new IngredientDataModel(Guid.NewGuid().ToString(), "Sugar", "kg", 50.00m)
}
),
new SupplyDataModel(
Guid.NewGuid().ToString(),
DateTime.Now,
200,
2000.00m,
new List<IngredientDataModel>
{
new IngredientDataModel(Guid.NewGuid().ToString(), "Flour", "kg", 30.00m)
}
)
};
_supplyStorageContact.Setup(x => x.GetList()).Returns(supplies);
// Act
var list = _supplyBusinessLogicContract.GetAllSupplies();
// Assert
Assert.That(list, Is.Not.Null);
Assert.That(list, Is.EquivalentTo(supplies));
Assert.That(
list.All(s =>
Guid.TryParse(s.Id, out _) &&
s.Date != default &&
s.TotalQuantity > 0 &&
s.TotalPrice >= 0 &&
s.Ingredients != null &&
s.Ingredients.Count > 0),
Is.True);
}
[Test]
public void GetAllSupplies_ReturnsEmptyList_Test()
{
// Arrange
_supplyStorageContact.Setup(x => x.GetList()).Returns(new List<SupplyDataModel>());
// Act
var list = _supplyBusinessLogicContract.GetAllSupplies();
// Assert
Assert.That(list, Is.Not.Null);
Assert.That(list, Has.Count.EqualTo(0));
_supplyStorageContact.Verify(x => x.GetList(), Times.Once);
}
[Test]
public void GetAllSupplies_ReturnsNull_ThrowException_Test()
{
// Arrange
_supplyStorageContact.Setup(x => x.GetList()).Returns((List<SupplyDataModel>)null);
// Act & Assert
Assert.That(() => _supplyBusinessLogicContract.GetAllSupplies(), Throws.TypeOf<NullListException>());
_supplyStorageContact.Verify(x => x.GetList(), Times.Once);
}
[Test]
public void GetAllSupplies_StorageThrowError_ThrowException_Test()
{
// Arrange
_supplyStorageContact.Setup(x => x.GetList())
.Throws(new StorageException(new InvalidOperationException()));
// Act & Assert
Assert.That(() => _supplyBusinessLogicContract.GetAllSupplies(), Throws.TypeOf<StorageException>());
_supplyStorageContact.Verify(x => x.GetList(), Times.Once);
}
[Test]
public void GetSupplyByData_ReturnsSupplyById_Test()
{
// Arrange
var id = Guid.NewGuid().ToString();
var supply = new SupplyDataModel(
id,
DateTime.Now,
100,
1000.00m,
new List<IngredientDataModel>
{
new IngredientDataModel(Guid.NewGuid().ToString(), "Sugar", "kg", 50.00m)
}
);
_supplyStorageContact.Setup(x => x.GetElementById(id)).Returns(supply);
// Act
var element = _supplyBusinessLogicContract.GetSupplyByData(id);
// Assert
Assert.That(element, Is.Not.Null);
Assert.That(element.Id, Is.EqualTo(id));
Assert.That(Guid.TryParse(element.Id, out _), Is.True);
Assert.That(element.Date, Is.Not.EqualTo(default(DateTime)));
Assert.That(element.TotalQuantity, Is.GreaterThan(0));
Assert.That(element.TotalPrice, Is.GreaterThanOrEqualTo(0));
Assert.That(element.Ingredients, Is.Not.Null);
Assert.That(element.Ingredients.Count, Is.GreaterThan(0));
_supplyStorageContact.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Once);
}
[Test]
public void GetSupplyByData_EmptyData_ThrowException_Test()
{
// Act & Assert
Assert.That(() => _supplyBusinessLogicContract.GetSupplyByData(null),
Throws.TypeOf<ArgumentNullException>());
Assert.That(() => _supplyBusinessLogicContract.GetSupplyByData(string.Empty),
Throws.TypeOf<ArgumentNullException>());
_supplyStorageContact.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Never);
}
[Test]
public void GetSupplyByData_NotFoundSupply_ThrowException_Test()
{
// Arrange
var id = Guid.NewGuid().ToString();
_supplyStorageContact.Setup(x => x.GetElementById(id)).Returns((SupplyDataModel)null);
// Act & Assert
Assert.That(() => _supplyBusinessLogicContract.GetSupplyByData(id),
Throws.TypeOf<ElementNotFoundException>());
_supplyStorageContact.Verify(x => x.GetElementById(id), Times.Once);
}
[Test]
public void GetSupplyByData_StorageThrowError_ThrowException_Test()
{
// Arrange
_supplyStorageContact.Setup(x => x.GetElementById(It.IsAny<string>()))
.Throws(new StorageException(new InvalidOperationException()));
// Act & Assert
Assert.That(() => _supplyBusinessLogicContract.GetSupplyByData(Guid.NewGuid().ToString()),
Throws.TypeOf<StorageException>());
_supplyStorageContact.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Once);
}
[Test]
public void InsertSupply_CorrectRecord_Test()
{
// Arrange
var flag = false;
var ingredientId = Guid.NewGuid().ToString();
var supply = new SupplyDataModel(
Guid.NewGuid().ToString(),
DateTime.Now,
100,
1000.00m,
new List<IngredientDataModel>
{
new IngredientDataModel(ingredientId, "Sugar", "kg", 50.00m)
}
);
_supplyStorageContact.Setup(x => x.AddElement(It.IsAny<SupplyDataModel>()))
.Callback((SupplyDataModel x) =>
{
flag = x.Id == supply.Id &&
x.Date == supply.Date &&
x.TotalQuantity == supply.TotalQuantity &&
x.TotalPrice == supply.TotalPrice &&
x.Ingredients.SequenceEqual(supply.Ingredients);
});
_ingredientStorageContact.Setup(x => x.GetElementById(ingredientId)).Returns(supply.Ingredients[0]);
// Act
_supplyBusinessLogicContract.InsertSupply(supply);
// Assert
_supplyStorageContact.Verify(x => x.AddElement(It.IsAny<SupplyDataModel>()), Times.Once);
Assert.That(flag);
Assert.That(Guid.TryParse(supply.Id, out _), Is.True);
Assert.That(supply.Date, Is.Not.EqualTo(default(DateTime)));
Assert.That(supply.TotalQuantity, Is.GreaterThan(0));
Assert.That(supply.TotalPrice, Is.GreaterThanOrEqualTo(0));
Assert.That(supply.Ingredients, Is.Not.Null);
Assert.That(supply.Ingredients.Count, Is.GreaterThan(0));
}
[Test]
public void InsertSupply_RecordWithExistsData_ThrowException_Test()
{
// Arrange
var ingredientId = Guid.NewGuid().ToString();
var supply = new SupplyDataModel(
Guid.NewGuid().ToString(),
DateTime.Now,
100,
1000.00m,
new List<IngredientDataModel>
{
new IngredientDataModel(ingredientId, "Sugar", "kg", 50.00m)
}
);
_supplyStorageContact.Setup(x => x.AddElement(It.IsAny<SupplyDataModel>()))
.Throws(new ElementExistsException("ID", supply.Id));
_ingredientStorageContact.Setup(x => x.GetElementById(ingredientId)).Returns(supply.Ingredients[0]);
// Act & Assert
Assert.That(() => _supplyBusinessLogicContract.InsertSupply(supply),
Throws.TypeOf<ElementExistsException>());
_supplyStorageContact.Verify(x => x.AddElement(It.IsAny<SupplyDataModel>()), Times.Once);
}
[Test]
public void InsertSupply_NullRecord_ThrowException_Test()
{
// Act & Assert
Assert.That(() => _supplyBusinessLogicContract.InsertSupply(null),
Throws.TypeOf<ArgumentNullException>());
_supplyStorageContact.Verify(x => x.AddElement(It.IsAny<SupplyDataModel>()), Times.Never);
}
[Test]
public void InsertSupply_InvalidRecord_ThrowException_Test()
{
// Act & Assert
Assert.That(() => _supplyBusinessLogicContract.InsertSupply(new SupplyDataModel(
"", // Invalid ID
default(DateTime), // Invalid Date
0, // Invalid Quantity
-1, // Invalid Price
new List<IngredientDataModel>() // Empty ingredients list
)), Throws.TypeOf<ValidationException>());
_supplyStorageContact.Verify(x => x.AddElement(It.IsAny<SupplyDataModel>()), Times.Never);
}
[Test]
public void InsertSupply_StorageThrowError_ThrowException_Test()
{
// Arrange
var ingredientId = Guid.NewGuid().ToString();
var supply = new SupplyDataModel(
Guid.NewGuid().ToString(),
DateTime.Now,
100,
1000.00m,
new List<IngredientDataModel>
{
new IngredientDataModel(ingredientId, "Sugar", "kg", 50.00m)
}
);
_supplyStorageContact.Setup(x => x.AddElement(It.IsAny<SupplyDataModel>()))
.Throws(new StorageException(new InvalidOperationException()));
_ingredientStorageContact.Setup(x => x.GetElementById(ingredientId)).Returns(supply.Ingredients[0]);
// Act & Assert
Assert.That(() => _supplyBusinessLogicContract.InsertSupply(supply),
Throws.TypeOf<StorageException>());
_supplyStorageContact.Verify(x => x.AddElement(It.IsAny<SupplyDataModel>()), Times.Once);
}
[Test]
public void UpdateSupply_CorrectRecord_Test()
{
// Arrange
var flag = false;
var ingredientId = Guid.NewGuid().ToString();
var supply = new SupplyDataModel(
Guid.NewGuid().ToString(),
DateTime.Now,
100,
1000.00m,
new List<IngredientDataModel>
{
new IngredientDataModel(ingredientId, "Sugar", "kg", 50.00m)
}
);
_supplyStorageContact.Setup(x => x.UpdateElement(It.IsAny<SupplyDataModel>()))
.Callback((SupplyDataModel x) =>
{
flag = x.Id == supply.Id &&
x.Date == supply.Date &&
x.TotalQuantity == supply.TotalQuantity &&
x.TotalPrice == supply.TotalPrice &&
x.Ingredients.SequenceEqual(supply.Ingredients);
});
_ingredientStorageContact.Setup(x => x.GetElementById(ingredientId)).Returns(supply.Ingredients[0]);
// Act
_supplyBusinessLogicContract.UpdateSupply(supply);
// Assert
_supplyStorageContact.Verify(x => x.UpdateElement(It.IsAny<SupplyDataModel>()), Times.Once);
Assert.That(flag);
Assert.That(Guid.TryParse(supply.Id, out _), Is.True);
Assert.That(supply.Date, Is.Not.EqualTo(default(DateTime)));
Assert.That(supply.TotalQuantity, Is.GreaterThan(0));
Assert.That(supply.TotalPrice, Is.GreaterThanOrEqualTo(0));
Assert.That(supply.Ingredients, Is.Not.Null);
Assert.That(supply.Ingredients.Count, Is.GreaterThan(0));
}
[Test]
public void UpdateSupply_RecordNotFound_ThrowException_Test()
{
// Arrange
var ingredientId = Guid.NewGuid().ToString();
var supply = new SupplyDataModel(
Guid.NewGuid().ToString(),
DateTime.Now,
100,
1000.00m,
new List<IngredientDataModel>
{
new IngredientDataModel(ingredientId, "Sugar", "kg", 50.00m)
}
);
_supplyStorageContact.Setup(x => x.UpdateElement(It.IsAny<SupplyDataModel>()))
.Throws(new ElementNotFoundException("Supply not found"));
_ingredientStorageContact.Setup(x => x.GetElementById(ingredientId)).Returns(supply.Ingredients[0]);
// Act & Assert
Assert.That(() => _supplyBusinessLogicContract.UpdateSupply(supply),
Throws.TypeOf<ElementNotFoundException>());
_supplyStorageContact.Verify(x => x.UpdateElement(It.IsAny<SupplyDataModel>()), Times.Once);
}
[Test]
public void UpdateSupply_NullRecord_ThrowException_Test()
{
// Act & Assert
Assert.That(() => _supplyBusinessLogicContract.UpdateSupply(null),
Throws.TypeOf<ArgumentNullException>());
_supplyStorageContact.Verify(x => x.UpdateElement(It.IsAny<SupplyDataModel>()), Times.Never);
}
[Test]
public void UpdateSupply_InvalidRecord_ThrowException_Test()
{
// Act & Assert
Assert.That(() => _supplyBusinessLogicContract.UpdateSupply(new SupplyDataModel(
"", // Invalid ID
default(DateTime), // Invalid Date
0, // Invalid Quantity
-1, // Invalid Price
new List<IngredientDataModel>() // Empty ingredients list
)), Throws.TypeOf<ValidationException>());
_supplyStorageContact.Verify(x => x.UpdateElement(It.IsAny<SupplyDataModel>()), Times.Never);
}
[Test]
public void UpdateSupply_StorageThrowError_ThrowException_Test()
{
// Arrange
var ingredientId = Guid.NewGuid().ToString();
var supply = new SupplyDataModel(
Guid.NewGuid().ToString(),
DateTime.Now,
100,
1000.00m,
new List<IngredientDataModel>
{
new IngredientDataModel(ingredientId, "Sugar", "kg", 50.00m)
}
);
_supplyStorageContact.Setup(x => x.UpdateElement(It.IsAny<SupplyDataModel>()))
.Throws(new StorageException(new InvalidOperationException()));
_ingredientStorageContact.Setup(x => x.GetElementById(ingredientId)).Returns(supply.Ingredients[0]);
// Act & Assert
Assert.That(() => _supplyBusinessLogicContract.UpdateSupply(supply),
Throws.TypeOf<StorageException>());
_supplyStorageContact.Verify(x => x.UpdateElement(It.IsAny<SupplyDataModel>()), Times.Once);
}
[Test]
public void DeleteSupply_CorrectId_Test()
{
// Arrange
var id = Guid.NewGuid().ToString();
var supply = new SupplyDataModel(
id,
DateTime.Now,
100,
1000.00m,
new List<IngredientDataModel>
{
new IngredientDataModel(Guid.NewGuid().ToString(), "Sugar", "kg", 50.00m)
}
);
var flag = false;
_supplyStorageContact.Setup(x => x.GetElementById(id)).Returns(supply);
_supplyStorageContact.Setup(x => x.DeleteElement(id)).Callback(() => { flag = true; });
// Act
_supplyBusinessLogicContract.DeleteSupply(id);
// Assert
_supplyStorageContact.Verify(x => x.DeleteElement(id), Times.Once);
Assert.That(flag);
Assert.That(Guid.TryParse(supply.Id, out _), Is.True);
Assert.That(supply.Date, Is.Not.EqualTo(default(DateTime)));
Assert.That(supply.TotalQuantity, Is.GreaterThan(0));
Assert.That(supply.TotalPrice, Is.GreaterThanOrEqualTo(0));
Assert.That(supply.Ingredients, Is.Not.Null);
Assert.That(supply.Ingredients.Count, Is.GreaterThan(0));
}
[Test]
public void DeleteSupply_RecordNotFound_ThrowException_Test()
{
// Arrange
var id = Guid.NewGuid().ToString();
_supplyStorageContact.Setup(x => x.GetElementById(id)).Returns((SupplyDataModel)null);
// Act & Assert
Assert.That(() => _supplyBusinessLogicContract.DeleteSupply(id),
Throws.TypeOf<ElementNotFoundException>());
_supplyStorageContact.Verify(x => x.GetElementById(id), Times.Once);
}
[Test]
public void DeleteSupply_NullOrEmptyId_ThrowException_Test()
{
// Act & Assert
Assert.That(() => _supplyBusinessLogicContract.DeleteSupply(null),
Throws.TypeOf<ArgumentNullException>());
Assert.That(() => _supplyBusinessLogicContract.DeleteSupply(string.Empty),
Throws.TypeOf<ArgumentNullException>());
_supplyStorageContact.Verify(x => x.DeleteElement(It.IsAny<string>()), Times.Never);
}
[Test]
public void DeleteSupply_InvalidId_ThrowException_Test()
{
// Arrange
var id = "invalid-id";
// Act & Assert
Assert.That(() => _supplyBusinessLogicContract.DeleteSupply(id),
Throws.TypeOf<ValidationException>());
_supplyStorageContact.Verify(x => x.GetElementById(id), Times.Never);
}
[Test]
public void DeleteSupply_StorageThrowError_ThrowException_Test()
{
// Arrange
var id = Guid.NewGuid().ToString();
var supply = new SupplyDataModel(
id,
DateTime.Now,
100,
1000.00m,
new List<IngredientDataModel>
{
new (Guid.NewGuid().ToString(), "Sugar", "kg", 50.00m)
}
);
_supplyStorageContact.Setup(x => x.GetElementById(id)).Returns(supply);
_supplyStorageContact.Setup(x => x.DeleteElement(id))
.Throws(new StorageException(new InvalidOperationException()));
// Act & Assert
Assert.That(() => _supplyBusinessLogicContract.DeleteSupply(id),
Throws.TypeOf<StorageException>());
_supplyStorageContact.Verify(x => x.GetElementById(id), Times.Once);
_supplyStorageContact.Verify(x => x.DeleteElement(id), Times.Once);
}
}
}

View File

@@ -154,6 +154,8 @@
<Compile Include="BusinessLogicsContractsTests\PositionBusinessLogicContractTests.cs" />
<Compile Include="BusinessLogicsContractsTests\ProductBusinessLogicContractTests.cs" />
<Compile Include="BusinessLogicsContractsTests\SalaryBusinessLogicContractTests.cs" />
<Compile Include="BusinessLogicsContractsTests\StorageBusinessLogicContractTests.cs" />
<Compile Include="BusinessLogicsContractsTests\SupplyBusinessLogicContractTests.cs" />
<Compile Include="DataModelsTests\IngredientDataModelTests.cs" />
<Compile Include="DataModelsTests\OrderDataModelTests.cs" />
<Compile Include="DataModelsTests\PekarDataModelTests.cs" />
@@ -162,6 +164,10 @@
<Compile Include="DataModelsTests\ProductDataModelTests.cs" />
<Compile Include="DataModelsTests\RecipeDataModelTests.cs" />
<Compile Include="DataModelsTests\SalaryDataModelTests.cs" />
<Compile Include="DataModelsTests\StorageDataModelTests.cs" />
<Compile Include="DataModelsTests\StorageIngredientDataModelTests.cs" />
<Compile Include="DataModelsTests\SupplyDataModelTests.cs" />
<Compile Include="DataModelsTests\SupplyIngredientDataModelTests.cs" />
</ItemGroup>
<ItemGroup>
<None Include="App.config" />

View File

@@ -0,0 +1,92 @@
using System;
using System.Collections.Generic;
using NUnit.Framework;
using CandyHouseBase.DataModels;
using CandyHouseBase.Enums;
using CandyHouseBase.Exceptions;
namespace CandyHouseTests.DataModelsTests
{
[TestFixture]
public class StorageDataModelTests
{
[Test]
public void CreateStorageDataModel_ValidData_ShouldCreateSuccessfully()
{
var id = Guid.NewGuid().ToString();
var title = "Main Warehouse";
var storageType = StorageType.Fridge;
var ingredients = new List<IngredientDataModel>
{
new IngredientDataModel(Guid.NewGuid().ToString(), "Sugar", "kg", 10)
};
var storage = new StorageDataModel(id, title, storageType, ingredients);
Assert.AreEqual(id, storage.Id);
Assert.AreEqual(title, storage.Title);
Assert.AreEqual(storageType, storage.StorageType);
Assert.AreEqual(ingredients, storage.Ingredients);
}
[Test]
public void Validate_ValidData_ShouldNotThrowException()
{
var storage = new StorageDataModel(
Guid.NewGuid().ToString(),
"Main Warehouse",
StorageType.Fridge,
new List<IngredientDataModel>());
Assert.DoesNotThrow(() => storage.Validate());
}
[Test]
public void Validate_EmptyId_ShouldThrowValidationException()
{
var storage = new StorageDataModel(
"",
"Main Warehouse",
StorageType.Fridge,
new List<IngredientDataModel>());
Assert.Throws<ValidationException>(() => storage.Validate());
}
[Test]
public void Validate_InvalidIdFormat_ShouldThrowValidationException()
{
var storage = new StorageDataModel(
"invalid-guid",
"Main Warehouse",
StorageType.Fridge,
new List<IngredientDataModel>());
Assert.Throws<ValidationException>(() => storage.Validate());
}
[Test]
public void Validate_EmptyTitle_ShouldThrowValidationException()
{
var storage = new StorageDataModel(
Guid.NewGuid().ToString(),
"",
StorageType.Fridge,
new List<IngredientDataModel>());
Assert.Throws<ValidationException>(() => storage.Validate());
}
[Test]
public void Validate_InvalidStorageType_ShouldThrowValidationException()
{
var storage = new StorageDataModel(
Guid.NewGuid().ToString(),
"Main Warehouse",
(StorageType)999,
new List<IngredientDataModel>());
Assert.Throws<ValidationException>(() => storage.Validate());
}
}
}

View File

@@ -0,0 +1,102 @@
using System;
using NUnit.Framework;
using CandyHouseBase.DataModels;
using CandyHouseBase.Exceptions;
namespace CandyHouseTests.DataModelsTests
{
[TestFixture]
public class StorageIngredientDataModelTests
{
[Test]
public void CreateStorageIngredientDataModel_ValidData_ShouldCreateSuccessfully()
{
var storageId = Guid.NewGuid().ToString();
var ingredientId = Guid.NewGuid().ToString();
var quantity = 5;
var storageIngredient = new StorageIngredientDataModel(storageId, ingredientId, quantity);
Assert.AreEqual(storageId, storageIngredient.StorageId);
Assert.AreEqual(ingredientId, storageIngredient.IngredientId);
Assert.AreEqual(quantity, storageIngredient.Quantity);
}
[Test]
public void Validate_ValidData_ShouldNotThrowException()
{
var storageIngredient = new StorageIngredientDataModel(
Guid.NewGuid().ToString(),
Guid.NewGuid().ToString(),
10);
Assert.DoesNotThrow(() => storageIngredient.Validate());
}
[Test]
public void Validate_EmptyStorageId_ShouldThrowValidationException()
{
var storageIngredient = new StorageIngredientDataModel(
"",
Guid.NewGuid().ToString(),
10);
Assert.Throws<ValidationException>(() => storageIngredient.Validate());
}
[Test]
public void Validate_InvalidStorageIdFormat_ShouldThrowValidationException()
{
var storageIngredient = new StorageIngredientDataModel(
"invalid-guid",
Guid.NewGuid().ToString(),
10);
Assert.Throws<ValidationException>(() => storageIngredient.Validate());
}
[Test]
public void Validate_EmptyIngredientId_ShouldThrowValidationException()
{
var storageIngredient = new StorageIngredientDataModel(
Guid.NewGuid().ToString(),
"",
10);
Assert.Throws<ValidationException>(() => storageIngredient.Validate());
}
[Test]
public void Validate_InvalidIngredientIdFormat_ShouldThrowValidationException()
{
var storageIngredient = new StorageIngredientDataModel(
Guid.NewGuid().ToString(),
"not-a-guid",
10);
Assert.Throws<ValidationException>(() => storageIngredient.Validate());
}
[Test]
public void Validate_NonPositiveQuantity_ShouldThrowValidationException()
{
var storageIngredient = new StorageIngredientDataModel(
Guid.NewGuid().ToString(),
Guid.NewGuid().ToString(),
0);
Assert.Throws<ValidationException>(() => storageIngredient.Validate());
}
[Test]
public void Validate_NegativeQuantity_ShouldThrowValidationException()
{
var storageIngredient = new StorageIngredientDataModel(
Guid.NewGuid().ToString(),
Guid.NewGuid().ToString(),
-5);
Assert.Throws<ValidationException>(() => storageIngredient.Validate());
}
}
}

View File

@@ -0,0 +1,155 @@
using System;
using System.Collections.Generic;
using NUnit.Framework;
using CandyHouseBase.DataModels;
using CandyHouseBase.Exceptions;
namespace CandyHouseTests.DataModelsTests
{
[TestFixture]
public class SupplyDataModelTests
{
[Test]
public void CreateSupplyDataModel_ValidData_ShouldCreateSuccessfully()
{
var id = Guid.NewGuid().ToString();
var date = DateTime.Now;
var totalQuantity = 100;
var totalPrice = 150.75m;
var ingredients = new List<IngredientDataModel>
{
new IngredientDataModel(Guid.NewGuid().ToString(), "Sugar", "kg", 10)
};
var supply = new SupplyDataModel(id, date, totalQuantity, totalPrice, ingredients);
Assert.AreEqual(id, supply.Id);
Assert.AreEqual(date, supply.Date);
Assert.AreEqual(totalQuantity, supply.TotalQuantity);
Assert.AreEqual(totalPrice, supply.TotalPrice);
Assert.AreEqual(ingredients, supply.Ingredients);
}
[Test]
public void Validate_ValidData_ShouldNotThrowException()
{
var supply = new SupplyDataModel(
Guid.NewGuid().ToString(),
DateTime.Now,
50,
75.25m,
new List<IngredientDataModel>
{
new IngredientDataModel(Guid.NewGuid().ToString(), "Sugar", "kg", 10)
});
Assert.DoesNotThrow(() => supply.Validate());
}
[Test]
public void Validate_EmptyId_ShouldThrowValidationException()
{
var supply = new SupplyDataModel(
"",
DateTime.Now,
50,
75.25m,
new List<IngredientDataModel>
{
new IngredientDataModel(Guid.NewGuid().ToString(), "Sugar", "kg", 10)
});
Assert.Throws<ValidationException>(() => supply.Validate());
}
[Test]
public void Validate_InvalidIdFormat_ShouldThrowValidationException()
{
var supply = new SupplyDataModel(
"not-a-guid",
DateTime.Now,
50,
75.25m,
new List<IngredientDataModel>
{
new IngredientDataModel(Guid.NewGuid().ToString(), "Sugar", "kg", 10)
});
Assert.Throws<ValidationException>(() => supply.Validate());
}
[Test]
public void Validate_DefaultDate_ShouldThrowValidationException()
{
var supply = new SupplyDataModel(
Guid.NewGuid().ToString(),
default(DateTime),
50,
75.25m,
new List<IngredientDataModel>
{
new IngredientDataModel(Guid.NewGuid().ToString(), "Sugar", "kg", 10)
});
Assert.Throws<ValidationException>(() => supply.Validate());
}
[Test]
public void Validate_NonPositiveQuantity_ShouldThrowValidationException()
{
var supply = new SupplyDataModel(
Guid.NewGuid().ToString(),
DateTime.Now,
0,
75.25m,
new List<IngredientDataModel>
{
new IngredientDataModel(Guid.NewGuid().ToString(), "Sugar", "kg", 10)
});
Assert.Throws<ValidationException>(() => supply.Validate());
}
[Test]
public void Validate_NegativeTotalPrice_ShouldThrowValidationException()
{
var supply = new SupplyDataModel(
Guid.NewGuid().ToString(),
DateTime.Now,
50,
-10.50m,
new List<IngredientDataModel>
{
new IngredientDataModel(Guid.NewGuid().ToString(), "Sugar", "kg", 10)
});
Assert.Throws<ValidationException>(() => supply.Validate());
}
[Test]
public void Validate_NullIngredients_ShouldThrowValidationException()
{
var supply = new SupplyDataModel(
Guid.NewGuid().ToString(),
DateTime.Now,
50,
75.25m,
null);
Assert.Throws<ValidationException>(() => supply.Validate());
}
[Test]
public void Validate_EmptyIngredients_ShouldThrowValidationException()
{
var supply = new SupplyDataModel(
Guid.NewGuid().ToString(),
DateTime.Now,
50,
75.25m,
new List<IngredientDataModel>());
Assert.Throws<ValidationException>(() => supply.Validate());
}
}
}

View File

@@ -0,0 +1,102 @@
using System;
using NUnit.Framework;
using CandyHouseBase.DataModels;
using CandyHouseBase.Exceptions;
namespace CandyHouseTests.DataModelsTests
{
[TestFixture]
public class SupplyIngredientDataModelTests
{
[Test]
public void CreateSupplyIngredientDataModel_ValidData_ShouldCreateSuccessfully()
{
var supplyId = Guid.NewGuid().ToString();
var ingredientId = Guid.NewGuid().ToString();
var quantity = 10;
var supplyIngredient = new SupplyIngredientDataModel(supplyId, ingredientId, quantity);
Assert.AreEqual(supplyId, supplyIngredient.SupplyId);
Assert.AreEqual(ingredientId, supplyIngredient.IngredientId);
Assert.AreEqual(quantity, supplyIngredient.Quantity);
}
[Test]
public void Validate_ValidData_ShouldNotThrowException()
{
var supplyIngredient = new SupplyIngredientDataModel(
Guid.NewGuid().ToString(),
Guid.NewGuid().ToString(),
5);
Assert.DoesNotThrow(() => supplyIngredient.Validate());
}
[Test]
public void Validate_EmptySupplyId_ShouldThrowValidationException()
{
var supplyIngredient = new SupplyIngredientDataModel(
"",
Guid.NewGuid().ToString(),
5);
Assert.Throws<ValidationException>(() => supplyIngredient.Validate());
}
[Test]
public void Validate_InvalidSupplyIdFormat_ShouldThrowValidationException()
{
var supplyIngredient = new SupplyIngredientDataModel(
"invalid-guid",
Guid.NewGuid().ToString(),
5);
Assert.Throws<ValidationException>(() => supplyIngredient.Validate());
}
[Test]
public void Validate_EmptyIngredientId_ShouldThrowValidationException()
{
var supplyIngredient = new SupplyIngredientDataModel(
Guid.NewGuid().ToString(),
"",
5);
Assert.Throws<ValidationException>(() => supplyIngredient.Validate());
}
[Test]
public void Validate_InvalidIngredientIdFormat_ShouldThrowValidationException()
{
var supplyIngredient = new SupplyIngredientDataModel(
Guid.NewGuid().ToString(),
"not-a-guid",
5);
Assert.Throws<ValidationException>(() => supplyIngredient.Validate());
}
[Test]
public void Validate_NonPositiveQuantity_ShouldThrowValidationException()
{
var supplyIngredient = new SupplyIngredientDataModel(
Guid.NewGuid().ToString(),
Guid.NewGuid().ToString(),
0);
Assert.Throws<ValidationException>(() => supplyIngredient.Validate());
}
[Test]
public void Validate_NegativeQuantity_ShouldThrowValidationException()
{
var supplyIngredient = new SupplyIngredientDataModel(
Guid.NewGuid().ToString(),
Guid.NewGuid().ToString(),
-3);
Assert.Throws<ValidationException>(() => supplyIngredient.Validate());
}
}
}