18 Commits

Author SHA1 Message Date
ca8e1737d0 Зеленая зона по Работнику 2025-04-21 20:58:18 +04:00
aeffe2f451 Зеленая зона З\п 2025-04-21 20:49:20 +04:00
ab3a3f6819 Производство зеленая зона 2025-04-21 20:30:06 +04:00
6f2d341f01 Соответсвующий фикс для Продукта 2025-04-21 19:08:39 +04:00
148b67f195 Исключение 2025-04-21 18:44:30 +04:00
dbf2331511 Макрофикс по списку ингердиентов 2025-04-21 18:44:20 +04:00
4b72fdbe96 Зеленая зона продукта 2025-04-21 17:36:06 +04:00
f5a54bab32 Красная зона Продукта 2025-04-21 17:25:41 +04:00
0d4a275328 Зеленые тесты по должности(реализация) 2025-04-09 13:29:24 +04:00
684073e3be Фикс должности, тк там удаление и нужно было где то помечать удален или нет 2025-04-09 13:29:00 +04:00
5cc67e747e Красная зона для Должности 2025-04-09 12:40:50 +04:00
56a54e19aa Зеленая зона для Ингредиента (реализация) 2025-04-09 12:11:20 +04:00
2e976740af Красная зона для ингредиентов доделана. Добавлено исключение 2025-04-09 11:53:18 +04:00
1d72951b99 Красная зона по ингредиентам + несколько исключений. У меня отвалился лмс и файлик тоже... 2025-04-09 00:23:09 +04:00
595041c319 интерфейсы 2025-04-08 22:39:54 +04:00
71b48ba503 Фикс маленькой буквы в перечислении. Добавление заглушенной реализации интерфейсов 2025-04-08 01:35:03 +04:00
1d8c331535 Бизнес логика готова 2025-04-07 23:04:41 +04:00
362f1e73de Хранилище контрактов добавил 2025-04-06 22:51:53 +04:00
40 changed files with 3710 additions and 81 deletions

View File

