2 Commits

Author SHA1 Message Date
fd218c2ec1 лаба2 2025-04-16 00:15:22 +04:00
60d9e15bba Сделанная 1 лаба 2025-02-27 12:23:58 +04:00
49 changed files with 4358 additions and 1 deletions

View File

@@ -0,0 +1,74 @@
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using ThreeFatMenContracts.BusinessLogicsContracts;
using ThreeFatMenContracts.DataModels;
using ThreeFatMenContracts.Exceptions;
using ThreeFatMenContracts.Extensions;
using ThreeFatMenContracts.StoragesContracts;
namespace ThreeFatMenBusinessLogic.Implementations;
internal class BuyerBusinessLogicContract(IBuyerStorageContract buyerStorageContract, ILogger logger) : IBuyerBusinessLogicContract
{
private readonly ILogger _logger = logger;
private readonly IBuyerStorageContract _buyerStorageContract = buyerStorageContract;
public List<BuyerDataModel> GetAllBuyers()
{
_logger.LogInformation("GetAllBuyers");
return _buyerStorageContract.GetList() ?? throw new NullListException();
}
public BuyerDataModel GetBuyerByData(string data)
{
_logger.LogInformation("Get element by data: {data}", data);
if (data.IsEmpty())
{
throw new ArgumentNullException(nameof(data));
}
if (data.IsGuid())
{
return _buyerStorageContract.GetElementById(data) ?? throw new ElementNotFoundException(data);
}
if (Regex.IsMatch(data, @"^((8|\+7)[\- ]?)?(\(?\d{3}\)?[\- ]?)?[\d\-]{7,10}$"))
{
return _buyerStorageContract.GetElementByPhoneNumber(data) ??
throw new ElementNotFoundException(data);
}
return _buyerStorageContract.GetElementByFIO(data) ?? throw new ElementNotFoundException(data);
}
public void InsertBuyer(BuyerDataModel buyerDataModel)
{
_logger.LogInformation("New data: {json}",
JsonSerializer.Serialize(buyerDataModel));
ArgumentNullException.ThrowIfNull(buyerDataModel);
buyerDataModel.Validate();
_buyerStorageContract.AddElement(buyerDataModel);
}
public void UpdateBuyer(BuyerDataModel buyerDataModel)
{
_logger.LogInformation("Update data: {json}",
JsonSerializer.Serialize(buyerDataModel));
ArgumentNullException.ThrowIfNull(buyerDataModel);
buyerDataModel.Validate();
_buyerStorageContract.UpdElement(buyerDataModel);
}
public void DeleteBuyer(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");
}
_buyerStorageContract.DelElement(id);
}
}

View File

@@ -0,0 +1,85 @@
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
using ThreeFatMenContracts.BusinessLogicsContracts;
using ThreeFatMenContracts.DataModels;
using ThreeFatMenContracts.Exceptions;
using ThreeFatMenContracts.Extensions;
using ThreeFatMenContracts.StoragesContracts;
namespace ThreeFatMenBusinessLogic.Implementations;
internal class LunchBusinessLogicContract(ILunchStorageContract lunchStorageContract, ILogger logger) : ILunchBusinessLogicContract
{
private readonly ILogger _logger = logger;
private readonly ILunchStorageContract _lunchStorageContract = lunchStorageContract;
public List<LunchDataModel> GetAllLunches(bool onlyActive)
{
_logger.LogInformation("GetAllProducts params: {onlyActive}", onlyActive);
return _lunchStorageContract.GetList(onlyActive) ?? throw new NullListException();
}
public List<LunchHistoryDataModel> GetLunchHistoryByLunch(string lunchId)
{
_logger.LogInformation("GetProductHistoryByProduct for {productId}", lunchId);
if (lunchId.IsEmpty())
{
throw new ArgumentNullException(nameof(lunchId));
}
if (!lunchId.IsGuid())
{
throw new ValidationException("The value in the field productId is not a unique identifier.");
}
return _lunchStorageContract.GetHistoryByLunchId(lunchId) ??
throw new NullListException();
}
public LunchDataModel GetLunchByData(string data)
{
_logger.LogInformation("Get element by data: {data}", data);
if (data.IsEmpty())
{
throw new ArgumentNullException(nameof(data));
}
if (data.IsGuid())
{
return _lunchStorageContract.GetElementById(data) ?? throw
new ElementNotFoundException(data);
}
return _lunchStorageContract.GetElementByName(data) ?? throw new
ElementNotFoundException(data);
}
public void InsertLunch(LunchDataModel lunchDataModel)
{
_logger.LogInformation("New data: {json}",
JsonSerializer.Serialize(lunchDataModel));
ArgumentNullException.ThrowIfNull(lunchDataModel);
lunchDataModel.Validate();
_lunchStorageContract.AddElement(lunchDataModel);
}
public void UpdateLunch(LunchDataModel lunchDataModel)
{
_logger.LogInformation("Update data: {json}",
JsonSerializer.Serialize(lunchDataModel));
ArgumentNullException.ThrowIfNull(lunchDataModel);
lunchDataModel.Validate();
_lunchStorageContract.UpdElement(lunchDataModel);
}
public void DeleteLunch(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");
}
_lunchStorageContract.DelElement(id);
}
}

View File

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

View File

@@ -0,0 +1,125 @@
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
using ThreeFatMenContracts.BusinessLogicsContracts;
using ThreeFatMenContracts.DataModels;
using ThreeFatMenContracts.Exceptions;
using ThreeFatMenContracts.Extensions;
using ThreeFatMenContracts.StoragesContracts;
namespace ThreeFatMenBusinessLogic.Implementations;
internal class SaleBusinessLogicContract(ISaleStorageContract saleStorageContract, ILogger logger) : ISaleBusinessLogicContract
{
private readonly ILogger _logger = logger;
private readonly ISaleStorageContract _saleStorageContract =
saleStorageContract;
public List<SaleDataModel> GetAllSalesByPeriod(DateTime fromDate, DateTime
toDate)
{
_logger.LogInformation("GetAllSales params: {fromDate}, {toDate}",
fromDate, toDate);
if (fromDate.IsDateNotOlder(toDate))
{
throw new IncorrectDatesException(fromDate, toDate);
}
return _saleStorageContract.GetList(fromDate, toDate) ?? throw new
NullListException();
}
public List<SaleDataModel> GetAllSalesByWorkerByPeriod(string workerId,
DateTime fromDate, DateTime toDate)
{
_logger.LogInformation("GetAllSales 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 _saleStorageContract.GetList(fromDate, toDate, workerId:
workerId) ?? throw new NullListException();
}
public List<SaleDataModel> GetAllSalesByBuyerByPeriod(string buyerId,
DateTime fromDate, DateTime toDate)
{
_logger.LogInformation("GetAllSales params: {buyerId}, {fromDate}, { toDate}", buyerId, fromDate, toDate);
if (fromDate.IsDateNotOlder(toDate))
{
throw new IncorrectDatesException(fromDate, toDate);
}
if (buyerId.IsEmpty())
{
throw new ArgumentNullException(nameof(buyerId));
}
if (!buyerId.IsGuid())
{
throw new ValidationException("The value in the field buyerId is not a unique identifier.");
}
return _saleStorageContract.GetList(fromDate, toDate, buyerId:
buyerId) ?? throw new NullListException();
}
public List<SaleDataModel> GetAllSalesByProductByPeriod(string productId,
DateTime fromDate, DateTime toDate)
{
_logger.LogInformation("GetAllSales 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 _saleStorageContract.GetList(fromDate, toDate, lunchId:
productId) ?? throw new NullListException();
}
public SaleDataModel GetSaleByData(string data)
{
_logger.LogInformation("Get element by data: {data}", data);
if (data.IsEmpty())
{
throw new ArgumentNullException(nameof(data));
}
if (!data.IsGuid())
{
throw new ValidationException("Id is not a unique identifier");
}
return _saleStorageContract.GetElementById(data) ?? throw new
ElementNotFoundException(data);
}
public void InsertSale(SaleDataModel saleDataModel)
{
_logger.LogInformation("New data: {json}",
JsonSerializer.Serialize(saleDataModel));
ArgumentNullException.ThrowIfNull(saleDataModel);
saleDataModel.Validate();
_saleStorageContract.AddElement(saleDataModel);
}
public void CancelSale(string id)
{
_logger.LogInformation("Cancel by id: {id}", id);
if (id.IsEmpty())
{
throw new ArgumentNullException(nameof(id));
}
if (!id.IsGuid())
{
throw new ValidationException("Id is not a unique identifier");
}
_saleStorageContract.DelElement(id);
}
}

View File

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

View File

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

View File

@@ -0,0 +1,21 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ThreeFatMenContracts.DataModels;
namespace ThreeFatMenContracts.BusinessLogicsContracts;
public interface IBuyerBusinessLogicContract
{
List<BuyerDataModel> GetAllBuyers();
BuyerDataModel GetBuyerByData(string data);
void InsertBuyer(BuyerDataModel buyerDataModel);
void UpdateBuyer(BuyerDataModel buyerDataModel);
void DeleteBuyer(string id);
}

View File

@@ -0,0 +1,18 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ThreeFatMenContracts.DataModels;
namespace ThreeFatMenContracts.BusinessLogicsContracts;
public interface ILunchBusinessLogicContract
{
List<LunchDataModel> GetAllLunches(bool onlyActive = true);
List<LunchHistoryDataModel> GetLunchHistoryByLunch(string lunchId);
LunchDataModel GetLunchByData(string data);
void InsertLunch(LunchDataModel lunchDataModel);
void UpdateLunch(LunchDataModel lunchDataModel);
void DeleteLunch(string id);
}

View File

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

View File

@@ -0,0 +1,19 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ThreeFatMenContracts.DataModels;
namespace ThreeFatMenContracts.BusinessLogicsContracts;
public interface ISaleBusinessLogicContract
{
List<SaleDataModel> GetAllSalesByPeriod(DateTime fromDate, DateTime toDate);
List<SaleDataModel> GetAllSalesByWorkerByPeriod(string workerId, DateTime fromDate, DateTime toDate);
List<SaleDataModel> GetAllSalesByBuyerByPeriod(string buyerId, DateTime fromDate, DateTime toDate);
List<SaleDataModel> GetAllSalesByProductByPeriod(string productId, DateTime fromDate, DateTime toDate);
SaleDataModel GetSaleByData(string data);
void InsertSale(SaleDataModel saleDataModel);
void CancelSale(string id);
}

View File

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

View File

@@ -0,0 +1,40 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using ThreeFatMenContracts.Exceptions;
using ThreeFatMenContracts.Extensions;
using ThreeFatMenContracts.Infrastructure;
namespace ThreeFatMenContracts.DataModels;
public class BuyerDataModel(string id, string fio, string phoneNumber, double discountSize) : IValidation
{
public string Id { get; private set; } = id;
public string FIO { get; private set; } = fio;
public string PhoneNumber { get; private set; } = phoneNumber;
public double DiscountSize { get; private set; } = discountSize;
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 (FIO.IsEmpty())
throw new ValidationException("Field FIO is Empty");
if (PhoneNumber.IsEmpty())
throw new ValidationException("Field PhoneNumber is Empty");
if (!Regex.IsMatch(PhoneNumber, @"^((8|\+7)[\- ]?)?(\(?\d{3}\)?[\- ]?)?[\d\- ]{7,10}$"))
throw new ValidationException("Field PhoneNumber is not a phone number");
}
}

View File

@@ -0,0 +1,34 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ThreeFatMenContracts.Enums;
using ThreeFatMenContracts.Exceptions;
using ThreeFatMenContracts.Extensions;
using ThreeFatMenContracts.Infrastructure;
namespace ThreeFatMenContracts.DataModels;
public class LunchDataModel(string id, string lunchName, IngredientType ingredientType, double price, bool isDeleted) : IValidation
{
public string Id { get; private set; } = id;
public string LunchName { get; private set; } = lunchName;
public IngredientType IngredientType { get; private set; } = ingredientType;
public double Price { get; private set; } = price;
public bool IsDeleted { get; private set; } = isDeleted;
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 (LunchName.IsEmpty())
throw new ValidationException("Field LucnhName is empty");
if (IngredientType == IngredientType.None)
throw new ValidationException("Field Ingredients is empty");
if (Price <= 0)
throw new ValidationException("Field Price is less than or equal to 0");
}
}

View File

@@ -0,0 +1,26 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ThreeFatMenContracts.Exceptions;
using ThreeFatMenContracts.Extensions;
using ThreeFatMenContracts.Infrastructure;
namespace ThreeFatMenContracts.DataModels;
public class LunchHistoryDataModel(string lunchId, double oldPrice) : IValidation
{
public string LunchId { get; private set; } = lunchId;
public double OldPrice { get; private set; } = oldPrice;
public DateTime ChangeDate { get; private set; } = DateTime.UtcNow;
public void Validate()
{
if (LunchId.IsEmpty())
throw new ValidationException("Field DishId is empty");
if (!LunchId.IsGuid())
throw new ValidationException("The value in the field DishId is not a unique identifier");
if (OldPrice <= 0)
throw new ValidationException("Field OldPrice is less than or equal to 0");
}
}

View File

@@ -0,0 +1,32 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ThreeFatMenContracts.Enums;
using ThreeFatMenContracts.Exceptions;
using ThreeFatMenContracts.Extensions;
using ThreeFatMenContracts.Infrastructure;
namespace ThreeFatMenContracts.DataModels;
public class PostDataModel(string id, string postName, PostType postType, bool isActual, DateTime changeDate) : IValidation
{
public string Id { get; private set; } = id;
public string PostName { get; private set; } = postName;
public PostType PostType { get; private set; } = postType;
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");
}
}

View File

@@ -0,0 +1,56 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ThreeFatMenContracts.Enums;
using ThreeFatMenContracts.Exceptions;
using ThreeFatMenContracts.Extensions;
using ThreeFatMenContracts.Infrastructure;
namespace ThreeFatMenContracts.DataModels;
public class SaleDataModel(string id, string workerId, string? buyerId, double sum, DiscountType discountType, double discount, bool isCancel, List<SaleLunchDataModel> lunches) : IValidation
{
public string Id { get; private set; } = id;
public string WorkerId { get; private set; } = workerId;
public string? BuyerId { get; private set; } = buyerId;
public DateTime SaleDate { get; private set; } = DateTime.UtcNow;
public double Sum { get; private set; } = sum;
public DiscountType DiscountType { get; private set; } = discountType;
public double Discount { get; private set; } = discount;
public bool IsCancel { get; private set; } = isCancel;
public List<SaleLunchDataModel> Lunches { get; private set; } = lunches;
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 (WorkerId.IsEmpty())
throw new ValidationException("Field EmployeeId is empty");
if (!WorkerId.IsGuid())
throw new ValidationException("The value in the field EmployeeId is not a unique identifier");
if (!BuyerId?.IsGuid() ?? !BuyerId?.IsEmpty() ?? false)
throw new ValidationException("The value in the field BuyerId is not a unique identifier");
if (Sum <= 0)
throw new ValidationException("Field Sum is less than or equal to 0");
if ((Lunches?.Count ?? 0) == 0)
throw new ValidationException("The sale must include cocktails");
}
}

