290 lines
12 KiB
C#
290 lines
12 KiB
C#
using Microsoft.Extensions.Logging;
|
|
using Moq;
|
|
using PuferFishBusinessLogic.Implementations;
|
|
using PuferFishContracts.DataModels;
|
|
using PuferFishContracts.Enums;
|
|
using PuferFishContracts.Exceptions;
|
|
using PuferFishContracts.StoragesContracts;
|
|
|
|
namespace PuferFishTests.BusinessLogicsContractsTests;
|
|
|
|
[TestFixture]
|
|
internal class PointsBusinessLogicContractTests
|
|
{
|
|
|
|
private PointsBusinessLogicContract _pointsBusinessLogicContract;
|
|
|
|
private Mock<IPointsStorageContract> _pointsStorageContract;
|
|
private Mock<ISaleStorageContract> _saleStorageContract;
|
|
private Mock<IBuyerStorageContract> _buyerStorageContract;
|
|
|
|
|
|
[OneTimeSetUp]
|
|
public void OneTimeSetUp()
|
|
{
|
|
_pointsStorageContract = new Mock<IPointsStorageContract>();
|
|
_buyerStorageContract = new Mock<IBuyerStorageContract>();
|
|
_saleStorageContract = new Mock<ISaleStorageContract>();
|
|
|
|
_pointsBusinessLogicContract = new PointsBusinessLogicContract(_pointsStorageContract.Object, _saleStorageContract.Object, _buyerStorageContract.Object, new Mock<ILogger>().Object);
|
|
}
|
|
|
|
[SetUp]
|
|
public void SetUp()
|
|
{
|
|
_pointsStorageContract.Reset();
|
|
}
|
|
|
|
[Test]
|
|
public void GetAllPoints_ReturnListOfRecords_Test()
|
|
{
|
|
// Arrange
|
|
var startDate = DateTime.UtcNow;
|
|
var endDate = DateTime.UtcNow.AddDays(1);
|
|
var listOriginal = new List<PointsDataModel>
|
|
{
|
|
new(Guid.NewGuid().ToString(), DateTime.UtcNow, 10),
|
|
new(Guid.NewGuid().ToString(), DateTime.UtcNow.AddDays(1), 14),
|
|
new(Guid.NewGuid().ToString(), DateTime.UtcNow.AddDays(-1), 30),
|
|
};
|
|
_pointsStorageContract.Setup(x => x.GetList(It.IsAny<DateTime>(), It.IsAny<DateTime>(), It.IsAny<string>())).Returns(listOriginal);
|
|
|
|
// Act
|
|
var list = _pointsBusinessLogicContract.GetAllPointsByPeriod(startDate, endDate);
|
|
|
|
// Assert
|
|
Assert.That(list, Is.Not.Null);
|
|
Assert.That(list, Is.EquivalentTo(listOriginal));
|
|
_pointsStorageContract.Verify(x => x.GetList(startDate, endDate, null), Times.Once);
|
|
}
|
|
|
|
[Test]
|
|
public void GetAllPoints_ReturnEmptyList_Test()
|
|
{
|
|
// Arrange
|
|
_pointsStorageContract.Setup(x => x.GetList(It.IsAny<DateTime>(), It.IsAny<DateTime>(), It.IsAny<string>())).Returns(new List<PointsDataModel>());
|
|
|
|
// Act
|
|
var list = _pointsBusinessLogicContract.GetAllPointsByPeriod(DateTime.UtcNow, DateTime.UtcNow.AddDays(1));
|
|
|
|
// Assert
|
|
Assert.That(list, Is.Not.Null);
|
|
Assert.That(list, Has.Count.EqualTo(0));
|
|
_pointsStorageContract.Verify(x => x.GetList(It.IsAny<DateTime>(), It.IsAny<DateTime>(), It.IsAny<string>()), Times.Once);
|
|
}
|
|
|
|
[Test]
|
|
public void GetAllPoints_IncorrectDates_ThrowException_Test()
|
|
{
|
|
// Arrange
|
|
var dateTime = DateTime.UtcNow;
|
|
|
|
// Act & Assert
|
|
Assert.That(() => _pointsBusinessLogicContract.GetAllPointsByPeriod(dateTime, dateTime), Throws.TypeOf<IncorrectDatesException>());
|
|
Assert.That(() => _pointsBusinessLogicContract.GetAllPointsByPeriod(dateTime, dateTime.AddSeconds(-1)), Throws.TypeOf<IncorrectDatesException>());
|
|
_pointsStorageContract.Verify(x => x.GetList(It.IsAny<DateTime>(), It.IsAny<DateTime>(), It.IsAny<string>()), Times.Never);
|
|
}
|
|
|
|
[Test]
|
|
public void GetAllPoints_ReturnNull_ThrowException_Test()
|
|
{
|
|
// Act & Assert
|
|
Assert.That(() => _pointsBusinessLogicContract.GetAllPointsByPeriod(DateTime.UtcNow, DateTime.UtcNow.AddDays(1)), Throws.TypeOf<NullListException>());
|
|
_pointsStorageContract.Verify(x => x.GetList(It.IsAny<DateTime>(), It.IsAny<DateTime>(), It.IsAny<string>()), Times.Once);
|
|
}
|
|
|
|
[Test]
|
|
public void GetAllPoints_StorageThrowError_ThrowException_Test()
|
|
{
|
|
// Arrange
|
|
_pointsStorageContract.Setup(x => x.GetList(It.IsAny<DateTime>(), It.IsAny<DateTime>(), It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException()));
|
|
|
|
// Act & Assert
|
|
Assert.That(() => _pointsBusinessLogicContract.GetAllPointsByPeriod(DateTime.UtcNow, DateTime.UtcNow.AddDays(1)), Throws.TypeOf<StorageException>());
|
|
_pointsStorageContract.Verify(x => x.GetList(It.IsAny<DateTime>(), It.IsAny<DateTime>(), It.IsAny<string>()), Times.Once);
|
|
}
|
|
|
|
[Test]
|
|
public void GetAllPointsByBuyer_ReturnListOfRecords_Test()
|
|
{
|
|
// Arrange
|
|
var startDate = DateTime.UtcNow;
|
|
var endDate = DateTime.UtcNow.AddDays(1);
|
|
var buyerId = Guid.NewGuid().ToString();
|
|
var listOriginal = new List<PointsDataModel>
|
|
{
|
|
new(Guid.NewGuid().ToString(), DateTime.UtcNow, 10),
|
|
new(Guid.NewGuid().ToString(), DateTime.UtcNow.AddDays(1), 14),
|
|
new(Guid.NewGuid().ToString(), DateTime.UtcNow.AddDays(-1), 30),
|
|
};
|
|
_pointsStorageContract.Setup(x => x.GetList(It.IsAny<DateTime>(), It.IsAny<DateTime>(), It.IsAny<string>())).Returns(listOriginal);
|
|
|
|
// Act
|
|
var list = _pointsBusinessLogicContract.GetAllPointsByPeriodByBuyer(startDate, endDate, buyerId);
|
|
|
|
// Assert
|
|
Assert.That(list, Is.Not.Null);
|
|
Assert.That(list, Is.EquivalentTo(listOriginal));
|
|
_pointsStorageContract.Verify(x => x.GetList(startDate, endDate, buyerId), Times.Once);
|
|
}
|
|
|
|
[Test]
|
|
public void GetAllPointsByBuyer_ReturnEmptyList_Test()
|
|
{
|
|
// Arrange
|
|
_pointsStorageContract.Setup(x => x.GetList(It.IsAny<DateTime>(), It.IsAny<DateTime>(), It.IsAny<string>())).Returns(new List<PointsDataModel>());
|
|
|
|
// Act
|
|
var list = _pointsBusinessLogicContract.GetAllPointsByPeriodByBuyer(DateTime.UtcNow, DateTime.UtcNow.AddDays(1), Guid.NewGuid().ToString());
|
|
|
|
// Assert
|
|
Assert.That(list, Is.Not.Null);
|
|
Assert.That(list, Has.Count.EqualTo(0));
|
|
_pointsStorageContract.Verify(x => x.GetList(It.IsAny<DateTime>(), It.IsAny<DateTime>(), It.IsAny<string>()), Times.Once);
|
|
}
|
|
|
|
[Test]
|
|
public void GetAllPointsByBuyer_IncorrectDates_ThrowException_Test()
|
|
{
|
|
// Arrange
|
|
var dateTime = DateTime.UtcNow;
|
|
|
|
// Act & Assert
|
|
Assert.That(() => _pointsBusinessLogicContract.GetAllPointsByPeriodByBuyer(dateTime, dateTime, Guid.NewGuid().ToString()), Throws.TypeOf<IncorrectDatesException>());
|
|
Assert.That(() => _pointsBusinessLogicContract.GetAllPointsByPeriodByBuyer(dateTime, dateTime.AddSeconds(-1), Guid.NewGuid().ToString()), Throws.TypeOf<IncorrectDatesException>());
|
|
_pointsStorageContract.Verify(x => x.GetList(It.IsAny<DateTime>(), It.IsAny<DateTime>(), It.IsAny<string>()), Times.Never);
|
|
}
|
|
|
|
[Test]
|
|
public void GetAllPointsByBuyer_BuyerIdIsNUllOrEmpty_ThrowException_Test()
|
|
{
|
|
// Act & Assert
|
|
Assert.That(() => _pointsBusinessLogicContract.GetAllPointsByPeriodByBuyer(DateTime.UtcNow, DateTime.UtcNow.AddDays(1), null), Throws.TypeOf<ArgumentNullException>());
|
|
Assert.That(() => _pointsBusinessLogicContract.GetAllPointsByPeriodByBuyer(DateTime.UtcNow, DateTime.UtcNow.AddDays(1), string.Empty), Throws.TypeOf<ArgumentNullException>());
|
|
_pointsStorageContract.Verify(x => x.GetList(It.IsAny<DateTime>(), It.IsAny<DateTime>(), It.IsAny<string>()), Times.Never);
|
|
}
|
|
|
|
[Test]
|
|
public void GetAllPointsByBuyer_BuyerIdIsNotGuid_ThrowException_Test()
|
|
{
|
|
// Act & Assert
|
|
Assert.That(() => _pointsBusinessLogicContract.GetAllPointsByPeriodByBuyer(DateTime.UtcNow, DateTime.UtcNow.AddDays(1), "buyerId"), Throws.TypeOf<ValidationException>());
|
|
_pointsStorageContract.Verify(x => x.GetList(It.IsAny<DateTime>(), It.IsAny<DateTime>(), It.IsAny<string>()), Times.Never);
|
|
}
|
|
|
|
[Test]
|
|
public void GetAllPointsByBuyer_ReturnNull_ThrowException_Test()
|
|
{
|
|
// Act & Assert
|
|
Assert.That(() => _pointsBusinessLogicContract.GetAllPointsByPeriodByBuyer(DateTime.UtcNow, DateTime.UtcNow.AddDays(1), Guid.NewGuid().ToString()), Throws.TypeOf<NullListException>());
|
|
_pointsStorageContract.Verify(x => x.GetList(It.IsAny<DateTime>(), It.IsAny<DateTime>(), It.IsAny<string>()), Times.Once);
|
|
}
|
|
|
|
[Test]
|
|
public void GetAllPointsByBuyer_StorageThrowError_ThrowException_Test()
|
|
{
|
|
// Arrange
|
|
_pointsStorageContract.Setup(x => x.GetList(It.IsAny<DateTime>(), It.IsAny<DateTime>(), It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException()));
|
|
|
|
// Act & Assert
|
|
Assert.That(() => _pointsBusinessLogicContract.GetAllPointsByPeriodByBuyer(DateTime.UtcNow, DateTime.UtcNow.AddDays(1), Guid.NewGuid().ToString()), Throws.TypeOf<StorageException>());
|
|
_pointsStorageContract.Verify(x => x.GetList(It.IsAny<DateTime>(), It.IsAny<DateTime>(), It.IsAny<string>()), Times.Once);
|
|
}
|
|
|
|
[Test]
|
|
public void CalculatePointsByMonth_CalculatePoints_Test()
|
|
{
|
|
// Arrange
|
|
var buyerId = Guid.NewGuid().ToString();
|
|
var expectedPoints = 100.0;
|
|
|
|
_buyerStorageContract.Setup(x => x.GetList())
|
|
.Returns(new List<BuyerDataModel> { new BuyerDataModel(buyerId, "Test", "123456789", expectedPoints) });
|
|
|
|
var calculatedPoints = 0.0;
|
|
_pointsStorageContract.Setup(x => x.AddElement(It.IsAny<PointsDataModel>()))
|
|
.Callback((PointsDataModel x) =>
|
|
{
|
|
calculatedPoints = x.Points;
|
|
});
|
|
|
|
// Act
|
|
_pointsBusinessLogicContract.CalculatePointsByMounth(DateTime.UtcNow);
|
|
|
|
// Assert
|
|
Assert.That(calculatedPoints, Is.EqualTo(expectedPoints));
|
|
}
|
|
|
|
[Test]
|
|
public void CalculatePointsByMonth_WithSeveralBuyers_Test()
|
|
{
|
|
// Arrange
|
|
var buyer1Id = Guid.NewGuid().ToString();
|
|
var buyer2Id = Guid.NewGuid().ToString();
|
|
var buyer3Id = Guid.NewGuid().ToString();
|
|
|
|
var buyers = new List<BuyerDataModel>
|
|
{
|
|
new(buyer1Id, "Test", "123456789", 0),
|
|
new(buyer2Id, "Test", "123456789", 0),
|
|
new(buyer3Id, "Test", "123456789", 0)
|
|
};
|
|
|
|
_buyerStorageContract.Setup(x => x.GetList())
|
|
.Returns(buyers);
|
|
|
|
// Act
|
|
_pointsBusinessLogicContract.CalculatePointsByMounth(DateTime.UtcNow);
|
|
|
|
// Assert
|
|
_pointsStorageContract.Verify(x => x.AddElement(It.IsAny<PointsDataModel>()), Times.Exactly(buyers.Count));
|
|
}
|
|
|
|
[Test]
|
|
public void CalculatePointsByMonth_WithoutPurchasesByBuyer_Test()
|
|
{
|
|
// Arrange
|
|
var buyerId = Guid.NewGuid().ToString();
|
|
|
|
// Убедитесь, что мок возвращает непустой список
|
|
_buyerStorageContract.Setup(x => x.GetList())
|
|
.Returns(new List<BuyerDataModel> { new BuyerDataModel(buyerId, "Test", "123456789", 0) });
|
|
|
|
// Установите начальное значение баллов в 0
|
|
var calculatedPoints = 0.0;
|
|
_pointsStorageContract.Setup(x => x.AddElement(It.IsAny<PointsDataModel>()))
|
|
.Callback((PointsDataModel x) =>
|
|
{
|
|
calculatedPoints = x.Points;
|
|
});
|
|
|
|
// Act
|
|
_pointsBusinessLogicContract.CalculatePointsByMounth(DateTime.UtcNow);
|
|
|
|
// Assert
|
|
Assert.That(calculatedPoints, Is.EqualTo(0));
|
|
}
|
|
|
|
[Test]
|
|
public void CalculatePointsByMonth_BuyerStorageReturnNull_ThrowException_Test()
|
|
{
|
|
// Arrange
|
|
_buyerStorageContract.Setup(x => x.GetList())
|
|
.Returns((List<BuyerDataModel>)null);
|
|
|
|
// Act & Assert
|
|
Assert.That(() => _pointsBusinessLogicContract.CalculatePointsByMounth(DateTime.UtcNow), Throws.TypeOf<NullListException>());
|
|
}
|
|
|
|
[Test]
|
|
public void CalculatePointsByMonth_BuyerStorageThrowException_ThrowException_Test()
|
|
{
|
|
// Arrange
|
|
_buyerStorageContract.Setup(x => x.GetList())
|
|
.Throws(new StorageException(new InvalidOperationException()));
|
|
|
|
// Act & Assert
|
|
Assert.That(() => _pointsBusinessLogicContract.CalculatePointsByMounth(DateTime.UtcNow), Throws.TypeOf<StorageException>());
|
|
}
|
|
}
|