@@ -5,7 +5,9 @@ VisualStudioVersion = 17.9.34728.123
MinimumVisualStudioVersion = 10.0.40219.1 MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SladkieBulkiContrakts", "SladkieBulkiContrakts\SladkieBulkiContrakts.csproj", "{04F3CF43-43CA-4EF0-B5BB-913425A85E29}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SladkieBulkiContrakts", "SladkieBulkiContrakts\SladkieBulkiContrakts.csproj", "{04F3CF43-43CA-4EF0-B5BB-913425A85E29}"
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SladkieBulkiTests", "SladkieBulkiTests\SladkieBulkiTests.csproj", "{12312C3E-A15B-4563-BD04-BB814149B2F6}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SladkieBulkiTests", "SladkieBulkiTests\SladkieBulkiTests.csproj", "{12312C3E-A15B-4563-BD04-BB814149B2F6}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SladkieBulkiBusinessLogic", "SladkieBulkiBusinessLogic\SladkieBulkiBusinessLogic.csproj", "{004CA71C-5308-44C7-82DA-4098BA6F5F1C}"
EndProject EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -21,6 +23,10 @@ Global
{12312C3E-A15B-4563-BD04-BB814149B2F6}.Debug|Any CPU.Build.0 = Debug|Any CPU {12312C3E-A15B-4563-BD04-BB814149B2F6}.Debug|Any CPU.Build.0 = Debug|Any CPU
{12312C3E-A15B-4563-BD04-BB814149B2F6}.Release|Any CPU.ActiveCfg = Release|Any CPU {12312C3E-A15B-4563-BD04-BB814149B2F6}.Release|Any CPU.ActiveCfg = Release|Any CPU
{12312C3E-A15B-4563-BD04-BB814149B2F6}.Release|Any CPU.Build.0 = Release|Any CPU {12312C3E-A15B-4563-BD04-BB814149B2F6}.Release|Any CPU.Build.0 = Release|Any CPU
{004CA71C-5308-44C7-82DA-4098BA6F5F1C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{004CA71C-5308-44C7-82DA-4098BA6F5F1C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{004CA71C-5308-44C7-82DA-4098BA6F5F1C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{004CA71C-5308-44C7-82DA-4098BA6F5F1C}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection EndGlobalSection
GlobalSection(SolutionProperties) = preSolution GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE HideSolutionNode = FALSE

View File

@@ -0,0 +1,92 @@
using Microsoft.Extensions.Logging;
using SladkieBulkiContrakts.BusinessLogicsContracts;
using SladkieBulkiContrakts.DataModels;
using SladkieBulkiContrakts.Exceptions;
using SladkieBulkiContrakts.Extensions;
using SladkieBulkiContrakts.StoragesContarcts;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
namespace SladkieBulkiBusinessLogic.Implementations;
internal class IngredientBusinessLogicContract (IIngredientStorageContract ingredientStorageContract, ILogger logger) : IIngredientBusinessLogicContract
{
private readonly ILogger _logger = logger;
private readonly IIngredientStorageContract _ingredientStorageContract = ingredientStorageContract;
public List<IngredientDataModel> GetAllIngredients()
{
_logger.LogInformation("GetAllBuyers");
return _ingredientStorageContract.GetList() ?? throw new NullListException();
}
public List<IngredientDataModel> GetIngredientsBySizeUnit(string sizeUnit)
{
_logger.LogInformation("GetIngredientsBySizeUnit: {sizeUnit}", sizeUnit);
if (string.IsNullOrWhiteSpace(sizeUnit))
{
throw new ArgumentNullException(nameof(sizeUnit));
}
var list = _ingredientStorageContract.GetElementBySizeUnit(sizeUnit);
if (list == null)
{
throw new NullListException();
}
return list.Where(x => x.SizeInit == sizeUnit).ToList();
}
public IngredientDataModel? GetIngredientById(string id)
{
_logger.LogInformation("GetIngredientById: {id}", id);
if (id.IsEmpty())
{
throw new ArgumentNullException(nameof(id));
}
if (!id.IsGuid())
{
throw new ValidationException("Id is not a unique identifier");
}
return _ingredientStorageContract.GetElementById(id) ?? throw new ElementNotFoundException(id);
}
public IngredientDataModel? GetIngredientByName(string name)
{
_logger.LogInformation("GetIngredientByName: {name}", name);
if (string.IsNullOrWhiteSpace(name))
{
throw new ArgumentNullException(nameof(name));
}
return _ingredientStorageContract.GetElementByName(name) ?? throw new ElementNotFoundException(name);
}
public void InsertIngredient(IngredientDataModel ingredientDataModel)
{
_logger.LogInformation("New ingredient: {json}", JsonSerializer.Serialize(ingredientDataModel));
ArgumentNullException.ThrowIfNull(ingredientDataModel);
ingredientDataModel.Validate();
_ingredientStorageContract.AddElement(ingredientDataModel);
}
public void UpdateIngredient(IngredientDataModel ingredientDataModel)
{
_logger.LogInformation("Update ingredient: {json}", JsonSerializer.Serialize(ingredientDataModel));
ArgumentNullException.ThrowIfNull(ingredientDataModel);
ingredientDataModel.Validate();
_ingredientStorageContract.UpdElement(ingredientDataModel);
}
}

View File

@@ -0,0 +1,93 @@
using Microsoft.Extensions.Logging;
using SladkieBulkiContrakts.BusinessLogicsContracts;
using SladkieBulkiContrakts.DataModels;
using SladkieBulkiContrakts.Exceptions;
using SladkieBulkiContrakts.Extensions;
using SladkieBulkiContrakts.StoragesContarcts;
using System.Text.Json;
namespace SladkieBulkiBusinessLogic.Implementations;
internal class PostBusinessLogicContract(IPostStorageContract postStorageContract, ILogger logger) : IPostBusinessLogicContract
{
private readonly ILogger _logger = logger;
private readonly IPostStorageContract _postStorageContract = postStorageContract;
public List<PostDataModel> GetAllPosts(bool onlyActive = true)
{
_logger.LogInformation("GetAllPosts params: {onlyActive}", onlyActive);
return _postStorageContract.GetList(onlyActive) ?? throw new NullListException();
}
public List<PostDataModel> GetAllDataOfPost(string postId)
{
_logger.LogInformation("GetAllDataOfPost for {postId}", postId);
if (postId.IsEmpty())
{
throw new ArgumentNullException(nameof(postId));
}
if (!postId.IsGuid())
{
throw new ValidationException("The value in the field postId is not a unique identifier.");
}
return _postStorageContract.GetPostWithHistory(postId) ?? throw new NullListException();
}
public PostDataModel GetPostByData(string data)
{
_logger.LogInformation("Get element by data: {data}", data);
if (data.IsEmpty())
{
throw new ArgumentNullException(nameof(data));
}
if (data.IsGuid())
{
return _postStorageContract.GetElementById(data) ?? throw new ElementNotFoundException(data);
}
return _postStorageContract.GetElementByName(data) ?? throw new ElementNotFoundException(data);
}
public void InsertPost(PostDataModel postDataModel)
{
_logger.LogInformation("New data: {json}", JsonSerializer.Serialize(postDataModel));
ArgumentNullException.ThrowIfNull(postDataModel);
postDataModel.Validate();
_postStorageContract.AddElement(postDataModel);
}
public void UpdatePost(PostDataModel postDataModel)
{
_logger.LogInformation("Update data: {json}", JsonSerializer.Serialize(postDataModel));
ArgumentNullException.ThrowIfNull(postDataModel);
postDataModel.Validate();
_postStorageContract.UpdElement(postDataModel);
}
public void DeletePost(string id)
{
_logger.LogInformation("Delete by id: {id}", id);
if (id.IsEmpty())
{
throw new ArgumentNullException(nameof(id));
}
if (!id.IsGuid())
{
throw new ValidationException("Id is not a unique identifier");
}
_postStorageContract.DelElement(id);
}
public void RestorePost(string id)
{
_logger.LogInformation("Restore by id: {id}", id);
if (id.IsEmpty())
{
throw new ArgumentNullException(nameof(id));
}
if (!id.IsGuid())
{
throw new ValidationException("Id is not a unique identifier");
}
_postStorageContract.ResElement(id);
}
}

View File

@@ -0,0 +1,87 @@
using Microsoft.Extensions.Logging;
using SladkieBulkiContrakts.BusinessLogicsContracts;
using SladkieBulkiContrakts.DataModels;
using SladkieBulkiContrakts.Exceptions;
using SladkieBulkiContrakts.Extensions;
using SladkieBulkiContrakts.StoragesContarcts;
using System.Text.Json;
namespace SladkieBulkiBusinessLogic.Implementations;
internal class ProductBusinessLogicContract(IProductStorageContract productStorageContract, ILogger logger) : IProductBusinessLogicContract
{
private readonly ILogger _logger = logger;
private readonly IProductStorageContract _productStorageContract = productStorageContract;
public List<ProductDataModel> GetAllProducts(bool onlyActive)
{
_logger.LogInformation("GetAllProducts params: {onlyActive}", onlyActive);
return _productStorageContract.GetList(onlyActive) ?? throw new NullListException();
//return [];
}
public List<ProductHistoryDataModel> GetProductHistoryByProduct(string productId)
{
_logger.LogInformation("GetProductHistoryByProduct for {productId}", productId);
if (productId.IsEmpty())
{
throw new ArgumentNullException(nameof(productId));
}
if (!productId.IsGuid())
{
throw new ValidationException("The value in the field productId is not a unique identifier.");
}
return _productStorageContract.GetHistoryByProductId(productId) ?? throw new NullListException();
//return [];
}
public ProductDataModel GetProductByData(string data)
{
_logger.LogInformation("Get element by data: {data}", data);
if (data.IsEmpty())
{
throw new ArgumentNullException(nameof(data));
}
if (data.IsGuid())
{
return _productStorageContract.GetElementById(data) ?? throw new ElementNotFoundException(data);
}
return _productStorageContract.GetElementByName(data) ?? throw new ElementNotFoundException(data);
//return new("", "", "", 0, [], 0, false);
}
public void InsertProduct(ProductDataModel productDataModel)
{
_logger.LogInformation("New data: {json}", JsonSerializer.Serialize(productDataModel));
ArgumentNullException.ThrowIfNull(productDataModel);
productDataModel.Validate();
_productStorageContract.AddElement(productDataModel);
}
public void UpdateProduct(ProductDataModel productDataModel)
{
_logger.LogInformation("Update data: {json}", JsonSerializer.Serialize(productDataModel));
ArgumentNullException.ThrowIfNull(productDataModel);
productDataModel.Validate();
_productStorageContract.UpdElement(productDataModel);
}
public void DeleteProduct(string id)
{
_logger.LogInformation("Delete by id: {id}", id);
if (id.IsEmpty())
{
throw new ArgumentNullException(nameof(id));
}
if (!id.IsGuid())
{
throw new ValidationException("Id is not a unique identifier");
}
_productStorageContract.DelElement(id);
}
}

View File

@@ -0,0 +1,98 @@
using Microsoft.Extensions.Logging;
using SladkieBulkiContrakts.BusinessLogicsContracts;
using SladkieBulkiContrakts.DataModels;
using SladkieBulkiContrakts.Exceptions;
using SladkieBulkiContrakts.Extensions;
using SladkieBulkiContrakts.StoragesContarcts;
using System.Text.Json;
namespace SladkieBulkiBusinessLogic.Implementations;
internal class ProductionBusinessLogicContract(IProductionStorageContract productionStorageContract,ILogger logger) : IProductionBusinessLogicContract
{
private readonly ILogger _logger = logger;
private readonly IProductionStorageContract _productionStorageContract = productionStorageContract;
public List<ProductionDataModel> GetAllSalesByPeriod(DateTime fromDate, DateTime toDate)
{
_logger.LogInformation("GetAllProductionsByPeriod params: {fromDate}, {toDate}", fromDate, toDate);
if (fromDate.IsDateNotOlder(toDate))
{
throw new IncorrectDatesException(fromDate, toDate);
}
return _productionStorageContract.GetList(fromDate, toDate) ?? throw new NullListException();
}
public List<ProductionDataModel> GetAllSalesByProductByPeriod(string productId, DateTime fromDate, DateTime toDate)
{
_logger.LogInformation("GetAllProductionsByProduct params: {productId}, {fromDate}, {toDate}", productId, fromDate, toDate);
if (fromDate.IsDateNotOlder(toDate))
{
throw new IncorrectDatesException(fromDate, toDate);
}
if (productId.IsEmpty())
{
throw new ArgumentNullException(nameof(productId));
}
if (!productId.IsGuid())
{
throw new ValidationException("The value in the field productId is not a unique identifier.");
}
return _productionStorageContract.GetList(fromDate, toDate, productId: productId) ?? throw new NullListException();
}
public List<ProductionDataModel> GetAllSalesByWorkerByPeriod(string workerId, DateTime fromDate, DateTime toDate)
{
_logger.LogInformation("GetAllProductionsByWorker params: {workerId}, {fromDate}, {toDate}", workerId, fromDate, toDate);
if (fromDate.IsDateNotOlder(toDate))
{
throw new IncorrectDatesException(fromDate, toDate);
}
if (workerId.IsEmpty())
{
throw new ArgumentNullException(nameof(workerId));
}
if (!workerId.IsGuid())
{
throw new ValidationException("The value in the field workerId is not a unique identifier.");
}
return _productionStorageContract.GetList(fromDate, toDate, workerId: workerId) ?? throw new NullListException();
}
public ProductionDataModel GetProductionByData(string data)
{
_logger.LogInformation("GetProductionByData: {data}", data);
if (data.IsEmpty())
{
throw new ArgumentNullException(nameof(data));
}
if (!data.IsGuid())
{
throw new ValidationException("Id is not a unique identifier");
}
return _productionStorageContract.GetElementById(data) ?? throw new ElementNotFoundException(data);
}
public void InsertProduction(ProductionDataModel productionDataModel)
{
_logger.LogInformation("InsertProduction: {json}", JsonSerializer.Serialize(productionDataModel));
ArgumentNullException.ThrowIfNull(productionDataModel);
productionDataModel.Validate();
_productionStorageContract.AddElement(productionDataModel);
}
public void CancelProduction(string id)
{
_logger.LogInformation("CancelProduction by id: {id}", id);
if (id.IsEmpty())
{
throw new ArgumentNullException(nameof(id));
}
if (!id.IsGuid())
{
throw new ValidationException("Id is not a unique identifier");
}
_productionStorageContract.DelElement(id);
}
}

View File

@@ -0,0 +1,71 @@
using Microsoft.Extensions.Logging;
using SladkieBulkiContrakts.BusinessLogicsContracts;
using SladkieBulkiContrakts.DataModels;
using SladkieBulkiContrakts.Exceptions;
using SladkieBulkiContrakts.Extensions;
using SladkieBulkiContrakts.StoragesContarcts;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SladkieBulkiBusinessLogic.Implementations;
internal class SalaryBusinessLogicContract(ISalaryStorageContract salaryStorageContract, IProductionStorageContract productionStorageContract, IPostStorageContract postStorageContract, IWorkerStorageContract workerStorageContract, ILogger logger) : ISalaryBusinessLogicContract
{
private readonly ILogger _logger = logger;
private readonly ISalaryStorageContract _salaryStorageContract =
salaryStorageContract;
private readonly IProductionStorageContract _productionStorageContract = productionStorageContract;
private readonly IPostStorageContract _postStorageContract =
postStorageContract;
private readonly IWorkerStorageContract _workerStorageContract =
workerStorageContract;
public List<SalaryDataModel> GetAllSalariesByPeriod(DateTime fromDate, DateTime toDate)
{
_logger.LogInformation("GetAllSalaries params: {fromDate}, {toDate}", fromDate, toDate);
if (fromDate.IsDateNotOlder(toDate))
{
throw new IncorrectDatesException(fromDate, toDate);
}
return _salaryStorageContract.GetList(fromDate, toDate) ?? throw new NullListException();
}
public List<SalaryDataModel> GetAllSalariesByPeriodByWorker(DateTime fromDate, DateTime toDate, string workerId)
{
if (fromDate.IsDateNotOlder(toDate))
{
throw new IncorrectDatesException(fromDate, toDate);
}
if (workerId.IsEmpty())
{
throw new ArgumentNullException(nameof(workerId));
}
if (!workerId.IsGuid())
{
throw new ValidationException("The value in the field workerId is not a unique identifier.");
}
_logger.LogInformation("GetAllSalaries params: {fromDate}, {toDate}, {workerId}", fromDate, toDate, workerId);
return _salaryStorageContract.GetList(fromDate, toDate, workerId) ?? throw new NullListException();
}
public void CalculateSalaryByMounth(DateTime date)
{
_logger.LogInformation("CalculateSalaryByMounth: {date}", date);
var startDate = new DateTime(date.Year, date.Month, 1);
var finishDate = new DateTime(date.Year, date.Month, DateTime.DaysInMonth(date.Year, date.Month));
var workers = _workerStorageContract.GetList() ?? throw new NullListException();
foreach (var worker in workers)
{
var sales = _productionStorageContract.GetList(startDate, finishDate, workerId: worker.Id)?.Sum(x => x.Sum) ??
throw new NullListException();
var post = _postStorageContract.GetElementById(worker.PostId) ??
throw new NullListException();
var salary = post.Salary + sales * 0.1;
_logger.LogDebug("The employee {workerId} was paid a salary of {salary}", worker.Id, salary);
_salaryStorageContract.AddElement(new SalaryDataModel(worker.Id, finishDate, salary));
}
}
}

View File

@@ -0,0 +1,99 @@
using Microsoft.Extensions.Logging;
using SladkieBulkiContrakts.BusinessLogicsContracts;
using SladkieBulkiContrakts.DataModels;
using SladkieBulkiContrakts.Exceptions;
using SladkieBulkiContrakts.Extensions;
using SladkieBulkiContrakts.StoragesContarcts;
using System.Text.Json;
namespace SladkieBulkiBusinessLogic.Implementations;
internal class WorkerBusinessLogicContract(IWorkerStorageContract workerStorageContract,ILogger logger) : IWorkerBusinessLogicContract
{
private readonly IWorkerStorageContract _workerStorageContract = workerStorageContract;
private readonly ILogger _logger = logger;
public List<WorkerDataModel> GetAllWorkers(bool onlyActive = true)
{
_logger.LogInformation("GetAllWorkers params: {onlyActive}", onlyActive);
return _workerStorageContract.GetList(onlyActive) ?? throw new NullListException();
}
public List<WorkerDataModel> GetAllWorkersByPost(string postId, bool onlyActive = true)
{
_logger.LogInformation("GetAllWorkers params: {postId}, {onlyActive},", postId, onlyActive);
if (postId.IsEmpty())
{
throw new ArgumentNullException(nameof(postId));
}
if (!postId.IsGuid())
{
throw new ValidationException("The value in the field postId is not a unique identifier.");
}
return _workerStorageContract.GetList(onlyActive, postId) ?? throw new NullListException();
}
public List<WorkerDataModel> GetAllWorkersByBirthDate(DateTime fromDate, DateTime toDate, bool onlyActive = true)
{
_logger.LogInformation("GetAllWorkers params: {onlyActive}, {fromDate}, {toDate}", onlyActive, fromDate, toDate);
if (fromDate.IsDateNotOlder(toDate))
{
throw new IncorrectDatesException(fromDate, toDate);
}
return _workerStorageContract.GetList(onlyActive, fromBirthDate: fromDate, toBirthDate: toDate) ?? throw new NullListException();
}
public List<WorkerDataModel> GetAllWorkersByEmploymentDate(DateTime fromDate, DateTime toDate, bool onlyActive = true)
{
_logger.LogInformation("GetAllWorkers params: {onlyActive}, {fromDate}, {toDate}", onlyActive, fromDate, toDate);
if (fromDate.IsDateNotOlder(toDate))
{
throw new IncorrectDatesException(fromDate, toDate);
}
return _workerStorageContract.GetList(onlyActive, fromEmploymentDate: fromDate, toEmploymentDate: toDate) ?? throw new NullListException();
}
public WorkerDataModel GetWorkerByData(string data)
{
_logger.LogInformation("Get element by data: {data}", data);
if (data.IsEmpty())
{
throw new ArgumentNullException(nameof(data));
}
if (data.IsGuid())
{
return _workerStorageContract.GetElementById(data) ?? throw new ElementNotFoundException(data);
}
return _workerStorageContract.GetElementByFIO(data) ?? throw new ElementNotFoundException(data);
}
public void InsertWorker(WorkerDataModel workerDataModel)
{
_logger.LogInformation("New data: {json}", JsonSerializer.Serialize(workerDataModel));
ArgumentNullException.ThrowIfNull(workerDataModel);
workerDataModel.Validate();
_workerStorageContract.AddElement(workerDataModel);
}
public void UpdateWorker(WorkerDataModel workerDataModel)
{
_logger.LogInformation("Update data: {json}", JsonSerializer.Serialize(workerDataModel));
ArgumentNullException.ThrowIfNull(workerDataModel);
workerDataModel.Validate();
_workerStorageContract.UpdElement(workerDataModel);
}
public void DeleteWorker(string id)
{
_logger.LogInformation("Delete by id: {id}", id);
if (id.IsEmpty())
{
throw new ArgumentNullException(nameof(id));
}
if (!id.IsGuid())
{
throw new ValidationException("Id is not a unique identifier");
}
_workerStorageContract.DelElement(id);
}
}

View File

@@ -0,0 +1,22 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<InternalsVisibleTo Include="SladkieBulkiTests" />
<InternalsVisibleTo Include="DynamicProxyGenAssembly2" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="9.0.1" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\SladkieBulkiContrakts\SladkieBulkiContrakts.csproj" />
</ItemGroup>
</Project>

View File

@@ -0,0 +1,23 @@
using SladkieBulkiContrakts.DataModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SladkieBulkiContrakts.BusinessLogicsContracts;
public interface IIngredientBusinessLogicContract
{
List<IngredientDataModel> GetAllIngredients();
List<IngredientDataModel> GetIngredientsBySizeUnit(string sizeUnit);
IngredientDataModel? GetIngredientByName(string name);
IngredientDataModel? GetIngredientById(string id);
void InsertIngredient(IngredientDataModel ingredientDataModel);
void UpdateIngredient(IngredientDataModel ingredientDataModel);
}

View File

@@ -0,0 +1,20 @@
using SladkieBulkiContrakts.DataModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SladkieBulkiContrakts.BusinessLogicsContracts;
public interface IPostBusinessLogicContract
{
List<PostDataModel> GetAllPosts(bool onlyActive);
List<PostDataModel> GetAllDataOfPost(string postId);
PostDataModel GetPostByData(string data);
void InsertPost(PostDataModel postDataModel);
void UpdatePost(PostDataModel postDataModel);
void DeletePost(string id);
void RestorePost(string id);
}

View File

@@ -0,0 +1,19 @@
using SladkieBulkiContrakts.DataModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SladkieBulkiContrakts.BusinessLogicsContracts;
public interface IProductBusinessLogicContract
{
List<ProductDataModel> GetAllProducts(bool onlyActive = true);
List<ProductHistoryDataModel> GetProductHistoryByProduct(string productId);
ProductDataModel GetProductByData(string data);
void InsertProduct(ProductDataModel productDataModel);
void UpdateProduct(ProductDataModel productDataModel);
void DeleteProduct(string id);
}

View File

@@ -0,0 +1,18 @@
using SladkieBulkiContrakts.DataModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SladkieBulkiContrakts.BusinessLogicsContracts;
public interface IProductionBusinessLogicContract
{
List<ProductionDataModel> GetAllSalesByPeriod(DateTime fromDate, DateTime toDate);
List<ProductionDataModel> GetAllSalesByWorkerByPeriod(string workerId, DateTime fromDate, DateTime toDate);
List<ProductionDataModel> GetAllSalesByProductByPeriod(string productId, DateTime fromDate, DateTime toDate);
ProductionDataModel GetProductionByData(string data);
void InsertProduction(ProductionDataModel productionDataModel);
void CancelProduction(string id);
}

View File

@@ -0,0 +1,15 @@
using SladkieBulkiContrakts.DataModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SladkieBulkiContrakts.BusinessLogicsContracts;
public interface ISalaryBusinessLogicContract
{
List<SalaryDataModel> GetAllSalariesByPeriod(DateTime fromDate, DateTime toDate);
List<SalaryDataModel> GetAllSalariesByPeriodByWorker(DateTime fromDate, DateTime toDate, string workerId);
void CalculateSalaryByMounth(DateTime date);
}

View File

@@ -0,0 +1,20 @@
using SladkieBulkiContrakts.DataModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SladkieBulkiContrakts.BusinessLogicsContracts;
public interface IWorkerBusinessLogicContract
{
List<WorkerDataModel> GetAllWorkers(bool onlyActive = true);
List<WorkerDataModel> GetAllWorkersByPost(string postId, bool onlyActive = true);
List<WorkerDataModel> GetAllWorkersByBirthDate(DateTime fromDate, DateTime toDate, bool onlyActive = true);
List<WorkerDataModel> GetAllWorkersByEmploymentDate(DateTime fromDate, DateTime toDate, bool onlyActive = true);
WorkerDataModel GetWorkerByData(string data);
void InsertWorker(WorkerDataModel workerDataModel);
void UpdateWorker(WorkerDataModel workerDataModel);
void DeleteWorker(string id);
}

View File

@@ -10,24 +10,30 @@ using System.Xml;
namespace SladkieBulkiContrakts.DataModels; namespace SladkieBulkiContrakts.DataModels;
public class PostDataModel(string id, string postName, PostType postType, double salary) : IValidation public class PostDataModel(string id, string postName, PostType postType, double salary, bool isActual, DateTime changeDate) : IValidation
{ {
public string Id { get; private set; } = id; public string Id { get; private set; } = id;
public string PostName { get; private set; } = postName; public string PostName { get; private set; } = postName;
public PostType PostType { get; private set; } = postType; public PostType PostType { get; private set; } = postType;
public double Salary { get; private set; } = salary; // Ставка за 1 булку тк по заданию зп на основе изготовленной продукции public double Salary { get; private set; } = salary;
public bool IsActual { get; private set; } = isActual;
public DateTime ChangeDate { get; private set; } = changeDate;
public void Validate() public void Validate()
{ {
if (Id.IsEmpty()) if (Id.IsEmpty())
throw new ValidationException("Field Id is empty"); throw new ValidationException("Field Id is empty");
if (!Id.IsGuid()) if (!Id.IsGuid())
throw new ValidationException("The value in the field Id is not a unique identifier"); throw new ValidationException("The value in the field Id is not a unique identifier");
if (PostName.IsEmpty()) if (PostName.IsEmpty())
throw new ValidationException("Field PostName is empty"); throw new ValidationException("Field PostName is empty");
if (PostType == PostType.None) if (PostType == PostType.None)
throw new ValidationException("Field PostType is empty"); throw new ValidationException("Field PostType is empty");
if (Salary <= 0) if (Salary <= 0)
throw new ValidationException("Field Salary is empty"); throw new ValidationException("Field Salary is empty");
} }
} }

View File

@@ -10,7 +10,7 @@ using System.Xml;
namespace SladkieBulkiContrakts.DataModels; namespace SladkieBulkiContrakts.DataModels;
public class ProductDataModel (string id, string name, string description, ProductType productType, int unitPrice, bool isDeleted) : IValidation public class ProductDataModel (string id, string name, string description, ProductType productType, List<ProductIngredientDataModel> ingredients, int unitPrice, bool isDeleted) : IValidation
{ {
public string Id { get; private set; } = id; public string Id { get; private set; } = id;
@@ -20,10 +20,13 @@ public class ProductDataModel (string id, string name, string description, Produ
public ProductType ProductType { get; private set; } = productType; public ProductType ProductType { get; private set; } = productType;
public List<ProductIngredientDataModel> Ingredients { get; private set; } = ingredients;
public int UnitPrice { get; private set; } = unitPrice; public int UnitPrice { get; private set; } = unitPrice;
public bool IsDeleted { get; private set; } = isDeleted; public bool IsDeleted { get; private set; } = isDeleted;
public void Validate() public void Validate()
{ {
if (Id.IsEmpty()) if (Id.IsEmpty())
@@ -38,5 +41,7 @@ public class ProductDataModel (string id, string name, string description, Produ
throw new ValidationException("Field ProductType is empty"); throw new ValidationException("Field ProductType is empty");
if (UnitPrice <= 0) if (UnitPrice <= 0)
throw new ValidationException("Field UnitPrice must be greater than zero"); throw new ValidationException("Field UnitPrice must be greater than zero");
if (Ingredients == null || Ingredients.Count == 0)
throw new ValidationException("No ingredients defined");
} }
} }

View File

@@ -1,14 +1,7 @@
using SladkieBulkiContrakts.Extensions; using SladkieBulkiContrakts.Extensions;
using SladkieBulkiContrakts.Infrastructure; using SladkieBulkiContrakts.Infrastructure;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SladkieBulkiContrakts.DataModels; public class ProductionDataModel(string id, DateTime productionDate, int count, double sum, string workerId, string productId) : IValidation
public class ProductionDataModel(string id, DateTime productionDate, int count, double sum, string workerId, string productId, List<ProductIngredientDataModel>? products) : IValidation
{ {
public string Id { get; private set; } = id; public string Id { get; private set; } = id;
public DateTime ProductionDate { get; private set; } = productionDate; public DateTime ProductionDate { get; private set; } = productionDate;
@@ -16,7 +9,6 @@ public class ProductionDataModel(string id, DateTime productionDate, int count,
public double Sum { get; private set; } = sum; public double Sum { get; private set; } = sum;
public string WorkerId { get; private set; } = workerId; public string WorkerId { get; private set; } = workerId;
public string ProductId { get; private set; } = productId; public string ProductId { get; private set; } = productId;
public List<ProductIngredientDataModel>? Products { get; private set; } = products;
public void Validate() public void Validate()
{ {
@@ -38,8 +30,5 @@ public class ProductionDataModel(string id, DateTime productionDate, int count,
throw new ValidationException("The value in the field ProductId is not a unique identifier"); throw new ValidationException("The value in the field ProductId is not a unique identifier");
if (Sum <= 0) if (Sum <= 0)
throw new ValidationException("Field Sum is less than or equal to 0"); throw new ValidationException("Field Sum is less than or equal to 0");
if ((Products?.Count ?? 0) == 0)
throw new ValidationException("The sale must include products");
} }
}
}

View File

@@ -11,6 +11,6 @@ public enum PostType
None = 0, None = 0,
Preform = 1, // Заготовщик Preform = 1, // Заготовщик
Manufacturer = 2, // Изготовитель Manufacturer = 2, // Изготовитель
packer = 3 // Упаковщик Packer = 3 // Упаковщик
} }

View File

@@ -0,0 +1,18 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SladkieBulkiContrakts.Exceptions;
public class ElementExistsException : Exception
{
public string ParamName { get; private set; }
public string ParamValue { get; private set; }
public ElementExistsException(string paramName, string paramValue) : base($"There is already an element with value{paramValue} of parameter {paramName}")
{
ParamName = paramName;
ParamValue = paramValue;
}
}

View File

@@ -0,0 +1,17 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SladkieBulkiContrakts.Exceptions;
public class ElementNotFoundException : Exception
{
public string Value { get; private set; }
public ElementNotFoundException(string value) : base($"Element not found at value = { value}")
{
Value = value;
}
}

View File

@@ -0,0 +1,10 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
public class IncorrectDatesException : Exception
{
public IncorrectDatesException(DateTime start, DateTime end) : base($"The end date must be later than the start date.. StartDate: {start:dd.MM.YYYY}. EndDate: {end:dd.MM.YYYY}") { }
}

View File

@@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SladkieBulkiContrakts.Exceptions;
public class NullListException : Exception
{
public NullListException() : base("The returned list is null") { }
}

View File

@@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SladkieBulkiContrakts.Exceptions;
public class StorageException : Exception
{
public StorageException(Exception ex) : base($"Error while working in storage: { ex.Message}", ex) { }
}

View File

@@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SladkieBulkiContrakts.Extensions;
public static class DateTimeExtensions
{
public static bool IsDateNotOlder(this DateTime date, DateTime olderDate)
{
return date >= olderDate;
}
}

View File

@@ -0,0 +1,25 @@
using SladkieBulkiContrakts.DataModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SladkieBulkiContrakts.StoragesContarcts;
public interface IIngredientStorageContract
{
List<IngredientDataModel> GetList();
// Получить ингредиенты по единице измерения
List<IngredientDataModel>? GetElementBySizeUnit(string sizeInit);
IngredientDataModel? GetElementById(string id);
IngredientDataModel? GetElementByName(string name);
void AddElement(IngredientDataModel ingredientDataModel);
void UpdElement(IngredientDataModel ingredientDataModel);
}

View File

@@ -0,0 +1,21 @@
using SladkieBulkiContrakts.DataModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SladkieBulkiContrakts.StoragesContarcts;
public interface IPostStorageContract
{
List<PostDataModel> GetList(bool onlyActual = true);
List<PostDataModel> GetPostWithHistory(string postId);
PostDataModel? GetElementById(string id);
PostDataModel? GetElementByName(string name);
void AddElement(PostDataModel postDataModel);
void UpdElement(PostDataModel postDataModel);
void DelElement(string id);
void ResElement(string id);
}

View File

@@ -0,0 +1,21 @@
using SladkieBulkiContrakts.DataModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SladkieBulkiContrakts.StoragesContarcts;
public interface IProductStorageContract
{
List<ProductDataModel> GetList(bool onlyActive = true);
List<ProductHistoryDataModel> GetHistoryByProductId(string productId);
ProductDataModel? GetElementById(string id);
ProductDataModel? GetElementByName(string name);
void AddElement(ProductDataModel productDataModel);
void UpdElement(ProductDataModel productDataModel);
void DelElement(string id);
}

View File

@@ -0,0 +1,16 @@
using SladkieBulkiContrakts.DataModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SladkieBulkiContrakts.StoragesContarcts;
public interface IProductionStorageContract
{
List<ProductionDataModel> GetList(DateTime? startDate = null, DateTime? endDate = null, string? workerId = null, string? productId = null);
ProductionDataModel? GetElementById(string id);
void AddElement(ProductionDataModel productionDataModel);
void DelElement(string id);
}

View File

@@ -0,0 +1,14 @@
using SladkieBulkiContrakts.DataModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SladkieBulkiContrakts.StoragesContarcts;
public interface ISalaryStorageContract
{
List<SalaryDataModel> GetList(DateTime startDate, DateTime endDate, string? workerId = null);
void AddElement(SalaryDataModel salaryDataModel);
}

View File

@@ -0,0 +1,18 @@
using SladkieBulkiContrakts.DataModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SladkieBulkiContrakts.StoragesContarcts;
public interface IWorkerStorageContract
{
List<WorkerDataModel> GetList(bool onlyActive = true, string? postId = null, DateTime? fromBirthDate = null, DateTime? toBirthDate = null, DateTime? fromEmploymentDate = null, DateTime? toEmploymentDate = null);
WorkerDataModel? GetElementById(string id);
WorkerDataModel? GetElementByFIO(string fio);
void AddElement(WorkerDataModel workerDataModel);
void UpdElement(WorkerDataModel workerDataModel);
void DelElement(string id);
}

View File

@@ -0,0 +1,384 @@
using Moq;
using SladkieBulkiBusinessLogic.Implementations;
using SladkieBulkiContrakts.StoragesContarcts;
using SladkieBulkiContrakts.DataModels;
using Microsoft.Extensions.Logging;
using SladkieBulkiContrakts.Exceptions;
namespace SladkieBulkiTests.BusinessLogicsContractsTests;
[TestFixture]
internal class IngredientBusinessLogicContractTests
{
private IngredientBusinessLogicContract _ingredientBusinessLogicContract;
private Mock<IIngredientStorageContract> _ingredientStorageContract;
[OneTimeSetUp]
public void OneTimeSetUp()
{
_ingredientStorageContract = new Mock<IIngredientStorageContract>();
_ingredientBusinessLogicContract = new IngredientBusinessLogicContract(_ingredientStorageContract.Object, new Mock<ILogger>().Object);
}
[SetUp]
public void SetUp()
{
_ingredientStorageContract.Reset();
}
[Test]
public void GetAllIngredients_ReturnListOfRecords_Test()
{
// Arrange
var listOriginal = new List<IngredientDataModel>()
{
new IngredientDataModel(Guid.NewGuid().ToString(), "Sugar", "kg", 100),
new IngredientDataModel(Guid.NewGuid().ToString(), "Flour", "kg", 50),
new IngredientDataModel(Guid.NewGuid().ToString(), "Butter", "kg", 30),
};
_ingredientStorageContract.Setup(x => x.GetList()).Returns(listOriginal);
// Act
var list = _ingredientBusinessLogicContract.GetAllIngredients();
// Assert
Assert.That(list, Is.Not.Null);
Assert.That(list, Is.EquivalentTo(listOriginal));
}
[Test]
public void GetAllIngredients_ReturnEmptyList_Test()
{
//Arrange
_ingredientStorageContract.Setup(x => x.GetList()).Returns([]);
//Act
var list = _ingredientBusinessLogicContract.GetAllIngredients();
//Assert
Assert.That(list, Is.Not.Null);
Assert.That(list, Has.Count.EqualTo(0));
_ingredientStorageContract.Verify(x => x.GetList(), Times.Once);
}
[Test]
public void GetAllBuyers_ReturnNull_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _ingredientBusinessLogicContract.GetAllIngredients(), Throws.TypeOf<NullListException>());
_ingredientStorageContract.Verify(x => x.GetList(), Times.Once);
}
[Test]
public void GetAllIngredients_StorageThrowError_ThrowException_Test()
{
//Arrange
_ingredientStorageContract.Setup(x => x.GetList()).Throws(new StorageException(new InvalidOperationException()));
//Act&Assert
Assert.That(() => _ingredientBusinessLogicContract.GetAllIngredients(), Throws.TypeOf<StorageException>());
_ingredientStorageContract.Verify(x => x.GetList(), Times.Once);
}
[Test]
public void GetIngredientsBySizeUnit_ReturnListOfIngredients_Test()
{
// Arrange
var sizeUnit = "kg";
var ingredientsList = new List<IngredientDataModel>
{
new IngredientDataModel(Guid.NewGuid().ToString(), "Sugar", "kg", 10),
new IngredientDataModel(Guid.NewGuid().ToString(), "Flour", "kg", 15),
};
_ingredientStorageContract.Setup(x => x.GetElementBySizeUnit(sizeUnit)).Returns(ingredientsList);
// Act
var result = _ingredientBusinessLogicContract.GetIngredientsBySizeUnit(sizeUnit);
// Assert
Assert.That(result, Is.Not.Null);
Assert.That(result.Count, Is.EqualTo(2));
Assert.That(result[0].SizeInit, Is.EqualTo(sizeUnit));
Assert.That(result[1].SizeInit, Is.EqualTo(sizeUnit));
_ingredientStorageContract.Verify(x => x.GetElementBySizeUnit(It.IsAny<string>()), Times.Once);
}
[Test]
public void GetIngredientById_GetById_ReturnRecord_Test()
{
// Arrange
var id = Guid.NewGuid().ToString();
var record = new IngredientDataModel(id, "Sugar", "kg", 100);
_ingredientStorageContract.Setup(x => x.GetElementById(id)).Returns(record);
// Act
var element = _ingredientBusinessLogicContract.GetIngredientById(id);
// Assert
Assert.That(element, Is.Not.Null);
Assert.That(element.Id, Is.EqualTo(id));
_ingredientStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Once);
}
[Test]
public void GetIngredientByName_GetByName_ReturnRecord_Test()
{
// Arrange
var name = "Sugar";
var record = new IngredientDataModel(Guid.NewGuid().ToString(), name, "kg", 100);
_ingredientStorageContract.Setup(x =>
x.GetElementByName(name)).Returns(record);
// Act
var element = _ingredientBusinessLogicContract.GetIngredientByName(name);
// Assert
Assert.That(element, Is.Not.Null);
Assert.That(element.NameIngridients, Is.EqualTo(name));
_ingredientStorageContract.Verify(x =>
x.GetElementByName(It.IsAny<string>()), Times.Once);
}
[Test]
public void GetIngredientByData_EmptyData_ThrowException_Test()
{
// Act & Assert
Assert.That(() => _ingredientBusinessLogicContract.GetIngredientById(null),
Throws.TypeOf<ArgumentNullException>());
Assert.That(() => _ingredientBusinessLogicContract.GetIngredientByName(string.Empty),
Throws.TypeOf<ArgumentNullException>());
_ingredientStorageContract.Verify(x =>
x.GetElementById(It.IsAny<string>()), Times.Never);
_ingredientStorageContract.Verify(x =>
x.GetElementByName(It.IsAny<string>()), Times.Never);
}
[Test]
public void GetIngredientById_GetById_NotFoundRecord_ThrowException_Test()
{
// Act & Assert
Assert.That(() =>
_ingredientBusinessLogicContract.GetIngredientById(Guid.NewGuid().ToString()),
Throws.TypeOf<ElementNotFoundException>());
_ingredientStorageContract.Verify(x =>
x.GetElementById(It.IsAny<string>()), Times.Once);
_ingredientStorageContract.Verify(x =>
x.GetElementByName(It.IsAny<string>()), Times.Never);
}
[Test]
public void GetIngredientByName_GetByName_NotFoundRecord_ThrowException_Test()
{
// Act & Assert
Assert.That(() => _ingredientBusinessLogicContract.GetIngredientByName("Sugar"),
Throws.TypeOf<ElementNotFoundException>());
_ingredientStorageContract.Verify(x =>
x.GetElementById(It.IsAny<string>()), Times.Never);
_ingredientStorageContract.Verify(x =>
x.GetElementByName(It.IsAny<string>()), Times.Once);
}
[Test]
public void GetIngredientByData_StorageThrowError_ThrowException_Test()
{
// Arrange
_ingredientStorageContract.Setup(x =>
x.GetElementById(It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException()));
_ingredientStorageContract.Setup(x =>
x.GetElementByName(It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException()));
// Act & Assert
Assert.That(() =>
_ingredientBusinessLogicContract.GetIngredientById(Guid.NewGuid().ToString()),
Throws.TypeOf<StorageException>());
Assert.That(() => _ingredientBusinessLogicContract.GetIngredientByName("Sugar"),
Throws.TypeOf<StorageException>());
_ingredientStorageContract.Verify(x =>
x.GetElementById(It.IsAny<string>()), Times.Once);
_ingredientStorageContract.Verify(x =>
x.GetElementByName(It.IsAny<string>()), Times.Once);
}
[Test]
public void InsertIngredient_CorrectRecord_Test()
{
// Arrange
var flag = false;
var record = new IngredientDataModel(Guid.NewGuid().ToString(), "Sugar", "kg", 120.5);
_ingredientStorageContract.Setup(x =>
x.AddElement(It.IsAny<IngredientDataModel>()))
.Callback((IngredientDataModel x) =>
{
flag = x.Id == record.Id &&
x.NameIngridients == record.NameIngridients &&
x.SizeInit == record.SizeInit &&
x.InitPrice == record.InitPrice;
});
// Act
_ingredientBusinessLogicContract.InsertIngredient(record);
// Assert
_ingredientStorageContract.Verify(x =>
x.AddElement(It.IsAny<IngredientDataModel>()), Times.Once);
Assert.That(flag);
}
[Test]
public void InsertIngredient_RecordWithExistsData_ThrowException_Test()
{
// Arrange
_ingredientStorageContract.Setup(x =>
x.AddElement(It.IsAny<IngredientDataModel>()))
.Throws(new ElementExistsException("Data", "Data"));
// Act & Assert
Assert.That(() =>
_ingredientBusinessLogicContract.InsertIngredient(
new(Guid.NewGuid().ToString(), "Sugar", "kg", 120.5)),
Throws.TypeOf<ElementExistsException>());
_ingredientStorageContract.Verify(x =>
x.AddElement(It.IsAny<IngredientDataModel>()), Times.Once);
}
[Test]
public void InsertIngredient_NullRecord_ThrowException_Test()
{
// Act & Assert
Assert.That(() => _ingredientBusinessLogicContract.InsertIngredient(null),
Throws.TypeOf<ArgumentNullException>());
_ingredientStorageContract.Verify(x =>
x.AddElement(It.IsAny<IngredientDataModel>()), Times.Never);
}
[Test]
public void InsertIngredient_InvalidRecord_ThrowException_Test()
{
// Act & Assert
Assert.That(() => _ingredientBusinessLogicContract.InsertIngredient(
new("id", "", "", -10)),
Throws.TypeOf<ValidationException>());
_ingredientStorageContract.Verify(x =>
x.AddElement(It.IsAny<IngredientDataModel>()), Times.Never);
}
[Test]
public void InsertIngredient_StorageThrowError_ThrowException_Test()
{
// Arrange
_ingredientStorageContract.Setup(x =>
x.AddElement(It.IsAny<IngredientDataModel>()))
.Throws(new StorageException(new InvalidOperationException()));
// Act & Assert
Assert.That(() =>
_ingredientBusinessLogicContract.InsertIngredient(
new(Guid.NewGuid().ToString(), "Sugar", "kg", 120.5)),
Throws.TypeOf<StorageException>());
_ingredientStorageContract.Verify(x =>
x.AddElement(It.IsAny<IngredientDataModel>()), Times.Once);
}
[Test]
public void UpdateIngredient_CorrectRecord_Test()
{
// Arrange
var flag = false;
var record = new IngredientDataModel(Guid.NewGuid().ToString(), "Sugar", "kg", 120.5);
_ingredientStorageContract.Setup(x =>
x.UpdElement(It.IsAny<IngredientDataModel>()))
.Callback((IngredientDataModel x) =>
{
flag = x.Id == record.Id &&
x.NameIngridients == record.NameIngridients &&
x.SizeInit == record.SizeInit &&
x.InitPrice == record.InitPrice;
});
// Act
_ingredientBusinessLogicContract.UpdateIngredient(record);
// Assert
_ingredientStorageContract.Verify(x =>
x.UpdElement(It.IsAny<IngredientDataModel>()), Times.Once);
Assert.That(flag);
}
[Test]
public void UpdateIngredient_RecordWithIncorrectData_ThrowException_Test()
{
// Arrange
_ingredientStorageContract.Setup(x =>
x.UpdElement(It.IsAny<IngredientDataModel>()))
.Throws(new ElementNotFoundException(""));
// Act & Assert
Assert.That(() =>
_ingredientBusinessLogicContract.UpdateIngredient(
new(Guid.NewGuid().ToString(), "Sugar", "kg", 120.5)),
Throws.TypeOf<ElementNotFoundException>());
_ingredientStorageContract.Verify(x =>
x.UpdElement(It.IsAny<IngredientDataModel>()), Times.Once);
}
[Test]
public void UpdateIngredient_RecordWithExistsData_ThrowException_Test()
{
// Arrange
_ingredientStorageContract.Setup(x =>
x.UpdElement(It.IsAny<IngredientDataModel>()))
.Throws(new ElementExistsException("Data", "Data"));
// Act & Assert
Assert.That(() =>
_ingredientBusinessLogicContract.UpdateIngredient(
new(Guid.NewGuid().ToString(), "Sugar", "kg", 120.5)),
Throws.TypeOf<ElementExistsException>());
_ingredientStorageContract.Verify(x =>
x.UpdElement(It.IsAny<IngredientDataModel>()), Times.Once);
}
[Test]
public void UpdateIngredient_NullRecord_ThrowException_Test()
{
// Act & Assert
Assert.That(() => _ingredientBusinessLogicContract.UpdateIngredient(null),
Throws.TypeOf<ArgumentNullException>());
_ingredientStorageContract.Verify(x =>
x.UpdElement(It.IsAny<IngredientDataModel>()), Times.Never);
}
[Test]
public void UpdateIngredient_InvalidRecord_ThrowException_Test()
{
// Act & Assert
Assert.That(() => _ingredientBusinessLogicContract.UpdateIngredient(
new("id", "", "", -10)),
Throws.TypeOf<ValidationException>());
_ingredientStorageContract.Verify(x =>
x.UpdElement(It.IsAny<IngredientDataModel>()), Times.Never);
}
[Test]
public void UpdateIngredient_StorageThrowError_ThrowException_Test()
{
// Arrange
_ingredientStorageContract.Setup(x =>
x.UpdElement(It.IsAny<IngredientDataModel>()))
.Throws(new StorageException(new InvalidOperationException()));
// Act & Assert
Assert.That(() =>
_ingredientBusinessLogicContract.UpdateIngredient(
new(Guid.NewGuid().ToString(), "Sugar", "kg", 120.5)),
Throws.TypeOf<StorageException>());
_ingredientStorageContract.Verify(x =>
x.UpdElement(It.IsAny<IngredientDataModel>()), Times.Once);
}
}

View File

@@ -0,0 +1,456 @@
using Moq;
using SladkieBulkiBusinessLogic.Implementations;
using SladkieBulkiContrakts.StoragesContarcts;
using SladkieBulkiContrakts.DataModels;
using Microsoft.Extensions.Logging;
using SladkieBulkiContrakts.Exceptions;
using SladkieBulkiContrakts.Enums;
namespace SladkieBulkiTests.BusinessLogicsContractsTests;
[TestFixture]
internal class PostBusinessLogicContractTests
{
private PostBusinessLogicContract _postBusinessLogicContract;
private Mock<IPostStorageContract> _postStorageContract;
[OneTimeSetUp]
public void OneTimeSetUp()
{
_postStorageContract = new Mock<IPostStorageContract>();
_postBusinessLogicContract = new PostBusinessLogicContract(_postStorageContract.Object, new Mock<ILogger>().Object);
}
[SetUp]
public void SetUp()
{
_postStorageContract.Reset();
}
[Test]
public void GetAllPosts_ReturnListOfRecords_Test()
{
//Arrange
var listOriginal = new List<PostDataModel>()
{
new(Guid.NewGuid().ToString(),"name 1", PostType.Preform, 10, true, DateTime.UtcNow),
new(Guid.NewGuid().ToString(), "name 2", PostType.Preform, 10, false, DateTime.UtcNow),
new(Guid.NewGuid().ToString(), "name 3", PostType.Preform, 10, true, DateTime.UtcNow),
};
_postStorageContract.Setup(x => x.GetList(It.IsAny<bool>())).Returns(listOriginal);
//Act
var listOnlyActive = _postBusinessLogicContract.GetAllPosts(true);
var listAll = _postBusinessLogicContract.GetAllPosts(false);
//Assert
Assert.Multiple(() =>
{
Assert.That(listOnlyActive, Is.Not.Null);
Assert.That(listAll, Is.Not.Null);
Assert.That(listOnlyActive, Is.EquivalentTo(listOriginal));
Assert.That(listAll, Is.EquivalentTo(listOriginal));
});
_postStorageContract.Verify(x => x.GetList(true), Times.Once);
_postStorageContract.Verify(x => x.GetList(false), Times.Once);
}
[Test]
public void GetAllPosts_ReturnEmptyList_Test()
{
//Arrange
_postStorageContract.Setup(x => x.GetList(It.IsAny<bool>())).Returns([]);
//Act
var listOnlyActive = _postBusinessLogicContract.GetAllPosts(true);
var listAll = _postBusinessLogicContract.GetAllPosts(false);
//Assert
Assert.Multiple(() =>
{
Assert.That(listOnlyActive, Is.Not.Null);
Assert.That(listAll, Is.Not.Null);
Assert.That(listOnlyActive, Has.Count.EqualTo(0));
Assert.That(listAll, Has.Count.EqualTo(0));
});
_postStorageContract.Verify(x => x.GetList(It.IsAny<bool>()), Times.Exactly(2));
}
[Test]
public void GetAllPosts_ReturnNull_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _postBusinessLogicContract.GetAllPosts(It.IsAny<bool>()), Throws.TypeOf<NullListException>());
_postStorageContract.Verify(x => x.GetList(It.IsAny<bool>()), Times.Once);
}
[Test]
public void GetAllPosts_StorageThrowError_ThrowException_Test()
{
//Arrange
_postStorageContract.Setup(x => x.GetList(It.IsAny<bool>())).Throws(new StorageException(new InvalidOperationException()));
//Act&Assert
Assert.That(() => _postBusinessLogicContract.GetAllPosts(It.IsAny<bool>()), Throws.TypeOf<StorageException>());
_postStorageContract.Verify(x => x.GetList(It.IsAny<bool>()), Times.Once);
}
[Test]
public void GetAllDataOfPost_ReturnListOfRecords_Test()
{
//Arrange
var postId = Guid.NewGuid().ToString();
var listOriginal = new List<PostDataModel>()
{
new(postId, "name 1", PostType.Preform, 10, true, DateTime.UtcNow),
new(postId, "name 2", PostType.Preform, 10, false, DateTime.UtcNow)
};
_postStorageContract.Setup(x => x.GetPostWithHistory(It.IsAny<string>())).Returns(listOriginal);
//Act
var list = _postBusinessLogicContract.GetAllDataOfPost(postId);
//Assert
Assert.That(list, Is.Not.Null);
Assert.That(list, Has.Count.EqualTo(2));
_postStorageContract.Verify(x => x.GetPostWithHistory(postId), Times.Once);
}
[Test]
public void GetAllDataOfPost_ReturnEmptyList_Test()
{
//Arrange
_postStorageContract.Setup(x => x.GetPostWithHistory(It.IsAny<string>())).Returns([]);
//Act
var list = _postBusinessLogicContract.GetAllDataOfPost(Guid.NewGuid().ToString());
//Assert
Assert.That(list, Is.Not.Null);
Assert.That(list, Has.Count.EqualTo(0));
_postStorageContract.Verify(x => x.GetPostWithHistory(It.IsAny<string>()), Times.Once);
}
[Test]
public void GetAllDataOfPost_PostIdIsNullOrEmpty_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _postBusinessLogicContract.GetAllDataOfPost(null), Throws.TypeOf<ArgumentNullException>());
Assert.That(() => _postBusinessLogicContract.GetAllDataOfPost(string.Empty), Throws.TypeOf<ArgumentNullException>());
_postStorageContract.Verify(x => x.GetPostWithHistory(It.IsAny<string>()), Times.Never);
}
[Test]
public void GetAllDataOfPost_PostIdIsNotGuid_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _postBusinessLogicContract.GetAllDataOfPost("id"), Throws.TypeOf<ValidationException>());
_postStorageContract.Verify(x => x.GetPostWithHistory(It.IsAny<string>()), Times.Never);
}
[Test]
public void GetAllDataOfPost_ReturnNull_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _postBusinessLogicContract.GetAllDataOfPost(Guid.NewGuid().ToString()), Throws.TypeOf<NullListException>());
_postStorageContract.Verify(x => x.GetPostWithHistory(It.IsAny<string>()), Times.Once);
}
[Test]
public void GetAllDataOfPost_StorageThrowError_ThrowException_Test()
{
//Arrange
_postStorageContract.Setup(x => x.GetPostWithHistory(It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException()));
//Act&Assert
Assert.That(() => _postBusinessLogicContract.GetAllDataOfPost(Guid.NewGuid().ToString()), Throws.TypeOf<StorageException>());
_postStorageContract.Verify(x => x.GetPostWithHistory(It.IsAny<string>()), Times.Once);
}
[Test]
public void GetPostByData_GetById_ReturnRecord_Test()
{
//Arrange
var id = Guid.NewGuid().ToString();
var record = new PostDataModel(id, "name", PostType.Preform, 10, true, DateTime.UtcNow);
_postStorageContract.Setup(x => x.GetElementById(id)).Returns(record);
//Act
var element = _postBusinessLogicContract.GetPostByData(id);
//Assert
Assert.That(element, Is.Not.Null);
Assert.That(element.Id, Is.EqualTo(id));
_postStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Once);
}
[Test]
public void GetPostByData_GetByName_ReturnRecord_Test()
{
//Arrange
var postName = "name";
var record = new PostDataModel(Guid.NewGuid().ToString(), postName, PostType.Preform, 10, true, DateTime.UtcNow);
_postStorageContract.Setup(x => x.GetElementByName(postName)).Returns(record);
//Act
var element = _postBusinessLogicContract.GetPostByData(postName);
//Assert
Assert.That(element, Is.Not.Null);
Assert.That(element.PostName, Is.EqualTo(postName));
_postStorageContract.Verify(x => x.GetElementByName(It.IsAny<string>()), Times.Once);
}
[Test]
public void GetPostByData_EmptyData_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _postBusinessLogicContract.GetPostByData(null), Throws.TypeOf<ArgumentNullException>());
Assert.That(() => _postBusinessLogicContract.GetPostByData(string.Empty), Throws.TypeOf<ArgumentNullException>());
_postStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Never);
_postStorageContract.Verify(x => x.GetElementByName(It.IsAny<string>()), Times.Never);
}
[Test]
public void GetPostByData_GetById_NotFoundRecord_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _postBusinessLogicContract.GetPostByData(Guid.NewGuid().ToString()), Throws.TypeOf<ElementNotFoundException>());
_postStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Once);
_postStorageContract.Verify(x => x.GetElementByName(It.IsAny<string>()), Times.Never);
}
[Test]
public void GetPostByData_GetByName_NotFoundRecord_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _postBusinessLogicContract.GetPostByData("name"), Throws.TypeOf<ElementNotFoundException>());
_postStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Never);
_postStorageContract.Verify(x => x.GetElementByName(It.IsAny<string>()), Times.Once);
}
[Test]
public void GetPostByData_StorageThrowError_ThrowException_Test()
{
//Arrange
_postStorageContract.Setup(x => x.GetElementById(It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException()));
_postStorageContract.Setup(x => x.GetElementByName(It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException()));
//Act&Assert
Assert.That(() => _postBusinessLogicContract.GetPostByData(Guid.NewGuid().ToString()), Throws.TypeOf<StorageException>());
Assert.That(() => _postBusinessLogicContract.GetPostByData("name"), Throws.TypeOf<StorageException>());
_postStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Once);
_postStorageContract.Verify(x => x.GetElementByName(It.IsAny<string>()), Times.Once);
}
[Test]
public void InsertPost_CorrectRecord_Test()
{
//Arrange
var flag = false;
var record = new PostDataModel(Guid.NewGuid().ToString(), "name", PostType.Manufacturer, 10, true, DateTime.UtcNow.AddDays(-1));
_postStorageContract.Setup(x => x.AddElement(It.IsAny<PostDataModel>()))
.Callback((PostDataModel x) =>
{
flag = x.Id == record.Id && x.PostName == record.PostName && x.PostType == record.PostType && x.Salary == record.Salary &&
x.ChangeDate == record.ChangeDate;
});
//Act
_postBusinessLogicContract.InsertPost(record);
//Assert
_postStorageContract.Verify(x => x.AddElement(It.IsAny<PostDataModel>()), Times.Once);
Assert.That(flag);
}
[Test]
public void InsertPost_RecordWithExistsData_ThrowException_Test()
{
//Arrange
_postStorageContract.Setup(x => x.AddElement(It.IsAny<PostDataModel>())).Throws(new ElementExistsException("Data", "Data"));
//Act&Assert
Assert.That(() => _postBusinessLogicContract.InsertPost(new(Guid.NewGuid().ToString(), "name", PostType.Manufacturer, 10, true, DateTime.UtcNow)), Throws.TypeOf<ElementExistsException>());
_postStorageContract.Verify(x => x.AddElement(It.IsAny<PostDataModel>()), Times.Once);
}
[Test]
public void InsertPost_NullRecord_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _postBusinessLogicContract.InsertPost(null), Throws.TypeOf<ArgumentNullException>());
_postStorageContract.Verify(x => x.AddElement(It.IsAny<PostDataModel>()), Times.Never);
}
[Test]
public void InsertPost_InvalidRecord_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _postBusinessLogicContract.InsertPost(new PostDataModel("id", "name", PostType.Manufacturer, 10, true, DateTime.UtcNow)), Throws.TypeOf<ValidationException>());
_postStorageContract.Verify(x => x.AddElement(It.IsAny<PostDataModel>()), Times.Never);
}
[Test]
public void InsertPost_StorageThrowError_ThrowException_Test()
{
//Arrange
_postStorageContract.Setup(x => x.AddElement(It.IsAny<PostDataModel>())).Throws(new StorageException(new InvalidOperationException()));
//Act&Assert
Assert.That(() => _postBusinessLogicContract.InsertPost(new(Guid.NewGuid().ToString(), "name", PostType.Manufacturer, 10, true, DateTime.UtcNow)), Throws.TypeOf<StorageException>());
_postStorageContract.Verify(x => x.AddElement(It.IsAny<PostDataModel>()), Times.Once);
}
[Test]
public void UpdatePost_CorrectRecord_Test()
{
//Arrange
var flag = false;
var record = new PostDataModel(Guid.NewGuid().ToString(), "name", PostType.Manufacturer, 10, true, DateTime.UtcNow.AddDays(-1));
_postStorageContract.Setup(x => x.UpdElement(It.IsAny<PostDataModel>()))
.Callback((PostDataModel x) =>
{
flag = x.Id == record.Id && x.PostName == record.PostName && x.PostType == record.PostType && x.Salary == record.Salary &&
x.ChangeDate == record.ChangeDate;
});
//Act
_postBusinessLogicContract.UpdatePost(record);
//Assert
_postStorageContract.Verify(x => x.UpdElement(It.IsAny<PostDataModel>()), Times.Once);
Assert.That(flag);
}
[Test]
public void UpdatePost_RecordWithIncorrectData_ThrowException_Test()
{
//Arrange
_postStorageContract.Setup(x => x.UpdElement(It.IsAny<PostDataModel>())).Throws(new ElementNotFoundException(""));
//Act&Assert
Assert.That(() => _postBusinessLogicContract.UpdatePost(new(Guid.NewGuid().ToString(), "name", PostType.Manufacturer, 10, true, DateTime.UtcNow)), Throws.TypeOf<ElementNotFoundException>());
_postStorageContract.Verify(x => x.UpdElement(It.IsAny<PostDataModel>()), Times.Once);
}
[Test]
public void UpdatePost_RecordWithExistsData_ThrowException_Test()
{
//Arrange
_postStorageContract.Setup(x => x.UpdElement(It.IsAny<PostDataModel>())).Throws(new ElementExistsException("Data", "Data"));
//Act&Assert
Assert.That(() => _postBusinessLogicContract.UpdatePost(new(Guid.NewGuid().ToString(), "anme", PostType.Manufacturer, 10, true, DateTime.UtcNow)), Throws.TypeOf<ElementExistsException>());
_postStorageContract.Verify(x => x.UpdElement(It.IsAny<PostDataModel>()), Times.Once);
}
[Test]
public void UpdatePost_NullRecord_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _postBusinessLogicContract.UpdatePost(null), Throws.TypeOf<ArgumentNullException>());
_postStorageContract.Verify(x => x.UpdElement(It.IsAny<PostDataModel>()), Times.Never);
}
[Test]
public void UpdatePost_InvalidRecord_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _postBusinessLogicContract.UpdatePost(new PostDataModel("id", "name", PostType.Manufacturer, 10, true, DateTime.UtcNow)), Throws.TypeOf<ValidationException>());
_postStorageContract.Verify(x => x.UpdElement(It.IsAny<PostDataModel>()), Times.Never);
}
[Test]
public void UpdatePost_StorageThrowError_ThrowException_Test()
{
//Arrange
_postStorageContract.Setup(x => x.UpdElement(It.IsAny<PostDataModel>())).Throws(new StorageException(new InvalidOperationException()));
//Act&Assert
Assert.That(() => _postBusinessLogicContract.UpdatePost(new(Guid.NewGuid().ToString(), "name", PostType.Manufacturer, 10, true, DateTime.UtcNow)), Throws.TypeOf<StorageException>());
_postStorageContract.Verify(x => x.UpdElement(It.IsAny<PostDataModel>()), Times.Once);
}
[Test]
public void DeletePost_CorrectRecord_Test()
{
//Arrange
var id = Guid.NewGuid().ToString();
var flag = false;
_postStorageContract.Setup(x => x.DelElement(It.Is((string x) => x == id))).Callback(() => { flag = true; });
//Act
_postBusinessLogicContract.DeletePost(id);
//Assert
_postStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Once);
Assert.That(flag);
}
[Test]
public void DeletePost_RecordWithIncorrectId_ThrowException_Test()
{
//Arrange
var id = Guid.NewGuid().ToString();
_postStorageContract.Setup(x => x.DelElement(It.IsAny<string>())).Throws(new ElementNotFoundException(id));
//Act&Assert
Assert.That(() => _postBusinessLogicContract.DeletePost(Guid.NewGuid().ToString()), Throws.TypeOf<ElementNotFoundException>());
_postStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Once);
}
[Test]
public void DeletePost_IdIsNullOrEmpty_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _postBusinessLogicContract.DeletePost(null), Throws.TypeOf<ArgumentNullException>());
Assert.That(() => _postBusinessLogicContract.DeletePost(string.Empty), Throws.TypeOf<ArgumentNullException>());
_postStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Never);
}
[Test]
public void DeletePost_IdIsNotGuid_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _postBusinessLogicContract.DeletePost("id"), Throws.TypeOf<ValidationException>());
_postStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Never);
}
[Test]
public void DeletePost_StorageThrowError_ThrowException_Test()
{
//Arrange
_postStorageContract.Setup(x => x.DelElement(It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException()));
//Act&Assert
Assert.That(() => _postBusinessLogicContract.DeletePost(Guid.NewGuid().ToString()), Throws.TypeOf<StorageException>());
_postStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Once);
}
[Test]
public void RestorePost_CorrectRecord_Test()
{
//Arrange
var id = Guid.NewGuid().ToString();
var flag = false;
_postStorageContract.Setup(x => x.ResElement(It.Is((string x) => x == id))).Callback(() => { flag = true; });
//Act
_postBusinessLogicContract.RestorePost(id);
//Assert
_postStorageContract.Verify(x => x.ResElement(It.IsAny<string>()), Times.Once);
Assert.That(flag);
}
[Test]
public void RestorePost_RecordWithIncorrectId_ThrowException_Test()
{
//Arrange
var id = Guid.NewGuid().ToString();
_postStorageContract.Setup(x => x.ResElement(It.IsAny<string>())).Throws(new ElementNotFoundException(id));
//Act&Assert
Assert.That(() => _postBusinessLogicContract.RestorePost(Guid.NewGuid().ToString()), Throws.TypeOf<ElementNotFoundException>());
_postStorageContract.Verify(x => x.ResElement(It.IsAny<string>()), Times.Once);
}
[Test]
public void RestorePost_IdIsNullOrEmpty_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _postBusinessLogicContract.RestorePost(null), Throws.TypeOf<ArgumentNullException>());
Assert.That(() => _postBusinessLogicContract.RestorePost(string.Empty), Throws.TypeOf<ArgumentNullException>());
_postStorageContract.Verify(x => x.ResElement(It.IsAny<string>()), Times.Never);
}
[Test]
public void RestorePost_IdIsNotGuid_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _postBusinessLogicContract.RestorePost("id"), Throws.TypeOf<ValidationException>());
_postStorageContract.Verify(x => x.ResElement(It.IsAny<string>()), Times.Never);
}
[Test]
public void RestorePost_StorageThrowError_ThrowException_Test()
{
//Arrange
_postStorageContract.Setup(x => x.ResElement(It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException()));
//Act&Assert
Assert.That(() => _postBusinessLogicContract.RestorePost(Guid.NewGuid().ToString()), Throws.TypeOf<StorageException>());
_postStorageContract.Verify(x => x.ResElement(It.IsAny<string>()), Times.Once);
}
}