View File

@@ -0,0 +1,37 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ThreeFatMenContracts.Exceptions;
using ThreeFatMenContracts.Extensions;
using ThreeFatMenContracts.Infrastructure;
namespace ThreeFatMenContracts.DataModels;
public class SaleLunchDataModel(string saleId, string lunchId, int count) : IValidation
{
public string SaleId { get; private set; } = saleId;
public string LunchId { get; private set; } = lunchId;
public int Count { get; private set; } = count;
public void Validate()
{
if (SaleId.IsEmpty())
throw new ValidationException("Field SaleId is empty");
if (!SaleId.IsGuid())
throw new ValidationException("The value in the field SaleId is not a unique identifier");
if (LunchId.IsEmpty())
throw new ValidationException("Field ProductId is empty");
if (!LunchId.IsGuid())
throw new ValidationException("The value in the field ProductId is not a unique identifier");
if (Count <= 0)
throw new ValidationException("Field Count is less than or equal to 0");
}
}

View File

@@ -0,0 +1,42 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ThreeFatMenContracts.Exceptions;
using ThreeFatMenContracts.Extensions;
using ThreeFatMenContracts.Infrastructure;
namespace ThreeFatMenContracts.DataModels;
public class WorkerDataModel(string id, string fio, string postId, DateTime birthDate, DateTime employmentDate, bool isDeleted) : IValidation
{
public string Id { get; private set; } = id;
public string FIO { get; private set; } = fio;
public string PostId { get; private set; } = postId;
public DateTime BirthDate { get; private set; } = birthDate;
public DateTime EmploymentDate { get; private set; } = employmentDate;
public bool IsDeleted { get; private set; } = isDeleted;
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 (FIO.IsEmpty())
throw new ValidationException("Field FIO is empty");
if (PostId.IsEmpty())
throw new ValidationException("Field PostId is empty");
if (!PostId.IsGuid())
throw new ValidationException("The value in the field PostId is not a unique identifier");
if (BirthDate.Date > DateTime.Now.AddYears(-16).Date)
throw new ValidationException($"Minors cannot be hired (BirthDate = {BirthDate.ToShortDateString()})");
if (EmploymentDate.Date < BirthDate.Date)
throw new ValidationException("The date of employment cannot be less than the date of birth");
if ((EmploymentDate - BirthDate).TotalDays / 365 < 16) // EmploymentDate.Year - BirthDate.Year
throw new ValidationException($"Minors cannot be hired (EmploymentDate - {EmploymentDate.ToShortDateString()}, BirthDate - {BirthDate.ToShortDateString()})");
}
}

View File

@@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ThreeFatMenContracts.Enums;
[Flags]
public enum DiscountType
{
None = 0,
OnSale = 1,
RegularCustomer = 2,
Certificate = 4
}

View File

@@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ThreeFatMenContracts.Enums;
[Flags]
public enum IngredientType
{
None = 0,
Meat = 1,
Fish = 2,
Vegetables = 4
}

View File

@@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ThreeFatMenContracts.Enums;
public enum PostType
{
None = 0,
Administrator = 1,
Cook = 2,
Waiter = 3,
}

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@@ -0,0 +1,11 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ThreeFatMenContracts.Exceptions;
public class ValidationException(string message) : Exception(message)
{
}

View File

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

View File

@@ -0,0 +1,20 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ThreeFatMenContracts.Extensions;
public static class StringExtensions
{
public static bool IsEmpty(this string str)
{
return string.IsNullOrWhiteSpace(str);
}
public static bool IsGuid(this string str)
{
return Guid.TryParse(str, out _);
}
}

View File

@@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ThreeFatMenContracts.Infrastructure;
public interface IValidation
{
void Validate();
}

View File

@@ -0,0 +1,19 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ThreeFatMenContracts.DataModels;
namespace ThreeFatMenContracts.StoragesContracts;
public interface IBuyerStorageContract
{
List<BuyerDataModel> GetList();
BuyerDataModel? GetElementById(string id);
BuyerDataModel? GetElementByPhoneNumber(string phoneNumber);
BuyerDataModel? GetElementByFIO(string fio);
void AddElement(BuyerDataModel buyerDataModel);
void UpdElement(BuyerDataModel buyerDataModel);
void DelElement(string id);
}

View File

@@ -0,0 +1,25 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ThreeFatMenContracts.DataModels;
namespace ThreeFatMenContracts.StoragesContracts;
public interface ILunchStorageContract
{
List<LunchDataModel> GetList(bool onlyActive = true);
List<LunchHistoryDataModel> GetHistoryByLunchId(string lunchId);
LunchDataModel? GetElementById(string id);
LunchDataModel? GetElementByName(string name);
void AddElement(LunchDataModel lunchDataModel);
void UpdElement(LunchDataModel lunchDataModel);
void DelElement(string id);
}

View File

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

View File

@@ -0,0 +1,21 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ThreeFatMenContracts.DataModels;
namespace ThreeFatMenContracts.StoragesContracts;
public interface ISaleStorageContract
{
List<SaleDataModel> GetList(DateTime? startDate = null, DateTime? endDate = null, string? workerId = null, string? buyerId = null, string? lunchId = null);
SaleDataModel? GetElementById(string id);
void AddElement(SaleDataModel saleDataModel);
void DelElement(string id);
}

View File

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

View File

@@ -1,10 +1,14 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.12.35707.178 d17.12
VisualStudioVersion = 17.12.35707.178
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ThreeFatMenContracts", "ThreeFatMenContracts\ThreeFatMenContracts.csproj", "{DE3F0625-08C6-4A14-BF98-745334E1D5AB}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ThreeFatMenTests", "ThreeFatMenTests\ThreeFatMenTests.csproj", "{A37EA18A-E569-4528-ADCC-EE1EC74523A8}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ThreeFatMenBusinessLogic", "ThreeFatMenBusinessLogic\ThreeFatMenBusinessLogic.csproj", "{12309C22-2123-4AFC-A601-8EDA2BCE9603}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@@ -15,6 +19,14 @@ Global
{DE3F0625-08C6-4A14-BF98-745334E1D5AB}.Debug|Any CPU.Build.0 = Debug|Any CPU
{DE3F0625-08C6-4A14-BF98-745334E1D5AB}.Release|Any CPU.ActiveCfg = Release|Any CPU
{DE3F0625-08C6-4A14-BF98-745334E1D5AB}.Release|Any CPU.Build.0 = Release|Any CPU
{A37EA18A-E569-4528-ADCC-EE1EC74523A8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A37EA18A-E569-4528-ADCC-EE1EC74523A8}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A37EA18A-E569-4528-ADCC-EE1EC74523A8}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A37EA18A-E569-4528-ADCC-EE1EC74523A8}.Release|Any CPU.Build.0 = Release|Any CPU
{12309C22-2123-4AFC-A601-8EDA2BCE9603}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{12309C22-2123-4AFC-A601-8EDA2BCE9603}.Debug|Any CPU.Build.0 = Debug|Any CPU
{12309C22-2123-4AFC-A601-8EDA2BCE9603}.Release|Any CPU.ActiveCfg = Release|Any CPU
{12309C22-2123-4AFC-A601-8EDA2BCE9603}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE

View File

