forked from slavaxom9k/PIBD-23_Fomichev_V.S._MagicCarpet
Compare commits
27 Commits
lab02_Buis
...
lab03_Stor
| Author | SHA1 | Date | |
|---|---|---|---|
| ac4aa4a146 | |||
| ea790d5d17 | |||
| ce473a3725 | |||
| 1475434fef | |||
| 0df46ec709 | |||
| 0a00160b2f | |||
| 39dada6bca | |||
| 45a4bb91ae | |||
| a43f4b2ac1 | |||
| f8c393fcc9 | |||
| b9bb08da63 | |||
| f7b2442b3e | |||
| 9e9cfe3adf | |||
| 7b15c9c56e | |||
| a83fdd892d | |||
| 86aefae2d4 | |||
| da93722d9f | |||
| 43dc07e662 | |||
| 087a21da11 | |||
| c12efe7f92 | |||
| 18df50575b | |||
| 56af2f0e0a | |||
| 66fe491cf6 | |||
| 599475e22f | |||
| 651c170ce2 | |||
| ec2eea3184 | |||
| b7cb388d19 |
@@ -0,0 +1,74 @@
|
||||
using MagicCarpetContracts.BusinessLogicContracts;
|
||||
using MagicCarpetContracts.DataModels;
|
||||
using MagicCarpetContracts.Enums;
|
||||
using MagicCarpetContracts.Exceptions;
|
||||
using MagicCarpetContracts.Extensions;
|
||||
using MagicCarpetContracts.StoragesContracts;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MagicCarpetBusinessLogic.Implementations;
|
||||
|
||||
public class AgencyBusinessLogicContract(IAgencyStorageContract agencyStorageContract, ILogger logger) : IAgencyBusinessLogicContract
|
||||
{
|
||||
private readonly IAgencyStorageContract _agencyStorageContract = agencyStorageContract;
|
||||
private readonly ILogger _logger = logger;
|
||||
public List<AgencyDataModel> GetAllComponents()
|
||||
{
|
||||
_logger.LogInformation("GetAllComponents");
|
||||
return _agencyStorageContract.GetList() ?? throw new NullListException();
|
||||
|
||||
return [];
|
||||
}
|
||||
|
||||
public AgencyDataModel GetComponentByData(string data)
|
||||
{
|
||||
_logger.LogInformation("Get element by data: {data}", data);
|
||||
if (data.IsEmpty())
|
||||
{
|
||||
throw new ArgumentNullException(nameof(data));
|
||||
}
|
||||
if (!data.IsGuid())
|
||||
{
|
||||
throw new ElementNotFoundException(data);
|
||||
}
|
||||
return _agencyStorageContract.GetElementById(data) ?? throw new ElementNotFoundException(data);
|
||||
|
||||
return new("", TourType.None, 0, []);
|
||||
}
|
||||
|
||||
public void InsertComponent(AgencyDataModel agencyDataModel)
|
||||
{
|
||||
_logger.LogInformation("New data: {json}", JsonSerializer.Serialize(agencyDataModel));
|
||||
ArgumentNullException.ThrowIfNull(agencyDataModel);
|
||||
agencyDataModel.Validate();
|
||||
_agencyStorageContract.AddElement(agencyDataModel);
|
||||
}
|
||||
|
||||
public void UpdateComponent(AgencyDataModel agencyDataModel)
|
||||
{
|
||||
_logger.LogInformation("Update data: {json}", JsonSerializer.Serialize(agencyDataModel));
|
||||
ArgumentNullException.ThrowIfNull(agencyDataModel);
|
||||
agencyDataModel.Validate();
|
||||
_agencyStorageContract.UpdElement(agencyDataModel);
|
||||
}
|
||||
|
||||
public void DeleteComponent(string id)
|
||||
{
|
||||
logger.LogInformation("Delete by id: {id}", id);
|
||||
if (id.IsEmpty())
|
||||
{
|
||||
throw new ArgumentNullException(nameof(id));
|
||||
}
|
||||
if (!id.IsGuid())
|
||||
{
|
||||
throw new ValidationException("Id is not a unique identifier");
|
||||
}
|
||||
_agencyStorageContract.DelElement(id);
|
||||
}
|
||||
}
|
||||
@@ -13,10 +13,11 @@ using System.Threading.Tasks;
|
||||
|
||||
namespace MagicCarpetBusinessLogic.Implementations;
|
||||
|
||||
internal class SaleBusinessLogicContract(ISaleStorageContract saleStorageContract, ILogger logger) : ISaleBusinessLogicContract
|
||||
internal class SaleBusinessLogicContract(ISaleStorageContract saleStorageContract, IAgencyStorageContract agencyStorageContract, ILogger logger) : ISaleBusinessLogicContract
|
||||
{
|
||||
private readonly ILogger _logger = logger;
|
||||
private readonly ISaleStorageContract _saleStorageContract = saleStorageContract;
|
||||
private readonly IAgencyStorageContract _agencyStorageContract = agencyStorageContract;
|
||||
|
||||
public List<SaleDataModel> GetAllSalesByPeriod(DateTime fromDate, DateTime toDate)
|
||||
{
|
||||
@@ -101,6 +102,10 @@ internal class SaleBusinessLogicContract(ISaleStorageContract saleStorageContrac
|
||||
_logger.LogInformation("New data: {json}", JsonSerializer.Serialize(saleDataModel));
|
||||
ArgumentNullException.ThrowIfNull(saleDataModel);
|
||||
saleDataModel.Validate();
|
||||
if (!_agencyStorageContract.CheckComponents(saleDataModel))
|
||||
{
|
||||
throw new InsufficientException("Dont have tour in agency");
|
||||
}
|
||||
_saleStorageContract.AddElement(saleDataModel);
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,61 @@
|
||||
using MagicCarpetContracts.BusinessLogicContracts;
|
||||
using MagicCarpetContracts.DataModels;
|
||||
using MagicCarpetContracts.Enums;
|
||||
using MagicCarpetContracts.Exceptions;
|
||||
using MagicCarpetContracts.Extensions;
|
||||
using MagicCarpetContracts.StoragesContracts;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MagicCarpetBusinessLogic.Implementations;
|
||||
|
||||
public class SuppliesBusinessLogicContract(ISuppliesStorageContract suppliesStorageContract, ILogger logger) : ISuppliesBusinessLogicContract
|
||||
{
|
||||
private readonly ISuppliesStorageContract _suppliesStorageContract = suppliesStorageContract;
|
||||
private readonly ILogger _logger = logger;
|
||||
|
||||
public List<SuppliesDataModel> GetAllComponents()
|
||||
{
|
||||
_logger.LogInformation("GetAllComponents");
|
||||
return _suppliesStorageContract.GetList() ?? throw new NullListException();
|
||||
|
||||
return [];
|
||||
}
|
||||
|
||||
public SuppliesDataModel GetComponentByData(string data)
|
||||
{
|
||||
_logger.LogInformation("Get element by data: {data}", data);
|
||||
if (data.IsEmpty())
|
||||
{
|
||||
throw new ArgumentNullException(nameof(data));
|
||||
}
|
||||
if (!data.IsGuid())
|
||||
{
|
||||
throw new ElementNotFoundException(data);
|
||||
}
|
||||
return _suppliesStorageContract.GetElementById(data) ?? throw new ElementNotFoundException(data);
|
||||
|
||||
return new("", TourType.None, DateTime.UtcNow, 0, []);
|
||||
}
|
||||
|
||||
public void InsertComponent(SuppliesDataModel suppliesDataModel)
|
||||
{
|
||||
_logger.LogInformation("New data: {json}", JsonSerializer.Serialize(suppliesDataModel));
|
||||
ArgumentNullException.ThrowIfNull(suppliesDataModel);
|
||||
suppliesDataModel.Validate();
|
||||
_suppliesStorageContract.AddElement(suppliesDataModel);
|
||||
}
|
||||
|
||||
public void UpdateComponent(SuppliesDataModel suppliesDataModel)
|
||||
{
|
||||
_logger.LogInformation("Update data: {json}", JsonSerializer.Serialize(suppliesDataModel));
|
||||
ArgumentNullException.ThrowIfNull(suppliesDataModel);
|
||||
suppliesDataModel.Validate();
|
||||
_suppliesStorageContract.UpdElement(suppliesDataModel);
|
||||
}
|
||||
}
|
||||
@@ -56,7 +56,6 @@ internal class TourBusinessLogicContract(ITourStorageContract tourStorageContrac
|
||||
_logger.LogInformation("New data: {json}", JsonSerializer.Serialize(tourDataModel));
|
||||
ArgumentNullException.ThrowIfNull(tourDataModel);
|
||||
tourDataModel.Validate();
|
||||
_tourStorageContract.AddElement(tourDataModel);
|
||||
}
|
||||
|
||||
public void UpdateTour(TourDataModel tourDataModel)
|
||||
@@ -64,7 +63,6 @@ internal class TourBusinessLogicContract(ITourStorageContract tourStorageContrac
|
||||
_logger.LogInformation("Update data: {json}", JsonSerializer.Serialize(tourDataModel));
|
||||
ArgumentNullException.ThrowIfNull(tourDataModel);
|
||||
tourDataModel.Validate();
|
||||
_tourStorageContract.UpdElement(tourDataModel);
|
||||
}
|
||||
|
||||
public void DeleteTour(string id)
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
using MagicCarpetContracts.DataModels;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MagicCarpetContracts.BusinessLogicContracts;
|
||||
|
||||
public interface IAgencyBusinessLogicContract
|
||||
{
|
||||
List<AgencyDataModel> GetAllComponents();
|
||||
AgencyDataModel GetComponentByData(string data);
|
||||
void InsertComponent(AgencyDataModel agencyDataModel);
|
||||
void UpdateComponent(AgencyDataModel agencyDataModel);
|
||||
void DeleteComponent(string id);
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
using MagicCarpetContracts.DataModels;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MagicCarpetContracts.BusinessLogicContracts;
|
||||
|
||||
public interface ISuppliesBusinessLogicContract
|
||||
{
|
||||
List<SuppliesDataModel> GetAllComponents();
|
||||
SuppliesDataModel GetComponentByData(string data);
|
||||
void InsertComponent(SuppliesDataModel componentDataModel);
|
||||
void UpdateComponent(SuppliesDataModel componentDataModel);
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
using MagicCarpetContracts.Enums;
|
||||
using MagicCarpetContracts.Exceptions;
|
||||
using MagicCarpetContracts.Extensions;
|
||||
using MagicCarpetContracts.Infrastructure;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MagicCarpetContracts.DataModels;
|
||||
|
||||
public class AgencyDataModel(string id, TourType tourType, int count, List<TourAgencyDataModel> tours) : IValidation
|
||||
{
|
||||
public string Id { get; private set; } = id;
|
||||
public TourType Type { get; private set; } = tourType;
|
||||
public int Count { get; private set; } = count;
|
||||
public List<TourAgencyDataModel> Tours { get; private set; } = tours;
|
||||
|
||||
public void Validate()
|
||||
{
|
||||
if (Id.IsEmpty())
|
||||
throw new ValidationException("Field Id is empty");
|
||||
if (!Id.IsGuid())
|
||||
throw new ValidationException("The value in the field Id is not a unique identifier");
|
||||
if (Type == TourType.None)
|
||||
throw new ValidationException("Field Type is empty");
|
||||
if (Count <= 0)
|
||||
throw new ValidationException("Field Count is less than or equal to 0");
|
||||
if ((Tours?.Count ?? 0) == 0)
|
||||
throw new ValidationException("The sale must include tours");
|
||||
}
|
||||
}
|
||||
@@ -11,7 +11,7 @@ using System.Threading.Tasks;
|
||||
namespace MagicCarpetContracts.DataModels;
|
||||
|
||||
public class SaleDataModel(string id, string employeeId, string? clientId, double sum, DiscountType discountType,
|
||||
double discount, bool isCancel, List<SaleTourDataModel> tours) : IValidation
|
||||
double discount, bool isCancel, List<SaleTourDataModel> saleTours) : IValidation
|
||||
{
|
||||
public string Id { get; private set; } = id;
|
||||
|
||||
@@ -28,7 +28,7 @@ public class SaleDataModel(string id, string employeeId, string? clientId, doubl
|
||||
|
||||
public bool IsCancel { get; private set; } = isCancel;
|
||||
|
||||
public List<SaleTourDataModel> Tours { get; private set; } = tours;
|
||||
public List<SaleTourDataModel> Tours { get; private set; } = saleTours;
|
||||
|
||||
public void Validate()
|
||||
{
|
||||
|
||||
@@ -9,11 +9,11 @@ using System.Threading.Tasks;
|
||||
|
||||
namespace MagicCarpetContracts.DataModels;
|
||||
|
||||
public class SaleTourDataModel(string saleId, string cocktailId, int count) : IValidation
|
||||
public class SaleTourDataModel(string saleId, string tourId, int count) : IValidation
|
||||
{
|
||||
public string SaleId { get; private set; } = saleId;
|
||||
|
||||
public string TourId { get; private set; } = cocktailId;
|
||||
public string TourId { get; private set; } = tourId;
|
||||
|
||||
public int Count { get; private set; } = count;
|
||||
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
using MagicCarpetContracts.Enums;
|
||||
using MagicCarpetContracts.Exceptions;
|
||||
using MagicCarpetContracts.Extensions;
|
||||
using MagicCarpetContracts.Infrastructure;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using static System.Runtime.InteropServices.JavaScript.JSType;
|
||||
|
||||
namespace MagicCarpetContracts.DataModels;
|
||||
|
||||
public class SuppliesDataModel(string id, TourType tourType, DateTime date, int count, List<TourSuppliesDataModel> tours) : IValidation
|
||||
{
|
||||
public string Id { get; private set; } = id;
|
||||
public TourType Type { get; private set; } = tourType;
|
||||
public DateTime ProductuionDate { get; private set; } = date;
|
||||
public int Count { get; private set; } = count;
|
||||
public List<TourSuppliesDataModel> Tours { get; private set; } = tours;
|
||||
|
||||
public void Validate()
|
||||
{
|
||||
if (Id.IsEmpty())
|
||||
throw new ValidationException("Field Id is empty");
|
||||
if (!Id.IsGuid())
|
||||
throw new ValidationException("The value in the field Id is not a unique identifier");
|
||||
if (Type == TourType.None)
|
||||
throw new ValidationException("Field Type is empty");
|
||||
if (Count <= 0)
|
||||
throw new ValidationException("Field Count is less than or equal to 0");
|
||||
if ((Tours?.Count ?? 0) == 0)
|
||||
throw new ValidationException("The sale must include tours");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
using MagicCarpetContracts.Exceptions;
|
||||
using MagicCarpetContracts.Extensions;
|
||||
using MagicCarpetContracts.Infrastructure;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MagicCarpetContracts.DataModels;
|
||||
|
||||
public class TourAgencyDataModel(string agencyId, string tourId, int count) : IValidation
|
||||
{
|
||||
public string AgencyId { get; private set; } = agencyId;
|
||||
public string TourId { get; private set; } = tourId;
|
||||
public int Count { get; private set; } = count;
|
||||
|
||||
public void Validate()
|
||||
{
|
||||
if (AgencyId.IsEmpty())
|
||||
throw new ValidationException("Field AgencyId is empty");
|
||||
if (!AgencyId.IsGuid())
|
||||
throw new ValidationException("The value in the field AgencyId is not a unique identifier");
|
||||
if (TourId.IsEmpty())
|
||||
throw new ValidationException("Field TourId is empty");
|
||||
if (!TourId.IsGuid())
|
||||
throw new ValidationException("The value in the field TourId is not a unique identifier");
|
||||
if (Count <= 0)
|
||||
throw new ValidationException("Field Count is less than or equal to 0");
|
||||
}
|
||||
}
|
||||
@@ -18,7 +18,7 @@ public class TourDataModel(string id, string tourName, string tourCountry, doubl
|
||||
public string TourName { get; private set; } = tourName;
|
||||
public string TourCountry { get; private set; } = tourCountry;
|
||||
public double Price { get; private set; } = price;
|
||||
public TourType Type { get; private set; } = tourType;
|
||||
public TourType TourType { get; private set; } = tourType;
|
||||
|
||||
public void Validate()
|
||||
{
|
||||
@@ -32,7 +32,7 @@ public class TourDataModel(string id, string tourName, string tourCountry, doubl
|
||||
throw new ValidationException("Field TourCountry is empty");
|
||||
if (Price <= 0)
|
||||
throw new ValidationException("Field Price is less than or equal to 0");
|
||||
if (Type == TourType.None)
|
||||
if (TourType == TourType.None)
|
||||
throw new ValidationException("Field Type is empty");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
using MagicCarpetContracts.Exceptions;
|
||||
using MagicCarpetContracts.Extensions;
|
||||
using MagicCarpetContracts.Infrastructure;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MagicCarpetContracts.DataModels;
|
||||
|
||||
public class TourSuppliesDataModel(string suppliesId, string tourId, int count) : IValidation
|
||||
{
|
||||
public string SuppliesId { get; private set; } = suppliesId;
|
||||
public string TourId { get; private set; } = tourId;
|
||||
public int Count { get; private set; } = count;
|
||||
|
||||
public void Validate()
|
||||
{
|
||||
if (SuppliesId.IsEmpty())
|
||||
throw new ValidationException("Field SuppliesId is empty");
|
||||
if (!SuppliesId.IsGuid())
|
||||
throw new ValidationException("The value in the field SuppliesId is not a unique identifier");
|
||||
if (TourId.IsEmpty())
|
||||
throw new ValidationException("Field TourId is empty");
|
||||
if (!TourId.IsGuid())
|
||||
throw new ValidationException("The value in the field BlandId is not a unique identifier");
|
||||
if (Count <= 0)
|
||||
throw new ValidationException("Field Count is less than or equal to 0");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MagicCarpetContracts.Exceptions;
|
||||
|
||||
public class ElementDeletedException : Exception
|
||||
{
|
||||
public ElementDeletedException(string id) : base($"Cannot modify a deleted item (id: {id})") { }
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MagicCarpetContracts.Exceptions;
|
||||
|
||||
public class InsufficientException(string message) : Exception(message)
|
||||
{
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MagicCarpetContracts.Infrastructure;
|
||||
|
||||
public interface IConfigurationDatabase
|
||||
{
|
||||
string ConnectionString { get; }
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
using MagicCarpetContracts.DataModels;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MagicCarpetContracts.StoragesContracts;
|
||||
|
||||
public interface IAgencyStorageContract
|
||||
{
|
||||
List<AgencyDataModel> GetList();
|
||||
AgencyDataModel GetElementById(string id);
|
||||
void AddElement(AgencyDataModel agencyDataModel);
|
||||
void UpdElement(AgencyDataModel agencyDataModel);
|
||||
void DelElement(string id);
|
||||
bool CheckComponents(SaleDataModel saleDataModel);
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
using MagicCarpetContracts.DataModels;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MagicCarpetContracts.StoragesContracts;
|
||||
|
||||
public interface ISuppliesStorageContract
|
||||
{
|
||||
List<SuppliesDataModel> GetList(DateTime? startDate = null);
|
||||
SuppliesDataModel GetElementById(string id);
|
||||
void AddElement(SuppliesDataModel suppliesDataModel);
|
||||
void UpdElement(SuppliesDataModel suppliesDataModel);
|
||||
}
|
||||
@@ -0,0 +1,148 @@
|
||||
using AutoMapper;
|
||||
using MagicCarpetContracts.DataModels;
|
||||
using MagicCarpetContracts.Exceptions;
|
||||
using MagicCarpetContracts.StoragesContracts;
|
||||
using MagicCarpetDatabase.Models;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MagicCarpetDatabase.Implementations;
|
||||
|
||||
public class AgencyStorageContract : IAgencyStorageContract
|
||||
{
|
||||
private readonly MagicCarpetDbContext _dbContext;
|
||||
private readonly Mapper _mapper;
|
||||
|
||||
public AgencyStorageContract(MagicCarpetDbContext dbContext)
|
||||
{
|
||||
_dbContext = dbContext;
|
||||
var config = new MapperConfiguration(cfg =>
|
||||
{
|
||||
cfg.CreateMap<Agency, AgencyDataModel>()
|
||||
.ConstructUsing(src => new AgencyDataModel(
|
||||
src.Id,
|
||||
src.Type,
|
||||
src.Count,
|
||||
_mapper.Map<List<TourAgencyDataModel>>(src.Tours)
|
||||
));
|
||||
|
||||
cfg.CreateMap<AgencyDataModel, Agency>();
|
||||
cfg.CreateMap<TourAgencyDataModel, TourAgency>().ReverseMap();
|
||||
});
|
||||
_mapper = new Mapper(config);
|
||||
}
|
||||
|
||||
public List<AgencyDataModel> GetList()
|
||||
{
|
||||
try
|
||||
{
|
||||
return [.. _dbContext.Agencies.Select(x => _mapper.Map<AgencyDataModel>(x))];
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw new StorageException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
public AgencyDataModel GetElementById(string id)
|
||||
{
|
||||
try
|
||||
{
|
||||
return _mapper.Map<AgencyDataModel>(GetAgencyById(id));
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw new StorageException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
public void AddElement(AgencyDataModel agencyDataModel)
|
||||
{
|
||||
try
|
||||
{
|
||||
_dbContext.Agencies.Add(_mapper.Map<Agency>(agencyDataModel));
|
||||
_dbContext.SaveChanges();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw new StorageException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
public void UpdElement(AgencyDataModel agencyDataModel)
|
||||
{
|
||||
try
|
||||
{
|
||||
var element = GetAgencyById(agencyDataModel.Id) ?? throw new ElementNotFoundException(agencyDataModel.Id);
|
||||
_dbContext.Agencies.Update(_mapper.Map(agencyDataModel, element));
|
||||
_dbContext.SaveChanges();
|
||||
}
|
||||
catch (ElementNotFoundException)
|
||||
{
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw new StorageException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
public void DelElement(string id)
|
||||
{
|
||||
try
|
||||
{
|
||||
var element = GetAgencyById(id) ?? throw new ElementNotFoundException(id);
|
||||
_dbContext.Agencies.Remove(element);
|
||||
_dbContext.SaveChanges();
|
||||
}
|
||||
catch (ElementNotFoundException)
|
||||
{
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw new StorageException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
public bool CheckComponents(SaleDataModel saleDataModel)
|
||||
{
|
||||
using (var transaction = _dbContext.Database.BeginTransaction())
|
||||
{
|
||||
foreach (SaleTourDataModel sale_tour in saleDataModel.Tours)
|
||||
{
|
||||
var tour = _dbContext.Tours.FirstOrDefault(x => x.Id == sale_tour.TourId);
|
||||
var agency = _dbContext.Agencies.FirstOrDefault(x => x.Type == tour.TourType && x.Count >= sale_tour.Count);
|
||||
|
||||
if (agency == null)
|
||||
{
|
||||
transaction.Rollback();
|
||||
return false;
|
||||
}
|
||||
if (agency.Count - sale_tour.Count == 0)
|
||||
{
|
||||
DelElement(agency.Id);
|
||||
}
|
||||
else
|
||||
{
|
||||
agency.Count -= sale_tour.Count;
|
||||
}
|
||||
}
|
||||
transaction.Commit();
|
||||
_dbContext.SaveChanges();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
private Agency? GetAgencyById(string id) => _dbContext.Agencies.FirstOrDefault(x => x.Id == id);
|
||||
}
|
||||
@@ -0,0 +1,144 @@
|
||||
using AutoMapper;
|
||||
using MagicCarpetContracts.DataModels;
|
||||
using MagicCarpetContracts.Exceptions;
|
||||
using MagicCarpetContracts.StoragesContracts;
|
||||
using MagicCarpetDatabase.Models;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Npgsql;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MagicCarpetDatabase.Implementations;
|
||||
|
||||
internal class ClientStorageContarct : IClientStorageContract
|
||||
{
|
||||
private readonly MagicCarpetDbContext _dbContext;
|
||||
private readonly Mapper _mapper;
|
||||
public ClientStorageContarct(MagicCarpetDbContext magicCarpetDbContext)
|
||||
{
|
||||
_dbContext = magicCarpetDbContext;
|
||||
var config = new MapperConfiguration(cfg =>
|
||||
{
|
||||
cfg.AddMaps(typeof(MagicCarpetDbContext).Assembly);
|
||||
});
|
||||
_mapper = new Mapper(config);
|
||||
}
|
||||
public List<ClientDataModel> GetList()
|
||||
{
|
||||
try
|
||||
{
|
||||
return [.. _dbContext.Clients.Select(x => _mapper.Map<ClientDataModel>(x))];
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw new StorageException(ex);
|
||||
}
|
||||
}
|
||||
public ClientDataModel? GetElementById(string id)
|
||||
{
|
||||
try
|
||||
{
|
||||
return _mapper.Map<ClientDataModel>(GetClientById(id));
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw new StorageException(ex);
|
||||
}
|
||||
}
|
||||
public ClientDataModel? GetElementByFIO(string fio)
|
||||
{
|
||||
try
|
||||
{
|
||||
return _mapper.Map<ClientDataModel>(_dbContext.Clients.FirstOrDefault(x => x.FIO == fio));
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw new StorageException(ex);
|
||||
}
|
||||
}
|
||||
public ClientDataModel? GetElementByPhoneNumber(string phoneNumber)
|
||||
{
|
||||
try
|
||||
{
|
||||
return _mapper.Map<ClientDataModel>(_dbContext.Clients.FirstOrDefault(x => x.PhoneNumber == phoneNumber));
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw new StorageException(ex);
|
||||
}
|
||||
}
|
||||
public void AddElement(ClientDataModel clientDataModel)
|
||||
{
|
||||
try
|
||||
{
|
||||
_dbContext.Clients.Add(_mapper.Map<Client>(clientDataModel));
|
||||
_dbContext.SaveChanges();
|
||||
}
|
||||
catch (InvalidOperationException ex) when (ex.TargetSite?.Name == "ThrowIdentityConflict")
|
||||
{
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw new ElementExistsException("Id", clientDataModel.Id);
|
||||
}
|
||||
catch (DbUpdateException ex) when (ex.InnerException is PostgresException { ConstraintName: "IX_Clients_PhoneNumber" })
|
||||
{
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw new ElementExistsException("PhoneNumber", clientDataModel.PhoneNumber);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw new StorageException(ex);
|
||||
}
|
||||
}
|
||||
public void UpdElement(ClientDataModel clientDataModel)
|
||||
{
|
||||
try
|
||||
{
|
||||
var element = GetClientById(clientDataModel.Id) ?? throw new ElementNotFoundException(clientDataModel.Id);
|
||||
_dbContext.Clients.Update(_mapper.Map(clientDataModel, element));
|
||||
_dbContext.SaveChanges();
|
||||
}
|
||||
catch (ElementNotFoundException)
|
||||
{
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw;
|
||||
}
|
||||
catch (DbUpdateException ex) when (ex.InnerException is PostgresException { ConstraintName: "IX_Clients_PhoneNumber" })
|
||||
{
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw new ElementExistsException("PhoneNumber", clientDataModel.PhoneNumber);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw new StorageException(ex);
|
||||
}
|
||||
}
|
||||
public void DelElement(string id)
|
||||
{
|
||||
try
|
||||
{
|
||||
var element = GetClientById(id) ?? throw new ElementNotFoundException(id);
|
||||
_dbContext.Clients.Remove(element);
|
||||
_dbContext.SaveChanges();
|
||||
}
|
||||
catch (ElementNotFoundException)
|
||||
{
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw new StorageException(ex);
|
||||
}
|
||||
}
|
||||
private Client? GetClientById(string id) => _dbContext.Clients.FirstOrDefault(x => x.Id == id);
|
||||
}
|
||||
@@ -0,0 +1,156 @@
|
||||
using AutoMapper;
|
||||
using MagicCarpetContracts.DataModels;
|
||||
using MagicCarpetContracts.Exceptions;
|
||||
using MagicCarpetContracts.StoragesContracts;
|
||||
using MagicCarpetDatabase.Models;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MagicCarpetDatabase.Implementations;
|
||||
|
||||
internal class EmployeeStorageContract : IEmployeeStorageContract
|
||||
{
|
||||
private readonly MagicCarpetDbContext _dbContext;
|
||||
private readonly Mapper _mapper;
|
||||
|
||||
public EmployeeStorageContract(MagicCarpetDbContext dbContext)
|
||||
{
|
||||
_dbContext = dbContext;
|
||||
var config = new MapperConfiguration(cfg =>
|
||||
{
|
||||
cfg.AddMaps(typeof(MagicCarpetDbContext).Assembly);
|
||||
});
|
||||
_mapper = new Mapper(config);
|
||||
}
|
||||
public List<EmployeeDataModel> GetList(bool onlyActive = true, string? postId = null, DateTime? fromBirthDate = null, DateTime? toBirthDate = null, DateTime? fromEmploymentDate = null, DateTime? toEmploymentDate = null)
|
||||
{
|
||||
try
|
||||
{
|
||||
var query = _dbContext.Employees.AsQueryable();
|
||||
if (onlyActive)
|
||||
{
|
||||
query = query.Where(x => !x.IsDeleted);
|
||||
}
|
||||
if (postId is not null)
|
||||
{
|
||||
query = query.Where(x => x.PostId == postId);
|
||||
}
|
||||
if (fromBirthDate is not null && toBirthDate is not null)
|
||||
{
|
||||
query = query.Where(x => x.BirthDate >= fromBirthDate && x.BirthDate <= toBirthDate);
|
||||
}
|
||||
if (fromEmploymentDate is not null && toEmploymentDate is not null)
|
||||
{
|
||||
query = query.Where(x => x.EmploymentDate >= fromEmploymentDate && x.EmploymentDate <= toEmploymentDate);
|
||||
}
|
||||
return [.. query.Select(x => _mapper.Map<EmployeeDataModel>(x))];
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw new StorageException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
public EmployeeDataModel? GetElementById(string id)
|
||||
{
|
||||
try
|
||||
{
|
||||
return _mapper.Map<EmployeeDataModel>(GetEmployeeById(id));
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw new StorageException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
public EmployeeDataModel? GetElementByFIO(string fio)
|
||||
{
|
||||
try
|
||||
{
|
||||
return _mapper.Map<EmployeeDataModel>(_dbContext.Employees.FirstOrDefault(x => x.FIO == fio));
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw new StorageException(ex);
|
||||
}
|
||||
}
|
||||
public EmployeeDataModel? GetElementByEmail(string email)
|
||||
{
|
||||
try
|
||||
{
|
||||
return _mapper.Map<EmployeeDataModel>(_dbContext.Employees.FirstOrDefault(x => x.Email == email));
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw new StorageException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
public void AddElement(EmployeeDataModel employeeDataModel)
|
||||
{
|
||||
try
|
||||
{
|
||||
_dbContext.Employees.Add(_mapper.Map<Employee>(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 UpdElement(EmployeeDataModel employeeDataModel)
|
||||
{
|
||||
try
|
||||
{
|
||||
var element = GetEmployeeById(employeeDataModel.Id) ?? throw new ElementNotFoundException(employeeDataModel.Id);
|
||||
_dbContext.Employees.Update(_mapper.Map(employeeDataModel, element));
|
||||
_dbContext.SaveChanges();
|
||||
}
|
||||
catch (ElementNotFoundException)
|
||||
{
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw new StorageException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
public void DelElement(string id)
|
||||
{
|
||||
try
|
||||
{
|
||||
var element = GetEmployeeById(id) ?? throw new ElementNotFoundException(id);
|
||||
element.IsDeleted = true;
|
||||
_dbContext.SaveChanges();
|
||||
}
|
||||
catch (ElementNotFoundException)
|
||||
{
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw new StorageException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
private Employee? GetEmployeeById(string id) => _dbContext.Employees.FirstOrDefault(x => x.Id == id && !x.IsDeleted);
|
||||
}
|
||||
@@ -0,0 +1,196 @@
|
||||
using AutoMapper;
|
||||
using MagicCarpetContracts.DataModels;
|
||||
using MagicCarpetContracts.Exceptions;
|
||||
using MagicCarpetContracts.StoragesContracts;
|
||||
using MagicCarpetDatabase.Models;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Npgsql;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MagicCarpetDatabase.Implementations;
|
||||
|
||||
internal class PostStorageContract : IPostStorageContract
|
||||
{
|
||||
private readonly MagicCarpetDbContext _dbContext;
|
||||
private readonly Mapper _mapper;
|
||||
|
||||
public PostStorageContract(MagicCarpetDbContext dbContext)
|
||||
{
|
||||
_dbContext = dbContext;
|
||||
var config = new MapperConfiguration(cfg =>
|
||||
{
|
||||
cfg.CreateMap<Post, PostDataModel>()
|
||||
.ForMember(x => x.Id, x => x.MapFrom(src => src.PostId));
|
||||
cfg.CreateMap<PostDataModel, Post>()
|
||||
.ForMember(x => x.Id, x => x.Ignore())
|
||||
.ForMember(x => x.PostId, x => x.MapFrom(src => src.Id))
|
||||
.ForMember(x => x.IsActual, x => x.MapFrom(src => true))
|
||||
.ForMember(x => x.ChangeDate, x => x.MapFrom(src => DateTime.UtcNow));
|
||||
});
|
||||
_mapper = new Mapper(config);
|
||||
}
|
||||
|
||||
public List<PostDataModel> GetList(bool onlyActual = true)
|
||||
{
|
||||
try
|
||||
{
|
||||
var query = _dbContext.Posts.AsQueryable();
|
||||
if (onlyActual)
|
||||
{
|
||||
query = query.Where(x => x.IsActual);
|
||||
}
|
||||
return [.. query.Select(x => _mapper.Map<PostDataModel>(x))];
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw new StorageException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
public List<PostDataModel> GetPostWithHistory(string postId)
|
||||
{
|
||||
try
|
||||
{
|
||||
return [.. _dbContext.Posts.Where(x => x.PostId == postId).Select(x => _mapper.Map<PostDataModel>(x))];
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw new StorageException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
public PostDataModel? GetElementById(string id)
|
||||
{
|
||||
try
|
||||
{
|
||||
return _mapper.Map<PostDataModel>(_dbContext.Posts.FirstOrDefault(x => x.PostId == id && x.IsActual));
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw new StorageException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
public PostDataModel? GetElementByName(string name)
|
||||
{
|
||||
try
|
||||
{
|
||||
return _mapper.Map<PostDataModel>(_dbContext.Posts.FirstOrDefault(x => x.PostName == name && x.IsActual));
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw new StorageException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
public void AddElement(PostDataModel postDataModel)
|
||||
{
|
||||
try
|
||||
{
|
||||
_dbContext.Posts.Add(_mapper.Map<Post>(postDataModel));
|
||||
_dbContext.SaveChanges();
|
||||
}
|
||||
catch (DbUpdateException ex) when (ex.InnerException is PostgresException { ConstraintName: "IX_Posts_PostName_IsActual" })
|
||||
{
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw new ElementExistsException("PostName", postDataModel.PostName);
|
||||
}
|
||||
catch (DbUpdateException ex) when (ex.InnerException is PostgresException { ConstraintName: "IX_Posts_PostId_IsActual" })
|
||||
{
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw new ElementExistsException("PostId", postDataModel.Id);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw new StorageException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
public void UpdElement(PostDataModel postDataModel)
|
||||
{
|
||||
try
|
||||
{
|
||||
var transaction = _dbContext.Database.BeginTransaction();
|
||||
try
|
||||
{
|
||||
var element = GetPostById(postDataModel.Id) ?? throw new ElementNotFoundException(postDataModel.Id);
|
||||
if (!element.IsActual)
|
||||
{
|
||||
throw new ElementDeletedException(postDataModel.Id);
|
||||
}
|
||||
element.IsActual = false;
|
||||
_dbContext.SaveChanges();
|
||||
var newElement = _mapper.Map<Post>(postDataModel);
|
||||
_dbContext.Posts.Add(newElement);
|
||||
_dbContext.SaveChanges();
|
||||
transaction.Commit();
|
||||
}
|
||||
catch
|
||||
{
|
||||
transaction.Rollback();
|
||||
throw;
|
||||
}
|
||||
}
|
||||
catch (DbUpdateException ex) when (ex.InnerException is PostgresException { ConstraintName: "IX_Posts_PostName_IsActual" })
|
||||
{
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw new ElementExistsException("PostName", postDataModel.PostName);
|
||||
}
|
||||
catch (Exception ex) when (ex is ElementDeletedException || ex is ElementNotFoundException)
|
||||
{
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw new StorageException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
public void DelElement(string id)
|
||||
{
|
||||
try
|
||||
{
|
||||
var element = GetPostById(id) ?? throw new ElementNotFoundException(id);
|
||||
if (!element.IsActual)
|
||||
{
|
||||
throw new ElementDeletedException(id);
|
||||
}
|
||||
element.IsActual = false;
|
||||
_dbContext.SaveChanges();
|
||||
}
|
||||
catch
|
||||
{
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public void ResElement(string id)
|
||||
{
|
||||
try
|
||||
{
|
||||
var element = GetPostById(id) ?? throw new ElementNotFoundException(id);
|
||||
element.IsActual = true;
|
||||
_dbContext.SaveChanges();
|
||||
}
|
||||
catch
|
||||
{
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
private Post? GetPostById(string id) => _dbContext.Posts.Where(x => x.PostId == id)
|
||||
.OrderByDescending(x => x.ChangeDate).FirstOrDefault();
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
using AutoMapper;
|
||||
using MagicCarpetContracts.DataModels;
|
||||
using MagicCarpetContracts.Exceptions;
|
||||
using MagicCarpetContracts.StoragesContracts;
|
||||
using MagicCarpetDatabase.Models;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MagicCarpetDatabase.Implementations;
|
||||
|
||||
internal class SalaryStorageContract : ISalaryStorageContract
|
||||
{
|
||||
private readonly MagicCarpetDbContext _dbContext;
|
||||
private readonly Mapper _mapper;
|
||||
|
||||
public SalaryStorageContract(MagicCarpetDbContext dbContext)
|
||||
{
|
||||
_dbContext = dbContext;
|
||||
var config = new MapperConfiguration(cfg =>
|
||||
{
|
||||
cfg.CreateMap<Salary, SalaryDataModel>();
|
||||
cfg.CreateMap<SalaryDataModel, Salary>()
|
||||
.ForMember(dest => dest.EmployeeSalary, opt => opt.MapFrom(src => src.Salary));
|
||||
});
|
||||
_mapper = new Mapper(config);
|
||||
}
|
||||
|
||||
public List<SalaryDataModel> GetList(DateTime startDate, DateTime endDate, string? employeeId = null)
|
||||
{
|
||||
try
|
||||
{
|
||||
var query = _dbContext.Salaries.Where(x => x.SalaryDate >= startDate && x.SalaryDate <= endDate);
|
||||
if (employeeId is not null)
|
||||
{
|
||||
query = query.Where(x => x.EmployeeId == employeeId);
|
||||
}
|
||||
return [.. query.Select(x => _mapper.Map<SalaryDataModel>(x))];
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw new StorageException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
public void AddElement(SalaryDataModel salaryDataModel)
|
||||
{
|
||||
try
|
||||
{
|
||||
_dbContext.Salaries.Add(_mapper.Map<Salary>(salaryDataModel));
|
||||
_dbContext.SaveChanges();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw new StorageException(ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,117 @@
|
||||
using AutoMapper;
|
||||
using MagicCarpetContracts.DataModels;
|
||||
using MagicCarpetContracts.Exceptions;
|
||||
using MagicCarpetContracts.StoragesContracts;
|
||||
using MagicCarpetDatabase.Models;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MagicCarpetDatabase.Implementations;
|
||||
|
||||
internal class SaleStorageContract : ISaleStorageContract
|
||||
{
|
||||
private readonly MagicCarpetDbContext _dbContext;
|
||||
private readonly Mapper _mapper;
|
||||
|
||||
public SaleStorageContract(MagicCarpetDbContext dbContext)
|
||||
{
|
||||
_dbContext = dbContext;
|
||||
var config = new MapperConfiguration(cfg =>
|
||||
{
|
||||
cfg.CreateMap<SaleTour, SaleTourDataModel>();
|
||||
cfg.CreateMap<SaleTourDataModel, SaleTour>();
|
||||
cfg.CreateMap<Sale, SaleDataModel>();
|
||||
cfg.CreateMap<SaleDataModel, Sale>()
|
||||
.ForMember(x => x.IsCancel, x => x.MapFrom(src => false))
|
||||
.ForMember(x => x.SaleTours, x => x.MapFrom(src => src.Tours));
|
||||
});
|
||||
_mapper = new Mapper(config);
|
||||
}
|
||||
|
||||
public List<SaleDataModel> GetList(DateTime? startDate = null, DateTime? endDate = null, string? employeeId = null, string? clientId = null, string? tourId = null)
|
||||
{
|
||||
try
|
||||
{
|
||||
var query = _dbContext.Sales.Include(x => x.SaleTours).AsQueryable();
|
||||
if (startDate is not null && endDate is not null)
|
||||
{
|
||||
query = query.Where(x => x.SaleDate >= startDate && x.SaleDate < endDate);
|
||||
}
|
||||
if (employeeId is not null)
|
||||
{
|
||||
query = query.Where(x => x.EmployeeId == employeeId);
|
||||
}
|
||||
if (clientId is not null)
|
||||
{
|
||||
query = query.Where(x => x.ClientId == clientId);
|
||||
}
|
||||
if (tourId is not null)
|
||||
{
|
||||
query = query.Where(x => x.SaleTours!.Any(y => y.TourId == tourId));
|
||||
}
|
||||
return [.. query.Select(x => _mapper.Map<SaleDataModel>(x))];
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw new StorageException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
public SaleDataModel? GetElementById(string id)
|
||||
{
|
||||
try
|
||||
{
|
||||
return _mapper.Map<SaleDataModel>(GetSaleById(id));
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw new StorageException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
public void AddElement(SaleDataModel saleDataModel)
|
||||
{
|
||||
try
|
||||
{
|
||||
_dbContext.Sales.Add(_mapper.Map<Sale>(saleDataModel));
|
||||
_dbContext.SaveChanges();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw new StorageException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
public void DelElement(string id)
|
||||
{
|
||||
try
|
||||
{
|
||||
var element = GetSaleById(id) ?? throw new ElementNotFoundException(id);
|
||||
if (element.IsCancel)
|
||||
{
|
||||
throw new ElementDeletedException(id);
|
||||
}
|
||||
element.IsCancel = true;
|
||||
_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);
|
||||
}
|
||||
}
|
||||
|
||||
private Sale? GetSaleById(string id) => _dbContext.Sales.FirstOrDefault(x => x.Id == id);
|
||||
}
|
||||
@@ -0,0 +1,100 @@
|
||||
using AutoMapper;
|
||||
using MagicCarpetContracts.DataModels;
|
||||
using MagicCarpetContracts.Exceptions;
|
||||
using MagicCarpetContracts.StoragesContracts;
|
||||
using MagicCarpetDatabase.Models;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MagicCarpetDatabase.Implementations;
|
||||
|
||||
public class SuppliesStorageContract : ISuppliesStorageContract
|
||||
{
|
||||
private readonly MagicCarpetDbContext _dbContext;
|
||||
private readonly Mapper _mapper;
|
||||
|
||||
public SuppliesStorageContract(MagicCarpetDbContext dbContext)
|
||||
{
|
||||
_dbContext = dbContext;
|
||||
var config = new MapperConfiguration(cfg =>
|
||||
{
|
||||
cfg.CreateMap<Supplies, SuppliesDataModel>()
|
||||
.ConstructUsing(src => new SuppliesDataModel(
|
||||
src.Id,
|
||||
src.Type,
|
||||
src.ProductuionDate,
|
||||
src.Count,
|
||||
_mapper.Map<List<TourSuppliesDataModel>>(src.Tours)
|
||||
));
|
||||
|
||||
cfg.CreateMap<SuppliesDataModel, Supplies>();
|
||||
cfg.CreateMap<TourSuppliesDataModel, TourSupplies>().ReverseMap();
|
||||
});
|
||||
_mapper = new Mapper(config);
|
||||
}
|
||||
|
||||
public List<SuppliesDataModel> GetList(DateTime? startDate = null)
|
||||
{
|
||||
try
|
||||
{
|
||||
return [.. _dbContext.Supplieses.Select(x => _mapper.Map<SuppliesDataModel>(x))];
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw new StorageException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
public SuppliesDataModel GetElementById(string id)
|
||||
{
|
||||
try
|
||||
{
|
||||
return _mapper.Map<SuppliesDataModel>(GetSuppliesById(id));
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw new StorageException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
public void AddElement(SuppliesDataModel suppliesDataModel)
|
||||
{
|
||||
try
|
||||
{
|
||||
_dbContext.Supplieses.Add(_mapper.Map<Supplies>(suppliesDataModel));
|
||||
_dbContext.SaveChanges();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw new StorageException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
public void UpdElement(SuppliesDataModel suppliesDataModel)
|
||||
{
|
||||
try
|
||||
{
|
||||
var element = GetSuppliesById(suppliesDataModel.Id) ?? throw new ElementNotFoundException(suppliesDataModel.Id);
|
||||
_dbContext.Supplieses.Update(_mapper.Map(suppliesDataModel, element));
|
||||
_dbContext.SaveChanges();
|
||||
}
|
||||
catch (ElementNotFoundException)
|
||||
{
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw new StorageException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
private Supplies? GetSuppliesById(string id) => _dbContext.Supplieses.FirstOrDefault(x => x.Id == id);
|
||||
}
|
||||
@@ -0,0 +1,169 @@
|
||||
using AutoMapper;
|
||||
using MagicCarpetContracts.DataModels;
|
||||
using MagicCarpetContracts.Exceptions;
|
||||
using MagicCarpetContracts.StoragesContracts;
|
||||
using MagicCarpetDatabase.Models;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Npgsql;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MagicCarpetDatabase.Implementations;
|
||||
|
||||
internal class TourStorageContract : ITourStorageContract
|
||||
{
|
||||
private readonly MagicCarpetDbContext _dbContext;
|
||||
private readonly Mapper _mapper;
|
||||
|
||||
public TourStorageContract(MagicCarpetDbContext dbContext)
|
||||
{
|
||||
_dbContext = dbContext;
|
||||
var config = new MapperConfiguration(cfg =>
|
||||
{
|
||||
cfg.CreateMap<Tour, TourDataModel>();
|
||||
cfg.CreateMap<TourDataModel, Tour>();
|
||||
cfg.CreateMap<TourHistory, TourHistoryDataModel>(); ;
|
||||
});
|
||||
_mapper = new Mapper(config);
|
||||
}
|
||||
public List<TourDataModel> GetList()
|
||||
{
|
||||
try
|
||||
{
|
||||
return [.. _dbContext.Tours.Select(x => _mapper.Map<TourDataModel>(x))];
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw new StorageException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
public List<TourHistoryDataModel> GetHistoryByTourId(string tourId)
|
||||
{
|
||||
try
|
||||
{
|
||||
return [.. _dbContext.TourHistories.Where(x => x.TourId == tourId).OrderByDescending(x => x.ChangeDate).Select(x => _mapper.Map<TourHistoryDataModel>(x))];
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw new StorageException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
public TourDataModel? GetElementById(string id)
|
||||
{
|
||||
try
|
||||
{
|
||||
return _mapper.Map<TourDataModel>(GetTourById(id));
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw new StorageException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
public TourDataModel? GetElementByName(string name)
|
||||
{
|
||||
try
|
||||
{
|
||||
return _mapper.Map<TourDataModel>(_dbContext.Tours.FirstOrDefault(x => x.TourName == name));
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw new StorageException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
public void AddElement(TourDataModel tourDataModel)
|
||||
{
|
||||
try
|
||||
{
|
||||
_dbContext.Tours.Add(_mapper.Map<Tour>(tourDataModel));
|
||||
_dbContext.SaveChanges();
|
||||
}
|
||||
catch (InvalidOperationException ex) when (ex.TargetSite?.Name == "ThrowIdentityConflict")
|
||||
{
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw new ElementExistsException("Id", tourDataModel.Id);
|
||||
}
|
||||
catch (DbUpdateException ex) when (ex.InnerException is PostgresException { ConstraintName: "IX_Tours_TourName" })
|
||||
{
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw new ElementExistsException("TourName", tourDataModel.TourName);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw new StorageException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
public void UpdElement(TourDataModel tourDataModel)
|
||||
{
|
||||
try
|
||||
{
|
||||
var element = GetTourById(tourDataModel.Id) ?? throw new ElementNotFoundException(tourDataModel.Id);
|
||||
_dbContext.Tours.Update(_mapper.Map(tourDataModel, element));
|
||||
_dbContext.SaveChanges();
|
||||
}
|
||||
catch (ElementNotFoundException)
|
||||
{
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw;
|
||||
}
|
||||
catch (DbUpdateException ex) when (ex.InnerException is PostgresException { ConstraintName: "IX_Tours_TourName" })
|
||||
{
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw new ElementExistsException("TourName", tourDataModel.TourName);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw new StorageException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
public void DelElement(string id)
|
||||
{
|
||||
try
|
||||
{
|
||||
var element = GetTourById(id) ?? throw new ElementNotFoundException(id);
|
||||
_dbContext.Tours.Remove(element);
|
||||
_dbContext.SaveChanges();
|
||||
}
|
||||
catch (ElementNotFoundException)
|
||||
{
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw new StorageException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
public void ResElement(string id)
|
||||
{
|
||||
try
|
||||
{
|
||||
var element = GetTourById(id) ?? throw new ElementNotFoundException(id);
|
||||
_dbContext.SaveChanges();
|
||||
}
|
||||
catch
|
||||
{
|
||||
_dbContext.ChangeTracker.Clear();
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
private Tour? GetTourById(string id) => _dbContext.Tours.FirstOrDefault(x => x.Id == id);
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
<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.3" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\MagicCarpetContracts\MagicCarpetContracts.csproj" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<InternalsVisibleTo Include="MagicCarpetTests" />
|
||||
<InternalsVisibleTo Include="DynamicProxyGenAssembly2" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -0,0 +1,66 @@
|
||||
using MagicCarpetContracts.Infrastructure;
|
||||
using MagicCarpetDatabase.Models;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MagicCarpetDatabase;
|
||||
|
||||
public class MagicCarpetDbContext(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<Client>().HasIndex(x => x.PhoneNumber).IsUnique();
|
||||
|
||||
modelBuilder.Entity<Tour>().HasIndex(x => x.TourName).IsUnique();
|
||||
|
||||
modelBuilder.Entity<Post>()
|
||||
.HasIndex(e => new { e.PostName, e.IsActual })
|
||||
.IsUnique()
|
||||
.HasFilter($"\"{nameof(Post.IsActual)}\" = TRUE");
|
||||
|
||||
modelBuilder.Entity<Post>()
|
||||
.HasIndex(e => new { e.PostId, e.IsActual })
|
||||
.IsUnique()
|
||||
.HasFilter($"\"{nameof(Post.IsActual)}\" = TRUE");
|
||||
|
||||
modelBuilder.Entity<SaleTour>().HasKey(x => new { x.SaleId, x.TourId });
|
||||
|
||||
modelBuilder.Entity<TourSupplies>().HasKey(x => new { x.SuppliesId, x.TourId });
|
||||
|
||||
modelBuilder.Entity<TourAgency>().HasKey(x => new { x.AgencyId, x.TourId });
|
||||
}
|
||||
|
||||
public DbSet<Client> Clients { get; set; }
|
||||
|
||||
public DbSet<Post> Posts { get; set; }
|
||||
|
||||
public DbSet<Tour> Tours { get; set; }
|
||||
|
||||
public DbSet<TourHistory> TourHistories { get; set; }
|
||||
|
||||
public DbSet<Salary> Salaries { get; set; }
|
||||
|
||||
public DbSet<Sale> Sales { get; set; }
|
||||
|
||||
public DbSet<SaleTour> SaleTours { get; set; }
|
||||
|
||||
public DbSet<Employee> Employees { get; set; }
|
||||
public DbSet<Supplies> Supplieses { get; set; }
|
||||
public DbSet<Agency> Agencies { get; set; }
|
||||
public DbSet<TourSupplies> TourSupplieses { get; set; }
|
||||
public DbSet<TourAgency> TourAgensies { get; set; }
|
||||
}
|
||||
21
MagicCarpetProject/MagicCarpetDatabase/Models/Agency.cs
Normal file
21
MagicCarpetProject/MagicCarpetDatabase/Models/Agency.cs
Normal file
@@ -0,0 +1,21 @@
|
||||
using AutoMapper;
|
||||
using MagicCarpetContracts.DataModels;
|
||||
using MagicCarpetContracts.Enums;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MagicCarpetDatabase.Models;
|
||||
|
||||
public class Agency
|
||||
{
|
||||
public required string Id { get; set; }
|
||||
public required TourType Type { get; set; }
|
||||
public required int Count { get; set; }
|
||||
|
||||
[ForeignKey("AgencyId")]
|
||||
public List<TourAgency>? Tours { get; set; }
|
||||
}
|
||||
24
MagicCarpetProject/MagicCarpetDatabase/Models/Client.cs
Normal file
24
MagicCarpetProject/MagicCarpetDatabase/Models/Client.cs
Normal file
@@ -0,0 +1,24 @@
|
||||
using AutoMapper;
|
||||
using MagicCarpetContracts.DataModels;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MagicCarpetDatabase.Models;
|
||||
|
||||
[AutoMap(typeof(ClientDataModel), ReverseMap = true)]
|
||||
public class Client
|
||||
{
|
||||
public required string Id { get; set; }
|
||||
|
||||
public required string FIO { get; set; }
|
||||
|
||||
public required string PhoneNumber { get; set; }
|
||||
|
||||
public double DiscountSize { get; set; }
|
||||
[ForeignKey("ClientId")]
|
||||
public List<Sale>? Sales { get; set; }
|
||||
}
|
||||
32
MagicCarpetProject/MagicCarpetDatabase/Models/Employee.cs
Normal file
32
MagicCarpetProject/MagicCarpetDatabase/Models/Employee.cs
Normal file
@@ -0,0 +1,32 @@
|
||||
using AutoMapper;
|
||||
using MagicCarpetContracts.DataModels;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MagicCarpetDatabase.Models;
|
||||
|
||||
[AutoMap(typeof(EmployeeDataModel), ReverseMap = true)]
|
||||
public class Employee
|
||||
{
|
||||
public required string Id { get; set; }
|
||||
|
||||
public required string FIO { get; set; }
|
||||
|
||||
public string Email { get; set; }
|
||||
|
||||
public string PostId { get; set; }
|
||||
|
||||
public DateTime BirthDate { get; set; }
|
||||
|
||||
public DateTime EmploymentDate { get; set; }
|
||||
|
||||
public bool IsDeleted { get; set; }
|
||||
[ForeignKey("EmployeeId")]
|
||||
public List<Salary>? Salaries { get; set; }
|
||||
[ForeignKey("EmployeeId")]
|
||||
public List<Sale>? Sales { get; set; }
|
||||
}
|
||||
20
MagicCarpetProject/MagicCarpetDatabase/Models/Post.cs
Normal file
20
MagicCarpetProject/MagicCarpetDatabase/Models/Post.cs
Normal file
@@ -0,0 +1,20 @@
|
||||
using MagicCarpetContracts.Enums;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MagicCarpetDatabase.Models;
|
||||
|
||||
public class Post
|
||||
{
|
||||
public required string Id { get; set; } = Guid.NewGuid().ToString();
|
||||
public required string PostId { get; set; }
|
||||
public required string PostName { get; set; }
|
||||
public PostType PostType { get; set; }
|
||||
public double Salary { get; set; }
|
||||
public bool IsActual { get; set; }
|
||||
public DateTime ChangeDate { get; set; }
|
||||
}
|
||||
16
MagicCarpetProject/MagicCarpetDatabase/Models/Salary.cs
Normal file
16
MagicCarpetProject/MagicCarpetDatabase/Models/Salary.cs
Normal file
@@ -0,0 +1,16 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MagicCarpetDatabase.Models;
|
||||
|
||||
public class Salary
|
||||
{
|
||||
public string Id { get; set; } = Guid.NewGuid().ToString();
|
||||
public required string EmployeeId { get; set; }
|
||||
public DateTime SalaryDate { get; set; }
|
||||
public double EmployeeSalary { get; set; }
|
||||
public Employee? Employee { get; set; }
|
||||
}
|
||||
32
MagicCarpetProject/MagicCarpetDatabase/Models/Sale.cs
Normal file
32
MagicCarpetProject/MagicCarpetDatabase/Models/Sale.cs
Normal file
@@ -0,0 +1,32 @@
|
||||
using MagicCarpetContracts.DataModels;
|
||||
using MagicCarpetContracts.Enums;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MagicCarpetDatabase.Models;
|
||||
|
||||
public class Sale
|
||||
{
|
||||
public string Id { get; set; } = Guid.NewGuid().ToString();
|
||||
|
||||
public required string EmployeeId { get; set; }
|
||||
|
||||
public string? ClientId { get; set; }
|
||||
|
||||
public DateTime SaleDate { get; set; }
|
||||
public double Sum { get; set; }
|
||||
|
||||
public DiscountType DiscountType { get; set; }
|
||||
|
||||
public double Discount { get; set; }
|
||||
|
||||
public bool IsCancel { get; set; }
|
||||
public Employee? Employee { get; set; }
|
||||
public Client? Client { get; set; }
|
||||
[ForeignKey("SaleId")]
|
||||
public List<SaleTour>? SaleTours { get; set; }
|
||||
}
|
||||
16
MagicCarpetProject/MagicCarpetDatabase/Models/SaleTour.cs
Normal file
16
MagicCarpetProject/MagicCarpetDatabase/Models/SaleTour.cs
Normal file
@@ -0,0 +1,16 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MagicCarpetDatabase.Models;
|
||||
|
||||
public class SaleTour
|
||||
{
|
||||
public required string SaleId { get; set; }
|
||||
|
||||
public required string TourId { get; set; }
|
||||
|
||||
public int Count { get; set; }
|
||||
}
|
||||
22
MagicCarpetProject/MagicCarpetDatabase/Models/Supplies.cs
Normal file
22
MagicCarpetProject/MagicCarpetDatabase/Models/Supplies.cs
Normal file
@@ -0,0 +1,22 @@
|
||||
using AutoMapper;
|
||||
using MagicCarpetContracts.DataModels;
|
||||
using MagicCarpetContracts.Enums;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MagicCarpetDatabase.Models;
|
||||
|
||||
public class Supplies
|
||||
{
|
||||
public required string Id { get; set; }
|
||||
public required TourType Type { get; set; }
|
||||
public DateTime ProductuionDate { get; set; }
|
||||
public required int Count { get; set; }
|
||||
|
||||
[ForeignKey("SuppliesId")]
|
||||
public List<TourSupplies>? Tours { get; set; }
|
||||
}
|
||||
30
MagicCarpetProject/MagicCarpetDatabase/Models/Tour.cs
Normal file
30
MagicCarpetProject/MagicCarpetDatabase/Models/Tour.cs
Normal file
@@ -0,0 +1,30 @@
|
||||
using AutoMapper;
|
||||
using MagicCarpetContracts.DataModels;
|
||||
using MagicCarpetContracts.Enums;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MagicCarpetDatabase.Models;
|
||||
|
||||
[AutoMap(typeof(TourDataModel), ReverseMap = true)]
|
||||
public class Tour
|
||||
{
|
||||
public required string Id { get; set; }
|
||||
public required string TourName { get; set; }
|
||||
public string? TourCountry { get; set; }
|
||||
public double Price { get; set; }
|
||||
public required TourType TourType { get; set; }
|
||||
[ForeignKey("TourId")]
|
||||
public List<SaleTour>? SaleTours { get; set; }
|
||||
[ForeignKey("TourId")]
|
||||
public List<TourHistory>? TourHistories { get; set; }
|
||||
[ForeignKey("SuppliesesId")]
|
||||
public List<TourSupplies>? TourSupplies { get; set; }
|
||||
[ForeignKey("AgenciesId")]
|
||||
public List<TourAgency>? TourAgency { get; set; }
|
||||
}
|
||||
16
MagicCarpetProject/MagicCarpetDatabase/Models/TourAgency.cs
Normal file
16
MagicCarpetProject/MagicCarpetDatabase/Models/TourAgency.cs
Normal file
@@ -0,0 +1,16 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MagicCarpetDatabase.Models;
|
||||
|
||||
public class TourAgency
|
||||
{
|
||||
public required string AgencyId { get; set; }
|
||||
public required string TourId { get; set; }
|
||||
public int Count { get; set; }
|
||||
public Agency? Agencies { get; set; }
|
||||
public Tour? Tours { get; set; }
|
||||
}
|
||||
16
MagicCarpetProject/MagicCarpetDatabase/Models/TourHistory.cs
Normal file
16
MagicCarpetProject/MagicCarpetDatabase/Models/TourHistory.cs
Normal file
@@ -0,0 +1,16 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MagicCarpetDatabase.Models;
|
||||
|
||||
public class TourHistory
|
||||
{
|
||||
public string Id { get; set; } = Guid.NewGuid().ToString();
|
||||
public required string TourId { get; set; }
|
||||
public double OldPrice { get; set; }
|
||||
public DateTime ChangeDate { get; set; }
|
||||
public Tour? Tour { get; set; }
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
using AutoMapper;
|
||||
using MagicCarpetContracts.DataModels;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MagicCarpetDatabase.Models;
|
||||
|
||||
[AutoMap(typeof(TourSuppliesDataModel), ReverseMap = true)]
|
||||
public class TourSupplies
|
||||
{
|
||||
public required string SuppliesId { get; set; }
|
||||
public required string TourId { get; set; }
|
||||
public int Count { get; set; }
|
||||
public Supplies? Supplies { get; set; }
|
||||
public Tour? Tours { get; set; }
|
||||
}
|
||||
@@ -9,6 +9,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MagicCarpetTests", "MagicCa
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MagicCarpetBusinessLogic", "MagicCarpetBusinessLogic\MagicCarpetBusinessLogic.csproj", "{688F9182-851F-4CF8-97CD-9B6F1E43D758}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MagicCarpetDatabase", "MagicCarpetDatabase\MagicCarpetDatabase.csproj", "{C69B43B2-8680-42D7-A111-306E2E8225FE}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Any CPU = Debug|Any CPU
|
||||
@@ -27,6 +29,10 @@ Global
|
||||
{688F9182-851F-4CF8-97CD-9B6F1E43D758}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{688F9182-851F-4CF8-97CD-9B6F1E43D758}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{688F9182-851F-4CF8-97CD-9B6F1E43D758}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{C69B43B2-8680-42D7-A111-306E2E8225FE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{C69B43B2-8680-42D7-A111-306E2E8225FE}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{C69B43B2-8680-42D7-A111-306E2E8225FE}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{C69B43B2-8680-42D7-A111-306E2E8225FE}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
|
||||
@@ -0,0 +1,296 @@
|
||||
using MagicCarpetBusinessLogic.Implementations;
|
||||
using MagicCarpetContracts.BusinessLogicContracts;
|
||||
using MagicCarpetContracts.DataModels;
|
||||
using MagicCarpetContracts.Enums;
|
||||
using MagicCarpetContracts.Exceptions;
|
||||
using MagicCarpetContracts.StoragesContracts;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Moq;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MagicCarpetTests.BusinessLogicContractsTests;
|
||||
|
||||
[TestFixture]
|
||||
internal class AgencyBusinessLogicContractTests
|
||||
{
|
||||
private IAgencyBusinessLogicContract _warehouseBusinessLogicContract;
|
||||
private Mock<IAgencyStorageContract> _warehouseStorageContract;
|
||||
|
||||
[OneTimeSetUp]
|
||||
public void OneTimeSetUp()
|
||||
{
|
||||
_warehouseStorageContract = new Mock<IAgencyStorageContract>();
|
||||
_warehouseBusinessLogicContract = new AgencyBusinessLogicContract(_warehouseStorageContract.Object, new Mock<ILogger>().Object);
|
||||
}
|
||||
|
||||
[TearDown]
|
||||
public void TearDown()
|
||||
{
|
||||
_warehouseStorageContract.Reset();
|
||||
}
|
||||
|
||||
public void GetAllSupplies_ReturnListOfRecords_Test()
|
||||
{
|
||||
// Arrange
|
||||
var listOriginal = new List<AgencyDataModel>()
|
||||
{
|
||||
new(Guid.NewGuid().ToString(), TourType.Beach, 1, []),
|
||||
new(Guid.NewGuid().ToString(), TourType.Beach, 1, []),
|
||||
new(Guid.NewGuid().ToString(), TourType.Beach, 1, []),
|
||||
};
|
||||
_warehouseStorageContract.Setup(x => x.GetList()).Returns(listOriginal);
|
||||
// Act
|
||||
var list = _warehouseBusinessLogicContract.GetAllComponents();
|
||||
// Assert
|
||||
Assert.That(list, Is.Not.Null);
|
||||
Assert.That(list, Is.EquivalentTo(listOriginal));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetAllSupplies_ReturnEmptyList_Test()
|
||||
{
|
||||
// Arrange
|
||||
_warehouseStorageContract.Setup(x => x.GetList()).Returns([]);
|
||||
// Act
|
||||
var list = _warehouseBusinessLogicContract.GetAllComponents();
|
||||
// Assert
|
||||
Assert.That(list, Is.Not.Null);
|
||||
Assert.That(list, Has.Count.EqualTo(0));
|
||||
_warehouseStorageContract.Verify(x => x.GetList(), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetAllSupplies_ReturnNull_ThrowException_Test()
|
||||
{
|
||||
// Arrange
|
||||
_warehouseStorageContract.Setup(x => x.GetList()).Returns((List<AgencyDataModel>)null);
|
||||
// Act & Assert
|
||||
Assert.That(() => _warehouseBusinessLogicContract.GetAllComponents(), Throws.TypeOf<NullListException>());
|
||||
_warehouseStorageContract.Verify(x => x.GetList(), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetAllSupplies_StorageThrowError_ThrowException_Test()
|
||||
{
|
||||
// Arrange
|
||||
_warehouseStorageContract.Setup(x => x.GetList()).Throws(new StorageException(new InvalidOperationException()));
|
||||
// Act & Assert
|
||||
Assert.That(() => _warehouseBusinessLogicContract.GetAllComponents(), Throws.TypeOf<StorageException>());
|
||||
_warehouseStorageContract.Verify(x => x.GetList(), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetSuppliesByData_GetById_ReturnRecord_Test()
|
||||
{
|
||||
// Arrange
|
||||
var id = Guid.NewGuid().ToString();
|
||||
var record = new AgencyDataModel(id, TourType.Beach, 1, []);
|
||||
_warehouseStorageContract.Setup(x => x.GetElementById(id)).Returns(record);
|
||||
// Act
|
||||
var element = _warehouseBusinessLogicContract.GetComponentByData(id);
|
||||
// Assert
|
||||
Assert.That(element, Is.Not.Null);
|
||||
Assert.That(element.Id, Is.EqualTo(id));
|
||||
_warehouseStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetComponentsByData_EmptyData_ThrowException_Test()
|
||||
{
|
||||
//Act&Assert
|
||||
Assert.That(() => _warehouseBusinessLogicContract.GetComponentByData(null), Throws.TypeOf<ArgumentNullException>());
|
||||
Assert.That(() => _warehouseBusinessLogicContract.GetComponentByData(string.Empty), Throws.TypeOf<ArgumentNullException>());
|
||||
_warehouseStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Never);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetSuppliesByData_GetById_NotFoundRecord_ThrowException_Test()
|
||||
{
|
||||
// Arrange
|
||||
_warehouseStorageContract.Setup(x => x.GetElementById(It.IsAny<string>())).Throws(new ElementNotFoundException(""));
|
||||
// Act & Assert
|
||||
Assert.That(() => _warehouseBusinessLogicContract.GetComponentByData(Guid.NewGuid().ToString()), Throws.TypeOf<ElementNotFoundException>());
|
||||
_warehouseStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetSuppliesByData_StorageThrowError_ThrowException_Test()
|
||||
{
|
||||
// Arrange
|
||||
_warehouseStorageContract.Setup(x => x.GetElementById(It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException()));
|
||||
// Act & Assert
|
||||
Assert.That(() => _warehouseBusinessLogicContract.GetComponentByData(Guid.NewGuid().ToString()), Throws.TypeOf<StorageException>());
|
||||
_warehouseStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void InsertSupplies_CorrectRecord_Test()
|
||||
{
|
||||
// Arrange
|
||||
var record = new AgencyDataModel(Guid.NewGuid().ToString(), TourType.Beach, 1,
|
||||
[new TourAgencyDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)]);
|
||||
_warehouseStorageContract.Setup(x => x.AddElement(It.IsAny<AgencyDataModel>()));
|
||||
// Act
|
||||
_warehouseBusinessLogicContract.InsertComponent(record);
|
||||
// Assert
|
||||
_warehouseStorageContract.Verify(x => x.AddElement(It.IsAny<AgencyDataModel>()), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void InsertSupplies_RecordWithExistsData_ThrowException_Test()
|
||||
{
|
||||
// Arrange
|
||||
_warehouseStorageContract.Setup(x => x.AddElement(It.IsAny<AgencyDataModel>())).Throws(new ElementExistsException("Data", "Data"));
|
||||
// Act & Assert
|
||||
Assert.That(() => _warehouseBusinessLogicContract.InsertComponent(new(Guid.NewGuid().ToString(), TourType.Beach, 1,
|
||||
[new TourAgencyDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)])), Throws.TypeOf<ElementExistsException>());
|
||||
_warehouseStorageContract.Verify(x => x.AddElement(It.IsAny<AgencyDataModel>()), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void InsertSupplies_NullRecord_ThrowException_Test()
|
||||
{
|
||||
// Act & Assert
|
||||
Assert.That(() => _warehouseBusinessLogicContract.InsertComponent(null), Throws.TypeOf<ArgumentNullException>());
|
||||
_warehouseStorageContract.Verify(x => x.AddElement(It.IsAny<AgencyDataModel>()), Times.Never);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void InsertComponents_InvalidRecord_ThrowException_Test()
|
||||
{
|
||||
//Act&Assert
|
||||
Assert.That(() => _warehouseBusinessLogicContract.InsertComponent(new AgencyDataModel("id", TourType.Beach, 1, [])), Throws.TypeOf<ValidationException>());
|
||||
_warehouseStorageContract.Verify(x => x.AddElement(It.IsAny<AgencyDataModel>()), Times.Never);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void InsertSupplies_StorageThrowError_ThrowException_Test()
|
||||
{
|
||||
// Arrange
|
||||
_warehouseStorageContract.Setup(x => x.AddElement(It.IsAny<AgencyDataModel>())).Throws(new StorageException(new InvalidOperationException()));
|
||||
// Act & Assert
|
||||
Assert.That(() => _warehouseBusinessLogicContract.InsertComponent(new(Guid.NewGuid().ToString(), TourType.Beach, 1,
|
||||
[new TourAgencyDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)])), Throws.TypeOf<StorageException>());
|
||||
_warehouseStorageContract.Verify(x => x.AddElement(It.IsAny<AgencyDataModel>()), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void UpdateSupplies_CorrectRecord_Test()
|
||||
{
|
||||
// Arrange
|
||||
var record = new AgencyDataModel(Guid.NewGuid().ToString(), TourType.Beach, 1,
|
||||
[new TourAgencyDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)]);
|
||||
_warehouseStorageContract.Setup(x => x.UpdElement(It.IsAny<AgencyDataModel>()));
|
||||
// Act
|
||||
_warehouseBusinessLogicContract.UpdateComponent(record);
|
||||
// Assert
|
||||
_warehouseStorageContract.Verify(x => x.UpdElement(It.IsAny<AgencyDataModel>()), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void UpdateSupplies_RecordWithIncorrectData_ThrowException_Test()
|
||||
{
|
||||
// Arrange
|
||||
_warehouseStorageContract.Setup(x => x.UpdElement(It.IsAny<AgencyDataModel>())).Throws(new ElementNotFoundException(""));
|
||||
// Act & Assert
|
||||
Assert.That(() => _warehouseBusinessLogicContract.UpdateComponent(new(Guid.NewGuid().ToString(), TourType.Beach, 1,
|
||||
[new TourAgencyDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)])), Throws.TypeOf<ElementNotFoundException>());
|
||||
_warehouseStorageContract.Verify(x => x.UpdElement(It.IsAny<AgencyDataModel>()), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void UpdateSupplies_RecordWithExistsData_ThrowException_Test()
|
||||
{
|
||||
// Arrange
|
||||
_warehouseStorageContract.Setup(x => x.UpdElement(It.IsAny<AgencyDataModel>())).Throws(new ElementExistsException("Data", "Data"));
|
||||
// Act & Assert
|
||||
Assert.That(() => _warehouseBusinessLogicContract.UpdateComponent(new(Guid.NewGuid().ToString(), TourType.Beach, 1,
|
||||
[new TourAgencyDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)])), Throws.TypeOf<ElementExistsException>());
|
||||
_warehouseStorageContract.Verify(x => x.UpdElement(It.IsAny<AgencyDataModel>()), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void UpdateSupplies_NullRecord_ThrowException_Test()
|
||||
{
|
||||
// Act & Assert
|
||||
Assert.That(() => _warehouseBusinessLogicContract.UpdateComponent(null), Throws.TypeOf<ArgumentNullException>());
|
||||
_warehouseStorageContract.Verify(x => x.UpdElement(It.IsAny<AgencyDataModel>()), Times.Never);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void UpdateComponents_InvalidRecord_ThrowException_Test()
|
||||
{
|
||||
//Act&Assert
|
||||
Assert.That(() => _warehouseBusinessLogicContract.UpdateComponent(new AgencyDataModel(Guid.NewGuid().ToString(), TourType.Beach, 1, [])), Throws.TypeOf<ValidationException>());
|
||||
_warehouseStorageContract.Verify(x => x.UpdElement(It.IsAny<AgencyDataModel>()), Times.Never);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void UpdateSupplies_StorageThrowError_ThrowException_Test()
|
||||
{
|
||||
// Arrange
|
||||
_warehouseStorageContract.Setup(x => x.UpdElement(It.IsAny<AgencyDataModel>())).Throws(new StorageException(new InvalidOperationException()));
|
||||
// Act & Assert
|
||||
Assert.That(() => _warehouseBusinessLogicContract.UpdateComponent(new(Guid.NewGuid().ToString(), TourType.Beach, 1,
|
||||
[new TourAgencyDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)])), Throws.TypeOf<StorageException>());
|
||||
_warehouseStorageContract.Verify(x => x.UpdElement(It.IsAny<AgencyDataModel>()), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DeleteComponents_CorrectRecord_Test()
|
||||
{
|
||||
//Arrange
|
||||
var id = Guid.NewGuid().ToString();
|
||||
var flag = false;
|
||||
_warehouseStorageContract.Setup(x => x.DelElement(It.Is((string x) => x == id))).Callback(() => { flag = true; });
|
||||
//Act
|
||||
_warehouseBusinessLogicContract.DeleteComponent(id);
|
||||
//Assert
|
||||
_warehouseStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Once); Assert.That(flag);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DeleteComponents_RecordWithIncorrectId_ThrowException_Test()
|
||||
{
|
||||
//Arrange
|
||||
var id = Guid.NewGuid().ToString();
|
||||
_warehouseStorageContract.Setup(x => x.DelElement(It.IsAny<string>())).Throws(new ElementNotFoundException(id));
|
||||
//Act&Assert
|
||||
Assert.That(() => _warehouseBusinessLogicContract.DeleteComponent(Guid.NewGuid().ToString()),
|
||||
Throws.TypeOf<ElementNotFoundException>());
|
||||
_warehouseStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DeleteComponents_IdIsNullOrEmpty_ThrowException_Test()
|
||||
{
|
||||
//Act&Assert
|
||||
Assert.That(() => _warehouseBusinessLogicContract.DeleteComponent(null), Throws.TypeOf<ArgumentNullException>());
|
||||
Assert.That(() => _warehouseBusinessLogicContract.DeleteComponent(string.Empty), Throws.TypeOf<ArgumentNullException>());
|
||||
_warehouseStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Never);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DeleteComponents_IdIsNotGuid_ThrowException_Test()
|
||||
{
|
||||
//Act&Assert
|
||||
Assert.That(() => _warehouseBusinessLogicContract.DeleteComponent("id"),
|
||||
Throws.TypeOf<ValidationException>());
|
||||
_warehouseStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Never);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DeleteComponents_StorageThrowError_ThrowException_Test()
|
||||
{
|
||||
//Arrange
|
||||
_warehouseStorageContract.Setup(x => x.DelElement(It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException()));
|
||||
//Act&Assert
|
||||
Assert.That(() => _warehouseBusinessLogicContract.DeleteComponent(Guid.NewGuid().ToString()),
|
||||
Throws.TypeOf<StorageException>());
|
||||
_warehouseStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Once);
|
||||
}
|
||||
}
|
||||
@@ -18,18 +18,22 @@ internal class SaleBusinessLogicContractTests
|
||||
{
|
||||
private SaleBusinessLogicContract _saleBusinessLogicContract;
|
||||
private Mock<ISaleStorageContract> _saleStorageContract;
|
||||
private Mock<IAgencyStorageContract> _agencyStorageContract;
|
||||
|
||||
[OneTimeSetUp]
|
||||
public void OneTimeSetUp()
|
||||
{
|
||||
_saleStorageContract = new Mock<ISaleStorageContract>();
|
||||
_saleBusinessLogicContract = new SaleBusinessLogicContract(_saleStorageContract.Object, new Mock<ILogger>().Object);
|
||||
_agencyStorageContract = new Mock<IAgencyStorageContract>();
|
||||
_saleBusinessLogicContract = new SaleBusinessLogicContract(_saleStorageContract.Object,
|
||||
_agencyStorageContract.Object, new Mock<ILogger>().Object);
|
||||
}
|
||||
|
||||
[TearDown]
|
||||
public void TearDown()
|
||||
{
|
||||
_saleStorageContract.Reset();
|
||||
_agencyStorageContract.Reset();
|
||||
}
|
||||
|
||||
[Test]
|
||||
@@ -396,6 +400,7 @@ internal class SaleBusinessLogicContractTests
|
||||
var flag = false;
|
||||
var record = new SaleDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 10, DiscountType.None, 10,
|
||||
false, [new SaleTourDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)]);
|
||||
_agencyStorageContract.Setup(x => x.CheckComponents(It.IsAny<SaleDataModel>())).Returns(true);
|
||||
_saleStorageContract.Setup(x => x.AddElement(It.IsAny<SaleDataModel>()))
|
||||
.Callback((SaleDataModel x) =>
|
||||
{
|
||||
@@ -409,6 +414,7 @@ internal class SaleBusinessLogicContractTests
|
||||
//Act
|
||||
_saleBusinessLogicContract.InsertSale(record);
|
||||
//Assert
|
||||
_agencyStorageContract.Verify(x => x.CheckComponents(It.IsAny<SaleDataModel>()), Times.Once);
|
||||
_saleStorageContract.Verify(x => x.AddElement(It.IsAny<SaleDataModel>()), Times.Once);
|
||||
Assert.That(flag);
|
||||
}
|
||||
@@ -417,11 +423,13 @@ internal class SaleBusinessLogicContractTests
|
||||
public void InsertSale_RecordWithExistsData_ThrowException_Test()
|
||||
{
|
||||
//Arrange
|
||||
_agencyStorageContract.Setup(x => x.CheckComponents(It.IsAny<SaleDataModel>())).Returns(true);
|
||||
_saleStorageContract.Setup(x => x.AddElement(It.IsAny<SaleDataModel>())).Throws(new ElementExistsException("Data", "Data"));
|
||||
//Act&Assert
|
||||
Assert.That(() => _saleBusinessLogicContract.InsertSale(new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(),
|
||||
Guid.NewGuid().ToString(), 10, DiscountType.None, 10, false, [new SaleTourDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)])), Throws.TypeOf<ElementExistsException>());
|
||||
_saleStorageContract.Verify(x => x.AddElement(It.IsAny<SaleDataModel>()), Times.Once);
|
||||
_agencyStorageContract.Verify(x => x.CheckComponents(It.IsAny<SaleDataModel>()), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
@@ -444,13 +452,27 @@ internal class SaleBusinessLogicContractTests
|
||||
public void InsertSale_StorageThrowError_ThrowException_Test()
|
||||
{
|
||||
//Arrange
|
||||
_agencyStorageContract.Setup(x => x.CheckComponents(It.IsAny<SaleDataModel>())).Returns(true);
|
||||
_saleStorageContract.Setup(x => x.AddElement(It.IsAny<SaleDataModel>())).Throws(new StorageException(new InvalidOperationException()));
|
||||
//Act&Assert
|
||||
Assert.That(() => _saleBusinessLogicContract.InsertSale(new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(),
|
||||
Guid.NewGuid().ToString(), 10, DiscountType.None, 10, false, [new SaleTourDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)])), Throws.TypeOf<StorageException>());
|
||||
_agencyStorageContract.Verify(x => x.CheckComponents(It.IsAny<SaleDataModel>()), Times.Once);
|
||||
_saleStorageContract.Verify(x => x.AddElement(It.IsAny<SaleDataModel>()), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void InsertSale_InsufficientError_ThrowException_Test()
|
||||
{
|
||||
//Arrange
|
||||
_agencyStorageContract.Setup(x => x.CheckComponents(It.IsAny<SaleDataModel>())).Returns(false);
|
||||
Assert.That(() => _saleBusinessLogicContract.InsertSale(new SaleDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(),
|
||||
Guid.NewGuid().ToString(), 10, DiscountType.None, 10, false,
|
||||
[new SaleTourDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)])), Throws.TypeOf<InsufficientException>());
|
||||
//Act&Assert
|
||||
_agencyStorageContract.Verify(x => x.CheckComponents(It.IsAny<SaleDataModel>()), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void CancelSale_CorrectRecord_Test()
|
||||
{
|
||||
|
||||
@@ -0,0 +1,242 @@
|
||||
using MagicCarpetBusinessLogic.Implementations;
|
||||
using MagicCarpetContracts.DataModels;
|
||||
using MagicCarpetContracts.Enums;
|
||||
using MagicCarpetContracts.Exceptions;
|
||||
using MagicCarpetContracts.StoragesContracts;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Moq;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MagicCarpetTests.BusinessLogicContractsTests;
|
||||
|
||||
[TestFixture]
|
||||
internal class SuppliesBusinessLogicContractTests
|
||||
{
|
||||
private SuppliesBusinessLogicContract _suppliesBusinessLogicContract;
|
||||
private Mock<ISuppliesStorageContract> _suppliesStorageContract;
|
||||
|
||||
[OneTimeSetUp]
|
||||
public void OneTimeSetUp()
|
||||
{
|
||||
_suppliesStorageContract = new Mock<ISuppliesStorageContract>();
|
||||
_suppliesBusinessLogicContract = new SuppliesBusinessLogicContract(_suppliesStorageContract.Object, new Mock<ILogger>().Object);
|
||||
}
|
||||
|
||||
[TearDown]
|
||||
public void TearDown()
|
||||
{
|
||||
_suppliesStorageContract.Reset();
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetAllSupplies_ReturnListOfRecords_Test()
|
||||
{
|
||||
// Arrange
|
||||
var listOriginal = new List<SuppliesDataModel>()
|
||||
{
|
||||
new(Guid.NewGuid().ToString(), TourType.Beach, DateTime.Now, 1, []),
|
||||
new(Guid.NewGuid().ToString(), TourType.Beach, DateTime.Now, 1, []),
|
||||
new(Guid.NewGuid().ToString(), TourType.Beach, DateTime.Now, 1, []),
|
||||
};
|
||||
_suppliesStorageContract.Setup(x => x.GetList(It.IsAny<DateTime?>())).Returns(listOriginal);
|
||||
// Act
|
||||
var list = _suppliesBusinessLogicContract.GetAllComponents();
|
||||
// Assert
|
||||
Assert.That(list, Is.Not.Null);
|
||||
Assert.That(list, Is.EquivalentTo(listOriginal));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetAllSupplies_ReturnEmptyList_Test()
|
||||
{
|
||||
// Arrange
|
||||
_suppliesStorageContract.Setup(x => x.GetList(It.IsAny<DateTime?>())).Returns([]);
|
||||
// Act
|
||||
var list = _suppliesBusinessLogicContract.GetAllComponents();
|
||||
// Assert
|
||||
Assert.That(list, Is.Not.Null);
|
||||
Assert.That(list, Has.Count.EqualTo(0));
|
||||
_suppliesStorageContract.Verify(x => x.GetList(It.IsAny<DateTime?>()), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetAllSupplies_ReturnNull_ThrowException_Test()
|
||||
{
|
||||
// Arrange
|
||||
_suppliesStorageContract.Setup(x => x.GetList(It.IsAny<DateTime?>())).Returns((List<SuppliesDataModel>)null);
|
||||
// Act & Assert
|
||||
Assert.That(() => _suppliesBusinessLogicContract.GetAllComponents(), Throws.TypeOf<NullListException>());
|
||||
_suppliesStorageContract.Verify(x => x.GetList(It.IsAny<DateTime?>()), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetAllSupplies_StorageThrowError_ThrowException_Test()
|
||||
{
|
||||
// Arrange
|
||||
_suppliesStorageContract.Setup(x => x.GetList(It.IsAny<DateTime?>())).Throws(new StorageException(new InvalidOperationException()));
|
||||
// Act & Assert
|
||||
Assert.That(() => _suppliesBusinessLogicContract.GetAllComponents(), Throws.TypeOf<StorageException>());
|
||||
_suppliesStorageContract.Verify(x => x.GetList(It.IsAny<DateTime?>()), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetSuppliesByData_GetById_ReturnRecord_Test()
|
||||
{
|
||||
// Arrange
|
||||
var id = Guid.NewGuid().ToString();
|
||||
var record = new SuppliesDataModel(id,TourType.Beach, DateTime.Now, 1, []);
|
||||
_suppliesStorageContract.Setup(x => x.GetElementById(id)).Returns(record);
|
||||
// Act
|
||||
var element = _suppliesBusinessLogicContract.GetComponentByData(id);
|
||||
// Assert
|
||||
Assert.That(element, Is.Not.Null);
|
||||
Assert.That(element.Id, Is.EqualTo(id));
|
||||
_suppliesStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetComponentsByData_EmptyData_ThrowException_Test()
|
||||
{
|
||||
//Act&Assert
|
||||
Assert.That(() => _suppliesBusinessLogicContract.GetComponentByData(null), Throws.TypeOf<ArgumentNullException>());
|
||||
Assert.That(() => _suppliesBusinessLogicContract.GetComponentByData(string.Empty), Throws.TypeOf<ArgumentNullException>());
|
||||
_suppliesStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Never);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetSuppliesByData_GetById_NotFoundRecord_ThrowException_Test()
|
||||
{
|
||||
// Arrange
|
||||
_suppliesStorageContract.Setup(x => x.GetElementById(It.IsAny<string>())).Throws(new ElementNotFoundException(""));
|
||||
// Act & Assert
|
||||
Assert.That(() => _suppliesBusinessLogicContract.GetComponentByData(Guid.NewGuid().ToString()), Throws.TypeOf<ElementNotFoundException>());
|
||||
_suppliesStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetSuppliesByData_StorageThrowError_ThrowException_Test()
|
||||
{
|
||||
// Arrange
|
||||
_suppliesStorageContract.Setup(x => x.GetElementById(It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException()));
|
||||
// Act & Assert
|
||||
Assert.That(() => _suppliesBusinessLogicContract.GetComponentByData(Guid.NewGuid().ToString()), Throws.TypeOf<StorageException>());
|
||||
_suppliesStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void InsertSupplies_CorrectRecord_Test()
|
||||
{
|
||||
// Arrange
|
||||
var record = new SuppliesDataModel(Guid.NewGuid().ToString(), TourType.Beach, DateTime.Now, 1,
|
||||
[new TourSuppliesDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)]);
|
||||
_suppliesStorageContract.Setup(x => x.AddElement(It.IsAny<SuppliesDataModel>()));
|
||||
// Act
|
||||
_suppliesBusinessLogicContract.InsertComponent(record);
|
||||
// Assert
|
||||
_suppliesStorageContract.Verify(x => x.AddElement(It.IsAny<SuppliesDataModel>()), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void InsertSupplies_RecordWithExistsData_ThrowException_Test()
|
||||
{
|
||||
// Arrange
|
||||
_suppliesStorageContract.Setup(x => x.AddElement(It.IsAny<SuppliesDataModel>())).Throws(new ElementExistsException("Data", "Data"));
|
||||
// Act & Assert
|
||||
Assert.That(() => _suppliesBusinessLogicContract.InsertComponent(new(Guid.NewGuid().ToString(), TourType.Beach, DateTime.Now, 1,
|
||||
[new TourSuppliesDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)])), Throws.TypeOf<ElementExistsException>());
|
||||
_suppliesStorageContract.Verify(x => x.AddElement(It.IsAny<SuppliesDataModel>()), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void InsertSupplies_NullRecord_ThrowException_Test()
|
||||
{
|
||||
// Act & Assert
|
||||
Assert.That(() => _suppliesBusinessLogicContract.InsertComponent(null), Throws.TypeOf<ArgumentNullException>());
|
||||
_suppliesStorageContract.Verify(x => x.AddElement(It.IsAny<SuppliesDataModel>()), Times.Never);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void InsertComponents_InvalidRecord_ThrowException_Test()
|
||||
{
|
||||
//Act&Assert
|
||||
Assert.That(() => _suppliesBusinessLogicContract.InsertComponent(new SuppliesDataModel("id", TourType.Beach, DateTime.UtcNow, 1, [])), Throws.TypeOf<ValidationException>());
|
||||
_suppliesStorageContract.Verify(x => x.AddElement(It.IsAny<SuppliesDataModel>()), Times.Never);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void InsertSupplies_StorageThrowError_ThrowException_Test()
|
||||
{
|
||||
// Arrange
|
||||
_suppliesStorageContract.Setup(x => x.AddElement(It.IsAny<SuppliesDataModel>())).Throws(new StorageException(new InvalidOperationException()));
|
||||
// Act & Assert
|
||||
Assert.That(() => _suppliesBusinessLogicContract.InsertComponent(new(Guid.NewGuid().ToString(), TourType.Beach, DateTime.Now, 1,
|
||||
[new TourSuppliesDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)])), Throws.TypeOf<StorageException>());
|
||||
_suppliesStorageContract.Verify(x => x.AddElement(It.IsAny<SuppliesDataModel>()), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void UpdateSupplies_CorrectRecord_Test()
|
||||
{
|
||||
// Arrange
|
||||
var record = new SuppliesDataModel(Guid.NewGuid().ToString(), TourType.Beach, DateTime.Now, 1,
|
||||
[new TourSuppliesDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)]);
|
||||
_suppliesStorageContract.Setup(x => x.UpdElement(It.IsAny<SuppliesDataModel>()));
|
||||
// Act
|
||||
_suppliesBusinessLogicContract.UpdateComponent(record);
|
||||
// Assert
|
||||
_suppliesStorageContract.Verify(x => x.UpdElement(It.IsAny<SuppliesDataModel>()), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void UpdateSupplies_RecordWithIncorrectData_ThrowException_Test()
|
||||
{
|
||||
// Arrange
|
||||
_suppliesStorageContract.Setup(x => x.UpdElement(It.IsAny<SuppliesDataModel>())).Throws(new ElementNotFoundException(""));
|
||||
// Act & Assert
|
||||
Assert.That(() => _suppliesBusinessLogicContract.UpdateComponent(new(Guid.NewGuid().ToString(), TourType.Beach, DateTime.Now, 1,
|
||||
[new TourSuppliesDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)])), Throws.TypeOf<ElementNotFoundException>());
|
||||
_suppliesStorageContract.Verify(x => x.UpdElement(It.IsAny<SuppliesDataModel>()), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void UpdateSupplies_RecordWithExistsData_ThrowException_Test()
|
||||
{
|
||||
// Arrange
|
||||
_suppliesStorageContract.Setup(x => x.UpdElement(It.IsAny<SuppliesDataModel>())).Throws(new ElementExistsException("Data", "Data"));
|
||||
// Act & Assert
|
||||
Assert.That(() => _suppliesBusinessLogicContract.UpdateComponent(new(Guid.NewGuid().ToString(), TourType.Beach, DateTime.Now, 1,
|
||||
[new TourSuppliesDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)])), Throws.TypeOf<ElementExistsException>());
|
||||
_suppliesStorageContract.Verify(x => x.UpdElement(It.IsAny<SuppliesDataModel>()), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void UpdateSupplies_NullRecord_ThrowException_Test()
|
||||
{
|
||||
// Act & Assert
|
||||
Assert.That(() => _suppliesBusinessLogicContract.UpdateComponent(null), Throws.TypeOf<ArgumentNullException>());
|
||||
_suppliesStorageContract.Verify(x => x.UpdElement(It.IsAny<SuppliesDataModel>()), Times.Never);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void UpdateComponents_InvalidRecord_ThrowException_Test()
|
||||
{
|
||||
//Act&Assert
|
||||
Assert.That(() => _suppliesBusinessLogicContract.UpdateComponent(new SuppliesDataModel(Guid.NewGuid().ToString(), TourType.Beach, DateTime.Now, 1, [])), Throws.TypeOf<ValidationException>());
|
||||
_suppliesStorageContract.Verify(x => x.UpdElement(It.IsAny<SuppliesDataModel>()), Times.Never);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void UpdateSupplies_StorageThrowError_ThrowException_Test()
|
||||
{
|
||||
// Arrange
|
||||
_suppliesStorageContract.Setup(x => x.UpdElement(It.IsAny<SuppliesDataModel>())).Throws(new StorageException(new InvalidOperationException()));
|
||||
// Act & Assert
|
||||
Assert.That(() => _suppliesBusinessLogicContract.UpdateComponent(new(Guid.NewGuid().ToString(), TourType.Beach, DateTime.Now, 1,
|
||||
[new TourSuppliesDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)])), Throws.TypeOf<StorageException>());
|
||||
_suppliesStorageContract.Verify(x => x.UpdElement(It.IsAny<SuppliesDataModel>()), Times.Once);
|
||||
}
|
||||
}
|
||||
@@ -3,6 +3,7 @@ using MagicCarpetContracts.DataModels;
|
||||
using MagicCarpetContracts.Enums;
|
||||
using MagicCarpetContracts.Exceptions;
|
||||
using MagicCarpetContracts.StoragesContracts;
|
||||
using MagicCarpetTests.DataModelTests;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Moq;
|
||||
using System;
|
||||
@@ -154,7 +155,7 @@ internal class TourBusinessLogicContractTests
|
||||
{
|
||||
//Arrange
|
||||
var id = Guid.NewGuid().ToString();
|
||||
var record = new TourDataModel(id, "name","country", 10, TourType.Ski);
|
||||
var record = new TourDataModel(id, "name", "country", 10, TourType.Ski);
|
||||
_tourStorageContract.Setup(x => x.GetElementById(id)).Returns(record);
|
||||
//Act
|
||||
var element = _tourBusinessLogicContract.GetTourByData(id);
|
||||
@@ -183,7 +184,7 @@ internal class TourBusinessLogicContractTests
|
||||
{
|
||||
//Arrange
|
||||
var country = "country";
|
||||
var record = new TourDataModel(Guid.NewGuid().ToString(), "name", country, 10, TourType.Ski);
|
||||
var record = new TourDataModel(Guid.NewGuid().ToString(), "name", country, 10, TourType.Ski);
|
||||
_tourStorageContract.Setup(x => x.GetElementByName(country)).Returns(record);
|
||||
//Act
|
||||
var element = _tourBusinessLogicContract.GetTourByData(country);
|
||||
@@ -249,7 +250,7 @@ internal class TourBusinessLogicContractTests
|
||||
.Callback((TourDataModel x) =>
|
||||
{
|
||||
flag = x.Id == record.Id && x.TourName == record.TourName && x.TourCountry == record.TourCountry
|
||||
&& x.Price == record.Price && x.Type == record.Type;
|
||||
&& x.Price == record.Price && x.TourType == record.TourType;
|
||||
});
|
||||
//Act
|
||||
_tourBusinessLogicContract.InsertTour(record);
|
||||
@@ -288,10 +289,18 @@ internal class TourBusinessLogicContractTests
|
||||
public void InsertTour_StorageThrowError_ThrowException_Test()
|
||||
{
|
||||
//Arrange
|
||||
_tourStorageContract.Setup(x => x.AddElement(It.IsAny<TourDataModel>())).Throws(new StorageException(new InvalidOperationException()));
|
||||
Assert.That(() => _tourBusinessLogicContract.InsertTour(new TourDataModel(Guid.NewGuid().ToString(), "name","country", 10, TourType.Ski)), Throws.TypeOf<InsufficientException>());
|
||||
//Act&Assert
|
||||
Assert.That(() => _tourBusinessLogicContract.InsertTour(new(Guid.NewGuid().ToString(), "name", "country", 10, TourType.Ski)), Throws.TypeOf<StorageException>());
|
||||
_tourStorageContract.Verify(x => x.AddElement(It.IsAny<TourDataModel>()), Times.Once);
|
||||
_tourStorageContract.Verify(x => x.UpdElement(It.IsAny<TourDataModel>()), Times.Never);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void InsertFurniture_InsufficientError_ThrowException_Test()
|
||||
{
|
||||
//Arrange
|
||||
Assert.That(() => _tourBusinessLogicContract.InsertTour(new TourDataModel(Guid.NewGuid().ToString(), "name", "country", 10, TourType.Ski)),Throws.TypeOf<InsufficientException>());
|
||||
//Act&Assert
|
||||
_tourStorageContract.Verify(x => x.UpdElement(It.IsAny<TourDataModel>()), Times.Never);
|
||||
}
|
||||
|
||||
[Test]
|
||||
@@ -304,7 +313,7 @@ internal class TourBusinessLogicContractTests
|
||||
.Callback((TourDataModel x) =>
|
||||
{
|
||||
flag = x.Id == record.Id && x.TourName == record.TourName && x.TourCountry == record.TourCountry
|
||||
&& x.Price == record.Price && x.Type == record.Type;
|
||||
&& x.Price == record.Price && x.TourType == record.TourType;
|
||||
});
|
||||
//Act
|
||||
_tourBusinessLogicContract.UpdateTour(record);
|
||||
@@ -329,7 +338,7 @@ internal class TourBusinessLogicContractTests
|
||||
//Arrange
|
||||
_tourStorageContract.Setup(x => x.UpdElement(It.IsAny<TourDataModel>())).Throws(new ElementExistsException("Data", "Data"));
|
||||
//Act&Assert
|
||||
Assert.That(() => _tourBusinessLogicContract.UpdateTour(new(Guid.NewGuid().ToString(), "name", "country", 10, TourType.Ski)), Throws.TypeOf<ElementExistsException>());
|
||||
Assert.That(() => _tourBusinessLogicContract.UpdateTour(new(Guid.NewGuid().ToString(), "name", "country", 10, TourType.Ski)), Throws.TypeOf <ElementExistsException>());
|
||||
_tourStorageContract.Verify(x => x.UpdElement(It.IsAny<TourDataModel>()), Times.Once);
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,79 @@
|
||||
using MagicCarpetContracts.DataModels;
|
||||
using MagicCarpetContracts.Enums;
|
||||
using MagicCarpetContracts.Exceptions;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MagicCarpetTests.DataModelTests;
|
||||
|
||||
[TestFixture]
|
||||
internal class AgencyDataModelTests
|
||||
{
|
||||
[Test]
|
||||
public void IdIsNullOrEmptyTest()
|
||||
{
|
||||
var model = CreateDataModel(null, TourType.Beach, 1, CreateSubDataModel());
|
||||
Assert.That(() => model.Validate(), Throws.TypeOf<ValidationException>());
|
||||
model = CreateDataModel(string.Empty, TourType.Beach, 1, CreateSubDataModel());
|
||||
Assert.That(() => model.Validate(), Throws.TypeOf<ValidationException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void IdIsNotGuidTest()
|
||||
{
|
||||
var model = CreateDataModel("id", TourType.Beach, 1, CreateSubDataModel());
|
||||
Assert.That(() => model.Validate(), Throws.TypeOf<ValidationException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TypeIsNoneTest()
|
||||
{
|
||||
var model = CreateDataModel(Guid.NewGuid().ToString(), TourType.None, 1, CreateSubDataModel());
|
||||
Assert.That(() => model.Validate(), Throws.TypeOf<ValidationException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void CountIsLessOrZeroTest()
|
||||
{
|
||||
var model = CreateDataModel(Guid.NewGuid().ToString(), TourType.None, 0, CreateSubDataModel());
|
||||
Assert.That(() => model.Validate(), Throws.TypeOf<ValidationException>());
|
||||
|
||||
model = CreateDataModel(Guid.NewGuid().ToString(), TourType.None, -1, CreateSubDataModel());
|
||||
Assert.That(() => model.Validate(), Throws.TypeOf<ValidationException>());
|
||||
}
|
||||
[Test]
|
||||
public void ToursIsNullOrEmptyTest()
|
||||
{
|
||||
var sale = CreateDataModel(Guid.NewGuid().ToString(), TourType.Beach, 1, null);
|
||||
Assert.That(() => sale.Validate(), Throws.TypeOf<ValidationException>());
|
||||
|
||||
sale = CreateDataModel(Guid.NewGuid().ToString(), TourType.Beach, 1, []);
|
||||
Assert.That(() => sale.Validate(), Throws.TypeOf<ValidationException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void AllFieldsIsCorrectTest()
|
||||
{
|
||||
var id = Guid.NewGuid().ToString();
|
||||
var type = TourType.Beach;
|
||||
var count = 1;
|
||||
var tours = CreateSubDataModel();
|
||||
var model = CreateDataModel(id, type, count, tours);
|
||||
Assert.DoesNotThrow(() => model.Validate());
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(model.Id, Is.EqualTo(id));
|
||||
Assert.That(model.Type, Is.EqualTo(type));
|
||||
Assert.That(model.Count, Is.EqualTo(count));
|
||||
Assert.That(model.Tours, Is.EqualTo(tours));
|
||||
});
|
||||
}
|
||||
|
||||
private static AgencyDataModel CreateDataModel(string? id, TourType type, int count, List<TourAgencyDataModel> tours)
|
||||
=> new AgencyDataModel(id, type, count, tours);
|
||||
private static List<TourAgencyDataModel> CreateSubDataModel()
|
||||
=> [new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 1)];
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
using MagicCarpetContracts.DataModels;
|
||||
using MagicCarpetContracts.Enums;
|
||||
using MagicCarpetContracts.Exceptions;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MagicCarpetTests.DataModelTests;
|
||||
|
||||
[TestFixture]
|
||||
internal class SuppliesDataModelTests
|
||||
{
|
||||
[Test]
|
||||
public void IdIsNullOrEmptyTest()
|
||||
{
|
||||
var model = CreateDataModel(null, TourType.Beach, DateTime.Now, 1, CreateSubDataModel());
|
||||
Assert.That(() => model.Validate(), Throws.TypeOf<ValidationException>());
|
||||
model = CreateDataModel(string.Empty, TourType.Beach, DateTime.Now, 1, CreateSubDataModel());
|
||||
Assert.That(() => model.Validate(), Throws.TypeOf<ValidationException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void IdIsNotGuidTest()
|
||||
{
|
||||
var model = CreateDataModel("id", TourType.Beach, DateTime.Now, 1, CreateSubDataModel());
|
||||
Assert.That(() => model.Validate(), Throws.TypeOf<ValidationException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TypeIsNoneTest()
|
||||
{
|
||||
var model = CreateDataModel(Guid.NewGuid().ToString(), TourType.None, DateTime.Now, 1, CreateSubDataModel());
|
||||
Assert.That(() => model.Validate(), Throws.TypeOf<ValidationException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void CountIsLessOrZeroTest()
|
||||
{
|
||||
var model = CreateDataModel(Guid.NewGuid().ToString(), TourType.None, DateTime.Now, 0, CreateSubDataModel());
|
||||
Assert.That(() => model.Validate(), Throws.TypeOf<ValidationException>());
|
||||
|
||||
model = CreateDataModel(Guid.NewGuid().ToString(), TourType.None, DateTime.Now, -1, CreateSubDataModel());
|
||||
Assert.That(() => model.Validate(), Throws.TypeOf<ValidationException>());
|
||||
}
|
||||
[Test]
|
||||
public void ToursIsNullOrEmptyTest()
|
||||
{
|
||||
var sale = CreateDataModel(Guid.NewGuid().ToString(), TourType.Beach, DateTime.Now, 1, null);
|
||||
Assert.That(() => sale.Validate(), Throws.TypeOf<ValidationException>());
|
||||
sale = CreateDataModel(Guid.NewGuid().ToString(), TourType.Beach, DateTime.Now, 1, []);
|
||||
Assert.That(() => sale.Validate(), Throws.TypeOf<ValidationException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void AllFieldsIsCorrectTest()
|
||||
{
|
||||
var id = Guid.NewGuid().ToString();
|
||||
var type = TourType.Beach;
|
||||
var date = DateTime.Now;
|
||||
var count = 1;
|
||||
var tours = CreateSubDataModel();
|
||||
var model = CreateDataModel(id, type, date, count, tours);
|
||||
Assert.DoesNotThrow(() => model.Validate());
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(model.Id, Is.EqualTo(id));
|
||||
Assert.That(model.Type, Is.EqualTo(type));
|
||||
Assert.That(model.ProductuionDate, Is.EqualTo(date));
|
||||
Assert.That(model.Count, Is.EqualTo(count));
|
||||
Assert.That(model.Tours, Is.EqualTo(tours));
|
||||
});
|
||||
}
|
||||
|
||||
private static SuppliesDataModel CreateDataModel(string? id, TourType type, DateTime date, int count, List<TourSuppliesDataModel> tours)
|
||||
=> new(id, type, date, count, tours);
|
||||
private static List<TourSuppliesDataModel> CreateSubDataModel()
|
||||
=> [new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 1)];
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
using MagicCarpetContracts.DataModels;
|
||||
using MagicCarpetContracts.Exceptions;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MagicCarpetTests.DataModelTests;
|
||||
|
||||
[TestFixture]
|
||||
internal class TourAgencyDataModelTests
|
||||
{
|
||||
[Test]
|
||||
public void AgencyIdIsNullOrEmptyTest()
|
||||
{
|
||||
var model = CreateDataModel(null, Guid.NewGuid().ToString(), 1);
|
||||
Assert.That(() => model.Validate(), Throws.TypeOf<ValidationException>());
|
||||
|
||||
model = CreateDataModel(string.Empty, Guid.NewGuid().ToString(), 1);
|
||||
Assert.That(() => model.Validate(), Throws.TypeOf<ValidationException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void AgencyIdIsNotGuidTest()
|
||||
{
|
||||
var model = CreateDataModel("id", Guid.NewGuid().ToString(), 1);
|
||||
Assert.That(() => model.Validate(), Throws.TypeOf<ValidationException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TourIdIsNullOrEmptyTest()
|
||||
{
|
||||
var model = CreateDataModel(Guid.NewGuid().ToString(), null, 1);
|
||||
Assert.That(() => model.Validate(), Throws.TypeOf<ValidationException>());
|
||||
|
||||
model = CreateDataModel(Guid.NewGuid().ToString(), string.Empty, 1);
|
||||
Assert.That(() => model.Validate(), Throws.TypeOf<ValidationException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TourIdIsNotGuidTest()
|
||||
{
|
||||
var model = CreateDataModel(Guid.NewGuid().ToString(), "id", 1);
|
||||
Assert.That(() => model.Validate(), Throws.TypeOf<ValidationException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void CountIsLessOrZeroTest()
|
||||
{
|
||||
var model = CreateDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 0);
|
||||
Assert.That(() => model.Validate(), Throws.TypeOf<ValidationException>());
|
||||
model = CreateDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), -1);
|
||||
Assert.That(() => model.Validate(), Throws.TypeOf<ValidationException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void AllFieldsIsCorrectTest()
|
||||
{
|
||||
var agencyId = Guid.NewGuid().ToString();
|
||||
var tourId = Guid.NewGuid().ToString();
|
||||
var count = 1;
|
||||
var model = CreateDataModel(agencyId, tourId, count);
|
||||
Assert.That(() => model.Validate(), Throws.Nothing);
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(model.AgencyId, Is.EqualTo(agencyId));
|
||||
Assert.That(model.TourId, Is.EqualTo(tourId));
|
||||
Assert.That(model.Count, Is.EqualTo(count));
|
||||
});
|
||||
}
|
||||
|
||||
private static TourAgencyDataModel CreateDataModel(string? agencyId, string? tourId, int count)
|
||||
=> new(agencyId, tourId, count);
|
||||
}
|
||||
@@ -3,6 +3,7 @@ using MagicCarpetContracts.Enums;
|
||||
using MagicCarpetContracts.Exceptions;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
@@ -14,17 +15,17 @@ internal class TourDataModelTests
|
||||
[Test]
|
||||
public void IdIsNullOrEmptyTest()
|
||||
{
|
||||
var cocktail = CreateDataModel(null, "name", "country", 10.5, TourType.Beach);
|
||||
Assert.That(() => cocktail.Validate(), Throws.TypeOf<ValidationException>());
|
||||
cocktail = CreateDataModel(string.Empty, "name", "country", 10.5, TourType.Beach);
|
||||
Assert.That(() => cocktail.Validate(), Throws.TypeOf<ValidationException>());
|
||||
var tour = CreateDataModel(null, "name", "country", 10.5, TourType.Beach);
|
||||
Assert.That(() => tour.Validate(), Throws.TypeOf<ValidationException>());
|
||||
tour = CreateDataModel(string.Empty, "name", "country", 10.5, TourType.Beach);
|
||||
Assert.That(() => tour.Validate(), Throws.TypeOf<ValidationException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void IdIsNotGuidTest()
|
||||
{
|
||||
var cocktail = CreateDataModel("id", "name", "country", 10.5, TourType.Beach);
|
||||
Assert.That(() => cocktail.Validate(), Throws.TypeOf<ValidationException>());
|
||||
var tour = CreateDataModel("id", "name", "country", 10.5, TourType.Beach);
|
||||
Assert.That(() => tour.Validate(), Throws.TypeOf<ValidationException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
@@ -46,17 +47,17 @@ internal class TourDataModelTests
|
||||
[Test]
|
||||
public void PriceIsLessOrZeroTest()
|
||||
{
|
||||
var cocktail = CreateDataModel(Guid.NewGuid().ToString(), null, null, 0, TourType.Beach);
|
||||
Assert.That(() => cocktail.Validate(), Throws.TypeOf<ValidationException>());
|
||||
cocktail = CreateDataModel(Guid.NewGuid().ToString(), string.Empty, string.Empty, -10.5, TourType.Beach);
|
||||
Assert.That(() => cocktail.Validate(), Throws.TypeOf<ValidationException>());
|
||||
var tour = CreateDataModel(Guid.NewGuid().ToString(), null, null, 0, TourType.Beach);
|
||||
Assert.That(() => tour.Validate(), Throws.TypeOf<ValidationException>());
|
||||
tour = CreateDataModel(Guid.NewGuid().ToString(), string.Empty, string.Empty, -10.5, TourType.Beach);
|
||||
Assert.That(() => tour.Validate(), Throws.TypeOf<ValidationException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TourTypeIsNoneTest()
|
||||
{
|
||||
var cocktail = CreateDataModel(Guid.NewGuid().ToString(), null, null, 0, TourType.Beach);
|
||||
Assert.That(() => cocktail.Validate(), Throws.TypeOf<ValidationException>());
|
||||
var tour = CreateDataModel(Guid.NewGuid().ToString(), null, null, 0, TourType.Beach);
|
||||
Assert.That(() => tour.Validate(), Throws.TypeOf<ValidationException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
@@ -67,6 +68,8 @@ internal class TourDataModelTests
|
||||
var tourCountry = "country";
|
||||
var price = 10.5;
|
||||
var tourType = TourType.Ski;
|
||||
var supplies = CreateSuppliesDataModel();
|
||||
var agency = CreateAgencyDataModel();
|
||||
var tour = CreateDataModel(tourId, tourName, tourCountry, price, tourType);
|
||||
Assert.That(() => tour.Validate(), Throws.Nothing);
|
||||
Assert.Multiple(() =>
|
||||
@@ -75,10 +78,15 @@ internal class TourDataModelTests
|
||||
Assert.That(tour.TourName, Is.EqualTo(tourName));
|
||||
Assert.That(tour.TourCountry, Is.EqualTo(tourCountry));
|
||||
Assert.That(tour.Price, Is.EqualTo(price));
|
||||
Assert.That(tour.Type, Is.EqualTo(tourType));
|
||||
Assert.That(tour.TourType, Is.EqualTo(tourType));
|
||||
});
|
||||
}
|
||||
|
||||
private static TourDataModel CreateDataModel(string? id, string? tourName, string? countryName,double price, TourType tourType) =>
|
||||
private static TourDataModel CreateDataModel(string? id, string? tourName, string? countryName, double price, TourType tourType) =>
|
||||
new(id, tourName, countryName,price, tourType);
|
||||
private static List<TourSuppliesDataModel> CreateSuppliesDataModel()
|
||||
=> [new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 1)];
|
||||
|
||||
private static List<TourAgencyDataModel> CreateAgencyDataModel()
|
||||
=> [new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 1)];
|
||||
}
|
||||
|
||||
@@ -0,0 +1,75 @@
|
||||
using MagicCarpetContracts.DataModels;
|
||||
using MagicCarpetContracts.Exceptions;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MagicCarpetTests.DataModelTests;
|
||||
|
||||
[TestFixture]
|
||||
internal class TourSuppliesDataModelTests
|
||||
{
|
||||
[Test]
|
||||
public void SuppliesIdIsNullOrEmptyTest()
|
||||
{
|
||||
var model = CreateDataModel(null, Guid.NewGuid().ToString(), 1);
|
||||
Assert.That(() => model.Validate(), Throws.TypeOf<ValidationException>());
|
||||
|
||||
model = CreateDataModel(string.Empty, Guid.NewGuid().ToString(), 1);
|
||||
Assert.That(() => model.Validate(), Throws.TypeOf<ValidationException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void SuppliesIdIsNotGuidTest()
|
||||
{
|
||||
var model = CreateDataModel("id", Guid.NewGuid().ToString(), 1);
|
||||
Assert.That(() => model.Validate(), Throws.TypeOf<ValidationException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TourIdIsNullOrEmptyTest()
|
||||
{
|
||||
var model = CreateDataModel(Guid.NewGuid().ToString(), null, 1);
|
||||
Assert.That(() => model.Validate(), Throws.TypeOf<ValidationException>());
|
||||
|
||||
model = CreateDataModel(Guid.NewGuid().ToString(), string.Empty, 1);
|
||||
Assert.That(() => model.Validate(), Throws.TypeOf<ValidationException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TourIdIsNotGuidTest()
|
||||
{
|
||||
var model = CreateDataModel(Guid.NewGuid().ToString(), "id", 1);
|
||||
Assert.That(() => model.Validate(), Throws.TypeOf<ValidationException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void CountIsLessOrZeroTest()
|
||||
{
|
||||
var model = CreateDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 0);
|
||||
Assert.That(() => model.Validate(), Throws.TypeOf<ValidationException>());
|
||||
model = CreateDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), -1);
|
||||
Assert.That(() => model.Validate(), Throws.TypeOf<ValidationException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void AllFieldsIsCorrectTest()
|
||||
{
|
||||
var suppliesId = Guid.NewGuid().ToString();
|
||||
var tourId = Guid.NewGuid().ToString();
|
||||
var count = 1;
|
||||
var model = CreateDataModel(suppliesId, tourId, count);
|
||||
Assert.That(() => model.Validate(), Throws.Nothing);
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(model.SuppliesId, Is.EqualTo(suppliesId));
|
||||
Assert.That(model.TourId, Is.EqualTo(tourId));
|
||||
Assert.That(model.Count, Is.EqualTo(count));
|
||||
});
|
||||
}
|
||||
|
||||
private static TourSuppliesDataModel CreateDataModel(string? suppliesId, string? tourId, int count)
|
||||
=> new(suppliesId, tourId, count);
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using MagicCarpetContracts.Infrastructure;
|
||||
|
||||
namespace MagicCarpetTests.Infrastructure;
|
||||
|
||||
internal class ConfigurationDatabaseTest : IConfigurationDatabase
|
||||
{
|
||||
public string ConnectionString => "Host=127.0.0.1;Port=5432;Database=MagicCarpetTest;Username=postgres;Password=postgres;";
|
||||
}
|
||||
@@ -11,6 +11,7 @@
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="coverlet.collector" Version="6.0.0" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="9.0.2" />
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
|
||||
<PackageReference Include="Moq" Version="4.20.72" />
|
||||
<PackageReference Include="NUnit" Version="3.14.0" />
|
||||
@@ -21,6 +22,7 @@
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\MagicCarpetBusinessLogic\MagicCarpetBusinessLogic.csproj" />
|
||||
<ProjectReference Include="..\MagicCarpetContracts\MagicCarpetContracts.csproj" />
|
||||
<ProjectReference Include="..\MagicCarpetDatabase\MagicCarpetDatabase.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
||||
@@ -0,0 +1,215 @@
|
||||
using MagicCarpetContracts.Exceptions;
|
||||
using MagicCarpetDatabase.Implementations;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using MagicCarpetDatabase.Models;
|
||||
using MagicCarpetContracts.Enums;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using MagicCarpetContracts.DataModels;
|
||||
|
||||
namespace MagicCarpetTests.StoragesContractsTests;
|
||||
|
||||
[TestFixture]
|
||||
internal class AgencyStorageContractTests : BaseStorageContractTest
|
||||
{
|
||||
private AgencyStorageContract _agencyStorageContract;
|
||||
private Tour _tour;
|
||||
|
||||
[SetUp]
|
||||
public void SetUp()
|
||||
{
|
||||
_agencyStorageContract = new AgencyStorageContract(MagicCarpetDbContext);
|
||||
_tour = InsertTourToDatabaseAndReturn(Guid.NewGuid().ToString(), "name", TourType.Sightseeing);
|
||||
}
|
||||
|
||||
[TearDown]
|
||||
public void TearDown()
|
||||
{
|
||||
MagicCarpetDbContext.Database.ExecuteSqlRaw("TRUNCATE \"Agencies\" CASCADE;");
|
||||
MagicCarpetDbContext.Database.ExecuteSqlRaw("TRUNCATE \"Tours\" CASCADE;");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_GetList_WhenHaveRecords_Test()
|
||||
{
|
||||
var agency = InsertAgencyToDatabaseAndReturn(Guid.NewGuid().ToString(), TourType.Sightseeing, 5);
|
||||
InsertAgencyToDatabaseAndReturn(Guid.NewGuid().ToString(), TourType.Sightseeing, 5);
|
||||
InsertAgencyToDatabaseAndReturn(Guid.NewGuid().ToString(), TourType.Sightseeing, 5);
|
||||
var list = _agencyStorageContract.GetList();
|
||||
Assert.That(list, Is.Not.Null);
|
||||
Assert.That(list, Has.Count.EqualTo(3));
|
||||
AssertElement(list.First(), agency);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_GetList_WhenNoRecords_Test()
|
||||
{
|
||||
var list = _agencyStorageContract.GetList();
|
||||
Assert.That(list, Is.Not.Null);
|
||||
Assert.That(list, Is.Empty);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_GetElementById_WhenHaveRecord_Test()
|
||||
{
|
||||
var agency = InsertAgencyToDatabaseAndReturn(Guid.NewGuid().ToString(), TourType.Sightseeing, 5);
|
||||
var result = _agencyStorageContract.GetElementById(agency.Id);
|
||||
AssertElement(result, agency);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_GetElementById_WhenNoRecord_Test()
|
||||
{
|
||||
Assert.That(() => _agencyStorageContract.GetElementById(Guid.NewGuid().ToString()), Is.Null);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_AddElement_WhenValidData_Test()
|
||||
{
|
||||
var agency = CreateModel(Guid.NewGuid().ToString(), TourType.Sightseeing, 5, [_tour.Id]);
|
||||
_agencyStorageContract.AddElement(agency);
|
||||
var result = GetAgencyFromDatabaseById(agency.Id);
|
||||
AssertElement(result, agency);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_AddElement_WhenHaveRecordWithSameId_Test()
|
||||
{
|
||||
var agency = CreateModel(Guid.NewGuid().ToString(), TourType.Sightseeing, 5, [_tour.Id]);
|
||||
InsertAgencyToDatabaseAndReturn(agency.Id, TourType.Sightseeing, 5, [(_tour.Id, 3)]);
|
||||
Assert.That(() => _agencyStorageContract.AddElement(agency), Throws.TypeOf<StorageException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_AddElement_WhenNoHaveTour_Test()
|
||||
{
|
||||
var agency = CreateModel(Guid.NewGuid().ToString(), TourType.Sightseeing, 0, [_tour.Id]);
|
||||
InsertAgencyToDatabaseAndReturn(agency.Id, TourType.Sightseeing, 5, [(_tour.Id, 3)]);
|
||||
Assert.That(() => _agencyStorageContract.AddElement(agency), Throws.TypeOf<StorageException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_UpdElement_WhenValidData_Test()
|
||||
{
|
||||
var agency = CreateModel(Guid.NewGuid().ToString(), TourType.Sightseeing, 5, [_tour.Id]);
|
||||
InsertAgencyToDatabaseAndReturn(agency.Id, TourType.Ski, 10, null);
|
||||
_agencyStorageContract.UpdElement(agency);
|
||||
var result = GetAgencyFromDatabaseById(agency.Id);
|
||||
AssertElement(result, agency);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_UpdElement_WhenNoRecordWithThisId_Test()
|
||||
{
|
||||
var agency = CreateModel(Guid.NewGuid().ToString(), TourType.Sightseeing, 5, [_tour.Id]);
|
||||
Assert.That(() => _agencyStorageContract.UpdElement(agency), Throws.TypeOf<ElementNotFoundException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_DelElement_WhenRecordExists_Test()
|
||||
{
|
||||
var agency = InsertAgencyToDatabaseAndReturn(Guid.NewGuid().ToString(), TourType.Sightseeing, 5);
|
||||
_agencyStorageContract.DelElement(agency.Id);
|
||||
var result = GetAgencyFromDatabaseById(agency.Id);
|
||||
Assert.That(result, Is.Null);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_DelElement_WhenRecordDoesNotExist_ThrowsElementNotFoundException()
|
||||
{
|
||||
Assert.That(() => _agencyStorageContract.DelElement(Guid.NewGuid().ToString()), Throws.TypeOf<ElementNotFoundException>());
|
||||
}
|
||||
|
||||
private Tour InsertTourToDatabaseAndReturn(string id, string name, TourType type)
|
||||
{
|
||||
var tour = new Tour { Id = id, TourName = name, TourType = type };
|
||||
MagicCarpetDbContext.Tours.Add(tour);
|
||||
MagicCarpetDbContext.SaveChanges();
|
||||
return tour;
|
||||
}
|
||||
|
||||
private Agency InsertAgencyToDatabaseAndReturn(string id, TourType type, int count, List<(string, int)>? tours = null)
|
||||
{
|
||||
var agency = new Agency { Id = id, Type = type, Count = count, Tours = [] };
|
||||
if (tours is not null)
|
||||
{
|
||||
foreach (var elem in tours)
|
||||
{
|
||||
agency.Tours.Add(new TourAgency { AgencyId = agency.Id, TourId = elem.Item1, Count = elem.Item2 });
|
||||
}
|
||||
}
|
||||
MagicCarpetDbContext.Agencies.Add(agency);
|
||||
MagicCarpetDbContext.SaveChanges();
|
||||
return agency;
|
||||
}
|
||||
|
||||
private static void AssertElement(AgencyDataModel? actual, Agency expected)
|
||||
{
|
||||
Assert.That(actual, Is.Not.Null);
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(actual.Id, Is.EqualTo(expected.Id));
|
||||
Assert.That(actual.Type, Is.EqualTo(expected.Type));
|
||||
Assert.That(actual.Count, Is.EqualTo(expected.Count));
|
||||
});
|
||||
if (expected.Tours is not null)
|
||||
{
|
||||
Assert.That(actual.Tours, Is.Not.Null);
|
||||
Assert.That(actual.Tours, Has.Count.EqualTo(expected.Tours.Count));
|
||||
for (int i = 0; i < actual.Tours.Count; ++i)
|
||||
{
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(actual.Tours[i].TourId, Is.EqualTo(expected.Tours[i].TourId));
|
||||
Assert.That(actual.Tours[i].Count, Is.EqualTo(expected.Tours[i].Count));
|
||||
});
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Assert.That(actual.Tours, Is.Null);
|
||||
}
|
||||
}
|
||||
|
||||
private static void AssertElement(Agency? actual, AgencyDataModel expected)
|
||||
{
|
||||
Assert.That(actual, Is.Not.Null);
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(actual.Id, Is.EqualTo(expected.Id));
|
||||
Assert.That(actual.Type, Is.EqualTo(expected.Type));
|
||||
Assert.That(actual.Count, Is.EqualTo(expected.Count));
|
||||
});
|
||||
if (expected.Tours is not null)
|
||||
{
|
||||
Assert.That(actual.Tours, Is.Not.Null);
|
||||
Assert.That(actual.Tours, Has.Count.EqualTo(expected.Tours.Count));
|
||||
for (int i = 0; i < actual.Tours.Count; ++i)
|
||||
{
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(actual.Tours[i].TourId, Is.EqualTo(expected.Tours[i].TourId));
|
||||
Assert.That(actual.Tours[i].Count, Is.EqualTo(expected.Tours[i].Count));
|
||||
});
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Assert.That(actual.Tours, Is.Null);
|
||||
}
|
||||
}
|
||||
|
||||
private static AgencyDataModel CreateModel(string id, TourType type, int count, List<string> toursIds)
|
||||
{
|
||||
var tours = toursIds.Select(x => new TourAgencyDataModel(id, x, 1)).ToList();
|
||||
return new(id, type, count, tours);
|
||||
}
|
||||
|
||||
private Agency? GetAgencyFromDatabaseById(string id)
|
||||
{
|
||||
return MagicCarpetDbContext.Agencies.FirstOrDefault(x => x.Id == id);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
using MagicCarpetTests.Infrastructure;
|
||||
using MagicCarpetDatabase;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MagicCarpetTests.StoragesContractsTests;
|
||||
|
||||
internal abstract class BaseStorageContractTest
|
||||
{
|
||||
protected MagicCarpetDbContext MagicCarpetDbContext { get; private set; }
|
||||
|
||||
[OneTimeSetUp]
|
||||
public void OneTimeSetUp()
|
||||
{
|
||||
MagicCarpetDbContext = new MagicCarpetDbContext(new ConfigurationDatabaseTest());
|
||||
|
||||
MagicCarpetDbContext.Database.EnsureDeleted();
|
||||
MagicCarpetDbContext.Database.EnsureCreated();
|
||||
}
|
||||
|
||||
[OneTimeTearDown]
|
||||
public void OneTimeTearDown()
|
||||
{
|
||||
MagicCarpetDbContext.Database.EnsureDeleted();
|
||||
MagicCarpetDbContext.Dispose();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,217 @@
|
||||
using MagicCarpetContracts.DataModels;
|
||||
using MagicCarpetContracts.Enums;
|
||||
using MagicCarpetContracts.Exceptions;
|
||||
using MagicCarpetDatabase.Implementations;
|
||||
using MagicCarpetDatabase.Models;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MagicCarpetTests.StoragesContractsTests;
|
||||
|
||||
[TestFixture]
|
||||
internal class ClientStorageContractTests : BaseStorageContractTest
|
||||
{
|
||||
private ClientStorageContarct _clientStorageContract;
|
||||
|
||||
[SetUp]
|
||||
public void SetUp()
|
||||
{
|
||||
_clientStorageContract = new ClientStorageContarct(MagicCarpetDbContext);
|
||||
}
|
||||
|
||||
[TearDown]
|
||||
public void TearDown()
|
||||
{
|
||||
MagicCarpetDbContext.Database.ExecuteSqlRaw("TRUNCATE \"Sales\" CASCADE;");
|
||||
MagicCarpetDbContext.Database.ExecuteSqlRaw("TRUNCATE \"Employees\" CASCADE;");
|
||||
MagicCarpetDbContext.Database.ExecuteSqlRaw("TRUNCATE \"Clients\" CASCADE;");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_GetList_WhenHaveRecords_Test()
|
||||
{
|
||||
var client = InsertClientToDatabaseAndReturn(Guid.NewGuid().ToString(), phoneNumber: "+5-555-555-55-55");
|
||||
InsertClientToDatabaseAndReturn(Guid.NewGuid().ToString(), phoneNumber: "+6-666-666-66-66");
|
||||
InsertClientToDatabaseAndReturn(Guid.NewGuid().ToString(), phoneNumber: "+7-777-777-77-77");
|
||||
var list = _clientStorageContract.GetList();
|
||||
Assert.That(list, Is.Not.Null);
|
||||
Assert.That(list, Has.Count.EqualTo(3));
|
||||
AssertElement(list.First(x => x.Id == client.Id), client);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_GetList_WhenNoRecords_Test()
|
||||
{
|
||||
var list = _clientStorageContract.GetList();
|
||||
Assert.That(list, Is.Not.Null);
|
||||
Assert.That(list, Is.Empty);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_GetElementById_WhenHaveRecord_Test()
|
||||
{
|
||||
var client = InsertClientToDatabaseAndReturn(Guid.NewGuid().ToString());
|
||||
AssertElement(_clientStorageContract.GetElementById(client.Id), client);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_GetElementById_WhenNoRecord_Test()
|
||||
{
|
||||
InsertClientToDatabaseAndReturn(Guid.NewGuid().ToString());
|
||||
Assert.That(() => _clientStorageContract.GetElementById(Guid.NewGuid().ToString()), Is.Null);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_GetElementByFIO_WhenHaveRecord_Test()
|
||||
{
|
||||
var client = InsertClientToDatabaseAndReturn(Guid.NewGuid().ToString());
|
||||
AssertElement(_clientStorageContract.GetElementByFIO(client.FIO), client);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_GetElementByFIO_WhenNoRecord_Test()
|
||||
{
|
||||
InsertClientToDatabaseAndReturn(Guid.NewGuid().ToString());
|
||||
Assert.That(() => _clientStorageContract.GetElementByFIO("New Fio"), Is.Null);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_GetElementByPhoneNumber_WhenHaveRecord_Test()
|
||||
{
|
||||
var client = InsertClientToDatabaseAndReturn(Guid.NewGuid().ToString());
|
||||
AssertElement(_clientStorageContract.GetElementByPhoneNumber(client.PhoneNumber), client);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_GetElementByPhoneNumber_WhenNoRecord_Test()
|
||||
{
|
||||
InsertClientToDatabaseAndReturn(Guid.NewGuid().ToString());
|
||||
Assert.That(() => _clientStorageContract.GetElementByPhoneNumber("+8-888-888-88-88"), Is.Null);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_AddElement_Test()
|
||||
{
|
||||
var client = CreateModel(Guid.NewGuid().ToString());
|
||||
_clientStorageContract.AddElement(client);
|
||||
AssertElement(GetClientFromDatabase(client.Id), client);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_AddElement_WhenHaveRecordWithSameId_Test()
|
||||
{
|
||||
var client = CreateModel(Guid.NewGuid().ToString(), "New Fio", "+5-555-555-55-55", 500);
|
||||
InsertClientToDatabaseAndReturn(client.Id);
|
||||
Assert.That(() => _clientStorageContract.AddElement(client), Throws.TypeOf<ElementExistsException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_AddElement_WhenHaveRecordWithSamePhoneNumber_Test()
|
||||
{
|
||||
var client = CreateModel(Guid.NewGuid().ToString(), "New Fio", "+5-555-555-55-55", 500);
|
||||
InsertClientToDatabaseAndReturn(Guid.NewGuid().ToString(), phoneNumber: client.PhoneNumber);
|
||||
Assert.That(() => _clientStorageContract.AddElement(client), Throws.TypeOf<ElementExistsException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_UpdElement_Test()
|
||||
{
|
||||
var client = CreateModel(Guid.NewGuid().ToString(), "New Fio", "+5-555-555-55-55", 500);
|
||||
InsertClientToDatabaseAndReturn(client.Id);
|
||||
_clientStorageContract.UpdElement(client);
|
||||
AssertElement(GetClientFromDatabase(client.Id), client);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_UpdElement_WhenNoRecordWithThisId_Test()
|
||||
{
|
||||
Assert.That(() => _clientStorageContract.UpdElement(CreateModel(Guid.NewGuid().ToString())), Throws.TypeOf<ElementNotFoundException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_UpdElement_WhenHaveRecordWithSamePhoneNumber_Test()
|
||||
{
|
||||
var client = CreateModel(Guid.NewGuid().ToString(), "New Fio", "+5-555-555-55-55", 500);
|
||||
InsertClientToDatabaseAndReturn(client.Id, phoneNumber: "+7-777-777-77-77");
|
||||
InsertClientToDatabaseAndReturn(Guid.NewGuid().ToString(), phoneNumber: client.PhoneNumber);
|
||||
Assert.That(() => _clientStorageContract.UpdElement(client), Throws.TypeOf<ElementExistsException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_DelElement_Test()
|
||||
{
|
||||
var client = InsertClientToDatabaseAndReturn(Guid.NewGuid().ToString());
|
||||
_clientStorageContract.DelElement(client.Id);
|
||||
var element = GetClientFromDatabase(client.Id);
|
||||
Assert.That(element, Is.Null);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_DelElement_WhenHaveSalesByThisBuyer_Test()
|
||||
{
|
||||
var client = InsertClientToDatabaseAndReturn(Guid.NewGuid().ToString());
|
||||
var employeeId = Guid.NewGuid().ToString();
|
||||
MagicCarpetDbContext.Employees.Add(new Employee() { Id = employeeId, FIO = "test", PostId = Guid.NewGuid().ToString(), Email = "abc@gmail.com" });
|
||||
MagicCarpetDbContext.Sales.Add(new Sale() { Id = Guid.NewGuid().ToString(), EmployeeId = employeeId, ClientId = client.Id, Sum = 10, DiscountType = DiscountType.None, Discount = 0 });
|
||||
MagicCarpetDbContext.Sales.Add(new Sale() { Id = Guid.NewGuid().ToString(), EmployeeId = employeeId, ClientId = client.Id, Sum = 10, DiscountType = DiscountType.None, Discount = 0 });
|
||||
MagicCarpetDbContext.SaveChanges();
|
||||
var salesBeforeDelete = MagicCarpetDbContext.Sales.Where(x => x.ClientId == client.Id).ToArray();
|
||||
_clientStorageContract.DelElement(client.Id);
|
||||
var element = GetClientFromDatabase(client.Id);
|
||||
var salesAfterDelete = MagicCarpetDbContext.Sales.Where(x => x.ClientId == client.Id).ToArray();
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(element, Is.Null);
|
||||
Assert.That(salesBeforeDelete, Has.Length.EqualTo(2));
|
||||
Assert.That(salesAfterDelete, Is.Empty);
|
||||
Assert.That(MagicCarpetDbContext.Sales.Count(), Is.EqualTo(2));
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_DelElement_WhenNoRecordWithThisId_Test()
|
||||
{
|
||||
Assert.That(() => _clientStorageContract.DelElement(Guid.NewGuid().ToString()), Throws.TypeOf<ElementNotFoundException>());
|
||||
}
|
||||
|
||||
private Client InsertClientToDatabaseAndReturn(string id, string fio = "test", string phoneNumber = "+7-777-777-77-77", double discountSize = 10)
|
||||
{
|
||||
var client = new Client() { Id = id, FIO = fio, PhoneNumber = phoneNumber, DiscountSize = discountSize };
|
||||
MagicCarpetDbContext.Clients.Add(client);
|
||||
MagicCarpetDbContext.SaveChanges();
|
||||
return client;
|
||||
}
|
||||
|
||||
private static void AssertElement(ClientDataModel? actual, Client expected)
|
||||
{
|
||||
Assert.That(actual, Is.Not.Null);
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(actual.Id, Is.EqualTo(expected.Id));
|
||||
Assert.That(actual.FIO, Is.EqualTo(expected.FIO));
|
||||
Assert.That(actual.PhoneNumber, Is.EqualTo(expected.PhoneNumber));
|
||||
Assert.That(actual.DiscountSize, Is.EqualTo(expected.DiscountSize));
|
||||
});
|
||||
}
|
||||
|
||||
private static ClientDataModel CreateModel(string id, string fio = "test", string phoneNumber = "+7-777-777-77-77", double discountSize = 10)
|
||||
=> new(id, fio, phoneNumber, discountSize);
|
||||
|
||||
private Client? GetClientFromDatabase(string id) => MagicCarpetDbContext.Clients.FirstOrDefault(x => x.Id == id);
|
||||
|
||||
private static void AssertElement(Client? actual, ClientDataModel expected)
|
||||
{
|
||||
Assert.That(actual, Is.Not.Null);
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(actual.Id, Is.EqualTo(expected.Id));
|
||||
Assert.That(actual.FIO, Is.EqualTo(expected.FIO));
|
||||
Assert.That(actual.PhoneNumber, Is.EqualTo(expected.PhoneNumber));
|
||||
Assert.That(actual.DiscountSize, Is.EqualTo(expected.DiscountSize));
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,233 @@
|
||||
using MagicCarpetContracts.DataModels;
|
||||
using MagicCarpetContracts.Exceptions;
|
||||
using MagicCarpetDatabase.Implementations;
|
||||
using MagicCarpetDatabase.Models;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MagicCarpetTests.StoragesContractsTests;
|
||||
|
||||
[TestFixture]
|
||||
class EmployeeStorageContractTests : BaseStorageContractTest
|
||||
{
|
||||
private EmployeeStorageContract _employeeStorageContract;
|
||||
|
||||
[SetUp]
|
||||
public void SetUp()
|
||||
{
|
||||
_employeeStorageContract = new EmployeeStorageContract(MagicCarpetDbContext);
|
||||
}
|
||||
|
||||
[TearDown]
|
||||
public void TearDown()
|
||||
{
|
||||
MagicCarpetDbContext.Database.ExecuteSqlRaw("TRUNCATE \"Employees\"CASCADE; ");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_GetList_WhenHaveRecords_Test()
|
||||
{
|
||||
var employee = InsertEmployeeToDatabaseAndReturn(Guid.NewGuid().ToString(), "fio 1", "abc@gmail.com");
|
||||
InsertEmployeeToDatabaseAndReturn(Guid.NewGuid().ToString(), "fio 2", "abc@gmail.com");
|
||||
InsertEmployeeToDatabaseAndReturn(Guid.NewGuid().ToString(), "fio 3", "abc@gmail.com");
|
||||
var list = _employeeStorageContract.GetList();
|
||||
Assert.That(list, Is.Not.Null);
|
||||
Assert.That(list, Has.Count.EqualTo(3));
|
||||
AssertElement(list.First(), employee);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_GetList_WhenNoRecords_Test()
|
||||
{
|
||||
var list = _employeeStorageContract.GetList();
|
||||
Assert.That(list, Is.Not.Null);
|
||||
Assert.That(list, Is.Empty);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_GetList_ByPostId_Test()
|
||||
{
|
||||
var postId = Guid.NewGuid().ToString();
|
||||
InsertEmployeeToDatabaseAndReturn(Guid.NewGuid().ToString(), "fio 1", "abc@gmail.com", postId);
|
||||
InsertEmployeeToDatabaseAndReturn(Guid.NewGuid().ToString(), "fio 2", "abc@gmail.com", postId);
|
||||
InsertEmployeeToDatabaseAndReturn(Guid.NewGuid().ToString(), "fio 3", "abc@gmail.com");
|
||||
var list = _employeeStorageContract.GetList(postId: postId);
|
||||
Assert.That(list, Is.Not.Null);
|
||||
Assert.That(list, Has.Count.EqualTo(2));
|
||||
Assert.That(list.All(x => x.PostId == postId));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_GetList_ByBirthDate_Test()
|
||||
{
|
||||
InsertEmployeeToDatabaseAndReturn(Guid.NewGuid().ToString(), "fio 1", "abc@gmail.com", birthDate: DateTime.UtcNow.AddYears(-25));
|
||||
InsertEmployeeToDatabaseAndReturn(Guid.NewGuid().ToString(), "fio 2", "abc@gmail.com", birthDate: DateTime.UtcNow.AddYears(-21));
|
||||
InsertEmployeeToDatabaseAndReturn(Guid.NewGuid().ToString(), "fio 3", "abc@gmail.com", birthDate: DateTime.UtcNow.AddYears(-20));
|
||||
InsertEmployeeToDatabaseAndReturn(Guid.NewGuid().ToString(), "fio 4", "abc@gmail.com", birthDate: DateTime.UtcNow.AddYears(-19));
|
||||
var list = _employeeStorageContract.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()
|
||||
{
|
||||
InsertEmployeeToDatabaseAndReturn(Guid.NewGuid().ToString(), "fio 1", "abc@gmail.com", employmentDate: DateTime.UtcNow.AddDays(-2));
|
||||
InsertEmployeeToDatabaseAndReturn(Guid.NewGuid().ToString(), "fio 2", "abc@gmail.com", employmentDate: DateTime.UtcNow.AddDays(-1));
|
||||
InsertEmployeeToDatabaseAndReturn(Guid.NewGuid().ToString(), "fio 3", "abc@gmail.com", employmentDate: DateTime.UtcNow.AddDays(1));
|
||||
InsertEmployeeToDatabaseAndReturn(Guid.NewGuid().ToString(), "fio 4", "abc@gmail.com", employmentDate: DateTime.UtcNow.AddDays(2));
|
||||
var list = _employeeStorageContract.GetList(fromEmploymentDate: DateTime.UtcNow.AddDays(-1).AddMinutes(-1), toEmploymentDate: DateTime.UtcNow.AddDays(1).AddMinutes(1));
|
||||
Assert.That(list, Is.Not.Null);
|
||||
Assert.That(list, Has.Count.EqualTo(2));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_GetList_ByAllParameters_Test()
|
||||
{
|
||||
var postId = Guid.NewGuid().ToString();
|
||||
InsertEmployeeToDatabaseAndReturn(Guid.NewGuid().ToString(), "fio 1", "abc@gmail.com", postId, birthDate: DateTime.UtcNow.AddYears(-25), employmentDate: DateTime.UtcNow.AddDays(-2));
|
||||
InsertEmployeeToDatabaseAndReturn(Guid.NewGuid().ToString(), "fio 2", "abc@gmail.com", postId, birthDate: DateTime.UtcNow.AddYears(-22), employmentDate: DateTime.UtcNow.AddDays(-1));
|
||||
InsertEmployeeToDatabaseAndReturn(Guid.NewGuid().ToString(), "fio 3", "abc@gmail.com", postId, birthDate: DateTime.UtcNow.AddYears(-21), employmentDate: DateTime.UtcNow.AddDays(-1));
|
||||
InsertEmployeeToDatabaseAndReturn(Guid.NewGuid().ToString(), "fio 4", "abc@gmail.com", birthDate: DateTime.UtcNow.AddYears(-20), employmentDate: DateTime.UtcNow.AddDays(1));
|
||||
var list = _employeeStorageContract.GetList(postId: postId, fromBirthDate: DateTime.UtcNow.AddYears(-21).AddMinutes(-1), toBirthDate: DateTime.UtcNow.AddYears(-20).AddMinutes(1),
|
||||
fromEmploymentDate: DateTime.UtcNow.AddDays(-1).AddMinutes(-1), toEmploymentDate: DateTime.UtcNow.AddDays(1).AddMinutes(1));
|
||||
Assert.That(list, Is.Not.Null);
|
||||
Assert.That(list, Has.Count.EqualTo(1));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_GetElementById_WhenHaveRecord_Test()
|
||||
{
|
||||
var employee = InsertEmployeeToDatabaseAndReturn(Guid.NewGuid().ToString());
|
||||
AssertElement(_employeeStorageContract.GetElementById(employee.Id), employee);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_GetElementById_WhenNoRecord_Test()
|
||||
{
|
||||
Assert.That(() => _employeeStorageContract.GetElementById(Guid.NewGuid().ToString()), Is.Null);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_GetElementByFIO_WhenHaveRecord_Test()
|
||||
{
|
||||
var employee = InsertEmployeeToDatabaseAndReturn(Guid.NewGuid().ToString());
|
||||
AssertElement(_employeeStorageContract.GetElementByFIO(employee.FIO), employee);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_GetElementByFIO_WhenNoRecord_Test()
|
||||
{
|
||||
Assert.That(() => _employeeStorageContract.GetElementByFIO("New Fio"), Is.Null);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_AddElement_Test()
|
||||
{
|
||||
var employee = CreateModel(Guid.NewGuid().ToString());
|
||||
_employeeStorageContract.AddElement(employee);
|
||||
AssertElement(GetEmployeeFromDatabase(employee.Id), employee);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_AddElement_WhenHaveRecordWithSameId_Test()
|
||||
{
|
||||
var employee = CreateModel(Guid.NewGuid().ToString());
|
||||
InsertEmployeeToDatabaseAndReturn(employee.Id);
|
||||
Assert.That(() => _employeeStorageContract.AddElement(employee), Throws.TypeOf<ElementExistsException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_UpdElement_Test()
|
||||
{
|
||||
var employee = CreateModel(Guid.NewGuid().ToString(), "New Fio");
|
||||
InsertEmployeeToDatabaseAndReturn(employee.Id);
|
||||
_employeeStorageContract.UpdElement(employee);
|
||||
AssertElement(GetEmployeeFromDatabase(employee.Id), employee);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_UpdElement_WhenNoRecordWithThisId_Test()
|
||||
{
|
||||
Assert.That(() => _employeeStorageContract.UpdElement(CreateModel(Guid.NewGuid().ToString())), Throws.TypeOf<ElementNotFoundException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_UpdElement_WhenNoRecordWasDeleted_Test()
|
||||
{
|
||||
var employee = CreateModel(Guid.NewGuid().ToString());
|
||||
InsertEmployeeToDatabaseAndReturn(employee.Id, isDeleted: true);
|
||||
Assert.That(() => _employeeStorageContract.UpdElement(employee), Throws.TypeOf<ElementNotFoundException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_DelElement_Test()
|
||||
{
|
||||
var employee = InsertEmployeeToDatabaseAndReturn(Guid.NewGuid().ToString());
|
||||
_employeeStorageContract.DelElement(employee.Id);
|
||||
var element = GetEmployeeFromDatabase(employee.Id);
|
||||
Assert.That(element, Is.Not.Null);
|
||||
Assert.That(element.IsDeleted);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_DelElement_WhenNoRecordWithThisId_Test()
|
||||
{
|
||||
Assert.That(() => _employeeStorageContract.DelElement(Guid.NewGuid().ToString()), Throws.TypeOf<ElementNotFoundException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_DelElement_WhenNoRecordWasDeleted_Test()
|
||||
{
|
||||
var employee = CreateModel(Guid.NewGuid().ToString());
|
||||
InsertEmployeeToDatabaseAndReturn(employee.Id, isDeleted: true);
|
||||
Assert.That(() => _employeeStorageContract.DelElement(employee.Id), Throws.TypeOf<ElementNotFoundException>());
|
||||
}
|
||||
|
||||
private Employee InsertEmployeeToDatabaseAndReturn(string id, string fio = "test", string email = "abc@mail.ru",string ? postId = null, DateTime? birthDate = null, DateTime? employmentDate = null, bool isDeleted = false)
|
||||
{
|
||||
var employee = new Employee() { Id = id, FIO = fio, Email = email, PostId = postId ?? Guid.NewGuid().ToString(), BirthDate = birthDate ?? DateTime.UtcNow.AddYears(-20), EmploymentDate = employmentDate ?? DateTime.UtcNow, IsDeleted = isDeleted };
|
||||
MagicCarpetDbContext.Employees.Add(employee);
|
||||
MagicCarpetDbContext.SaveChanges();
|
||||
return employee;
|
||||
}
|
||||
|
||||
private static void AssertElement(EmployeeDataModel? actual, Employee expected)
|
||||
{
|
||||
Assert.That(actual, Is.Not.Null);
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(actual.Id, Is.EqualTo(expected.Id));
|
||||
Assert.That(actual.PostId, Is.EqualTo(expected.PostId));
|
||||
Assert.That(actual.FIO, Is.EqualTo(expected.FIO));
|
||||
Assert.That(actual.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 EmployeeDataModel CreateModel(string id, string fio = "fio", string email = "abc@mail.ru", string? postId = null, DateTime? birthDate = null, DateTime? employmentDate = null, bool isDeleted = false) =>
|
||||
new(id, fio, email, postId ?? Guid.NewGuid().ToString(), birthDate ?? DateTime.UtcNow.AddYears(-20), employmentDate ?? DateTime.UtcNow, isDeleted);
|
||||
|
||||
private Employee? GetEmployeeFromDatabase(string id) => MagicCarpetDbContext.Employees.FirstOrDefault(x => x.Id == id);
|
||||
|
||||
private static void AssertElement(Employee? actual, EmployeeDataModel expected)
|
||||
{
|
||||
Assert.That(actual, Is.Not.Null);
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(actual.Id, Is.EqualTo(expected.Id));
|
||||
Assert.That(actual.PostId, Is.EqualTo(expected.PostId));
|
||||
Assert.That(actual.FIO, Is.EqualTo(expected.FIO));
|
||||
Assert.That(actual.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,153 @@
|
||||
using MagicCarpetContracts.DataModels;
|
||||
using MagicCarpetDatabase.Implementations;
|
||||
using MagicCarpetDatabase.Models;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MagicCarpetTests.StoragesContractsTests;
|
||||
|
||||
[TestFixture]
|
||||
internal class SalaryStorageContractTests : BaseStorageContractTest
|
||||
{
|
||||
private SalaryStorageContract _salaryStorageContract;
|
||||
private Employee _employee;
|
||||
|
||||
[SetUp]
|
||||
public void SetUp()
|
||||
{
|
||||
_salaryStorageContract = new SalaryStorageContract(MagicCarpetDbContext);
|
||||
_employee = InsertEmployeeToDatabaseAndReturn();
|
||||
}
|
||||
|
||||
[TearDown]
|
||||
public void TearDown()
|
||||
{
|
||||
MagicCarpetDbContext.Database.ExecuteSqlRaw("TRUNCATE \"Salaries\" CASCADE;");
|
||||
MagicCarpetDbContext.Database.ExecuteSqlRaw("TRUNCATE \"Employees\" CASCADE;");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_GetList_WhenHaveRecords_Test()
|
||||
{
|
||||
var salary = InsertSalaryToDatabaseAndReturn(_employee.Id, employeeSalary: 100);
|
||||
InsertSalaryToDatabaseAndReturn(_employee.Id);
|
||||
InsertSalaryToDatabaseAndReturn(_employee.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(_employee.Id, salaryDate: DateTime.UtcNow.AddDays(-2));
|
||||
InsertSalaryToDatabaseAndReturn(_employee.Id, salaryDate: DateTime.UtcNow.AddDays(-1).AddMinutes(-5));
|
||||
InsertSalaryToDatabaseAndReturn(_employee.Id, salaryDate: DateTime.UtcNow.AddDays(-1).AddMinutes(5));
|
||||
InsertSalaryToDatabaseAndReturn(_employee.Id, salaryDate: DateTime.UtcNow.AddDays(1).AddMinutes(-5));
|
||||
InsertSalaryToDatabaseAndReturn(_employee.Id, salaryDate: DateTime.UtcNow.AddDays(1).AddMinutes(5));
|
||||
InsertSalaryToDatabaseAndReturn(_employee.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_ByEmployee_Test()
|
||||
{
|
||||
var employee = InsertEmployeeToDatabaseAndReturn("name 2");
|
||||
InsertSalaryToDatabaseAndReturn(_employee.Id);
|
||||
InsertSalaryToDatabaseAndReturn(_employee.Id);
|
||||
InsertSalaryToDatabaseAndReturn(employee.Id);
|
||||
var list = _salaryStorageContract.GetList(DateTime.UtcNow.AddDays(-1), DateTime.UtcNow.AddDays(1), _employee.Id);
|
||||
Assert.That(list, Is.Not.Null);
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(list, Has.Count.EqualTo(2));
|
||||
Assert.That(list.All(x => x.EmployeeId == _employee.Id));
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_GetList_ByEmployeeOnlyInDatePeriod_Test()
|
||||
{
|
||||
var employee = InsertEmployeeToDatabaseAndReturn("name 2");
|
||||
InsertSalaryToDatabaseAndReturn(_employee.Id, salaryDate: DateTime.UtcNow.AddDays(-2));
|
||||
InsertSalaryToDatabaseAndReturn(_employee.Id, salaryDate: DateTime.UtcNow.AddDays(-1).AddMinutes(5));
|
||||
InsertSalaryToDatabaseAndReturn(employee.Id, salaryDate: DateTime.UtcNow.AddDays(-1).AddMinutes(5));
|
||||
InsertSalaryToDatabaseAndReturn(_employee.Id, salaryDate: DateTime.UtcNow.AddDays(1).AddMinutes(-5));
|
||||
InsertSalaryToDatabaseAndReturn(employee.Id, salaryDate: DateTime.UtcNow.AddDays(1).AddMinutes(-5));
|
||||
InsertSalaryToDatabaseAndReturn(_employee.Id, salaryDate: DateTime.UtcNow.AddDays(-2));
|
||||
var list = _salaryStorageContract.GetList(DateTime.UtcNow.AddDays(-1), DateTime.UtcNow.AddDays(1), _employee.Id);
|
||||
Assert.That(list, Is.Not.Null);
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(list, Has.Count.EqualTo(2));
|
||||
Assert.That(list.All(x => x.EmployeeId == _employee.Id));
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_AddElement_Test()
|
||||
{
|
||||
var salary = CreateModel(_employee.Id);
|
||||
_salaryStorageContract.AddElement(salary);
|
||||
AssertElement(GetSalaryFromDatabaseByEmployeeId(_employee.Id), salary);
|
||||
}
|
||||
|
||||
private Employee InsertEmployeeToDatabaseAndReturn(string employeeFIO = "fio", string employeeEmail = "abc@mail.ru")
|
||||
{
|
||||
var employee = new Employee() { Id = Guid.NewGuid().ToString(), PostId = Guid.NewGuid().ToString(), FIO = employeeFIO, Email = employeeEmail, IsDeleted = false };
|
||||
MagicCarpetDbContext.Employees.Add(employee);
|
||||
MagicCarpetDbContext.SaveChanges();
|
||||
return employee;
|
||||
}
|
||||
|
||||
private Salary InsertSalaryToDatabaseAndReturn(string employeeId, double employeeSalary = 1, DateTime? salaryDate = null)
|
||||
{
|
||||
var salary = new Salary() { EmployeeId = employeeId, EmployeeSalary = employeeSalary, SalaryDate = salaryDate ?? DateTime.UtcNow };
|
||||
MagicCarpetDbContext.Salaries.Add(salary);
|
||||
MagicCarpetDbContext.SaveChanges();
|
||||
return salary;
|
||||
}
|
||||
|
||||
private static void AssertElement(SalaryDataModel? actual, Salary expected)
|
||||
{
|
||||
Assert.That(actual, Is.Not.Null);
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(actual.EmployeeId, Is.EqualTo(expected.EmployeeId));
|
||||
Assert.That(actual.Salary, Is.EqualTo(expected.EmployeeSalary));
|
||||
});
|
||||
}
|
||||
|
||||
private static SalaryDataModel CreateModel(string employeeId, double employeeSalary = 1, DateTime? salaryDate = null)
|
||||
=> new(employeeId, salaryDate ?? DateTime.UtcNow, employeeSalary);
|
||||
|
||||
private Salary? GetSalaryFromDatabaseByEmployeeId(string id) => MagicCarpetDbContext.Salaries.FirstOrDefault(x => x.EmployeeId == id);
|
||||
|
||||
private static void AssertElement(Salary? actual, SalaryDataModel expected)
|
||||
{
|
||||
Assert.That(actual, Is.Not.Null);
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(actual.EmployeeId, Is.EqualTo(expected.EmployeeId));
|
||||
Assert.That(actual.EmployeeSalary, Is.EqualTo(expected.Salary));
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,306 @@
|
||||
using MagicCarpetContracts.DataModels;
|
||||
using MagicCarpetContracts.Enums;
|
||||
using MagicCarpetContracts.Exceptions;
|
||||
using MagicCarpetDatabase.Implementations;
|
||||
using MagicCarpetDatabase.Models;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using static NUnit.Framework.Internal.OSPlatform;
|
||||
|
||||
namespace MagicCarpetTests.StoragesContractsTests;
|
||||
|
||||
[TestFixture]
|
||||
internal class SaleStorageContractTests : BaseStorageContractTest
|
||||
{
|
||||
private SaleStorageContract _saleStorageContract;
|
||||
private Client _client;
|
||||
private Employee _employee;
|
||||
private Tour _tour;
|
||||
|
||||
[SetUp]
|
||||
public void SetUp()
|
||||
{
|
||||
_saleStorageContract = new SaleStorageContract(MagicCarpetDbContext);
|
||||
_client = InsertClientToDatabaseAndReturn();
|
||||
_employee = InsertEmployeeToDatabaseAndReturn();
|
||||
_tour = InsertTourToDatabaseAndReturn();
|
||||
}
|
||||
|
||||
[TearDown]
|
||||
public void TearDown()
|
||||
{
|
||||
MagicCarpetDbContext.Database.ExecuteSqlRaw("TRUNCATE \"Sales\" CASCADE;");
|
||||
MagicCarpetDbContext.Database.ExecuteSqlRaw("TRUNCATE \"Clients\" CASCADE;");
|
||||
MagicCarpetDbContext.Database.ExecuteSqlRaw("TRUNCATE \"Employees\" CASCADE;");
|
||||
MagicCarpetDbContext.Database.ExecuteSqlRaw("TRUNCATE \"Tours\" CASCADE;");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_GetList_WhenHaveRecords_Test()
|
||||
{
|
||||
var sale = InsertSaleToDatabaseAndReturn(_employee.Id, _client.Id, tours: [(_tour.Id, 1)]);
|
||||
InsertSaleToDatabaseAndReturn(_employee.Id, _client.Id, tours: [(_tour.Id, 5)]);
|
||||
InsertSaleToDatabaseAndReturn(_employee.Id, null, tours: [(_tour.Id, 10)]);
|
||||
var list = _saleStorageContract.GetList();
|
||||
Assert.That(list, Is.Not.Null);
|
||||
Assert.That(list, Has.Count.EqualTo(3));
|
||||
AssertElement(list.First(x => x.Id == sale.Id), sale);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_GetList_WhenNoRecords_Test()
|
||||
{
|
||||
var list = _saleStorageContract.GetList();
|
||||
Assert.That(list, Is.Not.Null);
|
||||
Assert.That(list, Is.Empty);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_GetList_ByPeriod_Test()
|
||||
{
|
||||
InsertSaleToDatabaseAndReturn(_employee.Id, _client.Id, saleDate: DateTime.UtcNow.AddDays(-1).AddMinutes(-3), tours: [(_tour.Id, 1)]);
|
||||
InsertSaleToDatabaseAndReturn(_employee.Id, _client.Id, saleDate: DateTime.UtcNow.AddDays(-1).AddMinutes(3), tours: [(_tour.Id, 1)]);
|
||||
InsertSaleToDatabaseAndReturn(_employee.Id, null, saleDate: DateTime.UtcNow.AddDays(1).AddMinutes(-3), tours: [(_tour.Id, 1)]);
|
||||
InsertSaleToDatabaseAndReturn(_employee.Id, null, saleDate: DateTime.UtcNow.AddDays(1).AddMinutes(3), tours: [(_tour.Id, 1)]);
|
||||
var list = _saleStorageContract.GetList(startDate: DateTime.UtcNow.AddDays(-1), endDate: DateTime.UtcNow.AddDays(1));
|
||||
Assert.That(list, Is.Not.Null);
|
||||
Assert.That(list, Has.Count.EqualTo(2));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_GetList_ByEmployeeId_Test()
|
||||
{
|
||||
var employee = InsertEmployeeToDatabaseAndReturn("Other employee");
|
||||
InsertSaleToDatabaseAndReturn(_employee.Id, _client.Id, tours: [(_tour.Id, 1)]);
|
||||
InsertSaleToDatabaseAndReturn(_employee.Id, _client.Id, tours: [(_tour.Id, 1)]);
|
||||
InsertSaleToDatabaseAndReturn(employee.Id, null, tours: [(_tour.Id, 1)]);
|
||||
var list = _saleStorageContract.GetList(employeeId: _employee.Id);
|
||||
Assert.That(list, Is.Not.Null);
|
||||
Assert.That(list, Has.Count.EqualTo(2));
|
||||
Assert.That(list.All(x => x.EmployeeId == _employee.Id));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_GetList_ByClientId_Test()
|
||||
{
|
||||
var client = InsertClientToDatabaseAndReturn("Other fio", "+8-888-888-88-88");
|
||||
InsertSaleToDatabaseAndReturn(_employee.Id, _client.Id, tours: [(_tour.Id, 1)]);
|
||||
InsertSaleToDatabaseAndReturn(_employee.Id, _client.Id, tours: [(_tour.Id, 1)]);
|
||||
InsertSaleToDatabaseAndReturn(_employee.Id, client.Id, tours: [(_tour.Id, 1)]);
|
||||
InsertSaleToDatabaseAndReturn(_employee.Id, null, tours: [(_tour.Id, 1)]);
|
||||
var list = _saleStorageContract.GetList(clientId: _client.Id);
|
||||
Assert.That(list, Is.Not.Null);
|
||||
Assert.That(list, Has.Count.EqualTo(2));
|
||||
Assert.That(list.All(x => x.ClientId == _client.Id));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_GetList_ByTourId_Test()
|
||||
{
|
||||
var tour = InsertTourToDatabaseAndReturn("Other name");
|
||||
InsertSaleToDatabaseAndReturn(_employee.Id, _client.Id, tours: [(_tour.Id, 5)]);
|
||||
InsertSaleToDatabaseAndReturn(_employee.Id, _client.Id, tours: [(_tour.Id, 1), (tour.Id, 4)]);
|
||||
InsertSaleToDatabaseAndReturn(_employee.Id, null, tours: [(tour.Id, 1)]);
|
||||
InsertSaleToDatabaseAndReturn(_employee.Id, null, tours: [(tour.Id, 1), (_tour.Id, 1)]);
|
||||
var list = _saleStorageContract.GetList(tourId: _tour.Id);
|
||||
Assert.That(list, Is.Not.Null);
|
||||
Assert.That(list, Has.Count.EqualTo(3));
|
||||
Assert.That(list.All(x => x.Tours.Any(y => y.TourId == _tour.Id)));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_GetList_ByAllParameters_Test()
|
||||
{
|
||||
var employee = InsertEmployeeToDatabaseAndReturn("Other employee");
|
||||
var client = InsertClientToDatabaseAndReturn("Other fio", "+8-888-888-88-88");
|
||||
var tour = InsertTourToDatabaseAndReturn("Other name");
|
||||
InsertSaleToDatabaseAndReturn(_employee.Id, _client.Id, saleDate: DateTime.UtcNow.AddDays(-1).AddMinutes(-3), tours: [(_tour.Id, 1)]);
|
||||
InsertSaleToDatabaseAndReturn(employee.Id, null, saleDate: DateTime.UtcNow.AddDays(-1).AddMinutes(3), tours: [(_tour.Id, 1)]);
|
||||
InsertSaleToDatabaseAndReturn(employee.Id, _client.Id, saleDate: DateTime.UtcNow.AddDays(-1).AddMinutes(3), tours: [(_tour.Id, 1)]);
|
||||
InsertSaleToDatabaseAndReturn(employee.Id, _client.Id, saleDate: DateTime.UtcNow.AddDays(-1).AddMinutes(3), tours: [(tour.Id, 1)]);
|
||||
InsertSaleToDatabaseAndReturn(_employee.Id, client.Id, saleDate: DateTime.UtcNow.AddDays(1).AddMinutes(-3), tours: [(_tour.Id, 1)]);
|
||||
InsertSaleToDatabaseAndReturn(_employee.Id, _client.Id, saleDate: DateTime.UtcNow.AddDays(1).AddMinutes(-3), tours: [(tour.Id, 1)]);
|
||||
InsertSaleToDatabaseAndReturn(employee.Id, null, saleDate: DateTime.UtcNow.AddDays(1).AddMinutes(-3), tours: [(_tour.Id, 1)]);
|
||||
var list = _saleStorageContract.GetList(startDate: DateTime.UtcNow.AddDays(-1), endDate: DateTime.UtcNow.AddDays(1), employeeId: _employee.Id, clientId: _client.Id, tourId: tour.Id);
|
||||
Assert.That(list, Is.Not.Null);
|
||||
Assert.That(list, Has.Count.EqualTo(1));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_GetElementById_WhenHaveRecord_Test()
|
||||
{
|
||||
var sale = InsertSaleToDatabaseAndReturn(_employee.Id, _client.Id, tours: [(_tour.Id, 1)]);
|
||||
AssertElement(_saleStorageContract.GetElementById(sale.Id), sale);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_GetElementById_WhenNoRecord_Test()
|
||||
{
|
||||
InsertSaleToDatabaseAndReturn(_employee.Id, _client.Id, tours: [(_tour.Id, 1)]);
|
||||
Assert.That(() => _saleStorageContract.GetElementById(Guid.NewGuid().ToString()), Is.Null);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_GetElementById_WhenRecordHasCanceled_Test()
|
||||
{
|
||||
var sale = InsertSaleToDatabaseAndReturn(_employee.Id, _client.Id, tours: [(_tour.Id, 1)], isCancel: true);
|
||||
AssertElement(_saleStorageContract.GetElementById(sale.Id), sale);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_AddElement_Test()
|
||||
{
|
||||
var sale = CreateModel(Guid.NewGuid().ToString(), _employee.Id, _client.Id, 1, DiscountType.RegularCustomer, 1, false, [_tour.Id]);
|
||||
_saleStorageContract.AddElement(sale);
|
||||
AssertElement(GetSaleFromDatabaseById(sale.Id), sale);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_AddElement_WhenIsDeletedIsTrue_Test()
|
||||
{
|
||||
var sale = CreateModel(Guid.NewGuid().ToString(), _employee.Id, _client.Id, 1, DiscountType.RegularCustomer, 1, true, [_tour.Id]);
|
||||
Assert.That(() => _saleStorageContract.AddElement(sale), Throws.Nothing);
|
||||
AssertElement(GetSaleFromDatabaseById(sale.Id), CreateModel(sale.Id, _employee.Id, _client.Id, 1, DiscountType.RegularCustomer, 1, false, [_tour.Id]));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_DelElement_Test()
|
||||
{
|
||||
var sale = InsertSaleToDatabaseAndReturn(_employee.Id, _client.Id, tours: [(_tour.Id, 1)], isCancel: false);
|
||||
_saleStorageContract.DelElement(sale.Id);
|
||||
var element = GetSaleFromDatabaseById(sale.Id);
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(element, Is.Not.Null);
|
||||
Assert.That(element!.IsCancel);
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_DelElement_WhenNoRecordWithThisId_Test()
|
||||
{
|
||||
Assert.That(() => _saleStorageContract.DelElement(Guid.NewGuid().ToString()), Throws.TypeOf<ElementNotFoundException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_DelElement_WhenRecordWasCanceled_Test()
|
||||
{
|
||||
var sale = InsertSaleToDatabaseAndReturn(_employee.Id, _client.Id, tours: [(_tour.Id, 1)], isCancel: true);
|
||||
Assert.That(() => _saleStorageContract.DelElement(sale.Id), Throws.TypeOf<ElementDeletedException>());
|
||||
}
|
||||
|
||||
private Client InsertClientToDatabaseAndReturn(string fio = "test", string phoneNumber = "+7-777-777-77-77")
|
||||
{
|
||||
var client = new Client() { Id = Guid.NewGuid().ToString(), FIO = fio, PhoneNumber = phoneNumber, DiscountSize = 10 };
|
||||
MagicCarpetDbContext.Clients.Add(client);
|
||||
MagicCarpetDbContext.SaveChanges();
|
||||
return client;
|
||||
}
|
||||
|
||||
private Employee InsertEmployeeToDatabaseAndReturn(string fio = "test", string employeeEmail = "abc@gmail.com")
|
||||
{
|
||||
var employee = new Employee() { Id = Guid.NewGuid().ToString(), FIO = fio, Email = employeeEmail, PostId = Guid.NewGuid().ToString() };
|
||||
MagicCarpetDbContext.Employees.Add(employee);
|
||||
MagicCarpetDbContext.SaveChanges();
|
||||
return employee;
|
||||
}
|
||||
|
||||
private Tour InsertTourToDatabaseAndReturn(string tourName = "test", TourType tourType = TourType.Sightseeing, double price = 1)
|
||||
{
|
||||
var tour = new Tour() { Id = Guid.NewGuid().ToString(), TourName = tourName, TourType = tourType, Price = price };
|
||||
MagicCarpetDbContext.Tours.Add(tour);
|
||||
MagicCarpetDbContext.SaveChanges();
|
||||
return tour;
|
||||
}
|
||||
|
||||
private Sale InsertSaleToDatabaseAndReturn(string employeeId, string? clientId, DateTime? saleDate = null, double sum = 1, DiscountType discountType = DiscountType.OnSale, double discount = 0, bool isCancel = false, List<(string, int)>? tours = null)
|
||||
{
|
||||
var sale = new Sale() { EmployeeId = employeeId, ClientId = clientId, SaleDate = saleDate ?? DateTime.UtcNow, Sum = sum, DiscountType = discountType, Discount = discount, IsCancel = isCancel, SaleTours = [] };
|
||||
if (tours is not null)
|
||||
{
|
||||
foreach (var elem in tours)
|
||||
{
|
||||
sale.SaleTours.Add(new SaleTour { TourId = elem.Item1, SaleId = sale.Id, Count = elem.Item2 });
|
||||
}
|
||||
}
|
||||
MagicCarpetDbContext.Sales.Add(sale);
|
||||
MagicCarpetDbContext.SaveChanges();
|
||||
return sale;
|
||||
}
|
||||
|
||||
private static void AssertElement(SaleDataModel? actual, Sale expected)
|
||||
{
|
||||
Assert.That(actual, Is.Not.Null);
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(actual.Id, Is.EqualTo(expected.Id));
|
||||
Assert.That(actual.EmployeeId, Is.EqualTo(expected.EmployeeId));
|
||||
Assert.That(actual.ClientId, Is.EqualTo(expected.ClientId));
|
||||
Assert.That(actual.DiscountType, Is.EqualTo(expected.DiscountType));
|
||||
Assert.That(actual.Discount, Is.EqualTo(expected.Discount));
|
||||
Assert.That(actual.IsCancel, Is.EqualTo(expected.IsCancel));
|
||||
});
|
||||
|
||||
if (expected.SaleTours is not null)
|
||||
{
|
||||
Assert.That(actual.Tours, Is.Not.Null);
|
||||
Assert.That(actual.Tours, Has.Count.EqualTo(expected.SaleTours.Count));
|
||||
for (int i = 0; i < actual.Tours.Count; ++i)
|
||||
{
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(actual.Tours[i].TourId, Is.EqualTo(expected.SaleTours[i].TourId));
|
||||
Assert.That(actual.Tours[i].Count, Is.EqualTo(expected.SaleTours[i].Count));
|
||||
});
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Assert.That(actual.Tours, Is.Null);
|
||||
}
|
||||
}
|
||||
|
||||
private static SaleDataModel CreateModel(string id, string employeeId, string? clientId, double sum, DiscountType discountType, double discount, bool isCancel, List<string> tourIds)
|
||||
{
|
||||
var tours = tourIds.Select(x => new SaleTourDataModel(id, x, 1)).ToList();
|
||||
return new(id, employeeId, clientId, sum, discountType, discount, isCancel, tours);
|
||||
}
|
||||
|
||||
private Sale? GetSaleFromDatabaseById(string id) => MagicCarpetDbContext.Sales.Include(x => x.SaleTours).FirstOrDefault(x => x.Id == id);
|
||||
|
||||
private static void AssertElement(Sale? actual, SaleDataModel expected)
|
||||
{
|
||||
Assert.That(actual, Is.Not.Null);
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(actual.Id, Is.EqualTo(expected.Id));
|
||||
Assert.That(actual.EmployeeId, Is.EqualTo(expected.EmployeeId));
|
||||
Assert.That(actual.ClientId, Is.EqualTo(expected.ClientId));
|
||||
Assert.That(actual.DiscountType, Is.EqualTo(expected.DiscountType));
|
||||
Assert.That(actual.Discount, Is.EqualTo(expected.Discount));
|
||||
Assert.That(actual.IsCancel, Is.EqualTo(expected.IsCancel));
|
||||
});
|
||||
|
||||
if (expected.Tours is not null)
|
||||
{
|
||||
Assert.That(actual.SaleTours, Is.Not.Null);
|
||||
Assert.That(actual.SaleTours, Has.Count.EqualTo(expected.Tours.Count));
|
||||
for (int i = 0; i < actual.SaleTours.Count; ++i)
|
||||
{
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(actual.SaleTours[i].TourId, Is.EqualTo(expected.Tours[i].TourId));
|
||||
Assert.That(actual.SaleTours[i].Count, Is.EqualTo(expected.Tours[i].Count));
|
||||
});
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Assert.That(actual.SaleTours, Is.Null);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,191 @@
|
||||
using MagicCarpetContracts.DataModels;
|
||||
using MagicCarpetContracts.Enums;
|
||||
using MagicCarpetContracts.Exceptions;
|
||||
using MagicCarpetDatabase.Implementations;
|
||||
using MagicCarpetDatabase.Models;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MagicCarpetTests.StoragesContractsTests;
|
||||
|
||||
[TestFixture]
|
||||
internal class SuppliesStorageContractTests : BaseStorageContractTest
|
||||
{
|
||||
private SuppliesStorageContract _suppliesStorageContract;
|
||||
private Tour _tour;
|
||||
|
||||
[SetUp]
|
||||
public void SetUp()
|
||||
{
|
||||
_suppliesStorageContract = new SuppliesStorageContract(MagicCarpetDbContext);
|
||||
_tour = InsertTourToDatabaseAndReturn();
|
||||
}
|
||||
|
||||
[TearDown]
|
||||
public void TearDown()
|
||||
{
|
||||
MagicCarpetDbContext.Database.ExecuteSqlRaw("TRUNCATE \"Supplieses\" CASCADE;");
|
||||
MagicCarpetDbContext.Database.ExecuteSqlRaw("TRUNCATE \"Tours\" CASCADE;");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_GetList_WhenHaveRecords_Test()
|
||||
{
|
||||
var supply = InsertSupplyToDatabaseAndReturn(Guid.NewGuid().ToString(), TourType.Ski, 5);
|
||||
InsertSupplyToDatabaseAndReturn(Guid.NewGuid().ToString(), TourType.Ski, 5);
|
||||
InsertSupplyToDatabaseAndReturn(Guid.NewGuid().ToString(), TourType.Ski, 5);
|
||||
var list = _suppliesStorageContract.GetList();
|
||||
Assert.That(list, Is.Not.Null);
|
||||
Assert.That(list, Has.Count.EqualTo(3));
|
||||
AssertElement(list.First(), supply);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_GetList_WhenNoRecords_Test()
|
||||
{
|
||||
var list = _suppliesStorageContract.GetList();
|
||||
Assert.That(list, Is.Not.Null);
|
||||
Assert.That(list, Is.Empty);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_GetElementById_WhenHaveRecord_Test()
|
||||
{
|
||||
var supply = InsertSupplyToDatabaseAndReturn(Guid.NewGuid().ToString(), TourType.Ski, 5);
|
||||
AssertElement(_suppliesStorageContract.GetElementById(supply.Id), supply);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_GetElementById_WhenNoRecord_Test()
|
||||
{
|
||||
Assert.That(() => _suppliesStorageContract.GetElementById(Guid.NewGuid().ToString()), Is.Null);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_AddElement_Test()
|
||||
{
|
||||
var supply = CreateModel(Guid.NewGuid().ToString(), TourType.Ski, 5, [_tour.Id]);
|
||||
_suppliesStorageContract.AddElement(supply);
|
||||
AssertElement(GetSupplyFromDatabaseById(supply.Id), supply);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_AddElement_WhenHaveRecordWithSameId_Test()
|
||||
{
|
||||
var supply = CreateModel(Guid.NewGuid().ToString(), TourType.Ski, 5, [_tour.Id]);
|
||||
InsertSupplyToDatabaseAndReturn(supply.Id, TourType.Ski, 5, [(_tour.Id, 3)]);
|
||||
Assert.That(() => _suppliesStorageContract.AddElement(supply), Throws.TypeOf<StorageException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_UpdElement_Test()
|
||||
{
|
||||
var supply = CreateModel(Guid.NewGuid().ToString(), TourType.Ski, 5, [_tour.Id]);
|
||||
InsertSupplyToDatabaseAndReturn(supply.Id, TourType.Ski, 5, null);
|
||||
_suppliesStorageContract.UpdElement(supply);
|
||||
AssertElement(GetSupplyFromDatabaseById(supply.Id), supply);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_UpdElement_WhenNoRecordWithThisId_Test()
|
||||
{
|
||||
Assert.That(() => _suppliesStorageContract.UpdElement(CreateModel(Guid.NewGuid().ToString(), TourType.Ski, 5, [_tour.Id])), Throws.TypeOf<ElementNotFoundException>());
|
||||
}
|
||||
|
||||
private Tour InsertTourToDatabaseAndReturn()
|
||||
{
|
||||
var tour = new Tour { Id = Guid.NewGuid().ToString(), TourName = "Test Tour", TourType = TourType.Ski };
|
||||
MagicCarpetDbContext.Tours.Add(tour);
|
||||
MagicCarpetDbContext.SaveChanges();
|
||||
return tour;
|
||||
}
|
||||
|
||||
private Supplies InsertSupplyToDatabaseAndReturn(string id, TourType type, int count, List<(string, int)>? tours = null)
|
||||
{
|
||||
var supply = new Supplies { Id = id, Type = type, ProductuionDate = DateTime.UtcNow, Count = count, Tours = [] };
|
||||
if (tours is not null)
|
||||
{
|
||||
foreach (var elem in tours)
|
||||
{
|
||||
supply.Tours.Add(new TourSupplies { SuppliesId = supply.Id, TourId = elem.Item1, Count = elem.Item2 });
|
||||
}
|
||||
}
|
||||
MagicCarpetDbContext.Supplieses.Add(supply);
|
||||
MagicCarpetDbContext.SaveChanges();
|
||||
return supply;
|
||||
}
|
||||
|
||||
private static void AssertElement(SuppliesDataModel? actual, Supplies expected)
|
||||
{
|
||||
Assert.That(actual, Is.Not.Null);
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(actual.Id, Is.EqualTo(expected.Id));
|
||||
Assert.That(actual.Type, Is.EqualTo(expected.Type));
|
||||
Assert.That(actual.ProductuionDate, Is.EqualTo(expected.ProductuionDate));
|
||||
Assert.That(actual.Count, Is.EqualTo(expected.Count));
|
||||
});
|
||||
if (expected.Tours is not null)
|
||||
{
|
||||
Assert.That(actual.Tours, Is.Not.Null);
|
||||
Assert.That(actual.Tours, Has.Count.EqualTo(expected.Tours.Count));
|
||||
for (int i = 0; i < actual.Tours.Count; ++i)
|
||||
{
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(actual.Tours[i].TourId, Is.EqualTo(expected.Tours[i].TourId));
|
||||
Assert.That(actual.Tours[i].Count, Is.EqualTo(expected.Tours[i].Count));
|
||||
});
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Assert.That(actual.Tours, Is.Null);
|
||||
}
|
||||
}
|
||||
|
||||
private static void AssertElement(Supplies? actual, SuppliesDataModel expected)
|
||||
{
|
||||
Assert.That(actual, Is.Not.Null);
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(actual.Id, Is.EqualTo(expected.Id));
|
||||
Assert.That(actual.Type, Is.EqualTo(expected.Type));
|
||||
Assert.That(actual.ProductuionDate, Is.EqualTo(expected.ProductuionDate));
|
||||
Assert.That(actual.Count, Is.EqualTo(expected.Count));
|
||||
});
|
||||
if (expected.Tours is not null)
|
||||
{
|
||||
Assert.That(actual.Tours, Is.Not.Null);
|
||||
Assert.That(actual.Tours, Has.Count.EqualTo(expected.Tours.Count));
|
||||
for (int i = 0; i < actual.Tours.Count; ++i)
|
||||
{
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(actual.Tours[i].TourId, Is.EqualTo(expected.Tours[i].TourId));
|
||||
Assert.That(actual.Tours[i].Count, Is.EqualTo(expected.Tours[i].Count));
|
||||
});
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Assert.That(actual.Tours, Is.Null);
|
||||
}
|
||||
}
|
||||
|
||||
private static SuppliesDataModel CreateModel(string id, TourType type, int count, List<string> toursIds)
|
||||
{
|
||||
var tours = toursIds.Select(x => new TourSuppliesDataModel(id, x, 1)).ToList();
|
||||
return new(id, type, DateTime.UtcNow, count, tours);
|
||||
}
|
||||
|
||||
|
||||
private Supplies? GetSupplyFromDatabaseById(string id)
|
||||
{
|
||||
return MagicCarpetDbContext.Supplieses.FirstOrDefault(x => x.Id == id);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,215 @@
|
||||
using MagicCarpetContracts.DataModels;
|
||||
using MagicCarpetContracts.Enums;
|
||||
using MagicCarpetContracts.Exceptions;
|
||||
using MagicCarpetDatabase.Implementations;
|
||||
using MagicCarpetDatabase.Models;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Moq;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using static NUnit.Framework.Internal.OSPlatform;
|
||||
|
||||
namespace MagicCarpetTests.StoragesContractsTests;
|
||||
|
||||
[TestFixture]
|
||||
internal class TourStorageContractTests : BaseStorageContractTest
|
||||
{
|
||||
private TourStorageContract _tourStorageContract;
|
||||
|
||||
[SetUp]
|
||||
public void SetUp()
|
||||
{
|
||||
_tourStorageContract = new TourStorageContract(MagicCarpetDbContext);
|
||||
}
|
||||
|
||||
[TearDown]
|
||||
public void TearDown()
|
||||
{
|
||||
MagicCarpetDbContext.Database.ExecuteSqlRaw("TRUNCATE \"Tours\" CASCADE;");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_GetList_WhenHaveRecords_Test()
|
||||
{
|
||||
var tour = InsertTourToDatabaseAndReturn(Guid.NewGuid().ToString(), "name 1");
|
||||
InsertTourToDatabaseAndReturn(Guid.NewGuid().ToString(), "name 2");
|
||||
InsertTourToDatabaseAndReturn(Guid.NewGuid().ToString(), "name 3");
|
||||
var list = _tourStorageContract.GetList();
|
||||
Assert.That(list, Is.Not.Null);
|
||||
Assert.That(list, Has.Count.EqualTo(3));
|
||||
AssertElement(list.First(x => x.Id == tour.Id), tour);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_GetList_WhenNoRecords_Test()
|
||||
{
|
||||
var list = _tourStorageContract.GetList();
|
||||
Assert.That(list, Is.Not.Null);
|
||||
Assert.That(list, Is.Empty);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_GetHistoryByTourId_WhenHaveRecords_Test()
|
||||
{
|
||||
var tour = InsertTourToDatabaseAndReturn(Guid.NewGuid().ToString(), "name 1");
|
||||
InsertTourHistoryToDatabaseAndReturn(tour.Id, 20, DateTime.UtcNow.AddDays(-1));
|
||||
InsertTourHistoryToDatabaseAndReturn(tour.Id, 30, DateTime.UtcNow.AddMinutes(-10));
|
||||
InsertTourHistoryToDatabaseAndReturn(tour.Id, 40, DateTime.UtcNow.AddDays(1));
|
||||
var list = _tourStorageContract.GetHistoryByTourId(tour.Id);
|
||||
Assert.That(list, Is.Not.Null);
|
||||
Assert.That(list, Has.Count.EqualTo(3));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_GetHistoryByTourId_WhenNoRecords_Test()
|
||||
{
|
||||
var tour = InsertTourToDatabaseAndReturn(Guid.NewGuid().ToString(), "name 1");
|
||||
InsertTourHistoryToDatabaseAndReturn(tour.Id, 20, DateTime.UtcNow.AddDays(-1));
|
||||
InsertTourHistoryToDatabaseAndReturn(tour.Id, 30, DateTime.UtcNow.AddMinutes(-10));
|
||||
InsertTourHistoryToDatabaseAndReturn(tour.Id, 40, DateTime.UtcNow.AddDays(1));
|
||||
var list = _tourStorageContract.GetHistoryByTourId(Guid.NewGuid().ToString());
|
||||
Assert.That(list, Is.Not.Null);
|
||||
Assert.That(list, Has.Count.EqualTo(0));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_GetElementById_WhenHaveRecord_Test()
|
||||
{
|
||||
var tour = InsertTourToDatabaseAndReturn(Guid.NewGuid().ToString());
|
||||
AssertElement(_tourStorageContract.GetElementById(tour.Id), tour);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_GetElementById_WhenNoRecord_Test()
|
||||
{
|
||||
InsertTourToDatabaseAndReturn(Guid.NewGuid().ToString());
|
||||
Assert.That(() => _tourStorageContract.GetElementById(Guid.NewGuid().ToString()), Is.Null);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_GetElementByName_WhenHaveRecord_Test()
|
||||
{
|
||||
var tour = InsertTourToDatabaseAndReturn(Guid.NewGuid().ToString());
|
||||
AssertElement(_tourStorageContract.GetElementByName(tour.TourName), tour);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_GetElementByName_WhenNoRecord_Test()
|
||||
{
|
||||
InsertTourToDatabaseAndReturn(Guid.NewGuid().ToString());
|
||||
Assert.That(() => _tourStorageContract.GetElementByName("name"), Is.Null);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_AddElement_Test()
|
||||
{
|
||||
var tour = CreateModel(Guid.NewGuid().ToString());
|
||||
_tourStorageContract.AddElement(tour);
|
||||
AssertElement(GetTourFromDatabaseById(tour.Id), tour);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_AddElement_WhenHaveRecordWithSameId_Test()
|
||||
{
|
||||
var tour = CreateModel(Guid.NewGuid().ToString());
|
||||
InsertTourToDatabaseAndReturn(tour.Id, tourName: "name unique");
|
||||
Assert.That(() => _tourStorageContract.AddElement(tour), Throws.TypeOf<ElementExistsException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_AddElement_WhenHaveRecordWithSameName_Test()
|
||||
{
|
||||
var tour = CreateModel(Guid.NewGuid().ToString(), "name unique");
|
||||
InsertTourToDatabaseAndReturn(Guid.NewGuid().ToString(), tourName: tour.TourName);
|
||||
Assert.That(() => _tourStorageContract.AddElement(tour), Throws.TypeOf<ElementExistsException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_UpdElement_Test()
|
||||
{
|
||||
var tour = CreateModel(Guid.NewGuid().ToString(), "new name", "country");
|
||||
InsertTourToDatabaseAndReturn(tour.Id);
|
||||
_tourStorageContract.UpdElement(tour);
|
||||
AssertElement(GetTourFromDatabaseById(tour.Id), tour);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_UpdElement_WhenNoRecordWithThisId_Test()
|
||||
{
|
||||
Assert.That(() => _tourStorageContract.UpdElement(CreateModel(Guid.NewGuid().ToString())), Throws.TypeOf<ElementNotFoundException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_UpdElement_WhenHaveRecordWithSameName_Test()
|
||||
{
|
||||
var tour = CreateModel(Guid.NewGuid().ToString(), "name unique");
|
||||
InsertTourToDatabaseAndReturn(tour.Id, tourName: "name");
|
||||
InsertTourToDatabaseAndReturn(Guid.NewGuid().ToString(), tourName: tour.TourName);
|
||||
Assert.That(() => _tourStorageContract.UpdElement(tour), Throws.TypeOf<ElementExistsException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_DelElement_Test()
|
||||
{
|
||||
var tour = InsertTourToDatabaseAndReturn(Guid.NewGuid().ToString());
|
||||
_tourStorageContract.DelElement(tour.Id);
|
||||
var element = GetTourFromDatabaseById(tour.Id);
|
||||
Assert.That(element, Is.Null);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_DelElement_WhenNoRecordWithThisId_Test()
|
||||
{
|
||||
Assert.That(() => _tourStorageContract.DelElement(Guid.NewGuid().ToString()), Throws.TypeOf<ElementNotFoundException>());
|
||||
}
|
||||
|
||||
private Tour InsertTourToDatabaseAndReturn(string id, string tourName = "test", string tourCountry = "country", TourType tourType = TourType.Beach, double price = 1)
|
||||
{
|
||||
var tour = new Tour() { Id = id, TourName = tourName, TourCountry = tourCountry, TourType = tourType, Price = price };
|
||||
MagicCarpetDbContext.Tours.Add(tour);
|
||||
MagicCarpetDbContext.SaveChanges();
|
||||
return tour;
|
||||
}
|
||||
|
||||
private TourHistory InsertTourHistoryToDatabaseAndReturn(string tourId, double price, DateTime changeDate)
|
||||
{
|
||||
var tourHistory = new TourHistory() { Id = Guid.NewGuid().ToString(), TourId = tourId, OldPrice = price, ChangeDate = changeDate };
|
||||
MagicCarpetDbContext.TourHistories.Add(tourHistory);
|
||||
MagicCarpetDbContext.SaveChanges();
|
||||
return tourHistory;
|
||||
}
|
||||
|
||||
private static void AssertElement(TourDataModel? actual, Tour expected)
|
||||
{
|
||||
Assert.That(actual, Is.Not.Null);
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(actual.Id, Is.EqualTo(expected.Id));
|
||||
Assert.That(actual.TourName, Is.EqualTo(expected.TourName));
|
||||
Assert.That(actual.TourCountry, Is.EqualTo(expected.TourCountry));
|
||||
Assert.That(actual.TourType, Is.EqualTo(expected.TourType));
|
||||
Assert.That(actual.Price, Is.EqualTo(expected.Price));
|
||||
});
|
||||
}
|
||||
|
||||
private static TourDataModel CreateModel(string id, string tourName = "test", string tourCountry = "country", TourType type = TourType.Beach, double price = 1)
|
||||
=> new(id, tourName, tourCountry, price, type);
|
||||
|
||||
private Tour? GetTourFromDatabaseById(string id) => MagicCarpetDbContext.Tours.FirstOrDefault(x => x.Id == id);
|
||||
|
||||
private static void AssertElement(Tour? actual, TourDataModel expected)
|
||||
{
|
||||
Assert.That(actual, Is.Not.Null);
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(actual.Id, Is.EqualTo(expected.Id));
|
||||
Assert.That(actual.TourName, Is.EqualTo(expected.TourName));
|
||||
Assert.That(actual.TourCountry, Is.EqualTo(expected.TourCountry));
|
||||
Assert.That(actual.TourType, Is.EqualTo(expected.TourType));
|
||||
Assert.That(actual.Price, Is.EqualTo(expected.Price));
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user