Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| c32d8ec83e |
@@ -5,12 +5,24 @@ using WildPlumContracts.Infrastructure;
|
|||||||
|
|
||||||
namespace WildPlumContracts.DataModels;
|
namespace WildPlumContracts.DataModels;
|
||||||
|
|
||||||
public class RoleDataModel(string id, WorkerRole role, bool isActual, DateTime changeDate) : IValidation
|
public class RoleDataModel : IValidation
|
||||||
{
|
{
|
||||||
public string Id { get; private set; } = id;
|
public string Id { get; private set; }
|
||||||
public WorkerRole Role { get; private set; } = role;
|
public WorkerRole WorkerRole { get; private set; }
|
||||||
public bool IsActual { get; private set; } = isActual;
|
public string RoleId { get; private set; }
|
||||||
public DateTime ChangeDate { get; private set; } = changeDate;
|
public bool IsActual { get; private set; }
|
||||||
|
public DateTime ChangeDate { get; private set; }
|
||||||
|
|
||||||
|
public RoleDataModel() { }
|
||||||
|
|
||||||
|
public RoleDataModel(string id, WorkerRole role, string postId, bool isActual, DateTime changeDate)
|
||||||
|
{
|
||||||
|
Id = id;
|
||||||
|
WorkerRole = role;
|
||||||
|
RoleId = postId;
|
||||||
|
IsActual = isActual;
|
||||||
|
ChangeDate = changeDate;
|
||||||
|
}
|
||||||
|
|
||||||
public void Validate()
|
public void Validate()
|
||||||
{
|
{
|
||||||
@@ -18,7 +30,11 @@ public class RoleDataModel(string id, WorkerRole role, bool isActual, DateTime c
|
|||||||
throw new ValidationException("Field Id is empty");
|
throw new ValidationException("Field Id is empty");
|
||||||
if (!Id.IsGuid())
|
if (!Id.IsGuid())
|
||||||
throw new ValidationException("The value in the field Id is not a unique identifier");
|
throw new ValidationException("The value in the field Id is not a unique identifier");
|
||||||
if (Role == WorkerRole.None)
|
if (RoleId.IsEmpty())
|
||||||
|
throw new ValidationException("Field RoleId is empty");
|
||||||
|
if (!RoleId.IsGuid())
|
||||||
|
throw new ValidationException("The value in the field RoleId is not a unique identifier");
|
||||||
|
if (WorkerRole == WorkerRole.None)
|
||||||
throw new ValidationException("Field Role is empty");
|
throw new ValidationException("Field Role is empty");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,6 @@
|
|||||||
|
namespace WildPlumContracts.Exceptions;
|
||||||
|
|
||||||
|
public class ElementDeletedException : Exception
|
||||||
|
{
|
||||||
|
public ElementDeletedException(string id) : base($"Cannot modify a deleted item (id: {id})") { }
|
||||||
|
}
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
namespace WildPlumContracts.Infrastructure;
|
||||||
|
|
||||||
|
public interface IConfigurationDatabase
|
||||||
|
{
|
||||||
|
string ConnectionString { get; }
|
||||||
|
}
|
||||||
@@ -0,0 +1,119 @@
|
|||||||
|
using AutoMapper;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using WildPlumContracts.DataModels;
|
||||||
|
using WildPlumContracts.Exceptions;
|
||||||
|
using WildPlumContracts.StorageContracts;
|
||||||
|
using WildPlumDatabase.Models;
|
||||||
|
|
||||||
|
namespace WildPlumDatabase.Implementations;
|
||||||
|
|
||||||
|
public class OrderStorageContract : IOrderStorageContract
|
||||||
|
{
|
||||||
|
private readonly WildPlumDbContext _dbContext;
|
||||||
|
private readonly Mapper _mapper;
|
||||||
|
|
||||||
|
public OrderStorageContract(WildPlumDbContext dbContext)
|
||||||
|
{
|
||||||
|
_dbContext = dbContext;
|
||||||
|
var config = new MapperConfiguration(cfg =>
|
||||||
|
{
|
||||||
|
cfg.CreateMap<OrderProduct, OrderProductDataModel>();
|
||||||
|
cfg.CreateMap<OrderProductDataModel, OrderProduct>();
|
||||||
|
cfg.CreateMap<Order, OrderDataModel>();
|
||||||
|
cfg.CreateMap<OrderHistory, OrderHistoryDataModel>();
|
||||||
|
cfg.CreateMap<OrderDataModel, Order>().ForMember(x => x.Products, x => x.MapFrom(src => src.Products));
|
||||||
|
});
|
||||||
|
_mapper = new Mapper(config);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void AddElement(OrderDataModel orderDataModel)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
_dbContext.Orders.Add(_mapper.Map<Order>(orderDataModel));
|
||||||
|
_dbContext.SaveChanges();
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_dbContext.ChangeTracker.Clear();
|
||||||
|
throw new StorageException(ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public List<OrderHistoryDataModel> GetHistoryByOrderId(string orderId)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
return [.. _dbContext.OrderHistories.Where(x => x.OrderId
|
||||||
|
== orderId).OrderByDescending(x => x.ChangeDate).Select(x =>
|
||||||
|
_mapper.Map<OrderHistoryDataModel>(x))];
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_dbContext.ChangeTracker.Clear();
|
||||||
|
throw new StorageException(ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void DelElement(string id)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var element = GetOrderById(id) ?? throw new ElementNotFoundException(id);
|
||||||
|
_dbContext.Orders.Remove(element);
|
||||||
|
_dbContext.SaveChanges();
|
||||||
|
}
|
||||||
|
catch (Exception ex) when (ex is ElementDeletedException || ex is ElementNotFoundException)
|
||||||
|
{
|
||||||
|
_dbContext.ChangeTracker.Clear();
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_dbContext.ChangeTracker.Clear();
|
||||||
|
throw new StorageException(ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public OrderDataModel? GetElementById(string id)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
return _mapper.Map<OrderDataModel>(GetOrderById(id));
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_dbContext.ChangeTracker.Clear();
|
||||||
|
throw new StorageException(ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<OrderDataModel> GetList(DateTime? startDate = null, DateTime? endDate = null, string? employeerId = null, string? productId = null)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var query = _dbContext.Orders.Include(x => x.Products).AsQueryable();
|
||||||
|
if (startDate is not null && endDate is not null)
|
||||||
|
{
|
||||||
|
query = query.Where(x => x.OrderDate >= startDate && x.OrderDate < endDate);
|
||||||
|
}
|
||||||
|
if (employeerId is not null)
|
||||||
|
{
|
||||||
|
query = query.Where(x => x.WorkerId == employeerId);
|
||||||
|
}
|
||||||
|
if (productId is not null)
|
||||||
|
{
|
||||||
|
query = query.Where(x => x.Products!.Any(y => y.ProductId == productId));
|
||||||
|
}
|
||||||
|
return [.. query.Select(x => _mapper.Map<OrderDataModel>(x))];
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_dbContext.ChangeTracker.Clear();
|
||||||
|
throw new StorageException(ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private Order? GetOrderById(string id) => _dbContext.Orders.FirstOrDefault(x => x.Id == id);
|
||||||
|
}
|
||||||
@@ -0,0 +1,152 @@
|
|||||||
|
using AutoMapper;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using Npgsql;
|
||||||
|
using WildPlumContracts.DataModels;
|
||||||
|
using WildPlumContracts.Exceptions;
|
||||||
|
using WildPlumContracts.StorageContracts;
|
||||||
|
using WildPlumDatabase.Models;
|
||||||
|
|
||||||
|
namespace WildPlumDatabase.Implementations;
|
||||||
|
|
||||||
|
public class ProductStorageContract : IProductStorageContract
|
||||||
|
{
|
||||||
|
private readonly WildPlumDbContext _dbContext;
|
||||||
|
private readonly Mapper _mapper;
|
||||||
|
|
||||||
|
public ProductStorageContract(WildPlumDbContext dbContext)
|
||||||
|
{
|
||||||
|
_dbContext = dbContext;
|
||||||
|
var config = new MapperConfiguration(cfg =>
|
||||||
|
{
|
||||||
|
cfg.CreateMap<Product, ProductDataModel>();
|
||||||
|
cfg.CreateMap<ProductDataModel, Product>()
|
||||||
|
.ForMember(x => x.IsDeleted, x => x.MapFrom(src => false));
|
||||||
|
});
|
||||||
|
_mapper = new Mapper(config);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void AddElement(ProductDataModel productDataModel)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
_dbContext.Products.Add(_mapper.Map<Product>(productDataModel));
|
||||||
|
_dbContext.SaveChanges();
|
||||||
|
}
|
||||||
|
catch (InvalidOperationException ex) when (ex.TargetSite?.Name == "ThrowIdentityConflict")
|
||||||
|
{
|
||||||
|
_dbContext.ChangeTracker.Clear();
|
||||||
|
throw new ElementExistsException("Id", productDataModel.Id);
|
||||||
|
}
|
||||||
|
catch (DbUpdateException ex) when (ex.InnerException is PostgresException { ConstraintName: "IX_Products_ProductName_IsDeleted" })
|
||||||
|
{
|
||||||
|
_dbContext.ChangeTracker.Clear();
|
||||||
|
throw new ElementExistsException("ProductName", productDataModel.ProductName);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_dbContext.ChangeTracker.Clear();
|
||||||
|
throw new StorageException(ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void DelElement(string id)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var element = GetProductById(id) ?? throw new ElementNotFoundException(id);
|
||||||
|
element.IsDeleted = true;
|
||||||
|
_dbContext.SaveChanges();
|
||||||
|
}
|
||||||
|
catch (ElementNotFoundException ex)
|
||||||
|
{
|
||||||
|
_dbContext.ChangeTracker.Clear();
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_dbContext.ChangeTracker.Clear();
|
||||||
|
throw new StorageException(ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public ProductDataModel? GetElementById(string id)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
return _mapper.Map<ProductDataModel>(GetProductById(id));
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_dbContext.ChangeTracker.Clear();
|
||||||
|
throw new StorageException(ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public ProductDataModel? GetElementByName(string name)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
return _mapper.Map<ProductDataModel>(_dbContext.Products.FirstOrDefault(x => x.ProductName == name && !x.IsDeleted));
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_dbContext.ChangeTracker.Clear();
|
||||||
|
throw new StorageException(ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<ProductDataModel> GetList(bool onlyActive = true)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var query = _dbContext.Products.AsQueryable();
|
||||||
|
if (onlyActive)
|
||||||
|
{
|
||||||
|
query = query.Where(x => !x.IsDeleted);
|
||||||
|
}
|
||||||
|
return [.. query.Select(x => _mapper.Map<ProductDataModel>(x))];
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_dbContext.ChangeTracker.Clear();
|
||||||
|
throw new StorageException(ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void UpdElement(ProductDataModel productDataModel)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var transaction = _dbContext.Database.BeginTransaction();
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var element = GetProductById(productDataModel.Id) ?? throw new ElementNotFoundException(productDataModel.Id);
|
||||||
|
_dbContext.Products.Update(_mapper.Map(productDataModel, element));
|
||||||
|
_dbContext.SaveChanges();
|
||||||
|
transaction.Commit();
|
||||||
|
}
|
||||||
|
catch
|
||||||
|
{
|
||||||
|
transaction.Rollback();
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (DbUpdateException ex) when (ex.InnerException is PostgresException { ConstraintName: "IX_Products_ProductName_IsDeleted" })
|
||||||
|
{
|
||||||
|
_dbContext.ChangeTracker.Clear();
|
||||||
|
throw new ElementExistsException("ProductName", productDataModel.ProductName);
|
||||||
|
}
|
||||||
|
catch (Exception ex) when (ex is ElementDeletedException || ex is ElementNotFoundException)
|
||||||
|
{
|
||||||
|
_dbContext.ChangeTracker.Clear();
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_dbContext.ChangeTracker.Clear();
|
||||||
|
throw new StorageException(ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private Product? GetProductById(string id) => _dbContext.Products.FirstOrDefault(x => x.Id == id && !x.IsDeleted);
|
||||||
|
}
|
||||||
@@ -0,0 +1,173 @@
|
|||||||
|
using AutoMapper;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using Npgsql;
|
||||||
|
using WildPlumContracts.DataModels;
|
||||||
|
using WildPlumContracts.Exceptions;
|
||||||
|
using WildPlumContracts.StorageContracts;
|
||||||
|
using WildPlumDatabase.Models;
|
||||||
|
|
||||||
|
namespace WildPlumDatabase.Implementations;
|
||||||
|
|
||||||
|
public class RoleStorageContract : IRoleStorageContract
|
||||||
|
{
|
||||||
|
private readonly WildPlumDbContext _dbContext;
|
||||||
|
private readonly Mapper _mapper;
|
||||||
|
|
||||||
|
public RoleStorageContract(WildPlumDbContext dbContext)
|
||||||
|
{
|
||||||
|
_dbContext = dbContext;
|
||||||
|
var config = new MapperConfiguration(cfg =>
|
||||||
|
{
|
||||||
|
cfg.CreateMap<Role, RoleDataModel>()
|
||||||
|
.ForMember(x => x.Id, x => x.MapFrom(src => src.RoleId));
|
||||||
|
cfg.CreateMap<RoleDataModel, Role>()
|
||||||
|
.ForMember(x => x.Id, x => x.Ignore())
|
||||||
|
.ForMember(x => x.RoleId, x => x.MapFrom(src => src.Id))
|
||||||
|
.ForMember(x => x.IsActual, x => x.MapFrom(src => true))
|
||||||
|
.ForMember(x => x.ChangeDate, x => x.MapFrom(src => DateTime.UtcNow));
|
||||||
|
});
|
||||||
|
_mapper = new Mapper(config);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void AddElement(RoleDataModel roleDataModel)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
_dbContext.Roles.Add(_mapper.Map<Role>(roleDataModel));
|
||||||
|
_dbContext.SaveChanges();
|
||||||
|
}
|
||||||
|
catch (DbUpdateException ex) when (ex.InnerException is PostgresException { ConstraintName: "IX_Roles_RoleId_IsActual" })
|
||||||
|
{
|
||||||
|
_dbContext.ChangeTracker.Clear();
|
||||||
|
throw new ElementExistsException("RoleId", roleDataModel.RoleId);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_dbContext.ChangeTracker.Clear();
|
||||||
|
throw new StorageException(ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void DelElement(string id)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var element = GetRoleById(id) ?? throw new ElementNotFoundException(id);
|
||||||
|
if (!element.IsActual)
|
||||||
|
{
|
||||||
|
throw new ElementDeletedException(id);
|
||||||
|
}
|
||||||
|
element.IsActual = false;
|
||||||
|
_dbContext.SaveChanges();
|
||||||
|
}
|
||||||
|
catch
|
||||||
|
{
|
||||||
|
_dbContext.ChangeTracker.Clear();
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public RoleDataModel? GetElementById(string id)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
return
|
||||||
|
_mapper.Map<RoleDataModel>(_dbContext.Roles.FirstOrDefault(x => x.Id == id && x.IsActual));
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_dbContext.ChangeTracker.Clear();
|
||||||
|
throw new StorageException(ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<RoleDataModel> GetList(bool onlyActual = true)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var query = _dbContext.Roles.AsQueryable();
|
||||||
|
if (onlyActual)
|
||||||
|
{
|
||||||
|
query = query.Where(x => x.IsActual);
|
||||||
|
}
|
||||||
|
return [.. query.Select(x => _mapper.Map<RoleDataModel>(x))];
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_dbContext.ChangeTracker.Clear();
|
||||||
|
throw new StorageException(ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<RoleDataModel> GetRoleWithHistory(string roleId)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
return [.. _dbContext.Roles.Where(x => x.RoleId == roleId).Select(x => _mapper.Map<RoleDataModel>(x))];
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_dbContext.ChangeTracker.Clear();
|
||||||
|
throw new StorageException(ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void ResElement(string id)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var element = GetRoleById(id) ?? throw new ElementNotFoundException(id);
|
||||||
|
element.IsActual = true;
|
||||||
|
_dbContext.SaveChanges();
|
||||||
|
}
|
||||||
|
catch
|
||||||
|
{
|
||||||
|
_dbContext.ChangeTracker.Clear();
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void UpdElement(RoleDataModel roleDataModel)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var transaction = _dbContext.Database.BeginTransaction();
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var element = GetRoleById(roleDataModel.Id) ?? throw new ElementNotFoundException(roleDataModel.Id);
|
||||||
|
if (!element.IsActual)
|
||||||
|
{
|
||||||
|
throw new ElementDeletedException(roleDataModel.Id);
|
||||||
|
}
|
||||||
|
element.IsActual = false;
|
||||||
|
_dbContext.SaveChanges();
|
||||||
|
var newElement = _mapper.Map<Role>(roleDataModel);
|
||||||
|
_dbContext.Roles.Add(newElement);
|
||||||
|
_dbContext.SaveChanges();
|
||||||
|
transaction.Commit();
|
||||||
|
}
|
||||||
|
catch
|
||||||
|
{
|
||||||
|
transaction.Rollback();
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (DbUpdateException ex) when (ex.InnerException is PostgresException { ConstraintName: "IX_Roles_RoleId_IsActual" })
|
||||||
|
{
|
||||||
|
_dbContext.ChangeTracker.Clear();
|
||||||
|
throw new ElementExistsException("RoleId", roleDataModel.RoleId);
|
||||||
|
}
|
||||||
|
catch (Exception ex) when (ex is ElementDeletedException || ex is ElementNotFoundException)
|
||||||
|
{
|
||||||
|
_dbContext.ChangeTracker.Clear();
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_dbContext.ChangeTracker.Clear();
|
||||||
|
throw new StorageException(ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private Role? GetRoleById(string id) => _dbContext.Roles.Where(x => x.RoleId == id).OrderByDescending(x => x.ChangeDate).FirstOrDefault();
|
||||||
|
}
|
||||||
@@ -0,0 +1,57 @@
|
|||||||
|
using AutoMapper;
|
||||||
|
using WildPlumContracts.DataModels;
|
||||||
|
using WildPlumContracts.Exceptions;
|
||||||
|
using WildPlumContracts.StorageContracts;
|
||||||
|
using WildPlumDatabase.Models;
|
||||||
|
|
||||||
|
namespace WildPlumDatabase.Implementations;
|
||||||
|
|
||||||
|
public class SalaryStorageContract : ISalaryStorageContract
|
||||||
|
{
|
||||||
|
private readonly WildPlumDbContext _dbContext;
|
||||||
|
private readonly Mapper _mapper;
|
||||||
|
|
||||||
|
public SalaryStorageContract(WildPlumDbContext dbContext)
|
||||||
|
{
|
||||||
|
_dbContext = dbContext;
|
||||||
|
var config = new MapperConfiguration(cfg =>
|
||||||
|
{
|
||||||
|
cfg.CreateMap<Salary, SalaryDataModel>();
|
||||||
|
cfg.CreateMap<SalaryDataModel, Salary>()
|
||||||
|
.ForMember(dest => dest.Amount, opt => opt.MapFrom(src => src.Amount));
|
||||||
|
});
|
||||||
|
_mapper = new Mapper(config);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void AddElement(SalaryDataModel salaryDataModel)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
_dbContext.Salaries.Add(_mapper.Map<Salary>(salaryDataModel));
|
||||||
|
_dbContext.SaveChanges();
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_dbContext.ChangeTracker.Clear();
|
||||||
|
throw new StorageException(ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<SalaryDataModel> GetList(DateTime startDate, DateTime endDate, string? workerId = null)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var query = _dbContext.Salaries.Where(x => x.SalaryDate >= startDate && x.SalaryDate <= endDate);
|
||||||
|
if (workerId is not null)
|
||||||
|
{
|
||||||
|
query = query.Where(x => x.WorkerId == workerId);
|
||||||
|
}
|
||||||
|
return [.. query.Select(x => _mapper.Map<SalaryDataModel>(x))];
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_dbContext.ChangeTracker.Clear();
|
||||||
|
throw new StorageException(ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,141 @@
|
|||||||
|
using AutoMapper;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using Npgsql;
|
||||||
|
using WildPlumContracts.DataModels;
|
||||||
|
using WildPlumContracts.Exceptions;
|
||||||
|
using WildPlumContracts.StorageContracts;
|
||||||
|
using WildPlumDatabase.Models;
|
||||||
|
|
||||||
|
namespace WildPlumDatabase.Implementations;
|
||||||
|
|
||||||
|
public class WarehouseStorageContract : IWarehouseStorageContract
|
||||||
|
{
|
||||||
|
private readonly WildPlumDbContext _dbContext;
|
||||||
|
private readonly Mapper _mapper;
|
||||||
|
|
||||||
|
public WarehouseStorageContract(WildPlumDbContext dbContext)
|
||||||
|
{
|
||||||
|
_dbContext = dbContext;
|
||||||
|
var config = new MapperConfiguration(cfg =>
|
||||||
|
{
|
||||||
|
cfg.CreateMap<Warehouse, WarehouseDataModel>();
|
||||||
|
cfg.CreateMap<WarehouseDataModel, Warehouse>();
|
||||||
|
});
|
||||||
|
_mapper = new Mapper(config);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void AddElement(WarehouseDataModel warehouseDataModel)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
_dbContext.Warehouses.Add(_mapper.Map<Warehouse>(warehouseDataModel));
|
||||||
|
_dbContext.SaveChanges();
|
||||||
|
}
|
||||||
|
catch (InvalidOperationException ex) when (ex.TargetSite?.Name == "ThrowIdentityConflict")
|
||||||
|
{
|
||||||
|
_dbContext.ChangeTracker.Clear();
|
||||||
|
throw new ElementExistsException("Id", warehouseDataModel.Id);
|
||||||
|
}
|
||||||
|
catch (DbUpdateException ex) when (ex.InnerException is
|
||||||
|
PostgresException { ConstraintName: "IX_Warehouses_Name" })
|
||||||
|
{
|
||||||
|
_dbContext.ChangeTracker.Clear();
|
||||||
|
throw new ElementExistsException("Name", warehouseDataModel.Name);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_dbContext.ChangeTracker.Clear();
|
||||||
|
throw new StorageException(ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void DelElement(string id)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var element = GetWarehouseById(id) ?? throw new ElementNotFoundException(id);
|
||||||
|
_dbContext.Warehouses.Remove(element);
|
||||||
|
_dbContext.SaveChanges();
|
||||||
|
}
|
||||||
|
catch (ElementNotFoundException)
|
||||||
|
{
|
||||||
|
_dbContext.ChangeTracker.Clear();
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_dbContext.ChangeTracker.Clear();
|
||||||
|
throw new StorageException(ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public WarehouseDataModel? GetElementById(string id)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
return _mapper.Map<WarehouseDataModel>(GetWarehouseById(id));
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_dbContext.ChangeTracker.Clear();
|
||||||
|
throw new StorageException(ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public WarehouseDataModel? GetElementByName(string name)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
return _mapper.Map<WarehouseDataModel>(_dbContext.Warehouses.FirstOrDefault(x => x.Name == name));
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_dbContext.ChangeTracker.Clear();
|
||||||
|
throw new StorageException(ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public List<WarehouseDataModel> GetList()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var query = _dbContext.Warehouses.AsQueryable();
|
||||||
|
return [.. query.Select(x => _mapper.Map<WarehouseDataModel>(x))];
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_dbContext.ChangeTracker.Clear();
|
||||||
|
throw new StorageException(ex);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public void UpdElement(WarehouseDataModel warehouseDataModel)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var element = GetWarehouseById(warehouseDataModel.Id) ?? throw new ElementNotFoundException(warehouseDataModel.Id);
|
||||||
|
_dbContext.Warehouses.Update(_mapper.Map(warehouseDataModel, element));
|
||||||
|
_dbContext.SaveChanges();
|
||||||
|
}
|
||||||
|
catch (DbUpdateException ex) when (ex.InnerException is PostgresException { ConstraintName: "IX_Warehouses_Name" })
|
||||||
|
{
|
||||||
|
_dbContext.ChangeTracker.Clear();
|
||||||
|
throw new ElementExistsException("Name", warehouseDataModel.Name);
|
||||||
|
}
|
||||||
|
catch (Exception ex) when (ex is ElementDeletedException || ex is
|
||||||
|
ElementNotFoundException)
|
||||||
|
{
|
||||||
|
_dbContext.ChangeTracker.Clear();
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_dbContext.ChangeTracker.Clear();
|
||||||
|
throw new StorageException(ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private Warehouse? GetWarehouseById(string id) => _dbContext.Warehouses.FirstOrDefault(x => x.Id == id);
|
||||||
|
}
|
||||||
@@ -0,0 +1,140 @@
|
|||||||
|
using AutoMapper;
|
||||||
|
using WildPlumContracts.DataModels;
|
||||||
|
using WildPlumContracts.Exceptions;
|
||||||
|
using WildPlumContracts.StorageContracts;
|
||||||
|
using WildPlumDatabase.Models;
|
||||||
|
|
||||||
|
namespace WildPlumDatabase.Implementations;
|
||||||
|
|
||||||
|
public class WorkerStorageContract : IWorkerStorageContract
|
||||||
|
{
|
||||||
|
private readonly WildPlumDbContext _dbContext;
|
||||||
|
private readonly Mapper _mapper;
|
||||||
|
|
||||||
|
public WorkerStorageContract(WildPlumDbContext dbContext)
|
||||||
|
{
|
||||||
|
_dbContext = dbContext;
|
||||||
|
|
||||||
|
var configuration = new MapperConfiguration(cfg => cfg.AddMaps(typeof(Worker)));
|
||||||
|
|
||||||
|
_mapper = new Mapper(configuration);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void AddElement(WorkerDataModel employeeDataModel)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
_dbContext.Workers.Add(_mapper.Map<Worker>(employeeDataModel));
|
||||||
|
_dbContext.SaveChanges();
|
||||||
|
}
|
||||||
|
catch (InvalidOperationException ex) when (ex.TargetSite?.Name == "ThrowIdentityConflict")
|
||||||
|
{
|
||||||
|
_dbContext.ChangeTracker.Clear();
|
||||||
|
throw new ElementExistsException("Id", employeeDataModel.Id);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_dbContext.ChangeTracker.Clear();
|
||||||
|
throw new StorageException(ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void DelElement(string id)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var element = GetWorkerById(id) ?? throw new ElementNotFoundException(id);
|
||||||
|
element.IsDeleted = true;
|
||||||
|
_dbContext.SaveChanges();
|
||||||
|
}
|
||||||
|
catch (ElementNotFoundException)
|
||||||
|
{
|
||||||
|
_dbContext.ChangeTracker.Clear();
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_dbContext.ChangeTracker.Clear();
|
||||||
|
throw new StorageException(ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public WorkerDataModel? GetElementByEmail(string email)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
return
|
||||||
|
_mapper.Map<WorkerDataModel>(_dbContext.Workers.FirstOrDefault(x => x.Email == email));
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_dbContext.ChangeTracker.Clear();
|
||||||
|
throw new StorageException(ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public WorkerDataModel? GetElementById(string id)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
return _mapper.Map<WorkerDataModel>(GetWorkerById(id));
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_dbContext.ChangeTracker.Clear();
|
||||||
|
throw new StorageException(ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<WorkerDataModel> GetList(bool onlyActive = true, string? roleId = null, DateTime? fromBirthDate = null, DateTime? toBirthDate = null, DateTime? fromEmploymentDate = null, DateTime? toEmploymentDate = null)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var query = _dbContext.Workers.AsQueryable();
|
||||||
|
if (onlyActive)
|
||||||
|
{
|
||||||
|
query = query.Where(x => !x.IsDeleted);
|
||||||
|
}
|
||||||
|
if (roleId is not null)
|
||||||
|
{
|
||||||
|
query = query.Where(x => x.RoleId == roleId);
|
||||||
|
}
|
||||||
|
if (fromBirthDate is not null && toBirthDate is not null)
|
||||||
|
{
|
||||||
|
query = query.Where(x => x.BirthDate >= fromBirthDate && x.BirthDate <= toBirthDate);
|
||||||
|
}
|
||||||
|
if (fromEmploymentDate is not null && toEmploymentDate is not null)
|
||||||
|
{
|
||||||
|
query = query.Where(x => x.EmploymentDate >= fromEmploymentDate && x.EmploymentDate <= toEmploymentDate);
|
||||||
|
}
|
||||||
|
return [.. query.Select(x => _mapper.Map<WorkerDataModel>(x))];
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_dbContext.ChangeTracker.Clear();
|
||||||
|
throw new StorageException(ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void UpdElement(WorkerDataModel employeeDataModel)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var element = GetWorkerById(employeeDataModel.Id) ?? throw new ElementNotFoundException(employeeDataModel.Id);
|
||||||
|
_dbContext.Workers.Update(_mapper.Map(employeeDataModel, element));
|
||||||
|
_dbContext.SaveChanges();
|
||||||
|
}
|
||||||
|
catch (ElementNotFoundException)
|
||||||
|
{
|
||||||
|
_dbContext.ChangeTracker.Clear();
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_dbContext.ChangeTracker.Clear();
|
||||||
|
throw new StorageException(ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private Worker? GetWorkerById(string id) => _dbContext.Workers.FirstOrDefault(x => x.Id == id && !x.IsDeleted);
|
||||||
|
}
|
||||||
25
WildPlumProject/WildPlumDatabase/Models/Order.cs
Normal file
25
WildPlumProject/WildPlumDatabase/Models/Order.cs
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
using System;
|
||||||
|
using System.ComponentModel.DataAnnotations.Schema;
|
||||||
|
using System.Net.NetworkInformation;
|
||||||
|
using WildPlumContracts.DataModels;
|
||||||
|
using WildPlumContracts.Enums;
|
||||||
|
|
||||||
|
namespace WildPlumDatabase.Models;
|
||||||
|
|
||||||
|
public class Order
|
||||||
|
{
|
||||||
|
public required string Id { get; set; }
|
||||||
|
public required string WarehouseId { get; set; }
|
||||||
|
public required string WorkerId { get; set; }
|
||||||
|
public required string CustomerName { get; set; }
|
||||||
|
public OrderStatus Status { get; set; }
|
||||||
|
public DateTime OrderDate { get; set; }
|
||||||
|
public Worker? Worker { get; set; }
|
||||||
|
public Warehouse? Warehouse { get; set; }
|
||||||
|
|
||||||
|
[ForeignKey("OrderId")]
|
||||||
|
public List<OrderProduct>? Products { get; set; }
|
||||||
|
|
||||||
|
[ForeignKey("OrderId")]
|
||||||
|
public List<OrderHistory>? OrderHistories { get; set; }
|
||||||
|
}
|
||||||
12
WildPlumProject/WildPlumDatabase/Models/OrderHistory.cs
Normal file
12
WildPlumProject/WildPlumDatabase/Models/OrderHistory.cs
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
using WildPlumContracts.Enums;
|
||||||
|
|
||||||
|
namespace WildPlumDatabase.Models;
|
||||||
|
|
||||||
|
public class OrderHistory
|
||||||
|
{
|
||||||
|
public required string Id { get; set; }
|
||||||
|
public required string OrderId { get; set; }
|
||||||
|
public OrderStatus OldStatus { get; set; }
|
||||||
|
public DateTime ChangeDate { get; set; }
|
||||||
|
public Order? Order { get; set; }
|
||||||
|
}
|
||||||
10
WildPlumProject/WildPlumDatabase/Models/OrderProduct.cs
Normal file
10
WildPlumProject/WildPlumDatabase/Models/OrderProduct.cs
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
namespace WildPlumDatabase.Models;
|
||||||
|
|
||||||
|
public class OrderProduct
|
||||||
|
{
|
||||||
|
public required string OrderId { get; set; }
|
||||||
|
public required string ProductId { get; set; }
|
||||||
|
public int Count { get; set; }
|
||||||
|
public Order? Order { get; set; }
|
||||||
|
public Product? Product { get; set; }
|
||||||
|
}
|
||||||
14
WildPlumProject/WildPlumDatabase/Models/Product.cs
Normal file
14
WildPlumProject/WildPlumDatabase/Models/Product.cs
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
using System.ComponentModel.DataAnnotations.Schema;
|
||||||
|
|
||||||
|
namespace WildPlumDatabase.Models;
|
||||||
|
|
||||||
|
public class Product
|
||||||
|
{
|
||||||
|
public required string Id { get; set; }
|
||||||
|
public required string ProductName { get; set; }
|
||||||
|
public double Price { get; set; }
|
||||||
|
public bool IsDeleted { get; set; }
|
||||||
|
|
||||||
|
[ForeignKey("ProductId")]
|
||||||
|
public List<OrderProduct>? Products { get; set; }
|
||||||
|
}
|
||||||
12
WildPlumProject/WildPlumDatabase/Models/Role.cs
Normal file
12
WildPlumProject/WildPlumDatabase/Models/Role.cs
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
using WildPlumContracts.Enums;
|
||||||
|
|
||||||
|
namespace WildPlumDatabase.Models;
|
||||||
|
|
||||||
|
public class Role
|
||||||
|
{
|
||||||
|
public string Id { get; set; } = Guid.NewGuid().ToString();
|
||||||
|
public required string RoleId { get; set; }
|
||||||
|
public WorkerRole WorkerRole { get; set; }
|
||||||
|
public bool IsActual { get; set; }
|
||||||
|
public DateTime ChangeDate { get; set; }
|
||||||
|
}
|
||||||
10
WildPlumProject/WildPlumDatabase/Models/Salary.cs
Normal file
10
WildPlumProject/WildPlumDatabase/Models/Salary.cs
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
namespace WildPlumDatabase.Models;
|
||||||
|
|
||||||
|
public class Salary
|
||||||
|
{
|
||||||
|
public string Id { get; set; } = Guid.NewGuid().ToString();
|
||||||
|
public required string WorkerId { get; set; }
|
||||||
|
public double Amount { get; set; }
|
||||||
|
public DateTime SalaryDate { get; set; }
|
||||||
|
public Worker? Worker { get; set; }
|
||||||
|
}
|
||||||
14
WildPlumProject/WildPlumDatabase/Models/Warehouse.cs
Normal file
14
WildPlumProject/WildPlumDatabase/Models/Warehouse.cs
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
using System.ComponentModel.DataAnnotations.Schema;
|
||||||
|
using System.Xml.Linq;
|
||||||
|
|
||||||
|
namespace WildPlumDatabase.Models;
|
||||||
|
|
||||||
|
public class Warehouse
|
||||||
|
{
|
||||||
|
public required string Id { get; set; }
|
||||||
|
public required string Name { get; set; }
|
||||||
|
public required string Location { get; set; }
|
||||||
|
|
||||||
|
[ForeignKey("WarehouseId")]
|
||||||
|
public List<Order>? Orders { get; set; }
|
||||||
|
}
|
||||||
23
WildPlumProject/WildPlumDatabase/Models/Worker.cs
Normal file
23
WildPlumProject/WildPlumDatabase/Models/Worker.cs
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
using AutoMapper;
|
||||||
|
using System.ComponentModel.DataAnnotations.Schema;
|
||||||
|
using WildPlumContracts.DataModels;
|
||||||
|
|
||||||
|
namespace WildPlumDatabase.Models;
|
||||||
|
|
||||||
|
[AutoMap(typeof(WorkerDataModel), ReverseMap = true)]
|
||||||
|
public class Worker
|
||||||
|
{
|
||||||
|
public required string Id { get; set; }
|
||||||
|
public required string FullName { get; set; }
|
||||||
|
public required string Email { get; set; }
|
||||||
|
public required string RoleId { get; set; }
|
||||||
|
public DateTime BirthDate { get; set; }
|
||||||
|
public DateTime EmploymentDate { get; set; }
|
||||||
|
public bool IsDeleted { get; set; }
|
||||||
|
|
||||||
|
[ForeignKey("WorkerId")]
|
||||||
|
public List<Salary>? Salaries { get; set; }
|
||||||
|
|
||||||
|
[ForeignKey("WorkerId")]
|
||||||
|
public List<Order>? Orders { get; set; }
|
||||||
|
}
|
||||||
24
WildPlumProject/WildPlumDatabase/WildPlumDatabase.csproj
Normal file
24
WildPlumProject/WildPlumDatabase/WildPlumDatabase.csproj
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<TargetFramework>net8.0</TargetFramework>
|
||||||
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
|
<Nullable>enable</Nullable>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<PackageReference Include="AutoMapper" Version="14.0.0" />
|
||||||
|
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="9.0.2" />
|
||||||
|
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="9.0.4" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<ProjectReference Include="..\WildPlumContracts\WildPlumContracts.csproj" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<InternalsVisibleTo Include="WildPlumTests" />
|
||||||
|
<InternalsVisibleTo Include="DynamicProxyGenAssembly2" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
</Project>
|
||||||
49
WildPlumProject/WildPlumDatabase/WildPlumDbContext.cs
Normal file
49
WildPlumProject/WildPlumDatabase/WildPlumDbContext.cs
Normal file
@@ -0,0 +1,49 @@
|
|||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using System;
|
||||||
|
using WildPlumContracts.Infrastructure;
|
||||||
|
using WildPlumDatabase.Models;
|
||||||
|
|
||||||
|
namespace WildPlumDatabase;
|
||||||
|
|
||||||
|
public class WildPlumDbContext(IConfigurationDatabase configurationDatabase) : DbContext
|
||||||
|
{
|
||||||
|
private readonly IConfigurationDatabase? _configurationDatabase = configurationDatabase;
|
||||||
|
|
||||||
|
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
|
||||||
|
{
|
||||||
|
optionsBuilder.UseNpgsql(_configurationDatabase?.ConnectionString, o => o.SetPostgresVersion(12, 2));
|
||||||
|
base.OnConfiguring(optionsBuilder);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
||||||
|
{
|
||||||
|
base.OnModelCreating(modelBuilder);
|
||||||
|
modelBuilder.Entity<Worker>().HasIndex(x => x.Email).IsUnique();
|
||||||
|
|
||||||
|
modelBuilder.Entity<Warehouse>().HasIndex(x => x.Name).IsUnique();
|
||||||
|
|
||||||
|
modelBuilder.Entity<Role>()
|
||||||
|
.HasIndex(e => new { e.RoleId, e.IsActual })
|
||||||
|
.IsUnique()
|
||||||
|
.HasFilter($"\"{nameof(Role.IsActual)}\" = TRUE");
|
||||||
|
|
||||||
|
modelBuilder.Entity<Product>()
|
||||||
|
.HasIndex(x => new { x.ProductName, x.IsDeleted })
|
||||||
|
.IsUnique()
|
||||||
|
.HasFilter($"\"{nameof(Product.IsDeleted)}\" = FALSE");
|
||||||
|
|
||||||
|
modelBuilder.Entity<OrderProduct>().HasKey(x => new
|
||||||
|
{
|
||||||
|
x.OrderId,
|
||||||
|
x.ProductId
|
||||||
|
});
|
||||||
|
}
|
||||||
|
public DbSet<Worker> Workers { get; set; }
|
||||||
|
public DbSet<OrderHistory> OrderHistories { get; set; }
|
||||||
|
public DbSet<Product> Products { get; set; }
|
||||||
|
public DbSet<Role> Roles { get; set; }
|
||||||
|
public DbSet<Salary> Salaries { get; set; }
|
||||||
|
public DbSet<Order> Orders { get; set; }
|
||||||
|
public DbSet<OrderProduct> OrderProducts { get; set; }
|
||||||
|
public DbSet<Warehouse> Warehouses { get; set; }
|
||||||
|
}
|
||||||
@@ -9,6 +9,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WildPlumTests", "WildPlumTe
|
|||||||
EndProject
|
EndProject
|
||||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WildPlumBusinessLogic", "WildPlumBusinessLogic\WildPlumBusinessLogic.csproj", "{55BEFDCB-041A-4D0C-B51F-164EB88D9E5B}"
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WildPlumBusinessLogic", "WildPlumBusinessLogic\WildPlumBusinessLogic.csproj", "{55BEFDCB-041A-4D0C-B51F-164EB88D9E5B}"
|
||||||
EndProject
|
EndProject
|
||||||
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WildPlumDatabase", "WildPlumDatabase\WildPlumDatabase.csproj", "{06CB985C-6592-4D94-9422-1C6B58002F59}"
|
||||||
|
EndProject
|
||||||
Global
|
Global
|
||||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||||
Debug|Any CPU = Debug|Any CPU
|
Debug|Any CPU = Debug|Any CPU
|
||||||
@@ -27,6 +29,10 @@ Global
|
|||||||
{55BEFDCB-041A-4D0C-B51F-164EB88D9E5B}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
{55BEFDCB-041A-4D0C-B51F-164EB88D9E5B}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
{55BEFDCB-041A-4D0C-B51F-164EB88D9E5B}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
{55BEFDCB-041A-4D0C-B51F-164EB88D9E5B}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
{55BEFDCB-041A-4D0C-B51F-164EB88D9E5B}.Release|Any CPU.Build.0 = Release|Any CPU
|
{55BEFDCB-041A-4D0C-B51F-164EB88D9E5B}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
|
{06CB985C-6592-4D94-9422-1C6B58002F59}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||||
|
{06CB985C-6592-4D94-9422-1C6B58002F59}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
|
{06CB985C-6592-4D94-9422-1C6B58002F59}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
|
{06CB985C-6592-4D94-9422-1C6B58002F59}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
EndGlobalSection
|
EndGlobalSection
|
||||||
GlobalSection(SolutionProperties) = preSolution
|
GlobalSection(SolutionProperties) = preSolution
|
||||||
HideSolutionNode = FALSE
|
HideSolutionNode = FALSE
|
||||||
|
|||||||
@@ -33,9 +33,9 @@ internal class RoleBusinessLogicContractTests
|
|||||||
//Arrange
|
//Arrange
|
||||||
var listOriginal = new List<RoleDataModel>()
|
var listOriginal = new List<RoleDataModel>()
|
||||||
{
|
{
|
||||||
new(Guid.NewGuid().ToString(), WorkerRole.Courier, true, DateTime.UtcNow),
|
new(Guid.NewGuid().ToString(), WorkerRole.Courier, Guid.NewGuid().ToString(),true, DateTime.UtcNow),
|
||||||
new(Guid.NewGuid().ToString(), WorkerRole.Courier, false, DateTime.UtcNow),
|
new(Guid.NewGuid().ToString(), WorkerRole.Courier, Guid.NewGuid().ToString(),false, DateTime.UtcNow),
|
||||||
new(Guid.NewGuid().ToString(), WorkerRole.Courier, true, DateTime.UtcNow),
|
new(Guid.NewGuid().ToString(), WorkerRole.Courier, Guid.NewGuid().ToString(),true, DateTime.UtcNow),
|
||||||
};
|
};
|
||||||
_postStorageContract.Setup(x => x.GetList(It.IsAny<bool>())).Returns(listOriginal);
|
_postStorageContract.Setup(x => x.GetList(It.IsAny<bool>())).Returns(listOriginal);
|
||||||
//Act
|
//Act
|
||||||
@@ -97,8 +97,8 @@ internal class RoleBusinessLogicContractTests
|
|||||||
var postId = Guid.NewGuid().ToString();
|
var postId = Guid.NewGuid().ToString();
|
||||||
var listOriginal = new List<RoleDataModel>()
|
var listOriginal = new List<RoleDataModel>()
|
||||||
{
|
{
|
||||||
new(postId, WorkerRole.Courier, true, DateTime.UtcNow),
|
new(postId, WorkerRole.Courier, Guid.NewGuid().ToString(),true, DateTime.UtcNow),
|
||||||
new(postId, WorkerRole.Courier, false, DateTime.UtcNow)
|
new(postId, WorkerRole.Courier, Guid.NewGuid().ToString(),false, DateTime.UtcNow)
|
||||||
};
|
};
|
||||||
_postStorageContract.Setup(x => x.GetRoleWithHistory(It.IsAny<string>())).Returns(listOriginal);
|
_postStorageContract.Setup(x => x.GetRoleWithHistory(It.IsAny<string>())).Returns(listOriginal);
|
||||||
//Act
|
//Act
|
||||||
@@ -162,7 +162,7 @@ internal class RoleBusinessLogicContractTests
|
|||||||
{
|
{
|
||||||
//Arrange
|
//Arrange
|
||||||
var id = Guid.NewGuid().ToString();
|
var id = Guid.NewGuid().ToString();
|
||||||
var record = new RoleDataModel(id, WorkerRole.Courier, true, DateTime.UtcNow);
|
var record = new RoleDataModel(id, WorkerRole.Courier, Guid.NewGuid().ToString(), true, DateTime.UtcNow);
|
||||||
_postStorageContract.Setup(x => x.GetElementById(id)).Returns(record);
|
_postStorageContract.Setup(x => x.GetElementById(id)).Returns(record);
|
||||||
//Act
|
//Act
|
||||||
var element = _postBusinessLogicContract.GetRoleByData(id);
|
var element = _postBusinessLogicContract.GetRoleByData(id);
|
||||||
@@ -204,11 +204,11 @@ internal class RoleBusinessLogicContractTests
|
|||||||
{
|
{
|
||||||
//Arrange
|
//Arrange
|
||||||
var flag = false;
|
var flag = false;
|
||||||
var record = new RoleDataModel(Guid.NewGuid().ToString(), WorkerRole.Courier, true, DateTime.UtcNow.AddDays(-1));
|
var record = new RoleDataModel(Guid.NewGuid().ToString(), WorkerRole.Courier, Guid.NewGuid().ToString(), true, DateTime.UtcNow.AddDays(-1));
|
||||||
_postStorageContract.Setup(x => x.AddElement(It.IsAny<RoleDataModel>()))
|
_postStorageContract.Setup(x => x.AddElement(It.IsAny<RoleDataModel>()))
|
||||||
.Callback((RoleDataModel x) =>
|
.Callback((RoleDataModel x) =>
|
||||||
{
|
{
|
||||||
flag = x.Id == record.Id && x.Role == record.Role &&
|
flag = x.Id == record.Id && x.WorkerRole == record.WorkerRole &&
|
||||||
x.ChangeDate == record.ChangeDate;
|
x.ChangeDate == record.ChangeDate;
|
||||||
});
|
});
|
||||||
//Act
|
//Act
|
||||||
@@ -224,7 +224,7 @@ internal class RoleBusinessLogicContractTests
|
|||||||
//Arrange
|
//Arrange
|
||||||
_postStorageContract.Setup(x => x.AddElement(It.IsAny<RoleDataModel>())).Throws(new ElementExistsException("Data", "Data"));
|
_postStorageContract.Setup(x => x.AddElement(It.IsAny<RoleDataModel>())).Throws(new ElementExistsException("Data", "Data"));
|
||||||
//Act&Assert
|
//Act&Assert
|
||||||
Assert.That(() => _postBusinessLogicContract.InsertRole(new(Guid.NewGuid().ToString(), WorkerRole.Courier, true, DateTime.UtcNow)), Throws.TypeOf<ElementExistsException>());
|
Assert.That(() => _postBusinessLogicContract.InsertRole(new(Guid.NewGuid().ToString(), WorkerRole.Courier, Guid.NewGuid().ToString(), true, DateTime.UtcNow)), Throws.TypeOf<ElementExistsException>());
|
||||||
_postStorageContract.Verify(x => x.AddElement(It.IsAny<RoleDataModel>()), Times.Once);
|
_postStorageContract.Verify(x => x.AddElement(It.IsAny<RoleDataModel>()), Times.Once);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -240,7 +240,7 @@ internal class RoleBusinessLogicContractTests
|
|||||||
public void InsertPost_InvalidRecord_ThrowException_Test()
|
public void InsertPost_InvalidRecord_ThrowException_Test()
|
||||||
{
|
{
|
||||||
//Act&Assert
|
//Act&Assert
|
||||||
Assert.That(() => _postBusinessLogicContract.InsertRole(new RoleDataModel("id", WorkerRole.Courier, true, DateTime.UtcNow)), Throws.TypeOf<ValidationException>());
|
Assert.That(() => _postBusinessLogicContract.InsertRole(new RoleDataModel("id", WorkerRole.Courier, Guid.NewGuid().ToString(), true, DateTime.UtcNow)), Throws.TypeOf<ValidationException>());
|
||||||
_postStorageContract.Verify(x => x.AddElement(It.IsAny<RoleDataModel>()), Times.Never);
|
_postStorageContract.Verify(x => x.AddElement(It.IsAny<RoleDataModel>()), Times.Never);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -250,7 +250,7 @@ internal class RoleBusinessLogicContractTests
|
|||||||
//Arrange
|
//Arrange
|
||||||
_postStorageContract.Setup(x => x.AddElement(It.IsAny<RoleDataModel>())).Throws(new StorageException(new InvalidOperationException()));
|
_postStorageContract.Setup(x => x.AddElement(It.IsAny<RoleDataModel>())).Throws(new StorageException(new InvalidOperationException()));
|
||||||
//Act&Assert
|
//Act&Assert
|
||||||
Assert.That(() => _postBusinessLogicContract.InsertRole(new(Guid.NewGuid().ToString(), WorkerRole.Courier, true, DateTime.UtcNow)), Throws.TypeOf<StorageException>());
|
Assert.That(() => _postBusinessLogicContract.InsertRole(new(Guid.NewGuid().ToString(), WorkerRole.Courier, Guid.NewGuid().ToString(), true, DateTime.UtcNow)), Throws.TypeOf<StorageException>());
|
||||||
_postStorageContract.Verify(x => x.AddElement(It.IsAny<RoleDataModel>()), Times.Once);
|
_postStorageContract.Verify(x => x.AddElement(It.IsAny<RoleDataModel>()), Times.Once);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -259,11 +259,11 @@ internal class RoleBusinessLogicContractTests
|
|||||||
{
|
{
|
||||||
//Arrange
|
//Arrange
|
||||||
var flag = false;
|
var flag = false;
|
||||||
var record = new RoleDataModel(Guid.NewGuid().ToString(), WorkerRole.Courier, true, DateTime.UtcNow.AddDays(-1));
|
var record = new RoleDataModel(Guid.NewGuid().ToString(), WorkerRole.Courier, Guid.NewGuid().ToString(), true, DateTime.UtcNow.AddDays(-1));
|
||||||
_postStorageContract.Setup(x => x.UpdElement(It.IsAny<RoleDataModel>()))
|
_postStorageContract.Setup(x => x.UpdElement(It.IsAny<RoleDataModel>()))
|
||||||
.Callback((RoleDataModel x) =>
|
.Callback((RoleDataModel x) =>
|
||||||
{
|
{
|
||||||
flag = x.Id == record.Id && x.Role == record.Role &&
|
flag = x.Id == record.Id && x.WorkerRole == record.WorkerRole &&
|
||||||
x.ChangeDate == record.ChangeDate;
|
x.ChangeDate == record.ChangeDate;
|
||||||
});
|
});
|
||||||
//Act
|
//Act
|
||||||
@@ -279,7 +279,7 @@ internal class RoleBusinessLogicContractTests
|
|||||||
//Arrange
|
//Arrange
|
||||||
_postStorageContract.Setup(x => x.UpdElement(It.IsAny<RoleDataModel>())).Throws(new ElementNotFoundException(""));
|
_postStorageContract.Setup(x => x.UpdElement(It.IsAny<RoleDataModel>())).Throws(new ElementNotFoundException(""));
|
||||||
//Act&Assert
|
//Act&Assert
|
||||||
Assert.That(() => _postBusinessLogicContract.UpdateRole(new(Guid.NewGuid().ToString(), WorkerRole.Courier, true, DateTime.UtcNow)), Throws.TypeOf<ElementNotFoundException>());
|
Assert.That(() => _postBusinessLogicContract.UpdateRole(new(Guid.NewGuid().ToString(), WorkerRole.Courier, Guid.NewGuid().ToString(), true, DateTime.UtcNow)), Throws.TypeOf<ElementNotFoundException>());
|
||||||
_postStorageContract.Verify(x => x.UpdElement(It.IsAny<RoleDataModel>()), Times.Once);
|
_postStorageContract.Verify(x => x.UpdElement(It.IsAny<RoleDataModel>()), Times.Once);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -289,7 +289,7 @@ internal class RoleBusinessLogicContractTests
|
|||||||
//Arrange
|
//Arrange
|
||||||
_postStorageContract.Setup(x => x.UpdElement(It.IsAny<RoleDataModel>())).Throws(new ElementExistsException("Data", "Data"));
|
_postStorageContract.Setup(x => x.UpdElement(It.IsAny<RoleDataModel>())).Throws(new ElementExistsException("Data", "Data"));
|
||||||
//Act&Assert
|
//Act&Assert
|
||||||
Assert.That(() => _postBusinessLogicContract.UpdateRole(new(Guid.NewGuid().ToString(), WorkerRole.Courier, true, DateTime.UtcNow)), Throws.TypeOf<ElementExistsException>());
|
Assert.That(() => _postBusinessLogicContract.UpdateRole(new(Guid.NewGuid().ToString(), WorkerRole.Courier, Guid.NewGuid().ToString(), true, DateTime.UtcNow)), Throws.TypeOf<ElementExistsException>());
|
||||||
_postStorageContract.Verify(x => x.UpdElement(It.IsAny<RoleDataModel>()), Times.Once);
|
_postStorageContract.Verify(x => x.UpdElement(It.IsAny<RoleDataModel>()), Times.Once);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -305,7 +305,7 @@ internal class RoleBusinessLogicContractTests
|
|||||||
public void UpdatePost_InvalidRecord_ThrowException_Test()
|
public void UpdatePost_InvalidRecord_ThrowException_Test()
|
||||||
{
|
{
|
||||||
//Act&Assert
|
//Act&Assert
|
||||||
Assert.That(() => _postBusinessLogicContract.UpdateRole(new RoleDataModel("id", WorkerRole.Courier, true, DateTime.UtcNow)), Throws.TypeOf<ValidationException>());
|
Assert.That(() => _postBusinessLogicContract.UpdateRole(new RoleDataModel("id", WorkerRole.Courier, Guid.NewGuid().ToString(), true, DateTime.UtcNow)), Throws.TypeOf<ValidationException>());
|
||||||
_postStorageContract.Verify(x => x.UpdElement(It.IsAny<RoleDataModel>()), Times.Never);
|
_postStorageContract.Verify(x => x.UpdElement(It.IsAny<RoleDataModel>()), Times.Never);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -315,7 +315,7 @@ internal class RoleBusinessLogicContractTests
|
|||||||
//Arrange
|
//Arrange
|
||||||
_postStorageContract.Setup(x => x.UpdElement(It.IsAny<RoleDataModel>())).Throws(new StorageException(new InvalidOperationException()));
|
_postStorageContract.Setup(x => x.UpdElement(It.IsAny<RoleDataModel>())).Throws(new StorageException(new InvalidOperationException()));
|
||||||
//Act&Assert
|
//Act&Assert
|
||||||
Assert.That(() => _postBusinessLogicContract.UpdateRole(new(Guid.NewGuid().ToString(), WorkerRole.Courier, true, DateTime.UtcNow)), Throws.TypeOf<StorageException>());
|
Assert.That(() => _postBusinessLogicContract.UpdateRole(new(Guid.NewGuid().ToString(), WorkerRole.Courier, Guid.NewGuid().ToString(), true, DateTime.UtcNow)), Throws.TypeOf<StorageException>());
|
||||||
_postStorageContract.Verify(x => x.UpdElement(It.IsAny<RoleDataModel>()), Times.Once);
|
_postStorageContract.Verify(x => x.UpdElement(It.IsAny<RoleDataModel>()), Times.Once);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -26,6 +26,25 @@ public class RoleDataModelTests
|
|||||||
Throws.TypeOf<ValidationException>());
|
Throws.TypeOf<ValidationException>());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void Validate_ThrowsException_WhenRoleIdIsNullOrEmpty()
|
||||||
|
{
|
||||||
|
Assert.Multiple(() =>
|
||||||
|
{
|
||||||
|
Assert.That(() => CreateDataModel(Guid.NewGuid().ToString(), null!, WorkerRole.Courier, true, DateTime.UtcNow).Validate(),
|
||||||
|
Throws.TypeOf<ValidationException>());
|
||||||
|
Assert.That(() => CreateDataModel(Guid.NewGuid().ToString(), string.Empty, WorkerRole.Courier, true, DateTime.UtcNow).Validate(),
|
||||||
|
Throws.TypeOf<ValidationException>());
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void Validate_ThrowsException_WhenRoleIdIsNotGuid()
|
||||||
|
{
|
||||||
|
Assert.That(() => CreateDataModel(Guid.NewGuid().ToString(), "invalid-guid", WorkerRole.Courier, true, DateTime.UtcNow).Validate(),
|
||||||
|
Throws.TypeOf<ValidationException>());
|
||||||
|
}
|
||||||
|
|
||||||
[Test]
|
[Test]
|
||||||
public void Validate_ThrowsException_WhenRoleIsNone()
|
public void Validate_ThrowsException_WhenRoleIsNone()
|
||||||
{
|
{
|
||||||
@@ -48,12 +67,12 @@ public class RoleDataModelTests
|
|||||||
Assert.Multiple(() =>
|
Assert.Multiple(() =>
|
||||||
{
|
{
|
||||||
Assert.That(roleData.Id, Is.EqualTo(id));
|
Assert.That(roleData.Id, Is.EqualTo(id));
|
||||||
Assert.That(roleData.Role, Is.EqualTo(role));
|
Assert.That(roleData.WorkerRole, Is.EqualTo(role));
|
||||||
Assert.That(roleData.IsActual, Is.EqualTo(isActual));
|
Assert.That(roleData.IsActual, Is.EqualTo(isActual));
|
||||||
Assert.That(roleData.ChangeDate, Is.EqualTo(changeDate).Within(TimeSpan.FromSeconds(1)));
|
Assert.That(roleData.ChangeDate, Is.EqualTo(changeDate).Within(TimeSpan.FromSeconds(1)));
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private static RoleDataModel CreateDataModel(string id, string roleId, WorkerRole role, bool isActive, DateTime createdAt) =>
|
private static RoleDataModel CreateDataModel(string id, string roleId, WorkerRole role, bool isActive, DateTime createdAt) =>
|
||||||
new(id, role, isActive, createdAt);
|
new(id, role, roleId, isActive, createdAt);
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
using WildPlumContracts.Infrastructure;
|
||||||
|
|
||||||
|
namespace WildPlumTests.Infrastructure;
|
||||||
|
|
||||||
|
internal class ConfigurationDatabaseTest : IConfigurationDatabase
|
||||||
|
{
|
||||||
|
public string ConnectionString => "Host=localhost;Port=5432;Database=RPP;Username=postgres;Password=525252;";
|
||||||
|
}
|
||||||
@@ -0,0 +1,23 @@
|
|||||||
|
using WildPlumTests.Infrastructure;
|
||||||
|
using WildPlumDatabase;
|
||||||
|
|
||||||
|
namespace WildPlumTests.StoragesContractsTests;
|
||||||
|
|
||||||
|
internal class BaseStorageContractTests
|
||||||
|
{
|
||||||
|
protected WildPlumDbContext WildPlumDbContext { get; private set; }
|
||||||
|
|
||||||
|
[OneTimeSetUp]
|
||||||
|
public void OneTimeSetUp()
|
||||||
|
{
|
||||||
|
WildPlumDbContext = new WildPlumDbContext(new ConfigurationDatabaseTest());
|
||||||
|
WildPlumDbContext.Database.EnsureDeleted();
|
||||||
|
WildPlumDbContext.Database.EnsureCreated();
|
||||||
|
}
|
||||||
|
[OneTimeTearDown]
|
||||||
|
public void OneTimeTearDown()
|
||||||
|
{
|
||||||
|
WildPlumDbContext.Database.EnsureDeleted();
|
||||||
|
WildPlumDbContext.Dispose();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,221 @@
|
|||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using WildPlumContracts.DataModels;
|
||||||
|
using WildPlumContracts.Exceptions;
|
||||||
|
using WildPlumDatabase.Implementations;
|
||||||
|
using WildPlumDatabase.Models;
|
||||||
|
|
||||||
|
namespace WildPlumTests.StoragesContractsTests;
|
||||||
|
|
||||||
|
[TestFixture]
|
||||||
|
internal class EmployeeStorageContractTests : BaseStorageContractTests
|
||||||
|
{
|
||||||
|
private WorkerStorageContract _workerStorageContract;
|
||||||
|
|
||||||
|
[SetUp]
|
||||||
|
public void SetUp()
|
||||||
|
{
|
||||||
|
_workerStorageContract = new WorkerStorageContract(WildPlumDbContext);
|
||||||
|
}
|
||||||
|
|
||||||
|
[TearDown]
|
||||||
|
public void TearDown()
|
||||||
|
{
|
||||||
|
WildPlumDbContext.Database.ExecuteSqlRaw("TRUNCATE \"Workers\" CASCADE; ");
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void Try_GetList_WhenHaveRecords_Test()
|
||||||
|
{
|
||||||
|
var worker = InsertWorkerToDatabaseAndReturn(Guid.NewGuid().ToString(), "fio 1", email: "test2@gmail.com");
|
||||||
|
InsertWorkerToDatabaseAndReturn(Guid.NewGuid().ToString(), "fio 2", email: "test3@gmail.com");
|
||||||
|
InsertWorkerToDatabaseAndReturn(Guid.NewGuid().ToString(), "fio 3", email: "test4@gmail.com");
|
||||||
|
var list = _workerStorageContract.GetList();
|
||||||
|
Assert.That(list, Is.Not.Null);
|
||||||
|
Assert.That(list, Has.Count.EqualTo(3));
|
||||||
|
AssertElement(list.First(), worker);
|
||||||
|
}
|
||||||
|
[Test]
|
||||||
|
public void Try_GetList_WhenNoRecords_Test()
|
||||||
|
{
|
||||||
|
var list = _workerStorageContract.GetList();
|
||||||
|
Assert.That(list, Is.Not.Null);
|
||||||
|
Assert.That(list, Is.Empty);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void Try_GetList_ByJobTitleId_Test()
|
||||||
|
{
|
||||||
|
var jobTitleId = Guid.NewGuid().ToString();
|
||||||
|
InsertWorkerToDatabaseAndReturn(Guid.NewGuid().ToString(), "fio 1", jobTitleId, email: "test2@gmail.com");
|
||||||
|
InsertWorkerToDatabaseAndReturn(Guid.NewGuid().ToString(), "fio 2", jobTitleId, email: "test3@gmail.com");
|
||||||
|
InsertWorkerToDatabaseAndReturn(Guid.NewGuid().ToString(), "fio 3", email: "test4@gmail.com");
|
||||||
|
var list = _workerStorageContract.GetList(roleId: jobTitleId);
|
||||||
|
Assert.That(list, Is.Not.Null);
|
||||||
|
Assert.That(list, Has.Count.EqualTo(2));
|
||||||
|
Assert.That(list.All(x => x.RoleId == jobTitleId));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void Try_GetList_ByBirthDate_Test()
|
||||||
|
{
|
||||||
|
InsertWorkerToDatabaseAndReturn(Guid.NewGuid().ToString(), "fio 1", birthDate: DateTime.UtcNow.AddYears(-25), email: "test2@gmail.com");
|
||||||
|
InsertWorkerToDatabaseAndReturn(Guid.NewGuid().ToString(), "fio 2", birthDate: DateTime.UtcNow.AddYears(-21), email: "test4@gmail.com");
|
||||||
|
InsertWorkerToDatabaseAndReturn(Guid.NewGuid().ToString(), "fio 3", birthDate: DateTime.UtcNow.AddYears(-20), email: "test53@gmail.com");
|
||||||
|
InsertWorkerToDatabaseAndReturn(Guid.NewGuid().ToString(), "fio 4", birthDate: DateTime.UtcNow.AddYears(-19), email: "test253gmail.com");
|
||||||
|
var list = _workerStorageContract.GetList(fromBirthDate: DateTime.UtcNow.AddYears(-21).AddMinutes(-1), toBirthDate: DateTime.UtcNow.AddYears(-20).AddMinutes(1));
|
||||||
|
Assert.That(list, Is.Not.Null);
|
||||||
|
Assert.That(list, Has.Count.EqualTo(2));
|
||||||
|
}
|
||||||
|
[Test]
|
||||||
|
public void Try_GetList_ByEmploymentDate_Test()
|
||||||
|
{
|
||||||
|
InsertWorkerToDatabaseAndReturn(Guid.NewGuid().ToString(), "fio 1", employmentDate: DateTime.UtcNow.AddDays(-2), email: "test2@gmail.com");
|
||||||
|
InsertWorkerToDatabaseAndReturn(Guid.NewGuid().ToString(), "fio 2", employmentDate: DateTime.UtcNow.AddDays(-1), email: "test4@gmail.com");
|
||||||
|
InsertWorkerToDatabaseAndReturn(Guid.NewGuid().ToString(), "fio 3", employmentDate: DateTime.UtcNow.AddDays(1), email: "test3@gmail.com");
|
||||||
|
InsertWorkerToDatabaseAndReturn(Guid.NewGuid().ToString(), "fio 4", employmentDate: DateTime.UtcNow.AddDays(2), email: "test11@gmail.com");
|
||||||
|
var list = _workerStorageContract.GetList(fromEmploymentDate: DateTime.UtcNow.AddDays(-1).AddMinutes(-1), toEmploymentDate: DateTime.UtcNow.AddDays(1).AddMinutes(1));
|
||||||
|
Assert.That(list, Is.Not.Null);
|
||||||
|
Assert.That(list, Has.Count.EqualTo(2));
|
||||||
|
}
|
||||||
|
[Test]
|
||||||
|
public void Try_GetList_ByAllParameters_Test()
|
||||||
|
{
|
||||||
|
var jobTitleId = Guid.NewGuid().ToString();
|
||||||
|
InsertWorkerToDatabaseAndReturn(Guid.NewGuid().ToString(), "fio 1", jobTitleId, email: "test2@gmail.com", birthDate: DateTime.UtcNow.AddYears(-25), employmentDate: DateTime.UtcNow.AddDays(-2));
|
||||||
|
InsertWorkerToDatabaseAndReturn(Guid.NewGuid().ToString(), "fio 2", jobTitleId, email: "test4@gmail.com", birthDate: DateTime.UtcNow.AddYears(-22), employmentDate: DateTime.UtcNow.AddDays(-1));
|
||||||
|
InsertWorkerToDatabaseAndReturn(Guid.NewGuid().ToString(), "fio 3", jobTitleId, email: "test6@gmail.com", birthDate: DateTime.UtcNow.AddYears(-21), employmentDate: DateTime.UtcNow.AddDays(-1));
|
||||||
|
InsertWorkerToDatabaseAndReturn(Guid.NewGuid().ToString(), "fio 4", birthDate: DateTime.UtcNow.AddYears(-20), employmentDate: DateTime.UtcNow.AddDays(1));
|
||||||
|
var list = _workerStorageContract.GetList(roleId: jobTitleId, fromBirthDate: DateTime.UtcNow.AddYears(-21).AddMinutes(-1),
|
||||||
|
toBirthDate: DateTime.UtcNow.AddYears(-20).AddMinutes(1), fromEmploymentDate: DateTime.UtcNow.AddDays(-1).AddMinutes(-1), toEmploymentDate: DateTime.UtcNow.AddDays(1).AddMinutes(1));
|
||||||
|
Assert.That(list, Is.Not.Null);
|
||||||
|
Assert.That(list, Has.Count.EqualTo(1));
|
||||||
|
}
|
||||||
|
[Test]
|
||||||
|
public void Try_GetElementById_WhenHaveRecord_Test()
|
||||||
|
{
|
||||||
|
var worker = InsertWorkerToDatabaseAndReturn(Guid.NewGuid().ToString());
|
||||||
|
AssertElement(_workerStorageContract.GetElementById(worker.Id), worker);
|
||||||
|
}
|
||||||
|
[Test]
|
||||||
|
public void Try_GetElementById_WhenNoRecord_Test()
|
||||||
|
{
|
||||||
|
Assert.That(() => _workerStorageContract.GetElementById(Guid.NewGuid().ToString()), Is.Null);
|
||||||
|
}
|
||||||
|
[Test]
|
||||||
|
public void Try_GetElementByEmail_WhenHaveRecord_Test()
|
||||||
|
{
|
||||||
|
var worker = InsertWorkerToDatabaseAndReturn(Guid.NewGuid().ToString());
|
||||||
|
AssertElement(_workerStorageContract.GetElementByEmail(worker.Email), worker);
|
||||||
|
}
|
||||||
|
[Test]
|
||||||
|
public void Try_GetElementByEmail_WhenNoRecord_Test()
|
||||||
|
{
|
||||||
|
Assert.That(() => _workerStorageContract.GetElementByEmail("New email"), Is.Null);
|
||||||
|
}
|
||||||
|
[Test]
|
||||||
|
public void Try_AddElement_Test()
|
||||||
|
{
|
||||||
|
var worker = CreateModel(Guid.NewGuid().ToString());
|
||||||
|
_workerStorageContract.AddElement(worker);
|
||||||
|
AssertElement(GetWorkerFromDatabase(worker.Id), worker);
|
||||||
|
}
|
||||||
|
[Test]
|
||||||
|
public void Try_AddElement_WhenHaveRecordWithSameId_Test()
|
||||||
|
{
|
||||||
|
var worker = CreateModel(Guid.NewGuid().ToString());
|
||||||
|
InsertWorkerToDatabaseAndReturn(worker.Id);
|
||||||
|
Assert.That(() => _workerStorageContract.AddElement(worker), Throws.TypeOf<ElementExistsException>());
|
||||||
|
}
|
||||||
|
[Test]
|
||||||
|
public void Try_UpdElement_Test()
|
||||||
|
{
|
||||||
|
var worker = CreateModel(Guid.NewGuid().ToString(), "New Fio", email: "test23@gmail.com");
|
||||||
|
InsertWorkerToDatabaseAndReturn(worker.Id);
|
||||||
|
_workerStorageContract.UpdElement(worker);
|
||||||
|
AssertElement(GetWorkerFromDatabase(worker.Id), worker);
|
||||||
|
}
|
||||||
|
[Test]
|
||||||
|
public void Try_UpdElement_WhenNoRecordWithThisId_Test()
|
||||||
|
{
|
||||||
|
Assert.That(() => _workerStorageContract.UpdElement(CreateModel(Guid.NewGuid().ToString())), Throws.TypeOf<ElementNotFoundException>());
|
||||||
|
}
|
||||||
|
[Test]
|
||||||
|
public void Try_UpdElement_WhenNoRecordWasDeleted_Test()
|
||||||
|
{
|
||||||
|
var worker = CreateModel(Guid.NewGuid().ToString(), email: "test255@gmail.com");
|
||||||
|
InsertWorkerToDatabaseAndReturn(worker.Id, isDeleted: true);
|
||||||
|
Assert.That(() => _workerStorageContract.UpdElement(worker), Throws.TypeOf<ElementNotFoundException>());
|
||||||
|
}
|
||||||
|
[Test]
|
||||||
|
public void Try_DelElement_Test()
|
||||||
|
{
|
||||||
|
var worker = InsertWorkerToDatabaseAndReturn(Guid.NewGuid().ToString());
|
||||||
|
_workerStorageContract.DelElement(worker.Id);
|
||||||
|
var element = GetWorkerFromDatabase(worker.Id);
|
||||||
|
Assert.That(element, Is.Not.Null);
|
||||||
|
Assert.That(element.IsDeleted);
|
||||||
|
}
|
||||||
|
[Test]
|
||||||
|
public void Try_DelElement_WhenNoRecordWithThisId_Test()
|
||||||
|
{
|
||||||
|
Assert.That(() => _workerStorageContract.DelElement(Guid.NewGuid().ToString()), Throws.TypeOf<ElementNotFoundException>());
|
||||||
|
}
|
||||||
|
[Test]
|
||||||
|
public void Try_DelElement_WhenNoRecordWasDeleted_Test()
|
||||||
|
{
|
||||||
|
var worker = CreateModel(Guid.NewGuid().ToString());
|
||||||
|
InsertWorkerToDatabaseAndReturn(worker.Id, isDeleted: true);
|
||||||
|
Assert.That(() => _workerStorageContract.DelElement(worker.Id), Throws.TypeOf<ElementNotFoundException>());
|
||||||
|
}
|
||||||
|
private Worker InsertWorkerToDatabaseAndReturn(string id, string fio = "test", string? jobTitleId = null, DateTime? birthDate = null, DateTime?
|
||||||
|
employmentDate = null, bool isDeleted = false, string email = "test@gmail.com")
|
||||||
|
{
|
||||||
|
var worker = new Worker()
|
||||||
|
{
|
||||||
|
Id = id,
|
||||||
|
FullName = fio,
|
||||||
|
Email = email,
|
||||||
|
RoleId = jobTitleId ?? Guid.NewGuid().ToString(),
|
||||||
|
BirthDate = birthDate ?? DateTime.UtcNow.AddYears(-20),
|
||||||
|
EmploymentDate = employmentDate ?? DateTime.UtcNow,
|
||||||
|
IsDeleted = isDeleted
|
||||||
|
};
|
||||||
|
WildPlumDbContext.Workers.Add(worker);
|
||||||
|
WildPlumDbContext.SaveChanges();
|
||||||
|
return worker;
|
||||||
|
}
|
||||||
|
private static void AssertElement(WorkerDataModel? actual, Worker expected)
|
||||||
|
{
|
||||||
|
Assert.That(actual, Is.Not.Null);
|
||||||
|
Assert.Multiple(() =>
|
||||||
|
{
|
||||||
|
Assert.That(actual.Id, Is.EqualTo(expected.Id));
|
||||||
|
Assert.That(actual.RoleId, Is.EqualTo(expected.RoleId));
|
||||||
|
Assert.That(actual.FullName, Is.EqualTo(expected.FullName));
|
||||||
|
Assert.That(actual.Email, Is.EqualTo(expected.Email));
|
||||||
|
Assert.That(actual.BirthDate, Is.EqualTo(expected.BirthDate));
|
||||||
|
Assert.That(actual.EmploymentDate, Is.EqualTo(expected.EmploymentDate));
|
||||||
|
Assert.That(actual.IsDeleted, Is.EqualTo(expected.IsDeleted));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
private static WorkerDataModel CreateModel(string id, string fio = "fio", string? jobTitleId = null, DateTime? birthDate = null,
|
||||||
|
DateTime? employmentDate = null, bool isDeleted = false, string email = "test@gmail.com") =>
|
||||||
|
new(id, fio, email, jobTitleId ?? Guid.NewGuid().ToString(), birthDate ?? DateTime.UtcNow.AddYears(-20), employmentDate ?? DateTime.UtcNow, isDeleted);
|
||||||
|
|
||||||
|
private Worker? GetWorkerFromDatabase(string id) => WildPlumDbContext.Workers.FirstOrDefault(x => x.Id == id);
|
||||||
|
|
||||||
|
private static void AssertElement(Worker? actual, WorkerDataModel expected)
|
||||||
|
{
|
||||||
|
Assert.That(actual, Is.Not.Null); Assert.Multiple(() =>
|
||||||
|
{
|
||||||
|
Assert.That(actual.Id, Is.EqualTo(expected.Id));
|
||||||
|
Assert.That(actual.RoleId, Is.EqualTo(expected.RoleId));
|
||||||
|
Assert.That(actual.FullName, Is.EqualTo(expected.FullName));
|
||||||
|
Assert.That(actual.Email, Is.EqualTo(expected.Email));
|
||||||
|
Assert.That(actual.BirthDate, Is.EqualTo(expected.BirthDate));
|
||||||
|
Assert.That(actual.EmploymentDate, Is.EqualTo(expected.EmploymentDate));
|
||||||
|
Assert.That(actual.IsDeleted, Is.EqualTo(expected.IsDeleted));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,305 @@
|
|||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using WildPlumContracts.DataModels;
|
||||||
|
using WildPlumContracts.Exceptions;
|
||||||
|
using WildPlumDatabase.Implementations;
|
||||||
|
using WildPlumDatabase.Models;
|
||||||
|
using WildPlumContracts.Enums;
|
||||||
|
|
||||||
|
namespace WildPlumTests.StoragesContractsTests;
|
||||||
|
|
||||||
|
internal class OrderStorageContractTests : BaseStorageContractTests
|
||||||
|
{
|
||||||
|
private OrderStorageContract _orderStorageContract;
|
||||||
|
private Warehouse _warehouse;
|
||||||
|
private Worker _worker;
|
||||||
|
private Product _product;
|
||||||
|
[SetUp]
|
||||||
|
public void SetUp()
|
||||||
|
{
|
||||||
|
_orderStorageContract = new OrderStorageContract(WildPlumDbContext);
|
||||||
|
_warehouse = InsertBuyerToDatabaseAndReturn();
|
||||||
|
_worker = InsertWorkerToDatabaseAndReturn();
|
||||||
|
_product = InsertProductToDatabaseAndReturn();
|
||||||
|
}
|
||||||
|
[TearDown]
|
||||||
|
public void TearDown()
|
||||||
|
{
|
||||||
|
WildPlumDbContext.Database.ExecuteSqlRaw("TRUNCATE \"Orders\" CASCADE; ");
|
||||||
|
WildPlumDbContext.Database.ExecuteSqlRaw("TRUNCATE \"Warehouses\" CASCADE; ");
|
||||||
|
WildPlumDbContext.Database.ExecuteSqlRaw("TRUNCATE \"Workers\" CASCADE; ");
|
||||||
|
WildPlumDbContext.Database.ExecuteSqlRaw("TRUNCATE \"Products\" CASCADE; ");
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void Try_GetList_WhenHaveRecords_Test()
|
||||||
|
{
|
||||||
|
var Order = InsertOrderToDatabaseAndReturn(_worker.Id, _warehouse.Id, products: [(_product.Id, 1)]);
|
||||||
|
InsertOrderToDatabaseAndReturn(_worker.Id, _warehouse.Id, products: [(_product.Id, 5)]);
|
||||||
|
var list = _orderStorageContract.GetList();
|
||||||
|
Assert.That(list, Is.Not.Null);
|
||||||
|
Assert.That(list, Has.Count.EqualTo(2));
|
||||||
|
AssertElement(list.First(x => x.Id == Order.Id), Order);
|
||||||
|
}
|
||||||
|
[Test]
|
||||||
|
public void Try_GetList_WhenNoRecords_Test()
|
||||||
|
{
|
||||||
|
var list = _orderStorageContract.GetList();
|
||||||
|
Assert.That(list, Is.Not.Null);
|
||||||
|
Assert.That(list, Is.Empty);
|
||||||
|
}
|
||||||
|
[Test]
|
||||||
|
public void Try_GetList_ByPeriod_Test()
|
||||||
|
{
|
||||||
|
InsertOrderToDatabaseAndReturn(_worker.Id, _warehouse.Id, OrderDate: DateTime.UtcNow.AddDays(-1).AddMinutes(-3), products: [(_product.Id, 1)]);
|
||||||
|
InsertOrderToDatabaseAndReturn(_worker.Id, _warehouse.Id, OrderDate: DateTime.UtcNow.AddDays(-1).AddMinutes(3), products: [(_product.Id, 1)]);
|
||||||
|
var list = _orderStorageContract.GetList(startDate: DateTime.UtcNow.AddDays(-1), endDate: DateTime.UtcNow.AddDays(1));
|
||||||
|
Assert.That(list, Is.Not.Null);
|
||||||
|
Assert.That(list, Has.Count.EqualTo(1));
|
||||||
|
}
|
||||||
|
[Test]
|
||||||
|
public void Try_GetList_ByWorkerId_Test()
|
||||||
|
{
|
||||||
|
var worker = InsertWorkerToDatabaseAndReturn("Other worker", "test4@gmail.com");
|
||||||
|
InsertOrderToDatabaseAndReturn(_worker.Id, _warehouse.Id, products: [(_product.Id, 1)]);
|
||||||
|
InsertOrderToDatabaseAndReturn(_worker.Id, _warehouse.Id, products: [(_product.Id, 1)]);
|
||||||
|
var list = _orderStorageContract.GetList(employeerId: _worker.Id);
|
||||||
|
Assert.That(list, Is.Not.Null);
|
||||||
|
Assert.That(list, Has.Count.EqualTo(2));
|
||||||
|
Assert.That(list.All(x => x.WorkerId == _worker.Id));
|
||||||
|
}
|
||||||
|
[Test]
|
||||||
|
public void Try_GetList_ByProductId_Test()
|
||||||
|
{
|
||||||
|
var product = InsertProductToDatabaseAndReturn("Other name");
|
||||||
|
InsertOrderToDatabaseAndReturn(_worker.Id, _warehouse.Id, products: [(_product.Id, 5)]);
|
||||||
|
InsertOrderToDatabaseAndReturn(_worker.Id, _warehouse.Id, products: [(_product.Id, 1), (product.Id, 4)]);
|
||||||
|
var list = _orderStorageContract.GetList(productId: _product.Id);
|
||||||
|
Assert.That(list, Is.Not.Null);
|
||||||
|
Assert.That(list, Has.Count.EqualTo(2));
|
||||||
|
Assert.That(list.All(x => x.Products!.Any(y => y.ProductId ==
|
||||||
|
_product.Id)));
|
||||||
|
}
|
||||||
|
[Test]
|
||||||
|
public void Try_GetList_ByAllParameters_Test()
|
||||||
|
{
|
||||||
|
var worker = InsertWorkerToDatabaseAndReturn("Other worker", "test2@gmail.com");
|
||||||
|
var buyer = InsertBuyerToDatabaseAndReturn("Other name1", "location");
|
||||||
|
var product = InsertProductToDatabaseAndReturn("Other name2");
|
||||||
|
InsertOrderToDatabaseAndReturn(_worker.Id, _warehouse.Id, OrderDate:
|
||||||
|
DateTime.UtcNow.AddDays(-1).AddMinutes(-3), products: [(_product.Id, 1)]);
|
||||||
|
InsertOrderToDatabaseAndReturn(worker.Id, _warehouse.Id, OrderDate:
|
||||||
|
DateTime.UtcNow.AddDays(-1).AddMinutes(3), products: [(_product.Id, 1)]);
|
||||||
|
InsertOrderToDatabaseAndReturn(worker.Id, _warehouse.Id, OrderDate:
|
||||||
|
DateTime.UtcNow.AddDays(-1).AddMinutes(3), products: [(product.Id, 1)]);
|
||||||
|
InsertOrderToDatabaseAndReturn(_worker.Id, buyer.Id, OrderDate:
|
||||||
|
DateTime.UtcNow.AddDays(1).AddMinutes(-3), products: [(_product.Id, 1)]);
|
||||||
|
InsertOrderToDatabaseAndReturn(_worker.Id, _warehouse.Id, OrderDate:
|
||||||
|
DateTime.UtcNow.AddDays(1).AddMinutes(-3), products: [(product.Id, 1)]);
|
||||||
|
var list = _orderStorageContract.GetList(startDate:
|
||||||
|
DateTime.UtcNow.AddDays(-1), endDate: DateTime.UtcNow.AddDays(1), employeerId: _worker.Id, productId: product.Id);
|
||||||
|
Assert.That(list, Is.Not.Null);
|
||||||
|
Assert.That(list, Has.Count.EqualTo(1));
|
||||||
|
}
|
||||||
|
[Test]
|
||||||
|
public void Try_GetElementById_WhenHaveRecord_Test()
|
||||||
|
{
|
||||||
|
var Order = InsertOrderToDatabaseAndReturn(_worker.Id, _warehouse.Id,
|
||||||
|
products: [(_product.Id, 1)]);
|
||||||
|
AssertElement(_orderStorageContract.GetElementById(Order.Id), Order);
|
||||||
|
}
|
||||||
|
[Test]
|
||||||
|
public void Try_GetElementById_WhenNoRecord_Test()
|
||||||
|
{
|
||||||
|
InsertOrderToDatabaseAndReturn(_worker.Id, _warehouse.Id, products: [(_product.Id, 1)]);
|
||||||
|
Assert.That(() =>
|
||||||
|
_orderStorageContract.GetElementById(Guid.NewGuid().ToString()), Is.Null);
|
||||||
|
}
|
||||||
|
[Test]
|
||||||
|
public void Try_GetElementById_WhenRecordHasCanceled_Test()
|
||||||
|
{
|
||||||
|
var Order = InsertOrderToDatabaseAndReturn(_worker.Id, _warehouse.Id, products: [(_product.Id, 1)], customerName: "fio fio");
|
||||||
|
AssertElement(_orderStorageContract.GetElementById(Order.Id), Order);
|
||||||
|
}
|
||||||
|
[Test]
|
||||||
|
|
||||||
|
public void Try_AddElement_Test()
|
||||||
|
{
|
||||||
|
var order = CreateModel(Guid.NewGuid().ToString(), _worker.Id, _warehouse.Id, "fio fio", OrderStatus.Pending, [_product.Id]);
|
||||||
|
_orderStorageContract.AddElement(order);
|
||||||
|
AssertElement(GetOrderFromDatabaseById(order.Id), order);
|
||||||
|
}
|
||||||
|
[Test]
|
||||||
|
public void Try_AddElement_WhenIsDeletedIsTrue_Test()
|
||||||
|
{
|
||||||
|
var Order = CreateModel(Guid.NewGuid().ToString(), _worker.Id, _warehouse.Id, "fio fio", OrderStatus.Pending, [_product.Id]);
|
||||||
|
Assert.That(() => _orderStorageContract.AddElement(Order), Throws.Nothing);
|
||||||
|
AssertElement(GetOrderFromDatabaseById(Order.Id), CreateModel(Order.Id, _worker.Id, _warehouse.Id, "fio fio", OrderStatus.Pending, [_product.Id]));
|
||||||
|
}
|
||||||
|
[Test]
|
||||||
|
public void Try_DelElement_Test()
|
||||||
|
{
|
||||||
|
var Order = InsertOrderToDatabaseAndReturn(_worker.Id, _warehouse.Id,
|
||||||
|
products: [(_product.Id, 1)], customerName: "name fio");
|
||||||
|
_orderStorageContract.DelElement(Order.Id);
|
||||||
|
var element = GetOrderFromDatabaseById(Order.Id);
|
||||||
|
Assert.That(element, Is.Null);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void Try_DelElement_WhenNoRecordWithThisId_Test()
|
||||||
|
{
|
||||||
|
Assert.That(() => _orderStorageContract.DelElement(Guid.NewGuid().ToString()), Throws.TypeOf<ElementNotFoundException>());
|
||||||
|
}
|
||||||
|
|
||||||
|
private OrderHistory InsertProductHistoryToDatabaseAndReturn(string productId, OrderStatus status, DateTime changeDate)
|
||||||
|
{
|
||||||
|
var orderHistory = new OrderHistory()
|
||||||
|
{
|
||||||
|
Id = Guid.NewGuid().ToString(),
|
||||||
|
OrderId = productId,
|
||||||
|
OldStatus = status,
|
||||||
|
ChangeDate = changeDate
|
||||||
|
};
|
||||||
|
WildPlumDbContext.OrderHistories.Add(orderHistory);
|
||||||
|
WildPlumDbContext.SaveChanges();
|
||||||
|
return orderHistory;
|
||||||
|
}
|
||||||
|
|
||||||
|
private Warehouse InsertBuyerToDatabaseAndReturn(string name = "test", string location = "Moscow")
|
||||||
|
{
|
||||||
|
var warehouse = new Warehouse()
|
||||||
|
{
|
||||||
|
Id = Guid.NewGuid().ToString(),
|
||||||
|
Name = name,
|
||||||
|
Location = location
|
||||||
|
};
|
||||||
|
WildPlumDbContext.Warehouses.Add(warehouse);
|
||||||
|
WildPlumDbContext.SaveChanges();
|
||||||
|
return warehouse;
|
||||||
|
}
|
||||||
|
|
||||||
|
private Worker InsertWorkerToDatabaseAndReturn(string fio = "test", string email = "test@gmail.com")
|
||||||
|
{
|
||||||
|
var worker = new Worker()
|
||||||
|
{
|
||||||
|
Id = Guid.NewGuid().ToString(),
|
||||||
|
FullName = fio,
|
||||||
|
Email = email,
|
||||||
|
RoleId = Guid.NewGuid().ToString()
|
||||||
|
};
|
||||||
|
WildPlumDbContext.Workers.Add(worker); WildPlumDbContext.SaveChanges();
|
||||||
|
return worker;
|
||||||
|
}
|
||||||
|
|
||||||
|
private Product InsertProductToDatabaseAndReturn(string productName = "test", double price = 1, bool isDeleted = false)
|
||||||
|
{
|
||||||
|
var product = new Product()
|
||||||
|
{
|
||||||
|
Id = Guid.NewGuid().ToString(),
|
||||||
|
ProductName = productName,
|
||||||
|
Price = price,
|
||||||
|
IsDeleted = isDeleted
|
||||||
|
};
|
||||||
|
WildPlumDbContext.Products.Add(product);
|
||||||
|
WildPlumDbContext.SaveChanges();
|
||||||
|
return product;
|
||||||
|
}
|
||||||
|
|
||||||
|
private Order InsertOrderToDatabaseAndReturn(string workerId, string warehouseId,
|
||||||
|
DateTime? OrderDate = null, string customerName = "fio",
|
||||||
|
List<(string, int)>? products = null)
|
||||||
|
{
|
||||||
|
var Order = new Order()
|
||||||
|
{
|
||||||
|
WorkerId = workerId,
|
||||||
|
WarehouseId = warehouseId,
|
||||||
|
OrderDate = OrderDate ?? DateTime.UtcNow,
|
||||||
|
Id = Guid.NewGuid().ToString(),
|
||||||
|
CustomerName = customerName,
|
||||||
|
Products = []
|
||||||
|
};
|
||||||
|
if (products is not null)
|
||||||
|
{
|
||||||
|
foreach (var elem in products)
|
||||||
|
{
|
||||||
|
Order.Products.Add(new OrderProduct
|
||||||
|
{
|
||||||
|
ProductId = elem.Item1,
|
||||||
|
OrderId = Order.Id,
|
||||||
|
Count = elem.Item2,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
WildPlumDbContext.Orders.Add(Order);
|
||||||
|
WildPlumDbContext.SaveChanges();
|
||||||
|
return Order;
|
||||||
|
}
|
||||||
|
private static void AssertElement(OrderDataModel? actual, Order expected)
|
||||||
|
{
|
||||||
|
Assert.That(actual, Is.Not.Null);
|
||||||
|
Assert.Multiple(() =>
|
||||||
|
{
|
||||||
|
Assert.That(actual.Id, Is.EqualTo(expected.Id));
|
||||||
|
Assert.That(actual.WorkerId, Is.EqualTo(expected.WorkerId));
|
||||||
|
Assert.That(actual.WarehouseId, Is.EqualTo(expected.WarehouseId));
|
||||||
|
Assert.That(actual.CustomerName, Is.EqualTo(expected.CustomerName));
|
||||||
|
});
|
||||||
|
if (expected.Products is not null)
|
||||||
|
{
|
||||||
|
Assert.That(actual.Products, Is.Not.Null); Assert.That(actual.Products,
|
||||||
|
Has.Count.EqualTo(expected.Products.Count));
|
||||||
|
for (int i = 0; i < actual.Products.Count; ++i)
|
||||||
|
{
|
||||||
|
Assert.Multiple(() =>
|
||||||
|
{
|
||||||
|
Assert.That(actual.Products[i].ProductId,
|
||||||
|
Is.EqualTo(expected.Products[i].ProductId));
|
||||||
|
Assert.That(actual.Products[i].Count,
|
||||||
|
Is.EqualTo(expected.Products[i].Count));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Assert.That(actual.Products, Is.Null);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
private static OrderDataModel CreateModel(string id, string workerId, string warehouseId, string customerName, OrderStatus orderStatus, List<string> productIds)
|
||||||
|
{
|
||||||
|
var products = productIds.Select(x => new OrderProductDataModel(x, id, 1)).ToList();
|
||||||
|
return new(id, warehouseId, workerId, customerName, orderStatus, DateTime.UtcNow, products);
|
||||||
|
}
|
||||||
|
|
||||||
|
private Order? GetOrderFromDatabaseById(string id) => WildPlumDbContext.Orders.Include(x => x.Products).FirstOrDefault(x => x.Id == id);
|
||||||
|
|
||||||
|
private static void AssertElement(Order? actual, OrderDataModel expected)
|
||||||
|
{
|
||||||
|
Assert.That(actual, Is.Not.Null);
|
||||||
|
Assert.Multiple(() =>
|
||||||
|
{
|
||||||
|
Assert.That(actual.Id, Is.EqualTo(expected.Id));
|
||||||
|
Assert.That(actual.WorkerId, Is.EqualTo(expected.WorkerId));
|
||||||
|
Assert.That(actual.WarehouseId, Is.EqualTo(expected.WarehouseId));
|
||||||
|
Assert.That(actual.Status, Is.EqualTo(expected.Status));
|
||||||
|
});
|
||||||
|
if (expected.Products is not null)
|
||||||
|
{
|
||||||
|
Assert.That(actual.Products, Is.Not.Null);
|
||||||
|
Assert.That(actual.Products, Has.Count.EqualTo(expected.Products.Count));
|
||||||
|
for (int i = 0; i < actual.Products.Count; ++i)
|
||||||
|
{
|
||||||
|
Assert.Multiple(() =>
|
||||||
|
{
|
||||||
|
Assert.That(actual.Products[i].ProductId,
|
||||||
|
Is.EqualTo(expected.Products[i].ProductId));
|
||||||
|
Assert.That(actual.Products[i].Count,
|
||||||
|
Is.EqualTo(expected.Products[i].Count));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Assert.That(actual.Products, Is.Null);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,264 @@
|
|||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using WildPlumContracts.DataModels;
|
||||||
|
using WildPlumContracts.Exceptions;
|
||||||
|
using WildPlumDatabase.Implementations;
|
||||||
|
using WildPlumDatabase.Models;
|
||||||
|
|
||||||
|
namespace WildPlumTests.StoragesContractsTests;
|
||||||
|
|
||||||
|
[TestFixture]
|
||||||
|
internal class ProductStorageContractTests : BaseStorageContractTests
|
||||||
|
{
|
||||||
|
private ProductStorageContract _productStorageContract;
|
||||||
|
|
||||||
|
[SetUp]
|
||||||
|
public void SetUp()
|
||||||
|
{
|
||||||
|
_productStorageContract = new ProductStorageContract(WildPlumDbContext);
|
||||||
|
}
|
||||||
|
[TearDown]
|
||||||
|
public void TearDown()
|
||||||
|
{
|
||||||
|
WildPlumDbContext.Database.ExecuteSqlRaw("TRUNCATE \"Products\" CASCADE; ");
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void Try_GetList_WhenHaveRecords_Test()
|
||||||
|
{
|
||||||
|
var product =
|
||||||
|
InsertProductToDatabaseAndReturn(Guid.NewGuid().ToString(), "name 1");
|
||||||
|
InsertProductToDatabaseAndReturn(Guid.NewGuid().ToString(), "name 2");
|
||||||
|
InsertProductToDatabaseAndReturn(Guid.NewGuid().ToString(), "name 3");
|
||||||
|
var list = _productStorageContract.GetList();
|
||||||
|
Assert.That(list, Is.Not.Null);
|
||||||
|
Assert.That(list, Has.Count.EqualTo(3));
|
||||||
|
AssertElement(list.First(x => x.Id == product.Id), product);
|
||||||
|
}
|
||||||
|
[Test]
|
||||||
|
public void Try_GetList_WhenNoRecords_Test()
|
||||||
|
{
|
||||||
|
var list = _productStorageContract.GetList();
|
||||||
|
Assert.That(list, Is.Not.Null);
|
||||||
|
Assert.That(list, Is.Empty);
|
||||||
|
}
|
||||||
|
[Test]
|
||||||
|
public void Try_GetList_OnlyActual_Test()
|
||||||
|
{
|
||||||
|
InsertProductToDatabaseAndReturn(Guid.NewGuid().ToString(), "name 1", isDeleted: true);
|
||||||
|
InsertProductToDatabaseAndReturn(Guid.NewGuid().ToString(), "name 2", isDeleted: false);
|
||||||
|
InsertProductToDatabaseAndReturn(Guid.NewGuid().ToString(), "name 3", isDeleted: false);
|
||||||
|
var list = _productStorageContract.GetList(onlyActive: true);
|
||||||
|
Assert.That(list, Is.Not.Null);
|
||||||
|
Assert.Multiple(() =>
|
||||||
|
{
|
||||||
|
Assert.That(list, Has.Count.EqualTo(2));
|
||||||
|
Assert.That(!list.Any(x => x.IsDeleted));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void Try_GetList_IncludeNoActual_Test()
|
||||||
|
{
|
||||||
|
InsertProductToDatabaseAndReturn(Guid.NewGuid().ToString(), "name 1", isDeleted: true);
|
||||||
|
InsertProductToDatabaseAndReturn(Guid.NewGuid().ToString(), "name 2", isDeleted: true);
|
||||||
|
InsertProductToDatabaseAndReturn(Guid.NewGuid().ToString(), "name 3", isDeleted: false);
|
||||||
|
var list = _productStorageContract.GetList(onlyActive: false);
|
||||||
|
Assert.That(list, Is.Not.Null);
|
||||||
|
Assert.Multiple(() =>
|
||||||
|
{
|
||||||
|
Assert.That(list, Has.Count.EqualTo(3));
|
||||||
|
Assert.That(list.Count(x => x.IsDeleted), Is.EqualTo(2));
|
||||||
|
Assert.That(list.Count(x => !x.IsDeleted), Is.EqualTo(1));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void Try_GetElementById_WhenHaveRecord_Test()
|
||||||
|
{
|
||||||
|
var product = InsertProductToDatabaseAndReturn(Guid.NewGuid().ToString());
|
||||||
|
AssertElement(_productStorageContract.GetElementById(product.Id), product);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void Try_GetElementById_WhenNoRecord_Test()
|
||||||
|
{
|
||||||
|
InsertProductToDatabaseAndReturn(Guid.NewGuid().ToString());
|
||||||
|
Assert.That(() => _productStorageContract.GetElementById(Guid.NewGuid().ToString()), Is.Null);
|
||||||
|
}
|
||||||
|
[Test]
|
||||||
|
public void Try_GetElementById_WhenRecordHasDeleted_Test()
|
||||||
|
{
|
||||||
|
var product =
|
||||||
|
InsertProductToDatabaseAndReturn(Guid.NewGuid().ToString(), isDeleted: true);
|
||||||
|
Assert.That(() => _productStorageContract.GetElementById(product.Id), Is.Null);
|
||||||
|
}
|
||||||
|
[Test]
|
||||||
|
public void Try_GetElementByName_WhenHaveRecord_Test()
|
||||||
|
{
|
||||||
|
var product =
|
||||||
|
InsertProductToDatabaseAndReturn(Guid.NewGuid().ToString());
|
||||||
|
AssertElement(_productStorageContract.GetElementByName(product.ProductName), product);
|
||||||
|
}
|
||||||
|
[Test]
|
||||||
|
public void Try_GetElementByName_WhenNoRecord_Test()
|
||||||
|
{
|
||||||
|
InsertProductToDatabaseAndReturn(Guid.NewGuid().ToString());
|
||||||
|
Assert.That(() => _productStorageContract.GetElementByName("name"), Is.Null);
|
||||||
|
}
|
||||||
|
[Test]
|
||||||
|
public void Try_GetElementByName_WhenRecordHasDeleted_Test()
|
||||||
|
{
|
||||||
|
var product = InsertProductToDatabaseAndReturn(Guid.NewGuid().ToString(), isDeleted: true);
|
||||||
|
Assert.That(() => _productStorageContract.GetElementById(product.ProductName), Is.Null);
|
||||||
|
}
|
||||||
|
[Test]
|
||||||
|
public void Try_AddElement_Test()
|
||||||
|
{
|
||||||
|
var product = CreateModel(Guid.NewGuid().ToString(), isDeleted: false);
|
||||||
|
_productStorageContract.AddElement(product);
|
||||||
|
AssertElement(GetProductFromDatabaseById(product.Id), product);
|
||||||
|
}
|
||||||
|
[Test]
|
||||||
|
public void Try_AddElement_WhenIsDeletedIsTrue_Test()
|
||||||
|
{
|
||||||
|
var product = CreateModel(Guid.NewGuid().ToString(), isDeleted: true);
|
||||||
|
Assert.That(() => _productStorageContract.AddElement(product), Throws.Nothing);
|
||||||
|
AssertElement(GetProductFromDatabaseById(product.Id), CreateModel(product.Id, isDeleted: false));
|
||||||
|
}
|
||||||
|
[Test]
|
||||||
|
public void Try_AddElement_WhenHaveRecordWithSameId_Test()
|
||||||
|
{
|
||||||
|
var product = CreateModel(Guid.NewGuid().ToString());
|
||||||
|
InsertProductToDatabaseAndReturn(product.Id, productName: "name unique");
|
||||||
|
Assert.That(() => _productStorageContract.AddElement(product), Throws.TypeOf<ElementExistsException>());
|
||||||
|
}
|
||||||
|
[Test]
|
||||||
|
public void Try_AddElement_WhenHaveRecordWithSameName_Test()
|
||||||
|
{
|
||||||
|
var product = CreateModel(Guid.NewGuid().ToString(), "name unique", isDeleted: false);
|
||||||
|
InsertProductToDatabaseAndReturn(Guid.NewGuid().ToString(), productName: product.ProductName, isDeleted: false);
|
||||||
|
Assert.That(() => _productStorageContract.AddElement(product), Throws.TypeOf<ElementExistsException>());
|
||||||
|
}
|
||||||
|
[Test]
|
||||||
|
public void Try_AddElement_WhenHaveRecordWithSameNameButOneWasDeleted_Test()
|
||||||
|
{
|
||||||
|
var product = CreateModel(Guid.NewGuid().ToString(), "name unique", isDeleted: false);
|
||||||
|
InsertProductToDatabaseAndReturn(Guid.NewGuid().ToString(), productName: product.ProductName, isDeleted: true);
|
||||||
|
Assert.That(() => _productStorageContract.AddElement(product), Throws.Nothing);
|
||||||
|
}
|
||||||
|
[Test]
|
||||||
|
public void Try_UpdElement_Test()
|
||||||
|
{
|
||||||
|
var product = CreateModel(Guid.NewGuid().ToString(), isDeleted: false);
|
||||||
|
InsertProductToDatabaseAndReturn(product.Id, isDeleted: false);
|
||||||
|
_productStorageContract.UpdElement(product);
|
||||||
|
AssertElement(GetProductFromDatabaseById(product.Id), product);
|
||||||
|
}
|
||||||
|
[Test]
|
||||||
|
public void Try_UpdElement_WhenIsDeletedIsTrue_Test()
|
||||||
|
{
|
||||||
|
var product = CreateModel(Guid.NewGuid().ToString(), isDeleted: true);
|
||||||
|
InsertProductToDatabaseAndReturn(product.Id, isDeleted: false);
|
||||||
|
_productStorageContract.UpdElement(product);
|
||||||
|
AssertElement(GetProductFromDatabaseById(product.Id), CreateModel(product.Id, isDeleted: false));
|
||||||
|
}
|
||||||
|
[Test]
|
||||||
|
public void Try_UpdElement_WhenNoRecordWithThisId_Test()
|
||||||
|
{
|
||||||
|
Assert.That(() => _productStorageContract.UpdElement(CreateModel(Guid.NewGuid().ToString())), Throws.TypeOf<ElementNotFoundException>());
|
||||||
|
}
|
||||||
|
[Test]
|
||||||
|
public void Try_UpdElement_WhenHaveRecordWithSameName_Test()
|
||||||
|
{
|
||||||
|
var product = CreateModel(Guid.NewGuid().ToString(), "name unique", isDeleted: false);
|
||||||
|
InsertProductToDatabaseAndReturn(product.Id, productName: "name");
|
||||||
|
InsertProductToDatabaseAndReturn(Guid.NewGuid().ToString(), productName: product.ProductName);
|
||||||
|
Assert.That(() => _productStorageContract.UpdElement(product),
|
||||||
|
Throws.TypeOf<ElementExistsException>());
|
||||||
|
}
|
||||||
|
[Test]
|
||||||
|
public void Try_UpdElement_WhenHaveRecordWithSameNameButOneWasDeleted_Test()
|
||||||
|
{
|
||||||
|
var product = CreateModel(Guid.NewGuid().ToString(), "name unique", isDeleted: false);
|
||||||
|
InsertProductToDatabaseAndReturn(product.Id, productName: "name"); InsertProductToDatabaseAndReturn(Guid.NewGuid().ToString(), productName: product.ProductName, isDeleted: true);
|
||||||
|
Assert.That(() => _productStorageContract.UpdElement(product), Throws.Nothing);
|
||||||
|
}
|
||||||
|
[Test]
|
||||||
|
public void Try_UpdElement_WhenRecordWasDeleted_Test()
|
||||||
|
{
|
||||||
|
var product = CreateModel(Guid.NewGuid().ToString());
|
||||||
|
InsertProductToDatabaseAndReturn(product.Id, isDeleted: true);
|
||||||
|
Assert.That(() => _productStorageContract.UpdElement(product),
|
||||||
|
Throws.TypeOf<ElementNotFoundException>());
|
||||||
|
}
|
||||||
|
[Test]
|
||||||
|
public void Try_DelElement_Test()
|
||||||
|
{
|
||||||
|
var product = InsertProductToDatabaseAndReturn(Guid.NewGuid().ToString(), isDeleted: false);
|
||||||
|
_productStorageContract.DelElement(product.Id);
|
||||||
|
var element = GetProductFromDatabaseById(product.Id);
|
||||||
|
Assert.Multiple(() =>
|
||||||
|
{
|
||||||
|
Assert.That(element, Is.Not.Null);
|
||||||
|
Assert.That(element!.IsDeleted);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void Try_DelElement_WhenNoRecordWithThisId_Test()
|
||||||
|
{
|
||||||
|
Assert.That(() => _productStorageContract.DelElement(Guid.NewGuid().ToString()), Throws.TypeOf<ElementNotFoundException>());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void Try_DelElement_WhenRecordWasDeleted_Test()
|
||||||
|
{
|
||||||
|
var product =
|
||||||
|
InsertProductToDatabaseAndReturn(Guid.NewGuid().ToString(), isDeleted: true);
|
||||||
|
Assert.That(() => _productStorageContract.DelElement(product.Id), Throws.TypeOf<ElementNotFoundException>());
|
||||||
|
}
|
||||||
|
|
||||||
|
private Product InsertProductToDatabaseAndReturn(string id, string productName = "test", double price = 1, bool isDeleted = false)
|
||||||
|
{
|
||||||
|
var product = new Product()
|
||||||
|
{
|
||||||
|
Id = id,
|
||||||
|
ProductName = productName,
|
||||||
|
Price = price,
|
||||||
|
IsDeleted = isDeleted
|
||||||
|
};
|
||||||
|
WildPlumDbContext.Products.Add(product);
|
||||||
|
WildPlumDbContext.SaveChanges();
|
||||||
|
return product;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void AssertElement(ProductDataModel? actual, Product expected)
|
||||||
|
{
|
||||||
|
Assert.That(actual, Is.Not.Null);
|
||||||
|
Assert.Multiple(() =>
|
||||||
|
{
|
||||||
|
Assert.That(actual.ProductName, Is.EqualTo(expected.ProductName));
|
||||||
|
Assert.That(actual.Price, Is.EqualTo(expected.Price));
|
||||||
|
Assert.That(actual.IsDeleted, Is.EqualTo(expected.IsDeleted));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
private static ProductDataModel CreateModel(string id, string productName = "test", double price = 1, bool isDeleted = false)
|
||||||
|
=> new(id, productName, price, isDeleted);
|
||||||
|
|
||||||
|
private Product? GetProductFromDatabaseById(string id) => WildPlumDbContext.Products.FirstOrDefault(x => x.Id == id);
|
||||||
|
|
||||||
|
private static void AssertElement(Product? actual, ProductDataModel
|
||||||
|
expected)
|
||||||
|
{
|
||||||
|
Assert.That(actual, Is.Not.Null);
|
||||||
|
Assert.Multiple(() =>
|
||||||
|
{
|
||||||
|
Assert.That(actual.Id, Is.EqualTo(expected.Id));
|
||||||
|
Assert.That(actual.ProductName, Is.EqualTo(expected.ProductName));
|
||||||
|
Assert.That(actual.Price, Is.EqualTo(expected.Price));
|
||||||
|
Assert.That(actual.IsDeleted, Is.EqualTo(expected.IsDeleted));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,215 @@
|
|||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using WildPlumContracts.DataModels;
|
||||||
|
using WildPlumContracts.Enums;
|
||||||
|
using WildPlumContracts.Exceptions;
|
||||||
|
using WildPlumDatabase.Implementations;
|
||||||
|
using WildPlumDatabase.Models;
|
||||||
|
|
||||||
|
namespace WildPlumTests.StoragesContractsTests;
|
||||||
|
|
||||||
|
[TestFixture]
|
||||||
|
internal class RoleStorageContractTests : BaseStorageContractTests
|
||||||
|
{
|
||||||
|
private RoleStorageContract _jobTitleStorageContract;
|
||||||
|
|
||||||
|
[SetUp]
|
||||||
|
public void SetUp()
|
||||||
|
{
|
||||||
|
_jobTitleStorageContract = new RoleStorageContract(WildPlumDbContext);
|
||||||
|
}
|
||||||
|
[TearDown]
|
||||||
|
public void TearDown()
|
||||||
|
{
|
||||||
|
WildPlumDbContext.Database.ExecuteSqlRaw("TRUNCATE \"Roles\" CASCADE; ");
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void Try_GetList_WhenHaveRecords_Test()
|
||||||
|
{
|
||||||
|
var jobTitle = InsertJobTitleToDatabaseAndReturn(Guid.NewGuid().ToString(), WorkerRole.Courier, true, DateTime.UtcNow);
|
||||||
|
InsertJobTitleToDatabaseAndReturn(Guid.NewGuid().ToString(), WorkerRole.Courier, true, DateTime.UtcNow);
|
||||||
|
InsertJobTitleToDatabaseAndReturn(Guid.NewGuid().ToString(), WorkerRole.Courier, true, DateTime.UtcNow);
|
||||||
|
var list = _jobTitleStorageContract.GetList();
|
||||||
|
Assert.That(list, Is.Not.Null);
|
||||||
|
Assert.That(list, Has.Count.EqualTo(3));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void Try_GetList_WhenNoRecords_Test()
|
||||||
|
{
|
||||||
|
var list = _jobTitleStorageContract.GetList();
|
||||||
|
Assert.That(list, Is.Not.Null);
|
||||||
|
Assert.That(list, Is.Empty);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void Try_GetJobTitleWithHistory_WhenHaveRecords_Test()
|
||||||
|
{
|
||||||
|
var jobTitleId = Guid.NewGuid().ToString();
|
||||||
|
InsertJobTitleToDatabaseAndReturn(Guid.NewGuid().ToString(), isActual: true);
|
||||||
|
InsertJobTitleToDatabaseAndReturn(jobTitleId, isActual: true);
|
||||||
|
InsertJobTitleToDatabaseAndReturn(jobTitleId, isActual: false);
|
||||||
|
var list = _jobTitleStorageContract.GetRoleWithHistory(jobTitleId);
|
||||||
|
Assert.That(list, Is.Not.Null);
|
||||||
|
Assert.That(list, Has.Count.EqualTo(2));
|
||||||
|
}
|
||||||
|
[Test]
|
||||||
|
public void Try_GetJobTitleWithHistory_WhenNoRecords_Test()
|
||||||
|
{
|
||||||
|
var jobTitleId = Guid.NewGuid().ToString();
|
||||||
|
InsertJobTitleToDatabaseAndReturn(Guid.NewGuid().ToString(), isActual: true);
|
||||||
|
InsertJobTitleToDatabaseAndReturn(jobTitleId,isActual: true);
|
||||||
|
InsertJobTitleToDatabaseAndReturn(jobTitleId, isActual: false);
|
||||||
|
var list = _jobTitleStorageContract.GetRoleWithHistory(Guid.NewGuid().ToString());
|
||||||
|
Assert.That(list, Is.Not.Null);
|
||||||
|
Assert.That(list, Has.Count.EqualTo(0));
|
||||||
|
}
|
||||||
|
[Test]
|
||||||
|
public void Try_GetElementById_WhenHaveRecord_Test()
|
||||||
|
{
|
||||||
|
var jobTitle = InsertJobTitleToDatabaseAndReturn(Guid.NewGuid().ToString());
|
||||||
|
AssertElement(_jobTitleStorageContract.GetElementById(jobTitle.Id), jobTitle);
|
||||||
|
}
|
||||||
|
[Test]
|
||||||
|
public void Try_GetElementById_WhenNoRecord_Test()
|
||||||
|
{
|
||||||
|
InsertJobTitleToDatabaseAndReturn(Guid.NewGuid().ToString());
|
||||||
|
Assert.That(() =>
|
||||||
|
_jobTitleStorageContract.GetElementById(Guid.NewGuid().ToString()), Is.Null);
|
||||||
|
}
|
||||||
|
[Test]
|
||||||
|
public void Try_GetElementById_WhenRecordHasDeleted_Test()
|
||||||
|
{
|
||||||
|
var jobTitle = InsertJobTitleToDatabaseAndReturn(Guid.NewGuid().ToString(),
|
||||||
|
isActual: false);
|
||||||
|
Assert.That(() => _jobTitleStorageContract.GetElementById(jobTitle.RoleId), Is.Null);
|
||||||
|
}
|
||||||
|
[Test]
|
||||||
|
public void Try_AddElement_Test()
|
||||||
|
{
|
||||||
|
var jobTitle = CreateModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), true, WorkerRole.Courier, DateTime.UtcNow);
|
||||||
|
_jobTitleStorageContract.AddElement(jobTitle);
|
||||||
|
AssertElement(GetJobTitleFromDatabaseByJobTitleId(jobTitle.Id), jobTitle);
|
||||||
|
}
|
||||||
|
[Test]
|
||||||
|
public void Try_AddElement_WhenHaveRecordWithSameJobTitleId_Test()
|
||||||
|
{
|
||||||
|
var jobTitle = CreateModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), true, WorkerRole.Courier, DateTime.UtcNow);
|
||||||
|
InsertJobTitleToDatabaseAndReturn(jobTitle.Id, isActual: true);
|
||||||
|
Assert.That(() => _jobTitleStorageContract.AddElement(jobTitle), Throws.TypeOf<ElementExistsException>());
|
||||||
|
}
|
||||||
|
[Test]
|
||||||
|
public void Try_UpdElement_Test()
|
||||||
|
{
|
||||||
|
var jobTitle = CreateModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), true, WorkerRole.Courier, DateTime.UtcNow);
|
||||||
|
InsertJobTitleToDatabaseAndReturn(jobTitle.Id, isActual: true);
|
||||||
|
_jobTitleStorageContract.UpdElement(jobTitle);
|
||||||
|
var jobTitles = WildPlumDbContext.Roles.Where(x => x.RoleId == jobTitle.Id).OrderByDescending(x => x.ChangeDate);
|
||||||
|
Assert.That(jobTitles.Count(), Is.EqualTo(2));
|
||||||
|
AssertElement(jobTitles.First(), CreateModel(jobTitle.Id, Guid.NewGuid().ToString(), true, WorkerRole.Courier, DateTime.UtcNow));
|
||||||
|
AssertElement(jobTitles.Last(), CreateModel(jobTitle.Id, Guid.NewGuid().ToString(), true, WorkerRole.Courier, DateTime.UtcNow));
|
||||||
|
}
|
||||||
|
[Test]
|
||||||
|
public void Try_UpdElement_WhenNoRecordWithThisId_Test()
|
||||||
|
{
|
||||||
|
Assert.That(() => _jobTitleStorageContract.UpdElement(CreateModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), true, WorkerRole.Courier, DateTime.UtcNow)), Throws.TypeOf<ElementNotFoundException>());
|
||||||
|
}
|
||||||
|
[Test]
|
||||||
|
public void Try_UpdElement_WhenRecordWasDeleted_Test()
|
||||||
|
{
|
||||||
|
var jobTitle = CreateModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), true, WorkerRole.Courier, DateTime.UtcNow);
|
||||||
|
InsertJobTitleToDatabaseAndReturn(jobTitle.Id, isActual: false);
|
||||||
|
Assert.That(() => _jobTitleStorageContract.UpdElement(jobTitle), Throws.TypeOf<ElementDeletedException>());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void Try_DelElement_Test()
|
||||||
|
{
|
||||||
|
var jobTitle = InsertJobTitleToDatabaseAndReturn(Guid.NewGuid().ToString(), isActual: true);
|
||||||
|
_jobTitleStorageContract.DelElement(jobTitle.RoleId);
|
||||||
|
var element = GetJobTitleFromDatabaseByJobTitleId(jobTitle.RoleId);
|
||||||
|
Assert.Multiple(() =>
|
||||||
|
{
|
||||||
|
Assert.That(element, Is.Not.Null);
|
||||||
|
Assert.That(!element!.IsActual);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void Try_DelElement_WhenNoRecordWithThisId_Test()
|
||||||
|
{
|
||||||
|
Assert.That(() => _jobTitleStorageContract.DelElement(Guid.NewGuid().ToString()), Throws.TypeOf<ElementNotFoundException>());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void Try_DelElement_WhenRecordWasDeleted_Test()
|
||||||
|
{
|
||||||
|
var jobTitle = InsertJobTitleToDatabaseAndReturn(Guid.NewGuid().ToString(), isActual: false);
|
||||||
|
Assert.That(() => _jobTitleStorageContract.DelElement(jobTitle.RoleId), Throws.TypeOf<ElementDeletedException>());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void Try_ResElement_Test()
|
||||||
|
{
|
||||||
|
var jobTitle = InsertJobTitleToDatabaseAndReturn(Guid.NewGuid().ToString(), isActual: false);
|
||||||
|
_jobTitleStorageContract.ResElement(jobTitle.RoleId);
|
||||||
|
var element = GetJobTitleFromDatabaseByJobTitleId(jobTitle.RoleId);
|
||||||
|
Assert.Multiple(() =>
|
||||||
|
{
|
||||||
|
Assert.That(element, Is.Not.Null);
|
||||||
|
Assert.That(element!.IsActual);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
[Test]
|
||||||
|
public void Try_ResElement_WhenNoRecordWithThisId_Test()
|
||||||
|
{
|
||||||
|
Assert.That(() => _jobTitleStorageContract.ResElement(Guid.NewGuid().ToString()), Throws.TypeOf<ElementNotFoundException>());
|
||||||
|
}
|
||||||
|
[Test]
|
||||||
|
public void Try_ResElement_WhenRecordNotWasDeleted_Test()
|
||||||
|
{
|
||||||
|
var jobTitle = InsertJobTitleToDatabaseAndReturn(Guid.NewGuid().ToString(), isActual: true);
|
||||||
|
Assert.That(() => _jobTitleStorageContract.ResElement(jobTitle.RoleId), Throws.Nothing);
|
||||||
|
}
|
||||||
|
|
||||||
|
private Role InsertJobTitleToDatabaseAndReturn(string id, WorkerRole workerRole = WorkerRole.Courier,
|
||||||
|
bool isActual = true, DateTime? changeDate = null)
|
||||||
|
{
|
||||||
|
var jobTitle = new Role()
|
||||||
|
{
|
||||||
|
Id = Guid.NewGuid().ToString(),
|
||||||
|
RoleId = id,
|
||||||
|
WorkerRole = workerRole,
|
||||||
|
IsActual = isActual,
|
||||||
|
ChangeDate = changeDate ?? DateTime.UtcNow
|
||||||
|
};
|
||||||
|
WildPlumDbContext.Roles.Add(jobTitle);
|
||||||
|
WildPlumDbContext.SaveChanges();
|
||||||
|
return jobTitle;
|
||||||
|
}
|
||||||
|
private static void AssertElement(RoleDataModel? actual, Role expected)
|
||||||
|
{
|
||||||
|
Assert.That(actual, Is.Not.Null);
|
||||||
|
Assert.Multiple(() =>
|
||||||
|
{
|
||||||
|
Assert.That(actual.Id, Is.EqualTo(expected.RoleId));
|
||||||
|
Assert.That(actual.WorkerRole, Is.EqualTo(expected.WorkerRole));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
private static RoleDataModel CreateModel(string id, string roleId, bool isActual,
|
||||||
|
WorkerRole workerRole, DateTime changeDate)
|
||||||
|
=> new(id, workerRole, roleId, isActual, changeDate);
|
||||||
|
|
||||||
|
private Role? GetJobTitleFromDatabaseByJobTitleId(string id) =>
|
||||||
|
WildPlumDbContext.Roles.Where(x => x.RoleId == id).OrderByDescending(x => x.ChangeDate).FirstOrDefault();
|
||||||
|
|
||||||
|
private static void AssertElement(Role? actual, RoleDataModel expected)
|
||||||
|
{
|
||||||
|
Assert.That(actual, Is.Not.Null);
|
||||||
|
Assert.Multiple(() =>
|
||||||
|
{
|
||||||
|
Assert.That(actual.RoleId, Is.EqualTo(expected.Id));
|
||||||
|
Assert.That(actual.WorkerRole, Is.EqualTo(expected.WorkerRole));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,153 @@
|
|||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using WildPlumContracts.DataModels;
|
||||||
|
using WildPlumDatabase.Implementations;
|
||||||
|
using WildPlumDatabase.Models;
|
||||||
|
|
||||||
|
namespace WildPlumTests.StoragesContractsTests;
|
||||||
|
|
||||||
|
[TestFixture]
|
||||||
|
internal class SalaryStorageContractTests : BaseStorageContractTests
|
||||||
|
{
|
||||||
|
private SalaryStorageContract _salaryStorageContract;
|
||||||
|
private Worker _worker;
|
||||||
|
|
||||||
|
[SetUp]
|
||||||
|
public void SetUp()
|
||||||
|
{
|
||||||
|
_salaryStorageContract = new
|
||||||
|
SalaryStorageContract(WildPlumDbContext);
|
||||||
|
_worker = InsertWorkerToDatabaseAndReturn();
|
||||||
|
}
|
||||||
|
[TearDown]
|
||||||
|
public void TearDown()
|
||||||
|
{
|
||||||
|
WildPlumDbContext.Database.ExecuteSqlRaw("TRUNCATE \"Salaries\" CASCADE; ");
|
||||||
|
WildPlumDbContext.Database.ExecuteSqlRaw("TRUNCATE \"Workers\" CASCADE; ");
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void Try_GetList_WhenHaveRecords_Test()
|
||||||
|
{
|
||||||
|
var salary = InsertSalaryToDatabaseAndReturn(_worker.Id, workerSalary: 100);
|
||||||
|
InsertSalaryToDatabaseAndReturn(_worker.Id);
|
||||||
|
InsertSalaryToDatabaseAndReturn(_worker.Id);
|
||||||
|
var list = _salaryStorageContract.GetList(DateTime.UtcNow.AddDays(-10), DateTime.UtcNow.AddDays(10));
|
||||||
|
Assert.That(list, Is.Not.Null);
|
||||||
|
Assert.That(list, Has.Count.EqualTo(3));
|
||||||
|
AssertElement(list.First(), salary);
|
||||||
|
}
|
||||||
|
[Test]
|
||||||
|
public void Try_GetList_WhenNoRecords_Test()
|
||||||
|
{
|
||||||
|
var list = _salaryStorageContract.GetList(DateTime.UtcNow.AddDays(-10), DateTime.UtcNow.AddDays(10));
|
||||||
|
Assert.That(list, Is.Not.Null);
|
||||||
|
Assert.That(list, Is.Empty);
|
||||||
|
}
|
||||||
|
[Test]
|
||||||
|
public void Try_GetList_OnlyInDatePeriod_Test()
|
||||||
|
{
|
||||||
|
InsertSalaryToDatabaseAndReturn(_worker.Id, salaryDate: DateTime.UtcNow.AddDays(-2));
|
||||||
|
InsertSalaryToDatabaseAndReturn(_worker.Id, salaryDate: DateTime.UtcNow.AddDays(-1).AddMinutes(-5));
|
||||||
|
InsertSalaryToDatabaseAndReturn(_worker.Id, salaryDate: DateTime.UtcNow.AddDays(-1).AddMinutes(5));
|
||||||
|
InsertSalaryToDatabaseAndReturn(_worker.Id, salaryDate: DateTime.UtcNow.AddDays(1).AddMinutes(-5));
|
||||||
|
InsertSalaryToDatabaseAndReturn(_worker.Id, salaryDate: DateTime.UtcNow.AddDays(1).AddMinutes(5));
|
||||||
|
InsertSalaryToDatabaseAndReturn(_worker.Id, salaryDate: DateTime.UtcNow.AddDays(-2));
|
||||||
|
var list = _salaryStorageContract.GetList(DateTime.UtcNow.AddDays(-1), DateTime.UtcNow.AddDays(1));
|
||||||
|
Assert.That(list, Is.Not.Null);
|
||||||
|
Assert.Multiple(() =>
|
||||||
|
{
|
||||||
|
Assert.That(list, Has.Count.EqualTo(2));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
[Test]
|
||||||
|
public void Try_GetList_ByWorker_Test()
|
||||||
|
{
|
||||||
|
var worker = InsertWorkerToDatabaseAndReturn("name 2", "test2@gmail.com");
|
||||||
|
InsertSalaryToDatabaseAndReturn(_worker.Id);
|
||||||
|
InsertSalaryToDatabaseAndReturn(_worker.Id);
|
||||||
|
InsertSalaryToDatabaseAndReturn(worker.Id);
|
||||||
|
var list = _salaryStorageContract.GetList(DateTime.UtcNow.AddDays(-1), DateTime.UtcNow.AddDays(1), _worker.Id);
|
||||||
|
Assert.That(list, Is.Not.Null);
|
||||||
|
Assert.Multiple(() =>
|
||||||
|
{
|
||||||
|
Assert.That(list, Has.Count.EqualTo(2));
|
||||||
|
Assert.That(list.All(x => x.WorkerId == _worker.Id));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
[Test]
|
||||||
|
public void Try_GetList_ByWorkerOnlyInDatePeriod_Test()
|
||||||
|
{
|
||||||
|
var worker = InsertWorkerToDatabaseAndReturn("name 2", "test3@gmail.com");
|
||||||
|
InsertSalaryToDatabaseAndReturn(_worker.Id, salaryDate: DateTime.UtcNow.AddDays(-2));
|
||||||
|
InsertSalaryToDatabaseAndReturn(_worker.Id, salaryDate: DateTime.UtcNow.AddDays(-1).AddMinutes(5));
|
||||||
|
InsertSalaryToDatabaseAndReturn(worker.Id, salaryDate: DateTime.UtcNow.AddDays(-1).AddMinutes(5));
|
||||||
|
InsertSalaryToDatabaseAndReturn(_worker.Id, salaryDate: DateTime.UtcNow.AddDays(1).AddMinutes(-5));
|
||||||
|
InsertSalaryToDatabaseAndReturn(worker.Id, salaryDate: DateTime.UtcNow.AddDays(1).AddMinutes(-5));
|
||||||
|
InsertSalaryToDatabaseAndReturn(_worker.Id, salaryDate: DateTime.UtcNow.AddDays(-2));
|
||||||
|
var list = _salaryStorageContract.GetList(DateTime.UtcNow.AddDays(-1), DateTime.UtcNow.AddDays(1), _worker.Id);
|
||||||
|
Assert.That(list, Is.Not.Null);
|
||||||
|
Assert.Multiple(() =>
|
||||||
|
{
|
||||||
|
Assert.That(list, Has.Count.EqualTo(2));
|
||||||
|
Assert.That(list.All(x => x.WorkerId == _worker.Id));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
[Test]
|
||||||
|
public void Try_AddElement_Test()
|
||||||
|
{
|
||||||
|
var salary = CreateModel(_worker.Id);
|
||||||
|
_salaryStorageContract.AddElement(salary);
|
||||||
|
AssertElement(GetSalaryFromDatabaseByWorkerId(_worker.Id), salary);
|
||||||
|
}
|
||||||
|
private Worker InsertWorkerToDatabaseAndReturn(string workerFIO = "fio", string workerEmail = "test@gmail.com")
|
||||||
|
{
|
||||||
|
var worker = new Worker()
|
||||||
|
{
|
||||||
|
Id = Guid.NewGuid().ToString(),
|
||||||
|
Email = workerEmail,
|
||||||
|
RoleId = Guid.NewGuid().ToString(),
|
||||||
|
FullName = workerFIO,
|
||||||
|
IsDeleted = false
|
||||||
|
};
|
||||||
|
WildPlumDbContext.Workers.Add(worker);
|
||||||
|
WildPlumDbContext.SaveChanges();
|
||||||
|
return worker;
|
||||||
|
}
|
||||||
|
|
||||||
|
private Salary InsertSalaryToDatabaseAndReturn(string workerId, double workerSalary = 1, DateTime? salaryDate = null)
|
||||||
|
{
|
||||||
|
var salary = new Salary()
|
||||||
|
{
|
||||||
|
WorkerId = workerId,
|
||||||
|
Amount = workerSalary,
|
||||||
|
SalaryDate = salaryDate ?? DateTime.UtcNow
|
||||||
|
};
|
||||||
|
WildPlumDbContext.Salaries.Add(salary);
|
||||||
|
WildPlumDbContext.SaveChanges();
|
||||||
|
return salary;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void AssertElement(SalaryDataModel? actual, Salary expected)
|
||||||
|
{
|
||||||
|
Assert.That(actual, Is.Not.Null);
|
||||||
|
Assert.Multiple(() =>
|
||||||
|
{
|
||||||
|
Assert.That(actual.WorkerId, Is.EqualTo(expected.WorkerId));
|
||||||
|
Assert.That(actual.Amount, Is.EqualTo(expected.Amount));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
private static SalaryDataModel CreateModel(string workerId, decimal workerSalary = 1, DateTime? salaryDate = null) => new(Guid.NewGuid().ToString(), workerId, workerSalary, salaryDate ?? DateTime.UtcNow);
|
||||||
|
|
||||||
|
private Salary? GetSalaryFromDatabaseByWorkerId(string id) => WildPlumDbContext.Salaries.FirstOrDefault(x => x.WorkerId == id);
|
||||||
|
|
||||||
|
private static void AssertElement(Salary? actual, SalaryDataModel expected)
|
||||||
|
{
|
||||||
|
Assert.That(actual, Is.Not.Null);
|
||||||
|
Assert.Multiple(() =>
|
||||||
|
{
|
||||||
|
Assert.That(actual.WorkerId, Is.EqualTo(expected.WorkerId));
|
||||||
|
Assert.That(actual.Amount, Is.EqualTo(expected.Amount));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,155 @@
|
|||||||
|
using WildPlumDatabase.Implementations;
|
||||||
|
using WildPlumDatabase;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using WildPlumContracts.DataModels;
|
||||||
|
using WildPlumDatabase.Models;
|
||||||
|
using WildPlumContracts.Exceptions;
|
||||||
|
|
||||||
|
namespace WildPlumTests.StoragesContractsTests;
|
||||||
|
|
||||||
|
[TestFixture]
|
||||||
|
internal class WarehouseStorageContractTests : BaseStorageContractTests
|
||||||
|
{
|
||||||
|
private WarehouseStorageContract _productStorageContract;
|
||||||
|
|
||||||
|
[SetUp]
|
||||||
|
public void SetUp()
|
||||||
|
{
|
||||||
|
_productStorageContract = new WarehouseStorageContract(WildPlumDbContext);
|
||||||
|
}
|
||||||
|
[TearDown]
|
||||||
|
public void TearDown()
|
||||||
|
{
|
||||||
|
WildPlumDbContext.Database.ExecuteSqlRaw("TRUNCATE \"Warehouses\" CASCADE; ");
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void Try_GetList_WhenHaveRecords_Test()
|
||||||
|
{
|
||||||
|
var product = InsertProductToDatabaseAndReturn(Guid.NewGuid().ToString(), "name 1", location: "loc3");
|
||||||
|
InsertProductToDatabaseAndReturn(Guid.NewGuid().ToString(), "name 2", location: "loc3");
|
||||||
|
InsertProductToDatabaseAndReturn(Guid.NewGuid().ToString(), "name 3", location: "loc3");
|
||||||
|
var list = _productStorageContract.GetList();
|
||||||
|
Assert.That(list, Is.Not.Null);
|
||||||
|
Assert.That(list, Has.Count.EqualTo(3));
|
||||||
|
AssertElement(list.First(x => x.Id == product.Id), product);
|
||||||
|
}
|
||||||
|
[Test]
|
||||||
|
public void Try_GetList_WhenNoRecords_Test()
|
||||||
|
{
|
||||||
|
var list = _productStorageContract.GetList();
|
||||||
|
Assert.That(list, Is.Not.Null);
|
||||||
|
Assert.That(list, Is.Empty);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void Try_GetElementByName_WhenHaveRecord_Test()
|
||||||
|
{
|
||||||
|
var product =
|
||||||
|
InsertProductToDatabaseAndReturn(Guid.NewGuid().ToString());
|
||||||
|
AssertElement(_productStorageContract.GetElementByName(product.Name), product);
|
||||||
|
}
|
||||||
|
[Test]
|
||||||
|
public void Try_GetElementByName_WhenNoRecord_Test()
|
||||||
|
{
|
||||||
|
InsertProductToDatabaseAndReturn(Guid.NewGuid().ToString());
|
||||||
|
Assert.That(() => _productStorageContract.GetElementByName("name"), Is.Null);
|
||||||
|
}
|
||||||
|
[Test]
|
||||||
|
public void Try_AddElement_Test()
|
||||||
|
{
|
||||||
|
var product = CreateModel(Guid.NewGuid().ToString(), "name2", "loc2");
|
||||||
|
_productStorageContract.AddElement(product);
|
||||||
|
AssertElement(GetProductFromDatabaseById(product.Id), product);
|
||||||
|
}
|
||||||
|
[Test]
|
||||||
|
public void Try_AddElement_WhenHaveRecordWithSameId_Test()
|
||||||
|
{
|
||||||
|
var product = CreateModel(Guid.NewGuid().ToString());
|
||||||
|
InsertProductToDatabaseAndReturn(product.Id, name: "name unique", location: "loc2");
|
||||||
|
Assert.That(() => _productStorageContract.AddElement(product), Throws.TypeOf<ElementExistsException>());
|
||||||
|
}
|
||||||
|
[Test]
|
||||||
|
public void Try_AddElement_WhenHaveRecordWithSameName_Test()
|
||||||
|
{
|
||||||
|
var product = CreateModel(Guid.NewGuid().ToString(), "name unique", location: "loc2");
|
||||||
|
InsertProductToDatabaseAndReturn(Guid.NewGuid().ToString(), name: product.Name);
|
||||||
|
Assert.That(() => _productStorageContract.AddElement(product), Throws.TypeOf<ElementExistsException>());
|
||||||
|
}
|
||||||
|
[Test]
|
||||||
|
public void Try_UpdElement_Test()
|
||||||
|
{
|
||||||
|
var product = CreateModel(Guid.NewGuid().ToString(), "name unique", location: "loc2");
|
||||||
|
InsertProductToDatabaseAndReturn(product.Id, "name unique", location: "loc2");
|
||||||
|
_productStorageContract.UpdElement(product);
|
||||||
|
AssertElement(GetProductFromDatabaseById(product.Id), product);
|
||||||
|
}
|
||||||
|
[Test]
|
||||||
|
public void Try_UpdElement_WhenNoRecordWithThisId_Test()
|
||||||
|
{
|
||||||
|
Assert.That(() => _productStorageContract.UpdElement(CreateModel(Guid.NewGuid().ToString())), Throws.TypeOf<ElementNotFoundException>());
|
||||||
|
}
|
||||||
|
[Test]
|
||||||
|
public void Try_UpdElement_WhenHaveRecordWithSameName_Test()
|
||||||
|
{
|
||||||
|
var product = CreateModel(Guid.NewGuid().ToString(), "name unique", location: "loc2");
|
||||||
|
InsertProductToDatabaseAndReturn(product.Id, name: "name", location: "loc3");
|
||||||
|
InsertProductToDatabaseAndReturn(Guid.NewGuid().ToString(), name: product.Name, location: "loc4");
|
||||||
|
Assert.That(() => _productStorageContract.UpdElement(product),
|
||||||
|
Throws.TypeOf<ElementExistsException>());
|
||||||
|
}
|
||||||
|
[Test]
|
||||||
|
public void Try_DelElement_Test()
|
||||||
|
{
|
||||||
|
var product = InsertProductToDatabaseAndReturn(Guid.NewGuid().ToString(), "name unique", location: "loc2");
|
||||||
|
_productStorageContract.DelElement(product.Id);
|
||||||
|
var element = GetProductFromDatabaseById(product.Id);
|
||||||
|
Assert.That(element, Is.Null);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void Try_DelElement_WhenNoRecordWithThisId_Test()
|
||||||
|
{
|
||||||
|
Assert.That(() => _productStorageContract.DelElement(Guid.NewGuid().ToString()), Throws.TypeOf<ElementNotFoundException>());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private Warehouse InsertProductToDatabaseAndReturn(string id, string name = "test", string location = "loc1")
|
||||||
|
{
|
||||||
|
var product = new Warehouse()
|
||||||
|
{
|
||||||
|
Id = id,
|
||||||
|
Name = name,
|
||||||
|
Location = location
|
||||||
|
};
|
||||||
|
WildPlumDbContext.Warehouses.Add(product);
|
||||||
|
WildPlumDbContext.SaveChanges();
|
||||||
|
return product;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void AssertElement(WarehouseDataModel? actual, Warehouse expected)
|
||||||
|
{
|
||||||
|
Assert.That(actual, Is.Not.Null);
|
||||||
|
Assert.Multiple(() =>
|
||||||
|
{
|
||||||
|
Assert.That(actual.Id, Is.EqualTo(expected.Id));
|
||||||
|
Assert.That(actual.Name, Is.EqualTo(expected.Name));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
private static WarehouseDataModel CreateModel(string id, string name = "test", string location = "loc1")
|
||||||
|
=> new(id, name, location);
|
||||||
|
|
||||||
|
private Warehouse? GetProductFromDatabaseById(string id) => WildPlumDbContext.Warehouses.FirstOrDefault(x => x.Id == id);
|
||||||
|
|
||||||
|
private static void AssertElement(Warehouse? actual, WarehouseDataModel
|
||||||
|
expected)
|
||||||
|
{
|
||||||
|
Assert.That(actual, Is.Not.Null);
|
||||||
|
Assert.Multiple(() =>
|
||||||
|
{
|
||||||
|
Assert.That(actual.Id, Is.EqualTo(expected.Id));
|
||||||
|
Assert.That(actual.Name, Is.EqualTo(expected.Name));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -22,6 +22,7 @@
|
|||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ProjectReference Include="..\WildPlumBusinessLogic\WildPlumBusinessLogic.csproj" />
|
<ProjectReference Include="..\WildPlumBusinessLogic\WildPlumBusinessLogic.csproj" />
|
||||||
<ProjectReference Include="..\WildPlumContracts\WildPlumContracts.csproj" />
|
<ProjectReference Include="..\WildPlumContracts\WildPlumContracts.csproj" />
|
||||||
|
<ProjectReference Include="..\WildPlumDatabase\WildPlumDatabase.csproj" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
|||||||
Reference in New Issue
Block a user