@@ -0,0 +1,355 @@
using Microsoft.Extensions.Logging;
using Moq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ThreeFatMenBusinessLogic.Implementations;
using ThreeFatMenContracts.BusinessLogicsContracts;
using ThreeFatMenContracts.DataModels;
using ThreeFatMenContracts.Exceptions;
using ThreeFatMenContracts.StoragesContracts;
namespace ThreeFatMenTests.BusinessLogicsContractsTests;
[TestFixture]
internal class BuyerBusinessLogicContractTests
{
private IBuyerBusinessLogicContract _buyerBusinessLogicContract;
private Mock<IBuyerStorageContract> _buyerStorageContract;
[OneTimeSetUp]
public void OneTimeSetUp()
{
_buyerStorageContract = new Mock<IBuyerStorageContract>();
_buyerBusinessLogicContract = new BuyerBusinessLogicContract(_buyerStorageContract.Object, new Mock<ILogger>().Object);
}
[SetUp]
public void SetUp()
{
_buyerStorageContract.Reset();
}
[Test]
public void GetAllBuyers_ReturnListOfRecords_Test()
{
//Arrange
var listOriginal = new List<BuyerDataModel>()
{
new(Guid.NewGuid().ToString(), "fio 1", "+7-111-111-11-11", 0),
new(Guid.NewGuid().ToString(), "fio 2", "+7-555-444-33-23", 10),
new(Guid.NewGuid().ToString(), "fio 3", "+7-777-777-7777", 0),};
_buyerStorageContract.Setup(x => x.GetList()).Returns(listOriginal);
//Act
var list = _buyerBusinessLogicContract.GetAllBuyers();
//Assert
Assert.That(list, Is.Not.Null);
Assert.That(list, Is.EquivalentTo(listOriginal));
}
[Test]
public void GetAllBuyers_ReturnEmptyList_Test()
{
//Arrange
_buyerStorageContract.Setup(x => x.GetList()).Returns([]);
//Act
var list = _buyerBusinessLogicContract.GetAllBuyers();
//Assert
Assert.That(list, Is.Not.Null);
Assert.That(list, Has.Count.EqualTo(0));
_buyerStorageContract.Verify(x => x.GetList(), Times.Once);
}
[Test]
public void GetAllBuyers_ReturnNull_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _buyerBusinessLogicContract.GetAllBuyers(),Throws.TypeOf<NullListException>());
_buyerStorageContract.Verify(x => x.GetList(), Times.Once);
}
[Test]
public void GetAllBuyers_StorageThrowError_ThrowException_Test()
{
//Arrange
_buyerStorageContract.Setup(x => x.GetList()).Throws(new StorageException(new InvalidOperationException()));
//Act&Assert
Assert.That(() => _buyerBusinessLogicContract.GetAllBuyers(),Throws.TypeOf<StorageException>());
_buyerStorageContract.Verify(x => x.GetList(), Times.Once);
}
[Test]
public void GetBuyerByData_GetById_ReturnRecord_Test()
{
//Arrange
var id = Guid.NewGuid().ToString();
var record = new BuyerDataModel(id, "fio", "+7-111-111-11-11", 0);
_buyerStorageContract.Setup(x => x.GetElementById(id)).Returns(record);
//Act
var element = _buyerBusinessLogicContract.GetBuyerByData(id);
//Assert
Assert.That(element, Is.Not.Null);
Assert.That(element.Id, Is.EqualTo(id));
_buyerStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Once);
}
[Test]
public void GetBuyerByData_GetByFio_ReturnRecord_Test()
{
//Arrange
var fio = "fio";
var record = new BuyerDataModel(Guid.NewGuid().ToString(), fio, "+7 - 111 - 111 - 11 - 11", 0);
_buyerStorageContract.Setup(x =>x.GetElementByFIO(fio)).Returns(record);
//Act
var element = _buyerBusinessLogicContract.GetBuyerByData(fio);
//Assert
Assert.That(element, Is.Not.Null);
Assert.That(element.FIO, Is.EqualTo(fio));
_buyerStorageContract.Verify(x =>x.GetElementByFIO(It.IsAny<string>()), Times.Once);
}
[Test]
public void GetBuyerByData_GetByPhoneNumber_ReturnRecord_Test()
{
//Arrange
var phoneNumber = "+7-111-111-11-11";
var record = new BuyerDataModel(Guid.NewGuid().ToString(), "fio",phoneNumber, 0);
_buyerStorageContract.Setup(x =>x.GetElementByPhoneNumber(phoneNumber)).Returns(record);
//Act
var element = _buyerBusinessLogicContract.GetBuyerByData(phoneNumber);
//Assert
Assert.That(element, Is.Not.Null);
Assert.That(element.PhoneNumber, Is.EqualTo(phoneNumber));
_buyerStorageContract.Verify(x => x.GetElementByPhoneNumber(It.IsAny<string>()), Times.Once);
}
[Test]
public void GetBuyerByData_EmptyData_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _buyerBusinessLogicContract.GetBuyerByData(null), Throws.TypeOf<ArgumentNullException>());
Assert.That(() => _buyerBusinessLogicContract.GetBuyerByData(string.Empty), Throws.TypeOf<ArgumentNullException>());
_buyerStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Never);
_buyerStorageContract.Verify(x => x.GetElementByPhoneNumber(It.IsAny<string>()), Times.Never);
_buyerStorageContract.Verify(x => x.GetElementByFIO(It.IsAny<string>()), Times.Never);
}
[Test]
public void GetBuyerByData_GetById_NotFoundRecord_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _buyerBusinessLogicContract.GetBuyerByData(Guid.NewGuid().ToString()), Throws.TypeOf<ElementNotFoundException>());
_buyerStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Once);
_buyerStorageContract.Verify(x =>x.GetElementByPhoneNumber(It.IsAny<string>()), Times.Never);
_buyerStorageContract.Verify(x =>x.GetElementByFIO(It.IsAny<string>()), Times.Never);
}
[Test]
public void GetBuyerByData_GetByFio_NotFoundRecord_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _buyerBusinessLogicContract.GetBuyerByData("fio"), Throws.TypeOf<ElementNotFoundException>());
_buyerStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Never);
_buyerStorageContract.Verify(x =>x.GetElementByFIO(It.IsAny<string>()), Times.Once);
_buyerStorageContract.Verify(x =>x.GetElementByPhoneNumber(It.IsAny<string>()), Times.Never);
}
[Test]
public void
GetBuyerByData_GetByPhoneNumber_NotFoundRecord_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _buyerBusinessLogicContract.GetBuyerByData("+7-111-111-11-12"), Throws.TypeOf<ElementNotFoundException>());
_buyerStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Never);
_buyerStorageContract.Verify(x => x.GetElementByFIO(It.IsAny<string>()), Times.Never);
_buyerStorageContract.Verify(x => x.GetElementByPhoneNumber(It.IsAny<string>()), Times.Once);
}
[Test]
public void GetBuyerByData_StorageThrowError_ThrowException_Test()
{
//Arrange
_buyerStorageContract.Setup(x => x.GetElementById(It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException()));
_buyerStorageContract.Setup(x => x.GetElementByFIO(It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException()));
_buyerStorageContract.Setup(x => x.GetElementByPhoneNumber(It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException()));
//Act&Assert
Assert.That(() => _buyerBusinessLogicContract.GetBuyerByData(Guid.NewGuid().ToString()),Throws.TypeOf<StorageException>());
Assert.That(() => _buyerBusinessLogicContract.GetBuyerByData("Фамилия Имя Отчество"),Throws.TypeOf<StorageException>());
Assert.That(() => _buyerBusinessLogicContract.GetBuyerByData("+7-111-111-11-12"), Throws.TypeOf<StorageException>());
_buyerStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Once);
_buyerStorageContract.Verify(x => x.GetElementByFIO(It.IsAny<string>()), Times.Once);
_buyerStorageContract.Verify(x => x.GetElementByPhoneNumber(It.IsAny<string>()), Times.Once);
}
[Test]
public void InsertBuyer_CorrectRecord_Test()
{
//Arrange
var flag = false;
var record = new BuyerDataModel(Guid.NewGuid().ToString(), "Фамилия Имя Отчество", "+7-111-111-11-11", 10);
_buyerStorageContract.Setup(x => x.AddElement(It.IsAny<BuyerDataModel>())).Callback((BuyerDataModel x) =>
{
flag = x.Id == record.Id && x.FIO == record.FIO && x.PhoneNumber == record.PhoneNumber && x.DiscountSize == record.DiscountSize;
});
//Act
_buyerBusinessLogicContract.InsertBuyer(record);
//Assert
_buyerStorageContract.Verify(x => x.AddElement(It.IsAny<BuyerDataModel>()), Times.Once);
Assert.That(flag);
}
[Test]
public void InsertBuyer_RecordWithExistsData_ThrowException_Test()
{
//Arrange
_buyerStorageContract.Setup(x => x.AddElement(It.IsAny<BuyerDataModel>())).Throws(new ElementExistsException("Data", "Data"));
//Act&Assert
Assert.That(() => _buyerBusinessLogicContract.InsertBuyer(new(Guid.NewGuid().ToString(), "Фамилия Имя Отчество", "+7-111-111-11-11", 0)), Throws.TypeOf<ElementExistsException>());
_buyerStorageContract.Verify(x => x.AddElement(It.IsAny<BuyerDataModel>()), Times.Once);
}
[Test]
public void InsertBuyer_NullRecord_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _buyerBusinessLogicContract.InsertBuyer(null),
Throws.TypeOf<ArgumentNullException>());
_buyerStorageContract.Verify(x => x.AddElement(It.IsAny<BuyerDataModel>()), Times.Never);
}
[Test]
public void InsertBuyer_InvalidRecord_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _buyerBusinessLogicContract.InsertBuyer(new BuyerDataModel("id", "fio", "+7-111-111-11-11", 10)),
Throws.TypeOf<ValidationException>());
_buyerStorageContract.Verify(x => x.AddElement(It.IsAny<BuyerDataModel>()), Times.Never);
}
[Test]
public void InsertBuyer_StorageThrowError_ThrowException_Test()
{
//Arrange
_buyerStorageContract.Setup(x => x.AddElement(It.IsAny<BuyerDataModel>())).Throws(new StorageException(new InvalidOperationException()));
//Act&Assert
Assert.That(() =>_buyerBusinessLogicContract.InsertBuyer(new(Guid.NewGuid().ToString(), "Фамилия Имя Отчество", "+7-111-111-11-11", 0)), Throws.TypeOf<StorageException>());
_buyerStorageContract.Verify(x =>x.AddElement(It.IsAny<BuyerDataModel>()), Times.Once);
}
[Test]
public void UpdateBuyer_CorrectRecord_Test()
{
//Arrange
var flag = false;
var record = new BuyerDataModel(Guid.NewGuid().ToString(), "Фамилия Имя Отчество", "+7-111-111-11-11", 0);
_buyerStorageContract.Setup(x => x.UpdElement(It.IsAny<BuyerDataModel>()))
.Callback((BuyerDataModel x) =>
{
flag = x.Id == record.Id && x.FIO == record.FIO && x.PhoneNumber == record.PhoneNumber && x.DiscountSize == record.DiscountSize;
});
//Act
_buyerBusinessLogicContract.UpdateBuyer(record);
//Assert
_buyerStorageContract.Verify(x => x.UpdElement(It.IsAny<BuyerDataModel>()), Times.Once);
Assert.That(flag);
}
[Test]
public void UpdateBuyer_RecordWithIncorrectData_ThrowException_Test()
{
//Arrange
_buyerStorageContract.Setup(x => x.UpdElement(It.IsAny<BuyerDataModel>())).Throws(new
ElementNotFoundException(""));
//Act&Assert
Assert.That(() => _buyerBusinessLogicContract.UpdateBuyer(new(Guid.NewGuid().ToString(), "Фамилия Имя Отчество", "+7-111-111-11-11", 0)), Throws.TypeOf<ElementNotFoundException>());
_buyerStorageContract.Verify(x => x.UpdElement(It.IsAny<BuyerDataModel>()), Times.Once);
}
[Test]
public void UpdateBuyer_RecordWithExistsData_ThrowException_Test()
{
//Arrange
_buyerStorageContract.Setup(x => x.UpdElement(It.IsAny<BuyerDataModel>())).Throws(new ElementExistsException("Data", "Data"));
//Act&Assert
Assert.That(() =>_buyerBusinessLogicContract.UpdateBuyer(new(Guid.NewGuid().ToString(), "Фамилия Имя Отчество", "+7-111-111-11-11", 0)), Throws.TypeOf<ElementExistsException>());
_buyerStorageContract.Verify(x => x.UpdElement(It.IsAny<BuyerDataModel>()), Times.Once);
}
[Test]
public void UpdateBuyer_NullRecord_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _buyerBusinessLogicContract.UpdateBuyer(null),
Throws.TypeOf<ArgumentNullException>());
_buyerStorageContract.Verify(x => x.UpdElement(It.IsAny<BuyerDataModel>()), Times.Never);
}
[Test]
public void UpdateBuyer_InvalidRecord_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _buyerBusinessLogicContract.UpdateBuyer(new BuyerDataModel("id", "fio", "+7-111-111-11-11", 10)),
Throws.TypeOf<ValidationException>());
_buyerStorageContract.Verify(x => x.UpdElement(It.IsAny<BuyerDataModel>()), Times.Never);
}
[Test]
public void UpdateBuyer_StorageThrowError_ThrowException_Test()
{
//Arrange
_buyerStorageContract.Setup(x =>x.UpdElement(It.IsAny<BuyerDataModel>())).Throws(new StorageException(new InvalidOperationException()));
//Act&Assert
Assert.That(() =>_buyerBusinessLogicContract.UpdateBuyer(new(Guid.NewGuid().ToString(), "Фамилия Имя Отчество", "+7-111-111-11-11", 0)), Throws.TypeOf<StorageException>());
_buyerStorageContract.Verify(x =>x.UpdElement(It.IsAny<BuyerDataModel>()), Times.Once);
}
[Test]
public void DeleteBuyer_CorrectRecord_Test()
{
//Arrange
var id = Guid.NewGuid().ToString();
var flag = false;
_buyerStorageContract.Setup(x => x.DelElement(It.Is((string x) => x == id))).Callback(() => { flag = true; });
//Act
_buyerBusinessLogicContract.DeleteBuyer(id);
//Assert
_buyerStorageContract.Verify(x => x.DelElement(It.IsAny<string>()),Times.Once);
Assert.That(flag);
}
[Test]
public void DeleteBuyer_RecordWithIncorrectId_ThrowException_Test()
{
//Arrange
_buyerStorageContract.Setup(x => x.DelElement(It.IsAny<string>())).Throws(new ElementNotFoundException(""));
//Act&Assert
Assert.That(() => _buyerBusinessLogicContract.DeleteBuyer(Guid.NewGuid().ToString()),
Throws.TypeOf<ElementNotFoundException>());
_buyerStorageContract.Verify(x => x.DelElement(It.IsAny<string>()),Times.Once);
}
[Test]
public void DeleteBuyer_IdIsNullOrEmpty_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _buyerBusinessLogicContract.DeleteBuyer(null),
Throws.TypeOf<ArgumentNullException>());
Assert.That(() =>
_buyerBusinessLogicContract.DeleteBuyer(string.Empty),
Throws.TypeOf<ArgumentNullException>());
_buyerStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Never);
}
[Test]
public void DeleteBuyer_IdIsNotGuid_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _buyerBusinessLogicContract.DeleteBuyer("id"),
Throws.TypeOf<ValidationException>());
_buyerStorageContract.Verify(x => x.DelElement(It.IsAny<string>()),Times.Never);
}
[Test]
public void DeleteBuyer_StorageThrowError_ThrowException_Test()
{
//Arrange
_buyerStorageContract.Setup(x => x.DelElement(It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException()));
//Act&Assert
Assert.That(() => _buyerBusinessLogicContract.DeleteBuyer(Guid.NewGuid().ToString()),
Throws.TypeOf<StorageException>());
_buyerStorageContract.Verify(x => x.DelElement(It.IsAny<string>()),Times.Once);
}
}

View File

