forked from slavaxom9k/PIBD-23_Fomichev_V.S._MagicCarpet
с горем по полам
This commit is contained in:
@@ -11,7 +11,7 @@ using System.Threading.Tasks;
|
|||||||
namespace MagicCarpetContracts.DataModels;
|
namespace MagicCarpetContracts.DataModels;
|
||||||
|
|
||||||
public class SaleDataModel(string id, string employeeId, string? clientId, double sum, DiscountType discountType,
|
public class SaleDataModel(string id, string employeeId, string? clientId, double sum, DiscountType discountType,
|
||||||
double discount, bool isCancel, List<SaleTourDataModel> tours) : IValidation
|
double discount, bool isCancel, List<SaleTourDataModel> saleTours) : IValidation
|
||||||
{
|
{
|
||||||
public string Id { get; private set; } = id;
|
public string Id { get; private set; } = id;
|
||||||
|
|
||||||
@@ -28,7 +28,7 @@ public class SaleDataModel(string id, string employeeId, string? clientId, doubl
|
|||||||
|
|
||||||
public bool IsCancel { get; private set; } = isCancel;
|
public bool IsCancel { get; private set; } = isCancel;
|
||||||
|
|
||||||
public List<SaleTourDataModel> Tours { get; private set; } = tours;
|
public List<SaleTourDataModel> Tours { get; private set; } = saleTours;
|
||||||
|
|
||||||
public void Validate()
|
public void Validate()
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -9,11 +9,11 @@ using System.Threading.Tasks;
|
|||||||
|
|
||||||
namespace MagicCarpetContracts.DataModels;
|
namespace MagicCarpetContracts.DataModels;
|
||||||
|
|
||||||
public class SaleTourDataModel(string saleId, string cocktailId, int count) : IValidation
|
public class SaleTourDataModel(string saleId, string tourId, int count) : IValidation
|
||||||
{
|
{
|
||||||
public string SaleId { get; private set; } = saleId;
|
public string SaleId { get; private set; } = saleId;
|
||||||
|
|
||||||
public string TourId { get; private set; } = cocktailId;
|
public string TourId { get; private set; } = tourId;
|
||||||
|
|
||||||
public int Count { get; private set; } = count;
|
public int Count { get; private set; } = count;
|
||||||
|
|
||||||
|
|||||||
@@ -22,8 +22,7 @@ internal class ClientStorageContarct : IClientStorageContract
|
|||||||
_dbContext = magicCarpetDbContext;
|
_dbContext = magicCarpetDbContext;
|
||||||
var config = new MapperConfiguration(cfg =>
|
var config = new MapperConfiguration(cfg =>
|
||||||
{
|
{
|
||||||
cfg.CreateMap<Client, ClientDataModel>();
|
cfg.AddMaps(typeof(MagicCarpetDbContext).Assembly);
|
||||||
cfg.CreateMap<ClientDataModel, Client>();
|
|
||||||
});
|
});
|
||||||
_mapper = new Mapper(config);
|
_mapper = new Mapper(config);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -21,8 +21,7 @@ internal class EmployeeStorageContract : IEmployeeStorageContract
|
|||||||
_dbContext = dbContext;
|
_dbContext = dbContext;
|
||||||
var config = new MapperConfiguration(cfg =>
|
var config = new MapperConfiguration(cfg =>
|
||||||
{
|
{
|
||||||
cfg.CreateMap<Employee, EmployeeDataModel>();
|
cfg.AddMaps(typeof(MagicCarpetDbContext).Assembly);
|
||||||
cfg.CreateMap<EmployeeDataModel, Employee>();
|
|
||||||
});
|
});
|
||||||
_mapper = new Mapper(config);
|
_mapper = new Mapper(config);
|
||||||
}
|
}
|
||||||
@@ -94,17 +93,17 @@ internal class EmployeeStorageContract : IEmployeeStorageContract
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void AddElement(EmployeeDataModel workerDataModel)
|
public void AddElement(EmployeeDataModel employeeDataModel)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
_dbContext.Employees.Add(_mapper.Map<Employee>(workerDataModel));
|
_dbContext.Employees.Add(_mapper.Map<Employee>(employeeDataModel));
|
||||||
_dbContext.SaveChanges();
|
_dbContext.SaveChanges();
|
||||||
}
|
}
|
||||||
catch (InvalidOperationException ex) when (ex.TargetSite?.Name == "ThrowIdentityConflict")
|
catch (InvalidOperationException ex) when (ex.TargetSite?.Name == "ThrowIdentityConflict")
|
||||||
{
|
{
|
||||||
_dbContext.ChangeTracker.Clear();
|
_dbContext.ChangeTracker.Clear();
|
||||||
throw new ElementExistsException("Id", workerDataModel.Id);
|
throw new ElementExistsException("Id", employeeDataModel.Id);
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
@@ -113,12 +112,12 @@ internal class EmployeeStorageContract : IEmployeeStorageContract
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void UpdElement(EmployeeDataModel workerDataModel)
|
public void UpdElement(EmployeeDataModel employeeDataModel)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
var element = GetEmployeeById(workerDataModel.Id) ?? throw new ElementNotFoundException(workerDataModel.Id);
|
var element = GetEmployeeById(employeeDataModel.Id) ?? throw new ElementNotFoundException(employeeDataModel.Id);
|
||||||
_dbContext.Employees.Update(_mapper.Map(workerDataModel, element));
|
_dbContext.Employees.Update(_mapper.Map(employeeDataModel, element));
|
||||||
_dbContext.SaveChanges();
|
_dbContext.SaveChanges();
|
||||||
}
|
}
|
||||||
catch (ElementNotFoundException)
|
catch (ElementNotFoundException)
|
||||||
|
|||||||
@@ -26,7 +26,15 @@ internal class SaleStorageContract
|
|||||||
cfg.CreateMap<Sale, SaleDataModel>();
|
cfg.CreateMap<Sale, SaleDataModel>();
|
||||||
cfg.CreateMap<SaleDataModel, Sale>()
|
cfg.CreateMap<SaleDataModel, Sale>()
|
||||||
.ForMember(x => x.IsCancel, x => x.MapFrom(src => false))
|
.ForMember(x => x.IsCancel, x => x.MapFrom(src => false))
|
||||||
.ForMember(x => x.SaleTours, x => x.MapFrom(src => src.Tours));
|
.ForMember(x => x.SaleTours, x => x.MapFrom(src => src.Tours))
|
||||||
|
.AfterMap((src, dest) =>
|
||||||
|
{
|
||||||
|
foreach (var tour in dest.SaleTours)
|
||||||
|
{
|
||||||
|
tour.SaleId = dest.Id;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
_mapper = new Mapper(config);
|
_mapper = new Mapper(config);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -23,7 +23,8 @@ 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>();
|
||||||
});
|
});
|
||||||
@@ -33,8 +34,7 @@ internal class TourStorageContract : ITourStorageContract
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
var query = _dbContext.Tours.AsQueryable();
|
return [.. _dbContext.Tours.Select(x => _mapper.Map<TourDataModel>(x))];
|
||||||
return [.. query.Select(x => _mapper.Map<TourDataModel>(x))];
|
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
@@ -43,12 +43,11 @@ internal class TourStorageContract : ITourStorageContract
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<TourHistoryDataModel> GetHistoryByTourId(string productId)
|
public List<TourHistoryDataModel> GetHistoryByTourId(string tourId)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
return [.. _dbContext.TourHistories.Where(x => x.TourId == productId).OrderByDescending(x => x.ChangeDate)
|
return [.. _dbContext.TourHistories.Where(x => x.TourId == tourId).OrderByDescending(x => x.ChangeDate).Select(x => _mapper.Map<TourHistoryDataModel>(x))];
|
||||||
.Select(x => _mapper.Map<TourHistoryDataModel>(x))];
|
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
@@ -83,22 +82,22 @@ internal class TourStorageContract : ITourStorageContract
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void AddElement(TourDataModel productDataModel)
|
public void AddElement(TourDataModel tourDataModel)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
_dbContext.Tours.Add(_mapper.Map<Tour>(productDataModel));
|
_dbContext.Tours.Add(_mapper.Map<Tour>(tourDataModel));
|
||||||
_dbContext.SaveChanges();
|
_dbContext.SaveChanges();
|
||||||
}
|
}
|
||||||
catch (InvalidOperationException ex) when (ex.TargetSite?.Name == "ThrowIdentityConflict")
|
catch (InvalidOperationException ex) when (ex.TargetSite?.Name == "ThrowIdentityConflict")
|
||||||
{
|
{
|
||||||
_dbContext.ChangeTracker.Clear();
|
_dbContext.ChangeTracker.Clear();
|
||||||
throw new ElementExistsException("Id", productDataModel.Id);
|
throw new ElementExistsException("Id", tourDataModel.Id);
|
||||||
}
|
}
|
||||||
catch (DbUpdateException ex) when (ex.InnerException is PostgresException { ConstraintName: "IX_Tours_TourName_IsDeleted" })
|
catch (DbUpdateException ex) when (ex.InnerException is PostgresException { ConstraintName: "IX_Tours_TourName" })
|
||||||
{
|
{
|
||||||
_dbContext.ChangeTracker.Clear();
|
_dbContext.ChangeTracker.Clear();
|
||||||
throw new ElementExistsException("TourName", productDataModel.TourName);
|
throw new ElementExistsException("TourName", tourDataModel.TourName);
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
@@ -107,39 +106,24 @@ internal class TourStorageContract : ITourStorageContract
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void UpdElement(TourDataModel productDataModel)
|
public void UpdElement(TourDataModel tourDataModel)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
var transaction = _dbContext.Database.BeginTransaction();
|
var element = GetTourById(tourDataModel.Id) ?? throw new ElementNotFoundException(tourDataModel.Id);
|
||||||
try
|
_dbContext.Tours.Update(_mapper.Map(tourDataModel, element));
|
||||||
{
|
|
||||||
var element = GetTourById(productDataModel.Id) ?? throw new ElementNotFoundException(productDataModel.Id);
|
|
||||||
if (element.Price != productDataModel.Price)
|
|
||||||
{
|
|
||||||
_dbContext.TourHistories.Add(new TourHistory() { TourId = element.Id, OldPrice = element.Price });
|
|
||||||
_dbContext.SaveChanges();
|
_dbContext.SaveChanges();
|
||||||
}
|
}
|
||||||
_dbContext.Tours.Update(_mapper.Map(productDataModel, element));
|
catch (ElementNotFoundException)
|
||||||
_dbContext.SaveChanges();
|
|
||||||
transaction.Commit();
|
|
||||||
}
|
|
||||||
catch
|
|
||||||
{
|
|
||||||
transaction.Rollback();
|
|
||||||
throw;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
catch (DbUpdateException ex) when (ex.InnerException is PostgresException { ConstraintName: "IX_Tours_TourName_IsDeleted" })
|
|
||||||
{
|
|
||||||
_dbContext.ChangeTracker.Clear();
|
|
||||||
throw new ElementExistsException("TourName", productDataModel.TourName);
|
|
||||||
}
|
|
||||||
catch (Exception ex) when (ex is ElementDeletedException || ex is ElementNotFoundException)
|
|
||||||
{
|
{
|
||||||
_dbContext.ChangeTracker.Clear();
|
_dbContext.ChangeTracker.Clear();
|
||||||
throw;
|
throw;
|
||||||
}
|
}
|
||||||
|
catch (DbUpdateException ex) when (ex.InnerException is PostgresException { ConstraintName: "IX_Tours_TourName" })
|
||||||
|
{
|
||||||
|
_dbContext.ChangeTracker.Clear();
|
||||||
|
throw new ElementExistsException("TourName", tourDataModel.TourName);
|
||||||
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
_dbContext.ChangeTracker.Clear();
|
_dbContext.ChangeTracker.Clear();
|
||||||
@@ -151,9 +135,11 @@ internal class TourStorageContract : ITourStorageContract
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
var element = GetTourById(id) ?? throw new ElementNotFoundException(id);
|
||||||
|
_dbContext.Tours.Remove(element);
|
||||||
_dbContext.SaveChanges();
|
_dbContext.SaveChanges();
|
||||||
}
|
}
|
||||||
catch (ElementNotFoundException ex)
|
catch (ElementNotFoundException)
|
||||||
{
|
{
|
||||||
_dbContext.ChangeTracker.Clear();
|
_dbContext.ChangeTracker.Clear();
|
||||||
throw;
|
throw;
|
||||||
@@ -165,5 +151,19 @@ internal class TourStorageContract : ITourStorageContract
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void ResElement(string id)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var element = GetTourById(id) ?? throw new ElementNotFoundException(id);
|
||||||
|
_dbContext.SaveChanges();
|
||||||
|
}
|
||||||
|
catch
|
||||||
|
{
|
||||||
|
_dbContext.ChangeTracker.Clear();
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private Tour? GetTourById(string id) => _dbContext.Tours.FirstOrDefault(x => x.Id == id);
|
private Tour? GetTourById(string id) => _dbContext.Tours.FirstOrDefault(x => x.Id == id);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -25,6 +25,8 @@ internal class MagicCarpetDbContext(IConfigurationDatabase configurationDatabase
|
|||||||
|
|
||||||
modelBuilder.Entity<Client>().HasIndex(x => x.PhoneNumber).IsUnique();
|
modelBuilder.Entity<Client>().HasIndex(x => x.PhoneNumber).IsUnique();
|
||||||
|
|
||||||
|
modelBuilder.Entity<Tour>().HasIndex(x => x.TourName).IsUnique();
|
||||||
|
|
||||||
modelBuilder.Entity<Post>()
|
modelBuilder.Entity<Post>()
|
||||||
.HasIndex(e => new { e.PostName, e.IsActual })
|
.HasIndex(e => new { e.PostName, e.IsActual })
|
||||||
.IsUnique()
|
.IsUnique()
|
||||||
|
|||||||
@@ -16,9 +16,9 @@ internal class Employee
|
|||||||
|
|
||||||
public required string FIO { get; set; }
|
public required string FIO { get; set; }
|
||||||
|
|
||||||
public required string PostId { get; set; }
|
public string Email { get; set; }
|
||||||
|
|
||||||
public string? Email { get; set; }
|
public string PostId { get; set; }
|
||||||
|
|
||||||
public DateTime BirthDate { get; set; }
|
public DateTime BirthDate { get; set; }
|
||||||
|
|
||||||
|
|||||||
@@ -12,4 +12,5 @@ internal class Salary
|
|||||||
public required string EmployeeId { get; set; }
|
public required string EmployeeId { get; set; }
|
||||||
public DateTime SalaryDate { get; set; }
|
public DateTime SalaryDate { get; set; }
|
||||||
public double EmployeeSalary { get; set; }
|
public double EmployeeSalary { get; set; }
|
||||||
|
public Employee? Employee { get; set; }
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,6 +13,4 @@ internal class SaleTour
|
|||||||
public required string TourId { get; set; }
|
public required string TourId { get; set; }
|
||||||
|
|
||||||
public int Count { get; set; }
|
public int Count { get; set; }
|
||||||
public Sale? Sale { get; set; }
|
|
||||||
public Tour? Tour { get; set; }
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,11 +11,11 @@ namespace MagicCarpetDatabase.Models;
|
|||||||
|
|
||||||
internal class Tour
|
internal class Tour
|
||||||
{
|
{
|
||||||
public required string Id { get; set; } = Guid.NewGuid().ToString();
|
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 TourType Type { get; set; }
|
public required TourType Type { get; set; }
|
||||||
[ForeignKey("TourId")]
|
[ForeignKey("TourId")]
|
||||||
public List<SaleTour>? SaleTours { get; set; }
|
public List<SaleTour>? SaleTours { get; set; }
|
||||||
[ForeignKey("TourId")]
|
[ForeignKey("TourId")]
|
||||||
|
|||||||
@@ -156,7 +156,7 @@ internal class ClientStorageContractTests : BaseStorageContractTest
|
|||||||
{
|
{
|
||||||
var client = InsertClientToDatabaseAndReturn(Guid.NewGuid().ToString());
|
var client = InsertClientToDatabaseAndReturn(Guid.NewGuid().ToString());
|
||||||
var employeeId = Guid.NewGuid().ToString();
|
var employeeId = Guid.NewGuid().ToString();
|
||||||
MagicCarpetDbContext.Employees.Add(new Employee() { Id = employeeId, FIO = "test", PostId = Guid.NewGuid().ToString() });
|
MagicCarpetDbContext.Employees.Add(new Employee() { Id = employeeId, FIO = "test", PostId = Guid.NewGuid().ToString(), Email = "abc@gmail.com" });
|
||||||
MagicCarpetDbContext.Sales.Add(new Sale() { Id = Guid.NewGuid().ToString(), EmployeeId = employeeId, ClientId = client.Id, Sum = 10, DiscountType = DiscountType.None, Discount = 0 });
|
MagicCarpetDbContext.Sales.Add(new Sale() { Id = Guid.NewGuid().ToString(), EmployeeId = employeeId, ClientId = client.Id, Sum = 10, DiscountType = DiscountType.None, Discount = 0 });
|
||||||
MagicCarpetDbContext.Sales.Add(new Sale() { Id = Guid.NewGuid().ToString(), EmployeeId = employeeId, ClientId = client.Id, Sum = 10, DiscountType = DiscountType.None, Discount = 0 });
|
MagicCarpetDbContext.Sales.Add(new Sale() { Id = Guid.NewGuid().ToString(), EmployeeId = employeeId, ClientId = client.Id, Sum = 10, DiscountType = DiscountType.None, Discount = 0 });
|
||||||
MagicCarpetDbContext.SaveChanges();
|
MagicCarpetDbContext.SaveChanges();
|
||||||
|
|||||||
@@ -31,9 +31,9 @@ class EmployeeStorageContractTests : BaseStorageContractTest
|
|||||||
[Test]
|
[Test]
|
||||||
public void Try_GetList_WhenHaveRecords_Test()
|
public void Try_GetList_WhenHaveRecords_Test()
|
||||||
{
|
{
|
||||||
var employee = InsertEmployeeToDatabaseAndReturn(Guid.NewGuid().ToString(), "fio 1");
|
var employee = InsertEmployeeToDatabaseAndReturn(Guid.NewGuid().ToString(), "fio 1", "abc@gmail.com");
|
||||||
InsertEmployeeToDatabaseAndReturn(Guid.NewGuid().ToString(), "fio 2");
|
InsertEmployeeToDatabaseAndReturn(Guid.NewGuid().ToString(), "fio 2", "abc@gmail.com");
|
||||||
InsertEmployeeToDatabaseAndReturn(Guid.NewGuid().ToString(), "fio 3");
|
InsertEmployeeToDatabaseAndReturn(Guid.NewGuid().ToString(), "fio 3", "abc@gmail.com");
|
||||||
var list = _employeeStorageContract.GetList();
|
var list = _employeeStorageContract.GetList();
|
||||||
Assert.That(list, Is.Not.Null);
|
Assert.That(list, Is.Not.Null);
|
||||||
Assert.That(list, Has.Count.EqualTo(3));
|
Assert.That(list, Has.Count.EqualTo(3));
|
||||||
@@ -52,9 +52,9 @@ class EmployeeStorageContractTests : BaseStorageContractTest
|
|||||||
public void Try_GetList_ByPostId_Test()
|
public void Try_GetList_ByPostId_Test()
|
||||||
{
|
{
|
||||||
var postId = Guid.NewGuid().ToString();
|
var postId = Guid.NewGuid().ToString();
|
||||||
InsertEmployeeToDatabaseAndReturn(Guid.NewGuid().ToString(), "fio 1", postId);
|
InsertEmployeeToDatabaseAndReturn(Guid.NewGuid().ToString(), "fio 1", "abc@gmail.com", postId);
|
||||||
InsertEmployeeToDatabaseAndReturn(Guid.NewGuid().ToString(), "fio 2", postId);
|
InsertEmployeeToDatabaseAndReturn(Guid.NewGuid().ToString(), "fio 2", "abc@gmail.com", postId);
|
||||||
InsertEmployeeToDatabaseAndReturn(Guid.NewGuid().ToString(), "fio 3");
|
InsertEmployeeToDatabaseAndReturn(Guid.NewGuid().ToString(), "fio 3", "abc@gmail.com");
|
||||||
var list = _employeeStorageContract.GetList(postId: postId);
|
var list = _employeeStorageContract.GetList(postId: postId);
|
||||||
Assert.That(list, Is.Not.Null);
|
Assert.That(list, Is.Not.Null);
|
||||||
Assert.That(list, Has.Count.EqualTo(2));
|
Assert.That(list, Has.Count.EqualTo(2));
|
||||||
@@ -64,10 +64,10 @@ class EmployeeStorageContractTests : BaseStorageContractTest
|
|||||||
[Test]
|
[Test]
|
||||||
public void Try_GetList_ByBirthDate_Test()
|
public void Try_GetList_ByBirthDate_Test()
|
||||||
{
|
{
|
||||||
InsertEmployeeToDatabaseAndReturn(Guid.NewGuid().ToString(), "fio 1", birthDate: DateTime.UtcNow.AddYears(-25));
|
InsertEmployeeToDatabaseAndReturn(Guid.NewGuid().ToString(), "fio 1", "abc@gmail.com", birthDate: DateTime.UtcNow.AddYears(-25));
|
||||||
InsertEmployeeToDatabaseAndReturn(Guid.NewGuid().ToString(), "fio 2", birthDate: DateTime.UtcNow.AddYears(-21));
|
InsertEmployeeToDatabaseAndReturn(Guid.NewGuid().ToString(), "fio 2", "abc@gmail.com", birthDate: DateTime.UtcNow.AddYears(-21));
|
||||||
InsertEmployeeToDatabaseAndReturn(Guid.NewGuid().ToString(), "fio 3", birthDate: DateTime.UtcNow.AddYears(-20));
|
InsertEmployeeToDatabaseAndReturn(Guid.NewGuid().ToString(), "fio 3", "abc@gmail.com", birthDate: DateTime.UtcNow.AddYears(-20));
|
||||||
InsertEmployeeToDatabaseAndReturn(Guid.NewGuid().ToString(), "fio 4", birthDate: DateTime.UtcNow.AddYears(-19));
|
InsertEmployeeToDatabaseAndReturn(Guid.NewGuid().ToString(), "fio 4", "abc@gmail.com", birthDate: DateTime.UtcNow.AddYears(-19));
|
||||||
var list = _employeeStorageContract.GetList(fromBirthDate: DateTime.UtcNow.AddYears(-21).AddMinutes(-1), toBirthDate: DateTime.UtcNow.AddYears(-20).AddMinutes(1));
|
var list = _employeeStorageContract.GetList(fromBirthDate: DateTime.UtcNow.AddYears(-21).AddMinutes(-1), toBirthDate: DateTime.UtcNow.AddYears(-20).AddMinutes(1));
|
||||||
Assert.That(list, Is.Not.Null);
|
Assert.That(list, Is.Not.Null);
|
||||||
Assert.That(list, Has.Count.EqualTo(2));
|
Assert.That(list, Has.Count.EqualTo(2));
|
||||||
@@ -76,10 +76,10 @@ class EmployeeStorageContractTests : BaseStorageContractTest
|
|||||||
[Test]
|
[Test]
|
||||||
public void Try_GetList_ByEmploymentDate_Test()
|
public void Try_GetList_ByEmploymentDate_Test()
|
||||||
{
|
{
|
||||||
InsertEmployeeToDatabaseAndReturn(Guid.NewGuid().ToString(), "fio 1", employmentDate: DateTime.UtcNow.AddDays(-2));
|
InsertEmployeeToDatabaseAndReturn(Guid.NewGuid().ToString(), "fio 1", "abc@gmail.com", employmentDate: DateTime.UtcNow.AddDays(-2));
|
||||||
InsertEmployeeToDatabaseAndReturn(Guid.NewGuid().ToString(), "fio 2", employmentDate: DateTime.UtcNow.AddDays(-1));
|
InsertEmployeeToDatabaseAndReturn(Guid.NewGuid().ToString(), "fio 2", "abc@gmail.com", employmentDate: DateTime.UtcNow.AddDays(-1));
|
||||||
InsertEmployeeToDatabaseAndReturn(Guid.NewGuid().ToString(), "fio 3", employmentDate: DateTime.UtcNow.AddDays(1));
|
InsertEmployeeToDatabaseAndReturn(Guid.NewGuid().ToString(), "fio 3", "abc@gmail.com", employmentDate: DateTime.UtcNow.AddDays(1));
|
||||||
InsertEmployeeToDatabaseAndReturn(Guid.NewGuid().ToString(), "fio 4", employmentDate: DateTime.UtcNow.AddDays(2));
|
InsertEmployeeToDatabaseAndReturn(Guid.NewGuid().ToString(), "fio 4", "abc@gmail.com", employmentDate: DateTime.UtcNow.AddDays(2));
|
||||||
var list = _employeeStorageContract.GetList(fromEmploymentDate: DateTime.UtcNow.AddDays(-1).AddMinutes(-1), toEmploymentDate: DateTime.UtcNow.AddDays(1).AddMinutes(1));
|
var list = _employeeStorageContract.GetList(fromEmploymentDate: DateTime.UtcNow.AddDays(-1).AddMinutes(-1), toEmploymentDate: DateTime.UtcNow.AddDays(1).AddMinutes(1));
|
||||||
Assert.That(list, Is.Not.Null);
|
Assert.That(list, Is.Not.Null);
|
||||||
Assert.That(list, Has.Count.EqualTo(2));
|
Assert.That(list, Has.Count.EqualTo(2));
|
||||||
@@ -89,10 +89,10 @@ class EmployeeStorageContractTests : BaseStorageContractTest
|
|||||||
public void Try_GetList_ByAllParameters_Test()
|
public void Try_GetList_ByAllParameters_Test()
|
||||||
{
|
{
|
||||||
var postId = Guid.NewGuid().ToString();
|
var postId = Guid.NewGuid().ToString();
|
||||||
InsertEmployeeToDatabaseAndReturn(Guid.NewGuid().ToString(), "fio 1", postId, birthDate: DateTime.UtcNow.AddYears(-25), employmentDate: DateTime.UtcNow.AddDays(-2));
|
InsertEmployeeToDatabaseAndReturn(Guid.NewGuid().ToString(), "fio 1", "abc@gmail.com", postId, birthDate: DateTime.UtcNow.AddYears(-25), employmentDate: DateTime.UtcNow.AddDays(-2));
|
||||||
InsertEmployeeToDatabaseAndReturn(Guid.NewGuid().ToString(), "fio 2", postId, birthDate: DateTime.UtcNow.AddYears(-22), employmentDate: DateTime.UtcNow.AddDays(-1));
|
InsertEmployeeToDatabaseAndReturn(Guid.NewGuid().ToString(), "fio 2", "abc@gmail.com", postId, birthDate: DateTime.UtcNow.AddYears(-22), employmentDate: DateTime.UtcNow.AddDays(-1));
|
||||||
InsertEmployeeToDatabaseAndReturn(Guid.NewGuid().ToString(), "fio 3", postId, birthDate: DateTime.UtcNow.AddYears(-21), employmentDate: DateTime.UtcNow.AddDays(-1));
|
InsertEmployeeToDatabaseAndReturn(Guid.NewGuid().ToString(), "fio 3", "abc@gmail.com", postId, birthDate: DateTime.UtcNow.AddYears(-21), employmentDate: DateTime.UtcNow.AddDays(-1));
|
||||||
InsertEmployeeToDatabaseAndReturn(Guid.NewGuid().ToString(), "fio 4", birthDate: DateTime.UtcNow.AddYears(-20), employmentDate: DateTime.UtcNow.AddDays(1));
|
InsertEmployeeToDatabaseAndReturn(Guid.NewGuid().ToString(), "fio 4", "abc@gmail.com", birthDate: DateTime.UtcNow.AddYears(-20), employmentDate: DateTime.UtcNow.AddDays(1));
|
||||||
var list = _employeeStorageContract.GetList(postId: postId, fromBirthDate: DateTime.UtcNow.AddYears(-21).AddMinutes(-1), toBirthDate: DateTime.UtcNow.AddYears(-20).AddMinutes(1),
|
var list = _employeeStorageContract.GetList(postId: postId, fromBirthDate: DateTime.UtcNow.AddYears(-21).AddMinutes(-1), toBirthDate: DateTime.UtcNow.AddYears(-20).AddMinutes(1),
|
||||||
fromEmploymentDate: DateTime.UtcNow.AddDays(-1).AddMinutes(-1), toEmploymentDate: DateTime.UtcNow.AddDays(1).AddMinutes(1));
|
fromEmploymentDate: DateTime.UtcNow.AddDays(-1).AddMinutes(-1), toEmploymentDate: DateTime.UtcNow.AddDays(1).AddMinutes(1));
|
||||||
Assert.That(list, Is.Not.Null);
|
Assert.That(list, Is.Not.Null);
|
||||||
@@ -188,9 +188,9 @@ class EmployeeStorageContractTests : BaseStorageContractTest
|
|||||||
Assert.That(() => _employeeStorageContract.DelElement(employee.Id), Throws.TypeOf<ElementNotFoundException>());
|
Assert.That(() => _employeeStorageContract.DelElement(employee.Id), Throws.TypeOf<ElementNotFoundException>());
|
||||||
}
|
}
|
||||||
|
|
||||||
private Employee InsertEmployeeToDatabaseAndReturn(string id, string fio = "test", string? email = null,string ? postId = null, DateTime? birthDate = null, DateTime? employmentDate = null, bool isDeleted = false)
|
private Employee InsertEmployeeToDatabaseAndReturn(string id, string fio = "test", string email = "abc@mail.ru",string ? postId = null, DateTime? birthDate = null, DateTime? employmentDate = null, bool isDeleted = false)
|
||||||
{
|
{
|
||||||
var employee = new Employee() { Id = id, FIO = fio, PostId = postId ?? Guid.NewGuid().ToString(), Email = email, BirthDate = birthDate ?? DateTime.UtcNow.AddYears(-20), EmploymentDate = employmentDate ?? DateTime.UtcNow, IsDeleted = isDeleted };
|
var employee = new Employee() { Id = id, FIO = fio, Email = email, PostId = postId ?? Guid.NewGuid().ToString(), BirthDate = birthDate ?? DateTime.UtcNow.AddYears(-20), EmploymentDate = employmentDate ?? DateTime.UtcNow, IsDeleted = isDeleted };
|
||||||
MagicCarpetDbContext.Employees.Add(employee);
|
MagicCarpetDbContext.Employees.Add(employee);
|
||||||
MagicCarpetDbContext.SaveChanges();
|
MagicCarpetDbContext.SaveChanges();
|
||||||
return employee;
|
return employee;
|
||||||
@@ -211,8 +211,8 @@ class EmployeeStorageContractTests : BaseStorageContractTest
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private static EmployeeDataModel CreateModel(string id, string fio = "fio", string? postId = null, DateTime? birthDate = null, DateTime? employmentDate = null, string email = "email", bool isDeleted = false) =>
|
private static EmployeeDataModel CreateModel(string id, string fio = "fio", string email = "abc@mail.ru", string? postId = null, DateTime? birthDate = null, DateTime? employmentDate = null, bool isDeleted = false) =>
|
||||||
new(id, fio, postId ?? Guid.NewGuid().ToString(), email, birthDate ?? DateTime.UtcNow.AddYears(-20), employmentDate ?? DateTime.UtcNow, isDeleted);
|
new(id, fio, email, postId ?? Guid.NewGuid().ToString(), birthDate ?? DateTime.UtcNow.AddYears(-20), employmentDate ?? DateTime.UtcNow, isDeleted);
|
||||||
|
|
||||||
private Employee? GetEmployeeFromDatabase(string id) => MagicCarpetDbContext.Employees.FirstOrDefault(x => x.Id == id);
|
private Employee? GetEmployeeFromDatabase(string id) => MagicCarpetDbContext.Employees.FirstOrDefault(x => x.Id == id);
|
||||||
|
|
||||||
|
|||||||
@@ -110,9 +110,9 @@ internal class SalaryStorageContractTests : BaseStorageContractTest
|
|||||||
AssertElement(GetSalaryFromDatabaseByEmployeeId(_employee.Id), salary);
|
AssertElement(GetSalaryFromDatabaseByEmployeeId(_employee.Id), salary);
|
||||||
}
|
}
|
||||||
|
|
||||||
private Employee InsertEmployeeToDatabaseAndReturn(string employeeFIO = "fio")
|
private Employee InsertEmployeeToDatabaseAndReturn(string employeeFIO = "fio", string employeeEmail = "abc@mail.ru")
|
||||||
{
|
{
|
||||||
var employee = new Employee() { Id = Guid.NewGuid().ToString(), PostId = Guid.NewGuid().ToString(), FIO = employeeFIO, IsDeleted = false };
|
var employee = new Employee() { Id = Guid.NewGuid().ToString(), PostId = Guid.NewGuid().ToString(), FIO = employeeFIO, Email = employeeEmail, IsDeleted = false };
|
||||||
MagicCarpetDbContext.Employees.Add(employee);
|
MagicCarpetDbContext.Employees.Add(employee);
|
||||||
MagicCarpetDbContext.SaveChanges();
|
MagicCarpetDbContext.SaveChanges();
|
||||||
return employee;
|
return employee;
|
||||||
|
|||||||
@@ -24,7 +24,7 @@ internal class SaleStorageContractTests : BaseStorageContractTest
|
|||||||
[SetUp]
|
[SetUp]
|
||||||
public void SetUp()
|
public void SetUp()
|
||||||
{
|
{
|
||||||
_saleStorageContract = new SaleStorageContract(MagicCarpetDbContext);;
|
_saleStorageContract = new SaleStorageContract(MagicCarpetDbContext);
|
||||||
_client = InsertClientToDatabaseAndReturn();
|
_client = InsertClientToDatabaseAndReturn();
|
||||||
_employee = InsertEmployeeToDatabaseAndReturn();
|
_employee = InsertEmployeeToDatabaseAndReturn();
|
||||||
_tour = InsertTourToDatabaseAndReturn();
|
_tour = InsertTourToDatabaseAndReturn();
|
||||||
@@ -201,15 +201,15 @@ internal class SaleStorageContractTests : BaseStorageContractTest
|
|||||||
return client;
|
return client;
|
||||||
}
|
}
|
||||||
|
|
||||||
private Employee InsertEmployeeToDatabaseAndReturn(string fio = "test")
|
private Employee InsertEmployeeToDatabaseAndReturn(string fio = "test", string employeeEmail = "abc@gmail.com")
|
||||||
{
|
{
|
||||||
var employee = new Employee() { Id = Guid.NewGuid().ToString(), FIO = fio, PostId = Guid.NewGuid().ToString() };
|
var employee = new Employee() { Id = Guid.NewGuid().ToString(), FIO = fio, Email = employeeEmail, PostId = Guid.NewGuid().ToString() };
|
||||||
MagicCarpetDbContext.Employees.Add(employee);
|
MagicCarpetDbContext.Employees.Add(employee);
|
||||||
MagicCarpetDbContext.SaveChanges();
|
MagicCarpetDbContext.SaveChanges();
|
||||||
return employee;
|
return employee;
|
||||||
}
|
}
|
||||||
|
|
||||||
private Tour InsertTourToDatabaseAndReturn(string tourName = "test", TourType tourType = TourType.Beach, double price = 1, bool isDeleted = false)
|
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, Type = tourType, Price = price };
|
||||||
MagicCarpetDbContext.Tours.Add(tour);
|
MagicCarpetDbContext.Tours.Add(tour);
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ using MagicCarpetContracts.Exceptions;
|
|||||||
using MagicCarpetDatabase.Implementations;
|
using MagicCarpetDatabase.Implementations;
|
||||||
using MagicCarpetDatabase.Models;
|
using MagicCarpetDatabase.Models;
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using Moq;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
@@ -39,7 +40,7 @@ internal class TourStorageContractTests : BaseStorageContractTest
|
|||||||
var list = _tourStorageContract.GetList();
|
var list = _tourStorageContract.GetList();
|
||||||
Assert.That(list, Is.Not.Null);
|
Assert.That(list, Is.Not.Null);
|
||||||
Assert.That(list, Has.Count.EqualTo(3));
|
Assert.That(list, Has.Count.EqualTo(3));
|
||||||
AssertElement(list.First(), tour);
|
AssertElement(list.First(x => x.Id == tour.Id), tour);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Test]
|
[Test]
|
||||||
@@ -53,49 +54,59 @@ internal class TourStorageContractTests : BaseStorageContractTest
|
|||||||
[Test]
|
[Test]
|
||||||
public void Try_GetHistoryByTourId_WhenHaveRecords_Test()
|
public void Try_GetHistoryByTourId_WhenHaveRecords_Test()
|
||||||
{
|
{
|
||||||
var tour = InsertTourToDatabaseAndReturn(Guid.NewGuid().ToString());
|
var tour = InsertTourToDatabaseAndReturn(Guid.NewGuid().ToString(), "name 1");
|
||||||
AssertElement(_tourStorageContract.GetElementById(tour.Id), tour);
|
InsertTourHistoryToDatabaseAndReturn(tour.Id, 20, DateTime.UtcNow.AddDays(-1));
|
||||||
|
InsertTourHistoryToDatabaseAndReturn(tour.Id, 30, DateTime.UtcNow.AddMinutes(-10));
|
||||||
|
InsertTourHistoryToDatabaseAndReturn(tour.Id, 40, DateTime.UtcNow.AddDays(1));
|
||||||
|
var list = _tourStorageContract.GetHistoryByTourId(tour.Id);
|
||||||
|
Assert.That(list, Is.Not.Null);
|
||||||
|
Assert.That(list, Has.Count.EqualTo(3));
|
||||||
}
|
}
|
||||||
|
|
||||||
[Test]
|
[Test]
|
||||||
public void Try_GetHistoryByTourId_WhenNoRecords_Test()
|
public void Try_GetHistoryByTourId_WhenNoRecords_Test()
|
||||||
{
|
{
|
||||||
InsertTourToDatabaseAndReturn(Guid.NewGuid().ToString());
|
var tour = InsertTourToDatabaseAndReturn(Guid.NewGuid().ToString(), "name 1");
|
||||||
Assert.That(() => _tourStorageContract.GetElementById(Guid.NewGuid().ToString()), Is.Null);
|
InsertTourHistoryToDatabaseAndReturn(tour.Id, 20, DateTime.UtcNow.AddDays(-1));
|
||||||
|
InsertTourHistoryToDatabaseAndReturn(tour.Id, 30, DateTime.UtcNow.AddMinutes(-10));
|
||||||
|
InsertTourHistoryToDatabaseAndReturn(tour.Id, 40, DateTime.UtcNow.AddDays(1));
|
||||||
|
var list = _tourStorageContract.GetHistoryByTourId(Guid.NewGuid().ToString());
|
||||||
|
Assert.That(list, Is.Not.Null);
|
||||||
|
Assert.That(list, Has.Count.EqualTo(0));
|
||||||
}
|
}
|
||||||
|
|
||||||
[Test]
|
[Test]
|
||||||
public void Try_GetElementById_WhenHaveRecord_Test()
|
public void Try_GetElementById_WhenHaveRecord_Test()
|
||||||
{
|
{
|
||||||
var tour = InsertTourToDatabaseAndReturn(Guid.NewGuid().ToString(), "name1");
|
var tour = InsertTourToDatabaseAndReturn(Guid.NewGuid().ToString());
|
||||||
AssertElement(_tourStorageContract.GetElementById(tour.Id), tour);
|
AssertElement(_tourStorageContract.GetElementById(tour.Id), tour);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Test]
|
[Test]
|
||||||
public void Try_GetElementById_WhenNoRecord_Test()
|
public void Try_GetElementById_WhenNoRecord_Test()
|
||||||
{
|
{
|
||||||
InsertTourToDatabaseAndReturn(Guid.NewGuid().ToString(), "name1");
|
InsertTourToDatabaseAndReturn(Guid.NewGuid().ToString());
|
||||||
Assert.That(() => _tourStorageContract.GetElementById(Guid.NewGuid().ToString()), Is.Null);
|
Assert.That(() => _tourStorageContract.GetElementById(Guid.NewGuid().ToString()), Is.Null);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Test]
|
[Test]
|
||||||
public void Try_GetElementByName_WhenHaveRecord_Test()
|
public void Try_GetElementByName_WhenHaveRecord_Test()
|
||||||
{
|
{
|
||||||
var tour = InsertTourToDatabaseAndReturn(Guid.NewGuid().ToString(), "name1");
|
var tour = InsertTourToDatabaseAndReturn(Guid.NewGuid().ToString());
|
||||||
AssertElement(_tourStorageContract.GetElementByName(tour.TourName), tour);
|
AssertElement(_tourStorageContract.GetElementByName(tour.TourName), tour);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Test]
|
[Test]
|
||||||
public void Try_GetElementByName_WhenNoRecord_Test()
|
public void Try_GetElementByName_WhenNoRecord_Test()
|
||||||
{
|
{
|
||||||
InsertTourToDatabaseAndReturn(Guid.NewGuid().ToString(), "name1");
|
InsertTourToDatabaseAndReturn(Guid.NewGuid().ToString());
|
||||||
Assert.That(() => _tourStorageContract.GetElementByName("name"), Is.Null);
|
Assert.That(() => _tourStorageContract.GetElementByName("name"), Is.Null);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Test]
|
[Test]
|
||||||
public void Try_AddElement_Test()
|
public void Try_AddElement_Test()
|
||||||
{
|
{
|
||||||
var tour = CreateModel(Guid.NewGuid().ToString(), "tour1");
|
var tour = CreateModel(Guid.NewGuid().ToString());
|
||||||
_tourStorageContract.AddElement(tour);
|
_tourStorageContract.AddElement(tour);
|
||||||
AssertElement(GetTourFromDatabaseById(tour.Id), tour);
|
AssertElement(GetTourFromDatabaseById(tour.Id), tour);
|
||||||
}
|
}
|
||||||
@@ -103,33 +114,24 @@ internal class TourStorageContractTests : BaseStorageContractTest
|
|||||||
[Test]
|
[Test]
|
||||||
public void Try_AddElement_WhenHaveRecordWithSameId_Test()
|
public void Try_AddElement_WhenHaveRecordWithSameId_Test()
|
||||||
{
|
{
|
||||||
var tour = CreateModel(Guid.NewGuid().ToString(), "Name");
|
var tour = CreateModel(Guid.NewGuid().ToString());
|
||||||
InsertTourToDatabaseAndReturn(tour.Id);
|
InsertTourToDatabaseAndReturn(tour.Id, tourName: "name unique");
|
||||||
Assert.That(() => _tourStorageContract.AddElement(tour), Throws.TypeOf<ElementExistsException>());
|
Assert.That(() => _tourStorageContract.AddElement(tour), Throws.TypeOf<ElementExistsException>());
|
||||||
}
|
}
|
||||||
|
|
||||||
[Test]
|
[Test]
|
||||||
public void Try_AddElement_WhenHaveRecordWithSameName_Test()
|
public void Try_AddElement_WhenHaveRecordWithSameName_Test()
|
||||||
{
|
{
|
||||||
var tour = CreateModel(Guid.NewGuid().ToString(), "Name");
|
var tour = CreateModel(Guid.NewGuid().ToString(), "name unique");
|
||||||
InsertTourToDatabaseAndReturn(Guid.NewGuid().ToString(), tour.TourName);
|
InsertTourToDatabaseAndReturn(Guid.NewGuid().ToString(), tourName: tour.TourName);
|
||||||
Assert.That(() => _tourStorageContract.AddElement(tour), Throws.TypeOf<ElementExistsException>());
|
Assert.That(() => _tourStorageContract.AddElement(tour), Throws.TypeOf<ElementExistsException>());
|
||||||
}
|
}
|
||||||
|
|
||||||
[Test]
|
[Test]
|
||||||
public void Try_UpdElement_Test()
|
public void Try_UpdElement_Test()
|
||||||
{
|
{
|
||||||
var tour = CreateModel(Guid.NewGuid().ToString(), "Name");
|
var tour = CreateModel(Guid.NewGuid().ToString(), "new name", "country");
|
||||||
InsertTourToDatabaseAndReturn(tour.Id, tourName: tour.TourName);
|
InsertTourToDatabaseAndReturn(tour.Id);
|
||||||
_tourStorageContract.UpdElement(tour);
|
|
||||||
AssertElement(GetTourFromDatabaseById(tour.Id), tour);
|
|
||||||
}
|
|
||||||
|
|
||||||
[Test]
|
|
||||||
public void Try_UpdElement_WhenNoChangeName_Test()
|
|
||||||
{
|
|
||||||
var tour = CreateModel(Guid.NewGuid().ToString(), "Name");
|
|
||||||
InsertTourToDatabaseAndReturn(tour.Id, tour.TourName);
|
|
||||||
_tourStorageContract.UpdElement(tour);
|
_tourStorageContract.UpdElement(tour);
|
||||||
AssertElement(GetTourFromDatabaseById(tour.Id), tour);
|
AssertElement(GetTourFromDatabaseById(tour.Id), tour);
|
||||||
}
|
}
|
||||||
@@ -143,9 +145,9 @@ internal class TourStorageContractTests : BaseStorageContractTest
|
|||||||
[Test]
|
[Test]
|
||||||
public void Try_UpdElement_WhenHaveRecordWithSameName_Test()
|
public void Try_UpdElement_WhenHaveRecordWithSameName_Test()
|
||||||
{
|
{
|
||||||
var tour = CreateModel(Guid.NewGuid().ToString(), "Duplicate Name");
|
var tour = CreateModel(Guid.NewGuid().ToString(), "name unique");
|
||||||
InsertTourToDatabaseAndReturn(tour.Id, "Old Name");
|
InsertTourToDatabaseAndReturn(tour.Id, tourName: "name");
|
||||||
InsertTourToDatabaseAndReturn(Guid.NewGuid().ToString(), tour.TourName);
|
InsertTourToDatabaseAndReturn(Guid.NewGuid().ToString(), tourName: tour.TourName);
|
||||||
Assert.That(() => _tourStorageContract.UpdElement(tour), Throws.TypeOf<ElementExistsException>());
|
Assert.That(() => _tourStorageContract.UpdElement(tour), Throws.TypeOf<ElementExistsException>());
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -164,14 +166,22 @@ internal class TourStorageContractTests : BaseStorageContractTest
|
|||||||
Assert.That(() => _tourStorageContract.DelElement(Guid.NewGuid().ToString()), Throws.TypeOf<ElementNotFoundException>());
|
Assert.That(() => _tourStorageContract.DelElement(Guid.NewGuid().ToString()), Throws.TypeOf<ElementNotFoundException>());
|
||||||
}
|
}
|
||||||
|
|
||||||
private Tour InsertTourToDatabaseAndReturn(string id, string tourName = "test")
|
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 };
|
var tour = new Tour() { Id = id, TourName = tourName, TourCountry = tourCountry, Type = tourType, Price = price };
|
||||||
MagicCarpetDbContext.Tours.Add(tour);
|
MagicCarpetDbContext.Tours.Add(tour);
|
||||||
MagicCarpetDbContext.SaveChanges();
|
MagicCarpetDbContext.SaveChanges();
|
||||||
return tour;
|
return tour;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private TourHistory InsertTourHistoryToDatabaseAndReturn(string tourId, double price, DateTime changeDate)
|
||||||
|
{
|
||||||
|
var tourHistory = new TourHistory() { Id = Guid.NewGuid().ToString(), TourId = tourId, OldPrice = price, ChangeDate = changeDate };
|
||||||
|
MagicCarpetDbContext.TourHistories.Add(tourHistory);
|
||||||
|
MagicCarpetDbContext.SaveChanges();
|
||||||
|
return tourHistory;
|
||||||
|
}
|
||||||
|
|
||||||
private static void AssertElement(TourDataModel? actual, Tour expected)
|
private static void AssertElement(TourDataModel? actual, Tour expected)
|
||||||
{
|
{
|
||||||
Assert.That(actual, Is.Not.Null);
|
Assert.That(actual, Is.Not.Null);
|
||||||
@@ -180,13 +190,13 @@ 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.Price, Is.EqualTo(expected.Price));
|
|
||||||
Assert.That(actual.Type, Is.EqualTo(expected.Type));
|
Assert.That(actual.Type, Is.EqualTo(expected.Type));
|
||||||
|
Assert.That(actual.Price, Is.EqualTo(expected.Price));
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private static TourDataModel CreateModel(string id, string tourName = "test", string tourCountry = "country", double price = 1, TourType tourType = TourType.Sightseeing)
|
private static TourDataModel CreateModel(string id, string tourName = "test", string tourCountry = "country", TourType type = TourType.Beach, double price = 1)
|
||||||
=> new(id, tourName, tourCountry, price, tourType);
|
=> new(id, tourName, tourCountry, price, type);
|
||||||
|
|
||||||
private Tour? GetTourFromDatabaseById(string id) => MagicCarpetDbContext.Tours.FirstOrDefault(x => x.Id == id);
|
private Tour? GetTourFromDatabaseById(string id) => MagicCarpetDbContext.Tours.FirstOrDefault(x => x.Id == id);
|
||||||
|
|
||||||
@@ -198,8 +208,8 @@ 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.Price, Is.EqualTo(expected.Price));
|
|
||||||
Assert.That(actual.Type, Is.EqualTo(expected.Type));
|
Assert.That(actual.Type, Is.EqualTo(expected.Type));
|
||||||
|
Assert.That(actual.Price, Is.EqualTo(expected.Price));
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user