Compare commits

...

14 Commits

48 changed files with 2034 additions and 53 deletions

View File

@@ -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);
}
}

View File

@@ -13,10 +13,11 @@ using System.Threading.Tasks;
namespace MagicCarpetBusinessLogic.Implementations; 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 ILogger _logger = logger;
private readonly ISaleStorageContract _saleStorageContract = saleStorageContract; private readonly ISaleStorageContract _saleStorageContract = saleStorageContract;
private readonly IAgencyStorageContract _agencyStorageContract = agencyStorageContract;
public List<SaleDataModel> GetAllSalesByPeriod(DateTime fromDate, DateTime toDate) 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)); _logger.LogInformation("New data: {json}", JsonSerializer.Serialize(saleDataModel));
ArgumentNullException.ThrowIfNull(saleDataModel); ArgumentNullException.ThrowIfNull(saleDataModel);
saleDataModel.Validate(); saleDataModel.Validate();
if (!_agencyStorageContract.CheckComponents(saleDataModel))
{
throw new InsufficientException("Dont have tour in agency");
}
_saleStorageContract.AddElement(saleDataModel); _saleStorageContract.AddElement(saleDataModel);
} }

View File

@@ -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);
}
}

View File

@@ -56,7 +56,6 @@ internal class TourBusinessLogicContract(ITourStorageContract tourStorageContrac
_logger.LogInformation("New data: {json}", JsonSerializer.Serialize(tourDataModel)); _logger.LogInformation("New data: {json}", JsonSerializer.Serialize(tourDataModel));
ArgumentNullException.ThrowIfNull(tourDataModel); ArgumentNullException.ThrowIfNull(tourDataModel);
tourDataModel.Validate(); tourDataModel.Validate();
_tourStorageContract.AddElement(tourDataModel);
} }
public void UpdateTour(TourDataModel tourDataModel) public void UpdateTour(TourDataModel tourDataModel)
@@ -64,7 +63,6 @@ internal class TourBusinessLogicContract(ITourStorageContract tourStorageContrac
_logger.LogInformation("Update data: {json}", JsonSerializer.Serialize(tourDataModel)); _logger.LogInformation("Update data: {json}", JsonSerializer.Serialize(tourDataModel));
ArgumentNullException.ThrowIfNull(tourDataModel); ArgumentNullException.ThrowIfNull(tourDataModel);
tourDataModel.Validate(); tourDataModel.Validate();
_tourStorageContract.UpdElement(tourDataModel);
} }
public void DeleteTour(string id) public void DeleteTour(string id)

View File

@@ -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);
}

View File

@@ -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);
}

View File

@@ -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");
}
}

View File

@@ -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");
}
}

View File

@@ -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");
}
}

View File

@@ -18,7 +18,7 @@ public class TourDataModel(string id, string tourName, string tourCountry, doubl
public string TourName { get; private set; } = tourName; public string TourName { get; private set; } = tourName;
public string TourCountry { get; private set; } = tourCountry; public string TourCountry { get; private set; } = tourCountry;
public double Price { get; private set; } = price; public double Price { get; private set; } = price;
public TourType Type { get; private set; } = tourType; public TourType TourType { get; private set; } = tourType;
public void Validate() public void Validate()
{ {
@@ -32,7 +32,7 @@ public class TourDataModel(string id, string tourName, string tourCountry, doubl
throw new ValidationException("Field TourCountry is empty"); throw new ValidationException("Field TourCountry is empty");
if (Price <= 0) if (Price <= 0)
throw new ValidationException("Field Price is less than or equal to 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"); throw new ValidationException("Field Type is empty");
} }
} }

View File

@@ -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");
}
}

View File

@@ -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)
{
}

View File

@@ -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);
}

View File

@@ -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);
}

View File

@@ -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);
}

View File

@@ -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);
}

View File