@@ -0,0 +1,446 @@
using Microsoft.Extensions.Logging;
using Moq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ThreeFatMenBusinessLogic.Implementations;
using ThreeFatMenContracts.BusinessLogicsContracts;
using ThreeFatMenContracts.DataModels;
using ThreeFatMenContracts.Enums;
using ThreeFatMenContracts.Exceptions;
using ThreeFatMenContracts.StoragesContracts;
namespace ThreeFatMenTests.BusinessLogicsContractsTests;
[TestFixture]
internal class LunchBusinessLogicContractTests
{
private ILunchBusinessLogicContract _lunchBusinessLogicContract;
private Mock<ILunchStorageContract> _lunchStorageContract;
private Mock<ILogger> _logger;
[OneTimeSetUp]
public void OneTimeSetUp()
{
_lunchStorageContract = new Mock<ILunchStorageContract>();
_logger = new Mock<ILogger>();
_lunchBusinessLogicContract = new LunchBusinessLogicContract(_lunchStorageContract.Object, _logger.Object);
}
[SetUp]
public void SetUp()
{
_lunchStorageContract.Reset();
}
[Test]
public void GetAllLunches_ReturnListOfRecords_Test()
{
// Arrange
var listOriginal = new List<LunchDataModel>
{
new(Guid.NewGuid().ToString(), "Lunch 1", IngredientType.Fish, 10.0, false),
new(Guid.NewGuid().ToString(), "Lunch 2", IngredientType.Fish, 10.0, true),
new(Guid.NewGuid().ToString(), "Lunch 3", IngredientType.Fish, 20.0, false),
};
// Ensure the mock returns the correct list based on the parameter
_lunchStorageContract.Setup(x => x.GetList(true)).Returns(listOriginal.Where(l => !l.IsDeleted).ToList());
_lunchStorageContract.Setup(x => x.GetList(false)).Returns(listOriginal);
// Act
var listOnlyActive = _lunchBusinessLogicContract.GetAllLunches(true);
var list = _lunchBusinessLogicContract.GetAllLunches(false);
// Assert
Assert.Multiple(() =>
{
Assert.That(listOnlyActive, Is.Not.Null);
Assert.That(list, Is.Not.Null);
Assert.That(listOnlyActive, Is.EquivalentTo(listOriginal.Where(l => !l.IsDeleted)));
Assert.That(list, Is.EquivalentTo(listOriginal));
});
_lunchStorageContract.Verify(x => x.GetList(true), Times.Once);
_lunchStorageContract.Verify(x => x.GetList(false), Times.Once);
}
[Test]
public void GetAllLunches_ReturnEmptyList_Test()
{
// Arrange
_lunchStorageContract.Setup(x => x.GetList(It.IsAny<bool>())).Returns(new List<LunchDataModel>());
// Act
var listOnlyActive = _lunchBusinessLogicContract.GetAllLunches(true);
var list = _lunchBusinessLogicContract.GetAllLunches(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));
});
_lunchStorageContract.Verify(x => x.GetList(It.IsAny<bool>()), Times.Exactly(2));
}
[Test]
public void GetAllLunches_ReturnNull_ThrowException_Test()
{
// Arrange
_lunchStorageContract.Setup(x => x.GetList(It.IsAny<bool>())).Returns((List<LunchDataModel>)null);
// Act & Assert
Assert.That(() => _lunchBusinessLogicContract.GetAllLunches(It.IsAny<bool>()), Throws.TypeOf<NullListException>());
_lunchStorageContract.Verify(x => x.GetList(It.IsAny<bool>()), Times.Once);
}
[Test]
public void GetAllLunches_StorageThrowError_ThrowException_Test()
{
// Arrange
_lunchStorageContract.Setup(x => x.GetList(It.IsAny<bool>())).Throws(new StorageException(new InvalidOperationException()));
// Act & Assert
Assert.That(() => _lunchBusinessLogicContract.GetAllLunches(It.IsAny<bool>()), Throws.TypeOf<StorageException>());
_lunchStorageContract.Verify(x => x.GetList(It.IsAny<bool>()), Times.Once);
}
[Test]
public void GetLunchHistoryByLunch_ReturnListOfRecords_Test()
{
// Arrange
var lunchId = Guid.NewGuid().ToString();
var listOriginal = new List<LunchHistoryDataModel>
{
new(lunchId, 10.0),
new(lunchId, 15.0),
new(lunchId, 20.0),
};
_lunchStorageContract.Setup(x => x.GetHistoryByLunchId(It.IsAny<string>())).Returns(listOriginal);
// Act
var list = _lunchBusinessLogicContract.GetLunchHistoryByLunch(lunchId);
// Assert
Assert.That(list, Is.Not.Null);
Assert.That(list, Is.EquivalentTo(listOriginal));
_lunchStorageContract.Verify(x => x.GetHistoryByLunchId(lunchId), Times.Once);
}
[Test]
public void GetLunchHistoryByLunch_ReturnEmptyList_Test()
{
// Arrange
_lunchStorageContract.Setup(x => x.GetHistoryByLunchId(It.IsAny<string>())).Returns(new List<LunchHistoryDataModel>());
// Act
var list = _lunchBusinessLogicContract.GetLunchHistoryByLunch(Guid.NewGuid().ToString());
// Assert
Assert.That(list, Is.Not.Null);
Assert.That(list, Has.Count.EqualTo(0));
_lunchStorageContract.Verify(x => x.GetHistoryByLunchId(It.IsAny<string>()), Times.Once);
}
[Test]
public void GetLunchHistoryByLunch_LunchIdIsNullOrEmpty_ThrowException_Test()
{
// Act & Assert
Assert.That(() => _lunchBusinessLogicContract.GetLunchHistoryByLunch(null), Throws.TypeOf<ArgumentNullException>());
Assert.That(() => _lunchBusinessLogicContract.GetLunchHistoryByLunch(string.Empty), Throws.TypeOf<ArgumentNullException>());
_lunchStorageContract.Verify(x => x.GetHistoryByLunchId(It.IsAny<string>()), Times.Never);
}
[Test]
public void GetLunchHistoryByLunch_LunchIdIsNotGuid_ThrowException_Test()
{
// Act & Assert
Assert.That(() => _lunchBusinessLogicContract.GetLunchHistoryByLunch("lunchId"), Throws.TypeOf<ValidationException>());
_lunchStorageContract.Verify(x => x.GetHistoryByLunchId(It.IsAny<string>()), Times.Never);
}
[Test]
public void GetLunchHistoryByLunch_ReturnNull_ThrowException_Test()
{
// Arrange
_lunchStorageContract.Setup(x => x.GetHistoryByLunchId(It.IsAny<string>())).Returns((List<LunchHistoryDataModel>)null);
// Act & Assert
Assert.That(() => _lunchBusinessLogicContract.GetLunchHistoryByLunch(Guid.NewGuid().ToString()), Throws.TypeOf<NullListException>());
_lunchStorageContract.Verify(x => x.GetHistoryByLunchId(It.IsAny<string>()), Times.Once);
}
[Test]
public void GetLunchHistoryByLunch_StorageThrowError_ThrowException_Test()
{
// Arrange
_lunchStorageContract.Setup(x => x.GetHistoryByLunchId(It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException()));
// Act & Assert
Assert.That(() => _lunchBusinessLogicContract.GetLunchHistoryByLunch(Guid.NewGuid().ToString()), Throws.TypeOf<StorageException>());
_lunchStorageContract.Verify(x => x.GetHistoryByLunchId(It.IsAny<string>()), Times.Once);
}
[Test]
public void GetLunchByData_GetById_ReturnRecord_Test()
{
// Arrange
var id = Guid.NewGuid().ToString();
var record = new LunchDataModel(id, "LunchName", IngredientType.Fish, 10.0, false);
_lunchStorageContract.Setup(x => x.GetElementById(id)).Returns(record);
// Act
var element = _lunchBusinessLogicContract.GetLunchByData(id);
// Assert
Assert.That(element, Is.Not.Null);
Assert.That(element.Id, Is.EqualTo(id));
_lunchStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Once);
}
[Test]
public void GetLunchByData_GetByName_ReturnRecord_Test()
{
// Arrange
var name = "LunchName";
var record = new LunchDataModel(Guid.NewGuid().ToString(), name, IngredientType.Fish, 10.0, false);
_lunchStorageContract.Setup(x => x.GetElementByName(name)).Returns(record);
// Act
var element = _lunchBusinessLogicContract.GetLunchByData(name);
// Assert
Assert.That(element, Is.Not.Null);
Assert.That(element.LunchName, Is.EqualTo(name));
_lunchStorageContract.Verify(x => x.GetElementByName(It.IsAny<string>()), Times.Once);
}
[Test]
public void GetLunchByData_EmptyData_ThrowException_Test()
{
// Act & Assert
Assert.That(() => _lunchBusinessLogicContract.GetLunchByData(null), Throws.TypeOf<ArgumentNullException>());
Assert.That(() => _lunchBusinessLogicContract.GetLunchByData(string.Empty), Throws.TypeOf<ArgumentNullException>());
_lunchStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Never);
_lunchStorageContract.Verify(x => x.GetElementByName(It.IsAny<string>()), Times.Never);
}
[Test]
public void GetLunchByData_GetById_NotFoundRecord_ThrowException_Test()
{
// Act & Assert
Assert.That(() => _lunchBusinessLogicContract.GetLunchByData(Guid.NewGuid().ToString()), Throws.TypeOf<ElementNotFoundException>());
_lunchStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Once);
}
[Test]
public void GetLunchByData_GetByName_NotFoundRecord_ThrowException_Test()
{
// Act & Assert
Assert.That(() => _lunchBusinessLogicContract.GetLunchByData("LunchName"), Throws.TypeOf<ElementNotFoundException>());
_lunchStorageContract.Verify(x => x.GetElementByName(It.IsAny<string>()), Times.Once);
}
[Test]
public void GetLunchByData_StorageThrowError_ThrowException_Test()
{
// Arrange
_lunchStorageContract.Setup(x => x.GetElementById(It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException()));
_lunchStorageContract.Setup(x => x.GetElementByName(It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException()));
// Act & Assert
Assert.That(() => _lunchBusinessLogicContract.GetLunchByData(Guid.NewGuid().ToString()), Throws.TypeOf<StorageException>());
Assert.That(() => _lunchBusinessLogicContract.GetLunchByData("LunchName"), Throws.TypeOf<StorageException>());
_lunchStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Once);
_lunchStorageContract.Verify(x => x.GetElementByName(It.IsAny<string>()), Times.Once);
}
[Test]
public void InsertLunch_CorrectRecord_Test()
{
// Arrange
var flag = false;
var record = new LunchDataModel(Guid.NewGuid().ToString(), "LunchName", IngredientType.Fish, 10.0, false);
_lunchStorageContract.Setup(x => x.AddElement(It.IsAny<LunchDataModel>()))
.Callback((LunchDataModel x) =>
{
flag = x.Id == record.Id && x.LunchName == record.LunchName && x.IngredientType == record.IngredientType && x.Price == record.Price && x.IsDeleted == record.IsDeleted;
});
// Act
_lunchBusinessLogicContract.InsertLunch(record);
// Assert
_lunchStorageContract.Verify(x => x.AddElement(It.IsAny<LunchDataModel>()), Times.Once);
Assert.That(flag);
}
[Test]
public void InsertLunch_RecordWithExistsData_ThrowException_Test()
{
// Arrange
_lunchStorageContract.Setup(x => x.AddElement(It.IsAny<LunchDataModel>())).Throws(new ElementExistsException("Data", "Data"));
// Act & Assert
Assert.That(() => _lunchBusinessLogicContract.InsertLunch(new LunchDataModel(Guid.NewGuid().ToString(), "LunchName", IngredientType.Fish, 10.0, false)), Throws.TypeOf<ElementExistsException>());
_lunchStorageContract.Verify(x => x.AddElement(It.IsAny<LunchDataModel>()), Times.Once);
}
[Test]
public void InsertLunch_NullRecord_ThrowException_Test()
{
// Act & Assert
Assert.That(() => _lunchBusinessLogicContract.InsertLunch(null), Throws.TypeOf<ArgumentNullException>());
_lunchStorageContract.Verify(x => x.AddElement(It.IsAny<LunchDataModel>()), Times.Never);
}
[Test]
public void InsertLunch_InvalidRecord_ThrowException_Test()
{
// Act & Assert
Assert.That(() => _lunchBusinessLogicContract.InsertLunch(new LunchDataModel("id", "LunchName", IngredientType.Fish, 10.0, false)), Throws.TypeOf<ValidationException>());
_lunchStorageContract.Verify(x => x.AddElement(It.IsAny<LunchDataModel>()), Times.Never);
}
[Test]
public void InsertLunch_StorageThrowError_ThrowException_Test()
{
// Arrange
_lunchStorageContract.Setup(x => x.AddElement(It.IsAny<LunchDataModel>())).Throws(new StorageException(new InvalidOperationException()));
// Act & Assert
Assert.That(() => _lunchBusinessLogicContract.InsertLunch(new LunchDataModel(Guid.NewGuid().ToString(), "LunchName", IngredientType.Fish, 10.0, false)), Throws.TypeOf<StorageException>());
_lunchStorageContract.Verify(x => x.AddElement(It.IsAny<LunchDataModel>()), Times.Once);
}
[Test]
public void UpdateLunch_CorrectRecord_Test()
{
// Arrange
var flag = false;
var record = new LunchDataModel(Guid.NewGuid().ToString(), "LunchName", IngredientType.Fish, 10.0, false);
_lunchStorageContract.Setup(x => x.UpdElement(It.IsAny<LunchDataModel>()))
.Callback((LunchDataModel x) =>
{
flag = x.Id == record.Id && x.LunchName == record.LunchName && x.IngredientType == record.IngredientType && x.Price == record.Price && x.IsDeleted == record.IsDeleted;
});
// Act
_lunchBusinessLogicContract.UpdateLunch(record);
// Assert
_lunchStorageContract.Verify(x => x.UpdElement(It.IsAny<LunchDataModel>()), Times.Once);
Assert.That(flag);
}
[Test]
public void UpdateLunch_RecordWithIncorrectData_ThrowException_Test()
{
// Arrange
_lunchStorageContract.Setup(x => x.UpdElement(It.IsAny<LunchDataModel>())).Throws(new ElementNotFoundException(""));
// Act & Assert
Assert.That(() => _lunchBusinessLogicContract.UpdateLunch(new LunchDataModel(Guid.NewGuid().ToString(), "LunchName", IngredientType.Fish, 10.0, false)), Throws.TypeOf<ElementNotFoundException>());
_lunchStorageContract.Verify(x => x.UpdElement(It.IsAny<LunchDataModel>()), Times.Once);
}
[Test]
public void UpdateLunch_RecordWithExistsData_ThrowException_Test()
{
// Arrange
_lunchStorageContract.Setup(x => x.UpdElement(It.IsAny<LunchDataModel>())).Throws(new ElementExistsException("Data", "Data"));
// Act & Assert
Assert.That(() => _lunchBusinessLogicContract.UpdateLunch(new LunchDataModel(Guid.NewGuid().ToString(), "LunchName", IngredientType.Fish, 10.0, false)), Throws.TypeOf<ElementExistsException>());
_lunchStorageContract.Verify(x => x.UpdElement(It.IsAny<LunchDataModel>()), Times.Once);
}
[Test]
public void UpdateLunch_NullRecord_ThrowException_Test()
{
// Act & Assert
Assert.That(() => _lunchBusinessLogicContract.UpdateLunch(null), Throws.TypeOf<ArgumentNullException>());
_lunchStorageContract.Verify(x => x.UpdElement(It.IsAny<LunchDataModel>()), Times.Never);
}
[Test]
public void UpdateLunch_InvalidRecord_ThrowException_Test()
{
// Act & Assert
Assert.That(() => _lunchBusinessLogicContract.UpdateLunch(new LunchDataModel("id", "LunchName", IngredientType.Fish, 10.0, false)), Throws.TypeOf<ValidationException>());
_lunchStorageContract.Verify(x => x.UpdElement(It.IsAny<LunchDataModel>()), Times.Never);
}
[Test]
public void UpdateLunch_StorageThrowError_ThrowException_Test()
{
// Arrange
_lunchStorageContract.Setup(x => x.UpdElement(It.IsAny<LunchDataModel>())).Throws(new StorageException(new InvalidOperationException()));
// Act & Assert
Assert.That(() => _lunchBusinessLogicContract.UpdateLunch(new LunchDataModel(Guid.NewGuid().ToString(), "LunchName", IngredientType.Fish, 10.0, false)), Throws.TypeOf<StorageException>());
_lunchStorageContract.Verify(x => x.UpdElement(It.IsAny<LunchDataModel>()), Times.Once);
}
[Test]
public void DeleteLunch_CorrectRecord_Test()
{
// Arrange
var id = Guid.NewGuid().ToString();
var flag = false;
_lunchStorageContract.Setup(x => x.DelElement(It.Is<string>(x => x == id))).Callback(() => { flag = true; });
// Act
_lunchBusinessLogicContract.DeleteLunch(id);
// Assert
_lunchStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Once);
Assert.That(flag);
}
[Test]
public void DeleteLunch_RecordWithIncorrectId_ThrowException_Test()
{
// Arrange
var id = Guid.NewGuid().ToString();
_lunchStorageContract.Setup(x => x.DelElement(It.IsAny<string>())).Throws(new ElementNotFoundException(id));
// Act & Assert
Assert.That(() => _lunchBusinessLogicContract.DeleteLunch(Guid.NewGuid().ToString()), Throws.TypeOf<ElementNotFoundException>());
_lunchStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Once);
}
[Test]
public void DeleteLunch_IdIsNullOrEmpty_ThrowException_Test()
{
// Act & Assert
Assert.That(() => _lunchBusinessLogicContract.DeleteLunch(null), Throws.TypeOf<ArgumentNullException>());
Assert.That(() => _lunchBusinessLogicContract.DeleteLunch(string.Empty), Throws.TypeOf<ArgumentNullException>());
_lunchStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Never);
}
[Test]
public void DeleteLunch_IdIsNotGuid_ThrowException_Test()
{
// Act & Assert
Assert.That(() => _lunchBusinessLogicContract.DeleteLunch("id"), Throws.TypeOf<ValidationException>());
_lunchStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Never);
}
[Test]
public void DeleteLunch_StorageThrowError_ThrowException_Test()
{
// Arrange
_lunchStorageContract.Setup(x => x.DelElement(It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException()));
// Act & Assert
Assert.That(() => _lunchBusinessLogicContract.DeleteLunch(Guid.NewGuid().ToString()), Throws.TypeOf<StorageException>());
_lunchStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Once);
}
}

View File