View File

@@ -0,0 +1,491 @@
using Microsoft.Extensions.Logging;
using Moq;
using SladkieBulkiBusinessLogic.Implementations;
using SladkieBulkiContrakts.DataModels;
using SladkieBulkiContrakts.Enums;
using SladkieBulkiContrakts.Exceptions;
using SladkieBulkiContrakts.StoragesContarcts;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SladkieBulkiTests.BusinessLogicsContractsTests;
[TestFixture]
internal class ProductBusinessLogicContractTests
{
private ProductBusinessLogicContract _productBusinessLogicContract;
private Mock<IProductStorageContract> _productStorageContract;
[OneTimeSetUp]
public void OneTimeSetUp()
{
_productStorageContract = new Mock<IProductStorageContract>();
_productBusinessLogicContract = new ProductBusinessLogicContract(_productStorageContract.Object, new Mock<ILogger>().Object);
}
[SetUp]
public void SetUp()
{
_productStorageContract.Reset();
}
[Test]
public void GetAllProducts_ReturnListOfRecords_Test()
{
// Arrange
var listOriginal = new List<ProductDataModel>
{
new(Guid.NewGuid().ToString(), "name 1", "description 1", ProductType.Workpiece, [new ProductIngredientDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)], 10, true),
new(Guid.NewGuid().ToString(), "name 2", "description 2", ProductType.Workpiece, [new ProductIngredientDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)], 10, false),
new(Guid.NewGuid().ToString(), "name 3", "description 3", ProductType.Workpiece, [new ProductIngredientDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)], 10, true)
};
_productStorageContract.Setup(x => x.GetList(true)).Returns(listOriginal);
_productStorageContract.Setup(x => x.GetList(false)).Returns(listOriginal);
// Act
var listOnlyActive = _productBusinessLogicContract.GetAllProducts(true);
var list = _productBusinessLogicContract.GetAllProducts(false);
// Assert
Assert.Multiple(() =>
{
Assert.That(listOnlyActive, Is.Not.Null);
Assert.That(list, Is.Not.Null);
Assert.That(listOnlyActive, Is.EquivalentTo(listOriginal));
Assert.That(list, Is.EquivalentTo(listOriginal));
});
_productStorageContract.Verify(x => x.GetList(true), Times.Once);
_productStorageContract.Verify(x => x.GetList(false), Times.Once);
}
[Test]
public void GetAllProducts_ReturnEmptyList_Test()
{
//Arrange
_productStorageContract.Setup(x => x.GetList(It.IsAny<bool>())).Returns([]);
//Act
var listOnlyActive = _productBusinessLogicContract.GetAllProducts(true);
var list = _productBusinessLogicContract.GetAllProducts(false);
//Assert
Assert.Multiple(() =>
{
Assert.That(listOnlyActive, Is.Not.Null);
Assert.That(list, Is.Not.Null);
Assert.That(listOnlyActive, Has.Count.EqualTo(0));
Assert.That(list, Has.Count.EqualTo(0));
});
_productStorageContract.Verify(x => x.GetList(It.IsAny<bool>()), Times.Exactly(2));
}
[Test]
public void GetAllProducts_ReturnNull_ThrowException_Test()
{
//Act&Assert
Assert.That(() =>
_productBusinessLogicContract.GetAllProducts(It.IsAny<bool>()),
Throws.TypeOf<NullListException>());
_productStorageContract.Verify(x => x.GetList(It.IsAny<bool>()), Times.Once);
}
[Test]
public void GetAllProducts_StorageThrowError_ThrowException_Test()
{
// Arrange
_productStorageContract.Setup(x => x.GetList(It.IsAny<bool>())).Throws(new StorageException(new InvalidOperationException()));
// Act & Assert
Assert.That(() => _productBusinessLogicContract.GetAllProducts(It.IsAny<bool>()), Throws.TypeOf<StorageException>());
_productStorageContract.Verify(x => x.GetList(It.IsAny<bool>()), Times.Once);
}
[Test]
public void GetProductHistoryByProduct_ReturnListOfRecords_Test()
{
//Arrange
var productId = Guid.NewGuid().ToString();
var listOriginal = new List<ProductHistoryDataModel>()
{
new(Guid.NewGuid().ToString(), 10),
new(Guid.NewGuid().ToString(), 15),
new(Guid.NewGuid().ToString(), 10),
};
_productStorageContract.Setup(x => x.GetHistoryByProductId(It.IsAny<string>())).Returns(listOriginal);
//Act
var list = _productBusinessLogicContract.GetProductHistoryByProduct(productId);
//Assert
Assert.That(list, Is.Not.Null);
Assert.That(list, Is.EquivalentTo(listOriginal));
_productStorageContract.Verify(x => x.GetHistoryByProductId(productId), Times.Once);
}
[Test]
public void GetProductHistoryByProduct_ReturnEmptyList_Test()
{
//Arrange
_productStorageContract.Setup(x => x.GetHistoryByProductId(It.IsAny<string>())).Returns([]);
//Act
var list = _productBusinessLogicContract.GetProductHistoryByProduct(Guid.NewGuid().ToString(
));
//Assert
Assert.That(list, Is.Not.Null);
Assert.That(list, Has.Count.EqualTo(0));
_productStorageContract.Verify(x => x.GetHistoryByProductId(It.IsAny<string>()), Times.Once);
}
[Test]
public void GetProductHistoryByProduct_ProductIdIsNullOrEmpty_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _productBusinessLogicContract.GetProductHistoryByProduct(null),
Throws.TypeOf<ArgumentNullException>());
Assert.That(() => _productBusinessLogicContract.GetProductHistoryByProduct(string.Empty),
Throws.TypeOf<ArgumentNullException>());
_productStorageContract.Verify(x => x.GetHistoryByProductId(It.IsAny<string>()), Times.Never);
}
[Test]
public void GetProductHistoryByProduct_ProductIdIsNotGuid_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _productBusinessLogicContract.GetProductHistoryByProduct("productId"),
Throws.TypeOf<ValidationException>());
_productStorageContract.Verify(x => x.GetHistoryByProductId(It.IsAny<string>()), Times.Never);
}
[Test]
public void GetProductHistoryByProduct_ReturnNull_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _productBusinessLogicContract.GetProductHistoryByProduct(Guid.NewGuid().ToString()), Throws.TypeOf<NullListException>());
_productStorageContract.Verify(x => x.GetHistoryByProductId(It.IsAny<string>()), Times.Once);
}
[Test]
public void GetProductHistoryByProduct_StorageThrowError_ThrowException_Test()
{
//Arrange
_productStorageContract.Setup(x => x.GetHistoryByProductId(It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException()));
//Act&Assert
Assert.That(() => _productBusinessLogicContract.GetProductHistoryByProduct(Guid.NewGuid().ToString()), Throws.TypeOf<StorageException>());
_productStorageContract.Verify(x => x.GetHistoryByProductId(It.IsAny<string>()), Times.Once);
}
[Test]
public void GetProductByData_GetById_ReturnRecord_Test()
{
// Arrange
var productId = Guid.NewGuid().ToString();
var expected = new ProductDataModel(productId, "Product Name", "Some description", ProductType.Workpiece, [new ProductIngredientDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)], 20, false);
_productStorageContract.Setup(x => x.GetElementById(productId)).Returns(expected);
// Act
var actual = _productBusinessLogicContract.GetProductByData(productId);
// Assert
Assert.Multiple(() =>
{
Assert.That(actual, Is.Not.Null);
Assert.That(actual.Id, Is.EqualTo(expected.Id));
Assert.That(actual.Name, Is.EqualTo(expected.Name));
Assert.That(actual.ProductType, Is.EqualTo(expected.ProductType));
Assert.That(actual.UnitPrice, Is.EqualTo(expected.UnitPrice));
Assert.That(actual.IsDeleted, Is.EqualTo(expected.IsDeleted));
Assert.That(actual.Ingredients, Is.EquivalentTo(expected.Ingredients));
});
_productStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Once);
}
[Test]
public void GetProductByData_GetByName_ReturnRecord_Test()
{
//Arrange
var name = "name";
var record = new ProductDataModel(Guid.NewGuid().ToString(), name, Guid.NewGuid().ToString(), ProductType.Workpiece, [new ProductIngredientDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)], 10, false);
_productStorageContract.Setup(x => x.GetElementByName(name)).Returns(record);
//Act
var element = _productBusinessLogicContract.GetProductByData(name);
//Assert
Assert.That(element, Is.Not.Null);
Assert.That(element.Name, Is.EqualTo(name));
_productStorageContract.Verify(x => x.GetElementByName(It.IsAny<string>()), Times.Once);
}
[Test]
public void GetProductByData_EmptyData_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _productBusinessLogicContract.GetProductByData(null), Throws.TypeOf<ArgumentNullException>());
Assert.That(() => _productBusinessLogicContract.GetProductByData(string.Empty), Throws.TypeOf<ArgumentNullException>());
_productStorageContract.Verify(x => x.GetElementByName(It.IsAny<string>()), Times.Never);
_productStorageContract.Verify(x => x.GetElementByName(It.IsAny<string>()), Times.Never);
}
[Test]
public void GetProductByData_GetById_NotFoundRecord_ThrowException_Test()
{
//Act&Assert
Assert.That(() =>
_productBusinessLogicContract.GetProductByData(Guid.NewGuid().ToString()),
Throws.TypeOf<ElementNotFoundException>());
_productStorageContract.Verify(x =>
x.GetElementById(It.IsAny<string>()), Times.Once);
}
[Test]
public void GetProductByData_GetByName_NotFoundRecord_ThrowException_Test()
{
//Act&Assert
Assert.That(() =>
_productBusinessLogicContract.GetProductByData("name"),
Throws.TypeOf<ElementNotFoundException>());
_productStorageContract.Verify(x =>
x.GetElementByName(It.IsAny<string>()), Times.Once);
}
[Test]
public void GetProductByData_StorageThrowError_ThrowException_Test()
{
//Arrange
_productStorageContract.Setup(x =>
x.GetElementById(It.IsAny<string>())).Throws(new StorageException(new
InvalidOperationException()));
_productStorageContract.Setup(x =>
x.GetElementByName(It.IsAny<string>())).Throws(new StorageException(new
InvalidOperationException()));
//Act&Assert
Assert.That(() =>
_productBusinessLogicContract.GetProductByData(Guid.NewGuid().ToString()),
Throws.TypeOf<StorageException>());
Assert.That(() =>
_productBusinessLogicContract.GetProductByData("name"),
Throws.TypeOf<StorageException>());
_productStorageContract.Verify(x =>
x.GetElementById(It.IsAny<string>()), Times.Once);
_productStorageContract.Verify(x =>
x.GetElementByName(It.IsAny<string>()), Times.Once);
}
[Test]
public void InsertProduct_CorrectRecord_Test()
{
// Arrange
var flag = false;
var record = new ProductDataModel(Guid.NewGuid().ToString(), "name", Guid.NewGuid().ToString(), ProductType.Workpiece, [new ProductIngredientDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)], 10, false);
_productStorageContract.Setup(x => x.AddElement(It.IsAny<ProductDataModel>()))
.Callback((ProductDataModel x) =>
{
flag = x.Id == record.Id && x.Name == record.Name && x.ProductType == record.ProductType && x.UnitPrice == record.UnitPrice && x.IsDeleted == record.IsDeleted;
});
// Act
_productBusinessLogicContract.InsertProduct(record);
// Assert
_productStorageContract.Verify(x => x.AddElement(It.IsAny<ProductDataModel>()), Times.Once);
Assert.That(flag);
}
[Test]
public void InsertProduct_RecordWithExistsData_ThrowException_Test()
{
// Arrange
_productStorageContract.Setup(x => x.AddElement(It.IsAny<ProductDataModel>())).Throws(new ElementExistsException("Data", "Data"));
// Act & Assert
Assert.That(() => _productBusinessLogicContract.InsertProduct(new(Guid.NewGuid().ToString(), "name", Guid.NewGuid().ToString(), ProductType.Workpiece, [new ProductIngredientDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)], 10, false)),
Throws.TypeOf<ElementExistsException>());
_productStorageContract.Verify(x => x.AddElement(It.IsAny<ProductDataModel>()), Times.Once);
}
[Test]
public void InsertProduct_NullRecord_ThrowException_Test()
{
// Act & Assert
Assert.That(() => _productBusinessLogicContract.InsertProduct(null), Throws.TypeOf<ArgumentNullException>());
_productStorageContract.Verify(x => x.AddElement(It.IsAny<ProductDataModel>()), Times.Never);
}
[Test]
public void InsertProduct_InvalidRecord_ThrowException_Test()
{
// Act & Assert
Assert.That(() => _productBusinessLogicContract.InsertProduct(new ProductDataModel("id", "name", Guid.NewGuid().ToString(), ProductType.Workpiece, [new ProductIngredientDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)], 10, false)),
Throws.TypeOf<ValidationException>());
_productStorageContract.Verify(x => x.AddElement(It.IsAny<ProductDataModel>()), Times.Never);
}
[Test]
public void InsertProduct_StorageThrowError_ThrowException_Test()
{
//Arrange
_productStorageContract.Setup(x => x.AddElement(It.IsAny<ProductDataModel>())).Throws(new StorageException(new
InvalidOperationException()));
//Act&Assert
Assert.That(() => _productBusinessLogicContract.InsertProduct(new(Guid.NewGuid().ToString(), "name", Guid.NewGuid().ToString(), ProductType.Workpiece, [new ProductIngredientDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)], 10, false)), Throws.TypeOf<StorageException>());
_productStorageContract.Verify(x => x.AddElement(It.IsAny<ProductDataModel>()), Times.Once);
}
[Test]
public void UpdateProduct_CorrectRecord_Test()
{
//Arrange
var flag = false;
var record = new ProductDataModel(Guid.NewGuid().ToString(), "name", Guid.NewGuid().ToString(), ProductType.Workpiece, [new ProductIngredientDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)], 10, false);
_productStorageContract.Setup(x => x.UpdElement(It.IsAny<ProductDataModel>())).Callback((ProductDataModel x) =>
{
flag = x.Id == record.Id && x.Name == record.Name && x.ProductType == record.ProductType && x.UnitPrice == record.UnitPrice && x.IsDeleted == record.IsDeleted;
});
//Act
_productBusinessLogicContract.UpdateProduct(record);
//Assert
_productStorageContract.Verify(x =>
x.UpdElement(It.IsAny<ProductDataModel>()), Times.Once);
Assert.That(flag);
}
[Test]
public void UpdateProduct_RecordWithIncorrectData_ThrowException_Test()
{
//Arrange
_productStorageContract.Setup(x =>
x.UpdElement(It.IsAny<ProductDataModel>())).Throws(new ElementNotFoundException(""));
//Act&Assert
Assert.That(() =>
_productBusinessLogicContract.UpdateProduct(new(Guid.NewGuid().ToString(), "name", Guid.NewGuid().ToString(), ProductType.Workpiece, [new ProductIngredientDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)], 10, false)),
Throws.TypeOf<ElementNotFoundException>());
_productStorageContract.Verify(x =>
x.UpdElement(It.IsAny<ProductDataModel>()), Times.Once);
}
[Test]
public void UpdateProduct_RecordWithExistsData_ThrowException_Test()
{
//Arrange
_productStorageContract.Setup(x =>
x.UpdElement(It.IsAny<ProductDataModel>())).Throws(new
ElementExistsException("Data", "Data"));
//Act&Assert
Assert.That(() =>
_productBusinessLogicContract.UpdateProduct(new(Guid.NewGuid().ToString(), "anme", Guid.NewGuid().ToString(), ProductType.Workpiece, [new ProductIngredientDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)], 10, false)),
Throws.TypeOf<ElementExistsException>());
_productStorageContract.Verify(x =>
x.UpdElement(It.IsAny<ProductDataModel>()), Times.Once);
}
[Test]
public void UpdateProduct_NullRecord_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _productBusinessLogicContract.UpdateProduct(null),
Throws.TypeOf<ArgumentNullException>());
_productStorageContract.Verify(x =>
x.UpdElement(It.IsAny<ProductDataModel>()), Times.Never);
}
[Test]
public void UpdateProduct_InvalidRecord_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _productBusinessLogicContract.UpdateProduct(new ProductDataModel("id", "name", Guid.NewGuid().ToString(), ProductType.Workpiece, [new ProductIngredientDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)], 10, false)), Throws.TypeOf<ValidationException>());
_productStorageContract.Verify(x =>
x.UpdElement(It.IsAny<ProductDataModel>()), Times.Never);
}
[Test]
public void UpdateProduct_StorageThrowError_ThrowException_Test()
{
//Arrange
_productStorageContract.Setup(x =>
x.UpdElement(It.IsAny<ProductDataModel>())).Throws(new StorageException(new
InvalidOperationException()));
//Act&Assert
Assert.That(() =>
_productBusinessLogicContract.UpdateProduct(new(Guid.NewGuid().ToString(), "name", Guid.NewGuid().ToString(), ProductType.Workpiece, [new ProductIngredientDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)], 10, false)),
Throws.TypeOf<StorageException>());
_productStorageContract.Verify(x =>
x.UpdElement(It.IsAny<ProductDataModel>()), Times.Once);
}
[Test]
public void DeleteProduct_CorrectRecord_Test()
{
//Arrange
var id = Guid.NewGuid().ToString();
var flag = false;
_productStorageContract.Setup(x => x.DelElement(It.Is((string x) => x
== id))).Callback(() => { flag = true; });
//Act
_productBusinessLogicContract.DeleteProduct(id);
//Assert
_productStorageContract.Verify(x => x.DelElement(It.IsAny<string>()),
Times.Once);
Assert.That(flag);
}
[Test]
public void DeleteProduct_RecordWithIncorrectId_ThrowException_Test()
{
//Arrange
var id = Guid.NewGuid().ToString();
_productStorageContract.Setup(x =>
x.DelElement(It.IsAny<string>())).Throws(new ElementNotFoundException(id));
//Act&Assert
Assert.That(() =>
_productBusinessLogicContract.DeleteProduct(Guid.NewGuid().ToString()),
Throws.TypeOf<ElementNotFoundException>());
_productStorageContract.Verify(x => x.DelElement(It.IsAny<string>()),
Times.Once);
}
[Test]
public void DeleteProduct_IdIsNullOrEmpty_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _productBusinessLogicContract.DeleteProduct(null),
Throws.TypeOf<ArgumentNullException>());
Assert.That(() =>
_productBusinessLogicContract.DeleteProduct(string.Empty),
Throws.TypeOf<ArgumentNullException>());
_productStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Never);
}
[Test]
public void DeleteProduct_IdIsNotGuid_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _productBusinessLogicContract.DeleteProduct("id"),
Throws.TypeOf<ValidationException>());
_productStorageContract.Verify(x => x.DelElement(It.IsAny<string>()),
Times.Never);
}
[Test]
public void DeleteProduct_StorageThrowError_ThrowException_Test()
{
//Arrange
_productStorageContract.Setup(x =>
x.DelElement(It.IsAny<string>())).Throws(new StorageException(new
InvalidOperationException()));
//Act&Assert
Assert.That(() =>
_productBusinessLogicContract.DeleteProduct(Guid.NewGuid().ToString()),
Throws.TypeOf<StorageException>());
_productStorageContract.Verify(x => x.DelElement(It.IsAny<string>()),
Times.Once);
}
}

