изменение безнес логики при нехватке продукта

This commit is contained in:
2025-03-17 18:56:45 +04:00
parent f7b2442b3e
commit 39dada6bca
5 changed files with 32 additions and 46 deletions

View File

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

View File

@@ -13,11 +13,10 @@ using System.Threading.Tasks;
namespace MagicCarpetBusinessLogic.Implementations;
internal class TourBusinessLogicContract(ITourStorageContract tourStorageContract, IAgencyStorageContract agencyStorageContract,ILogger logger) : ITourBusinessLogicContract
internal class TourBusinessLogicContract(ITourStorageContract tourStorageContract, ILogger logger) : ITourBusinessLogicContract
{
private readonly ILogger _logger = logger;
private readonly ITourStorageContract _tourStorageContract = tourStorageContract;
private readonly IAgencyStorageContract _agencyStorageContract = agencyStorageContract;
public List<TourDataModel> GetAllTours()
{
_logger.LogInformation("GetAllTours");
@@ -57,11 +56,6 @@ internal class TourBusinessLogicContract(ITourStorageContract tourStorageContrac
_logger.LogInformation("New data: {json}", JsonSerializer.Serialize(tourDataModel));
ArgumentNullException.ThrowIfNull(tourDataModel);
tourDataModel.Validate();
if (!_agencyStorageContract.CheckComponents(tourDataModel))
{
throw new InsufficientException("Dont have tour in agency");
}
_tourStorageContract.AddElement(tourDataModel);
}
public void UpdateTour(TourDataModel tourDataModel)
@@ -69,11 +63,6 @@ internal class TourBusinessLogicContract(ITourStorageContract tourStorageContrac
_logger.LogInformation("Update data: {json}", JsonSerializer.Serialize(tourDataModel));
ArgumentNullException.ThrowIfNull(tourDataModel);
tourDataModel.Validate();
if (!_agencyStorageContract.CheckComponents(tourDataModel))
{
throw new InsufficientException("Dont have tour in agency");
}
_tourStorageContract.UpdElement(tourDataModel);
}
public void DeleteTour(string id)

View File

@@ -14,5 +14,5 @@ public interface IAgencyStorageContract
void AddElement(AgencyDataModel agencyDataModel);
void UpdElement(AgencyDataModel agencyDataModel);
void DelElement(string id);
bool CheckComponents(TourDataModel tourDataModel);
bool CheckComponents(SaleDataModel saleDataModel);
}

View File

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

View File

@@ -18,22 +18,18 @@ namespace MagicCarpetTests.BusinessLogicContractsTests;
internal class TourBusinessLogicContractTests
{
private TourBusinessLogicContract _tourBusinessLogicContract;
private Mock<IAgencyStorageContract> _agencyStorageContract;
private Mock<ITourStorageContract> _tourStorageContract;
[OneTimeSetUp]
public void OneTimeSetUp()
{
_tourStorageContract = new Mock<ITourStorageContract>();
_agencyStorageContract = new Mock<IAgencyStorageContract>();
_tourBusinessLogicContract = new TourBusinessLogicContract(_tourStorageContract.Object,
_agencyStorageContract.Object, new Mock<ILogger>().Object);
_tourBusinessLogicContract = new TourBusinessLogicContract(_tourStorageContract.Object, new Mock<ILogger>().Object);
}
[SetUp]
public void SetUp()
{
_agencyStorageContract.Reset();
_tourStorageContract.Reset();
}
@@ -270,12 +266,10 @@ internal class TourBusinessLogicContractTests
public void InsertTour_RecordWithExistsData_ThrowException_Test()
{
//Arrange
_agencyStorageContract.Setup(x => x.CheckComponents(It.IsAny<TourDataModel>())).Returns(true);
_tourStorageContract.Setup(x => x.AddElement(It.IsAny<TourDataModel>())).Throws(new ElementExistsException("Data", "Data"));
//Act&Assert
Assert.That(() => _tourBusinessLogicContract.InsertTour(new(Guid.NewGuid().ToString(), "name","country",10, TourType.Ski, [new TourSuppliesDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)],
[new TourAgencyDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)])), Throws.TypeOf<ElementExistsException>());
_agencyStorageContract.Verify(x => x.CheckComponents(It.IsAny<TourDataModel>()), Times.Once);
_tourStorageContract.Verify(x => x.AddElement(It.IsAny<TourDataModel>()), Times.Once);
}
@@ -299,11 +293,9 @@ internal class TourBusinessLogicContractTests
public void InsertTour_StorageThrowError_ThrowException_Test()
{
//Arrange
_agencyStorageContract.Setup(x => x.CheckComponents(It.IsAny<TourDataModel>())).Returns(false);
Assert.That(() => _tourBusinessLogicContract.InsertTour(new TourDataModel(Guid.NewGuid().ToString(), "name","country", 10, TourType.Ski,
[new TourSuppliesDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)], [new TourAgencyDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)])), Throws.TypeOf<InsufficientException>());
//Act&Assert
_agencyStorageContract.Verify(x => x.CheckComponents(It.IsAny<TourDataModel>()), Times.Once);
_tourStorageContract.Verify(x => x.UpdElement(It.IsAny<TourDataModel>()), Times.Never);
}
@@ -311,11 +303,9 @@ internal class TourBusinessLogicContractTests
public void InsertFurniture_InsufficientError_ThrowException_Test()
{
//Arrange
_agencyStorageContract.Setup(x => x.CheckComponents(It.IsAny<TourDataModel>())).Returns(false);
Assert.That(() => _tourBusinessLogicContract.InsertTour(new TourDataModel(Guid.NewGuid().ToString(), "name", "country", 10, TourType.Ski, [new TourSuppliesDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)],
[new TourAgencyDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)])),Throws.TypeOf<InsufficientException>());
//Act&Assert
_agencyStorageContract.Verify(x => x.CheckComponents(It.IsAny<TourDataModel>()), Times.Once);
_tourStorageContract.Verify(x => x.UpdElement(It.IsAny<TourDataModel>()), Times.Never);
}
@@ -326,7 +316,6 @@ internal class TourBusinessLogicContractTests
var flag = false;
var record = new TourDataModel(Guid.NewGuid().ToString(), "name", "country", 10, TourType.Ski, [new TourSuppliesDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)],
[new TourAgencyDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)]);
_agencyStorageContract.Setup(x => x.CheckComponents(It.IsAny<TourDataModel>())).Returns(true);
_tourStorageContract.Setup(x => x.UpdElement(It.IsAny<TourDataModel>()))
.Callback((TourDataModel x) =>
{
@@ -336,7 +325,6 @@ internal class TourBusinessLogicContractTests
//Act
_tourBusinessLogicContract.UpdateTour(record);
//Assert
_agencyStorageContract.Verify(x => x.CheckComponents(It.IsAny<TourDataModel>()), Times.Once);
_tourStorageContract.Verify(x => x.UpdElement(It.IsAny<TourDataModel>()), Times.Once);
Assert.That(flag);
}
@@ -345,12 +333,10 @@ internal class TourBusinessLogicContractTests
public void UpdateTour_RecordWithIncorrectData_ThrowException_Test()
{
//Arrange
_agencyStorageContract.Setup(x => x.CheckComponents(It.IsAny<TourDataModel>())).Returns(true);
_tourStorageContract.Setup(x => x.UpdElement(It.IsAny<TourDataModel>())).Throws(new ElementNotFoundException(""));
//Act&Assert
Assert.That(() => _tourBusinessLogicContract.UpdateTour(new(Guid.NewGuid().ToString(), "name", "country", 10, TourType.Ski, [new TourSuppliesDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)],
[new TourAgencyDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)])), Throws.TypeOf<ElementNotFoundException>());
_agencyStorageContract.Verify(x => x.CheckComponents(It.IsAny<TourDataModel>()), Times.Once);
_tourStorageContract.Verify(x => x.UpdElement(It.IsAny<TourDataModel>()), Times.Once);
}
@@ -358,12 +344,10 @@ internal class TourBusinessLogicContractTests
public void UpdateTour_RecordWithExistsData_ThrowException_Test()
{
//Arrange
_agencyStorageContract.Setup(x => x.CheckComponents(It.IsAny<TourDataModel>())).Returns(true);
_tourStorageContract.Setup(x => x.UpdElement(It.IsAny<TourDataModel>())).Throws(new ElementExistsException("Data", "Data"));
//Act&Assert
Assert.That(() => _tourBusinessLogicContract.UpdateTour(new(Guid.NewGuid().ToString(), "name", "country", 10, TourType.Ski, [new TourSuppliesDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)],
[new TourAgencyDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)])), Throws.TypeOf <ElementExistsException>());
_agencyStorageContract.Verify(x => x.CheckComponents(It.IsAny<TourDataModel>()), Times.Once);
_tourStorageContract.Verify(x => x.UpdElement(It.IsAny<TourDataModel>()), Times.Once);
}
@@ -388,27 +372,13 @@ internal class TourBusinessLogicContractTests
public void UpdateTour_StorageThrowError_ThrowException_Test()
{
//Arrange
_agencyStorageContract.Setup(x => x.CheckComponents(It.IsAny<TourDataModel>())).Returns(true);
_tourStorageContract.Setup(x => x.UpdElement(It.IsAny<TourDataModel>())).Throws(new StorageException(new InvalidOperationException()));
//Act&Assert
Assert.That(() => _tourBusinessLogicContract.UpdateTour(new(Guid.NewGuid().ToString(), "name", "country", 10, TourType.Ski, [new TourSuppliesDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)],
[new TourAgencyDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)])), Throws.TypeOf<StorageException>());
_agencyStorageContract.Verify(x => x.CheckComponents(It.IsAny<TourDataModel>()), Times.Once);
_tourStorageContract.Verify(x => x.UpdElement(It.IsAny<TourDataModel>()), Times.Once);
}
[Test]
public void UpdateFurniture_InsufficientError_ThrowException_Test()
{
//Arrange
_agencyStorageContract.Setup(x => x.CheckComponents(It.IsAny<TourDataModel>())).Returns(false);
//Act&Assert
Assert.That(() => _tourBusinessLogicContract.UpdateTour(new(Guid.NewGuid().ToString(), "name", "country", 10, TourType.Ski,
[new TourSuppliesDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)], [new TourAgencyDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)])), Throws.TypeOf<InsufficientException>());
_agencyStorageContract.Verify(x => x.CheckComponents(It.IsAny<TourDataModel>()), Times.Once);
_tourStorageContract.Verify(x => x.UpdElement(It.IsAny<TourDataModel>()), Times.Never);
}
[Test]
public void DeleteTour_CorrectRecord_Test()
{