@@ -0,0 +1,471 @@
using Microsoft.Extensions.Logging;
using Moq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ThreeFatMenBusinessLogic.Implementations;
using ThreeFatMenContracts.BuisnessLogicsContracts;
using ThreeFatMenContracts.DataModels;
using ThreeFatMenContracts.Enums;
using ThreeFatMenContracts.Exceptions;
using ThreeFatMenContracts.StoragesContracts;
namespace ThreeFatMenTests.BusinessLogicsContractsTests;
[TestFixture]
public 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);
}
[TearDown]
public void TearDown()
{
_postStorageContract.Reset();
}
[Test]
public void GetAllPosts_ReturnListOfRecords_Test()
{
//Arrange
var listOriginal = new List<PostDataModel>()
{
new(Guid.NewGuid().ToString(),"name 1", PostType.Cook, true, DateTime.UtcNow),
new(Guid.NewGuid().ToString(), "name 2", PostType.Cook, true, DateTime.UtcNow),
new(Guid.NewGuid().ToString(), "name 3", PostType.Cook, 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.Cook, true, DateTime.UtcNow),
new(postId, "name 2", PostType.Cook, true, 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.Cook, 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.Cook, 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.Cook, 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.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.Cook, 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.Cook, 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.Cook, 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.Cook, true, DateTime.UtcNow);
_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.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.Cook, 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(), "name", PostType.Cook, 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.Cook, 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.Cook, true, DateTime.UtcNow)), Throws.TypeOf<StorageException>());
_postStorageContract.Verify(x => x.UpdElement(It.IsAny<PostDataModel>()), Times.Once);
}
[Test]
public void DeletePost_CorrectRecord_Test()
{
//Arrange
var id = Guid.NewGuid().ToString();
var flag = false;
_postStorageContract.Setup(x => x.DelElement(It.Is((string x) => x == id))).Callback(() => { flag = true; });
//Act
_postBusinessLogicContract.DeletePost(id);
//Assert
_postStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Once);
Assert.That(flag);
}
[Test]
public void DeletePost_RecordWithIncorrectId_ThrowException_Test()
{
//Arrange
var id = Guid.NewGuid().ToString();
_postStorageContract.Setup(x => x.DelElement(It.IsAny<string>())).Throws(new ElementNotFoundException(id));
//Act&Assert
Assert.That(() => _postBusinessLogicContract.DeletePost(Guid.NewGuid().ToString()), Throws.TypeOf<ElementNotFoundException>());
_postStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Once);
}
[Test]
public void DeletePost_IdIsNullOrEmpty_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _postBusinessLogicContract.DeletePost(null), Throws.TypeOf<ArgumentNullException>());
Assert.That(() => _postBusinessLogicContract.DeletePost(string.Empty), Throws.TypeOf<ArgumentNullException>());
_postStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Never);
}
[Test]
public void DeletePost_IdIsNotGuid_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _postBusinessLogicContract.DeletePost("id"), Throws.TypeOf<ValidationException>());
_postStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Never);
}
[Test]
public void DeletePost_StorageThrowError_ThrowException_Test()
{
//Arrange
_postStorageContract.Setup(x => x.DelElement(It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException()));
//Act&Assert
Assert.That(() => _postBusinessLogicContract.DeletePost(Guid.NewGuid().ToString()), Throws.TypeOf<StorageException>());
_postStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Once);
}
[Test]
public void RestorePost_CorrectRecord_Test()
{
//Arrange
var id = Guid.NewGuid().ToString();
var flag = false;
_postStorageContract.Setup(x => x.ResElement(It.Is((string x) => x == id))).Callback(() => { flag = true; });
//Act
_postBusinessLogicContract.RestorePost(id);
//Assert
_postStorageContract.Verify(x => x.ResElement(It.IsAny<string>()), Times.Once);
Assert.That(flag);
}
[Test]
public void RestorePost_RecordWithIncorrectId_ThrowException_Test()
{
//Arrange
var id = Guid.NewGuid().ToString();
_postStorageContract.Setup(x => x.ResElement(It.IsAny<string>())).Throws(new ElementNotFoundException(id));
//Act&Assert
Assert.That(() => _postBusinessLogicContract.RestorePost(Guid.NewGuid().ToString()), Throws.TypeOf<ElementNotFoundException>());
_postStorageContract.Verify(x => x.ResElement(It.IsAny<string>()), Times.Once);
}
[Test]
public void RestorePost_IdIsNullOrEmpty_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _postBusinessLogicContract.RestorePost(null), Throws.TypeOf<ArgumentNullException>());
Assert.That(() => _postBusinessLogicContract.RestorePost(string.Empty), Throws.TypeOf<ArgumentNullException>());
_postStorageContract.Verify(x => x.ResElement(It.IsAny<string>()), Times.Never);
}
[Test]
public void RestorePost_IdIsNotGuid_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _postBusinessLogicContract.RestorePost("id"), Throws.TypeOf<ValidationException>());
_postStorageContract.Verify(x => x.ResElement(It.IsAny<string>()), Times.Never);
}
[Test]
public void RestorePost_StorageThrowError_ThrowException_Test()
{
//Arrange
_postStorageContract.Setup(x => x.ResElement(It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException()));
//Act&Assert
Assert.That(() => _postBusinessLogicContract.RestorePost(Guid.NewGuid().ToString()), Throws.TypeOf<StorageException>());
_postStorageContract.Verify(x => x.ResElement(It.IsAny<string>()), Times.Once);
}
}

View File