View File

@@ -0,0 +1,500 @@
using Microsoft.Extensions.Logging;
using Moq;
using SladkieBulkiBusinessLogic.Implementations;
using SladkieBulkiContrakts.BusinessLogicsContracts;
using SladkieBulkiContrakts.DataModels;
using SladkieBulkiContrakts.Exceptions;
using SladkieBulkiContrakts.StoragesContarcts;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SladkieBulkiTests.BusinessLogicsContractsTests;
[TestFixture]
internal class ProductionBusinessLogicContractTests
{
private ProductionBusinessLogicContract _productionBusinessLogicContract;
private Mock<IProductionStorageContract> _productionStorageContract;
[OneTimeSetUp]
public void OneTimeSetUp()
{
_productionStorageContract = new Mock<IProductionStorageContract>();
_productionBusinessLogicContract = new ProductionBusinessLogicContract(_productionStorageContract.Object, new Mock<ILogger>().Object);
}
[SetUp]
public void SetUp()
{
_productionStorageContract.Reset();
}
[Test]
public void GetAllSalesByPeriod_ReturnListOfRecords_Test()
{
// Arrange
var date = DateTime.UtcNow;
var listOriginal = new List<ProductionDataModel>
{
new(
Guid.NewGuid().ToString(),
date,
5,
120.5,
Guid.NewGuid().ToString(),
Guid.NewGuid().ToString()
),
new(
Guid.NewGuid().ToString(),
date.AddHours(-1),
2,
50.0,
Guid.NewGuid().ToString(),
Guid.NewGuid().ToString()
)
};
_productionStorageContract.Setup(x => x.GetList(date, date.AddDays(1), null, null)).Returns(listOriginal);
// Act
var list = _productionBusinessLogicContract.GetAllSalesByPeriod(date, date.AddDays(1));
// Assert
Assert.That(list, Is.Not.Null);
Assert.That(list, Is.EquivalentTo(listOriginal));
_productionStorageContract.Verify(x => x.GetList(date, date.AddDays(1), null, null), Times.Once);
}
[Test]
public void GetAllSalesByPeriod_ReturnEmptyList_Test()
{
// Arrange
_productionStorageContract.Setup(x => x.GetList(
It.IsAny<DateTime?>(),
It.IsAny<DateTime?>(),
It.IsAny<string>(),
It.IsAny<string>())).Returns([]);
// Act
var list = _productionBusinessLogicContract.GetAllSalesByPeriod(
DateTime.UtcNow, DateTime.UtcNow.AddDays(1));
// Assert
Assert.That(list, Is.Not.Null);
Assert.That(list, Has.Count.EqualTo(0));
_productionStorageContract.Verify(x => x.GetList(
It.IsAny<DateTime?>(),
It.IsAny<DateTime?>(),
It.IsAny<string>(),
It.IsAny<string>()), Times.Once);
}
[Test]
public void GetAllSalesByPeriod_IncorrectDates_ThrowException_Test()
{
// Arrange
var date = DateTime.UtcNow;
// Act & Assert
Assert.That(() => _productionBusinessLogicContract.GetAllSalesByPeriod(date, date),
Throws.TypeOf<IncorrectDatesException>());
Assert.That(() => _productionBusinessLogicContract.GetAllSalesByPeriod(date, date.AddSeconds(-1)),
Throws.TypeOf<IncorrectDatesException>());
_productionStorageContract.Verify(x => x.GetList(
It.IsAny<DateTime?>(),
It.IsAny<DateTime?>(),
It.IsAny<string>(),
It.IsAny<string>()), Times.Never);
}
[Test]
public void GetAllSalesByPeriod_ReturnNull_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _productionBusinessLogicContract.GetAllSalesByPeriod(DateTime.UtcNow, DateTime.UtcNow.AddDays(1)), Throws.TypeOf<NullListException>());
_productionStorageContract.Verify(x => x.GetList(It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<string>(), It.IsAny<string>()), Times.Once);
}
[Test]
public void GetAllSalesByPeriod_StorageThrowError_ThrowException_Test()
{
//Arrange
_productionStorageContract.Setup(x => x.GetList(It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<string>(), It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException()));
//Act&Assert
Assert.That(() => _productionBusinessLogicContract.GetAllSalesByPeriod(DateTime.UtcNow, DateTime.UtcNow.AddDays(1)), Throws.TypeOf<StorageException>());
_productionStorageContract.Verify(x => x.GetList(It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<string>(), It.IsAny<string>()), Times.Once);
}
[Test]
public void GetAllSalesByWorkerByPeriod_ReturnListOfRecords_Test()
{
//Arrange
var date = DateTime.UtcNow;
var workerId = Guid.NewGuid().ToString();
var listOriginal = new List<ProductionDataModel>()
{
new(Guid.NewGuid().ToString(), date, 5, 10.0, Guid.NewGuid().ToString(), Guid.NewGuid().ToString()),
new(Guid.NewGuid().ToString(), date, 5, 10.0, Guid.NewGuid().ToString(), Guid.NewGuid().ToString()),
new(Guid.NewGuid().ToString(), date, 5, 10.0, Guid.NewGuid().ToString(), Guid.NewGuid().ToString())
};
_productionStorageContract.Setup(x => x.GetList(It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<string>(), It.IsAny<string>())).Returns(listOriginal);
//Act
var list = _productionBusinessLogicContract.GetAllSalesByWorkerByPeriod(workerId, date, date.AddDays(1));
//Assert
Assert.That(list, Is.Not.Null);
Assert.That(list, Is.EquivalentTo(listOriginal));
_productionStorageContract.Verify(x => x.GetList(date, date.AddDays(1), workerId, null), Times.Once);
}
[Test]
public void GetAllSalesByWorkerByPeriod_ReturnEmptyList_Test()
{
//Arrange
_productionStorageContract.Setup(x => x.GetList(It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<string>(), It.IsAny<string>())).Returns([]);
//Act
var list = _productionBusinessLogicContract.GetAllSalesByWorkerByPeriod(Guid.NewGuid().ToString(), DateTime.UtcNow, DateTime.UtcNow.AddDays(1));
//Assert
Assert.That(list, Is.Not.Null);
Assert.That(list, Has.Count.EqualTo(0));
_productionStorageContract.Verify(x => x.GetList(It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<string>(), It.IsAny<string>()), Times.Once);
}
[Test]
public void GetAllSalesByWorkerByPeriod_IncorrectDates_ThrowException_Test()
{
//Arrange
var date = DateTime.UtcNow;
//Act&Assert
Assert.That(() => _productionBusinessLogicContract.GetAllSalesByWorkerByPeriod(Guid.NewGuid().ToString(), date, date), Throws.TypeOf<IncorrectDatesException>());
Assert.That(() => _productionBusinessLogicContract.GetAllSalesByWorkerByPeriod(Guid.NewGuid().ToString(), date, date.AddSeconds(-1)), Throws.TypeOf<IncorrectDatesException>());
_productionStorageContract.Verify(x => x.GetList(It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<string>(), It.IsAny<string>()), Times.Never);
}
[Test]
public void GetAllSalesByWorkerByPeriod_WorkerIdIsNullOrEmpty_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _productionBusinessLogicContract.GetAllSalesByWorkerByPeriod(null, DateTime.UtcNow, DateTime.UtcNow.AddDays(1)), Throws.TypeOf<ArgumentNullException>());
Assert.That(() => _productionBusinessLogicContract.GetAllSalesByWorkerByPeriod(string.Empty, DateTime.UtcNow, DateTime.UtcNow.AddDays(1)), Throws.TypeOf<ArgumentNullException>());
_productionStorageContract.Verify(x => x.GetList(It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<string>(), It.IsAny<string>()), Times.Never);
}
[Test]
public void GetAllSalesByWorkerByPeriod_WorkerIdIsNotGuid_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _productionBusinessLogicContract.GetAllSalesByWorkerByPeriod("workerId", DateTime.UtcNow, DateTime.UtcNow.AddDays(1)), Throws.TypeOf<ValidationException>());
_productionStorageContract.Verify(x => x.GetList(It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<string>(), It.IsAny<string>()), Times.Never);
}
[Test]
public void GetAllSalesByWorkerByPeriod_ReturnNull_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _productionBusinessLogicContract.GetAllSalesByWorkerByPeriod(Guid.NewGuid().ToString(), DateTime.UtcNow, DateTime.UtcNow.AddDays(1)), Throws.TypeOf<NullListException>());
_productionStorageContract.Verify(x => x.GetList(It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<string>(), It.IsAny<string>()), Times.Once);
}
[Test]
public void GetAllSalesByWorkerByPeriod_StorageThrowError_ThrowException_Test()
{
//Arrange
_productionStorageContract.Setup(x => x.GetList(It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<string>(), It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException()));
//Act&Assert
Assert.That(() => _productionBusinessLogicContract.GetAllSalesByWorkerByPeriod(Guid.NewGuid().ToString(), DateTime.UtcNow, DateTime.UtcNow.AddDays(1)), Throws.TypeOf<StorageException>());
_productionStorageContract.Verify(x => x.GetList(It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<string>(), It.IsAny<string>()), Times.Once);
}
[Test]
public void GetAllSalesByProductByPeriod_ReturnListOfRecords_Test()
{
//Arrange
var date = DateTime.UtcNow;
var productId = Guid.NewGuid().ToString();
var listOriginal = new List<ProductionDataModel>()
{
new(Guid.NewGuid().ToString(), date, 10, 100.0, Guid.NewGuid().ToString(), productId),
new(Guid.NewGuid().ToString(), date, 10, 100.0, Guid.NewGuid().ToString(), productId),
new(Guid.NewGuid().ToString(), date, 10, 100.0, Guid.NewGuid().ToString(), productId),
};
_productionStorageContract.Setup(x => x.GetList(It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<string>(), It.IsAny<string>())).Returns(listOriginal);
//Act
var list = _productionBusinessLogicContract.GetAllSalesByProductByPeriod(productId, date, date.AddDays(1));
//Assert
Assert.That(list, Is.Not.Null);
Assert.That(list, Is.EquivalentTo(listOriginal));
_productionStorageContract.Verify(x => x.GetList(date, date.AddDays(1), null, productId), Times.Once);
}
[Test]
public void GetAllSalesByProductByPeriod_ReturnEmptyList_Test()
{
//Arrange
_productionStorageContract.Setup(x => x.GetList(It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<string>(), It.IsAny<string>())).Returns([]);
//Act
var list = _productionBusinessLogicContract.GetAllSalesByProductByPeriod(Guid.NewGuid().ToString(), DateTime.UtcNow, DateTime.UtcNow.AddDays(1));
//Assert
Assert.That(list, Is.Not.Null);
Assert.That(list, Has.Count.EqualTo(0));
_productionStorageContract.Verify(x => x.GetList(It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<string>(), It.IsAny<string>()), Times.Once);
}
[Test]
public void GetAllSalesByProductByPeriod_IncorrectDates_ThrowException_Test()
{
//Arrange
var date = DateTime.UtcNow;
//Act&Assert
Assert.That(() => _productionBusinessLogicContract.GetAllSalesByProductByPeriod(Guid.NewGuid().ToString(), date, date), Throws.TypeOf<IncorrectDatesException>());
Assert.That(() => _productionBusinessLogicContract.GetAllSalesByProductByPeriod(Guid.NewGuid().ToString(), date, date.AddSeconds(-1)), Throws.TypeOf<IncorrectDatesException>());
_productionStorageContract.Verify(x => x.GetList(It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<string>(), It.IsAny<string>()), Times.Never);
}
[Test]
public void GetAllSalesByProductByPeriod_ProductIdIsNullOrEmpty_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _productionBusinessLogicContract.GetAllSalesByProductByPeriod(null, DateTime.UtcNow, DateTime.UtcNow.AddDays(1)), Throws.TypeOf<ArgumentNullException>());
Assert.That(() => _productionBusinessLogicContract.GetAllSalesByProductByPeriod(string.Empty, DateTime.UtcNow, DateTime.UtcNow.AddDays(1)), Throws.TypeOf<ArgumentNullException>());
_productionStorageContract.Verify(x => x.GetList(It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<string>(), It.IsAny<string>()), Times.Never);
}
[Test]
public void GetAllSalesByProductByPeriod_ProductIdIsNotGuid_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _productionBusinessLogicContract.GetAllSalesByProductByPeriod("productId", DateTime.UtcNow, DateTime.UtcNow.AddDays(1)), Throws.TypeOf<ValidationException>());
_productionStorageContract.Verify(x => x.GetList(It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<string>(), It.IsAny<string>()), Times.Never);
}
[Test]
public void GetAllSalesByProductByPeriod_ReturnNull_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _productionBusinessLogicContract.GetAllSalesByProductByPeriod(Guid.NewGuid().ToString(), DateTime.UtcNow, DateTime.UtcNow.AddDays(1)), Throws.TypeOf<NullListException>());
_productionStorageContract.Verify(x => x.GetList(It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<string>(), It.IsAny<string>()), Times.Once);
}
[Test]
public void GetAllSalesByProductByPeriod_StorageThrowError_ThrowException_Test()
{
//Arrange
_productionStorageContract.Setup(x => x.GetList(It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<string>(), It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException()));
//Act&Assert
Assert.That(() => _productionBusinessLogicContract.GetAllSalesByProductByPeriod(Guid.NewGuid().ToString(), DateTime.UtcNow, DateTime.UtcNow.AddDays(1)), Throws.TypeOf<StorageException>());
_productionStorageContract.Verify(x => x.GetList(It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<string>(), It.IsAny<string>()), Times.Once);
}
[Test]
public void GetSaleByData_GetById_ReturnRecord_Test()
{
//Arrange
var id = Guid.NewGuid().ToString();
var record = new ProductionDataModel(id, DateTime.Now, 10, 100.0, Guid.NewGuid().ToString(), Guid.NewGuid().ToString());
_productionStorageContract.Setup(x => x.GetElementById(id)).Returns(record);
//Act
var element = _productionBusinessLogicContract.GetProductionByData(id);
//Assert
Assert.That(element, Is.Not.Null);
Assert.That(element.Id, Is.EqualTo(id));
_productionStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Once);
}
[Test]
public void GetSaleByData_EmptyData_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _productionBusinessLogicContract.GetProductionByData(null), Throws.TypeOf<ArgumentNullException>());
Assert.That(() => _productionBusinessLogicContract.GetProductionByData(string.Empty), Throws.TypeOf<ArgumentNullException>());
_productionStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Never);
}
[Test]
public void GetSaleByData_IdIsNotGuid_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _productionBusinessLogicContract.GetProductionByData("saleId"), Throws.TypeOf<ValidationException>());
_productionStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Never);
}
[Test]
public void GetSaleByData_GetById_NotFoundRecord_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _productionBusinessLogicContract.GetProductionByData(Guid.NewGuid().ToString()), Throws.TypeOf<ElementNotFoundException>());
_productionStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Once);
}
[Test]
public void GetSaleByData_StorageThrowError_ThrowException_Test()
{
//Arrange
_productionStorageContract.Setup(x => x.GetElementById(It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException()));
//Act&Assert
Assert.That(() => _productionBusinessLogicContract.GetProductionByData(Guid.NewGuid().ToString()), Throws.TypeOf<StorageException>());
_productionStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Once);
}
[Test]
public void InsertSale_CorrectRecord_Test()
{
// Arrange
var flag = false;
var record = new ProductionDataModel(
Guid.NewGuid().ToString(),
DateTime.Now,
10,
100.0,
Guid.NewGuid().ToString(),
Guid.NewGuid().ToString());
_productionStorageContract
.Setup(x => x.AddElement(It.IsAny<ProductionDataModel>()))
.Callback((ProductionDataModel x) =>
{
flag = x.Id == record.Id &&
x.ProductionDate == record.ProductionDate &&
x.Count == record.Count &&
x.Sum == record.Sum &&
x.WorkerId == record.WorkerId &&
x.ProductId == record.ProductId;
});
// Act
_productionBusinessLogicContract.InsertProduction(record);
// Assert
_productionStorageContract.Verify(x => x.AddElement(It.IsAny<ProductionDataModel>()), Times.Once);
Assert.That(flag, Is.True);
}
[Test]
public void InsertSale_RecordWithExistsData_ThrowException_Test()
{
// Arrange
_productionStorageContract
.Setup(x => x.AddElement(It.IsAny<ProductionDataModel>()))
.Throws(new ElementExistsException("Data", "Data"));
var record = new ProductionDataModel(
Guid.NewGuid().ToString(),
DateTime.Now,
10,
100.0,
Guid.NewGuid().ToString(),
Guid.NewGuid().ToString());
// Act & Assert
Assert.That(() => _productionBusinessLogicContract.InsertProduction(record), Throws.TypeOf<ElementExistsException>());
_productionStorageContract.Verify(x => x.AddElement(It.IsAny<ProductionDataModel>()), Times.Once);
}
[Test]
public void InsertSale_NullRecord_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _productionBusinessLogicContract.InsertProduction(null), Throws.TypeOf<ArgumentNullException>());
_productionStorageContract.Verify(x => x.AddElement(It.IsAny<ProductionDataModel>()), Times.Never);
}
[Test]
public void InsertSale_InvalidRecord_ThrowException_Test()
{
//Arrange
var invalidRecord = new ProductionDataModel(
"id",
DateTime.Now.AddDays(1),
-1,
0,
"",
"not-a-guid"
);
// Act & Assert
Assert.That(() => _productionBusinessLogicContract.InsertProduction(invalidRecord), Throws.TypeOf<ValidationException>());
_productionStorageContract.Verify(x => x.AddElement(It.IsAny<ProductionDataModel>()), Times.Never);
}
[Test]
public void InsertSale_StorageThrowError_ThrowException_Test()
{
// Arrange
_productionStorageContract
.Setup(x => x.AddElement(It.IsAny<ProductionDataModel>()))
.Throws(new StorageException(new InvalidOperationException()));
var record = new ProductionDataModel(
Guid.NewGuid().ToString(),
DateTime.Now,
5,
100.0,
Guid.NewGuid().ToString(),
Guid.NewGuid().ToString()
);
// Act & Assert
Assert.That(() => _productionBusinessLogicContract.InsertProduction(record), Throws.TypeOf<StorageException>());
_productionStorageContract.Verify(x => x.AddElement(It.IsAny<ProductionDataModel>()), Times.Once);
}
[Test]
public void CancelSale_CorrectRecord_Test()
{
//Arrange
var id = Guid.NewGuid().ToString();
var flag = false;
_productionStorageContract.Setup(x => x.DelElement(It.Is((string x) => x == id))).Callback(() => { flag = true; });
//Act
_productionBusinessLogicContract.CancelProduction(id);
//Assert
_productionStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Once);
Assert.That(flag);
}
[Test]
public void CancelSale_RecordWithIncorrectId_ThrowException_Test()
{
//Arrange
var id = Guid.NewGuid().ToString();
_productionStorageContract.Setup(x => x.DelElement(It.IsAny<string>())).Throws(new ElementNotFoundException(id));
//Act&Assert
Assert.That(() => _productionBusinessLogicContract.CancelProduction(Guid.NewGuid().ToString()), Throws.TypeOf<ElementNotFoundException>());
_productionStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Once);
}
[Test]
public void CancelSale_IdIsNullOrEmpty_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _productionBusinessLogicContract.CancelProduction(null), Throws.TypeOf<ArgumentNullException>());
Assert.That(() => _productionBusinessLogicContract.CancelProduction(string.Empty), Throws.TypeOf<ArgumentNullException>());
_productionStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Never);
}
[Test]
public void CancelSale_IdIsNotGuid_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _productionBusinessLogicContract.CancelProduction("id"), Throws.TypeOf<ValidationException>());
_productionStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Never);
}
[Test]
public void CancelSale_StorageThrowError_ThrowException_Test()
{
//Arrange
_productionStorageContract.Setup(x => x.DelElement(It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException()));
//Act&Assert
Assert.That(() => _productionBusinessLogicContract.CancelProduction(Guid.NewGuid().ToString()), Throws.TypeOf<StorageException>());
_productionStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Once);
}
}