@@ -7,6 +7,7 @@ using Microsoft.EntityFrameworkCore;
using Npgsql; using Npgsql;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.ComponentModel;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
@@ -23,10 +24,9 @@ internal class TourStorageContract : ITourStorageContract
_dbContext = dbContext; _dbContext = dbContext;
var config = new MapperConfiguration(cfg => var config = new MapperConfiguration(cfg =>
{ {
cfg.CreateMap<Tour, TourDataModel>() cfg.CreateMap<Tour, TourDataModel>();
.ConstructUsing(src => new TourDataModel(src.Id, src.TourName, src.TourCountry, src.Price, src.Type));
cfg.CreateMap<TourDataModel, Tour>(); cfg.CreateMap<TourDataModel, Tour>();
cfg.CreateMap<TourHistory, TourHistoryDataModel>(); cfg.CreateMap<TourHistory, TourHistoryDataModel>(); ;
}); });
_mapper = new Mapper(config); _mapper = new Mapper(config);
} }

View File

@@ -9,7 +9,7 @@ using System.Threading.Tasks;
namespace MagicCarpetDatabase; namespace MagicCarpetDatabase;
internal class MagicCarpetDbContext(IConfigurationDatabase configurationDatabase) : DbContext public class MagicCarpetDbContext(IConfigurationDatabase configurationDatabase) : DbContext
{ {
private readonly IConfigurationDatabase? _configurationDatabase = configurationDatabase; private readonly IConfigurationDatabase? _configurationDatabase = configurationDatabase;
@@ -38,6 +38,10 @@ internal class MagicCarpetDbContext(IConfigurationDatabase configurationDatabase
.HasFilter($"\"{nameof(Post.IsActual)}\" = TRUE"); .HasFilter($"\"{nameof(Post.IsActual)}\" = TRUE");
modelBuilder.Entity<SaleTour>().HasKey(x => new { x.SaleId, x.TourId }); 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<Client> Clients { get; set; }
@@ -55,4 +59,8 @@ internal class MagicCarpetDbContext(IConfigurationDatabase configurationDatabase
public DbSet<SaleTour> SaleTours { get; set; } public DbSet<SaleTour> SaleTours { get; set; }
public DbSet<Employee> Employees { 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; }
} }

View 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; }
}

View File