@@ -0,0 +1,626 @@
using Microsoft.Extensions.Logging;
using Moq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ThreeFatMenBusinessLogic.Implementations;
using ThreeFatMenContracts.DataModels;
using ThreeFatMenContracts.Enums;
using ThreeFatMenContracts.Exceptions;
using ThreeFatMenContracts.StoragesContracts;
namespace ThreeFatMenTests.BusinessLogicsContractsTests;
[TestFixture]
internal class SaleBusinessLogicContractTests
{
private SaleBusinessLogicContract _saleBusinessLogicContract;
private Mock<ISaleStorageContract> _saleStorageContract;
[OneTimeSetUp]
public void OneTimeSetUp()
{
_saleStorageContract = new Mock<ISaleStorageContract>();
_saleBusinessLogicContract = new SaleBusinessLogicContract(_saleStorageContract.Object, new Mock<ILogger>().Object);
}
[SetUp]
public void SetUp()
{
_saleStorageContract.Reset();
}
[Test]
public void GetAllSalesByPeriod_ReturnListOfRecords_Test()
{
//Arrange
var date = DateTime.UtcNow;
var listOriginal = new List<SaleDataModel>()
{
new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), null, 10, DiscountType.None ,10, false,
[new SaleLunchDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 300)]),
new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), null, 10, DiscountType.None ,10, false, []),
new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), null, 10, DiscountType.None, 10, false, []),
};
_saleStorageContract.Setup(x => x.GetList(It.IsAny<DateTime?>(),It.IsAny<DateTime?>(), It.IsAny<string>(), It.IsAny<string>(),It.IsAny<string>())).Returns(listOriginal);
//Act
var list = _saleBusinessLogicContract.GetAllSalesByPeriod(date,date.AddDays(1));
//Assert
Assert.That(list, Is.Not.Null);
Assert.That(list, Is.EquivalentTo(listOriginal));
_saleStorageContract.Verify(x => x.GetList(date, date.AddDays(1),null, null, null), Times.Once);
}
[Test]
public void GetAllSalesByPeriod_ReturnEmptyList_Test()
{
//Arrange
_saleStorageContract.Setup(x => x.GetList(It.IsAny<DateTime?>(),
It.IsAny<DateTime?>(), It.IsAny<string>(), It.IsAny<string>(),
It.IsAny<string>())).Returns([]);
//Act
var list =
_saleBusinessLogicContract.GetAllSalesByPeriod(DateTime.UtcNow, DateTime.UtcNow.AddDays(1));
//Assert
Assert.That(list, Is.Not.Null);
Assert.That(list, Has.Count.EqualTo(0));
_saleStorageContract.Verify(x => x.GetList(It.IsAny<DateTime?>(),
It.IsAny<DateTime?>(), It.IsAny<string>(), It.IsAny<string>(),
It.IsAny<string>()), Times.Once);
}
[Test]
public void GetAllSalesByPeriod_IncorrectDates_ThrowException_Test()
{
//Arrange
var date = DateTime.UtcNow;
//Act&Assert
Assert.That(() =>
_saleBusinessLogicContract.GetAllSalesByPeriod(date, date),
Throws.TypeOf<IncorrectDatesException>());
Assert.That(() =>
_saleBusinessLogicContract.GetAllSalesByPeriod(date, date.AddSeconds(-1)),
Throws.TypeOf<IncorrectDatesException>());
_saleStorageContract.Verify(x => x.GetList(It.IsAny<DateTime?>(),
It.IsAny<DateTime?>(), It.IsAny<string>(), It.IsAny<string>(),It.IsAny<string>()), Times.Never);
}
[Test]
public void GetAllSalesByPeriod_ReturnNull_ThrowException_Test()
{
//Act&Assert
Assert.That(() =>
_saleBusinessLogicContract.GetAllSalesByPeriod(DateTime.UtcNow,
DateTime.UtcNow.AddDays(1)), Throws.TypeOf<NullListException>());
_saleStorageContract.Verify(x => x.GetList(It.IsAny<DateTime?>(),
It.IsAny<DateTime?>(), It.IsAny<string>(), It.IsAny<string>(),It.IsAny<string>()), Times.Once);
}
[Test]
public void GetAllSalesByPeriod_StorageThrowError_ThrowException_Test()
{
//Arrange
_saleStorageContract.Setup(x => x.GetList(It.IsAny<DateTime?>(),
It.IsAny<DateTime?>(), It.IsAny<string>(), It.IsAny<string>(),
It.IsAny<string>())).Throws(new StorageException(new
InvalidOperationException()));
//Act&Assert
Assert.That(() =>
_saleBusinessLogicContract.GetAllSalesByPeriod(DateTime.UtcNow,
DateTime.UtcNow.AddDays(1)), Throws.TypeOf<StorageException>());
_saleStorageContract.Verify(x => x.GetList(It.IsAny<DateTime?>(),
It.IsAny<DateTime?>(), It.IsAny<string>(), 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<SaleDataModel>()
{
new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), null, 10, DiscountType.None, 10, false,
[new SaleLunchDataModel(Guid.NewGuid().ToString(),Guid.NewGuid().ToString(), 300)]), new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), null, 10, DiscountType.None,10, false, []),
new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), null, 10, DiscountType.None, 10, false, []),
};
_saleStorageContract.Setup(x => x.GetList(It.IsAny<DateTime?>(),
It.IsAny<DateTime?>(), It.IsAny<string>(), It.IsAny<string>(),
It.IsAny<string>())).Returns(listOriginal);
//Act
var list =
_saleBusinessLogicContract.GetAllSalesByWorkerByPeriod(workerId, date,
date.AddDays(1));
//Assert
Assert.That(list, Is.Not.Null);
Assert.That(list, Is.EquivalentTo(listOriginal));
_saleStorageContract.Verify(x => x.GetList(date, date.AddDays(1),
workerId, null, null), Times.Once);
}
[Test]
public void GetAllSalesByWorkerByPeriod_ReturnEmptyList_Test()
{
//Arrange
_saleStorageContract.Setup(x => x.GetList(It.IsAny<DateTime?>(),
It.IsAny<DateTime?>(), It.IsAny<string>(), It.IsAny<string>(),
It.IsAny<string>())).Returns([]);
//Act
var list =
_saleBusinessLogicContract.GetAllSalesByWorkerByPeriod(Guid.NewGuid().ToString(),
DateTime.UtcNow, DateTime.UtcNow.AddDays(1));
//Assert
Assert.That(list, Is.Not.Null);
Assert.That(list, Has.Count.EqualTo(0));
_saleStorageContract.Verify(x => x.GetList(It.IsAny<DateTime?>(),
It.IsAny<DateTime?>(), It.IsAny<string>(), It.IsAny<string>(),
It.IsAny<string>()), Times.Once);
}
[Test]
public void
GetAllSalesByWorkerByPeriod_IncorrectDates_ThrowException_Test()
{
//Arrange
var date = DateTime.UtcNow;
//Act&Assert
Assert.That(() =>
_saleBusinessLogicContract.GetAllSalesByWorkerByPeriod(Guid.NewGuid().ToString(),date, date), Throws.TypeOf<IncorrectDatesException>());
Assert.That(() =>
_saleBusinessLogicContract.GetAllSalesByWorkerByPeriod(Guid.NewGuid().ToString(),date, date.AddSeconds(-1)), Throws.TypeOf<IncorrectDatesException>());
_saleStorageContract.Verify(x => x.GetList(It.IsAny<DateTime?>(),
It.IsAny<DateTime?>(), It.IsAny<string>(), It.IsAny<string>(),
It.IsAny<string>()), Times.Never);
}
[Test]
public void
GetAllSalesByWorkerByPeriod_WorkerIdIsNullOrEmpty_ThrowException_Test()
{
//Act&Assert
Assert.That(() =>
_saleBusinessLogicContract.GetAllSalesByWorkerByPeriod(null, DateTime.UtcNow,
DateTime.UtcNow.AddDays(1)), Throws.TypeOf<ArgumentNullException>());
Assert.That(() =>
_saleBusinessLogicContract.GetAllSalesByWorkerByPeriod(string.Empty,
DateTime.UtcNow, DateTime.UtcNow.AddDays(1)),
Throws.TypeOf<ArgumentNullException>());
_saleStorageContract.Verify(x => x.GetList(It.IsAny<DateTime?>(),
It.IsAny<DateTime?>(), It.IsAny<string>(), It.IsAny<string>(),
It.IsAny<string>()), Times.Never);
}
[Test]
public void
GetAllSalesByWorkerByPeriod_WorkerIdIsNotGuid_ThrowException_Test()
{
//Act&Assert
Assert.That(() =>
_saleBusinessLogicContract.GetAllSalesByWorkerByPeriod("workerId", DateTime.UtcNow, DateTime.UtcNow.AddDays(1)),
Throws.TypeOf<ValidationException>());
_saleStorageContract.Verify(x => x.GetList(It.IsAny<DateTime?>(),
It.IsAny<DateTime?>(), It.IsAny<string>(), It.IsAny<string>(),
It.IsAny<string>()), Times.Never);
}
[Test]
public void GetAllSalesByWorkerByPeriod_ReturnNull_ThrowException_Test()
{
//Act&Assert
Assert.That(() =>_saleBusinessLogicContract.GetAllSalesByWorkerByPeriod(Guid.NewGuid().ToString(),
DateTime.UtcNow, DateTime.UtcNow.AddDays(1)),
Throws.TypeOf<NullListException>());
_saleStorageContract.Verify(x => x.GetList(It.IsAny<DateTime?>(),
It.IsAny<DateTime?>(), It.IsAny<string>(), It.IsAny<string>(),
It.IsAny<string>()), Times.Once);
}
[Test]
public void
GetAllSalesByWorkerByPeriod_StorageThrowError_ThrowException_Test()
{
//Arrange
_saleStorageContract.Setup(x => x.GetList(It.IsAny<DateTime?>(),
It.IsAny<DateTime?>(), It.IsAny<string>(), It.IsAny<string>(),
It.IsAny<string>())).Throws(new StorageException(new
InvalidOperationException()));
//Act&Assert
Assert.That(() =>
_saleBusinessLogicContract.GetAllSalesByWorkerByPeriod(Guid.NewGuid().ToString(),
DateTime.UtcNow, DateTime.UtcNow.AddDays(1)), Throws.TypeOf<StorageException>());
_saleStorageContract.Verify(x => x.GetList(It.IsAny<DateTime?>(),
It.IsAny<DateTime?>(), It.IsAny<string>(), It.IsAny<string>(),
It.IsAny<string>()), Times.Once);
}
[Test]
public void GetAllSalesByBuyerByPeriod_ReturnListOfRecords_Test()
{
//Arrange
var date = DateTime.UtcNow;
var buyerId = Guid.NewGuid().ToString();
var listOriginal = new List<SaleDataModel>()
{
new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), null, 10, DiscountType.None, 10, false,
[new SaleLunchDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 300)]),
new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), null, 10, DiscountType.None, 10, false, []),
new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), null, 10, DiscountType.None, 10, false, []),
};
_saleStorageContract.Setup(x => x.GetList(It.IsAny<DateTime?>(),
It.IsAny<DateTime?>(), It.IsAny<string>(), It.IsAny<string>(),
It.IsAny<string>())).Returns(listOriginal);
//Act
var list =
_saleBusinessLogicContract.GetAllSalesByBuyerByPeriod(buyerId, date, date.AddDays(1));
//Assert
Assert.That(list, Is.Not.Null);
Assert.That(list, Is.EquivalentTo(listOriginal));
_saleStorageContract.Verify(x => x.GetList(date, date.AddDays(1),
null, buyerId, null), Times.Once);
}
[Test]
public void GetAllSalesByBuyerByPeriod_ReturnEmptyList_Test()
{
//Arrange
_saleStorageContract.Setup(x => x.GetList(It.IsAny<DateTime?>(),
It.IsAny<DateTime?>(), It.IsAny<string>(), It.IsAny<string>(),
It.IsAny<string>())).Returns([]);
//Act
var list =
_saleBusinessLogicContract.GetAllSalesByBuyerByPeriod(Guid.NewGuid().ToString(),
DateTime.UtcNow, DateTime.UtcNow.AddDays(1));
//Assert
Assert.That(list, Is.Not.Null);
Assert.That(list, Has.Count.EqualTo(0));
_saleStorageContract.Verify(x => x.GetList(It.IsAny<DateTime?>(),It.IsAny<DateTime?>(), It.IsAny<string>(), It.IsAny<string>(),It.IsAny<string>()), Times.Once);
}
[Test]
public void GetAllSalesByBuyerByPeriod_IncorrectDates_ThrowException_Test()
{
//Arrange
var date = DateTime.UtcNow;
//Act&Assert
Assert.That(() =>
_saleBusinessLogicContract.GetAllSalesByBuyerByPeriod(Guid.NewGuid().ToString(),date, date), Throws.TypeOf<IncorrectDatesException>());
Assert.That(() =>
_saleBusinessLogicContract.GetAllSalesByBuyerByPeriod(Guid.NewGuid().ToString(), date, date.AddSeconds(-1)), Throws.TypeOf<IncorrectDatesException>());
_saleStorageContract.Verify(x => x.GetList(It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>()), Times.Never);
}
[Test]
public void
GetAllSalesByBuyerByPeriod_BuyerIdIsNullOrEmpty_ThrowException_Test()
{
//Act&Assert
Assert.That(() =>
_saleBusinessLogicContract.GetAllSalesByBuyerByPeriod(null, DateTime.UtcNow,
DateTime.UtcNow.AddDays(1)), Throws.TypeOf<ArgumentNullException>());
Assert.That(() =>
_saleBusinessLogicContract.GetAllSalesByBuyerByPeriod(string.Empty,
DateTime.UtcNow, DateTime.UtcNow.AddDays(1)),
Throws.TypeOf<ArgumentNullException>());
_saleStorageContract.Verify(x => x.GetList(It.IsAny<DateTime?>(),It.IsAny<DateTime?>(), It.IsAny<string>(), It.IsAny<string>(),It.IsAny<string>()), Times.Never);
}
[Test]
public void
GetAllSalesByBuyerByPeriod_BuyerIdIsNotGuid_ThrowException_Test()
{
//Act&Assert
Assert.That(() =>
_saleBusinessLogicContract.GetAllSalesByBuyerByPeriod("buyerId", DateTime.UtcNow,DateTime.UtcNow.AddDays(1)), Throws.TypeOf<ValidationException>());
_saleStorageContract.Verify(x => x.GetList(It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>()), Times.Never);
}
[Test]
public void GetAllSalesByBuyerByPeriod_ReturnNull_ThrowException_Test()
{
//Act&Assert
Assert.That(() =>
_saleBusinessLogicContract.GetAllSalesByBuyerByPeriod(Guid.NewGuid().ToString(),DateTime.UtcNow, DateTime.UtcNow.AddDays(1)),
Throws.TypeOf<NullListException>());
_saleStorageContract.Verify(x => x.GetList(It.IsAny<DateTime?>(),It.IsAny<DateTime?>(), It.IsAny<string>(), It.IsAny<string>(),It.IsAny<string>()), Times.Once);
}
[Test]
public void
GetAllSalesByBuyerByPeriod_StorageThrowError_ThrowException_Test()
{
//Arrange
_saleStorageContract.Setup(x => x.GetList(It.IsAny<DateTime?>(),
It.IsAny<DateTime?>(), It.IsAny<string>(), It.IsAny<string>(),
It.IsAny<string>())).Throws(new StorageException(new
InvalidOperationException()));
//Act&Assert
Assert.That(() =>
_saleBusinessLogicContract.GetAllSalesByBuyerByPeriod(Guid.NewGuid().ToString(),
DateTime.UtcNow, DateTime.UtcNow.AddDays(1)), Throws.TypeOf<StorageException>());
_saleStorageContract.Verify(x => x.GetList(It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<string>(), 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<SaleDataModel>()
{
new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), null, 10, DiscountType.None, 10, false,
[new SaleLunchDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 300)]),
new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), null, 10, DiscountType.None, 10, false, []),
new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), null, 10, DiscountType.None, 10, false, []),
};
_saleStorageContract.Setup(x => x.GetList(It.IsAny<DateTime?>(),
It.IsAny<DateTime?>(), It.IsAny<string>(), It.IsAny<string>(),
It.IsAny<string>())).Returns(listOriginal);
//Act
var list =
_saleBusinessLogicContract.GetAllSalesByProductByPeriod(productId, date, date.AddDays(1));
//Assert
Assert.That(list, Is.Not.Null);
Assert.That(list, Is.EquivalentTo(listOriginal));
_saleStorageContract.Verify(x => x.GetList(date, date.AddDays(1), null, null, productId), Times.Once);
}
[Test]
public void GetAllSalesByProductByPeriod_ReturnEmptyList_Test()
{
//Arrange
_saleStorageContract.Setup(x => x.GetList(It.IsAny<DateTime?>(),
It.IsAny<DateTime?>(), It.IsAny<string>(), It.IsAny<string>(),
It.IsAny<string>())).Returns([]);
//Act
var list =
_saleBusinessLogicContract.GetAllSalesByProductByPeriod(Guid.NewGuid().ToString()
, DateTime.UtcNow, DateTime.UtcNow.AddDays(1));
//Assert
Assert.That(list, Is.Not.Null);
Assert.That(list, Has.Count.EqualTo(0));
_saleStorageContract.Verify(x => x.GetList(It.IsAny<DateTime?>(),
It.IsAny<DateTime?>(), It.IsAny<string>(), It.IsAny<string>(),
It.IsAny<string>()), Times.Once);
}
[Test]
public void
GetAllSalesByProductByPeriod_IncorrectDates_ThrowException_Test()
{
//Arrange
var date = DateTime.UtcNow;
//Act&Assert
Assert.That(() =>
_saleBusinessLogicContract.GetAllSalesByProductByPeriod(Guid.NewGuid().ToString()
, date, date), Throws.TypeOf<IncorrectDatesException>());
Assert.That(() =>
_saleBusinessLogicContract.GetAllSalesByProductByPeriod(Guid.NewGuid().ToString()
, date, date.AddSeconds(-1)), Throws.TypeOf<IncorrectDatesException>());
_saleStorageContract.Verify(x => x.GetList(It.IsAny<DateTime?>(),
It.IsAny<DateTime?>(), It.IsAny<string>(), It.IsAny<string>(),
It.IsAny<string>()), Times.Never);
}
[Test]
public void
GetAllSalesByProductByPeriod_ProductIdIsNullOrEmpty_ThrowException_Test()
{
//Act&Assert
Assert.That(() =>
_saleBusinessLogicContract.GetAllSalesByProductByPeriod(null, DateTime.UtcNow,
DateTime.UtcNow.AddDays(1)), Throws.TypeOf<ArgumentNullException>());
Assert.That(() =>
_saleBusinessLogicContract.GetAllSalesByProductByPeriod(string.Empty,
DateTime.UtcNow, DateTime.UtcNow.AddDays(1)),
Throws.TypeOf<ArgumentNullException>());
_saleStorageContract.Verify(x => x.GetList(It.IsAny<DateTime?>(),
It.IsAny<DateTime?>(), It.IsAny<string>(), It.IsAny<string>(),
It.IsAny<string>()), Times.Never);
}
[Test]
public void
GetAllSalesByProductByPeriod_ProductIdIsNotGuid_ThrowException_Test()
{
//Act&Assert
Assert.That(() =>
_saleBusinessLogicContract.GetAllSalesByProductByPeriod("productId",
DateTime.UtcNow, DateTime.UtcNow.AddDays(1)),
Throws.TypeOf<ValidationException>());
_saleStorageContract.Verify(x => x.GetList(It.IsAny<DateTime?>(),
It.IsAny<DateTime?>(), It.IsAny<string>(), It.IsAny<string>(),
It.IsAny<string>()), Times.Never);
}
[Test]
public void GetAllSalesByProductByPeriod_ReturnNull_ThrowException_Test()
{
//Act&Assert
Assert.That(() =>
_saleBusinessLogicContract.GetAllSalesByProductByPeriod(Guid.NewGuid().ToString()
, DateTime.UtcNow, DateTime.UtcNow.AddDays(1)),
Throws.TypeOf<NullListException>());
_saleStorageContract.Verify(x => x.GetList(It.IsAny<DateTime?>(),
It.IsAny<DateTime?>(), It.IsAny<string>(), It.IsAny<string>(),
It.IsAny<string>()), Times.Once);
}
[Test]
public void
GetAllSalesByProductByPeriod_StorageThrowError_ThrowException_Test()
{
//Arrange
_saleStorageContract.Setup(x => x.GetList(It.IsAny<DateTime?>(),
It.IsAny<DateTime?>(), It.IsAny<string>(), It.IsAny<string>(),
It.IsAny<string>())).Throws(new StorageException(new
InvalidOperationException()));
//Act&Assert
Assert.That(() =>
_saleBusinessLogicContract.GetAllSalesByProductByPeriod(Guid.NewGuid().ToString()
, DateTime.UtcNow, DateTime.UtcNow.AddDays(1)),
Throws.TypeOf<StorageException>());
_saleStorageContract.Verify(x => x.GetList(It.IsAny<DateTime?>(),
It.IsAny<DateTime?>(), It.IsAny<string>(), It.IsAny<string>(),
It.IsAny<string>()), Times.Once);
}
[Test]
public void GetSaleByData_GetById_ReturnRecord_Test()
{
//Arrange
var id = Guid.NewGuid().ToString();
var record = new SaleDataModel(id, Guid.NewGuid().ToString(), null,10, DiscountType.None, 10, false,
[new SaleLunchDataModel(Guid.NewGuid().ToString(),Guid.NewGuid().ToString(), 300)]);
_saleStorageContract.Setup(x => x.GetElementById(id)).Returns(record);
//Act
var element = _saleBusinessLogicContract.GetSaleByData(id);
//Assert
Assert.That(element, Is.Not.Null);
Assert.That(element.Id, Is.EqualTo(id));
_saleStorageContract.Verify(x =>x.GetElementById(It.IsAny<string>()), Times.Once);
}
[Test]
public void GetSaleByData_EmptyData_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _saleBusinessLogicContract.GetSaleByData(null),
Throws.TypeOf<ArgumentNullException>());
Assert.That(() =>
_saleBusinessLogicContract.GetSaleByData(string.Empty),
Throws.TypeOf<ArgumentNullException>());
_saleStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Never);
}
[Test]
public void GetSaleByData_IdIsNotGuid_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _saleBusinessLogicContract.GetSaleByData("saleId"),
Throws.TypeOf<ValidationException>());
_saleStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Never);
}
[Test]
public void GetSaleByData_GetById_NotFoundRecord_ThrowException_Test()
{
//Act&Assert
Assert.That(() =>
_saleBusinessLogicContract.GetSaleByData(Guid.NewGuid().ToString()),
Throws.TypeOf<ElementNotFoundException>());
_saleStorageContract.Verify(x =>
x.GetElementById(It.IsAny<string>()), Times.Once);
}
[Test]
public void GetSaleByData_StorageThrowError_ThrowException_Test()
{
//Arrange
_saleStorageContract.Setup(x => x.GetElementById(It.IsAny<string>())).Throws(new StorageException(new
InvalidOperationException()));
//Act&Assert
Assert.That(() =>
_saleBusinessLogicContract.GetSaleByData(Guid.NewGuid().ToString()),
Throws.TypeOf<StorageException>());
_saleStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Once);
}
[Test]
public void InsertSale_CorrectRecord_Test()
{
//Arrange
var flag = false;
var record = new SaleDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 10, DiscountType.None, 10,
false, [new SaleLunchDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 300)]);
_saleStorageContract.Setup(x => x.AddElement(It.IsAny<SaleDataModel>()))
.Callback((SaleDataModel x) =>
{
flag = x.Id == record.Id && x.WorkerId == record.WorkerId && x.BuyerId == record.BuyerId && x.SaleDate == record.SaleDate && x.Sum ==record.Sum && x.IsCancel == record.IsCancel && x.Lunches.Count == record.Lunches.Count &&
x.Lunches.First().LunchId == record.Lunches.First().LunchId && x.Lunches.First().SaleId == record.Lunches.First().SaleId && x.Lunches.First().Count == record.Lunches.First().Count;
});
//Act
_saleBusinessLogicContract.InsertSale(record);
//Assert
_saleStorageContract.Verify(x =>
x.AddElement(It.IsAny<SaleDataModel>()), Times.Once);
Assert.That(flag);
}
[Test]
public void InsertSale_RecordWithExistsData_ThrowException_Test()
{
//Arrange
_saleStorageContract.Setup(x =>
x.AddElement(It.IsAny<SaleDataModel>())).Throws(new ElementExistsException("Data", "Data"));
//Act&Assert
Assert.That(() =>
_saleBusinessLogicContract.InsertSale(new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 10, DiscountType.None, 10, false,[new SaleLunchDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)])), Throws.TypeOf<ElementExistsException>());
_saleStorageContract.Verify(x =>
x.AddElement(It.IsAny<SaleDataModel>()), Times.Once);
}
[Test]
public void InsertSale_NullRecord_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _saleBusinessLogicContract.InsertSale(null),
Throws.TypeOf<ArgumentNullException>());
_saleStorageContract.Verify(x =>
x.AddElement(It.IsAny<SaleDataModel>()), Times.Never);
}
[Test]
public void InsertSale_InvalidRecord_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _saleBusinessLogicContract.InsertSale(new SaleDataModel("id", Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 10, DiscountType.None, 10, false, [])), Throws.TypeOf<ValidationException>());
_saleStorageContract.Verify(x =>x.AddElement(It.IsAny<SaleDataModel>()), Times.Never);
}
[Test]
public void InsertSale_StorageThrowError_ThrowException_Test()
{
//Arrange
_saleStorageContract.Setup(x =>
x.AddElement(It.IsAny<SaleDataModel>())).Throws(new StorageException(new
InvalidOperationException()));
//Act&Assert
Assert.That(() =>
_saleBusinessLogicContract.InsertSale(new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(),Guid.NewGuid().ToString(), 10, DiscountType.None, 10, false,
[new SaleLunchDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 300)])), Throws.TypeOf<StorageException>());
_saleStorageContract.Verify(x => x.AddElement(It.IsAny<SaleDataModel>()), Times.Once);
}
[Test]
public void CancelSale_CorrectRecord_Test()
{
//Arrange
var id = Guid.NewGuid().ToString();
var flag = false;
_saleStorageContract.Setup(x => x.DelElement(It.Is((string x) => x ==
id))).Callback(() => { flag = true; });
//Act
_saleBusinessLogicContract.CancelSale(id);
//Assert
_saleStorageContract.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();
_saleStorageContract.Setup(x =>
x.DelElement(It.IsAny<string>())).Throws(new ElementNotFoundException(id));
//Act&Assert
Assert.That(() =>
_saleBusinessLogicContract.CancelSale(Guid.NewGuid().ToString()),
Throws.TypeOf<ElementNotFoundException>());
_saleStorageContract.Verify(x => x.DelElement(It.IsAny<string>()),
Times.Once);
}
[Test]
public void CancelSale_IdIsNullOrEmpty_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _saleBusinessLogicContract.CancelSale(null),
Throws.TypeOf<ArgumentNullException>());
Assert.That(() =>
_saleBusinessLogicContract.CancelSale(string.Empty),
Throws.TypeOf<ArgumentNullException>());
_saleStorageContract.Verify(x => x.DelElement(It.IsAny<string>()),
Times.Never);
}
[Test]
public void CancelSale_IdIsNotGuid_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _saleBusinessLogicContract.CancelSale("id"),
Throws.TypeOf<ValidationException>());
_saleStorageContract.Verify(x => x.DelElement(It.IsAny<string>()),
Times.Never);
}
[Test]
public void CancelSale_StorageThrowError_ThrowException_Test()
{
//Arrange
_saleStorageContract.Setup(x =>
x.DelElement(It.IsAny<string>())).Throws(new StorageException(new
InvalidOperationException()));
//Act&Assert
Assert.That(() =>
_saleBusinessLogicContract.CancelSale(Guid.NewGuid().ToString()),
Throws.TypeOf<StorageException>());
_saleStorageContract.Verify(x => x.DelElement(It.IsAny<string>()),
Times.Once);
}
}