View File

@@ -0,0 +1,350 @@
using Microsoft.Extensions.Logging;
using Moq;
using SladkieBulkiBusinessLogic.Implementations;
using SladkieBulkiContrakts.DataModels;
using SladkieBulkiContrakts.Enums;
using SladkieBulkiContrakts.Exceptions;
using SladkieBulkiContrakts.StoragesContarcts;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SladkieBulkiTests.BusinessLogicsContractsTests;
internal class SalaryBusinessLogicContractTests
{
private SalaryBusinessLogicContract _salaryBusinessLogicContract;
private Mock<ISalaryStorageContract> _salaryStorageContract;
private Mock<IProductionStorageContract> _productionStorageContract;
private Mock<IPostStorageContract> _postStorageContract;
private Mock<IWorkerStorageContract> _workerStorageContract;
[OneTimeSetUp]
public void OneTimeSetUp()
{
_salaryStorageContract = new Mock<ISalaryStorageContract>();
_productionStorageContract = new Mock<IProductionStorageContract>();
_postStorageContract = new Mock<IPostStorageContract>();
_workerStorageContract = new Mock<IWorkerStorageContract>();
_salaryBusinessLogicContract = new SalaryBusinessLogicContract(_salaryStorageContract.Object,
_productionStorageContract.Object, _postStorageContract.Object, _workerStorageContract.Object, new Mock<ILogger>().Object);
}
[SetUp]
public void SetUp()
{
_salaryStorageContract.Reset();
_productionStorageContract.Reset();
_postStorageContract.Reset();
_workerStorageContract.Reset();
}
[Test]
public void GetAllSalaries_ReturnListOfRecords_Test()
{
//Arrange
var startDate = DateTime.UtcNow;
var endDate = DateTime.UtcNow.AddDays(1);
var listOriginal = new List<SalaryDataModel>()
{
new(Guid.NewGuid().ToString(), DateTime.UtcNow, 10),
new(Guid.NewGuid().ToString(), DateTime.UtcNow.AddDays(1), 14),
new(Guid.NewGuid().ToString(), DateTime.UtcNow.AddDays(-1), 30),
};
_salaryStorageContract.Setup(x => x.GetList(It.IsAny<DateTime>(), It.IsAny<DateTime>(), It.IsAny<string>())).Returns(listOriginal);
//Act
var list = _salaryBusinessLogicContract.GetAllSalariesByPeriod(startDate, endDate);
//Assert
Assert.That(list, Is.Not.Null);
Assert.That(list, Is.EquivalentTo(listOriginal));
_salaryStorageContract.Verify(x => x.GetList(startDate, endDate, null), Times.Once);
}
[Test]
public void GetAllSalaries_ReturnEmptyList_Test()
{
//Arrange
_salaryStorageContract.Setup(x => x.GetList(It.IsAny<DateTime>(), It.IsAny<DateTime>(), It.IsAny<string>())).Returns([]);
//Act
var list = _salaryBusinessLogicContract.GetAllSalariesByPeriod(DateTime.UtcNow, DateTime.UtcNow.AddDays(1));
//Assert
Assert.That(list, Is.Not.Null);
Assert.That(list, Has.Count.EqualTo(0));
_salaryStorageContract.Verify(x => x.GetList(It.IsAny<DateTime>(), It.IsAny<DateTime>(), It.IsAny<string>()), Times.Once);
}
[Test]
public void GetAllSalaries_IncorrectDates_ThrowException_Test()
{
//Arrange
var dateTime = DateTime.UtcNow;
//Act&Assert
Assert.That(() => _salaryBusinessLogicContract.GetAllSalariesByPeriod(dateTime, dateTime), Throws.TypeOf<IncorrectDatesException>());
Assert.That(() => _salaryBusinessLogicContract.GetAllSalariesByPeriod(dateTime, dateTime.AddSeconds(-1)), Throws.TypeOf<IncorrectDatesException>());
_salaryStorageContract.Verify(x => x.GetList(It.IsAny<DateTime>(), It.IsAny<DateTime>(), It.IsAny<string>()), Times.Never);
}
[Test]
public void GetAllSalaries_ReturnNull_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _salaryBusinessLogicContract.GetAllSalariesByPeriod(DateTime.UtcNow, DateTime.UtcNow.AddDays(1)), Throws.TypeOf<NullListException>());
_salaryStorageContract.Verify(x => x.GetList(It.IsAny<DateTime>(), It.IsAny<DateTime>(), It.IsAny<string>()), Times.Once);
}
[Test]
public void GetAllSalaries_StorageThrowError_ThrowException_Test()
{
//Arrange
_salaryStorageContract.Setup(x => x.GetList(It.IsAny<DateTime>(), It.IsAny<DateTime>(), It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException()));
//Act&Assert
Assert.That(() => _salaryBusinessLogicContract.GetAllSalariesByPeriod(DateTime.UtcNow, DateTime.UtcNow.AddDays(1)), Throws.TypeOf<StorageException>());
_salaryStorageContract.Verify(x => x.GetList(It.IsAny<DateTime>(), It.IsAny<DateTime>(), It.IsAny<string>()), Times.Once);
}
[Test]
public void GetAllSalariesByWorker_ReturnListOfRecords_Test()
{
//Arrange
var startDate = DateTime.UtcNow;
var endDate = DateTime.UtcNow.AddDays(1);
var workerId = Guid.NewGuid().ToString();
var listOriginal = new List<SalaryDataModel>()
{
new(Guid.NewGuid().ToString(), DateTime.UtcNow, 10),
new(Guid.NewGuid().ToString(), DateTime.UtcNow.AddDays(1), 14),
new(Guid.NewGuid().ToString(), DateTime.UtcNow.AddDays(-1), 30),
};
_salaryStorageContract.Setup(x => x.GetList(It.IsAny<DateTime>(), It.IsAny<DateTime>(), It.IsAny<string>())).Returns(listOriginal);
//Act
var list = _salaryBusinessLogicContract.GetAllSalariesByPeriodByWorker(startDate, endDate, workerId);
//Assert
Assert.That(list, Is.Not.Null);
Assert.That(list, Is.EquivalentTo(listOriginal));
_salaryStorageContract.Verify(x => x.GetList(startDate, endDate, workerId), Times.Once);
}
[Test]
public void GetAllSalariesByWorker_ReturnEmptyList_Test()
{
//Arrange
_salaryStorageContract.Setup(x => x.GetList(It.IsAny<DateTime>(), It.IsAny<DateTime>(), It.IsAny<string>())).Returns([]);
//Act
var list = _salaryBusinessLogicContract.GetAllSalariesByPeriodByWorker(DateTime.UtcNow, DateTime.UtcNow.AddDays(1), Guid.NewGuid().ToString());
//Assert
Assert.That(list, Is.Not.Null);
Assert.That(list, Has.Count.EqualTo(0));
_salaryStorageContract.Verify(x => x.GetList(It.IsAny<DateTime>(), It.IsAny<DateTime>(), It.IsAny<string>()), Times.Once);
}
[Test]
public void GetAllSalariesByWorker_IncorrectDates_ThrowException_Test()
{
//Arrange
var dateTime = DateTime.UtcNow;
//Act&Assert
Assert.That(() => _salaryBusinessLogicContract.GetAllSalariesByPeriodByWorker(dateTime, dateTime, Guid.NewGuid().ToString()), Throws.TypeOf<IncorrectDatesException>());
Assert.That(() => _salaryBusinessLogicContract.GetAllSalariesByPeriodByWorker(dateTime, dateTime.AddSeconds(-1), Guid.NewGuid().ToString()), Throws.TypeOf<IncorrectDatesException>());
_salaryStorageContract.Verify(x => x.GetList(It.IsAny<DateTime>(), It.IsAny<DateTime>(), It.IsAny<string>()), Times.Never);
}
[Test]
public void GetAllSalariesByWorker_WorkerIdIsNUllOrEmpty_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _salaryBusinessLogicContract.GetAllSalariesByPeriodByWorker(DateTime.UtcNow, DateTime.UtcNow.AddDays(1), null), Throws.TypeOf<ArgumentNullException>());
Assert.That(() => _salaryBusinessLogicContract.GetAllSalariesByPeriodByWorker(DateTime.UtcNow, DateTime.UtcNow.AddDays(1), string.Empty), Throws.TypeOf<ArgumentNullException>());
_salaryStorageContract.Verify(x => x.GetList(It.IsAny<DateTime>(), It.IsAny<DateTime>(), It.IsAny<string>()), Times.Never);
}
[Test]
public void GetAllSalariesByWorker_WorkerIdIsNotGuid_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _salaryBusinessLogicContract.GetAllSalariesByPeriodByWorker(DateTime.UtcNow, DateTime.UtcNow.AddDays(1), "workerId"), Throws.TypeOf<ValidationException>());
_salaryStorageContract.Verify(x => x.GetList(It.IsAny<DateTime>(), It.IsAny<DateTime>(), It.IsAny<string>()), Times.Never);
}
[Test]
public void GetAllSalariesByWorker_ReturnNull_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _salaryBusinessLogicContract.GetAllSalariesByPeriodByWorker(DateTime.UtcNow, DateTime.UtcNow.AddDays(1), Guid.NewGuid().ToString()), Throws.TypeOf<NullListException>());
_salaryStorageContract.Verify(x => x.GetList(It.IsAny<DateTime>(), It.IsAny<DateTime>(), It.IsAny<string>()), Times.Once);
}
[Test]
public void GetAllSalariesByWorker_StorageThrowError_ThrowException_Test()
{
//Arrange
_salaryStorageContract.Setup(x => x.GetList(It.IsAny<DateTime>(), It.IsAny<DateTime>(), It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException()));
//Act&Assert
Assert.That(() => _salaryBusinessLogicContract.GetAllSalariesByPeriodByWorker(DateTime.UtcNow, DateTime.UtcNow.AddDays(1), Guid.NewGuid().ToString()), Throws.TypeOf<StorageException>());
_salaryStorageContract.Verify(x => x.GetList(It.IsAny<DateTime>(), It.IsAny<DateTime>(), It.IsAny<string>()), Times.Once);
}
[Test]
public void CalculateSalaryByMounth_CalculateSalary_Test()
{
//Arrange
var workerId = Guid.NewGuid().ToString();
var saleSum = 200.0;
var postSalary = 2000.0;
_productionStorageContract.Setup(x => x.GetList(It.IsAny<DateTime>(), It.IsAny<DateTime>(), It.IsAny<string>(), It.IsAny<string>()))
.Returns([new ProductionDataModel(Guid.NewGuid().ToString(), DateTime.Now, 0, saleSum, workerId, null)]);
_postStorageContract.Setup(x => x.GetElementById(It.IsAny<string>()))
.Returns(new PostDataModel(Guid.NewGuid().ToString(), "name", PostType.Manufacturer, postSalary, true, DateTime.UtcNow));
_workerStorageContract.Setup(x => x.GetList(It.IsAny<bool>(), It.IsAny<string?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>()))
.Returns([new WorkerDataModel(workerId, "Test", Guid.NewGuid().ToString(), DateTime.UtcNow, DateTime.UtcNow, false, "1@mail.ru"),]);
var sum = 0.0;
var expectedSum = postSalary + saleSum * 0.1;
_salaryStorageContract.Setup(x => x.AddElement(It.IsAny<SalaryDataModel>()))
.Callback((SalaryDataModel x) =>
{
sum = x.Salary;
});
//Act
_salaryBusinessLogicContract.CalculateSalaryByMounth(DateTime.UtcNow);
//Assert
Assert.That(sum, Is.EqualTo(expectedSum));
}
[Test]
public void CalculateSalaryByMounth_WithSeveralWorkers_Test()
{
//Arrange
var worker1Id = Guid.NewGuid().ToString();
var worker2Id = Guid.NewGuid().ToString();
var worker3Id = Guid.NewGuid().ToString();
var list = new List<WorkerDataModel>() {
new(worker1Id, "Test", Guid.NewGuid().ToString(), DateTime.UtcNow, DateTime.UtcNow, false, "1@mail.ru"),
new(worker2Id, "Test", Guid.NewGuid().ToString(), DateTime.UtcNow, DateTime.UtcNow, false, "1@mail.ru"),
new(worker3Id, "Test", Guid.NewGuid().ToString(), DateTime.UtcNow, DateTime.UtcNow, false, "1@mail.ru")
};
_productionStorageContract.Setup(x => x.GetList(It.IsAny<DateTime>(), It.IsAny<DateTime>(), It.IsAny<string>(), It.IsAny<string>()))
.Returns([new ProductionDataModel(Guid.NewGuid().ToString(), DateTime.Now, 0, 0.0, worker1Id, null),
new ProductionDataModel(Guid.NewGuid().ToString(), DateTime.Now, 0, 0.0, worker1Id, null),
new ProductionDataModel(Guid.NewGuid().ToString(), DateTime.Now, 0, 0.0, worker2Id, null),
new ProductionDataModel(Guid.NewGuid().ToString(), DateTime.Now, 0, 0.0, worker3Id, null),
new ProductionDataModel(Guid.NewGuid().ToString(), DateTime.Now, 0, 0.0, worker3Id, null)]);
_postStorageContract.Setup(x => x.GetElementById(It.IsAny<string>()))
.Returns(new PostDataModel(Guid.NewGuid().ToString(), "name", PostType.Manufacturer, 2000, true, DateTime.UtcNow));
_workerStorageContract.Setup(x => x.GetList(It.IsAny<bool>(), It.IsAny<string?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>()))
.Returns(list);
//Act
_salaryBusinessLogicContract.CalculateSalaryByMounth(DateTime.UtcNow);
//Assert
_salaryStorageContract.Verify(x => x.AddElement(It.IsAny<SalaryDataModel>()), Times.Exactly(list.Count));
}
[Test]
public void CalculateSalaryByMounth_WithoitSalesByWorker_Test()
{
//Arrange
var postSalary = 2000.0;
var workerId = Guid.NewGuid().ToString();
_productionStorageContract.Setup(x => x.GetList(It.IsAny<DateTime>(), It.IsAny<DateTime>(), It.IsAny<string>(), It.IsAny<string>()))
.Returns([]);
_postStorageContract.Setup(x => x.GetElementById(It.IsAny<string>()))
.Returns(new PostDataModel(Guid.NewGuid().ToString(), "name", PostType.Manufacturer, postSalary, true, DateTime.UtcNow));
_workerStorageContract.Setup(x => x.GetList(It.IsAny<bool>(), It.IsAny<string?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>()))
.Returns([new WorkerDataModel(workerId, "Test", Guid.NewGuid().ToString(), DateTime.UtcNow, DateTime.UtcNow, false, "1@mail.ru")]);
var sum = 0.0;
var expectedSum = postSalary;
_salaryStorageContract.Setup(x => x.AddElement(It.IsAny<SalaryDataModel>()))
.Callback((SalaryDataModel x) =>
{
sum = x.Salary;
});
//Act
_salaryBusinessLogicContract.CalculateSalaryByMounth(DateTime.UtcNow);
//Assert
Assert.That(sum, Is.EqualTo(expectedSum));
}
[Test]
public void CalculateSalaryByMounth_SaleStorageReturnNull_ThrowException_Test()
{
//Arrange
var workerId = Guid.NewGuid().ToString();
_postStorageContract.Setup(x => x.GetElementById(It.IsAny<string>()))
.Returns(new PostDataModel(Guid.NewGuid().ToString(), "name", PostType.Manufacturer, 2000, true, DateTime.UtcNow));
_workerStorageContract.Setup(x => x.GetList(It.IsAny<bool>(), It.IsAny<string?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>()))
.Returns([new WorkerDataModel(workerId, "Test", Guid.NewGuid().ToString(), DateTime.UtcNow, DateTime.UtcNow, false, "1@mail.ru")]);
//Act&Assert
Assert.That(() => _salaryBusinessLogicContract.CalculateSalaryByMounth(DateTime.UtcNow), Throws.TypeOf<NullListException>());
}
[Test]
public void CalculateSalaryByMounth_PostStorageReturnNull_ThrowException_Test()
{
//Arrange
var workerId = Guid.NewGuid().ToString();
_productionStorageContract.Setup(x => x.GetList(It.IsAny<DateTime>(), It.IsAny<DateTime>(), It.IsAny<string>(), It.IsAny<string>()))
.Returns([new ProductionDataModel(Guid.NewGuid().ToString(), DateTime.Now, 0, 200.0, workerId, null)]);
_workerStorageContract.Setup(x => x.GetList(It.IsAny<bool>(), It.IsAny<string?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>()))
.Returns([new WorkerDataModel(workerId, "Test", Guid.NewGuid().ToString(), DateTime.UtcNow, DateTime.UtcNow, false, "1@mail.ru")]);
//Act&Assert
Assert.That(() => _salaryBusinessLogicContract.CalculateSalaryByMounth(DateTime.UtcNow), Throws.TypeOf<NullListException>());
}
[Test]
public void CalculateSalaryByMounth_WorkerStorageReturnNull_ThrowException_Test()
{
//Arrange
var workerId = Guid.NewGuid().ToString();
_productionStorageContract.Setup(x => x.GetList(It.IsAny<DateTime>(), It.IsAny<DateTime>(), It.IsAny<string>(), It.IsAny<string>()))
.Returns([new ProductionDataModel(Guid.NewGuid().ToString(), DateTime.Now, 0, 200.0, workerId, null)]);
_postStorageContract.Setup(x => x.GetElementById(It.IsAny<string>()))
.Returns(new PostDataModel(Guid.NewGuid().ToString(), "name", PostType.Manufacturer, 2000, true, DateTime.UtcNow));
//Act&Assert
Assert.That(() => _salaryBusinessLogicContract.CalculateSalaryByMounth(DateTime.UtcNow), Throws.TypeOf<NullListException>());
}
[Test]
public void CalculateSalaryByMounth_SaleStorageThrowException_ThrowException_Test()
{
//Arrange
var workerId = Guid.NewGuid().ToString();
_productionStorageContract.Setup(x => x.GetList(It.IsAny<DateTime>(), It.IsAny<DateTime>(), It.IsAny<string>(), It.IsAny<string>()))
.Throws(new StorageException(new InvalidOperationException()));
_postStorageContract.Setup(x => x.GetElementById(It.IsAny<string>()))
.Returns(new PostDataModel(Guid.NewGuid().ToString(), "name", PostType.Manufacturer, 2000, true, DateTime.UtcNow));
_workerStorageContract.Setup(x => x.GetList(It.IsAny<bool>(), It.IsAny<string?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>()))
.Returns([new WorkerDataModel(workerId, "Test", Guid.NewGuid().ToString(), DateTime.UtcNow, DateTime.UtcNow, false, "1@mail.ru")]);
//Act&Assert
Assert.That(() => _salaryBusinessLogicContract.CalculateSalaryByMounth(DateTime.UtcNow), Throws.TypeOf<StorageException>());
}
[Test]
public void CalculateSalaryByMounth_PostStorageThrowException_ThrowException_Test()
{
//Arrange
var workerId = Guid.NewGuid().ToString();
_productionStorageContract.Setup(x => x.GetList(It.IsAny<DateTime>(), It.IsAny<DateTime>(), It.IsAny<string>(), It.IsAny<string>()))
.Returns([new ProductionDataModel(Guid.NewGuid().ToString(), DateTime.Now, 0, 200.0, workerId, null)]);
_postStorageContract.Setup(x => x.GetElementById(It.IsAny<string>()))
.Throws(new StorageException(new InvalidOperationException()));
_workerStorageContract.Setup(x => x.GetList(It.IsAny<bool>(), It.IsAny<string?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>()))
.Returns([new WorkerDataModel(workerId, "Test", Guid.NewGuid().ToString(), DateTime.UtcNow, DateTime.UtcNow, false, "1@mail.ru")]);
//Act&Assert
Assert.That(() => _salaryBusinessLogicContract.CalculateSalaryByMounth(DateTime.UtcNow), Throws.TypeOf<StorageException>());
}
[Test]
public void CalculateSalaryByMounth_WorkerStorageThrowException_ThrowException_Test()
{
//Arrange
var workerId = Guid.NewGuid().ToString();
_productionStorageContract.Setup(x => x.GetList(It.IsAny<DateTime>(), It.IsAny<DateTime>(), It.IsAny<string>(), It.IsAny<string>()))
.Returns([new ProductionDataModel(Guid.NewGuid().ToString(), DateTime.Now, 0, 200.0, workerId, null)]);
_postStorageContract.Setup(x => x.GetElementById(It.IsAny<string>()))
.Returns(new PostDataModel(Guid.NewGuid().ToString(), "name", PostType.Manufacturer, 2000, true, DateTime.UtcNow));
_workerStorageContract.Setup(x => x.GetList(It.IsAny<bool>(), It.IsAny<string?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>()))
.Throws(new StorageException(new InvalidOperationException()));
//Act&Assert
Assert.That(() => _salaryBusinessLogicContract.CalculateSalaryByMounth(DateTime.UtcNow), Throws.TypeOf<StorageException>());
}
}