@@ -10,7 +10,7 @@ using System.Threading.Tasks;
namespace MagicCarpetDatabase.Models; namespace MagicCarpetDatabase.Models;
[AutoMap(typeof(ClientDataModel), ReverseMap = true)] [AutoMap(typeof(ClientDataModel), ReverseMap = true)]
internal class Client public class Client
{ {
public required string Id { get; set; } public required string Id { get; set; }

View File

@@ -10,7 +10,7 @@ using System.Threading.Tasks;
namespace MagicCarpetDatabase.Models; namespace MagicCarpetDatabase.Models;
[AutoMap(typeof(EmployeeDataModel), ReverseMap = true)] [AutoMap(typeof(EmployeeDataModel), ReverseMap = true)]
internal class Employee public class Employee
{ {
public required string Id { get; set; } public required string Id { get; set; }

View File

@@ -8,7 +8,7 @@ using System.Threading.Tasks;
namespace MagicCarpetDatabase.Models; namespace MagicCarpetDatabase.Models;
internal class Post public class Post
{ {
public required string Id { get; set; } = Guid.NewGuid().ToString(); public required string Id { get; set; } = Guid.NewGuid().ToString();
public required string PostId { get; set; } public required string PostId { get; set; }

View File

@@ -6,7 +6,7 @@ using System.Threading.Tasks;
namespace MagicCarpetDatabase.Models; namespace MagicCarpetDatabase.Models;
internal class Salary public class Salary
{ {
public string Id { get; set; } = Guid.NewGuid().ToString(); public string Id { get; set; } = Guid.NewGuid().ToString();
public required string EmployeeId { get; set; } public required string EmployeeId { get; set; }

View File

@@ -9,7 +9,7 @@ using System.Threading.Tasks;
namespace MagicCarpetDatabase.Models; namespace MagicCarpetDatabase.Models;
internal class Sale public class Sale
{ {
public string Id { get; set; } = Guid.NewGuid().ToString(); public string Id { get; set; } = Guid.NewGuid().ToString();

View File

@@ -6,7 +6,7 @@ using System.Threading.Tasks;
namespace MagicCarpetDatabase.Models; namespace MagicCarpetDatabase.Models;
internal class SaleTour public class SaleTour
{ {
public required string SaleId { get; set; } public required string SaleId { get; set; }

View 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; }
}

View File

@@ -1,4 +1,6 @@
using MagicCarpetContracts.Enums; using AutoMapper;
using MagicCarpetContracts.DataModels;
using MagicCarpetContracts.Enums;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema; using System.ComponentModel.DataAnnotations.Schema;
@@ -9,15 +11,20 @@ using System.Threading.Tasks;
namespace MagicCarpetDatabase.Models; namespace MagicCarpetDatabase.Models;
internal class Tour [AutoMap(typeof(TourDataModel), ReverseMap = true)]
public class Tour
{ {
public required string Id { get; set; } public required string Id { get; set; }
public required string TourName { get; set; } public required string TourName { get; set; }
public string? TourCountry { get; set; } public string? TourCountry { get; set; }
public double Price { get; set; } public double Price { get; set; }
public required TourType Type { get; set; } public required TourType TourType { get; set; }
[ForeignKey("TourId")] [ForeignKey("TourId")]
public List<SaleTour>? SaleTours { get; set; } public List<SaleTour>? SaleTours { get; set; }
[ForeignKey("TourId")] [ForeignKey("TourId")]
public List<TourHistory>? TourHistories { get; set; } public List<TourHistory>? TourHistories { get; set; }
[ForeignKey("SuppliesesId")]
public List<TourSupplies>? TourSupplies { get; set; }
[ForeignKey("AgenciesId")]
public List<TourAgency>? TourAgency { get; set; }
} }

View 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; }
}

View File

@@ -6,7 +6,7 @@ using System.Threading.Tasks;
namespace MagicCarpetDatabase.Models; namespace MagicCarpetDatabase.Models;
internal class TourHistory public class TourHistory
{ {
public string Id { get; set; } = Guid.NewGuid().ToString(); public string Id { get; set; } = Guid.NewGuid().ToString();
public required string TourId { get; set; } public required string TourId { get; set; }

View File

@@ -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; }
}

View File

@@ -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);
}
}

View File

@@ -18,18 +18,22 @@ internal class SaleBusinessLogicContractTests
{ {
private SaleBusinessLogicContract _saleBusinessLogicContract; private SaleBusinessLogicContract _saleBusinessLogicContract;
private Mock<ISaleStorageContract> _saleStorageContract; private Mock<ISaleStorageContract> _saleStorageContract;
private Mock<IAgencyStorageContract> _agencyStorageContract;
[OneTimeSetUp] [OneTimeSetUp]
public void OneTimeSetUp() public void OneTimeSetUp()
{ {
_saleStorageContract = new Mock<ISaleStorageContract>(); _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] [TearDown]
public void TearDown() public void TearDown()
{ {
_saleStorageContract.Reset(); _saleStorageContract.Reset();
_agencyStorageContract.Reset();
} }
[Test] [Test]
@@ -396,6 +400,7 @@ internal class SaleBusinessLogicContractTests
var flag = false; var flag = false;
var record = new SaleDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 10, DiscountType.None, 10, 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)]); 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>())) _saleStorageContract.Setup(x => x.AddElement(It.IsAny<SaleDataModel>()))
.Callback((SaleDataModel x) => .Callback((SaleDataModel x) =>
{ {
@@ -409,6 +414,7 @@ internal class SaleBusinessLogicContractTests
//Act //Act
_saleBusinessLogicContract.InsertSale(record); _saleBusinessLogicContract.InsertSale(record);
//Assert //Assert
_agencyStorageContract.Verify(x => x.CheckComponents(It.IsAny<SaleDataModel>()), Times.Once);
_saleStorageContract.Verify(x => x.AddElement(It.IsAny<SaleDataModel>()), Times.Once); _saleStorageContract.Verify(x => x.AddElement(It.IsAny<SaleDataModel>()), Times.Once);
Assert.That(flag); Assert.That(flag);
} }
@@ -417,11 +423,13 @@ internal class SaleBusinessLogicContractTests
public void InsertSale_RecordWithExistsData_ThrowException_Test() public void InsertSale_RecordWithExistsData_ThrowException_Test()
{ {
//Arrange //Arrange
_agencyStorageContract.Setup(x => x.CheckComponents(It.IsAny<SaleDataModel>())).Returns(true);
_saleStorageContract.Setup(x => x.AddElement(It.IsAny<SaleDataModel>())).Throws(new ElementExistsException("Data", "Data")); _saleStorageContract.Setup(x => x.AddElement(It.IsAny<SaleDataModel>())).Throws(new ElementExistsException("Data", "Data"));
//Act&Assert //Act&Assert
Assert.That(() => _saleBusinessLogicContract.InsertSale(new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 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>()); 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); _saleStorageContract.Verify(x => x.AddElement(It.IsAny<SaleDataModel>()), Times.Once);
_agencyStorageContract.Verify(x => x.CheckComponents(It.IsAny<SaleDataModel>()), Times.Once);
} }
[Test] [Test]
@@ -444,13 +452,27 @@ internal class SaleBusinessLogicContractTests
public void InsertSale_StorageThrowError_ThrowException_Test() public void InsertSale_StorageThrowError_ThrowException_Test()
{ {
//Arrange //Arrange
_agencyStorageContract.Setup(x => x.CheckComponents(It.IsAny<SaleDataModel>())).Returns(true);
_saleStorageContract.Setup(x => x.AddElement(It.IsAny<SaleDataModel>())).Throws(new StorageException(new InvalidOperationException())); _saleStorageContract.Setup(x => x.AddElement(It.IsAny<SaleDataModel>())).Throws(new StorageException(new InvalidOperationException()));
//Act&Assert //Act&Assert
Assert.That(() => _saleBusinessLogicContract.InsertSale(new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 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>()); 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); _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] [Test]
public void CancelSale_CorrectRecord_Test() public void CancelSale_CorrectRecord_Test()
{ {

View File

@@ -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);
}
}

View File

@@ -3,6 +3,7 @@ using MagicCarpetContracts.DataModels;
using MagicCarpetContracts.Enums; using MagicCarpetContracts.Enums;
using MagicCarpetContracts.Exceptions; using MagicCarpetContracts.Exceptions;
using MagicCarpetContracts.StoragesContracts; using MagicCarpetContracts.StoragesContracts;
using MagicCarpetTests.DataModelTests;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Moq; using Moq;
using System; using System;
@@ -154,7 +155,7 @@ internal class TourBusinessLogicContractTests
{ {
//Arrange //Arrange
var id = Guid.NewGuid().ToString(); 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); _tourStorageContract.Setup(x => x.GetElementById(id)).Returns(record);
//Act //Act
var element = _tourBusinessLogicContract.GetTourByData(id); var element = _tourBusinessLogicContract.GetTourByData(id);
@@ -183,7 +184,7 @@ internal class TourBusinessLogicContractTests
{ {
//Arrange //Arrange
var country = "country"; 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); _tourStorageContract.Setup(x => x.GetElementByName(country)).Returns(record);
//Act //Act
var element = _tourBusinessLogicContract.GetTourByData(country); var element = _tourBusinessLogicContract.GetTourByData(country);
@@ -249,7 +250,7 @@ internal class TourBusinessLogicContractTests
.Callback((TourDataModel x) => .Callback((TourDataModel x) =>
{ {
flag = x.Id == record.Id && x.TourName == record.TourName && x.TourCountry == record.TourCountry 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 //Act
_tourBusinessLogicContract.InsertTour(record); _tourBusinessLogicContract.InsertTour(record);
@@ -288,10 +289,18 @@ internal class TourBusinessLogicContractTests
public void InsertTour_StorageThrowError_ThrowException_Test() public void InsertTour_StorageThrowError_ThrowException_Test()
{ {
//Arrange //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 //Act&Assert
Assert.That(() => _tourBusinessLogicContract.InsertTour(new(Guid.NewGuid().ToString(), "name", "country", 10, TourType.Ski)), Throws.TypeOf<StorageException>()); _tourStorageContract.Verify(x => x.UpdElement(It.IsAny<TourDataModel>()), Times.Never);
_tourStorageContract.Verify(x => x.AddElement(It.IsAny<TourDataModel>()), Times.Once); }
[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] [Test]
@@ -304,7 +313,7 @@ internal class TourBusinessLogicContractTests
.Callback((TourDataModel x) => .Callback((TourDataModel x) =>
{ {
flag = x.Id == record.Id && x.TourName == record.TourName && x.TourCountry == record.TourCountry 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 //Act
_tourBusinessLogicContract.UpdateTour(record); _tourBusinessLogicContract.UpdateTour(record);
@@ -329,7 +338,7 @@ internal class TourBusinessLogicContractTests
//Arrange //Arrange
_tourStorageContract.Setup(x => x.UpdElement(It.IsAny<TourDataModel>())).Throws(new ElementExistsException("Data", "Data")); _tourStorageContract.Setup(x => x.UpdElement(It.IsAny<TourDataModel>())).Throws(new ElementExistsException("Data", "Data"));
//Act&Assert //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); _tourStorageContract.Verify(x => x.UpdElement(It.IsAny<TourDataModel>()), Times.Once);
} }

View File

@@ -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)];
}

View File

@@ -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)];
}

View File

@@ -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);
}

View File

@@ -3,6 +3,7 @@ using MagicCarpetContracts.Enums;
using MagicCarpetContracts.Exceptions; using MagicCarpetContracts.Exceptions;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.ComponentModel;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
@@ -14,17 +15,17 @@ internal class TourDataModelTests
[Test] [Test]
public void IdIsNullOrEmptyTest() public void IdIsNullOrEmptyTest()
{ {
var cocktail = CreateDataModel(null, "name", "country", 10.5, TourType.Beach); var tour = CreateDataModel(null, "name", "country", 10.5, TourType.Beach);
Assert.That(() => cocktail.Validate(), Throws.TypeOf<ValidationException>()); Assert.That(() => tour.Validate(), Throws.TypeOf<ValidationException>());
cocktail = CreateDataModel(string.Empty, "name", "country", 10.5, TourType.Beach); tour = CreateDataModel(string.Empty, "name", "country", 10.5, TourType.Beach);
Assert.That(() => cocktail.Validate(), Throws.TypeOf<ValidationException>()); Assert.That(() => tour.Validate(), Throws.TypeOf<ValidationException>());
} }
[Test] [Test]
public void IdIsNotGuidTest() public void IdIsNotGuidTest()
{ {
var cocktail = CreateDataModel("id", "name", "country", 10.5, TourType.Beach); var tour = CreateDataModel("id", "name", "country", 10.5, TourType.Beach);
Assert.That(() => cocktail.Validate(), Throws.TypeOf<ValidationException>()); Assert.That(() => tour.Validate(), Throws.TypeOf<ValidationException>());
} }
[Test] [Test]
@@ -46,17 +47,17 @@ internal class TourDataModelTests
[Test] [Test]
public void PriceIsLessOrZeroTest() public void PriceIsLessOrZeroTest()
{ {
var cocktail = CreateDataModel(Guid.NewGuid().ToString(), null, null, 0, TourType.Beach); var tour = CreateDataModel(Guid.NewGuid().ToString(), null, null, 0, TourType.Beach);
Assert.That(() => cocktail.Validate(), Throws.TypeOf<ValidationException>()); Assert.That(() => tour.Validate(), Throws.TypeOf<ValidationException>());
cocktail = CreateDataModel(Guid.NewGuid().ToString(), string.Empty, string.Empty, -10.5, TourType.Beach); tour = CreateDataModel(Guid.NewGuid().ToString(), string.Empty, string.Empty, -10.5, TourType.Beach);
Assert.That(() => cocktail.Validate(), Throws.TypeOf<ValidationException>()); Assert.That(() => tour.Validate(), Throws.TypeOf<ValidationException>());
} }
[Test] [Test]
public void TourTypeIsNoneTest() public void TourTypeIsNoneTest()
{ {
var cocktail = CreateDataModel(Guid.NewGuid().ToString(), null, null, 0, TourType.Beach); var tour = CreateDataModel(Guid.NewGuid().ToString(), null, null, 0, TourType.Beach);
Assert.That(() => cocktail.Validate(), Throws.TypeOf<ValidationException>()); Assert.That(() => tour.Validate(), Throws.TypeOf<ValidationException>());
} }
[Test] [Test]
@@ -67,6 +68,8 @@ internal class TourDataModelTests
var tourCountry = "country"; var tourCountry = "country";
var price = 10.5; var price = 10.5;
var tourType = TourType.Ski; var tourType = TourType.Ski;
var supplies = CreateSuppliesDataModel();
var agency = CreateAgencyDataModel();
var tour = CreateDataModel(tourId, tourName, tourCountry, price, tourType); var tour = CreateDataModel(tourId, tourName, tourCountry, price, tourType);
Assert.That(() => tour.Validate(), Throws.Nothing); Assert.That(() => tour.Validate(), Throws.Nothing);
Assert.Multiple(() => Assert.Multiple(() =>
@@ -75,10 +78,15 @@ internal class TourDataModelTests
Assert.That(tour.TourName, Is.EqualTo(tourName)); Assert.That(tour.TourName, Is.EqualTo(tourName));
Assert.That(tour.TourCountry, Is.EqualTo(tourCountry)); Assert.That(tour.TourCountry, Is.EqualTo(tourCountry));
Assert.That(tour.Price, Is.EqualTo(price)); 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); 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)];
} }

View File

@@ -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);
}

View File

@@ -11,6 +11,7 @@
<ItemGroup> <ItemGroup>
<PackageReference Include="coverlet.collector" Version="6.0.0" /> <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="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="Moq" Version="4.20.72" /> <PackageReference Include="Moq" Version="4.20.72" />
<PackageReference Include="NUnit" Version="3.14.0" /> <PackageReference Include="NUnit" Version="3.14.0" />

View File

@@ -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);
}
}