View File

@@ -0,0 +1,691 @@
using Microsoft.Extensions.Logging;
using Moq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ThreeFatMenBusinessLogic.Implementations;
using ThreeFatMenContracts.DataModels;
using ThreeFatMenContracts.Exceptions;
using ThreeFatMenContracts.StoragesContracts;
namespace ThreeFatMenTests.BusinessLogicsContractsTests;
[TestFixture]
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(), "Фамилия Имя Отчество", Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now, false);
_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(), "Фамилия Имя Отчество",
Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now,
false)), 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)), 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(), "Фамилия Имя Отчество",
Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now, false)), 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(), "Фамилия Имя Отчество",
Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now,
false);
_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(), "Фамилия Имя Отчество",
Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now,
false)), 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)), 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(), "Фамилия Имя Отчество",
Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now, false)), Throws.TypeOf<StorageException>());
_workerStorageContract.Verify(x => x.UpdElement(It.IsAny<WorkerDataModel>()), Times.Once);
}
[Test]
public void DeleteWorker_CorrectRecord_Test()
{
//Arrange
var id = Guid.NewGuid().ToString();
var flag = false;
_workerStorageContract.Setup(x => x.DelElement(It.Is((string x) => x == id))).Callback(() => { flag = true; });
//Act
_workerBusinessLogicContract.DeleteWorker(id);
//Assert
_workerStorageContract.Verify(x => x.DelElement(It.IsAny<string>()),
Times.Once);
Assert.That(flag);
}
[Test]
public void DeleteWorker_RecordWithIncorrectId_ThrowException_Test()
{
//Arrange
var id = Guid.NewGuid().ToString();
_workerStorageContract.Setup(x => x.DelElement(It.Is((string x) => x!= id))).Throws(new ElementNotFoundException(id));
//Act&Assert
Assert.That(() =>
_workerBusinessLogicContract.DeleteWorker(Guid.NewGuid().ToString()),
Throws.TypeOf<ElementNotFoundException>());
_workerStorageContract.Verify(x => x.DelElement(It.IsAny<string>()),
Times.Once);
}
[Test]
public void DeleteWorker_IdIsNullOrEmpty_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _workerBusinessLogicContract.DeleteWorker(null),
Throws.TypeOf<ArgumentNullException>());
Assert.That(() =>
_workerBusinessLogicContract.DeleteWorker(string.Empty),
Throws.TypeOf<ArgumentNullException>());
_workerStorageContract.Verify(x => x.DelElement(It.IsAny<string>()),
Times.Never);
}
[Test]
public void DeleteWorker_IdIsNotGuid_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _workerBusinessLogicContract.DeleteWorker("id"),
Throws.TypeOf<ValidationException>());
_workerStorageContract.Verify(x => x.DelElement(It.IsAny<string>()),
Times.Never);
}
[Test]
public void DeleteWorker_StorageThrowError_ThrowException_Test()
{
//Arrange
_workerStorageContract.Setup(x => x.DelElement(It.IsAny<string>())).Throws(new StorageException(new
InvalidOperationException()));
//Act&Assert
Assert.That(() =>
_workerBusinessLogicContract.DeleteWorker(Guid.NewGuid().ToString()),
Throws.TypeOf<StorageException>());
_workerStorageContract.Verify(x => x.DelElement(It.IsAny<string>()),
Times.Once);
}
}

View File

