Compare commits
33 Commits
Task_1_Mod
...
Task_3_Sto
| Author | SHA1 | Date | |
|---|---|---|---|
| f89577598a | |||
| 99fa16d354 | |||
| 0706b6e6e5 | |||
| 472ad965a8 | |||
| 75d8b547cb | |||
| c0f3362f42 | |||
| d92732140c | |||
| 5924d86f90 | |||
| 03d8022433 | |||
| c1a09db223 | |||
| e64854e1f9 | |||
| 70b42a97f4 | |||
| bb93edd951 | |||
| d28bb63707 | |||
| f54c8ecd02 | |||
| ca8e1737d0 | |||
| aeffe2f451 | |||
| ab3a3f6819 | |||
| 6f2d341f01 | |||
| 148b67f195 | |||
| dbf2331511 | |||
| 4b72fdbe96 | |||
| f5a54bab32 | |||
| 0d4a275328 | |||
| 684073e3be | |||
| 5cc67e747e | |||
| 56a54e19aa | |||
| 2e976740af | |||
| 1d72951b99 | |||
| 595041c319 | |||
| 71b48ba503 | |||
| 1d8c331535 | |||
| 362f1e73de |
@@ -5,7 +5,11 @@ VisualStudioVersion = 17.9.34728.123
|
||||
MinimumVisualStudioVersion = 10.0.40219.1
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SladkieBulkiContrakts", "SladkieBulkiContrakts\SladkieBulkiContrakts.csproj", "{04F3CF43-43CA-4EF0-B5BB-913425A85E29}"
|
||||
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("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SladkieBulkiBusinessLogic", "SladkieBulkiBusinessLogic\SladkieBulkiBusinessLogic.csproj", "{004CA71C-5308-44C7-82DA-4098BA6F5F1C}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SladkieBulkiDatabase", "SladkieBulkiDatabase\SladkieBulkiDatabase.csproj", "{A39687D5-A64A-4EF1-B988-BD58CFCACDDE}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
@@ -21,6 +25,14 @@ Global
|
||||
{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.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
|
||||
{A39687D5-A64A-4EF1-B988-BD58CFCACDDE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{A39687D5-A64A-4EF1-B988-BD58CFCACDDE}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{A39687D5-A64A-4EF1-B988-BD58CFCACDDE}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{A39687D5-A64A-4EF1-B988-BD58CFCACDDE}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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>
|
||||
@@ -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);
|
||||
}
|
||||
@@ -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);
|
||||
|
||||
}
|
||||
@@ -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);
|
||||
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
@@ -8,10 +8,10 @@ using System.Threading.Tasks;
|
||||
|
||||
namespace SladkieBulkiContrakts.DataModels;
|
||||
|
||||
public class IngredientDataModel(string id, string nameIngridients, string sizeInit, double initPrice) : IValidation
|
||||
public class IngredientDataModel(string id, string NameIngredients, string sizeInit, double initPrice) : IValidation
|
||||
{
|
||||
public string Id { get; private set; } = id;
|
||||
public string NameIngridients { get; private set; } = nameIngridients;
|
||||
public string NameIngredients { get; private set; } = NameIngredients;
|
||||
public string SizeInit { get; private set; } = sizeInit;
|
||||
public double InitPrice { get; private set; } = initPrice;
|
||||
|
||||
@@ -21,8 +21,8 @@ public class IngredientDataModel(string id, string nameIngridients, string sizeI
|
||||
throw new ValidationException("Field Id is empty");
|
||||
if (!Id.IsGuid())
|
||||
throw new ValidationException("The value in the field Id is not a unique identifier");
|
||||
if (NameIngridients.IsEmpty())
|
||||
throw new ValidationException("Field NameIngridients is empty");
|
||||
if (NameIngredients.IsEmpty())
|
||||
throw new ValidationException("Field NameIngredients is empty");
|
||||
if (SizeInit.IsEmpty())
|
||||
throw new ValidationException("Field SizeInit is empty");
|
||||
if (InitPrice <= 0)
|
||||
|
||||
@@ -10,24 +10,30 @@ using System.Xml;
|
||||
|
||||
namespace SladkieBulkiContrakts.DataModels;
|
||||
|
||||
public class PostDataModel(string id, string postName, PostType postType, double salary) : IValidation
|
||||
public class PostDataModel(string postId, string postName, PostType postType, double salary, bool isActual, DateTime changeDate) : IValidation
|
||||
{
|
||||
public string Id { get; private set; } = id;
|
||||
public string Id { get; private set; } = postId;
|
||||
public string PostName { get; private set; } = postName;
|
||||
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()
|
||||
{
|
||||
if (Id.IsEmpty())
|
||||
throw new ValidationException("Field Id is empty");
|
||||
|
||||
if (!Id.IsGuid())
|
||||
throw new ValidationException("The value in the field Id is not a unique identifier");
|
||||
|
||||
if (PostName.IsEmpty())
|
||||
throw new ValidationException("Field PostName is empty");
|
||||
|
||||
if (PostType == PostType.None)
|
||||
throw new ValidationException("Field PostType is empty");
|
||||
|
||||
if (Salary <= 0)
|
||||
throw new ValidationException("Field Salary is empty");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -10,7 +10,7 @@ using System.Xml;
|
||||
|
||||
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> productIngredients, int unitPrice, bool isDeleted) : IValidation
|
||||
{
|
||||
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 List<ProductIngredientDataModel> Ingredients { get; private set; } = productIngredients;
|
||||
|
||||
public int UnitPrice { get; private set; } = unitPrice;
|
||||
|
||||
public bool IsDeleted { get; private set; } = isDeleted;
|
||||
|
||||
|
||||
public void Validate()
|
||||
{
|
||||
if (Id.IsEmpty())
|
||||
@@ -38,5 +41,7 @@ public class ProductDataModel (string id, string name, string description, Produ
|
||||
throw new ValidationException("Field ProductType is empty");
|
||||
if (UnitPrice <= 0)
|
||||
throw new ValidationException("Field UnitPrice must be greater than zero");
|
||||
if (Ingredients == null || Ingredients.Count == 0)
|
||||
throw new ValidationException("No ingredients defined");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,14 +1,7 @@
|
||||
using SladkieBulkiContrakts.Extensions;
|
||||
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, List<ProductIngredientDataModel>? products) : IValidation
|
||||
public class ProductionDataModel(string id, DateTime productionDate, int count, double sum, string workerId, string productId) : IValidation
|
||||
{
|
||||
public string Id { get; private set; } = id;
|
||||
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 string WorkerId { get; private set; } = workerId;
|
||||
public string ProductId { get; private set; } = productId;
|
||||
public List<ProductIngredientDataModel>? Products { get; private set; } = products;
|
||||
|
||||
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");
|
||||
if (Sum <= 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");
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,14 +12,14 @@ public class SalaryDataModel(string workerId, DateTime salaryDate, double worker
|
||||
{
|
||||
public string WorkerId { get; private set; } = workerId;
|
||||
public DateTime SalaryDate { get; private set; } = salaryDate;
|
||||
public double Salary { get; private set; } = workerSalary;
|
||||
public double WorkerSalary { get; private set; } = workerSalary;
|
||||
public void Validate()
|
||||
{
|
||||
if (WorkerId.IsEmpty())
|
||||
throw new ValidationException("Field WorkerId is empty");
|
||||
if (!WorkerId.IsGuid())
|
||||
throw new ValidationException("The value in the field WorkerId is not a unique identifier");
|
||||
if (Salary <= 0)
|
||||
if (WorkerSalary <= 0)
|
||||
throw new ValidationException("Field Salary is less than or equal to 0");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,6 +11,6 @@ public enum PostType
|
||||
None = 0,
|
||||
Preform = 1, // Заготовщик
|
||||
Manufacturer = 2, // Изготовитель
|
||||
packer = 3 // Упаковщик
|
||||
Packer = 3 // Упаковщик
|
||||
|
||||
}
|
||||
|
||||
@@ -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 ElementDeletedException : Exception
|
||||
{
|
||||
public ElementDeletedException(string id) : base($"Cannot modify a deleted item (id: {id})") { }
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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}") { }
|
||||
}
|
||||
@@ -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") { }
|
||||
}
|
||||
@@ -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) { }
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SladkieBulkiContrakts.Infrastructure;
|
||||
|
||||
public interface IConfigurationDatabase
|
||||
{
|
||||
string ConnectionString { get; }
|
||||
}
|
||||
@@ -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);
|
||||
|
||||
}
|
||||
@@ -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);
|
||||
|
||||
}
|
||||
@@ -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);
|
||||
|
||||
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
@@ -0,0 +1,138 @@
|
||||
using AutoMapper;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Npgsql;
|
||||
using SladkieBulkiContrakts.DataModels;
|
||||
using SladkieBulkiContrakts.Exceptions;
|
||||
using SladkieBulkiContrakts.StoragesContarcts;
|
||||
using SladkieBulkiDatabase.Models;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SladkieBulkiDatabase.Implementations;
|
||||
|
||||
internal class IngredientStorageContract : IIngredientStorageContract
|
||||
{
|
||||
private readonly SladkieBulkiDbContext _dbContext;
|
||||
private readonly Mapper _mapper;
|
||||
|
||||
public IngredientStorageContract(SladkieBulkiDbContext sladkieBulkiDbContext)
|
||||
{
|
||||
_dbContext = sladkieBulkiDbContext;
|
||||
var config = new MapperConfiguration(cfg =>
|
||||
{
|
||||
cfg.CreateMap<Ingredient, IngredientDataModel>();
|
||||
cfg.CreateMap<IngredientDataModel, Ingredient>();
|
||||
});
|
||||
_mapper = new Mapper(config);
|
||||
}
|
||||
|
||||
public List<IngredientDataModel> GetList()
|
||||
{
|
||||
try
|
||||
{
|
||||
return [.. _dbContext.Ingredients.Select(x => _mapper.Map<IngredientDataModel>(x))];
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw new StorageException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
public IngredientDataModel? GetElementById(string id)
|
||||
{
|
||||
try
|
||||
{
|
||||
return _mapper.Map<IngredientDataModel>(GetIngredientById(id));
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw new StorageException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
public IngredientDataModel? GetElementByName(string nameIngredients)
|
||||
{
|
||||
try
|
||||
{
|
||||
return _mapper.Map<IngredientDataModel>(_dbContext.Ingredients.FirstOrDefault(x => x.NameIngredients == nameIngredients));
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw new StorageException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
public List<IngredientDataModel>? GetElementBySizeUnit(string sizeInit)
|
||||
{
|
||||
try
|
||||
{
|
||||
var ingredients = _dbContext.Ingredients.Where(x => x.SizeInit == sizeInit).ToList();
|
||||
|
||||
return _mapper.Map<List<IngredientDataModel>>(ingredients);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw new StorageException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
public void AddElement(IngredientDataModel ingredientDataModel)
|
||||
{
|
||||
try
|
||||
{
|
||||
_dbContext.Ingredients.Add(_mapper.Map<Ingredient>(ingredientDataModel));
|
||||
_dbContext.SaveChanges();
|
||||
}
|
||||
catch (InvalidOperationException ex) when (ex.TargetSite?.Name == "ThrowIdentityConflict")
|
||||
{
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw new ElementExistsException("Id", ingredientDataModel.Id);
|
||||
}
|
||||
catch (DbUpdateException ex) when (ex.InnerException is PostgresException { ConstraintName: "IX_Ingredients_NameIngridients" })
|
||||
{
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw new ElementExistsException("NameIngredients", ingredientDataModel.NameIngredients);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw new StorageException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public void UpdElement(IngredientDataModel ingredientDataModel)
|
||||
{
|
||||
try
|
||||
{
|
||||
var element = GetIngredientById(ingredientDataModel.Id) ?? throw new ElementNotFoundException(ingredientDataModel.Id);
|
||||
_dbContext.Ingredients.Update(_mapper.Map(ingredientDataModel, element));
|
||||
_dbContext.SaveChanges();
|
||||
}
|
||||
catch (ElementNotFoundException)
|
||||
{
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw;
|
||||
}
|
||||
catch (DbUpdateException ex) when (ex.InnerException is PostgresException { ConstraintName: "IX_Ingredients_NameIngridients" })
|
||||
{
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw new ElementExistsException("NameIngredients", ingredientDataModel.NameIngredients);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw new StorageException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
private Ingredient? GetIngredientById(string id) => _dbContext.Ingredients.FirstOrDefault(x => x.Id == id);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,195 @@
|
||||
using AutoMapper;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Npgsql;
|
||||
using SladkieBulkiContrakts.DataModels;
|
||||
using SladkieBulkiContrakts.Exceptions;
|
||||
using SladkieBulkiContrakts.StoragesContarcts;
|
||||
using SladkieBulkiDatabase.Models;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SladkieBulkiDatabase.Implementations;
|
||||
|
||||
internal class PostStorageContract : IPostStorageContract
|
||||
{
|
||||
private readonly SladkieBulkiDbContext _dbContext;
|
||||
private readonly Mapper _mapper;
|
||||
|
||||
public PostStorageContract(SladkieBulkiDbContext dbContext)
|
||||
{
|
||||
_dbContext = dbContext;
|
||||
var config = new MapperConfiguration(cfg =>
|
||||
{
|
||||
cfg.CreateMap<Post, PostDataModel>()
|
||||
.ForMember(x => x.Id, x => x.MapFrom(src => src.PostId));
|
||||
cfg.CreateMap<PostDataModel, Post>()
|
||||
.ForMember(x => x.Id, x => x.Ignore())
|
||||
.ForMember(x => x.PostId, x => x.MapFrom(src => src.Id))
|
||||
.ForMember(x => x.IsActual, x => x.MapFrom(src => true))
|
||||
.ForMember(x => x.ChangeDate, x => x.MapFrom(src => DateTime.UtcNow));
|
||||
});
|
||||
_mapper = new Mapper(config);
|
||||
}
|
||||
|
||||
public List<PostDataModel> GetList(bool onlyActual = true)
|
||||
{
|
||||
try
|
||||
{
|
||||
var query = _dbContext.Posts.AsQueryable();
|
||||
if (onlyActual)
|
||||
{
|
||||
query = query.Where(x => x.IsActual);
|
||||
}
|
||||
return [.. query.Select(x => _mapper.Map<PostDataModel>(x))];
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw new StorageException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
public List<PostDataModel> GetPostWithHistory(string postId)
|
||||
{
|
||||
try
|
||||
{
|
||||
return [.. _dbContext.Posts.Where(x => x.PostId == postId).Select(x => _mapper.Map<PostDataModel>(x))];
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw new StorageException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
public PostDataModel? GetElementById(string id)
|
||||
{
|
||||
try
|
||||
{
|
||||
return _mapper.Map<PostDataModel>(_dbContext.Posts.FirstOrDefault(x => x.PostId == id && x.IsActual));
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw new StorageException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
public PostDataModel? GetElementByName(string name)
|
||||
{
|
||||
try
|
||||
{
|
||||
return _mapper.Map<PostDataModel>(_dbContext.Posts.FirstOrDefault(x => x.PostName == name && x.IsActual));
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw new StorageException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
public void AddElement(PostDataModel postDataModel)
|
||||
{
|
||||
try
|
||||
{
|
||||
_dbContext.Posts.Add(_mapper.Map<Post>(postDataModel));
|
||||
_dbContext.SaveChanges();
|
||||
}
|
||||
catch (DbUpdateException ex) when (ex.InnerException is PostgresException { ConstraintName: "IX_Posts_PostName_IsActual" })
|
||||
{
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw new ElementExistsException("PostName", postDataModel.PostName);
|
||||
}
|
||||
catch (DbUpdateException ex) when (ex.InnerException is PostgresException { ConstraintName: "IX_Posts_PostId_IsActual" })
|
||||
{
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw new ElementExistsException("PostId", postDataModel.Id);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw new StorageException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
public void UpdElement(PostDataModel postDataModel)
|
||||
{
|
||||
try
|
||||
{
|
||||
var transaction = _dbContext.Database.BeginTransaction();
|
||||
try
|
||||
{
|
||||
var element = GetPostById(postDataModel.Id) ?? throw new ElementNotFoundException(postDataModel.Id);
|
||||
if (!element.IsActual)
|
||||
{
|
||||
throw new ElementDeletedException(postDataModel.Id);
|
||||
}
|
||||
element.IsActual = false;
|
||||
_dbContext.SaveChanges();
|
||||
var newElement = _mapper.Map<Post>(postDataModel);
|
||||
_dbContext.Posts.Add(newElement);
|
||||
_dbContext.SaveChanges();
|
||||
transaction.Commit();
|
||||
}
|
||||
catch
|
||||
{
|
||||
transaction.Rollback();
|
||||
throw;
|
||||
}
|
||||
}
|
||||
catch (DbUpdateException ex) when (ex.InnerException is PostgresException { ConstraintName: "IX_Posts_PostName_IsActual" })
|
||||
{
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw new ElementExistsException("PostName", postDataModel.PostName);
|
||||
}
|
||||
catch (Exception ex) when (ex is ElementDeletedException || ex is ElementNotFoundException)
|
||||
{
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw new StorageException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
public void DelElement(string id)
|
||||
{
|
||||
try
|
||||
{
|
||||
var element = GetPostById(id) ?? throw new ElementNotFoundException(id);
|
||||
if (!element.IsActual)
|
||||
{
|
||||
throw new ElementDeletedException(id);
|
||||
}
|
||||
element.IsActual = false;
|
||||
_dbContext.SaveChanges();
|
||||
}
|
||||
catch
|
||||
{
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public void ResElement(string id)
|
||||
{
|
||||
try
|
||||
{
|
||||
var element = GetPostById(id) ?? throw new ElementNotFoundException(id);
|
||||
element.IsActual = true;
|
||||
_dbContext.SaveChanges();
|
||||
}
|
||||
catch
|
||||
{
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
private Post? GetPostById(string id) => _dbContext.Posts.Where(x => x.PostId == id).OrderByDescending(x => x.ChangeDate).FirstOrDefault();
|
||||
}
|
||||
@@ -0,0 +1,213 @@
|
||||
using AutoMapper;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Npgsql;
|
||||
using SladkieBulkiContrakts.DataModels;
|
||||
using SladkieBulkiContrakts.Exceptions;
|
||||
using SladkieBulkiContrakts.StoragesContarcts;
|
||||
using SladkieBulkiDatabase.Models;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SladkieBulkiDatabase.Implementations;
|
||||
|
||||
internal class ProductStorageContract : IProductStorageContract
|
||||
{
|
||||
private readonly SladkieBulkiDbContext _dbContext;
|
||||
private readonly Mapper _mapper;
|
||||
|
||||
public ProductStorageContract(SladkieBulkiDbContext dbContext)
|
||||
{
|
||||
_dbContext = dbContext;
|
||||
var config = new MapperConfiguration(cfg =>
|
||||
{
|
||||
cfg.CreateMap<ProductIngredient, ProductIngredientDataModel>();
|
||||
cfg.CreateMap<ProductIngredientDataModel, ProductIngredient>();
|
||||
cfg.CreateMap<Product, ProductDataModel>();
|
||||
cfg.CreateMap<ProductDataModel, Product>()
|
||||
.ForMember(x => x.IsDeleted, x => x.MapFrom(src => false))
|
||||
.ForMember(x => x.ProductIngredients, x => x.MapFrom(src => src.Ingredients));
|
||||
cfg.CreateMap<ProductHistory, ProductHistoryDataModel>();
|
||||
});
|
||||
_mapper = new Mapper(config);
|
||||
}
|
||||
|
||||
|
||||
public List<ProductDataModel> GetList(bool onlyActive = true)
|
||||
{
|
||||
try
|
||||
{
|
||||
var query = _dbContext.Products.Include(x => x.ProductIngredients).AsQueryable();
|
||||
if (onlyActive)
|
||||
{
|
||||
query = query.Where(x => !x.IsDeleted);
|
||||
}
|
||||
return [.. query.Select(x => _mapper.Map<ProductDataModel>(x))];
|
||||
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw new StorageException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
public List<ProductHistoryDataModel> GetHistoryByProductId(string productId)
|
||||
{
|
||||
try
|
||||
{
|
||||
return _dbContext.ProductHistories
|
||||
.Where(x => x.ProductId == productId)
|
||||
.OrderByDescending(x => x.ChangeDate)
|
||||
.Select(x => _mapper.Map<ProductHistoryDataModel>(x)).ToList();
|
||||
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw new StorageException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
public ProductDataModel? GetElementById(string id)
|
||||
{
|
||||
try
|
||||
{
|
||||
return _mapper.Map<ProductDataModel>(GetProductById(id));
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw new StorageException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
public ProductDataModel? GetElementByName(string name)
|
||||
{
|
||||
try
|
||||
{
|
||||
return _mapper.Map<ProductDataModel>(
|
||||
_dbContext.Products.FirstOrDefault(x => x.Name == name && !x.IsDeleted));
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw new StorageException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
public void AddElement(ProductDataModel productDataModel)
|
||||
{
|
||||
try
|
||||
{
|
||||
_dbContext.Products.Add(_mapper.Map<Product>(productDataModel));
|
||||
_dbContext.SaveChanges();
|
||||
}
|
||||
catch (InvalidOperationException ex) when (ex.TargetSite?.Name == "ThrowIdentityConflict")
|
||||
{
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw new ElementExistsException("Id", productDataModel.Id);
|
||||
}
|
||||
catch (DbUpdateException ex) when (ex.InnerException is PostgresException pgEx &&
|
||||
pgEx.ConstraintName == "IX_Products_Name_IsDeleted")
|
||||
{
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw new ElementExistsException("Name", productDataModel.Name);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw new StorageException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public void UpdElement(ProductDataModel productDataModel)
|
||||
{
|
||||
try
|
||||
{
|
||||
using var transaction = _dbContext.Database.BeginTransaction();
|
||||
try
|
||||
{
|
||||
var element = GetProductById(productDataModel.Id)
|
||||
?? throw new ElementNotFoundException(productDataModel.Id);
|
||||
|
||||
if (element.IsDeleted)
|
||||
throw new ElementDeletedException(productDataModel.Id);
|
||||
|
||||
// Проверяем, если цена изменилась, то добавляем её в историю
|
||||
if (element.UnitPrice != productDataModel.UnitPrice)
|
||||
{
|
||||
_dbContext.ProductHistories.Add(new ProductHistory
|
||||
{
|
||||
ProductId = element.Id,
|
||||
OldPrice = element.UnitPrice
|
||||
});
|
||||
_dbContext.SaveChanges();
|
||||
}
|
||||
|
||||
// Обновляем флаг IsDeleted, если он изменился
|
||||
if (element.IsDeleted != productDataModel.IsDeleted)
|
||||
{
|
||||
element.IsDeleted = productDataModel.IsDeleted;
|
||||
}
|
||||
|
||||
// Маппим остальные данные
|
||||
_mapper.Map(productDataModel, element);
|
||||
_dbContext.SaveChanges();
|
||||
transaction.Commit();
|
||||
}
|
||||
catch
|
||||
{
|
||||
transaction.Rollback();
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
catch (DbUpdateException ex) when (ex.InnerException is PostgresException postgresEx && postgresEx.SqlState == "23505" && postgresEx.ConstraintName == "IX_Products_Name_IsDeleted")
|
||||
{
|
||||
// Обработка ошибки уникальности
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw new ElementExistsException("Name", productDataModel.Name);
|
||||
}
|
||||
catch (Exception ex) when (ex is ElementDeletedException || ex is ElementNotFoundException)
|
||||
{
|
||||
// Перекидываем только специфичные ошибки
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
// В случае других исключений — логируем ошибку
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw new StorageException(ex);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
public void DelElement(string id)
|
||||
{
|
||||
try
|
||||
{
|
||||
var element = GetProductById(id) ?? throw new ElementNotFoundException(id);
|
||||
element.IsDeleted = true;
|
||||
_dbContext.SaveChanges();
|
||||
}
|
||||
catch (ElementNotFoundException)
|
||||
{
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw new StorageException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
private Product? GetProductById(string id) =>
|
||||
_dbContext.Products.FirstOrDefault(x => x.Id == id && !x.IsDeleted);
|
||||
}
|
||||
@@ -0,0 +1,106 @@
|
||||
using AutoMapper;
|
||||
using SladkieBulkiContrakts.Exceptions;
|
||||
using SladkieBulkiContrakts.StoragesContarcts;
|
||||
using SladkieBulkiDatabase.Models;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SladkieBulkiDatabase.Implementations;
|
||||
|
||||
internal class ProductionStorageContract : IProductionStorageContract
|
||||
{
|
||||
private readonly SladkieBulkiDbContext _dbContext;
|
||||
private readonly Mapper _mapper;
|
||||
|
||||
public ProductionStorageContract(SladkieBulkiDbContext dbContext)
|
||||
{
|
||||
_dbContext = dbContext;
|
||||
var config = new MapperConfiguration(cfg =>
|
||||
{
|
||||
cfg.CreateMap<Production, ProductionDataModel>();
|
||||
cfg.CreateMap<ProductionDataModel, Production>()
|
||||
.ForMember(dest => dest.Worker, opt => opt.Ignore())
|
||||
.ForMember(dest => dest.Product, opt => opt.Ignore());
|
||||
});
|
||||
_mapper = new Mapper(config);
|
||||
}
|
||||
|
||||
public List<ProductionDataModel> GetList(DateTime? startDate = null, DateTime? endDate = null, string? workerId = null, string? productId = null)
|
||||
{
|
||||
try
|
||||
{
|
||||
var query = _dbContext.Productions.AsQueryable();
|
||||
|
||||
if (startDate is not null && endDate is not null)
|
||||
{
|
||||
query = query.Where(p => p.ProductionDate >= startDate && p.ProductionDate < endDate);
|
||||
}
|
||||
|
||||
if (!string.IsNullOrEmpty(workerId))
|
||||
{
|
||||
query = query.Where(p => p.WorkerId == workerId);
|
||||
}
|
||||
|
||||
if (!string.IsNullOrEmpty(productId))
|
||||
{
|
||||
query = query.Where(p => p.ProductId == productId);
|
||||
}
|
||||
|
||||
return [.. query.Select(p => _mapper.Map<ProductionDataModel>(p))];
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw new StorageException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
public ProductionDataModel? GetElementById(string id)
|
||||
{
|
||||
try
|
||||
{
|
||||
return _mapper.Map<ProductionDataModel>(GetProductionById(id));
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw new StorageException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
public void AddElement(ProductionDataModel productionDataModel)
|
||||
{
|
||||
try
|
||||
{
|
||||
_dbContext.Productions.Add(_mapper.Map<Production>(productionDataModel));
|
||||
_dbContext.SaveChanges();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw new StorageException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
public void DelElement(string id)
|
||||
{
|
||||
var element = GetProductionById(id);
|
||||
if (element == null)
|
||||
{
|
||||
throw new ElementNotFoundException($"Element not found at value = {id}");
|
||||
}
|
||||
|
||||
_dbContext.Productions.Remove(element);
|
||||
_dbContext.SaveChanges();
|
||||
}
|
||||
|
||||
|
||||
|
||||
private Production? GetProductionById(string id)
|
||||
{
|
||||
return _dbContext.Productions.FirstOrDefault(p => p.Id == id);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
using AutoMapper;
|
||||
using SladkieBulkiContrakts.DataModels;
|
||||
using SladkieBulkiContrakts.Exceptions;
|
||||
using SladkieBulkiContrakts.StoragesContarcts;
|
||||
using SladkieBulkiDatabase.Models;
|
||||
using SladkieBulkiDatabase;
|
||||
|
||||
internal class SalaryStorageContract : ISalaryStorageContract
|
||||
{
|
||||
private readonly SladkieBulkiDbContext _dbContext;
|
||||
private readonly Mapper _mapper;
|
||||
|
||||
public SalaryStorageContract(SladkieBulkiDbContext dbContext)
|
||||
{
|
||||
_dbContext = dbContext;
|
||||
var config = new MapperConfiguration(cfg =>
|
||||
{
|
||||
cfg.AddMaps(typeof(SladkieBulkiDbContext).Assembly);
|
||||
});
|
||||
_mapper = new Mapper(config);
|
||||
}
|
||||
|
||||
public List<SalaryDataModel> GetList(DateTime startDate, DateTime endDate, string? workerId = null)
|
||||
{
|
||||
try
|
||||
{
|
||||
var query = _dbContext.Salaries.Where(x => x.SalaryDate >= startDate && x.SalaryDate <= endDate);
|
||||
if (workerId is not null)
|
||||
{
|
||||
query = query.Where(x => x.WorkerId == workerId);
|
||||
}
|
||||
return [.. query.Select(x => _mapper.Map<SalaryDataModel>(x))];
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw new StorageException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
public void AddElement(SalaryDataModel salaryDataModel)
|
||||
{
|
||||
try
|
||||
{
|
||||
_dbContext.Salaries.Add(_mapper.Map<Salary>(salaryDataModel));
|
||||
_dbContext.SaveChanges();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw new StorageException(ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,156 @@
|
||||
using AutoMapper;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Npgsql;
|
||||
using SladkieBulkiContrakts.DataModels;
|
||||
using SladkieBulkiContrakts.Exceptions;
|
||||
using SladkieBulkiContrakts.StoragesContarcts;
|
||||
using SladkieBulkiDatabase.Models;
|
||||
|
||||
namespace SladkieBulkiDatabase.Implementations;
|
||||
|
||||
internal class WorkerStorageContract : IWorkerStorageContract
|
||||
{
|
||||
private readonly SladkieBulkiDbContext _dbContext;
|
||||
private readonly Mapper _mapper;
|
||||
|
||||
public WorkerStorageContract(SladkieBulkiDbContext dbContext)
|
||||
{
|
||||
_dbContext = dbContext;
|
||||
var config = new MapperConfiguration(cfg =>
|
||||
{
|
||||
cfg.AddMaps(typeof(Worker).Assembly);
|
||||
});
|
||||
_mapper = new Mapper(config);
|
||||
}
|
||||
|
||||
public List<WorkerDataModel> GetList(bool onlyActive = true, string? postId = null, DateTime? fromBirthDate = null, DateTime? toBirthDate = null, DateTime? fromEmploymentDate = null, DateTime? toEmploymentDate = null)
|
||||
{
|
||||
try
|
||||
{
|
||||
var query = _dbContext.Workers.AsQueryable();
|
||||
if (onlyActive)
|
||||
{
|
||||
query = query.Where(x => !x.IsDeleted);
|
||||
}
|
||||
if (postId is not null)
|
||||
{
|
||||
query = query.Where(x => x.PostId == postId);
|
||||
}
|
||||
if (fromBirthDate is not null && toBirthDate is not null)
|
||||
{
|
||||
query = query.Where(x => x.BirthDate >= fromBirthDate && x.BirthDate <= toBirthDate);
|
||||
}
|
||||
if (fromEmploymentDate is not null && toEmploymentDate is not null)
|
||||
{
|
||||
query = query.Where(x => x.EmploymentDate >= fromEmploymentDate && x.EmploymentDate <= toEmploymentDate);
|
||||
}
|
||||
return [.. query.Select(x => _mapper.Map<WorkerDataModel>(x))];
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw new StorageException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
public WorkerDataModel? GetElementById(string id)
|
||||
{
|
||||
try
|
||||
{
|
||||
return _mapper.Map<WorkerDataModel>(GetWorkerById(id));
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw new StorageException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
public WorkerDataModel? GetElementByFIO(string fio)
|
||||
{
|
||||
try
|
||||
{
|
||||
return _mapper.Map<WorkerDataModel>(_dbContext.Workers.FirstOrDefault(x => x.FIO == fio));
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw new StorageException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
public void AddElement(WorkerDataModel workerDataModel)
|
||||
{
|
||||
try
|
||||
{
|
||||
_dbContext.Workers.Add(_mapper.Map<Worker>(workerDataModel));
|
||||
_dbContext.SaveChanges();
|
||||
}
|
||||
catch (InvalidOperationException ex) when (ex.TargetSite?.Name == "ThrowIdentityConflict")
|
||||
{
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw new ElementExistsException("Id", workerDataModel.Id);
|
||||
}
|
||||
catch (DbUpdateException ex) when (ex.InnerException is PostgresException { ConstraintName: "IX_Workers_Email" })
|
||||
{
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw new ElementExistsException("Email", workerDataModel.Email);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw new StorageException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public void UpdElement(WorkerDataModel workerDataModel)
|
||||
{
|
||||
try
|
||||
{
|
||||
var element = GetWorkerById(workerDataModel.Id) ?? throw new ElementNotFoundException(workerDataModel.Id);
|
||||
|
||||
_mapper.Map(workerDataModel, element);
|
||||
_dbContext.SaveChanges();
|
||||
}
|
||||
catch (ElementNotFoundException)
|
||||
{
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw;
|
||||
}
|
||||
catch (DbUpdateException ex) when (ex.InnerException is PostgresException { ConstraintName: "IX_Workers_Email" })
|
||||
{
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw new ElementExistsException("Email", workerDataModel.Email);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw new StorageException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public void DelElement(string id)
|
||||
{
|
||||
try
|
||||
{
|
||||
var element = GetWorkerById(id) ?? throw new ElementNotFoundException(id);
|
||||
element.IsDeleted = true;
|
||||
_dbContext.SaveChanges();
|
||||
}
|
||||
catch (ElementNotFoundException)
|
||||
{
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw new StorageException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
private Worker? GetWorkerById(string id) => _dbContext.Workers.FirstOrDefault(x => x.Id == id && !x.IsDeleted);
|
||||
}
|
||||
|
||||
22
SladkieBulki/SladkieBulkiDatabase/Models/Ingredient.cs
Normal file
22
SladkieBulki/SladkieBulkiDatabase/Models/Ingredient.cs
Normal file
@@ -0,0 +1,22 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SladkieBulkiDatabase.Models;
|
||||
|
||||
internal class Ingredient
|
||||
{
|
||||
public required string Id { get; set; } = Guid.NewGuid().ToString();
|
||||
public required string NameIngredients { get; set; }
|
||||
public required string SizeInit { get; set; }
|
||||
public double InitPrice { get; set; }
|
||||
|
||||
[ForeignKey("IngredientId")]
|
||||
public List<ProductIngredient>? ProductIngredients { get; set; }
|
||||
|
||||
[ForeignKey("IngredienId")]
|
||||
public List<IngredientHistory>? IngredientHistory { get; set; }
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SladkieBulkiDatabase.Models;
|
||||
|
||||
internal class IngredientHistory
|
||||
{
|
||||
public string Id { get; set; }
|
||||
public required string IngredienId { get; set; }
|
||||
public double OldPrice { get; set; }
|
||||
public DateTime ChangeDate { get; set; }
|
||||
public Ingredient Ingredient { get; set; }
|
||||
}
|
||||
19
SladkieBulki/SladkieBulkiDatabase/Models/Post.cs
Normal file
19
SladkieBulki/SladkieBulkiDatabase/Models/Post.cs
Normal file
@@ -0,0 +1,19 @@
|
||||
using SladkieBulkiContrakts.Enums;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SladkieBulkiDatabase.Models;
|
||||
|
||||
internal class Post
|
||||
{
|
||||
public required string Id { get; set; } = Guid.NewGuid().ToString();
|
||||
public required string PostId { get; set; }
|
||||
public required string PostName { get; set; }
|
||||
public PostType PostType { get; set; }
|
||||
public double Salary { get; set; }
|
||||
public bool IsActual { get; set; }
|
||||
public DateTime ChangeDate { get; set; }
|
||||
}
|
||||
30
SladkieBulki/SladkieBulkiDatabase/Models/Product.cs
Normal file
30
SladkieBulki/SladkieBulkiDatabase/Models/Product.cs
Normal file
@@ -0,0 +1,30 @@
|
||||
using SladkieBulkiContrakts.DataModels;
|
||||
using SladkieBulkiContrakts.Enums;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Xml.Linq;
|
||||
|
||||
namespace SladkieBulkiDatabase.Models;
|
||||
|
||||
internal class Product
|
||||
{
|
||||
public required string Id { get; set; }
|
||||
public required string Name { get; set; }
|
||||
public required string Description { get; set; }
|
||||
public ProductType ProductType { get; set; }
|
||||
public int UnitPrice { get; set; }
|
||||
public bool IsDeleted { get; set; }
|
||||
|
||||
[ForeignKey("ProductId")]
|
||||
public List<ProductIngredient>? ProductIngredients { get; set; }
|
||||
|
||||
[ForeignKey("ProductId")]
|
||||
public List<Production>? Productions { get; set; }
|
||||
|
||||
[ForeignKey("ProductId")]
|
||||
public List<ProductHistory>? ProductHistories { get; set; }
|
||||
}
|
||||
16
SladkieBulki/SladkieBulkiDatabase/Models/ProductHistory.cs
Normal file
16
SladkieBulki/SladkieBulkiDatabase/Models/ProductHistory.cs
Normal file
@@ -0,0 +1,16 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SladkieBulkiDatabase.Models;
|
||||
|
||||
internal class ProductHistory
|
||||
{
|
||||
public string Id { get; set; } = Guid.NewGuid().ToString();
|
||||
public required string ProductId { get; set; }
|
||||
public double OldPrice { get; set; }
|
||||
public DateTime ChangeDate { get; set; } = DateTime.UtcNow;
|
||||
public Product? Product { get; set; }
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SladkieBulkiDatabase.Models;
|
||||
|
||||
internal class ProductIngredient
|
||||
{
|
||||
public required string ProductId { get; set; }
|
||||
public required string IngredientId { get; set; }
|
||||
public int Count { get; set; }
|
||||
public Ingredient? Ingredient { get; set; }
|
||||
public Product? Product { get; set; }
|
||||
}
|
||||
22
SladkieBulki/SladkieBulkiDatabase/Models/Production.cs
Normal file
22
SladkieBulki/SladkieBulkiDatabase/Models/Production.cs
Normal file
@@ -0,0 +1,22 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SladkieBulkiDatabase.Models;
|
||||
|
||||
internal class Production
|
||||
{
|
||||
public string Id { get; set; } = Guid.NewGuid().ToString();
|
||||
public DateTime ProductionDate { get; set; }
|
||||
public int Count { get; set; }
|
||||
public double Sum { get; set; }
|
||||
public required string WorkerId { get; set; }
|
||||
public required string ProductId { get; set; }
|
||||
public Worker? Worker { get; set; }
|
||||
public Product? Product { get; set; }
|
||||
|
||||
|
||||
}
|
||||
19
SladkieBulki/SladkieBulkiDatabase/Models/Salary.cs
Normal file
19
SladkieBulki/SladkieBulkiDatabase/Models/Salary.cs
Normal file
@@ -0,0 +1,19 @@
|
||||
using AutoMapper;
|
||||
using SladkieBulkiContrakts.DataModels;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SladkieBulkiDatabase.Models;
|
||||
|
||||
[AutoMap(typeof(SalaryDataModel), ReverseMap = true)]
|
||||
internal class Salary
|
||||
{
|
||||
public string Id { get; set; } = Guid.NewGuid().ToString();
|
||||
public required string WorkerId { get; set; }
|
||||
public DateTime SalaryDate { get; set; }
|
||||
public double WorkerSalary { get; set; }
|
||||
public Worker? Worker { get; set; }
|
||||
}
|
||||
28
SladkieBulki/SladkieBulkiDatabase/Models/Worker.cs
Normal file
28
SladkieBulki/SladkieBulkiDatabase/Models/Worker.cs
Normal file
@@ -0,0 +1,28 @@
|
||||
using AutoMapper;
|
||||
using SladkieBulkiContrakts.DataModels;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SladkieBulkiDatabase.Models;
|
||||
|
||||
[AutoMap(typeof(WorkerDataModel), ReverseMap = true)]
|
||||
internal class Worker
|
||||
{
|
||||
public required string Id { get; set; }
|
||||
public required string FIO { get; set; }
|
||||
public string PostId { get; set; }
|
||||
public DateTime BirthDate { get; set; }
|
||||
public DateTime EmploymentDate { get; set; }
|
||||
public bool IsDeleted { get; set; }
|
||||
public required string Email { get; set; }
|
||||
|
||||
[ForeignKey("WorkerId")]
|
||||
public List<Salary>? Salaries { get; set; }
|
||||
|
||||
[ForeignKey("WorkerId")]
|
||||
public List<Production>? Productions { get; set; }
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="AutoMapper" Version="14.0.0" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="9.0.4" />
|
||||
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="9.0.4" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\SladkieBulkiContrakts\SladkieBulkiContrakts.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<InternalsVisibleTo Include="SladkieBulkiTests" />
|
||||
<InternalsVisibleTo Include="DynamicProxyGenAssembly2" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
53
SladkieBulki/SladkieBulkiDatabase/SladkieBulkiDbContext.cs
Normal file
53
SladkieBulki/SladkieBulkiDatabase/SladkieBulkiDbContext.cs
Normal file
@@ -0,0 +1,53 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using SladkieBulkiContrakts.Infrastructure;
|
||||
using SladkieBulkiDatabase.Models;
|
||||
using System;
|
||||
|
||||
|
||||
namespace SladkieBulkiDatabase;
|
||||
|
||||
internal class SladkieBulkiDbContext(IConfigurationDatabase configurationDatabase): DbContext
|
||||
{
|
||||
private readonly IConfigurationDatabase? _configurationDatabase = configurationDatabase;
|
||||
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
|
||||
{
|
||||
optionsBuilder.UseNpgsql(_configurationDatabase?.ConnectionString, o => o.SetPostgresVersion(12, 2));
|
||||
base.OnConfiguring(optionsBuilder);
|
||||
}
|
||||
|
||||
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
||||
{
|
||||
base.OnModelCreating(modelBuilder);
|
||||
|
||||
modelBuilder.Entity<Ingredient>().HasIndex(x => x.NameIngredients).IsUnique();
|
||||
|
||||
modelBuilder.Entity<Post>()
|
||||
.HasIndex(e => new { e.PostName, e.IsActual })
|
||||
.IsUnique()
|
||||
.HasFilter($"\"{nameof(Post.IsActual)}\" = TRUE");
|
||||
|
||||
modelBuilder.Entity<Post>()
|
||||
.HasIndex(e => new { e.PostId, e.IsActual })
|
||||
.IsUnique()
|
||||
.HasFilter($"\"{nameof(Post.IsActual)}\" = TRUE");
|
||||
|
||||
modelBuilder.Entity<Product>()
|
||||
.HasIndex(x => new { x.Name, x.IsDeleted })
|
||||
.IsUnique()
|
||||
.HasFilter($"\"{nameof(Product.IsDeleted)}\" = FALSE");
|
||||
|
||||
modelBuilder.Entity<ProductIngredient>().HasKey(x => new { x.ProductId, x.IngredientId });
|
||||
}
|
||||
|
||||
|
||||
public DbSet<Ingredient> Ingredients { get; set; }
|
||||
public DbSet<IngredientHistory> IngredientHistories { get; set; }
|
||||
public DbSet<Post> Posts { get; set; }
|
||||
public DbSet<Product> Products { get; set; }
|
||||
public DbSet<ProductHistory> ProductHistories { get; set; }
|
||||
public DbSet<Salary> Salaries { get; set; }
|
||||
public DbSet<Production> Productions { get; set; }
|
||||
public DbSet<ProductIngredient> ProductIngredients { get; set; }
|
||||
public DbSet<Worker> Workers { get; set; }
|
||||
|
||||
}
|
||||
@@ -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.NameIngredients, 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.NameIngredients == record.NameIngredients &&
|
||||
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.NameIngredients == record.NameIngredients &&
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -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.WorkerSalary;
|
||||
});
|
||||
//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.WorkerSalary;
|
||||
});
|
||||
//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>());
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -71,12 +71,12 @@ internal class IngredientDataModelTests
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(ingredient.Id, Is.EqualTo(ingredientId));
|
||||
Assert.That(ingredient.NameIngridients, Is.EqualTo(ingredientName));
|
||||
Assert.That(ingredient.NameIngredients, Is.EqualTo(ingredientName));
|
||||
Assert.That(ingredient.SizeInit, Is.EqualTo(sizeInit));
|
||||
Assert.That(ingredient.InitPrice, Is.EqualTo(initPrice));
|
||||
});
|
||||
}
|
||||
|
||||
private static IngredientDataModel CreateDataModel(string? id, string? nameIngridients, string? sizeInit, double initPrice) =>
|
||||
new(id, nameIngridients, sizeInit, initPrice);
|
||||
private static IngredientDataModel CreateDataModel(string? id, string? NameIngredients, string? sizeInit, double initPrice) =>
|
||||
new(id, NameIngredients, sizeInit, initPrice);
|
||||
}
|
||||
|
||||
@@ -12,70 +12,68 @@ namespace SladkieBulkiTests.DataModelsTests;
|
||||
internal class PostDataModelTests
|
||||
{
|
||||
[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>());
|
||||
|
||||
post = CreateDataModel(string.Empty, "name", PostType.Preform, 100);
|
||||
post = CreateDataModel(string.Empty, "name", PostType.Preform, 10, true, DateTime.UtcNow);
|
||||
Assert.That(() => post.Validate(), Throws.TypeOf<ValidationException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
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>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void PostNameIsNullOrEmptyTest()
|
||||
public void PostNameIsEmptyTest()
|
||||
{
|
||||
var post = CreateDataModel(Guid.NewGuid().ToString(), null, PostType.Preform, 100);
|
||||
Assert.That(() => post.Validate(), Throws.TypeOf<ValidationException>());
|
||||
|
||||
post = CreateDataModel(Guid.NewGuid().ToString(), string.Empty, PostType.Preform, 100);
|
||||
Assert.That(() => post.Validate(), Throws.TypeOf<ValidationException>());
|
||||
var manufacturer = CreateDataModel(Guid.NewGuid().ToString(), null, PostType.Preform, 10, true, DateTime.UtcNow);
|
||||
Assert.That(() => manufacturer.Validate(), Throws.TypeOf<ValidationException>());
|
||||
manufacturer = CreateDataModel(Guid.NewGuid().ToString(), string.Empty, PostType.Preform, 10, true, DateTime.UtcNow);
|
||||
Assert.That(() => manufacturer.Validate(), Throws.TypeOf<ValidationException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
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>());
|
||||
}
|
||||
|
||||
[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>());
|
||||
|
||||
post = CreateDataModel(Guid.NewGuid().ToString(), "name", PostType.Preform, -100);
|
||||
post = CreateDataModel(Guid.NewGuid().ToString(), "name", PostType.Preform, -10, true, DateTime.UtcNow);
|
||||
Assert.That(() => post.Validate(), Throws.TypeOf<ValidationException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void AllFieldsAreCorrectTest()
|
||||
public void AllFieldsIsCorrectTest()
|
||||
{
|
||||
var postId = Guid.NewGuid().ToString();
|
||||
var postName = "Alan Worker";
|
||||
var postName = "name";
|
||||
var postType = PostType.Preform;
|
||||
var salary = 100;
|
||||
|
||||
var post = CreateDataModel(postId, postName, postType, salary);
|
||||
var salary = 10;
|
||||
var isActual = false;
|
||||
var changeDate = DateTime.UtcNow.AddDays(-1);
|
||||
var post = CreateDataModel(postId, postName, postType, salary, isActual, changeDate);
|
||||
Assert.That(() => post.Validate(), Throws.Nothing);
|
||||
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(post.Id, Is.EqualTo(postId));
|
||||
Assert.That(post.PostName, Is.EqualTo(postName));
|
||||
Assert.That(post.PostType, Is.EqualTo(postType));
|
||||
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) =>
|
||||
new(id, postName, postType, salary);
|
||||
private static PostDataModel CreateDataModel(string? id, string? postName, PostType postType, double salary, bool isActual, DateTime changeDate) =>
|
||||
new(id, postName, postType, salary, isActual, changeDate);
|
||||
}
|
||||
|
||||
|
||||
@@ -14,54 +14,54 @@ internal class ProductDataModelTests
|
||||
[Test]
|
||||
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>());
|
||||
|
||||
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>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
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>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
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>());
|
||||
|
||||
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>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
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>());
|
||||
|
||||
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>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
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>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
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>());
|
||||
|
||||
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>());
|
||||
}
|
||||
|
||||
@@ -73,8 +73,14 @@ internal class ProductDataModelTests
|
||||
var productDescription = "Description";
|
||||
var productType = ProductType.Production;
|
||||
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.Multiple(() =>
|
||||
@@ -84,9 +90,15 @@ internal class ProductDataModelTests
|
||||
Assert.That(product.Description, Is.EqualTo(productDescription));
|
||||
Assert.That(product.ProductType, Is.EqualTo(productType));
|
||||
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) };
|
||||
|
||||
}
|
||||
@@ -13,92 +13,85 @@ internal class ProductionDataModelTests
|
||||
[Test]
|
||||
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>());
|
||||
|
||||
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>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
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>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
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>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
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>());
|
||||
|
||||
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>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
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>());
|
||||
|
||||
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>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
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>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
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>());
|
||||
|
||||
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>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
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>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
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>());
|
||||
|
||||
production = CreateDataModel(Guid.NewGuid().ToString(), DateTime.Now, 5, 0, Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), new List<ProductIngredientDataModel>());
|
||||
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>());
|
||||
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 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.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);
|
||||
}
|
||||
}
|
||||
@@ -51,7 +51,7 @@ internal class SalaryDataModelTests
|
||||
{
|
||||
Assert.That(salary.WorkerId, Is.EqualTo(workerId));
|
||||
Assert.That(salary.SalaryDate, Is.EqualTo(salaryDate));
|
||||
Assert.That(salary.Salary, Is.EqualTo(workerSalary));
|
||||
Assert.That(salary.WorkerSalary, Is.EqualTo(workerSalary));
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
using SladkieBulkiContrakts.Infrastructure;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SladkieBulkiTests.Infrastructure;
|
||||
internal class ConfigurationDatabaseTest : IConfigurationDatabase
|
||||
{
|
||||
public string ConnectionString =>
|
||||
"Host=localhost;Port=5432;Database=rpp;Username=postgres;Password=postgres";
|
||||
}
|
||||
@@ -12,13 +12,16 @@
|
||||
<ItemGroup>
|
||||
<PackageReference Include="coverlet.collector" Version="6.0.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.Analyzers" Version="3.9.0" />
|
||||
<PackageReference Include="NUnit3TestAdapter" Version="4.5.0" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\SladkieBulkiBusinessLogic\SladkieBulkiBusinessLogic.csproj" />
|
||||
<ProjectReference Include="..\SladkieBulkiContrakts\SladkieBulkiContrakts.csproj" />
|
||||
<ProjectReference Include="..\SladkieBulkiDatabase\SladkieBulkiDatabase.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
using SladkieBulkiDatabase;
|
||||
using SladkieBulkiTests.Infrastructure;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SladkieBulkiTests.StoragesContracts;
|
||||
|
||||
internal abstract class BaseStorageContractTest
|
||||
{
|
||||
protected SladkieBulkiDbContext SladkieBulkiDbContext { get; private set; }
|
||||
|
||||
[OneTimeSetUp]
|
||||
public void OneTimeSetUp()
|
||||
{
|
||||
SladkieBulkiDbContext = new SladkieBulkiDbContext(new ConfigurationDatabaseTest());
|
||||
|
||||
SladkieBulkiDbContext.Database.EnsureDeleted();
|
||||
SladkieBulkiDbContext.Database.EnsureCreated();
|
||||
}
|
||||
|
||||
[OneTimeTearDown]
|
||||
public void OneTimeTearDown()
|
||||
{
|
||||
SladkieBulkiDbContext.Database.EnsureDeleted();
|
||||
SladkieBulkiDbContext.Dispose();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,145 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using SladkieBulkiContrakts.DataModels;
|
||||
using SladkieBulkiContrakts.Exceptions;
|
||||
using SladkieBulkiDatabase.Implementations;
|
||||
using SladkieBulkiDatabase.Models;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SladkieBulkiTests.StoragesContracts;
|
||||
|
||||
[TestFixture]
|
||||
internal class IngredientStorageContractTests : BaseStorageContractTest
|
||||
{
|
||||
private IngredientStorageContract _ingredientStorageContract;
|
||||
|
||||
[SetUp]
|
||||
public void SetUp()
|
||||
{
|
||||
_ingredientStorageContract = new IngredientStorageContract(SladkieBulkiDbContext);
|
||||
}
|
||||
|
||||
[TearDown]
|
||||
public void TearDown()
|
||||
{
|
||||
SladkieBulkiDbContext.Database.ExecuteSqlRaw("TRUNCATE \"ProductIngredients\" CASCADE;");
|
||||
SladkieBulkiDbContext.Database.ExecuteSqlRaw("TRUNCATE \"Ingredients\" CASCADE;");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_GetList_WhenHaveRecords_Test()
|
||||
{
|
||||
var ingr = InsertToDbAndReturn(Guid.NewGuid().ToString(), "Мука", "кг", 25.5);
|
||||
InsertToDbAndReturn(Guid.NewGuid().ToString(), "Сахар", "кг", 30);
|
||||
InsertToDbAndReturn(Guid.NewGuid().ToString(), "Масло", "л", 100);
|
||||
|
||||
var list = _ingredientStorageContract.GetList();
|
||||
Assert.That(list, Has.Count.EqualTo(3));
|
||||
AssertElement(list.First(x => x.Id == ingr.Id), ingr);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_GetList_WhenNoRecords_Test()
|
||||
{
|
||||
var list = _ingredientStorageContract.GetList();
|
||||
Assert.That(list, Is.Empty);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_GetElementById_WhenExists_Test()
|
||||
{
|
||||
var ingr = InsertToDbAndReturn(Guid.NewGuid().ToString(), "Дрожжи", "г", 5);
|
||||
var element = _ingredientStorageContract.GetElementById(ingr.Id);
|
||||
AssertElement(element, ingr);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_GetElementById_WhenNotExists_Test()
|
||||
{
|
||||
var result = _ingredientStorageContract.GetElementById(Guid.NewGuid().ToString());
|
||||
Assert.That(result, Is.Null);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_AddElement_Test()
|
||||
{
|
||||
var model = CreateModel(Guid.NewGuid().ToString(), "Соль", "г", 1.5);
|
||||
_ingredientStorageContract.AddElement(model);
|
||||
AssertElement(GetFromDb(model.Id), model);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_AddElement_WhenDuplicateId_Throws_Test()
|
||||
{
|
||||
var id = Guid.NewGuid().ToString();
|
||||
InsertToDbAndReturn(id, "Молоко", "л", 50);
|
||||
var model = CreateModel(id, "Молоко", "л", 50);
|
||||
Assert.That(() => _ingredientStorageContract.AddElement(model), Throws.TypeOf<ElementExistsException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_UpdElement_Test()
|
||||
{
|
||||
var id = Guid.NewGuid().ToString();
|
||||
InsertToDbAndReturn(id, "Мёд", "л", 200);
|
||||
var model = CreateModel(id, "Мёд цветочный", "л", 250);
|
||||
_ingredientStorageContract.UpdElement(model);
|
||||
AssertElement(GetFromDb(id), model);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_UpdElement_WhenNotExists_Throws_Test()
|
||||
{
|
||||
var model = CreateModel(Guid.NewGuid().ToString(), "Имбирь", "г", 10);
|
||||
Assert.That(() => _ingredientStorageContract.UpdElement(model), Throws.TypeOf<ElementNotFoundException>());
|
||||
}
|
||||
|
||||
|
||||
private static IngredientDataModel CreateModel(string id, string nameIngredients, string sizeInit, double initPrice)
|
||||
=> new(id, nameIngredients, sizeInit, initPrice);
|
||||
|
||||
private Ingredient InsertToDbAndReturn(string id, string nameIngredients, string sizeInit, double initPrice)
|
||||
{
|
||||
var ingr = new Ingredient
|
||||
{
|
||||
Id = id,
|
||||
NameIngredients = nameIngredients,
|
||||
SizeInit = sizeInit,
|
||||
InitPrice = initPrice
|
||||
};
|
||||
SladkieBulkiDbContext.Ingredients.Add(ingr);
|
||||
SladkieBulkiDbContext.SaveChanges();
|
||||
return ingr;
|
||||
}
|
||||
|
||||
private Ingredient? GetFromDb(string id)
|
||||
=> SladkieBulkiDbContext.Ingredients.FirstOrDefault(x => x.Id == id);
|
||||
|
||||
private static void AssertElement(IngredientDataModel? actual, Ingredient expected)
|
||||
{
|
||||
Assert.That(actual, Is.Not.Null);
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(actual.Id, Is.EqualTo(expected.Id));
|
||||
Assert.That(actual.NameIngredients, Is.EqualTo(expected.NameIngredients));
|
||||
Assert.That(actual.SizeInit, Is.EqualTo(expected.SizeInit));
|
||||
Assert.That(actual.InitPrice, Is.EqualTo(expected.InitPrice).Within(0.0001));
|
||||
});
|
||||
}
|
||||
|
||||
private static void AssertElement(Ingredient? actual, IngredientDataModel expected)
|
||||
{
|
||||
Assert.That(actual, Is.Not.Null);
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(actual.Id, Is.EqualTo(expected.Id));
|
||||
Assert.That(actual.NameIngredients, Is.EqualTo(expected.NameIngredients));
|
||||
Assert.That(actual.SizeInit, Is.EqualTo(expected.SizeInit));
|
||||
Assert.That(actual.InitPrice, Is.EqualTo(expected.InitPrice).Within(0.0001));
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,318 @@
|
||||
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using SladkieBulkiContrakts.DataModels;
|
||||
using SladkieBulkiContrakts.Enums;
|
||||
using SladkieBulkiContrakts.Exceptions;
|
||||
using SladkieBulkiDatabase.Implementations;
|
||||
using SladkieBulkiDatabase.Models;
|
||||
|
||||
namespace SladkieBulkiTests.StoragesContracts;
|
||||
|
||||
[TestFixture]
|
||||
internal class PostStorageContractTests : BaseStorageContractTest
|
||||
{
|
||||
private PostStorageContract _postStorageContract;
|
||||
|
||||
[SetUp]
|
||||
public void SetUp()
|
||||
{
|
||||
_postStorageContract = new PostStorageContract(SladkieBulkiDbContext);
|
||||
}
|
||||
|
||||
[TearDown]
|
||||
public void TearDown()
|
||||
{
|
||||
SladkieBulkiDbContext.Database.ExecuteSqlRaw("TRUNCATE \"Posts\" CASCADE;");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_GetList_WhenHaveRecords_Test()
|
||||
{
|
||||
var post = InsertPostToDatabaseAndReturn(Guid.NewGuid().ToString(), "name 1");
|
||||
InsertPostToDatabaseAndReturn(Guid.NewGuid().ToString(), "name 2");
|
||||
InsertPostToDatabaseAndReturn(Guid.NewGuid().ToString(), "name 3");
|
||||
var list = _postStorageContract.GetList();
|
||||
Assert.That(list, Is.Not.Null);
|
||||
Assert.That(list, Has.Count.EqualTo(3));
|
||||
AssertElement(list.First(x => x.Id == post.PostId), post);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_GetList_WhenNoRecords_Test()
|
||||
{
|
||||
var list = _postStorageContract.GetList();
|
||||
Assert.That(list, Is.Not.Null);
|
||||
Assert.That(list, Is.Empty);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_GetList_OnlyActual_Test()
|
||||
{
|
||||
InsertPostToDatabaseAndReturn(Guid.NewGuid().ToString(), "name 1", isActual: true);
|
||||
InsertPostToDatabaseAndReturn(Guid.NewGuid().ToString(), "name 2", isActual: true);
|
||||
InsertPostToDatabaseAndReturn(Guid.NewGuid().ToString(), "name 3", isActual: false);
|
||||
var list = _postStorageContract.GetList(onlyActual: true);
|
||||
Assert.That(list, Is.Not.Null);
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(list, Has.Count.EqualTo(2));
|
||||
Assert.That(!list.Any(x => !x.IsActual));
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_GetList_IncludeNoActual_Test()
|
||||
{
|
||||
InsertPostToDatabaseAndReturn(Guid.NewGuid().ToString(), "name 1", isActual: true);
|
||||
InsertPostToDatabaseAndReturn(Guid.NewGuid().ToString(), "name 2", isActual: true);
|
||||
InsertPostToDatabaseAndReturn(Guid.NewGuid().ToString(), "name 3", isActual: false);
|
||||
var list = _postStorageContract.GetList(onlyActual: false);
|
||||
Assert.That(list, Is.Not.Null);
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(list, Has.Count.EqualTo(3));
|
||||
Assert.That(list.Count(x => x.IsActual), Is.EqualTo(2));
|
||||
Assert.That(list.Count(x => !x.IsActual), Is.EqualTo(1));
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_GetPostWithHistory_WhenHaveRecords_Test()
|
||||
{
|
||||
var postId = Guid.NewGuid().ToString();
|
||||
InsertPostToDatabaseAndReturn(Guid.NewGuid().ToString(), "name 1", isActual: true);
|
||||
InsertPostToDatabaseAndReturn(postId, "name 2", isActual: true);
|
||||
InsertPostToDatabaseAndReturn(postId, "name 2", isActual: false);
|
||||
var list = _postStorageContract.GetPostWithHistory(postId);
|
||||
Assert.That(list, Is.Not.Null);
|
||||
Assert.That(list, Has.Count.EqualTo(2));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_GetPostWithHistory_WhenNoRecords_Test()
|
||||
{
|
||||
var postId = Guid.NewGuid().ToString();
|
||||
InsertPostToDatabaseAndReturn(Guid.NewGuid().ToString(), "name 1", isActual: true);
|
||||
InsertPostToDatabaseAndReturn(postId, "name 2", isActual: true);
|
||||
InsertPostToDatabaseAndReturn(postId, "name 2", isActual: false);
|
||||
var list = _postStorageContract.GetPostWithHistory(Guid.NewGuid().ToString());
|
||||
Assert.That(list, Is.Not.Null);
|
||||
Assert.That(list, Has.Count.EqualTo(0));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_GetElementById_WhenHaveRecord_Test()
|
||||
{
|
||||
var post = InsertPostToDatabaseAndReturn(Guid.NewGuid().ToString());
|
||||
AssertElement(_postStorageContract.GetElementById(post.PostId), post);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_GetElementById_WhenNoRecord_Test()
|
||||
{
|
||||
InsertPostToDatabaseAndReturn(Guid.NewGuid().ToString());
|
||||
Assert.That(() => _postStorageContract.GetElementById(Guid.NewGuid().ToString()), Is.Null);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_GetElementById_WhenRecordHasDeleted_Test()
|
||||
{
|
||||
var post = InsertPostToDatabaseAndReturn(Guid.NewGuid().ToString(), isActual: false);
|
||||
Assert.That(() => _postStorageContract.GetElementById(post.PostId), Is.Null);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_GetElementById_WhenTrySearchById_Test()
|
||||
{
|
||||
var post = InsertPostToDatabaseAndReturn(Guid.NewGuid().ToString());
|
||||
Assert.That(() => _postStorageContract.GetElementById(post.Id), Is.Null);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_GetElementByName_WhenHaveRecord_Test()
|
||||
{
|
||||
var post = InsertPostToDatabaseAndReturn(Guid.NewGuid().ToString());
|
||||
AssertElement(_postStorageContract.GetElementByName(post.PostName), post);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_GetElementByName_WhenNoRecord_Test()
|
||||
{
|
||||
InsertPostToDatabaseAndReturn(Guid.NewGuid().ToString());
|
||||
Assert.That(() => _postStorageContract.GetElementByName("name"), Is.Null);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_GetElementByName_WhenRecordHasDeleted_Test()
|
||||
{
|
||||
var post = InsertPostToDatabaseAndReturn(Guid.NewGuid().ToString(), isActual: false);
|
||||
Assert.That(() => _postStorageContract.GetElementById(post.PostName), Is.Null);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_AddElement_Test()
|
||||
{
|
||||
var post = CreateModel(Guid.NewGuid().ToString(), isActual: true);
|
||||
_postStorageContract.AddElement(post);
|
||||
AssertElement(GetPostFromDatabaseByPostId(post.Id), post);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_AddElement_WhenActualIsFalse_Test()
|
||||
{
|
||||
var post = CreateModel(Guid.NewGuid().ToString(), isActual: false);
|
||||
Assert.That(() => _postStorageContract.AddElement(post), Throws.Nothing);
|
||||
AssertElement(GetPostFromDatabaseByPostId(post.Id), CreateModel(post.Id, isActual: true));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_AddElement_WhenHaveRecordWithSameName_Test()
|
||||
{
|
||||
var post = CreateModel(Guid.NewGuid().ToString(), "name unique", isActual: true);
|
||||
InsertPostToDatabaseAndReturn(Guid.NewGuid().ToString(), postName: post.PostName, isActual: true);
|
||||
Assert.That(() => _postStorageContract.AddElement(post), Throws.TypeOf<ElementExistsException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_AddElement_WhenHaveRecordWithSamePostIdAndActualIsTrue_Test()
|
||||
{
|
||||
var post = CreateModel(Guid.NewGuid().ToString(), isActual: true);
|
||||
InsertPostToDatabaseAndReturn(post.Id, isActual: true);
|
||||
Assert.That(() => _postStorageContract.AddElement(post), Throws.TypeOf<ElementExistsException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_UpdElement_Test()
|
||||
{
|
||||
var post = CreateModel(Guid.NewGuid().ToString());
|
||||
InsertPostToDatabaseAndReturn(post.Id, isActual: true);
|
||||
_postStorageContract.UpdElement(post);
|
||||
var posts = SladkieBulkiDbContext.Posts.Where(x => x.PostId == post.Id).OrderByDescending(x => x.ChangeDate);
|
||||
Assert.That(posts.Count(), Is.EqualTo(2));
|
||||
AssertElement(posts.First(), CreateModel(post.Id, isActual: true));
|
||||
AssertElement(posts.Last(), CreateModel(post.Id, isActual: false));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_UpdElement_WhenActualIsFalse_Test()
|
||||
{
|
||||
var post = CreateModel(Guid.NewGuid().ToString(), isActual: false);
|
||||
InsertPostToDatabaseAndReturn(post.Id, isActual: true);
|
||||
_postStorageContract.UpdElement(post);
|
||||
AssertElement(GetPostFromDatabaseByPostId(post.Id), CreateModel(post.Id, isActual: true));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_UpdElement_WhenNoRecordWithThisId_Test()
|
||||
{
|
||||
Assert.That(() => _postStorageContract.UpdElement(CreateModel(Guid.NewGuid().ToString())), Throws.TypeOf<ElementNotFoundException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_UpdElement_WhenHaveRecordWithSameName_Test()
|
||||
{
|
||||
var post = CreateModel(Guid.NewGuid().ToString(), "New Name");
|
||||
InsertPostToDatabaseAndReturn(post.Id, postName: "name");
|
||||
InsertPostToDatabaseAndReturn(Guid.NewGuid().ToString(), postName: post.PostName);
|
||||
Assert.That(() => _postStorageContract.UpdElement(post), Throws.TypeOf<ElementExistsException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_UpdElement_WhenRecordWasDeleted_Test()
|
||||
{
|
||||
var post = CreateModel(Guid.NewGuid().ToString());
|
||||
InsertPostToDatabaseAndReturn(post.Id, isActual: false);
|
||||
Assert.That(() => _postStorageContract.UpdElement(post), Throws.TypeOf<ElementDeletedException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_DelElement_Test()
|
||||
{
|
||||
var post = InsertPostToDatabaseAndReturn(Guid.NewGuid().ToString(), isActual: true);
|
||||
_postStorageContract.DelElement(post.PostId);
|
||||
var element = GetPostFromDatabaseByPostId(post.PostId);
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(element, Is.Not.Null);
|
||||
Assert.That(!element!.IsActual);
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_DelElement_WhenNoRecordWithThisId_Test()
|
||||
{
|
||||
Assert.That(() => _postStorageContract.DelElement(Guid.NewGuid().ToString()), Throws.TypeOf<ElementNotFoundException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_DelElement_WhenRecordWasDeleted_Test()
|
||||
{
|
||||
var post = InsertPostToDatabaseAndReturn(Guid.NewGuid().ToString(), isActual: false);
|
||||
Assert.That(() => _postStorageContract.DelElement(post.PostId), Throws.TypeOf<ElementDeletedException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_ResElement_Test()
|
||||
{
|
||||
var post = InsertPostToDatabaseAndReturn(Guid.NewGuid().ToString(), isActual: false);
|
||||
_postStorageContract.ResElement(post.PostId);
|
||||
var element = GetPostFromDatabaseByPostId(post.PostId);
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(element, Is.Not.Null);
|
||||
Assert.That(element!.IsActual);
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_ResElement_WhenNoRecordWithThisId_Test()
|
||||
{
|
||||
Assert.That(() => _postStorageContract.ResElement(Guid.NewGuid().ToString()), Throws.TypeOf<ElementNotFoundException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_ResElement_WhenRecordNotWasDeleted_Test()
|
||||
{
|
||||
var post = InsertPostToDatabaseAndReturn(Guid.NewGuid().ToString(), isActual: true);
|
||||
Assert.That(() => _postStorageContract.ResElement(post.PostId), Throws.Nothing);
|
||||
}
|
||||
|
||||
private Post InsertPostToDatabaseAndReturn(string id, string postName = "test", PostType postType = PostType.Preform, double salary = 10, bool isActual = true, DateTime? changeDate = null)
|
||||
{
|
||||
var post = new Post() { Id = Guid.NewGuid().ToString(), PostId = id, PostName = postName, PostType = postType, Salary = salary, IsActual = isActual, ChangeDate = changeDate ?? DateTime.UtcNow };
|
||||
SladkieBulkiDbContext.Posts.Add(post);
|
||||
SladkieBulkiDbContext.SaveChanges();
|
||||
return post;
|
||||
}
|
||||
|
||||
private static void AssertElement(PostDataModel? actual, Post expected)
|
||||
{
|
||||
Assert.That(actual, Is.Not.Null);
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(actual.Id, Is.EqualTo(expected.PostId));
|
||||
Assert.That(actual.PostName, Is.EqualTo(expected.PostName));
|
||||
Assert.That(actual.PostType, Is.EqualTo(expected.PostType));
|
||||
Assert.That(actual.Salary, Is.EqualTo(expected.Salary));
|
||||
Assert.That(actual.IsActual, Is.EqualTo(expected.IsActual));
|
||||
});
|
||||
}
|
||||
|
||||
private static PostDataModel CreateModel(string postId, string postName = "test", PostType postType = PostType.Preform, double salary = 10, bool isActual = false, DateTime? changeDate = null)
|
||||
=> new(postId, postName, postType, salary, isActual, changeDate ?? DateTime.UtcNow);
|
||||
|
||||
private Post? GetPostFromDatabaseByPostId(string id) => SladkieBulkiDbContext.Posts.Where(x => x.PostId == id).OrderByDescending(x => x.ChangeDate).FirstOrDefault();
|
||||
|
||||
private static void AssertElement(Post? actual, PostDataModel expected)
|
||||
{
|
||||
Assert.That(actual, Is.Not.Null);
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(actual.PostId, Is.EqualTo(expected.Id));
|
||||
Assert.That(actual.PostName, Is.EqualTo(expected.PostName));
|
||||
Assert.That(actual.PostType, Is.EqualTo(expected.PostType));
|
||||
Assert.That(actual.Salary, Is.EqualTo(expected.Salary));
|
||||
Assert.That(actual.IsActual, Is.EqualTo(expected.IsActual));
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,400 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using SladkieBulkiContrakts.DataModels;
|
||||
using SladkieBulkiContrakts.Enums;
|
||||
using SladkieBulkiContrakts.Exceptions;
|
||||
using SladkieBulkiDatabase.Implementations;
|
||||
using SladkieBulkiDatabase.Models;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SladkieBulkiTests.StoragesContracts;
|
||||
|
||||
|
||||
[TestFixture]
|
||||
internal class ProductStorageContractTests : BaseStorageContractTest
|
||||
{
|
||||
private ProductStorageContract _productStorageContract;
|
||||
private Ingredient _ingredient;
|
||||
|
||||
[SetUp]
|
||||
public void SetUp()
|
||||
{
|
||||
_productStorageContract = new ProductStorageContract(SladkieBulkiDbContext);
|
||||
_ingredient = InsertIngredientToDatabaseAndReturn();
|
||||
}
|
||||
|
||||
[TearDown]
|
||||
public void TearDown()
|
||||
{
|
||||
SladkieBulkiDbContext.Database.ExecuteSqlRaw("TRUNCATE \"Products\" CASCADE;");
|
||||
SladkieBulkiDbContext.Database.ExecuteSqlRaw("TRUNCATE \"ProductIngredients\" CASCADE;");
|
||||
SladkieBulkiDbContext.Database.ExecuteSqlRaw("TRUNCATE \"Ingredients\" CASCADE;");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_GetList_WhenHaveRecords_Test()
|
||||
{
|
||||
var product = InsertProductToDatabaseAndReturn(Guid.NewGuid().ToString(), "name 1");
|
||||
InsertProductToDatabaseAndReturn(Guid.NewGuid().ToString(), "name 2");
|
||||
InsertProductToDatabaseAndReturn(Guid.NewGuid().ToString(), "name 3");
|
||||
var list = _productStorageContract.GetList();
|
||||
Assert.That(list, Is.Not.Null);
|
||||
Assert.That(list, Has.Count.EqualTo(3));
|
||||
AssertElement(list.First(x => x.Id == product.Id), product);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_GetList_WhenNoRecords_Test()
|
||||
{
|
||||
var list = _productStorageContract.GetList();
|
||||
Assert.That(list, Is.Not.Null);
|
||||
Assert.That(list, Is.Empty);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_GetList_OnlyActual_Test()
|
||||
{
|
||||
InsertProductToDatabaseAndReturn(Guid.NewGuid().ToString(), "name 1", isDeleted: true);
|
||||
InsertProductToDatabaseAndReturn(Guid.NewGuid().ToString(), "name 2", isDeleted: false);
|
||||
InsertProductToDatabaseAndReturn(Guid.NewGuid().ToString(), "name 3", isDeleted: false);
|
||||
var list = _productStorageContract.GetList(onlyActive: true);
|
||||
Assert.That(list, Is.Not.Null);
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(list, Has.Count.EqualTo(2));
|
||||
Assert.That(!list.Any(x => x.IsDeleted));
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_GetList_IncludeNoActual_Test()
|
||||
{
|
||||
InsertProductToDatabaseAndReturn(Guid.NewGuid().ToString(), "name 1", isDeleted: true);
|
||||
InsertProductToDatabaseAndReturn(Guid.NewGuid().ToString(), "name 2", isDeleted: true);
|
||||
InsertProductToDatabaseAndReturn(Guid.NewGuid().ToString(), "name 3", isDeleted: false);
|
||||
var list = _productStorageContract.GetList(onlyActive: false);
|
||||
Assert.That(list, Is.Not.Null);
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(list, Has.Count.EqualTo(3));
|
||||
Assert.That(list.Count(x => x.IsDeleted), Is.EqualTo(2));
|
||||
Assert.That(list.Count(x => !x.IsDeleted), Is.EqualTo(1));
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_GetHistoryByProductId_WhenHaveRecords_Test()
|
||||
{
|
||||
var product = InsertProductToDatabaseAndReturn(Guid.NewGuid().ToString(), "name 1");
|
||||
InsertProductHistoryToDatabaseAndReturn(product.Id, 20, DateTime.UtcNow.AddDays(-1));
|
||||
InsertProductHistoryToDatabaseAndReturn(product.Id, 30, DateTime.UtcNow.AddMinutes(-10));
|
||||
InsertProductHistoryToDatabaseAndReturn(product.Id, 40, DateTime.UtcNow.AddDays(1));
|
||||
var list = _productStorageContract.GetHistoryByProductId(product.Id);
|
||||
Assert.That(list, Is.Not.Null);
|
||||
Assert.That(list, Has.Count.EqualTo(3));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_GetHistoryByProductId_WhenNoRecords_Test()
|
||||
{
|
||||
var product = InsertProductToDatabaseAndReturn(Guid.NewGuid().ToString(), "name 1");
|
||||
InsertProductHistoryToDatabaseAndReturn(product.Id, 20, DateTime.UtcNow.AddDays(-1));
|
||||
InsertProductHistoryToDatabaseAndReturn(product.Id, 30, DateTime.UtcNow.AddMinutes(-10));
|
||||
InsertProductHistoryToDatabaseAndReturn(product.Id, 40, DateTime.UtcNow.AddDays(1));
|
||||
var list = _productStorageContract.GetHistoryByProductId(Guid.NewGuid().ToString());
|
||||
Assert.That(list, Is.Not.Null);
|
||||
Assert.That(list, Has.Count.EqualTo(0));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_GetElementById_WhenHaveRecord_Test()
|
||||
{
|
||||
var product = InsertProductToDatabaseAndReturn(Guid.NewGuid().ToString());
|
||||
AssertElement(_productStorageContract.GetElementById(product.Id), product);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_GetElementById_WhenNoRecord_Test()
|
||||
{
|
||||
InsertProductToDatabaseAndReturn(Guid.NewGuid().ToString());
|
||||
Assert.That(() => _productStorageContract.GetElementById(Guid.NewGuid().ToString()), Is.Null);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_GetElementById_WhenRecordHasDeleted_Test()
|
||||
{
|
||||
var product = InsertProductToDatabaseAndReturn(Guid.NewGuid().ToString(), isDeleted: true);
|
||||
Assert.That(() => _productStorageContract.GetElementById(product.Id), Is.Null);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_GetElementByName_WhenHaveRecord_Test()
|
||||
{
|
||||
var product = InsertProductToDatabaseAndReturn(Guid.NewGuid().ToString());
|
||||
AssertElement(_productStorageContract.GetElementByName(product.Name), product);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_GetElementByName_WhenNoRecord_Test()
|
||||
{
|
||||
InsertProductToDatabaseAndReturn(Guid.NewGuid().ToString());
|
||||
Assert.That(() => _productStorageContract.GetElementByName("name"), Is.Null);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_GetElementByName_WhenRecordHasDeleted_Test()
|
||||
{
|
||||
var product = InsertProductToDatabaseAndReturn(Guid.NewGuid().ToString(), isDeleted: true);
|
||||
Assert.That(() => _productStorageContract.GetElementById(product.Name), Is.Null);
|
||||
}
|
||||
|
||||
|
||||
[Test]
|
||||
public void Try_AddElement_Test()
|
||||
{
|
||||
var product = CreateModel(Guid.NewGuid().ToString(), "test", "description", ProductType.Workpiece, [_ingredient.Id], 1, false);
|
||||
_productStorageContract.AddElement(product);
|
||||
AssertElement(GetProductFromDatabaseById(product.Id), product);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_AddElement_WhenIsDeletedIsTrue_Test()
|
||||
{
|
||||
var product = CreateModel(Guid.NewGuid().ToString(), "test", "description", ProductType.Workpiece, [_ingredient.Id], 1, true);
|
||||
Assert.That(() => _productStorageContract.AddElement(product), Throws.Nothing);
|
||||
AssertElement(GetProductFromDatabaseById(product.Id), CreateModel(product.Id, "test", "description", ProductType.Workpiece, [_ingredient.Id], 1, isDeleted: false));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_AddElement_WhenHaveRecordWithSameId_Test()
|
||||
{
|
||||
var product = CreateModel(Guid.NewGuid().ToString(), "test", "description", ProductType.Workpiece, [_ingredient.Id], 1, false);
|
||||
InsertProductToDatabaseAndReturn(product.Id, productName: "name unique");
|
||||
Assert.That(() => _productStorageContract.AddElement(product), Throws.TypeOf<ElementExistsException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_AddElement_WhenHaveRecordWithSameName_Test()
|
||||
{
|
||||
var ingredients = new List<(string ingredientId, int count)> { (_ingredient.Id, 1) };
|
||||
|
||||
var product = CreateModel(
|
||||
Guid.NewGuid().ToString(),
|
||||
"test",
|
||||
"description",
|
||||
ProductType.Workpiece,
|
||||
ingredients.Select(i => i.ingredientId).ToList(),
|
||||
1,
|
||||
false
|
||||
);
|
||||
|
||||
InsertProductToDatabaseAndReturn(
|
||||
Guid.NewGuid().ToString(),
|
||||
productName: product.Name,
|
||||
description: "description",
|
||||
productType: ProductType.Workpiece,
|
||||
unitPrice: 1,
|
||||
isDeleted: false,
|
||||
ingredients: ingredients
|
||||
);
|
||||
|
||||
Assert.That(() => _productStorageContract.AddElement(product), Throws.TypeOf<ElementExistsException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_AddElement_WhenHaveRecordWithSameNameButOneWasDeleted_Test()
|
||||
{
|
||||
var product = CreateModel(Guid.NewGuid().ToString(), "test", "description", ProductType.Workpiece, [_ingredient.Id], 1, false);
|
||||
InsertProductToDatabaseAndReturn(Guid.NewGuid().ToString(), productName: product.Name, isDeleted: true);
|
||||
Assert.That(() => _productStorageContract.AddElement(product), Throws.Nothing);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_UpdElement_Test()
|
||||
{
|
||||
var product = CreateModel(Guid.NewGuid().ToString(), "test", "description", ProductType.Workpiece, [_ingredient.Id], 1, false);
|
||||
InsertProductToDatabaseAndReturn(product.Id, isDeleted: false);
|
||||
_productStorageContract.UpdElement(product);
|
||||
AssertElement(GetProductFromDatabaseById(product.Id), product);
|
||||
}
|
||||
|
||||
|
||||
[Test]
|
||||
public void Try_UpdElement_WhenIsDeletedIsTrue_Test()
|
||||
{
|
||||
var product = CreateModel(Guid.NewGuid().ToString(), isDeleted: true);
|
||||
InsertProductToDatabaseAndReturn(product.Id, isDeleted: false);
|
||||
_productStorageContract.UpdElement(product);
|
||||
AssertElement(GetProductFromDatabaseById(product.Id), CreateModel(product.Id, isDeleted: false));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_UpdElement_WhenNoRecordWithThisId_Test()
|
||||
{
|
||||
Assert.That(() => _productStorageContract.UpdElement(CreateModel(Guid.NewGuid().ToString())), Throws.TypeOf<ElementNotFoundException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_UpdElement_WhenHaveRecordWithSameName_Test()
|
||||
{
|
||||
var product = CreateModel(Guid.NewGuid().ToString(), "name unique", isDeleted: false);
|
||||
InsertProductToDatabaseAndReturn(product.Id, productName: "name");
|
||||
InsertProductToDatabaseAndReturn(Guid.NewGuid().ToString(), productName: product.Name);
|
||||
Assert.That(() => _productStorageContract.UpdElement(product), Throws.TypeOf<ElementExistsException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_UpdElement_WhenHaveRecordWithSameNameButOneWasDeleted_Test()
|
||||
{
|
||||
var product = CreateModel(Guid.NewGuid().ToString(), "name unique", isDeleted: false);
|
||||
InsertProductToDatabaseAndReturn(product.Id, productName: "name");
|
||||
InsertProductToDatabaseAndReturn(Guid.NewGuid().ToString(), productName: product.Name, isDeleted: true);
|
||||
Assert.That(() => _productStorageContract.UpdElement(product), Throws.Nothing);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_UpdElement_WhenRecordWasDeleted_Test()
|
||||
{
|
||||
var product = CreateModel(Guid.NewGuid().ToString());
|
||||
InsertProductToDatabaseAndReturn(product.Id, isDeleted: true);
|
||||
Assert.That(() => _productStorageContract.UpdElement(product), Throws.TypeOf<ElementNotFoundException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_DelElement_Test()
|
||||
{
|
||||
var product = InsertProductToDatabaseAndReturn(Guid.NewGuid().ToString(), isDeleted: false);
|
||||
_productStorageContract.DelElement(product.Id);
|
||||
var element = GetProductFromDatabaseById(product.Id);
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(element, Is.Not.Null);
|
||||
Assert.That(element!.IsDeleted);
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_DelElement_WhenNoRecordWithThisId_Test()
|
||||
{
|
||||
Assert.That(() => _productStorageContract.DelElement(Guid.NewGuid().ToString()), Throws.TypeOf<ElementNotFoundException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_DelElement_WhenRecordWasDeleted_Test()
|
||||
{
|
||||
var product = InsertProductToDatabaseAndReturn(Guid.NewGuid().ToString(), isDeleted: true);
|
||||
Assert.That(() => _productStorageContract.DelElement(product.Id), Throws.TypeOf<ElementNotFoundException>());
|
||||
}
|
||||
|
||||
|
||||
private Product InsertProductToDatabaseAndReturn(
|
||||
string id,
|
||||
string productName = "test",
|
||||
string description = "desc 1",
|
||||
ProductType productType = ProductType.Production,
|
||||
int unitPrice = 1,
|
||||
bool isDeleted = false,
|
||||
List<(string ingredientId, int count)>? ingredients = null)
|
||||
{
|
||||
var product = new Product
|
||||
{
|
||||
Id = id,
|
||||
Name = productName,
|
||||
Description = description,
|
||||
ProductType = productType,
|
||||
UnitPrice = unitPrice,
|
||||
IsDeleted = isDeleted,
|
||||
ProductIngredients = new List<ProductIngredient>()
|
||||
};
|
||||
|
||||
if (ingredients is not null)
|
||||
{
|
||||
foreach (var (ingredientId, count) in ingredients)
|
||||
{
|
||||
product.ProductIngredients.Add(new ProductIngredient
|
||||
{
|
||||
ProductId = product.Id,
|
||||
IngredientId = ingredientId,
|
||||
Count = count
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
SladkieBulkiDbContext.Products.Add(product);
|
||||
SladkieBulkiDbContext.SaveChanges();
|
||||
return product;
|
||||
}
|
||||
|
||||
|
||||
private ProductHistory InsertProductHistoryToDatabaseAndReturn(string productId, double price, DateTime changeDate)
|
||||
{
|
||||
var productHistory = new ProductHistory() { Id = Guid.NewGuid().ToString(), ProductId = productId, OldPrice = price, ChangeDate = changeDate };
|
||||
SladkieBulkiDbContext.ProductHistories.Add(productHistory);
|
||||
SladkieBulkiDbContext.SaveChanges();
|
||||
return productHistory;
|
||||
}
|
||||
|
||||
private Ingredient InsertIngredientToDatabaseAndReturn( string nameIngredients = "sugar", string sizeInit = "kg", double initPrice = 0.1)
|
||||
{
|
||||
var ingredient = new Ingredient() { Id = Guid.NewGuid().ToString(), NameIngredients = nameIngredients, SizeInit = sizeInit, InitPrice = initPrice };
|
||||
SladkieBulkiDbContext.Ingredients.Add(ingredient);
|
||||
SladkieBulkiDbContext.SaveChanges();
|
||||
return ingredient;
|
||||
}
|
||||
|
||||
private static void AssertElement(ProductDataModel? actual, Product expected)
|
||||
{
|
||||
Assert.That(actual, Is.Not.Null);
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(actual.Id, Is.EqualTo(expected.Id));
|
||||
Assert.That(actual.Name, Is.EqualTo(expected.Name));
|
||||
Assert.That(actual.Description, Is.EqualTo(expected.Description));
|
||||
Assert.That(actual.ProductType, Is.EqualTo(expected.ProductType));
|
||||
Assert.That(actual.UnitPrice, Is.EqualTo(expected.UnitPrice));
|
||||
Assert.That(actual.IsDeleted, Is.EqualTo(expected.IsDeleted));
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
|
||||
private static ProductDataModel CreateModel(
|
||||
string id,
|
||||
string name = "test",
|
||||
string description = "desk",
|
||||
ProductType productType = ProductType.Workpiece,
|
||||
List<string>? ingredientIds = null,
|
||||
int unitPrice = 1,
|
||||
bool isDeleted = false)
|
||||
{
|
||||
// Если ingredientIds равно null, заменяем на пустой список
|
||||
ingredientIds = ingredientIds ?? new List<string>();
|
||||
|
||||
// Создаём список ингредиентов с использованием Select
|
||||
var ingredients = ingredientIds.Select(x => new ProductIngredientDataModel(id, x, 1)).ToList();
|
||||
|
||||
return new ProductDataModel(id, name, description, productType, ingredients, unitPrice, isDeleted);
|
||||
}
|
||||
|
||||
|
||||
|
||||
private Product? GetProductFromDatabaseById(string id) => SladkieBulkiDbContext.Products.Include(x => x.ProductIngredients).FirstOrDefault(x => x.Id == id);
|
||||
|
||||
private static void AssertElement(Product? actual, ProductDataModel expected)
|
||||
{
|
||||
Assert.That(actual, Is.Not.Null);
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(actual.Id, Is.EqualTo(expected.Id));
|
||||
Assert.That(actual.Name, Is.EqualTo(expected.Name));
|
||||
Assert.That(actual.Description, Is.EqualTo(expected.Description));
|
||||
Assert.That(actual.ProductType, Is.EqualTo(expected.ProductType));
|
||||
Assert.That(actual.UnitPrice, Is.EqualTo(expected.UnitPrice));
|
||||
Assert.That(actual.IsDeleted, Is.EqualTo(expected.IsDeleted));
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,314 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||
using SladkieBulkiContrakts.Enums;
|
||||
using SladkieBulkiContrakts.Exceptions;
|
||||
using SladkieBulkiContrakts.StoragesContarcts;
|
||||
using SladkieBulkiDatabase.Implementations;
|
||||
using SladkieBulkiDatabase.Models;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SladkieBulkiTests.StoragesContracts;
|
||||
|
||||
[TestFixture]
|
||||
internal class ProductionStorageContractTests : BaseStorageContractTest
|
||||
{
|
||||
private ProductionStorageContract _productionStorageContract;
|
||||
private Worker _worker;
|
||||
private Product _product;
|
||||
|
||||
[SetUp]
|
||||
public void SetUp()
|
||||
{
|
||||
_productionStorageContract = new ProductionStorageContract(SladkieBulkiDbContext);
|
||||
_worker = InsertWorkerToDatabaseAndReturn();
|
||||
_product = InsertProductToDatabaseAndReturn();
|
||||
}
|
||||
|
||||
[TearDown]
|
||||
public void TearDown()
|
||||
{
|
||||
SladkieBulkiDbContext.Database.ExecuteSqlRaw("TRUNCATE \"Productions\" CASCADE;");
|
||||
SladkieBulkiDbContext.Database.ExecuteSqlRaw("TRUNCATE \"Products\" CASCADE;");
|
||||
SladkieBulkiDbContext.Database.ExecuteSqlRaw("TRUNCATE \"Workers\" CASCADE;");
|
||||
SladkieBulkiDbContext.Database.ExecuteSqlRaw("TRUNCATE \"ProductIngredients\" CASCADE;");
|
||||
SladkieBulkiDbContext.Database.ExecuteSqlRaw("TRUNCATE \"Ingredients\" CASCADE;");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_GetList_WhenHaveRecords_Test()
|
||||
{
|
||||
|
||||
var production = InsertProductionToDatabaseAndReturn(_worker.Id, _product.Id, DateTime.UtcNow, 5, 100);
|
||||
InsertProductionToDatabaseAndReturn(_worker.Id, _product.Id, DateTime.UtcNow, 2, 200);
|
||||
InsertProductionToDatabaseAndReturn(_worker.Id, _product.Id, DateTime.UtcNow, 5, 500);
|
||||
var list = _productionStorageContract.GetList();
|
||||
Assert.That(list, Is.Not.Null);
|
||||
Assert.That(list, Has.Count.EqualTo(3));
|
||||
AssertElement(list.First(x => x.Id == production.Id), production);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_GetList_WhenNoRecords_Test()
|
||||
{
|
||||
var list = _productionStorageContract.GetList();
|
||||
Assert.That(list, Is.Not.Null);
|
||||
Assert.That(list, Is.Empty);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_GetList_ByPeriod_Test()
|
||||
{
|
||||
|
||||
InsertProductionToDatabaseAndReturn(_worker.Id, _product.Id, DateTime.UtcNow.AddDays(-1).AddMinutes(-3), 1, 100);
|
||||
InsertProductionToDatabaseAndReturn(_worker.Id, _product.Id, DateTime.UtcNow.AddDays(-1).AddMinutes(3), 1, 100);
|
||||
InsertProductionToDatabaseAndReturn(_worker.Id, _product.Id, DateTime.UtcNow.AddDays(1).AddMinutes(-3), 1, 100);
|
||||
InsertProductionToDatabaseAndReturn(_worker.Id, _product.Id, DateTime.UtcNow.AddDays(1).AddMinutes(3), 1, 100);
|
||||
|
||||
var list = _productionStorageContract.GetList(startDate: DateTime.UtcNow.AddDays(-1), endDate: DateTime.UtcNow.AddDays(1));
|
||||
|
||||
Assert.That(list, Is.Not.Null);
|
||||
Assert.That(list, Has.Count.EqualTo(2));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_GetList_ByWorkerId_Test()
|
||||
{
|
||||
var worker1 = InsertWorkerToDatabaseAndReturn("Main worker");
|
||||
var worker2 = InsertWorkerToDatabaseAndReturn("Other worker");
|
||||
|
||||
InsertProductionToDatabaseAndReturn(worker1.Id, _product.Id, DateTime.UtcNow, 1, 100);
|
||||
InsertProductionToDatabaseAndReturn(worker1.Id, _product.Id, DateTime.UtcNow, 1, 100);
|
||||
InsertProductionToDatabaseAndReturn(worker2.Id, _product.Id, DateTime.UtcNow, 1, 100);
|
||||
|
||||
var list = _productionStorageContract.GetList(workerId: worker1.Id);
|
||||
|
||||
Assert.That(list, Is.Not.Null);
|
||||
Assert.That(list, Has.Count.EqualTo(2));
|
||||
Assert.That(list.All(x => x.WorkerId == worker1.Id));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_GetList_ByProductId_Test()
|
||||
{
|
||||
var product1 = InsertProductToDatabaseAndReturn("Product 1");
|
||||
var product2 = InsertProductToDatabaseAndReturn("Product 2");
|
||||
|
||||
InsertProductionToDatabaseAndReturn(_worker.Id, product1.Id, DateTime.UtcNow, 5, 500);
|
||||
InsertProductionToDatabaseAndReturn(_worker.Id, product1.Id, DateTime.UtcNow, 1, 100);
|
||||
InsertProductionToDatabaseAndReturn(_worker.Id, product2.Id, DateTime.UtcNow, 1, 100);
|
||||
InsertProductionToDatabaseAndReturn(_worker.Id, product2.Id, DateTime.UtcNow, 1, 100);
|
||||
|
||||
var list = _productionStorageContract.GetList(productId: product1.Id);
|
||||
|
||||
Assert.That(list, Is.Not.Null);
|
||||
Assert.That(list, Has.Count.EqualTo(2));
|
||||
Assert.That(list.All(x => x.ProductId == product1.Id));
|
||||
}
|
||||
|
||||
|
||||
[Test]
|
||||
public void Try_GetList_ByAllParameters_Test()
|
||||
{
|
||||
var worker1 = InsertWorkerToDatabaseAndReturn("Main worker");
|
||||
var worker2 = InsertWorkerToDatabaseAndReturn("Other worker");
|
||||
var product1 = InsertProductToDatabaseAndReturn("Main product");
|
||||
var product2 = InsertProductToDatabaseAndReturn("Other product");
|
||||
|
||||
InsertProductionToDatabaseAndReturn(worker1.Id, product2.Id, DateTime.UtcNow.AddDays(-1).AddMinutes(3), 1, 100);
|
||||
|
||||
InsertProductionToDatabaseAndReturn(worker1.Id, product1.Id, DateTime.UtcNow.AddDays(-1).AddMinutes(-3), 1, 100); // по дате
|
||||
InsertProductionToDatabaseAndReturn(worker2.Id, product2.Id, DateTime.UtcNow.AddDays(-1).AddMinutes(3), 1, 100); // по worker
|
||||
InsertProductionToDatabaseAndReturn(worker2.Id, product1.Id, DateTime.UtcNow.AddDays(-1).AddMinutes(3), 1, 100); // по worker и product
|
||||
InsertProductionToDatabaseAndReturn(worker1.Id, product2.Id, DateTime.UtcNow.AddDays(1).AddMinutes(3), 1, 100); // по дате
|
||||
|
||||
var list = _productionStorageContract.GetList(
|
||||
startDate: DateTime.UtcNow.AddDays(-1),
|
||||
endDate: DateTime.UtcNow.AddDays(1),
|
||||
workerId: worker1.Id,
|
||||
productId: product2.Id
|
||||
);
|
||||
|
||||
Assert.That(list, Is.Not.Null);
|
||||
Assert.That(list, Has.Count.EqualTo(1));
|
||||
}
|
||||
|
||||
|
||||
[Test]
|
||||
public void Try_GetElementById_WhenHaveRecord_Test()
|
||||
{
|
||||
var production = InsertProductionToDatabaseAndReturn(_worker.Id, _product.Id, DateTime.UtcNow, 1, 100);
|
||||
|
||||
var result = _productionStorageContract.GetElementById(production.Id);
|
||||
|
||||
Assert.That(result, Is.Not.Null);
|
||||
AssertElement(result, production);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_GetElementById_WhenNoRecord_Test()
|
||||
{
|
||||
InsertProductionToDatabaseAndReturn(_worker.Id, _product.Id, DateTime.UtcNow, 1, 100);
|
||||
|
||||
var result = _productionStorageContract.GetElementById(Guid.NewGuid().ToString());
|
||||
|
||||
Assert.That(result, Is.Null);
|
||||
}
|
||||
|
||||
|
||||
[Test]
|
||||
public void Try_GetElementById_WhenRecordHasCanceled_Test()
|
||||
{
|
||||
var production = InsertProductionToDatabaseAndReturn(_worker.Id, _product.Id, DateTime.UtcNow, 1, 100);
|
||||
|
||||
var result = _productionStorageContract.GetElementById(production.Id);
|
||||
|
||||
Assert.That(result, Is.Not.Null);
|
||||
AssertElement(result, production);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_AddElement_Test()
|
||||
{
|
||||
var productionDate = DateTime.UtcNow;
|
||||
var production = CreateModel(Guid.NewGuid().ToString(), productionDate, 1, 100, _worker.Id, _product.Id);
|
||||
_productionStorageContract.AddElement(production);
|
||||
AssertElement(GetProductionFromDatabaseById(production.Id), production);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_DelElement_Test()
|
||||
{
|
||||
|
||||
var production = InsertProductionToDatabaseAndReturn(_worker.Id, _product.Id, DateTime.UtcNow, 1, 100);
|
||||
_productionStorageContract.DelElement(production.Id);
|
||||
|
||||
var element = GetProductionFromDatabaseById(production.Id);
|
||||
Assert.That(element, Is.Null);
|
||||
}
|
||||
|
||||
|
||||
[Test]
|
||||
public void Try_DelElement_WhenNoRecordWithThisId_Test()
|
||||
{
|
||||
Assert.That(() => _productionStorageContract.DelElement(Guid.NewGuid().ToString()), Throws.TypeOf<ElementNotFoundException>());
|
||||
}
|
||||
|
||||
|
||||
private Worker InsertWorkerToDatabaseAndReturn(string fio = "test")
|
||||
{
|
||||
var worker = new Worker() { Id = Guid.NewGuid().ToString(), FIO = fio, PostId = Guid.NewGuid().ToString(), Email = "1@mail.ru" };
|
||||
SladkieBulkiDbContext.Workers.Add(worker);
|
||||
SladkieBulkiDbContext.SaveChanges();
|
||||
return worker;
|
||||
}
|
||||
|
||||
|
||||
private Product InsertProductToDatabaseAndReturn(
|
||||
|
||||
string productName = "test",
|
||||
string description = "desc 1",
|
||||
ProductType productType = ProductType.Production,
|
||||
int unitPrice = 1,
|
||||
bool isDeleted = false,
|
||||
List<(string ingredientId, int count)>? ingredients = null)
|
||||
{
|
||||
var product = new Product
|
||||
{
|
||||
Id = Guid.NewGuid().ToString(),
|
||||
Name = productName,
|
||||
Description = description,
|
||||
ProductType = productType,
|
||||
UnitPrice = unitPrice,
|
||||
IsDeleted = isDeleted,
|
||||
ProductIngredients = new List<ProductIngredient>()
|
||||
};
|
||||
|
||||
if (ingredients is not null)
|
||||
{
|
||||
foreach (var (ingredientId, count) in ingredients)
|
||||
{
|
||||
product.ProductIngredients.Add(new ProductIngredient
|
||||
{
|
||||
ProductId = product.Id,
|
||||
IngredientId = ingredientId,
|
||||
Count = count
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
SladkieBulkiDbContext.Products.Add(product);
|
||||
SladkieBulkiDbContext.SaveChanges();
|
||||
return product;
|
||||
}
|
||||
|
||||
|
||||
|
||||
private Production InsertProductionToDatabaseAndReturn(string workerId, string productId, DateTime productionDate, int count, double sum)
|
||||
{
|
||||
var production = new Production
|
||||
{
|
||||
Id = Guid.NewGuid().ToString(),
|
||||
WorkerId = workerId,
|
||||
ProductId = productId,
|
||||
ProductionDate = productionDate,
|
||||
Count = count,
|
||||
Sum = sum
|
||||
};
|
||||
SladkieBulkiDbContext.Productions.Add(production);
|
||||
SladkieBulkiDbContext.SaveChanges();
|
||||
return production;
|
||||
}
|
||||
|
||||
|
||||
private void AssertElement(ProductionDataModel result, Production expected)
|
||||
{
|
||||
Assert.That(result.Id, Is.EqualTo(expected.Id));
|
||||
Assert.That(result.ProductionDate, Is.EqualTo(expected.ProductionDate));
|
||||
Assert.That(result.Count, Is.EqualTo(expected.Count));
|
||||
Assert.That(result.Sum, Is.EqualTo(expected.Sum));
|
||||
Assert.That(result.WorkerId, Is.EqualTo(expected.WorkerId));
|
||||
Assert.That(result.ProductId, Is.EqualTo(expected.ProductId));
|
||||
}
|
||||
|
||||
private static ProductionDataModel CreateModel(string id, DateTime productionDate, int count, double sum, string workerId, string productId)
|
||||
{
|
||||
return new ProductionDataModel(id, productionDate, count, sum, workerId, productId);
|
||||
}
|
||||
|
||||
private Production? GetProductionFromDatabaseById(string id) =>
|
||||
SladkieBulkiDbContext.Productions
|
||||
.Include(x => x.Product)
|
||||
.Include(x => x.Worker)
|
||||
.FirstOrDefault(x => x.Id == id);
|
||||
|
||||
private static void AssertElement(Production? actual, ProductionDataModel expected)
|
||||
{
|
||||
Assert.That(actual, Is.Not.Null);
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(actual.Id, Is.EqualTo(expected.Id));
|
||||
Assert.That(actual.WorkerId, Is.EqualTo(expected.WorkerId));
|
||||
Assert.That(actual.ProductId, Is.EqualTo(expected.ProductId));
|
||||
Assert.That(actual.ProductionDate, Is.EqualTo(expected.ProductionDate));
|
||||
Assert.That(actual.Count, Is.EqualTo(expected.Count));
|
||||
Assert.That(actual.Sum, Is.EqualTo(expected.Sum));
|
||||
});
|
||||
|
||||
if (actual.Product != null)
|
||||
{
|
||||
Assert.That(actual.Product.Id, Is.EqualTo(expected.ProductId));
|
||||
}
|
||||
|
||||
if (actual.Worker != null)
|
||||
{
|
||||
Assert.That(actual.Worker.Id, Is.EqualTo(expected.WorkerId));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,167 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using SladkieBulkiContrakts.DataModels;
|
||||
using SladkieBulkiDatabase.Implementations;
|
||||
using SladkieBulkiDatabase.Models;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SladkieBulkiTests.StoragesContracts;
|
||||
|
||||
|
||||
[TestFixture]
|
||||
internal class SalaryStorageContractTests : BaseStorageContractTest
|
||||
{
|
||||
private SalaryStorageContract _salaryStorageContract;
|
||||
private Worker _worker;
|
||||
|
||||
[SetUp]
|
||||
public void SetUp()
|
||||
{
|
||||
_salaryStorageContract = new SalaryStorageContract(SladkieBulkiDbContext);
|
||||
_worker = InsertWorkerToDatabaseAndReturn();
|
||||
}
|
||||
|
||||
[TearDown]
|
||||
public void TearDown()
|
||||
{
|
||||
SladkieBulkiDbContext.Database.ExecuteSqlRaw("TRUNCATE \"Salaries\" CASCADE;");
|
||||
SladkieBulkiDbContext.Database.ExecuteSqlRaw("TRUNCATE \"Workers\" CASCADE;");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_GetList_WhenHaveRecords_Test()
|
||||
{
|
||||
var salary = InsertSalaryToDatabaseAndReturn(_worker.Id, workerSalary: 100);
|
||||
InsertSalaryToDatabaseAndReturn(_worker.Id);
|
||||
InsertSalaryToDatabaseAndReturn(_worker.Id);
|
||||
var list = _salaryStorageContract.GetList(DateTime.UtcNow.AddDays(-10), DateTime.UtcNow.AddDays(10));
|
||||
Assert.That(list, Is.Not.Null);
|
||||
Assert.That(list, Has.Count.EqualTo(3));
|
||||
AssertElement(list.First(), salary);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_GetList_WhenNoRecords_Test()
|
||||
{
|
||||
var list = _salaryStorageContract.GetList(DateTime.UtcNow.AddDays(-10), DateTime.UtcNow.AddDays(10));
|
||||
Assert.That(list, Is.Not.Null);
|
||||
Assert.That(list, Is.Empty);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_GetList_OnlyInDatePeriod_Test()
|
||||
{
|
||||
InsertSalaryToDatabaseAndReturn(_worker.Id, salaryDate: DateTime.UtcNow.AddDays(-2));
|
||||
InsertSalaryToDatabaseAndReturn(_worker.Id, salaryDate: DateTime.UtcNow.AddDays(-1).AddMinutes(-5));
|
||||
InsertSalaryToDatabaseAndReturn(_worker.Id, salaryDate: DateTime.UtcNow.AddDays(-1).AddMinutes(5));
|
||||
InsertSalaryToDatabaseAndReturn(_worker.Id, salaryDate: DateTime.UtcNow.AddDays(1).AddMinutes(-5));
|
||||
InsertSalaryToDatabaseAndReturn(_worker.Id, salaryDate: DateTime.UtcNow.AddDays(1).AddMinutes(5));
|
||||
InsertSalaryToDatabaseAndReturn(_worker.Id, salaryDate: DateTime.UtcNow.AddDays(-2));
|
||||
var list = _salaryStorageContract.GetList(DateTime.UtcNow.AddDays(-1), DateTime.UtcNow.AddDays(1));
|
||||
Assert.That(list, Is.Not.Null);
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(list, Has.Count.EqualTo(2));
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_GetList_ByWorker_Test()
|
||||
{
|
||||
var worker = InsertWorkerToDatabaseAndReturn("name 2");
|
||||
InsertSalaryToDatabaseAndReturn(_worker.Id);
|
||||
InsertSalaryToDatabaseAndReturn(_worker.Id);
|
||||
InsertSalaryToDatabaseAndReturn(worker.Id);
|
||||
var list = _salaryStorageContract.GetList(DateTime.UtcNow.AddDays(-1), DateTime.UtcNow.AddDays(1), _worker.Id);
|
||||
Assert.That(list, Is.Not.Null);
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(list, Has.Count.EqualTo(2));
|
||||
Assert.That(list.All(x => x.WorkerId == _worker.Id));
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_GetList_ByWorkerOnlyInDatePeriod_Test()
|
||||
{
|
||||
var worker = InsertWorkerToDatabaseAndReturn("name 2");
|
||||
InsertSalaryToDatabaseAndReturn(_worker.Id, salaryDate: DateTime.UtcNow.AddDays(-2));
|
||||
InsertSalaryToDatabaseAndReturn(_worker.Id, salaryDate: DateTime.UtcNow.AddDays(-1).AddMinutes(5));
|
||||
InsertSalaryToDatabaseAndReturn(worker.Id, salaryDate: DateTime.UtcNow.AddDays(-1).AddMinutes(5));
|
||||
InsertSalaryToDatabaseAndReturn(_worker.Id, salaryDate: DateTime.UtcNow.AddDays(1).AddMinutes(-5));
|
||||
InsertSalaryToDatabaseAndReturn(worker.Id, salaryDate: DateTime.UtcNow.AddDays(1).AddMinutes(-5));
|
||||
InsertSalaryToDatabaseAndReturn(_worker.Id, salaryDate: DateTime.UtcNow.AddDays(-2));
|
||||
var list = _salaryStorageContract.GetList(DateTime.UtcNow.AddDays(-1), DateTime.UtcNow.AddDays(1), _worker.Id);
|
||||
Assert.That(list, Is.Not.Null);
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(list, Has.Count.EqualTo(2));
|
||||
Assert.That(list.All(x => x.WorkerId == _worker.Id));
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_AddElement_Test()
|
||||
{
|
||||
|
||||
var salaryDate = DateTime.UtcNow;
|
||||
var salary = CreateModel(_worker.Id, 1.0, salaryDate);
|
||||
_salaryStorageContract.AddElement(salary);
|
||||
|
||||
var savedSalary = GetSalaryFromDatabaseByWorkerId(_worker.Id);
|
||||
Assert.NotNull(savedSalary);
|
||||
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(savedSalary.WorkerId, Is.EqualTo(salary.WorkerId));
|
||||
Assert.That(savedSalary.WorkerSalary, Is.EqualTo(salary.WorkerSalary));
|
||||
Assert.That(savedSalary.SalaryDate, Is.EqualTo(salaryDate).Within(TimeSpan.FromSeconds(1)));
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
private Worker InsertWorkerToDatabaseAndReturn(string workerFIO = "fio")
|
||||
{
|
||||
var worker = new Worker() { Id = Guid.NewGuid().ToString(), PostId = Guid.NewGuid().ToString(), FIO = workerFIO, IsDeleted = false , Email = "1@mail.ru"};
|
||||
SladkieBulkiDbContext.Workers.Add(worker);
|
||||
SladkieBulkiDbContext.SaveChanges();
|
||||
return worker;
|
||||
}
|
||||
|
||||
private Salary InsertSalaryToDatabaseAndReturn(string workerId, double workerSalary = 1, DateTime? salaryDate = null)
|
||||
{
|
||||
var salary = new Salary() { WorkerId = workerId, WorkerSalary = workerSalary, SalaryDate = salaryDate ?? DateTime.UtcNow };
|
||||
SladkieBulkiDbContext.Salaries.Add(salary);
|
||||
SladkieBulkiDbContext.SaveChanges();
|
||||
return salary;
|
||||
}
|
||||
|
||||
private static void AssertElement(SalaryDataModel? actual, Salary expected)
|
||||
{
|
||||
Assert.That(actual, Is.Not.Null);
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(actual.WorkerId, Is.EqualTo(expected.WorkerId));
|
||||
Assert.That(actual.WorkerSalary, Is.EqualTo(expected.WorkerSalary));
|
||||
});
|
||||
}
|
||||
|
||||
private static SalaryDataModel CreateModel(string workerId, double workerSalary, DateTime salaryDate)
|
||||
=> new(workerId, salaryDate, workerSalary);
|
||||
|
||||
|
||||
private Salary? GetSalaryFromDatabaseByWorkerId(string id) => SladkieBulkiDbContext.Salaries.FirstOrDefault(x => x.WorkerId == id);
|
||||
|
||||
private static void AssertElement(Salary? actual, SalaryDataModel expected)
|
||||
{
|
||||
Assert.That(actual, Is.Not.Null);
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(actual.WorkerId, Is.EqualTo(expected.WorkerId));
|
||||
Assert.That(actual.WorkerSalary, Is.EqualTo(expected.WorkerSalary));
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,252 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using SladkieBulkiContrakts.DataModels;
|
||||
using SladkieBulkiContrakts.Exceptions;
|
||||
using SladkieBulkiDatabase.Implementations;
|
||||
using SladkieBulkiDatabase.Models;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SladkieBulkiTests.StoragesContracts;
|
||||
|
||||
internal class WorkerStorageContractTests : BaseStorageContractTest
|
||||
{
|
||||
private WorkerStorageContract _workerStorageContract;
|
||||
|
||||
[SetUp]
|
||||
public void SetUp()
|
||||
{
|
||||
_workerStorageContract = new WorkerStorageContract(SladkieBulkiDbContext);
|
||||
}
|
||||
|
||||
[TearDown]
|
||||
public void TearDown()
|
||||
{
|
||||
SladkieBulkiDbContext.Database.ExecuteSqlRaw("TRUNCATE \"Workers\" CASCADE;");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_GetList_WhenHaveRecords_Test()
|
||||
{
|
||||
var worker = InsertWorkerToDatabaseAndReturn(Guid.NewGuid().ToString(), "fio 1");
|
||||
InsertWorkerToDatabaseAndReturn(Guid.NewGuid().ToString(), "fio 2");
|
||||
InsertWorkerToDatabaseAndReturn(Guid.NewGuid().ToString(), "fio 3");
|
||||
var list = _workerStorageContract.GetList();
|
||||
Assert.That(list, Is.Not.Null);
|
||||
Assert.That(list, Has.Count.EqualTo(3));
|
||||
AssertElement(list.First(), worker);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_GetList_WhenNoRecords_Test()
|
||||
{
|
||||
var list = _workerStorageContract.GetList();
|
||||
Assert.That(list, Is.Not.Null);
|
||||
Assert.That(list, Is.Empty);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_GetList_ByPostId_Test()
|
||||
{
|
||||
var postId = Guid.NewGuid().ToString();
|
||||
InsertWorkerToDatabaseAndReturn(Guid.NewGuid().ToString(), "fio 1", postId);
|
||||
InsertWorkerToDatabaseAndReturn(Guid.NewGuid().ToString(), "fio 2", postId);
|
||||
InsertWorkerToDatabaseAndReturn(Guid.NewGuid().ToString(), "fio 3");
|
||||
var list = _workerStorageContract.GetList(postId: postId);
|
||||
Assert.That(list, Is.Not.Null);
|
||||
Assert.That(list, Has.Count.EqualTo(2));
|
||||
Assert.That(list.All(x => x.PostId == postId));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_GetList_ByBirthDate_Test()
|
||||
{
|
||||
InsertWorkerToDatabaseAndReturn(Guid.NewGuid().ToString(), "fio 1", birthDate: DateTime.UtcNow.AddYears(-25));
|
||||
InsertWorkerToDatabaseAndReturn(Guid.NewGuid().ToString(), "fio 2", birthDate: DateTime.UtcNow.AddYears(-21));
|
||||
InsertWorkerToDatabaseAndReturn(Guid.NewGuid().ToString(), "fio 3", birthDate: DateTime.UtcNow.AddYears(-20));
|
||||
InsertWorkerToDatabaseAndReturn(Guid.NewGuid().ToString(), "fio 4", birthDate: DateTime.UtcNow.AddYears(-19));
|
||||
var list = _workerStorageContract.GetList(fromBirthDate: DateTime.UtcNow.AddYears(-21).AddMinutes(-1), toBirthDate: DateTime.UtcNow.AddYears(-20).AddMinutes(1));
|
||||
Assert.That(list, Is.Not.Null);
|
||||
Assert.That(list, Has.Count.EqualTo(2));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_GetList_ByEmploymentDate_Test()
|
||||
{
|
||||
InsertWorkerToDatabaseAndReturn(Guid.NewGuid().ToString(), "fio 1", employmentDate: DateTime.UtcNow.AddDays(-2));
|
||||
InsertWorkerToDatabaseAndReturn(Guid.NewGuid().ToString(), "fio 2", employmentDate: DateTime.UtcNow.AddDays(-1));
|
||||
InsertWorkerToDatabaseAndReturn(Guid.NewGuid().ToString(), "fio 3", employmentDate: DateTime.UtcNow.AddDays(1));
|
||||
InsertWorkerToDatabaseAndReturn(Guid.NewGuid().ToString(), "fio 4", employmentDate: DateTime.UtcNow.AddDays(2));
|
||||
var list = _workerStorageContract.GetList(fromEmploymentDate: DateTime.UtcNow.AddDays(-1).AddMinutes(-1), toEmploymentDate: DateTime.UtcNow.AddDays(1).AddMinutes(1));
|
||||
Assert.That(list, Is.Not.Null);
|
||||
Assert.That(list, Has.Count.EqualTo(2));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_GetList_ByAllParameters_Test()
|
||||
{
|
||||
var postId = Guid.NewGuid().ToString();
|
||||
InsertWorkerToDatabaseAndReturn(Guid.NewGuid().ToString(), "fio 1", postId, birthDate: DateTime.UtcNow.AddYears(-25), employmentDate: DateTime.UtcNow.AddDays(-2));
|
||||
InsertWorkerToDatabaseAndReturn(Guid.NewGuid().ToString(), "fio 2", postId, birthDate: DateTime.UtcNow.AddYears(-22), employmentDate: DateTime.UtcNow.AddDays(-1));
|
||||
InsertWorkerToDatabaseAndReturn(Guid.NewGuid().ToString(), "fio 3", postId, birthDate: DateTime.UtcNow.AddYears(-21), employmentDate: DateTime.UtcNow.AddDays(-1));
|
||||
InsertWorkerToDatabaseAndReturn(Guid.NewGuid().ToString(), "fio 4", birthDate: DateTime.UtcNow.AddYears(-20), employmentDate: DateTime.UtcNow.AddDays(1));
|
||||
var list = _workerStorageContract.GetList(postId: postId, fromBirthDate: DateTime.UtcNow.AddYears(-21).AddMinutes(-1), toBirthDate: DateTime.UtcNow.AddYears(-20).AddMinutes(1), fromEmploymentDate: DateTime.UtcNow.AddDays(-1).AddMinutes(-1), toEmploymentDate: DateTime.UtcNow.AddDays(1).AddMinutes(1));
|
||||
Assert.That(list, Is.Not.Null);
|
||||
Assert.That(list, Has.Count.EqualTo(1));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_GetElementById_WhenHaveRecord_Test()
|
||||
{
|
||||
var worker = InsertWorkerToDatabaseAndReturn(Guid.NewGuid().ToString());
|
||||
AssertElement(_workerStorageContract.GetElementById(worker.Id), worker);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_GetElementById_WhenNoRecord_Test()
|
||||
{
|
||||
Assert.That(() => _workerStorageContract.GetElementById(Guid.NewGuid().ToString()), Is.Null);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_GetElementByFIO_WhenHaveRecord_Test()
|
||||
{
|
||||
var worker = InsertWorkerToDatabaseAndReturn(Guid.NewGuid().ToString());
|
||||
AssertElement(_workerStorageContract.GetElementByFIO(worker.FIO), worker);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_GetElementByFIO_WhenNoRecord_Test()
|
||||
{
|
||||
Assert.That(() => _workerStorageContract.GetElementByFIO("New Fio"), Is.Null);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_AddElement_Test()
|
||||
{
|
||||
var worker = CreateModel(Guid.NewGuid().ToString());
|
||||
_workerStorageContract.AddElement(worker);
|
||||
AssertElement(GetWorkerFromDatabase(worker.Id), worker);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_AddElement_WhenHaveRecordWithSameId_Test()
|
||||
{
|
||||
var worker = CreateModel(Guid.NewGuid().ToString());
|
||||
InsertWorkerToDatabaseAndReturn(worker.Id);
|
||||
Assert.That(() => _workerStorageContract.AddElement(worker), Throws.TypeOf<ElementExistsException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_UpdElement_Test()
|
||||
{
|
||||
var worker = CreateModel(Guid.NewGuid().ToString(), "New Fio");
|
||||
InsertWorkerToDatabaseAndReturn(worker.Id);
|
||||
_workerStorageContract.UpdElement(worker);
|
||||
AssertElement(GetWorkerFromDatabase(worker.Id), worker);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_UpdElement_WhenNoRecordWithThisId_Test()
|
||||
{
|
||||
Assert.That(() => _workerStorageContract.UpdElement(CreateModel(Guid.NewGuid().ToString())), Throws.TypeOf<ElementNotFoundException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_UpdElement_WhenNoRecordWasDeleted_Test()
|
||||
{
|
||||
var worker = CreateModel(Guid.NewGuid().ToString());
|
||||
InsertWorkerToDatabaseAndReturn(worker.Id, isDeleted: true);
|
||||
Assert.That(() => _workerStorageContract.UpdElement(worker), Throws.TypeOf<ElementNotFoundException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_DelElement_Test()
|
||||
{
|
||||
var worker = InsertWorkerToDatabaseAndReturn(Guid.NewGuid().ToString());
|
||||
_workerStorageContract.DelElement(worker.Id);
|
||||
var element = GetWorkerFromDatabase(worker.Id);
|
||||
Assert.That(element, Is.Not.Null);
|
||||
Assert.That(element.IsDeleted);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_DelElement_WhenNoRecordWithThisId_Test()
|
||||
{
|
||||
Assert.That(() => _workerStorageContract.DelElement(Guid.NewGuid().ToString()), Throws.TypeOf<ElementNotFoundException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_DelElement_WhenNoRecordWasDeleted_Test()
|
||||
{
|
||||
var worker = CreateModel(Guid.NewGuid().ToString());
|
||||
InsertWorkerToDatabaseAndReturn(worker.Id, isDeleted: true);
|
||||
Assert.That(() => _workerStorageContract.DelElement(worker.Id), Throws.TypeOf<ElementNotFoundException>());
|
||||
}
|
||||
|
||||
private Worker InsertWorkerToDatabaseAndReturn(
|
||||
string id,
|
||||
string fio = "test",
|
||||
string? postId = null,
|
||||
DateTime? birthDate = null,
|
||||
DateTime? employmentDate = null,
|
||||
bool isDeleted = false,
|
||||
string email = "test@example.com")
|
||||
{
|
||||
var worker = new Worker()
|
||||
{
|
||||
Id = id,
|
||||
FIO = fio,
|
||||
Email = email,
|
||||
PostId = postId ?? Guid.NewGuid().ToString(),
|
||||
BirthDate = birthDate ?? DateTime.UtcNow.AddYears(-20),
|
||||
EmploymentDate = employmentDate ?? DateTime.UtcNow,
|
||||
IsDeleted = isDeleted
|
||||
};
|
||||
SladkieBulkiDbContext.Workers.Add(worker);
|
||||
SladkieBulkiDbContext.SaveChanges();
|
||||
return worker;
|
||||
}
|
||||
|
||||
private static void AssertElement(WorkerDataModel? actual, Worker expected)
|
||||
{
|
||||
Assert.That(actual, Is.Not.Null);
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(actual.Id, Is.EqualTo(expected.Id));
|
||||
Assert.That(actual.PostId, Is.EqualTo(expected.PostId));
|
||||
Assert.That(actual.FIO, Is.EqualTo(expected.FIO));
|
||||
Assert.That(actual.BirthDate, Is.EqualTo(expected.BirthDate));
|
||||
Assert.That(actual.EmploymentDate, Is.EqualTo(expected.EmploymentDate));
|
||||
Assert.That(actual.IsDeleted, Is.EqualTo(expected.IsDeleted));
|
||||
});
|
||||
}
|
||||
|
||||
private static WorkerDataModel CreateModel(
|
||||
string id,
|
||||
string fio = "fio",
|
||||
string? postId = null,
|
||||
DateTime? birthDate = null,
|
||||
DateTime? employmentDate = null,
|
||||
bool isDeleted = false,
|
||||
string email = "fio@example.com") =>
|
||||
new(id, fio, postId ?? Guid.NewGuid().ToString(), birthDate ?? DateTime.UtcNow.AddYears(-20), employmentDate ?? DateTime.UtcNow, isDeleted, email);
|
||||
|
||||
private Worker? GetWorkerFromDatabase(string id) => SladkieBulkiDbContext.Workers.FirstOrDefault(x => x.Id == id);
|
||||
|
||||
private static void AssertElement(Worker? actual, WorkerDataModel expected)
|
||||
{
|
||||
Assert.That(actual, Is.Not.Null);
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(actual.Id, Is.EqualTo(expected.Id));
|
||||
Assert.That(actual.PostId, Is.EqualTo(expected.PostId));
|
||||
Assert.That(actual.FIO, Is.EqualTo(expected.FIO));
|
||||
Assert.That(actual.BirthDate, Is.EqualTo(expected.BirthDate));
|
||||
Assert.That(actual.EmploymentDate, Is.EqualTo(expected.EmploymentDate));
|
||||
Assert.That(actual.IsDeleted, Is.EqualTo(expected.IsDeleted));
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user