View File

@@ -0,0 +1,561 @@
using Microsoft.Extensions.Logging;
using Moq;
using SladkieBulkiBusinessLogic.Implementations;
using SladkieBulkiContrakts.DataModels;
using SladkieBulkiContrakts.Exceptions;
using SladkieBulkiContrakts.StoragesContarcts;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SladkieBulkiTests.BusinessLogicsContractsTests;
internal class WorkerBusinessLogicContractTests
{
private WorkerBusinessLogicContract _workerBusinessLogicContract;
private Mock<IWorkerStorageContract> _workerStorageContract;
[OneTimeSetUp]
public void OneTimeSetUp()
{
_workerStorageContract = new Mock<IWorkerStorageContract>();
_workerBusinessLogicContract = new WorkerBusinessLogicContract(_workerStorageContract.Object, new Mock<ILogger>().Object);
}
[SetUp]
public void SetUp()
{
_workerStorageContract.Reset();
}
[Test]
public void GetAllWorkers_ReturnListOfRecords_Test()
{
//Arrange
var listOriginal = new List<WorkerDataModel>()
{
new(Guid.NewGuid().ToString(), "fio 1", Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now, false, ""),
new(Guid.NewGuid().ToString(), "fio 2", Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now, true, ""),
new(Guid.NewGuid().ToString(), "fio 3", Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now, false, ""),
};
_workerStorageContract.Setup(x => x.GetList(It.IsAny<bool>(), It.IsAny<string?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>())).Returns(listOriginal);
//Act
var listOnlyActive = _workerBusinessLogicContract.GetAllWorkers(true);
var list = _workerBusinessLogicContract.GetAllWorkers(false);
//Assert
Assert.Multiple(() =>
{
Assert.That(listOnlyActive, Is.Not.Null);
Assert.That(list, Is.Not.Null);
Assert.That(listOnlyActive, Is.EquivalentTo(listOriginal));
Assert.That(list, Is.EquivalentTo(listOriginal));
});
_workerStorageContract.Verify(x => x.GetList(true, null, null, null, null, null), Times.Once);
_workerStorageContract.Verify(x => x.GetList(false, null, null, null, null, null), Times.Once);
}
[Test]
public void GetAllWorkers_ReturnEmptyList_Test()
{
//Arrange
_workerStorageContract.Setup(x => x.GetList(It.IsAny<bool>(), It.IsAny<string?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>())).Returns([]);
//Act
var listOnlyActive = _workerBusinessLogicContract.GetAllWorkers(true);
var list = _workerBusinessLogicContract.GetAllWorkers(false);
//Assert
Assert.Multiple(() =>
{
Assert.That(listOnlyActive, Is.Not.Null);
Assert.That(list, Is.Not.Null);
Assert.That(listOnlyActive, Has.Count.EqualTo(0));
Assert.That(list, Has.Count.EqualTo(0));
});
_workerStorageContract.Verify(x => x.GetList(It.IsAny<bool>(), null, null, null, null, null), Times.Exactly(2));
}
[Test]
public void GetAllWorkers_ReturnNull_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _workerBusinessLogicContract.GetAllWorkers(It.IsAny<bool>()), Throws.TypeOf<NullListException>());
_workerStorageContract.Verify(x => x.GetList(It.IsAny<bool>(), It.IsAny<string?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>()), Times.Once);
}
[Test]
public void GetAllWorkers_StorageThrowError_ThrowException_Test()
{
//Arrange
_workerStorageContract.Setup(x => x.GetList(It.IsAny<bool>(), It.IsAny<string?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>())).Throws(new StorageException(new InvalidOperationException()));
//Act&Assert
Assert.That(() => _workerBusinessLogicContract.GetAllWorkers(It.IsAny<bool>()), Throws.TypeOf<StorageException>());
_workerStorageContract.Verify(x => x.GetList(It.IsAny<bool>(), null, null, null, null, null), Times.Once);
}
[Test]
public void GetAllWorkersByPost_ReturnListOfRecords_Test()
{
//Arrange
var postId = Guid.NewGuid().ToString();
var listOriginal = new List<WorkerDataModel>()
{
new(Guid.NewGuid().ToString(), "fio 1", Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now, false, ""),
new(Guid.NewGuid().ToString(), "fio 2", Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now, true, ""),
new(Guid.NewGuid().ToString(), "fio 3", Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now, false, ""),
};
_workerStorageContract.Setup(x => x.GetList(It.IsAny<bool>(), It.IsAny<string?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>())).Returns(listOriginal);
//Act
var listOnlyActive = _workerBusinessLogicContract.GetAllWorkersByPost(postId, true);
var list = _workerBusinessLogicContract.GetAllWorkersByPost(postId, false);
//Assert
Assert.Multiple(() =>
{
Assert.That(listOnlyActive, Is.Not.Null);
Assert.That(list, Is.Not.Null);
Assert.That(listOnlyActive, Is.EquivalentTo(listOriginal));
Assert.That(list, Is.EquivalentTo(listOriginal));
});
_workerStorageContract.Verify(x => x.GetList(true, postId, null, null, null, null), Times.Once);
_workerStorageContract.Verify(x => x.GetList(false, postId, null, null, null, null), Times.Once);
}
[Test]
public void GetAllWorkersByPost_ReturnEmptyList_Test()
{
//Arrange
_workerStorageContract.Setup(x => x.GetList(It.IsAny<bool>(), It.IsAny<string?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>())).Returns([]);
//Act
var listOnlyActive = _workerBusinessLogicContract.GetAllWorkersByPost(Guid.NewGuid().ToString(), true);
var list = _workerBusinessLogicContract.GetAllWorkersByPost(Guid.NewGuid().ToString(), false);
//Assert
Assert.Multiple(() =>
{
Assert.That(listOnlyActive, Is.Not.Null);
Assert.That(list, Is.Not.Null);
Assert.That(listOnlyActive, Has.Count.EqualTo(0));
Assert.That(list, Has.Count.EqualTo(0));
});
_workerStorageContract.Verify(x => x.GetList(It.IsAny<bool>(), It.IsAny<string>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>()), Times.Exactly(2));
}
[Test]
public void GetAllWorkersByPost_PostIdIsNullOrEmpty_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _workerBusinessLogicContract.GetAllWorkersByPost(null, It.IsAny<bool>()), Throws.TypeOf<ArgumentNullException>());
Assert.That(() => _workerBusinessLogicContract.GetAllWorkersByPost(string.Empty, It.IsAny<bool>()), Throws.TypeOf<ArgumentNullException>());
_workerStorageContract.Verify(x => x.GetList(It.IsAny<bool>(), It.IsAny<string>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>()), Times.Never);
}
[Test]
public void GetAllWorkersByPost_PostIdIsNotGuid_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _workerBusinessLogicContract.GetAllWorkersByPost("postId", It.IsAny<bool>()), Throws.TypeOf<ValidationException>());
_workerStorageContract.Verify(x => x.GetList(It.IsAny<bool>(), It.IsAny<string>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>()), Times.Never);
}
[Test]
public void GetAllWorkersByPost_ReturnNull_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _workerBusinessLogicContract.GetAllWorkersByPost(Guid.NewGuid().ToString(), It.IsAny<bool>()), Throws.TypeOf<NullListException>());
_workerStorageContract.Verify(x => x.GetList(It.IsAny<bool>(), It.IsAny<string>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>()), Times.Once);
}
[Test]
public void GetAllWorkersByPost_StorageThrowError_ThrowException_Test()
{
//Arrange
_workerStorageContract.Setup(x => x.GetList(It.IsAny<bool>(), It.IsAny<string?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>())).Throws(new StorageException(new InvalidOperationException()));
//Act&Assert
Assert.That(() => _workerBusinessLogicContract.GetAllWorkersByPost(Guid.NewGuid().ToString(), It.IsAny<bool>()), Throws.TypeOf<StorageException>());
_workerStorageContract.Verify(x => x.GetList(It.IsAny<bool>(), It.IsAny<string?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>()), Times.Once);
}
[Test]
public void GetAllWorkersByBirthDate_ReturnListOfRecords_Test()
{
//Arrange
var date = DateTime.UtcNow;
var listOriginal = new List<WorkerDataModel>()
{
new(Guid.NewGuid().ToString(), "fio 1", Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now, false, ""),
new(Guid.NewGuid().ToString(), "fio 2", Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now, true, ""),
new(Guid.NewGuid().ToString(), "fio 3", Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now, false, ""),
};
_workerStorageContract.Setup(x => x.GetList(It.IsAny<bool>(), It.IsAny<string?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>())).Returns(listOriginal);
//Act
var listOnlyActive = _workerBusinessLogicContract.GetAllWorkersByBirthDate(date, date.AddDays(1), true);
var list = _workerBusinessLogicContract.GetAllWorkersByBirthDate(date, date.AddDays(1), false);
//Assert
Assert.Multiple(() =>
{
Assert.That(listOnlyActive, Is.Not.Null);
Assert.That(list, Is.Not.Null);
Assert.That(listOnlyActive, Is.EquivalentTo(listOriginal));
Assert.That(list, Is.EquivalentTo(listOriginal));
});
_workerStorageContract.Verify(x => x.GetList(true, null, date, date.AddDays(1), null, null), Times.Once);
_workerStorageContract.Verify(x => x.GetList(false, null, date, date.AddDays(1), null, null), Times.Once);
}
[Test]
public void GetAllWorkersByBirthDate_ReturnEmptyList_Test()
{
//Arrange
_workerStorageContract.Setup(x => x.GetList(It.IsAny<bool>(), It.IsAny<string?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>())).Returns([]);
//Act
var listOnlyActive = _workerBusinessLogicContract.GetAllWorkersByBirthDate(DateTime.UtcNow, DateTime.UtcNow.AddDays(1), true);
var list = _workerBusinessLogicContract.GetAllWorkers(false);
//Assert
Assert.Multiple(() =>
{
Assert.That(listOnlyActive, Is.Not.Null);
Assert.That(list, Is.Not.Null);
Assert.That(listOnlyActive, Has.Count.EqualTo(0));
Assert.That(list, Has.Count.EqualTo(0));
});
_workerStorageContract.Verify(x => x.GetList(true, null, It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), null, null), Times.Once);
_workerStorageContract.Verify(x => x.GetList(false, null, It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), null, null), Times.Once);
}
[Test]
public void GetAllWorkersByBirthDate_IncorrectDates_ThrowException_Test()
{
//Arrange
var date = DateTime.UtcNow;
//Act&Assert
Assert.That(() => _workerBusinessLogicContract.GetAllWorkersByBirthDate(date, date, It.IsAny<bool>()), Throws.TypeOf<IncorrectDatesException>());
Assert.That(() => _workerBusinessLogicContract.GetAllWorkersByBirthDate(date, date.AddSeconds(-1), It.IsAny<bool>()), Throws.TypeOf<IncorrectDatesException>());
_workerStorageContract.Verify(x => x.GetList(It.IsAny<bool>(), It.IsAny<string?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>()), Times.Never);
}
[Test]
public void GetAllWorkersByBirthDate_ReturnNull_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _workerBusinessLogicContract.GetAllWorkersByBirthDate(DateTime.UtcNow, DateTime.UtcNow.AddDays(1), It.IsAny<bool>()), Throws.TypeOf<NullListException>());
_workerStorageContract.Verify(x => x.GetList(It.IsAny<bool>(), It.IsAny<string?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>()), Times.Once);
}
[Test]
public void GetAllWorkersByBirthDate_StorageThrowError_ThrowException_Test()
{
//Arrange
_workerStorageContract.Setup(x => x.GetList(It.IsAny<bool>(), It.IsAny<string?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>())).Throws(new StorageException(new InvalidOperationException()));
//Act&Assert
Assert.That(() => _workerBusinessLogicContract.GetAllWorkersByBirthDate(DateTime.UtcNow, DateTime.UtcNow.AddDays(1), It.IsAny<bool>()), Throws.TypeOf<StorageException>());
_workerStorageContract.Verify(x => x.GetList(It.IsAny<bool>(), It.IsAny<string?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>()), Times.Once);
}
[Test]
public void GetAllWorkersByEmploymentDate_ReturnListOfRecords_Test()
{
//Arrange
var date = DateTime.UtcNow;
var listOriginal = new List<WorkerDataModel>()
{
new(Guid.NewGuid().ToString(), "fio 1", Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now, false, ""),
new(Guid.NewGuid().ToString(), "fio 2", Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now, true, ""),
new(Guid.NewGuid().ToString(), "fio 3", Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now, false, ""),
};
_workerStorageContract.Setup(x => x.GetList(It.IsAny<bool>(), It.IsAny<string?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>())).Returns(listOriginal);
//Act
var listOnlyActive = _workerBusinessLogicContract.GetAllWorkersByEmploymentDate(date, date.AddDays(1), true);
var list = _workerBusinessLogicContract.GetAllWorkersByEmploymentDate(date, date.AddDays(1), false);
//Assert
Assert.Multiple(() =>
{
Assert.That(listOnlyActive, Is.Not.Null);
Assert.That(list, Is.Not.Null);
Assert.That(listOnlyActive, Is.EquivalentTo(listOriginal));
Assert.That(list, Is.EquivalentTo(listOriginal));
});
_workerStorageContract.Verify(x => x.GetList(true, null, null, null, date, date.AddDays(1)), Times.Once);
_workerStorageContract.Verify(x => x.GetList(false, null, null, null, date, date.AddDays(1)), Times.Once);
}
[Test]
public void GetAllWorkersByEmploymentDate_ReturnEmptyList_Test()
{
//Arrange
_workerStorageContract.Setup(x => x.GetList(It.IsAny<bool>(), It.IsAny<string?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>())).Returns([]);
//Act
var listOnlyActive = _workerBusinessLogicContract.GetAllWorkersByEmploymentDate(DateTime.UtcNow, DateTime.UtcNow.AddDays(1), true);
var list = _workerBusinessLogicContract.GetAllWorkersByEmploymentDate(DateTime.UtcNow, DateTime.UtcNow.AddDays(1), false);
//Assert
Assert.Multiple(() =>
{
Assert.That(listOnlyActive, Is.Not.Null);
Assert.That(list, Is.Not.Null);
Assert.That(listOnlyActive, Has.Count.EqualTo(0));
Assert.That(list, Has.Count.EqualTo(0));
});
_workerStorageContract.Verify(x => x.GetList(true, null, null, null, It.IsAny<DateTime?>(), It.IsAny<DateTime?>()), Times.Once);
_workerStorageContract.Verify(x => x.GetList(false, null, null, null, It.IsAny<DateTime?>(), It.IsAny<DateTime?>()), Times.Once);
}
[Test]
public void GetAllWorkersByEmploymentDate_IncorrectDates_ThrowException_Test()
{
//Arrange
var date = DateTime.UtcNow;
//Act&Assert
Assert.That(() => _workerBusinessLogicContract.GetAllWorkersByEmploymentDate(date, date, It.IsAny<bool>()), Throws.TypeOf<IncorrectDatesException>());
Assert.That(() => _workerBusinessLogicContract.GetAllWorkersByEmploymentDate(date, date.AddSeconds(-1), It.IsAny<bool>()), Throws.TypeOf<IncorrectDatesException>());
_workerStorageContract.Verify(x => x.GetList(It.IsAny<bool>(), It.IsAny<string?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>()), Times.Never);
}
[Test]
public void GetAllWorkersByEmploymentDate_ReturnNull_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _workerBusinessLogicContract.GetAllWorkersByEmploymentDate(DateTime.UtcNow, DateTime.UtcNow.AddDays(1), It.IsAny<bool>()), Throws.TypeOf<NullListException>());
_workerStorageContract.Verify(x => x.GetList(It.IsAny<bool>(), It.IsAny<string?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>()), Times.Once);
}
[Test]
public void GetAllWorkersByEmploymentDate_StorageThrowError_ThrowException_Test()
{
//Arrange
_workerStorageContract.Setup(x => x.GetList(It.IsAny<bool>(), It.IsAny<string?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>())).Throws(new StorageException(new InvalidOperationException()));
//Act&Assert
Assert.That(() => _workerBusinessLogicContract.GetAllWorkersByEmploymentDate(DateTime.UtcNow, DateTime.UtcNow.AddDays(1), It.IsAny<bool>()), Throws.TypeOf<StorageException>());
_workerStorageContract.Verify(x => x.GetList(It.IsAny<bool>(), It.IsAny<string?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>()), Times.Once);
}
[Test]
public void GetWorkerByData_GetById_ReturnRecord_Test()
{
//Arrange
var id = Guid.NewGuid().ToString();
var record = new WorkerDataModel(id, "fio", Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now, false, "");
_workerStorageContract.Setup(x => x.GetElementById(id)).Returns(record);
//Act
var element = _workerBusinessLogicContract.GetWorkerByData(id);
//Assert
Assert.That(element, Is.Not.Null);
Assert.That(element.Id, Is.EqualTo(id));
_workerStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Once);
}
[Test]
public void GetWorkerByData_GetByFio_ReturnRecord_Test()
{
//Arrange
var fio = "fio";
var record = new WorkerDataModel(Guid.NewGuid().ToString(), fio, Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now, false, "");
_workerStorageContract.Setup(x => x.GetElementByFIO(fio)).Returns(record);
//Act
var element = _workerBusinessLogicContract.GetWorkerByData(fio);
//Assert
Assert.That(element, Is.Not.Null);
Assert.That(element.FIO, Is.EqualTo(fio));
_workerStorageContract.Verify(x => x.GetElementByFIO(It.IsAny<string>()), Times.Once);
}
[Test]
public void GetWorkerByData_EmptyData_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _workerBusinessLogicContract.GetWorkerByData(null), Throws.TypeOf<ArgumentNullException>());
Assert.That(() => _workerBusinessLogicContract.GetWorkerByData(string.Empty), Throws.TypeOf<ArgumentNullException>());
_workerStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Never);
_workerStorageContract.Verify(x => x.GetElementByFIO(It.IsAny<string>()), Times.Never);
}
[Test]
public void GetWorkerByData_GetById_NotFoundRecord_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _workerBusinessLogicContract.GetWorkerByData(Guid.NewGuid().ToString()), Throws.TypeOf<ElementNotFoundException>());
_workerStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Once);
_workerStorageContract.Verify(x => x.GetElementByFIO(It.IsAny<string>()), Times.Never);
}
[Test]
public void GetWorkerByData_GetByFio_NotFoundRecord_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _workerBusinessLogicContract.GetWorkerByData("fio"), Throws.TypeOf<ElementNotFoundException>());
_workerStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Never);
_workerStorageContract.Verify(x => x.GetElementByFIO(It.IsAny<string>()), Times.Once);
}
[Test]
public void GetWorkerByData_StorageThrowError_ThrowException_Test()
{
//Arrange
_workerStorageContract.Setup(x => x.GetElementById(It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException()));
_workerStorageContract.Setup(x => x.GetElementByFIO(It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException()));
//Act&Assert
Assert.That(() => _workerBusinessLogicContract.GetWorkerByData(Guid.NewGuid().ToString()), Throws.TypeOf<StorageException>());
Assert.That(() => _workerBusinessLogicContract.GetWorkerByData("fio"), Throws.TypeOf<StorageException>());
_workerStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Once);
_workerStorageContract.Verify(x => x.GetElementByFIO(It.IsAny<string>()), Times.Once);
}
[Test]
public void InsertWorker_CorrectRecord_Test()
{
//Arrange
var flag = false;
var record = new WorkerDataModel(Guid.NewGuid().ToString(), "fio", Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now, false, "1@mail.ru");
_workerStorageContract.Setup(x => x.AddElement(It.IsAny<WorkerDataModel>()))
.Callback((WorkerDataModel x) =>
{
flag = x.Id == record.Id && x.FIO == record.FIO && x.PostId == record.PostId && x.BirthDate == record.BirthDate &&
x.EmploymentDate == record.EmploymentDate && x.IsDeleted == record.IsDeleted;
});
//Act
_workerBusinessLogicContract.InsertWorker(record);
//Assert
_workerStorageContract.Verify(x => x.AddElement(It.IsAny<WorkerDataModel>()), Times.Once);
Assert.That(flag);
}
[Test]
public void InsertWorker_RecordWithExistsData_ThrowException_Test()
{
//Arrange
_workerStorageContract.Setup(x => x.AddElement(It.IsAny<WorkerDataModel>())).Throws(new ElementExistsException("Data", "Data"));
//Act&Assert
Assert.That(() => _workerBusinessLogicContract.InsertWorker(new(Guid.NewGuid().ToString(), "fio", Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now, false, "1@mail.ru")), Throws.TypeOf<ElementExistsException>());
_workerStorageContract.Verify(x => x.AddElement(It.IsAny<WorkerDataModel>()), Times.Once);
}
[Test]
public void InsertWorker_NullRecord_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _workerBusinessLogicContract.InsertWorker(null), Throws.TypeOf<ArgumentNullException>());
_workerStorageContract.Verify(x => x.AddElement(It.IsAny<WorkerDataModel>()), Times.Never);
}
[Test]
public void InsertWorker_InvalidRecord_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _workerBusinessLogicContract.InsertWorker(new WorkerDataModel("id", "fio", Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now, false, "1@mail.ru")), Throws.TypeOf<ValidationException>());
_workerStorageContract.Verify(x => x.AddElement(It.IsAny<WorkerDataModel>()), Times.Never);
}
[Test]
public void InsertWorker_StorageThrowError_ThrowException_Test()
{
//Arrange
_workerStorageContract.Setup(x => x.AddElement(It.IsAny<WorkerDataModel>())).Throws(new StorageException(new InvalidOperationException()));
//Act&Assert
Assert.That(() => _workerBusinessLogicContract.InsertWorker(new(Guid.NewGuid().ToString(), "fio", Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now, false, "1@mail.ru")), Throws.TypeOf<StorageException>());
_workerStorageContract.Verify(x => x.AddElement(It.IsAny<WorkerDataModel>()), Times.Once);
}
[Test]
public void UpdateWorker_CorrectRecord_Test()
{
//Arrange
var flag = false;
var record = new WorkerDataModel(Guid.NewGuid().ToString(), "fio", Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now, false, "1@mail.ru");
_workerStorageContract.Setup(x => x.UpdElement(It.IsAny<WorkerDataModel>()))
.Callback((WorkerDataModel x) =>
{
flag = x.Id == record.Id && x.FIO == record.FIO && x.PostId == record.PostId && x.BirthDate == record.BirthDate &&
x.EmploymentDate == record.EmploymentDate && x.IsDeleted == record.IsDeleted;
});
//Act
_workerBusinessLogicContract.UpdateWorker(record);
//Assert
_workerStorageContract.Verify(x => x.UpdElement(It.IsAny<WorkerDataModel>()), Times.Once);
Assert.That(flag);
}
[Test]
public void UpdateWorker_RecordWithIncorrectData_ThrowException_Test()
{
//Arrange
_workerStorageContract.Setup(x => x.UpdElement(It.IsAny<WorkerDataModel>())).Throws(new ElementNotFoundException(""));
//Act&Assert
Assert.That(() => _workerBusinessLogicContract.UpdateWorker(new(Guid.NewGuid().ToString(), "fio", Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now, false, "1@mail.ru")), Throws.TypeOf<ElementNotFoundException>());
_workerStorageContract.Verify(x => x.UpdElement(It.IsAny<WorkerDataModel>()), Times.Once);
}
[Test]
public void UpdateWorker_NullRecord_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _workerBusinessLogicContract.UpdateWorker(null), Throws.TypeOf<ArgumentNullException>());
_workerStorageContract.Verify(x => x.UpdElement(It.IsAny<WorkerDataModel>()), Times.Never);
}
[Test]
public void UpdateWorker_InvalidRecord_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _workerBusinessLogicContract.UpdateWorker(new WorkerDataModel("id", "fio", Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now, false, "1@mail.ru")), Throws.TypeOf<ValidationException>());
_workerStorageContract.Verify(x => x.UpdElement(It.IsAny<WorkerDataModel>()), Times.Never);
}
[Test]
public void UpdateWorker_StorageThrowError_ThrowException_Test()
{
//Arrange
_workerStorageContract.Setup(x => x.UpdElement(It.IsAny<WorkerDataModel>())).Throws(new StorageException(new InvalidOperationException()));
//Act&Assert
Assert.That(() => _workerBusinessLogicContract.UpdateWorker(new(Guid.NewGuid().ToString(), "fio", Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now, false, "1@mail.ru")), Throws.TypeOf<StorageException>());
_workerStorageContract.Verify(x => x.UpdElement(It.IsAny<WorkerDataModel>()), Times.Once);
}
[Test]
public void DeleteWorker_CorrectRecord_Test()
{
//Arrange
var id = Guid.NewGuid().ToString();
var flag = false;
_workerStorageContract.Setup(x => x.DelElement(It.Is((string x) => x == id))).Callback(() => { flag = true; });
//Act
_workerBusinessLogicContract.DeleteWorker(id);
//Assert
_workerStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Once);
Assert.That(flag);
}
[Test]
public void DeleteWorker_RecordWithIncorrectId_ThrowException_Test()
{
//Arrange
var id = Guid.NewGuid().ToString();
_workerStorageContract.Setup(x => x.DelElement(It.Is((string x) => x != id))).Throws(new ElementNotFoundException(id));
//Act&Assert
Assert.That(() => _workerBusinessLogicContract.DeleteWorker(Guid.NewGuid().ToString()), Throws.TypeOf<ElementNotFoundException>());
_workerStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Once);
}
[Test]
public void DeleteWorker_IdIsNullOrEmpty_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _workerBusinessLogicContract.DeleteWorker(null), Throws.TypeOf<ArgumentNullException>());
Assert.That(() => _workerBusinessLogicContract.DeleteWorker(string.Empty), Throws.TypeOf<ArgumentNullException>());
_workerStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Never);
}
[Test]
public void DeleteWorker_IdIsNotGuid_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _workerBusinessLogicContract.DeleteWorker("id"), Throws.TypeOf<ValidationException>());
_workerStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Never);
}
[Test]
public void DeleteWorker_StorageThrowError_ThrowException_Test()
{
//Arrange
_workerStorageContract.Setup(x => x.DelElement(It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException()));
//Act&Assert
Assert.That(() => _workerBusinessLogicContract.DeleteWorker(Guid.NewGuid().ToString()), Throws.TypeOf<StorageException>());
_workerStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Once);
}
}