@@ -0,0 +1,74 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ThreeFatMenContracts.DataModels;
using ThreeFatMenContracts.Exceptions;
namespace ThreeFatMenTests.DataModelsTest;
[TestFixture]
internal class BuyerDataModelTests
{
[Test]
public void IdIsNullOrEmptyTest()
{
var client = CreateDataModel(null, "fio", "number", 10);
Assert.That(() => client.Validate(), Throws.TypeOf<ValidationException>());
client = CreateDataModel(string.Empty, "fio", "number", 10);
Assert.That(() => client.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void IdIsNotGuidTest()
{
var client = CreateDataModel("id", "fio", "number", 10);
Assert.That(() => client.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void FIOIsNullOrEmptyTest()
{
var client = CreateDataModel(Guid.NewGuid().ToString(), null, "number", 10);
Assert.That(() => client.Validate(), Throws.TypeOf<ValidationException>());
client = CreateDataModel(Guid.NewGuid().ToString(), string.Empty, "number", 10);
Assert.That(() => client.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void PhoneNumberIsNullOrEmptyTest()
{
var client = CreateDataModel(Guid.NewGuid().ToString(), "fio", null, 10);
Assert.That(() => client.Validate(), Throws.TypeOf<ValidationException>());
client = CreateDataModel(Guid.NewGuid().ToString(), "fio", string.Empty, 10);
Assert.That(() => client.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void PhoneNumberIsIncorrectTest()
{
var client = CreateDataModel(Guid.NewGuid().ToString(), "fio", "777", 10);
Assert.That(() => client.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void AllFieldsIsCorrectTest()
{
var clientId = Guid.NewGuid().ToString();
var fio = "Fio";
var phoneNumber = "+7-777-777-77-77";
var discountSize = 11;
var buyer = CreateDataModel(clientId, fio, phoneNumber, discountSize);
Assert.That(() => buyer.Validate(), Throws.Nothing);
Assert.Multiple(() =>
{
Assert.That(buyer.Id, Is.EqualTo(clientId));
Assert.That(buyer.FIO, Is.EqualTo(fio));
Assert.That(buyer.PhoneNumber, Is.EqualTo(phoneNumber));
Assert.That(buyer.DiscountSize, Is.EqualTo(discountSize));
});
}
private static BuyerDataModel CreateDataModel(string? id, string? fio, string? phoneNumber, double discountSize) => new(id, fio, phoneNumber, discountSize);
}

View File

@@ -0,0 +1,79 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Intrinsics.X86;
using System.Text;
using System.Threading.Tasks;
using ThreeFatMenContracts.DataModels;
using ThreeFatMenContracts.Enums;
using ThreeFatMenContracts.Exceptions;
namespace ThreeFatMenTests.DataModelsTest;
[TestFixture]
internal class LunchDataModelTests
{
[Test]
public void IdIsNullOrEmptyTest()
{
var lunch = CreateDataModel(null, "name", 10, IngredientType.Meat, false);
Assert.That(() => lunch.Validate(), Throws.TypeOf<ValidationException>());
lunch = CreateDataModel(string.Empty, "name", 10, IngredientType.Meat, false);
Assert.That(() => lunch.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void IdIsNotGuidTest()
{
var lunch = CreateDataModel("id", "name", 10, IngredientType.Meat, false);
Assert.That(() => lunch.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void LunchlNameIsNullOrEmptyTest()
{
var lunch = CreateDataModel(Guid.NewGuid().ToString(), null, 10, IngredientType.Meat, false);
Assert.That(() => lunch.Validate(), Throws.TypeOf<ValidationException>());
lunch = CreateDataModel(Guid.NewGuid().ToString(), string.Empty, 10.5, IngredientType.Meat, false);
Assert.That(() => lunch.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void PriceIsLessOrZeroTest()
{
var lunch = CreateDataModel(Guid.NewGuid().ToString(), null, 0, IngredientType.Meat, false);
Assert.That(() => lunch.Validate(), Throws.TypeOf<ValidationException>());
lunch = CreateDataModel(Guid.NewGuid().ToString(), string.Empty, -10, IngredientType.Meat, false);
Assert.That(() => lunch.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void IngredientTypeIsNoneTest()
{
var lunch = CreateDataModel(Guid.NewGuid().ToString(), null, 0, IngredientType.None, false);
Assert.That(() => lunch.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void AllFieldsIsCorrectTest()
{
var lunchId = Guid.NewGuid().ToString();
var lunchName = "name";
var price = 10;
var ingredientType = IngredientType.Meat;
var lunchIsDelete = false;
var lunch = CreateDataModel(lunchId, lunchName, price, ingredientType, lunchIsDelete);
Assert.That(() => lunch.Validate(), Throws.Nothing);
Assert.Multiple(() =>
{
Assert.That(lunch.Id, Is.EqualTo(lunchId));
Assert.That(lunch.LunchName, Is.EqualTo(lunchName));
Assert.That(lunch.Price, Is.EqualTo(price));
Assert.That(lunch.IngredientType, Is.EqualTo(ingredientType));
Assert.That(lunch.IsDeleted, Is.EqualTo(lunchIsDelete));
});
}
private static LunchDataModel CreateDataModel(string? id, string? lunchName, double price, IngredientType ingredientType, bool isDeleted) => new(id, lunchName, ingredientType, price, isDeleted);
}

View File

@@ -0,0 +1,56 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Intrinsics.X86;
using System.Text;
using System.Threading.Tasks;
using ThreeFatMenContracts.DataModels;
using ThreeFatMenContracts.Exceptions;
namespace ThreeFatMenTests.DataModelsTest;
[TestFixture]
internal class LunchHistoryDataModelTests
{
[Test]
public void LunchIdIsNullOrEmptyTest()
{
var lunch = CreateDataModel(null, 10);
Assert.That(() => lunch.Validate(), Throws.TypeOf<ValidationException>());
lunch = CreateDataModel(string.Empty, 10);
Assert.That(() => lunch.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void LunchIdIsNotGuidTest()
{
var lunch = CreateDataModel("id", 10);
Assert.That(() => lunch.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void OldPriceIsLessOrZeroTest()
{
var lunch = CreateDataModel(Guid.NewGuid().ToString(), 0);
Assert.That(() => lunch.Validate(), Throws.TypeOf<ValidationException>());
lunch = CreateDataModel(Guid.NewGuid().ToString(), -10);
Assert.That(() => lunch.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void AllFieldsIsCorrectTest()
{
var lunchId = Guid.NewGuid().ToString();
var oldPrice = 10;
var lunchHistory = CreateDataModel(lunchId, oldPrice);
Assert.That(() => lunchHistory.Validate(), Throws.Nothing);
Assert.Multiple(() =>
{
Assert.That(lunchHistory.LunchId, Is.EqualTo(lunchId));
Assert.That(lunchHistory.OldPrice, Is.EqualTo(oldPrice));
Assert.That(lunchHistory.ChangeDate, Is.LessThan(DateTime.UtcNow));
Assert.That(lunchHistory.ChangeDate, Is.GreaterThan(DateTime.UtcNow.AddMinutes(-1)));
});
}
private static LunchHistoryDataModel CreateDataModel(string? lunchId, double oldPrice) =>new(lunchId, oldPrice);
}

View File

@@ -0,0 +1,66 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ThreeFatMenContracts.DataModels;
using ThreeFatMenContracts.Enums;
using ThreeFatMenContracts.Exceptions;
namespace ThreeFatMenTests.DataModelsTest;
[TestFixture]
internal class PostDataModelTests
{
[Test]
public void IdIsNullOrEmptyTest()
{
var post = CreateDataModel(null, "name", PostType.Cook, true, DateTime.UtcNow);
Assert.That(() => post.Validate(), Throws.TypeOf<ValidationException>());
post = CreateDataModel(string.Empty, "name", PostType.Cook, true, DateTime.UtcNow);
Assert.That(() => post.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void IdIsNotGuidTest()
{
var post = CreateDataModel("id", "name", PostType.Cook, true, DateTime.UtcNow);
Assert.That(() => post.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void PostNameIsEmptyTest()
{
var manufacturer = CreateDataModel(Guid.NewGuid().ToString(), null, PostType.Cook, true, DateTime.UtcNow);
Assert.That(() => manufacturer.Validate(), Throws.TypeOf<ValidationException>());
manufacturer = CreateDataModel(Guid.NewGuid().ToString(), string.Empty, PostType.Cook, true, DateTime.UtcNow);
Assert.That(() => manufacturer.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void PostTypeIsNoneTest()
{
var post = CreateDataModel(Guid.NewGuid().ToString(), "name", PostType.None, true, DateTime.UtcNow);
Assert.That(() => post.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void AllFieldsIsCorrectTest()
{
var postId = Guid.NewGuid().ToString();
var postName = "name";
var postType = PostType.Cook;
var isActual = false;
var changeDate = DateTime.UtcNow.AddDays(-1);
var post = CreateDataModel(postId, postName, postType, 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.IsActual, Is.EqualTo(isActual));
Assert.That(post.ChangeDate, Is.EqualTo(changeDate));
});
}
private static PostDataModel CreateDataModel(string? id, string? postName, PostType postType, bool isActual, DateTime changeDate) =>new(id, postName, postType, isActual, changeDate);
}

View File

@@ -0,0 +1,101 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ThreeFatMenContracts.DataModels;
using ThreeFatMenContracts.Enums;
using ThreeFatMenContracts.Exceptions;
namespace ThreeFatMenTests.DataModelsTest;
[TestFixture]
internal class SaleDataModelTests
{
[Test]
public void IdIsNullOrEmptyTest()
{
var sale = CreateDataModel(null, Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 10, DiscountType.OnSale, 10, false, CreateSubDataModel());
Assert.That(() => sale.Validate(), Throws.TypeOf<ValidationException>());
sale = CreateDataModel(string.Empty, Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 10, DiscountType.OnSale, 10, false, CreateSubDataModel());
Assert.That(() => sale.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void IdIsNotGuidTest()
{
var sale = CreateDataModel("id", Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 10, DiscountType.OnSale, 10, false, CreateSubDataModel());
Assert.That(() => sale.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void EmployeeIdIsNullOrEmptyTest()
{
var sale = CreateDataModel(Guid.NewGuid().ToString(), null, Guid.NewGuid().ToString(), 10, DiscountType.OnSale, 10, false, CreateSubDataModel());
Assert.That(() => sale.Validate(), Throws.TypeOf<ValidationException>());
sale = CreateDataModel(Guid.NewGuid().ToString(), string.Empty, Guid.NewGuid().ToString(), 10, DiscountType.OnSale, 10, false, CreateSubDataModel());
Assert.That(() => sale.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void EmployeeIdIsNotGuidTest()
{
var sale = CreateDataModel(Guid.NewGuid().ToString(), "employeeId", Guid.NewGuid().ToString(), 10, DiscountType.OnSale, 10, false, CreateSubDataModel());
Assert.That(() => sale.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void BuyerIdIsNotGuidTest()
{
var sale = CreateDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), "buyerId", 10, DiscountType.OnSale, 10, false, CreateSubDataModel());
Assert.That(() => sale.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void SumIsLessOrZeroTest()
{
var sale = CreateDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 0, DiscountType.OnSale, 10, false, CreateSubDataModel());
Assert.That(() => sale.Validate(), Throws.TypeOf<ValidationException>());
sale = CreateDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), -10, DiscountType.OnSale, 10, false, CreateSubDataModel());
Assert.That(() => sale.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void LunchIsNullOrEmptyTest()
{
var sale = CreateDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 10, DiscountType.OnSale, 10, false, null);
Assert.That(() => sale.Validate(), Throws.TypeOf<ValidationException>());
sale = CreateDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 10, DiscountType.OnSale, 10, false, []);
Assert.That(() => sale.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void AllFieldsIsCorrectTest()
{
var saleId = Guid.NewGuid().ToString();
var workerId = Guid.NewGuid().ToString();
var buyerId = Guid.NewGuid().ToString();
var sum = 10;
var discountType = DiscountType.Certificate;
var discount = 1;
var isCancel = true;
var lunches = CreateSubDataModel();
var sale = CreateDataModel(saleId, workerId, buyerId, sum, discountType, discount, isCancel, lunches);
Assert.That(() => sale.Validate(), Throws.Nothing);
Assert.Multiple(() =>
{
Assert.That(sale.Id, Is.EqualTo(saleId));
Assert.That(sale.WorkerId, Is.EqualTo(workerId));
Assert.That(sale.BuyerId, Is.EqualTo(buyerId));
Assert.That(sale.Sum, Is.EqualTo(sum));
Assert.That(sale.DiscountType, Is.EqualTo(discountType));
Assert.That(sale.Discount, Is.EqualTo(discount));
Assert.That(sale.IsCancel, Is.EqualTo(isCancel));
Assert.That(sale.Lunches, Is.EquivalentTo(lunches));
});
}
private static SaleDataModel CreateDataModel(string? id, string? workerId, string? buyerId, double sum, DiscountType discountType, double discount, bool isCancel, List<SaleLunchDataModel>? lunches) =>
new(id, workerId, buyerId, sum, discountType, discount, isCancel, lunches);
private static List<SaleLunchDataModel> CreateSubDataModel()
=> [new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 1)];
}

View File

@@ -0,0 +1,72 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ThreeFatMenContracts.DataModels;
using ThreeFatMenContracts.Exceptions;
namespace ThreeFatMenTests.DataModelsTest;
[TestFixture]
internal class SaleLunchDataModelTests
{
[Test]
public void SaleIdIsNullOrEmptyTest()
{
var saleLunch = CreateDataModel(null, Guid.NewGuid().ToString(), 10);
Assert.That(() => saleLunch.Validate(), Throws.TypeOf<ValidationException>());
saleLunch = CreateDataModel(string.Empty, Guid.NewGuid().ToString(), 10);
Assert.That(() => saleLunch.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void SaleIdIsNotGuidTest()
{
var saleLunch = CreateDataModel("saleId", Guid.NewGuid().ToString(), 10);
Assert.That(() => saleLunch.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void LunchIdIsNullOrEmptyTest()
{
var saleLunch = CreateDataModel(Guid.NewGuid().ToString(), null, 10);
Assert.That(() => saleLunch.Validate(), Throws.TypeOf<ValidationException>());
saleLunch = CreateDataModel(string.Empty, Guid.NewGuid().ToString(), 10);
Assert.That(() => saleLunch.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void LunchIdIsNotGuidTest()
{
var saleLunch = CreateDataModel(Guid.NewGuid().ToString(), "lunchId", 10);
Assert.That(() => saleLunch.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void CountIsLessOrZeroTest()
{
var saleLunch = CreateDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 0);
Assert.That(() => saleLunch.Validate(), Throws.TypeOf<ValidationException>());
saleLunch = CreateDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), -10);
Assert.That(() => saleLunch.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void AllFieldsIsCorrectTest()
{
var saleId = Guid.NewGuid().ToString();
var lunchId = Guid.NewGuid().ToString();
var count = 10;
var saleLunch = CreateDataModel(saleId, lunchId, count);
Assert.That(() => saleLunch.Validate(), Throws.Nothing);
Assert.Multiple(() =>
{
Assert.That(saleLunch.SaleId, Is.EqualTo(saleId));
Assert.That(saleLunch.LunchId, Is.EqualTo(lunchId));
Assert.That(saleLunch.Count, Is.EqualTo(count));
});
}
private static SaleLunchDataModel CreateDataModel(string? saleId, string? lunchId, int count) =>
new(saleId, lunchId, count);
}

View File

@@ -0,0 +1,115 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ThreeFatMenContracts.DataModels;
using ThreeFatMenContracts.Exceptions;
namespace ThreeFatMenTests.DataModelsTest;
[TestFixture]
internal class WorkerDataModelTests
{
[Test]
public void IdIsNullOrEmptyTest()
{
var worker = CreateDataModel(null, "Фамилия Имя Отчество", Guid.NewGuid().ToString(),
DateTime.Now.AddYears(-18), DateTime.Now, false);
Assert.That(() => worker.Validate(),
Throws.TypeOf<ValidationException>());
worker = CreateDataModel(string.Empty, "Фамилия Имя Отчество",
Guid.NewGuid().ToString(), DateTime.Now.AddYears(-18), DateTime.Now, false);
Assert.That(() => worker.Validate(),
Throws.TypeOf<ValidationException>());
}
[Test]
public void IdIsNotGuidTest()
{
var worker = CreateDataModel("id", "Фамилия Имя Отчество", Guid.NewGuid().ToString(),
DateTime.Now.AddYears(-18), DateTime.Now, false);
Assert.That(() => worker.Validate(),
Throws.TypeOf<ValidationException>());
}
[Test]
public void FIOIsNullOrEmptyTest()
{
var worker = CreateDataModel(Guid.NewGuid().ToString(), null,
Guid.NewGuid().ToString(), DateTime.Now.AddYears(-18), DateTime.Now, false);
Assert.That(() => worker.Validate(),
Throws.TypeOf<ValidationException>());
worker = CreateDataModel(Guid.NewGuid().ToString(), string.Empty,
Guid.NewGuid().ToString(), DateTime.Now.AddYears(-18), DateTime.Now, false);
Assert.That(() => worker.Validate(),
Throws.TypeOf<ValidationException>());
}
[Test]
public void PostIdIsNullOrEmptyTest()
{
var worker = CreateDataModel(Guid.NewGuid().ToString(), "Фамилия Имя Отчество", null,
DateTime.Now.AddYears(-18), DateTime.Now, false);
Assert.That(() => worker.Validate(),
Throws.TypeOf<ValidationException>());
worker = CreateDataModel(Guid.NewGuid().ToString(), "Фамилия Имя Отчество",
string.Empty, DateTime.Now.AddYears(-18), DateTime.Now, false);
Assert.That(() => worker.Validate(),
Throws.TypeOf<ValidationException>());
}
[Test]
public void PostIdIsNotGuidTest()
{
var worker = CreateDataModel(Guid.NewGuid().ToString(), "Фамилия Имя Отчество",
"postId", DateTime.Now.AddYears(-18), DateTime.Now, false);
Assert.That(() => worker.Validate(),
Throws.TypeOf<ValidationException>());
}
[Test]
public void BirthDateIsNotCorrectTest()
{
var worker = CreateDataModel(Guid.NewGuid().ToString(), "Фамилия Имя Отчество",
Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(1), DateTime.Now,
false);
Assert.That(() => worker.Validate(),
Throws.TypeOf<ValidationException>());
}
[Test]
public void BirthDateAndEmploymentDateIsNotCorrectTest()
{
var worker = CreateDataModel(Guid.NewGuid().ToString(), "Фамилия Имя Отчество",
Guid.NewGuid().ToString(), DateTime.Now.AddYears(-18), DateTime.Now.AddYears(-
18).AddDays(-1), false);
Assert.That(() => worker.Validate(),
Throws.TypeOf<ValidationException>());
worker = CreateDataModel(Guid.NewGuid().ToString(), "Фамилия Имя Отчество",
Guid.NewGuid().ToString(), DateTime.Now.AddYears(-18), DateTime.Now.AddYears(-
16), false);
Assert.That(() => worker.Validate(),
Throws.TypeOf<ValidationException>());
}
[Test]
public void AllFieldsIsCorrectTest()
{
var workerId = Guid.NewGuid().ToString();
var fio = "Фамилия Имя Отчество";
var postId = Guid.NewGuid().ToString();
var birthDate = DateTime.Now.AddYears(-16).AddDays(-1);
var employmentDate = DateTime.Now;
var isDelete = false;
var worker = CreateDataModel(workerId, fio, postId, birthDate,
employmentDate, isDelete);
Assert.That(() => worker.Validate(), Throws.Nothing);
Assert.Multiple(() =>
{
Assert.That(worker.Id, Is.EqualTo(workerId));
Assert.That(worker.FIO, Is.EqualTo(fio));
Assert.That(worker.PostId, Is.EqualTo(postId));
Assert.That(worker.BirthDate, Is.EqualTo(birthDate));
Assert.That(worker.EmploymentDate,
Is.EqualTo(employmentDate));
Assert.That(worker.IsDeleted, Is.EqualTo(isDelete));
});
}
private static WorkerDataModel CreateDataModel(string? id, string? fio,
string? postId, DateTime birthDate, DateTime employmentDate, bool isDeleted) => new(id, fio, postId, birthDate, employmentDate, isDeleted);
}

View File

@@ -0,0 +1,29 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<LangVersion>latest</LangVersion>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="coverlet.collector" Version="6.0.2" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
<PackageReference Include="Moq" Version="4.20.72" />
<PackageReference Include="NUnit" Version="4.2.2" />
<PackageReference Include="NUnit.Analyzers" Version="4.4.0" />
<PackageReference Include="NUnit3TestAdapter" Version="4.6.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\ThreeFatMenBusinessLogic\ThreeFatMenBusinessLogic.csproj" />
<ProjectReference Include="..\ThreeFatMenContracts\ThreeFatMenContracts.csproj" />
</ItemGroup>
<ItemGroup>
<Using Include="NUnit.Framework" />
</ItemGroup>
</Project>