View File

@@ -6,7 +6,7 @@ using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace MagicCarpetTests.StoragesContracts; namespace MagicCarpetTests.StoragesContractsTests;
internal abstract class BaseStorageContractTest internal abstract class BaseStorageContractTest
{ {

View File

@@ -1,7 +1,6 @@
using MagicCarpetContracts.DataModels; using MagicCarpetContracts.DataModels;
using MagicCarpetContracts.Enums; using MagicCarpetContracts.Enums;
using MagicCarpetContracts.Exceptions; using MagicCarpetContracts.Exceptions;
using MagicCarpetContracts.StoragesContracts;
using MagicCarpetDatabase.Implementations; using MagicCarpetDatabase.Implementations;
using MagicCarpetDatabase.Models; using MagicCarpetDatabase.Models;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
@@ -11,7 +10,7 @@ using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace MagicCarpetTests.StoragesContracts; namespace MagicCarpetTests.StoragesContractsTests;
[TestFixture] [TestFixture]
internal class ClientStorageContractTests : BaseStorageContractTest internal class ClientStorageContractTests : BaseStorageContractTest

View File

@@ -9,7 +9,7 @@ using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace MagicCarpetTests.StoragesContracts; namespace MagicCarpetTests.StoragesContractsTests;
[TestFixture] [TestFixture]
class EmployeeStorageContractTests : BaseStorageContractTest class EmployeeStorageContractTests : BaseStorageContractTest

View File

@@ -8,7 +8,7 @@ using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace MagicCarpetTests.StoragesContracts; namespace MagicCarpetTests.StoragesContractsTests;
[TestFixture] [TestFixture]
internal class SalaryStorageContractTests : BaseStorageContractTest internal class SalaryStorageContractTests : BaseStorageContractTest

View File

@@ -11,7 +11,7 @@ using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using static NUnit.Framework.Internal.OSPlatform; using static NUnit.Framework.Internal.OSPlatform;
namespace MagicCarpetTests.StoragesContracts; namespace MagicCarpetTests.StoragesContractsTests;
[TestFixture] [TestFixture]
internal class SaleStorageContractTests : BaseStorageContractTest internal class SaleStorageContractTests : BaseStorageContractTest
@@ -211,7 +211,7 @@ internal class SaleStorageContractTests : BaseStorageContractTest
private Tour InsertTourToDatabaseAndReturn(string tourName = "test", TourType tourType = TourType.Sightseeing, double price = 1) private Tour InsertTourToDatabaseAndReturn(string tourName = "test", TourType tourType = TourType.Sightseeing, double price = 1)
{ {
var tour = new Tour() { Id = Guid.NewGuid().ToString(), TourName = tourName, Type = tourType, Price = price }; var tour = new Tour() { Id = Guid.NewGuid().ToString(), TourName = tourName, TourType = tourType, Price = price };
MagicCarpetDbContext.Tours.Add(tour); MagicCarpetDbContext.Tours.Add(tour);
MagicCarpetDbContext.SaveChanges(); MagicCarpetDbContext.SaveChanges();
return tour; return tour;

View File

@@ -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);
}
}

View File

@@ -12,7 +12,7 @@ using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using static NUnit.Framework.Internal.OSPlatform; using static NUnit.Framework.Internal.OSPlatform;
namespace MagicCarpetTests.StoragesContracts; namespace MagicCarpetTests.StoragesContractsTests;
[TestFixture] [TestFixture]
internal class TourStorageContractTests : BaseStorageContractTest internal class TourStorageContractTests : BaseStorageContractTest
@@ -168,7 +168,7 @@ internal class TourStorageContractTests : BaseStorageContractTest
private Tour InsertTourToDatabaseAndReturn(string id, string tourName = "test", string tourCountry = "country", TourType tourType = TourType.Beach, double price = 1) 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, Type = tourType, Price = price }; var tour = new Tour() { Id = id, TourName = tourName, TourCountry = tourCountry, TourType = tourType, Price = price };
MagicCarpetDbContext.Tours.Add(tour); MagicCarpetDbContext.Tours.Add(tour);
MagicCarpetDbContext.SaveChanges(); MagicCarpetDbContext.SaveChanges();
return tour; return tour;
@@ -190,7 +190,7 @@ internal class TourStorageContractTests : BaseStorageContractTest
Assert.That(actual.Id, Is.EqualTo(expected.Id)); Assert.That(actual.Id, Is.EqualTo(expected.Id));
Assert.That(actual.TourName, Is.EqualTo(expected.TourName)); Assert.That(actual.TourName, Is.EqualTo(expected.TourName));
Assert.That(actual.TourCountry, Is.EqualTo(expected.TourCountry)); Assert.That(actual.TourCountry, Is.EqualTo(expected.TourCountry));
Assert.That(actual.Type, Is.EqualTo(expected.Type)); Assert.That(actual.TourType, Is.EqualTo(expected.TourType));
Assert.That(actual.Price, Is.EqualTo(expected.Price)); Assert.That(actual.Price, Is.EqualTo(expected.Price));
}); });
} }
@@ -208,7 +208,7 @@ internal class TourStorageContractTests : BaseStorageContractTest
Assert.That(actual.Id, Is.EqualTo(expected.Id)); Assert.That(actual.Id, Is.EqualTo(expected.Id));
Assert.That(actual.TourName, Is.EqualTo(expected.TourName)); Assert.That(actual.TourName, Is.EqualTo(expected.TourName));
Assert.That(actual.TourCountry, Is.EqualTo(expected.TourCountry)); Assert.That(actual.TourCountry, Is.EqualTo(expected.TourCountry));
Assert.That(actual.Type, Is.EqualTo(expected.Type)); Assert.That(actual.TourType, Is.EqualTo(expected.TourType));
Assert.That(actual.Price, Is.EqualTo(expected.Price)); Assert.That(actual.Price, Is.EqualTo(expected.Price));
}); });
} }