View File

@@ -12,70 +12,68 @@ namespace SladkieBulkiTests.DataModelsTests;
internal class PostDataModelTests internal class PostDataModelTests
{ {
[Test] [Test]
public void IdIsNullEmptyTest() public void IdIsNullOrEmptyTest()
{ {
var post = CreateDataModel(null, "name", PostType.Preform, 100); var post = CreateDataModel(null, "name", PostType.Preform, 10, true, DateTime.UtcNow);
Assert.That(() => post.Validate(), Throws.TypeOf<ValidationException>()); Assert.That(() => post.Validate(), Throws.TypeOf<ValidationException>());
post = CreateDataModel(string.Empty, "name", PostType.Preform, 10, true, DateTime.UtcNow);
post = CreateDataModel(string.Empty, "name", PostType.Preform, 100);
Assert.That(() => post.Validate(), Throws.TypeOf<ValidationException>()); Assert.That(() => post.Validate(), Throws.TypeOf<ValidationException>());
} }
[Test] [Test]
public void IdIsNotGuidTest() public void IdIsNotGuidTest()
{ {
var post = CreateDataModel("id", "name", PostType.Preform, 100); var post = CreateDataModel("id", "name", PostType.Preform, 10, true, DateTime.UtcNow);
Assert.That(() => post.Validate(), Throws.TypeOf<ValidationException>()); Assert.That(() => post.Validate(), Throws.TypeOf<ValidationException>());
} }
[Test] [Test]
public void PostNameIsNullOrEmptyTest() public void PostNameIsEmptyTest()
{ {
var post = CreateDataModel(Guid.NewGuid().ToString(), null, PostType.Preform, 100); var manufacturer = CreateDataModel(Guid.NewGuid().ToString(), null, PostType.Preform, 10, true, DateTime.UtcNow);
Assert.That(() => post.Validate(), Throws.TypeOf<ValidationException>()); Assert.That(() => manufacturer.Validate(), Throws.TypeOf<ValidationException>());
manufacturer = CreateDataModel(Guid.NewGuid().ToString(), string.Empty, PostType.Preform, 10, true, DateTime.UtcNow);
post = CreateDataModel(Guid.NewGuid().ToString(), string.Empty, PostType.Preform, 100); Assert.That(() => manufacturer.Validate(), Throws.TypeOf<ValidationException>());
Assert.That(() => post.Validate(), Throws.TypeOf<ValidationException>());
} }
[Test] [Test]
public void PostTypeIsNoneTest() public void PostTypeIsNoneTest()
{ {
var post = CreateDataModel(Guid.NewGuid().ToString(), "name", PostType.None, 100); var post = CreateDataModel(Guid.NewGuid().ToString(), "name", PostType.None, 10, true, DateTime.UtcNow);
Assert.That(() => post.Validate(), Throws.TypeOf<ValidationException>()); Assert.That(() => post.Validate(), Throws.TypeOf<ValidationException>());
} }
[Test] [Test]
public void SalaryIsZeroOrNegativeTest() public void SalaryIsLessOrZeroTest()
{ {
var post = CreateDataModel(Guid.NewGuid().ToString(), "name", PostType.Preform, 0); var post = CreateDataModel(Guid.NewGuid().ToString(), "name", PostType.Preform, 0, true, DateTime.UtcNow);
Assert.That(() => post.Validate(), Throws.TypeOf<ValidationException>()); Assert.That(() => post.Validate(), Throws.TypeOf<ValidationException>());
post = CreateDataModel(Guid.NewGuid().ToString(), "name", PostType.Preform, -10, true, DateTime.UtcNow);
post = CreateDataModel(Guid.NewGuid().ToString(), "name", PostType.Preform, -100);
Assert.That(() => post.Validate(), Throws.TypeOf<ValidationException>()); Assert.That(() => post.Validate(), Throws.TypeOf<ValidationException>());
} }
[Test] [Test]
public void AllFieldsAreCorrectTest() public void AllFieldsIsCorrectTest()
{ {
var postId = Guid.NewGuid().ToString(); var postId = Guid.NewGuid().ToString();
var postName = "Alan Worker"; var postName = "name";
var postType = PostType.Preform; var postType = PostType.Preform;
var salary = 100; var salary = 10;
var isActual = false;
var post = CreateDataModel(postId, postName, postType, salary); var changeDate = DateTime.UtcNow.AddDays(-1);
var post = CreateDataModel(postId, postName, postType, salary, isActual, changeDate);
Assert.That(() => post.Validate(), Throws.Nothing); Assert.That(() => post.Validate(), Throws.Nothing);
Assert.Multiple(() => Assert.Multiple(() =>
{ {
Assert.That(post.Id, Is.EqualTo(postId)); Assert.That(post.Id, Is.EqualTo(postId));
Assert.That(post.PostName, Is.EqualTo(postName)); Assert.That(post.PostName, Is.EqualTo(postName));
Assert.That(post.PostType, Is.EqualTo(postType)); Assert.That(post.PostType, Is.EqualTo(postType));
Assert.That(post.Salary, Is.EqualTo(salary)); Assert.That(post.Salary, Is.EqualTo(salary));
Assert.That(post.IsActual, Is.EqualTo(isActual));
Assert.That(post.ChangeDate, Is.EqualTo(changeDate));
}); });
} }
private static PostDataModel CreateDataModel(string? id, string? postName, PostType postType, double salary) => private static PostDataModel CreateDataModel(string? id, string? postName, PostType postType, double salary, bool isActual, DateTime changeDate) =>
new(id, postName, postType, salary); new(id, postName, postType, salary, isActual, changeDate);
} }

View File

@@ -14,54 +14,54 @@ internal class ProductDataModelTests
[Test] [Test]
public void IdIsNullEmptyTest() public void IdIsNullEmptyTest()
{ {
var product = CreateDataModel(null, "Product1", "Description", ProductType.Production, 100, false); var product = CreateDataModel(null, "Product1", "Description", ProductType.Production, CreateSubDataModel(), 100, false);
Assert.That(() => product.Validate(), Throws.TypeOf<ValidationException>()); Assert.That(() => product.Validate(), Throws.TypeOf<ValidationException>());
product = CreateDataModel(string.Empty, "Product1", "Description", ProductType.Production, 100, false); product = CreateDataModel(string.Empty, "Product1", "Description", ProductType.Production, CreateSubDataModel(), 100, false);
Assert.That(() => product.Validate(), Throws.TypeOf<ValidationException>()); Assert.That(() => product.Validate(), Throws.TypeOf<ValidationException>());
} }
[Test] [Test]
public void IdIsNotGuidTest() public void IdIsNotGuidTest()
{ {
var product = CreateDataModel("id", "Product1", "Description", ProductType.Production, 100, false); var product = CreateDataModel("id", "Product1", "Description", ProductType.Production, CreateSubDataModel(), 100, false);
Assert.That(() => product.Validate(), Throws.TypeOf<ValidationException>()); Assert.That(() => product.Validate(), Throws.TypeOf<ValidationException>());
} }
[Test] [Test]
public void NameIsNullOrEmptyTest() public void NameIsNullOrEmptyTest()
{ {
var product = CreateDataModel(Guid.NewGuid().ToString(), null, "Description", ProductType.Production, 100, false); var product = CreateDataModel(Guid.NewGuid().ToString(), null, "Description", ProductType.Production, CreateSubDataModel(), 100, false);
Assert.That(() => product.Validate(), Throws.TypeOf<ValidationException>()); Assert.That(() => product.Validate(), Throws.TypeOf<ValidationException>());
product = CreateDataModel(Guid.NewGuid().ToString(), string.Empty, "Description", ProductType.Production, 100, false); product = CreateDataModel(Guid.NewGuid().ToString(), string.Empty, "Description", ProductType.Production, CreateSubDataModel(), 100, false);
Assert.That(() => product.Validate(), Throws.TypeOf<ValidationException>()); Assert.That(() => product.Validate(), Throws.TypeOf<ValidationException>());
} }
[Test] [Test]
public void DescriptionIsNullOrEmptyTest() public void DescriptionIsNullOrEmptyTest()
{ {
var product = CreateDataModel(Guid.NewGuid().ToString(), "Product1", null, ProductType.Production, 100, false); var product = CreateDataModel(Guid.NewGuid().ToString(), "Product1", null, ProductType.Production, CreateSubDataModel(), 100, false);
Assert.That(() => product.Validate(), Throws.TypeOf<ValidationException>()); Assert.That(() => product.Validate(), Throws.TypeOf<ValidationException>());
product = CreateDataModel(Guid.NewGuid().ToString(), "Product1", string.Empty, ProductType.Production, 100, false); product = CreateDataModel(Guid.NewGuid().ToString(), "Product1", string.Empty, ProductType.Production, CreateSubDataModel(), 100, false);
Assert.That(() => product.Validate(), Throws.TypeOf<ValidationException>()); Assert.That(() => product.Validate(), Throws.TypeOf<ValidationException>());
} }
[Test] [Test]
public void ProductTypeIsNoneTest() public void ProductTypeIsNoneTest()
{ {
var product = CreateDataModel(Guid.NewGuid().ToString(), "Product1", "Description", ProductType.None, 100, false); var product = CreateDataModel(Guid.NewGuid().ToString(), "Product1", "Description", ProductType.None, CreateSubDataModel(), 100, false);
Assert.That(() => product.Validate(), Throws.TypeOf<ValidationException>()); Assert.That(() => product.Validate(), Throws.TypeOf<ValidationException>());
} }
[Test] [Test]
public void UnitPriceIsZeroOrNegativeTest() public void UnitPriceIsZeroOrNegativeTest()
{ {
var product = CreateDataModel(Guid.NewGuid().ToString(), "Product1", "Description", ProductType.Production, 0, false); var product = CreateDataModel(Guid.NewGuid().ToString(), "Product1", "Description", ProductType.Production, CreateSubDataModel(), 0, false);
Assert.That(() => product.Validate(), Throws.TypeOf<ValidationException>()); Assert.That(() => product.Validate(), Throws.TypeOf<ValidationException>());
product = CreateDataModel(Guid.NewGuid().ToString(), "Product1", "Description", ProductType.Production, -100, false); product = CreateDataModel(Guid.NewGuid().ToString(), "Product1", "Description", ProductType.Production, CreateSubDataModel(), -100, false);
Assert.That(() => product.Validate(), Throws.TypeOf<ValidationException>()); Assert.That(() => product.Validate(), Throws.TypeOf<ValidationException>());
} }
@@ -73,8 +73,14 @@ internal class ProductDataModelTests
var productDescription = "Description"; var productDescription = "Description";
var productType = ProductType.Production; var productType = ProductType.Production;
var unitPrice = 100; var unitPrice = 100;
var ingredients = new List<ProductIngredientDataModel>
{
new ProductIngredientDataModel(Guid.NewGuid().ToString(), "Flour", 1),
new ProductIngredientDataModel(Guid.NewGuid().ToString(), "Sugar", 2)
};
var product = CreateDataModel(productId, productName, productDescription, productType, unitPrice, false);
var product = CreateDataModel(productId, productName, productDescription, productType, ingredients, unitPrice, false);
Assert.That(() => product.Validate(), Throws.Nothing); Assert.That(() => product.Validate(), Throws.Nothing);
Assert.Multiple(() => Assert.Multiple(() =>
@@ -84,9 +90,15 @@ internal class ProductDataModelTests
Assert.That(product.Description, Is.EqualTo(productDescription)); Assert.That(product.Description, Is.EqualTo(productDescription));
Assert.That(product.ProductType, Is.EqualTo(productType)); Assert.That(product.ProductType, Is.EqualTo(productType));
Assert.That(product.UnitPrice, Is.EqualTo(unitPrice)); Assert.That(product.UnitPrice, Is.EqualTo(unitPrice));
Assert.That(product.Ingredients, Is.EqualTo(ingredients)); // Добавлена проверка для списка ингредиентов
}); });
} }
private static ProductDataModel CreateDataModel(string? id, string? name, string? description, ProductType productType, int unitPrice, bool isDeleted) =>
new(id, name, description, productType, unitPrice, isDeleted); private static ProductDataModel CreateDataModel(string? id, string? name, string? description, ProductType productType, List<ProductIngredientDataModel> ingredients, int unitPrice, bool isDeleted) =>
new(id, name, description, productType, ingredients, unitPrice, isDeleted);
private static List<ProductIngredientDataModel> CreateSubDataModel()
=> new List<ProductIngredientDataModel> { new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 1) };
} }

View File

@@ -13,92 +13,85 @@ internal class ProductionDataModelTests
[Test] [Test]
public void IdIsNullOrEmptyTest() public void IdIsNullOrEmptyTest()
{ {
var production = CreateDataModel(null, DateTime.Now, 5, 100, Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), new List<ProductIngredientDataModel>()); var production = CreateDataModel(null, DateTime.Now, 5, 100, Guid.NewGuid().ToString(), Guid.NewGuid().ToString());
Assert.That(() => production.Validate(), Throws.TypeOf<ValidationException>()); Assert.That(() => production.Validate(), Throws.TypeOf<ValidationException>());
production = CreateDataModel(string.Empty, DateTime.Now, 5, 100, Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), new List<ProductIngredientDataModel>()); production = CreateDataModel(string.Empty, DateTime.Now, 5, 100, Guid.NewGuid().ToString(), Guid.NewGuid().ToString());
Assert.That(() => production.Validate(), Throws.TypeOf<ValidationException>()); Assert.That(() => production.Validate(), Throws.TypeOf<ValidationException>());
} }
[Test] [Test]
public void IdIsNotGuidTest() public void IdIsNotGuidTest()
{ {
var production = CreateDataModel("id", DateTime.Now, 5, 100, Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), new List<ProductIngredientDataModel>()); var production = CreateDataModel("id", DateTime.Now, 5, 100, Guid.NewGuid().ToString(), Guid.NewGuid().ToString());
Assert.That(() => production.Validate(), Throws.TypeOf<ValidationException>()); Assert.That(() => production.Validate(), Throws.TypeOf<ValidationException>());
} }
[Test] [Test]
public void ProductionDateIsInTheFutureTest() public void ProductionDateIsInTheFutureTest()
{ {
var production = CreateDataModel(Guid.NewGuid().ToString(), DateTime.Now.AddDays(1), 5, 100, Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), new List<ProductIngredientDataModel>()); var production = CreateDataModel(Guid.NewGuid().ToString(), DateTime.Now.AddDays(1), 5, 100, Guid.NewGuid().ToString(), Guid.NewGuid().ToString());
Assert.That(() => production.Validate(), Throws.TypeOf<ValidationException>()); Assert.That(() => production.Validate(), Throws.TypeOf<ValidationException>());
} }
[Test] [Test]
public void CountIsLessThanOrEqualToZeroTest() public void CountIsLessThanOrEqualToZeroTest()
{ {
var production = CreateDataModel(Guid.NewGuid().ToString(), DateTime.Now, 0, 100, Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), new List<ProductIngredientDataModel>()); var production = CreateDataModel(Guid.NewGuid().ToString(), DateTime.Now, 0, 100, Guid.NewGuid().ToString(), Guid.NewGuid().ToString());
Assert.That(() => production.Validate(), Throws.TypeOf<ValidationException>()); Assert.That(() => production.Validate(), Throws.TypeOf<ValidationException>());
production = CreateDataModel(Guid.NewGuid().ToString(), DateTime.Now, -1, 100, Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), new List<ProductIngredientDataModel>()); production = CreateDataModel(Guid.NewGuid().ToString(), DateTime.Now, -1, 100, Guid.NewGuid().ToString(), Guid.NewGuid().ToString());
Assert.That(() => production.Validate(), Throws.TypeOf<ValidationException>()); Assert.That(() => production.Validate(), Throws.TypeOf<ValidationException>());
} }
[Test] [Test]
public void WorkerIdIsNullOrEmptyTest() public void WorkerIdIsNullOrEmptyTest()
{ {
var production = CreateDataModel(Guid.NewGuid().ToString(), DateTime.Now, 5, 100, null, Guid.NewGuid().ToString(), new List<ProductIngredientDataModel>()); var production = CreateDataModel(Guid.NewGuid().ToString(), DateTime.Now, 5, 100, null, Guid.NewGuid().ToString());
Assert.That(() => production.Validate(), Throws.TypeOf<ValidationException>()); Assert.That(() => production.Validate(), Throws.TypeOf<ValidationException>());
production = CreateDataModel(Guid.NewGuid().ToString(), DateTime.Now, 5, 100, string.Empty, Guid.NewGuid().ToString(), new List<ProductIngredientDataModel>()); production = CreateDataModel(Guid.NewGuid().ToString(), DateTime.Now, 5, 100, string.Empty, Guid.NewGuid().ToString());
Assert.That(() => production.Validate(), Throws.TypeOf<ValidationException>()); Assert.That(() => production.Validate(), Throws.TypeOf<ValidationException>());
} }
[Test] [Test]
public void WorkerIdIsNotGuidTest() public void WorkerIdIsNotGuidTest()
{ {
var production = CreateDataModel(Guid.NewGuid().ToString(), DateTime.Now, 5, 100, "workerId", Guid.NewGuid().ToString(), new List<ProductIngredientDataModel>()); var production = CreateDataModel(Guid.NewGuid().ToString(), DateTime.Now, 5, 100, "workerId", Guid.NewGuid().ToString());
Assert.That(() => production.Validate(), Throws.TypeOf<ValidationException>()); Assert.That(() => production.Validate(), Throws.TypeOf<ValidationException>());
} }
[Test] [Test]
public void ProductIdIsNullOrEmptyTest() public void ProductIdIsNullOrEmptyTest()
{ {
var production = CreateDataModel(Guid.NewGuid().ToString(), DateTime.Now, 5, 100, Guid.NewGuid().ToString(), null, new List<ProductIngredientDataModel>()); var production = CreateDataModel(Guid.NewGuid().ToString(), DateTime.Now, 5, 100, Guid.NewGuid().ToString(), null);
Assert.That(() => production.Validate(), Throws.TypeOf<ValidationException>()); Assert.That(() => production.Validate(), Throws.TypeOf<ValidationException>());
production = CreateDataModel(Guid.NewGuid().ToString(), DateTime.Now, 5, 100, Guid.NewGuid().ToString(), string.Empty, new List<ProductIngredientDataModel>()); production = CreateDataModel(Guid.NewGuid().ToString(), DateTime.Now, 5, 100, Guid.NewGuid().ToString(), string.Empty);
Assert.That(() => production.Validate(), Throws.TypeOf<ValidationException>()); Assert.That(() => production.Validate(), Throws.TypeOf<ValidationException>());
} }
[Test] [Test]
public void ProductIdIsNotGuidTest() public void ProductIdIsNotGuidTest()
{ {
var production = CreateDataModel(Guid.NewGuid().ToString(), DateTime.Now, 5, 100, Guid.NewGuid().ToString(), "productId", new List<ProductIngredientDataModel>()); var production = CreateDataModel(Guid.NewGuid().ToString(), DateTime.Now, 5, 100, Guid.NewGuid().ToString(), "productId");
Assert.That(() => production.Validate(), Throws.TypeOf<ValidationException>()); Assert.That(() => production.Validate(), Throws.TypeOf<ValidationException>());
} }
[Test] [Test]
public void SumIsLessThanOrEqualToZeroTest() public void SumIsLessThanOrEqualToZeroTest()
{ {
var production = CreateDataModel(Guid.NewGuid().ToString(), DateTime.Now, 5, -100, Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), new List<ProductIngredientDataModel>()); var production = CreateDataModel(Guid.NewGuid().ToString(), DateTime.Now, 5, -100, Guid.NewGuid().ToString(), Guid.NewGuid().ToString());
Assert.That(() => production.Validate(), Throws.TypeOf<ValidationException>()); Assert.That(() => production.Validate(), Throws.TypeOf<ValidationException>());
production = CreateDataModel(Guid.NewGuid().ToString(), DateTime.Now, 5, 0, Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), new List<ProductIngredientDataModel>()); production = CreateDataModel(Guid.NewGuid().ToString(), DateTime.Now, 5, 0, Guid.NewGuid().ToString(), Guid.NewGuid().ToString());
Assert.That(() => production.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void ProductsIsEmptyTest()
{
var production = CreateDataModel(Guid.NewGuid().ToString(), DateTime.Now, 5, 100, Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), new List<ProductIngredientDataModel>());
Assert.That(() => production.Validate(), Throws.TypeOf<ValidationException>()); Assert.That(() => production.Validate(), Throws.TypeOf<ValidationException>());
} }
[Test] [Test]
public void AllFieldsAreCorrectTest() public void AllFieldsAreCorrectTest()
{ {
var production = CreateDataModel(Guid.NewGuid().ToString(), DateTime.Now, 5, 100, Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), new List<ProductIngredientDataModel> { new ProductIngredientDataModel("productId", "ingredientId", 1) }); var production = CreateDataModel(Guid.NewGuid().ToString(), DateTime.Now, 5, 100, Guid.NewGuid().ToString(), Guid.NewGuid().ToString());
Assert.That(() => production.Validate(), Throws.Nothing); Assert.That(() => production.Validate(), Throws.Nothing);
Assert.Multiple(() => Assert.Multiple(() =>
{ {
@@ -111,8 +104,8 @@ internal class ProductionDataModelTests
}); });
} }
private static ProductionDataModel CreateDataModel(string id, DateTime productionDate, int count, double sum, string workerId, string productId, List<ProductIngredientDataModel> products) private static ProductionDataModel CreateDataModel(string id, DateTime productionDate, int count, double sum, string workerId, string productId)
{ {
return new ProductionDataModel(id, productionDate, count, sum, workerId, productId, products); return new ProductionDataModel(id, productionDate, count, sum, workerId, productId);
} }
} }

View File

@@ -12,12 +12,14 @@
<ItemGroup> <ItemGroup>
<PackageReference Include="coverlet.collector" Version="6.0.0" /> <PackageReference Include="coverlet.collector" Version="6.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" /> <PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="Moq" Version="4.20.72" />
<PackageReference Include="NUnit" Version="3.14.0" /> <PackageReference Include="NUnit" Version="3.14.0" />
<PackageReference Include="NUnit.Analyzers" Version="3.9.0" /> <PackageReference Include="NUnit.Analyzers" Version="3.9.0" />
<PackageReference Include="NUnit3TestAdapter" Version="4.5.0" /> <PackageReference Include="NUnit3TestAdapter" Version="4.5.0" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\SladkieBulkiBusinessLogic\SladkieBulkiBusinessLogic.csproj" />
<ProjectReference Include="..\SladkieBulkiContrakts\SladkieBulkiContrakts.csproj" /> <ProjectReference Include="..\SladkieBulkiContrakts\SladkieBulkiContrakts.csproj" />
</ItemGroup> </ItemGroup>