875 lines
36 KiB
C#
Raw Permalink Normal View History

2025-02-26 23:49:16 +04:00
using Moq;
using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using CandyHouseBase.DataModels;
using CandyHouseBase.Enums;
using CandyHouseBase.Exceptions;
using CandyHouseBase.Extensions;
using CandyHouseBase.Implementations;
using CandyHouseBase.Interfaces.BusinessLogicsContracts;
using CandyHouseBase.Interfaces.StoragesContracts;
using Microsoft.Extensions.Logging;
namespace CandyHouseTests.BusinessLogicsContractsTests
{
[TestFixture]
internal class PekarBusinessLogicContractTests
{
private PekarBusinessLogicContract _pekarBusinessLogicContract;
private Mock<IPekarStorageContact> _pekarStorageContact;
private Mock<IProductStorageContact> _productStorageContact;
private Mock<IPositionStorageContact> _positionStorageContact;
[OneTimeSetUp]
public void OneTimeSetUp()
{
_pekarStorageContact = new Mock<IPekarStorageContact>();
_productStorageContact = new Mock<IProductStorageContact>();
_positionStorageContact = new Mock<IPositionStorageContact>();
_pekarBusinessLogicContract = new PekarBusinessLogicContract(
_pekarStorageContact.Object,
_productStorageContact.Object,
_positionStorageContact.Object,
new Mock<ILogger>().Object
);
}
[SetUp]
public void SetUp()
{
_pekarStorageContact.Reset();
_productStorageContact.Reset();
_positionStorageContact.Reset();
}
[Test]
public void GetAllPekars_ReturnsListOfRecords_Test()
{
// Arrange
var productId = Guid.NewGuid().ToString();
var ingredientId = Guid.NewGuid().ToString();
var pekars = new List<PekarDataModel>
{
new PekarDataModel(
Guid.NewGuid().ToString(),
"Ivan Ivanov",
2025-02-27 02:25:39 +04:00
Guid.NewGuid().ToString(), // Use Guid for Position
2025-02-26 23:49:16 +04:00
1.5m,
new List<ProductDataModel>
{
new ProductDataModel(
productId,
"Cake",
"Delicious cake",
new List<IngredientDataModel>
{
new IngredientDataModel(ingredientId, "Sugar", "kg", 100)
}
)
}
),
new PekarDataModel(
Guid.NewGuid().ToString(),
"Maria Petrova",
2025-02-27 02:25:39 +04:00
Guid.NewGuid().ToString(), // Use Guid for Position
2025-02-26 23:49:16 +04:00
1.8m,
new List<ProductDataModel>
{
new ProductDataModel(
Guid.NewGuid().ToString(),
"Pastry",
"Sweet pastry",
new List<IngredientDataModel>
{
new IngredientDataModel(Guid.NewGuid().ToString(), "Flour", "kg", 200)
}
)
}
)
};
_pekarStorageContact.Setup(x => x.GetList()).Returns(pekars);
_productStorageContact.Setup(x => x.GetElementById(productId)).Returns(pekars[0].ProductsItems[0]);
2025-02-27 02:25:39 +04:00
_positionStorageContact.Setup(x => x.GetElementById(pekars[0].Position))
.Returns(new PositionDataModel(pekars[0].Position, PositionType.Cool, "Baking position"));
_positionStorageContact.Setup(x => x.GetElementById(pekars[1].Position))
.Returns(new PositionDataModel(pekars[1].Position, PositionType.Cool, "Pastry Chef position"));
2025-02-26 23:49:16 +04:00
// Act
var list = _pekarBusinessLogicContract.GetAllPekars();
// Assert
Assert.That(list, Is.Not.Null);
Assert.That(list, Is.EquivalentTo(pekars));
Assert.That(
list.All(p =>
2025-02-27 02:25:39 +04:00
Guid.TryParse(p.Id, out _) && !p.FIO.IsEmpty() && Guid.TryParse(p.Position, out _) &&
2025-02-26 23:49:16 +04:00
p.BonusCoefficient > 0), Is.True);
}
[Test]
public void GetAllPekars_ReturnsEmptyList_Test()
{
// Arrange
_pekarStorageContact.Setup(x => x.GetList()).Returns(new List<PekarDataModel>());
// Act
var list = _pekarBusinessLogicContract.GetAllPekars();
// Assert
Assert.That(list, Is.Not.Null);
Assert.That(list, Has.Count.EqualTo(0));
_pekarStorageContact.Verify(x => x.GetList(), Times.Once);
}
[Test]
public void GetAllPekars_ReturnsNull_ThrowException_Test()
{
// Arrange
_pekarStorageContact.Setup(x => x.GetList()).Returns((List<PekarDataModel>)null);
// Act & Assert
Assert.That(() => _pekarBusinessLogicContract.GetAllPekars(), Throws.TypeOf<NullListException>());
_pekarStorageContact.Verify(x => x.GetList(), Times.Once);
}
[Test]
public void GetAllPekars_StorageThrowError_ThrowException_Test()
{
// Arrange
_pekarStorageContact.Setup(x => x.GetList()).Throws(new StorageException(new InvalidOperationException()));
// Act & Assert
Assert.That(() => _pekarBusinessLogicContract.GetAllPekars(), Throws.TypeOf<StorageException>());
_pekarStorageContact.Verify(x => x.GetList(), Times.Once);
}
[Test]
public void GetAllDataOfPekar_ReturnsListOfHistoryRecords_Test()
{
// Arrange
var pekarId = Guid.NewGuid().ToString();
var positionId = Guid.NewGuid().ToString();
2025-02-27 02:25:39 +04:00
var historyRecords = new List<PekarDataModel>
2025-02-26 23:49:16 +04:00
{
2025-02-27 02:25:39 +04:00
new PekarDataModel(pekarId, "Ivan Ivanov", positionId, 1.5m, new List<ProductDataModel>()),
new PekarDataModel(pekarId, "Ivan Ivanov", positionId, 1.7m, new List<ProductDataModel>())
2025-02-26 23:49:16 +04:00
};
var pekarsWithHistory = new List<PekarDataModel>
{
new PekarDataModel(
pekarId,
"Ivan Ivanov",
2025-02-27 02:25:39 +04:00
positionId,
2025-02-26 23:49:16 +04:00
1.8m,
new List<ProductDataModel>
{
new ProductDataModel(
Guid.NewGuid().ToString(),
"Cake",
"Delicious cake",
new List<IngredientDataModel>
{
new IngredientDataModel(Guid.NewGuid().ToString(), "Sugar", "kg", 100)
}
)
}
)
};
_pekarStorageContact.Setup(x => x.GetPekarWithHistory(pekarId)).Returns(pekarsWithHistory);
_positionStorageContact.Setup(x => x.GetElementById(positionId))
.Returns(new PositionDataModel(positionId, PositionType.Cool, "Baking position"));
// Act
var list = _pekarBusinessLogicContract.GetAllDataOfPekar(pekarId);
// Assert
Assert.That(list, Is.Not.Null);
2025-02-27 02:25:39 +04:00
Assert.That(2, Is.EqualTo(historyRecords.Count));
2025-02-26 23:49:16 +04:00
Assert.That(
list.All(h =>
Guid.TryParse(h.Id, out _) && !h.FIO.IsEmpty() && Guid.TryParse(h.Position, out _) &&
h.BonusCoefficient > 0), Is.True);
_pekarStorageContact.Verify(x => x.GetPekarWithHistory(pekarId), Times.Once);
_positionStorageContact.Verify(x => x.GetElementById(positionId), Times.AtLeastOnce);
}
[Test]
public void GetAllDataOfPekar_EmptyPekarId_ThrowException_Test()
{
// Act & Assert
Assert.That(() => _pekarBusinessLogicContract.GetAllDataOfPekar(null),
Throws.TypeOf<ArgumentNullException>());
Assert.That(() => _pekarBusinessLogicContract.GetAllDataOfPekar(string.Empty),
Throws.TypeOf<ArgumentNullException>());
_pekarStorageContact.Verify(x => x.GetPekarWithHistory(It.IsAny<string>()), Times.Never);
}
[Test]
public void GetAllDataOfPekar_NotFoundPekar_ThrowException_Test()
{
// Arrange
2025-02-27 02:25:39 +04:00
var pekarId = Guid.NewGuid().ToString();
2025-02-26 23:49:16 +04:00
_pekarStorageContact.Setup(x => x.GetPekarWithHistory(pekarId)).Returns(new List<PekarDataModel>());
// Act & Assert
Assert.That(() => _pekarBusinessLogicContract.GetAllDataOfPekar(pekarId),
Throws.TypeOf<ElementNotFoundException>());
_pekarStorageContact.Verify(x => x.GetPekarWithHistory(pekarId), Times.Once);
}
[Test]
public void GetAllDataOfPekar_StorageThrowError_ThrowException_Test()
{
// Arrange
_pekarStorageContact.Setup(x => x.GetPekarWithHistory(It.IsAny<string>()))
.Throws(new StorageException(new InvalidOperationException()));
// Act & Assert
Assert.That(() => _pekarBusinessLogicContract.GetAllDataOfPekar(Guid.NewGuid().ToString()),
Throws.TypeOf<StorageException>());
_pekarStorageContact.Verify(x => x.GetPekarWithHistory(It.IsAny<string>()), Times.Once);
}
[Test]
public void GetAllDataOfPekar_InvalidPosition_ThrowException_Test()
{
// Arrange
var pekarId = Guid.NewGuid().ToString();
2025-02-27 02:25:39 +04:00
var positionId = Guid.NewGuid().ToString();
2025-02-26 23:49:16 +04:00
var pekarsWithHistory = new List<PekarDataModel>
{
new PekarDataModel(
pekarId,
"Ivan Ivanov",
2025-02-27 02:25:39 +04:00
positionId,
2025-02-26 23:49:16 +04:00
1.8m,
new List<ProductDataModel>
{
new ProductDataModel(
Guid.NewGuid().ToString(),
"Cake",
"Delicious cake",
new List<IngredientDataModel>
{
new IngredientDataModel(Guid.NewGuid().ToString(), "Sugar", "kg", 100)
}
)
}
)
};
_pekarStorageContact.Setup(x => x.GetPekarWithHistory(pekarId)).Returns(pekarsWithHistory);
_positionStorageContact.Setup(x => x.GetElementById(positionId)).Returns((PositionDataModel)null);
// Act & Assert
Assert.That(() => _pekarBusinessLogicContract.GetAllDataOfPekar(pekarId),
Throws.TypeOf<ElementNotFoundException>());
_pekarStorageContact.Verify(x => x.GetPekarWithHistory(pekarId), Times.Once);
_positionStorageContact.Verify(x => x.GetElementById(positionId), Times.Once);
}
[Test]
public void GetPekarByData_ReturnsPekarById_Test()
{
// Arrange
var id = Guid.NewGuid().ToString();
var productId = Guid.NewGuid().ToString();
var ingredientId = Guid.NewGuid().ToString();
2025-02-27 02:25:39 +04:00
var positionId = Guid.NewGuid().ToString();
2025-02-26 23:49:16 +04:00
var pekar = new PekarDataModel(
id,
"Ivan Ivanov",
2025-02-27 02:25:39 +04:00
positionId,
2025-02-26 23:49:16 +04:00
1.5m,
new List<ProductDataModel>
{
new ProductDataModel(
productId,
"Cake",
"Delicious cake",
new List<IngredientDataModel>
{
new IngredientDataModel(ingredientId, "Sugar", "kg", 100)
}
)
}
);
_pekarStorageContact.Setup(x => x.GetElementById(id)).Returns(pekar);
_productStorageContact.Setup(x => x.GetElementById(productId)).Returns(pekar.ProductsItems[0]);
2025-02-27 02:25:39 +04:00
_positionStorageContact.Setup(x => x.GetElementById(positionId))
.Returns(new PositionDataModel(positionId, PositionType.Cool, "Baking position"));
2025-02-26 23:49:16 +04:00
// Act
var element = _pekarBusinessLogicContract.GetPekarByData(id);
// Assert
Assert.That(element, Is.Not.Null);
Assert.That(element.Id, Is.EqualTo(id));
Assert.That(Guid.TryParse(element.Id, out _), Is.True);
Assert.That(!element.FIO.IsEmpty());
Assert.That(Regex.IsMatch(element.FIO, @"^[A-Za-zА-Яа-яЁё\s\-]+$"), Is.True);
2025-02-27 02:25:39 +04:00
Assert.That(Guid.TryParse(element.Position, out _), Is.True);
2025-02-26 23:49:16 +04:00
Assert.That(element.BonusCoefficient > 0);
_pekarStorageContact.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Once);
}
[Test]
public void GetPekarByData_EmptyData_ThrowException_Test()
{
// Act & Assert
Assert.That(() => _pekarBusinessLogicContract.GetPekarByData(null), Throws.TypeOf<ArgumentNullException>());
Assert.That(() => _pekarBusinessLogicContract.GetPekarByData(string.Empty),
Throws.TypeOf<ArgumentNullException>());
_pekarStorageContact.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Never);
}
[Test]
public void GetPekarByData_NotFoundPekar_ThrowException_Test()
{
// Arrange
2025-02-27 02:25:39 +04:00
var id = Guid.NewGuid().ToString();
2025-02-26 23:49:16 +04:00
_pekarStorageContact.Setup(x => x.GetElementById(id)).Returns((PekarDataModel)null);
// Act & Assert
Assert.That(() => _pekarBusinessLogicContract.GetPekarByData(id),
Throws.TypeOf<ElementNotFoundException>());
2025-02-27 02:25:39 +04:00
_pekarStorageContact.Verify(x => x.GetElementById(id), Times.Once);
2025-02-26 23:49:16 +04:00
}
[Test]
public void GetPekarByData_StorageThrowError_ThrowException_Test()
{
// Arrange
_pekarStorageContact.Setup(x => x.GetElementById(It.IsAny<string>()))
.Throws(new StorageException(new InvalidOperationException()));
// Act & Assert
Assert.That(() => _pekarBusinessLogicContract.GetPekarByData(Guid.NewGuid().ToString()),
Throws.TypeOf<StorageException>());
_pekarStorageContact.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Once);
}
[Test]
public void InsertPekar_CorrectRecord_Test()
{
// Arrange
var productId = Guid.NewGuid().ToString();
var ingredientId = Guid.NewGuid().ToString();
2025-02-27 02:25:39 +04:00
var positionId = Guid.NewGuid().ToString();
2025-02-26 23:49:16 +04:00
var flag = false;
var pekar = new PekarDataModel(
Guid.NewGuid().ToString(),
"Ivan Ivanov",
2025-02-27 02:25:39 +04:00
positionId,
2025-02-26 23:49:16 +04:00
1.5m,
new List<ProductDataModel>
{
new ProductDataModel(
productId,
"Cake",
"Delicious cake",
new List<IngredientDataModel>
{
new IngredientDataModel(ingredientId, "Sugar", "kg", 100)
}
)
}
);
2025-02-27 02:25:39 +04:00
_pekarStorageContact.Setup(x => x.GetElementById(pekar.Id))
.Returns((PekarDataModel)null);
_pekarStorageContact.Setup(x => x.AddElement(It.Is<PekarDataModel>(p => p.Id == pekar.Id)))
2025-02-26 23:49:16 +04:00
.Callback((PekarDataModel x) =>
{
flag = x.Id == pekar.Id && x.FIO == pekar.FIO && x.Position == pekar.Position &&
x.BonusCoefficient == pekar.BonusCoefficient &&
x.ProductsItems.SequenceEqual(pekar.ProductsItems);
});
_productStorageContact.Setup(x => x.GetElementById(productId)).Returns(pekar.ProductsItems[0]);
2025-02-27 02:25:39 +04:00
_positionStorageContact.Setup(x => x.GetElementById(positionId))
.Returns(new PositionDataModel(positionId, PositionType.Cool, "Baking position"));
2025-02-26 23:49:16 +04:00
// Act
_pekarBusinessLogicContract.InsertPekar(pekar);
// Assert
2025-02-27 02:25:39 +04:00
_pekarStorageContact.Verify(x => x.AddElement(It.Is<PekarDataModel>(p => p.Id == pekar.Id)), Times.Once);
2025-02-26 23:49:16 +04:00
Assert.That(flag);
Assert.That(Guid.TryParse(pekar.Id, out _), Is.True);
Assert.That(!pekar.FIO.IsEmpty());
Assert.That(Regex.IsMatch(pekar.FIO, @"^[A-Za-zА-Яа-яЁё\s\-]+$"), Is.True);
2025-02-27 02:25:39 +04:00
Assert.That(Guid.TryParse(pekar.Position, out _), Is.True);
2025-02-26 23:49:16 +04:00
Assert.That(pekar.BonusCoefficient > 0);
}
[Test]
public void InsertPekar_RecordWithExistsData_ThrowException_Test()
{
// Arrange
2025-02-27 02:25:39 +04:00
var positionId = Guid.NewGuid().ToString();
var productId = Guid.NewGuid().ToString();
var ingredientId = Guid.NewGuid().ToString();
2025-02-26 23:49:16 +04:00
var pekar = new PekarDataModel(
Guid.NewGuid().ToString(),
"Ivan Ivanov",
2025-02-27 02:25:39 +04:00
positionId,
2025-02-26 23:49:16 +04:00
1.5m,
new List<ProductDataModel>
{
new ProductDataModel(
2025-02-27 02:25:39 +04:00
productId,
2025-02-26 23:49:16 +04:00
"Cake",
"Delicious cake",
new List<IngredientDataModel>
{
2025-02-27 02:25:39 +04:00
new IngredientDataModel(ingredientId, "Sugar", "kg", 100)
2025-02-26 23:49:16 +04:00
}
)
}
);
2025-02-27 02:25:39 +04:00
var existingPekar = new PekarDataModel(pekar.Id, "Old Name", positionId, 1.0m, new List<ProductDataModel>
{
new ProductDataModel(productId, "Old Cake", "Old cake", new List<IngredientDataModel>())
});
_pekarStorageContact.Setup(x => x.GetElementById(pekar.Id)).Returns(existingPekar);
2025-02-26 23:49:16 +04:00
_pekarStorageContact.Setup(x => x.AddElement(It.IsAny<PekarDataModel>()))
.Throws(new ElementExistsException("ID", pekar.Id));
2025-02-27 02:25:39 +04:00
_productStorageContact.Setup(x => x.GetElementById(productId))
.Returns(existingPekar.ProductsItems[0]); // Mock product existence
_positionStorageContact.Setup(x => x.GetElementById(positionId))
.Returns(new PositionDataModel(positionId, PositionType.Cool, "Baking position"));
2025-02-26 23:49:16 +04:00
// Act & Assert
Assert.That(() => _pekarBusinessLogicContract.InsertPekar(pekar), Throws.TypeOf<ElementExistsException>());
2025-02-27 02:25:39 +04:00
_pekarStorageContact.Verify(x => x.GetElementById(pekar.Id), Times.Once);
2025-02-26 23:49:16 +04:00
}
[Test]
public void InsertPekar_NullRecord_ThrowException_Test()
{
// Act & Assert
Assert.That(() => _pekarBusinessLogicContract.InsertPekar(null), Throws.TypeOf<ArgumentNullException>());
_pekarStorageContact.Verify(x => x.AddElement(It.IsAny<PekarDataModel>()), Times.Never);
}
[Test]
public void InsertPekar_InvalidRecord_ThrowException_Test()
{
// Act & Assert
Assert.That(() => _pekarBusinessLogicContract.InsertPekar(new PekarDataModel(
"",
2025-02-27 02:25:39 +04:00
"123",
Guid.NewGuid().ToString(),
2025-02-26 23:49:16 +04:00
-1.0m,
2025-02-27 02:25:39 +04:00
new List<ProductDataModel>
2025-02-26 23:49:16 +04:00
{
new ProductDataModel(
2025-02-27 02:25:39 +04:00
"",
"",
"",
2025-02-26 23:49:16 +04:00
new List<IngredientDataModel>
{
2025-02-27 02:25:39 +04:00
new IngredientDataModel("", "Sugar", "kg", -100)
2025-02-26 23:49:16 +04:00
}
)
}
)), Throws.TypeOf<ValidationException>());
_pekarStorageContact.Verify(x => x.AddElement(It.IsAny<PekarDataModel>()), Times.Never);
}
[Test]
public void InsertPekar_StorageThrowError_ThrowException_Test()
{
// Arrange
2025-02-27 02:25:39 +04:00
var positionId = Guid.NewGuid().ToString();
var productId = Guid.NewGuid().ToString();
var ingredientId = Guid.NewGuid().ToString();
2025-02-26 23:49:16 +04:00
var pekar = new PekarDataModel(
Guid.NewGuid().ToString(),
"Ivan Ivanov",
2025-02-27 02:25:39 +04:00
positionId,
2025-02-26 23:49:16 +04:00
1.5m,
new List<ProductDataModel>
{
new ProductDataModel(
2025-02-27 02:25:39 +04:00
productId,
2025-02-26 23:49:16 +04:00
"Cake",
"Delicious cake",
new List<IngredientDataModel>
{
2025-02-27 02:25:39 +04:00
new IngredientDataModel(ingredientId, "Sugar", "kg", 100)
2025-02-26 23:49:16 +04:00
}
)
}
);
2025-02-27 02:25:39 +04:00
_pekarStorageContact.Setup(x => x.GetElementById(pekar.Id)).Returns((PekarDataModel)null);
2025-02-26 23:49:16 +04:00
_pekarStorageContact.Setup(x => x.AddElement(It.IsAny<PekarDataModel>()))
.Throws(new StorageException(new InvalidOperationException()));
2025-02-27 02:25:39 +04:00
_productStorageContact.Setup(x => x.GetElementById(productId))
.Returns(pekar.ProductsItems[0]); // Mock product existence
_positionStorageContact.Setup(x => x.GetElementById(positionId))
.Returns(new PositionDataModel(positionId, PositionType.Cool, "Baking position"));
2025-02-26 23:49:16 +04:00
// Act & Assert
Assert.That(() => _pekarBusinessLogicContract.InsertPekar(pekar), Throws.TypeOf<StorageException>());
2025-02-27 02:25:39 +04:00
_pekarStorageContact.Verify(x => x.GetElementById(pekar.Id), Times.Once);
2025-02-26 23:49:16 +04:00
_pekarStorageContact.Verify(x => x.AddElement(It.IsAny<PekarDataModel>()), Times.Once);
}
[Test]
public void UpdatePekar_CorrectRecord_Test()
{
// Arrange
var productId = Guid.NewGuid().ToString();
var ingredientId = Guid.NewGuid().ToString();
2025-02-27 02:25:39 +04:00
var positionId = Guid.NewGuid().ToString();
2025-02-26 23:49:16 +04:00
var flag = false;
var pekar = new PekarDataModel(
Guid.NewGuid().ToString(),
"Ivan Ivanov",
2025-02-27 02:25:39 +04:00
positionId,
2025-02-26 23:49:16 +04:00
1.5m,
new List<ProductDataModel>
{
new ProductDataModel(
productId,
"Cake",
"Delicious cake",
new List<IngredientDataModel>
{
new IngredientDataModel(ingredientId, "Sugar", "kg", 100)
}
)
}
);
2025-02-27 02:25:39 +04:00
_pekarStorageContact.Setup(x => x.UpdateElement(It.Is<PekarDataModel>(p => p.Id == pekar.Id)))
2025-02-26 23:49:16 +04:00
.Callback((PekarDataModel x) =>
{
flag = x.Id == pekar.Id && x.FIO == pekar.FIO && x.Position == pekar.Position &&
x.BonusCoefficient == pekar.BonusCoefficient &&
x.ProductsItems.SequenceEqual(pekar.ProductsItems);
});
_productStorageContact.Setup(x => x.GetElementById(productId)).Returns(pekar.ProductsItems[0]);
2025-02-27 02:25:39 +04:00
_positionStorageContact.Setup(x => x.GetElementById(positionId))
.Returns(new PositionDataModel(positionId, PositionType.Cool, "Baking position"));
2025-02-26 23:49:16 +04:00
// Act
_pekarBusinessLogicContract.UpdatePekar(pekar);
// Assert
2025-02-27 02:25:39 +04:00
_pekarStorageContact.Verify(x => x.UpdateElement(It.Is<PekarDataModel>(p => p.Id == pekar.Id)), Times.Once);
2025-02-26 23:49:16 +04:00
Assert.That(flag);
Assert.That(Guid.TryParse(pekar.Id, out _), Is.True);
Assert.That(!pekar.FIO.IsEmpty());
Assert.That(Regex.IsMatch(pekar.FIO, @"^[A-Za-zА-Яа-яЁё\s\-]+$"), Is.True);
2025-02-27 02:25:39 +04:00
Assert.That(Guid.TryParse(pekar.Position, out _), Is.True);
2025-02-26 23:49:16 +04:00
Assert.That(pekar.BonusCoefficient > 0);
}
[Test]
public void UpdatePekar_RecordNotFound_ThrowException_Test()
{
// Arrange
2025-02-27 02:25:39 +04:00
var positionId = Guid.NewGuid().ToString();
2025-02-26 23:49:16 +04:00
var pekar = new PekarDataModel(
Guid.NewGuid().ToString(),
"Ivan Ivanov",
2025-02-27 02:25:39 +04:00
positionId,
2025-02-26 23:49:16 +04:00
1.5m,
new List<ProductDataModel>
{
new ProductDataModel(
Guid.NewGuid().ToString(),
"Cake",
"Delicious cake",
new List<IngredientDataModel>
{
new IngredientDataModel(Guid.NewGuid().ToString(), "Sugar", "kg", 100)
}
)
}
);
2025-02-27 02:25:39 +04:00
_productStorageContact.Setup(x => x.GetElementById(pekar.ProductsItems[0].Id))
.Returns(pekar.ProductsItems[0]);
_positionStorageContact.Setup(x => x.GetElementById(positionId))
.Returns(new PositionDataModel(positionId, PositionType.Cool, "Baking position"));
2025-02-26 23:49:16 +04:00
_pekarStorageContact.Setup(x => x.UpdateElement(It.IsAny<PekarDataModel>()))
.Throws(new ElementNotFoundException("Pekar not found"));
// Act & Assert
Assert.That(() => _pekarBusinessLogicContract.UpdatePekar(pekar),
Throws.TypeOf<ElementNotFoundException>());
_pekarStorageContact.Verify(x => x.UpdateElement(It.IsAny<PekarDataModel>()), Times.Once);
}
[Test]
public void UpdatePekar_NullRecord_ThrowException_Test()
{
// Act & Assert
Assert.That(() => _pekarBusinessLogicContract.UpdatePekar(null), Throws.TypeOf<ArgumentNullException>());
_pekarStorageContact.Verify(x => x.UpdateElement(It.IsAny<PekarDataModel>()), Times.Never);
}
[Test]
public void UpdatePekar_InvalidRecord_ThrowException_Test()
{
// Act & Assert
Assert.That(() => _pekarBusinessLogicContract.UpdatePekar(new PekarDataModel(
"",
"123",
2025-02-27 02:25:39 +04:00
Guid.NewGuid().ToString(),
-1.0m,
new List<ProductDataModel>
2025-02-26 23:49:16 +04:00
{
new ProductDataModel(
2025-02-27 02:25:39 +04:00
"",
"",
"",
2025-02-26 23:49:16 +04:00
new List<IngredientDataModel>
{
2025-02-27 02:25:39 +04:00
new IngredientDataModel("", "Sugar", "kg", -100)
2025-02-26 23:49:16 +04:00
}
)
}
)), Throws.TypeOf<ValidationException>());
_pekarStorageContact.Verify(x => x.UpdateElement(It.IsAny<PekarDataModel>()), Times.Never);
}
[Test]
public void UpdatePekar_StorageThrowError_ThrowException_Test()
{
// Arrange
2025-02-27 02:25:39 +04:00
var positionId = Guid.NewGuid().ToString();
2025-02-26 23:49:16 +04:00
var pekar = new PekarDataModel(
Guid.NewGuid().ToString(),
"Ivan Ivanov",
2025-02-27 02:25:39 +04:00
positionId,
2025-02-26 23:49:16 +04:00
1.5m,
new List<ProductDataModel>
{
new ProductDataModel(
Guid.NewGuid().ToString(),
"Cake",
"Delicious cake",
new List<IngredientDataModel>
{
new IngredientDataModel(Guid.NewGuid().ToString(), "Sugar", "kg", 100)
}
)
}
);
2025-02-27 02:25:39 +04:00
_pekarStorageContact.Setup(x => x.GetElementById(pekar.Id)).Returns(pekar); // Ensure Pekar exists
2025-02-26 23:49:16 +04:00
_pekarStorageContact.Setup(x => x.UpdateElement(It.IsAny<PekarDataModel>()))
.Throws(new StorageException(new InvalidOperationException()));
2025-02-27 02:25:39 +04:00
_positionStorageContact.Setup(x => x.GetElementById(positionId))
.Returns(new PositionDataModel(positionId, PositionType.Cool, "Baking position"));
_productStorageContact.Setup(x => x.GetElementById(pekar.ProductsItems[0].Id))
.Returns(pekar.ProductsItems[0]);
2025-02-26 23:49:16 +04:00
// Act & Assert
Assert.That(() => _pekarBusinessLogicContract.UpdatePekar(pekar), Throws.TypeOf<StorageException>());
_pekarStorageContact.Verify(x => x.UpdateElement(It.IsAny<PekarDataModel>()), Times.Once);
}
[Test]
public void DeletePekar_CorrectId_Test()
{
// Arrange
var id = Guid.NewGuid().ToString();
var productId = Guid.NewGuid().ToString();
var ingredientId = Guid.NewGuid().ToString();
var pekar = new PekarDataModel(
id,
"Ivan Ivanov",
2025-02-27 02:25:39 +04:00
Guid.NewGuid().ToString(),
2025-02-26 23:49:16 +04:00
1.5m,
new List<ProductDataModel>
{
new ProductDataModel(
productId,
"Cake",
"Delicious cake",
new List<IngredientDataModel>
{
new IngredientDataModel(ingredientId, "Sugar", "kg", 100)
}
)
}
);
var flag = false;
_pekarStorageContact.Setup(x => x.GetElementById(id)).Returns(pekar);
_pekarStorageContact.Setup(x => x.DeleteElement(id))
.Callback(() => { flag = true; });
_productStorageContact.Setup(x => x.GetElementById(productId)).Returns(pekar.ProductsItems[0]);
// Act
_pekarBusinessLogicContract.DeletePekar(id);
// Assert
_pekarStorageContact.Verify(x => x.DeleteElement(id), Times.Once);
Assert.That(flag);
Assert.That(Guid.TryParse(pekar.Id, out _), Is.True);
Assert.That(!pekar.FIO.IsEmpty());
Assert.That(Regex.IsMatch(pekar.FIO, @"^[A-Za-zА-Яа-яЁё\s\-]+$"), Is.True);
Assert.That(!pekar.Position.IsEmpty());
Assert.That(pekar.BonusCoefficient > 0);
}
[Test]
public void DeletePekar_RecordNotFound_ThrowException_Test()
{
// Arrange
2025-02-27 02:25:39 +04:00
var id = Guid.NewGuid().ToString();
2025-02-26 23:49:16 +04:00
_pekarStorageContact.Setup(x => x.GetElementById(id)).Returns((PekarDataModel)null);
// Act & Assert
Assert.That(() => _pekarBusinessLogicContract.DeletePekar(id), Throws.TypeOf<ElementNotFoundException>());
2025-02-27 02:25:39 +04:00
_pekarStorageContact.Verify(x => x.GetElementById(id), Times.Once);
2025-02-26 23:49:16 +04:00
}
[Test]
public void DeletePekar_NullOrEmptyId_ThrowException_Test()
{
// Act & Assert
Assert.That(() => _pekarBusinessLogicContract.DeletePekar(null), Throws.TypeOf<ArgumentNullException>());
Assert.That(() => _pekarBusinessLogicContract.DeletePekar(string.Empty),
Throws.TypeOf<ArgumentNullException>());
2025-02-27 02:25:39 +04:00
_pekarStorageContact.Verify(x => x.DeleteElement(It.IsAny<string>()), Times.Never);
2025-02-26 23:49:16 +04:00
}
[Test]
public void DeletePekar_InvalidId_ThrowException_Test()
{
2025-02-27 02:25:39 +04:00
// Arrange
var id = "invalid";
// No need to setup GetElementById for invalid ID since validation should prevent it
2025-02-26 23:49:16 +04:00
// Act & Assert
2025-02-27 02:25:39 +04:00
Assert.That(() => _pekarBusinessLogicContract.DeletePekar(id), Throws.TypeOf<ValidationException>());
_pekarStorageContact.Verify(x => x.GetElementById(id), Times.Never);
2025-02-26 23:49:16 +04:00
}
[Test]
public void DeletePekar_StorageThrowError_ThrowException_Test()
{
// Arrange
2025-02-27 02:25:39 +04:00
var id = Guid.NewGuid().ToString();
var pekar = new PekarDataModel(
id,
"Ivan Ivanov",
Guid.NewGuid().ToString(),
1.5m,
new List<ProductDataModel>()
);
_pekarStorageContact.Setup(x => x.GetElementById(id)).Returns(pekar);
_pekarStorageContact.Setup(x => x.DeleteElement(id))
2025-02-26 23:49:16 +04:00
.Throws(new StorageException(new InvalidOperationException()));
// Act & Assert
2025-02-27 02:25:39 +04:00
Assert.That(() => _pekarBusinessLogicContract.DeletePekar(id), Throws.TypeOf<StorageException>());
_pekarStorageContact.Verify(x => x.GetElementById(id), Times.Once);
_pekarStorageContact.Verify(x => x.DeleteElement(id), Times.Once);
2025-02-26 23:49:16 +04:00
}
[Test]
public void RestorePekar_CorrectId_Test()
{
// Arrange
var id = Guid.NewGuid().ToString();
var productId = Guid.NewGuid().ToString();
var ingredientId = Guid.NewGuid().ToString();
var pekar = new PekarDataModel(
id,
"Ivan Ivanov",
2025-02-27 02:25:39 +04:00
Guid.NewGuid().ToString(),
2025-02-26 23:49:16 +04:00
1.5m,
new List<ProductDataModel>
{
new ProductDataModel(
productId,
"Cake",
"Delicious cake",
new List<IngredientDataModel>
{
new IngredientDataModel(ingredientId, "Sugar", "kg", 100)
}
)
}
);
var flag = false;
_pekarStorageContact.Setup(x => x.GetElementById(id)).Returns(pekar);
_pekarStorageContact.Setup(x => x.RestoreElement(id))
.Callback(() => { flag = true; });
_productStorageContact.Setup(x => x.GetElementById(productId)).Returns(pekar.ProductsItems[0]);
// Act
_pekarBusinessLogicContract.RestorePekar(id);
// Assert
_pekarStorageContact.Verify(x => x.RestoreElement(id), Times.Once);
Assert.That(flag);
Assert.That(Guid.TryParse(pekar.Id, out _), Is.True);
Assert.That(!pekar.FIO.IsEmpty());
Assert.That(Regex.IsMatch(pekar.FIO, @"^[A-Za-zА-Яа-яЁё\s\-]+$"), Is.True);
Assert.That(!pekar.Position.IsEmpty());
Assert.That(pekar.BonusCoefficient > 0);
}
[Test]
public void RestorePekar_RecordNotFound_ThrowException_Test()
{
// Arrange
2025-02-27 02:25:39 +04:00
var id = Guid.NewGuid().ToString();
2025-02-26 23:49:16 +04:00
_pekarStorageContact.Setup(x => x.GetElementById(id)).Returns((PekarDataModel)null);
// Act & Assert
Assert.That(() => _pekarBusinessLogicContract.RestorePekar(id), Throws.TypeOf<ElementNotFoundException>());
2025-02-27 02:25:39 +04:00
_pekarStorageContact.Verify(x => x.GetElementById(id), Times.Once);
2025-02-26 23:49:16 +04:00
}
[Test]
public void RestorePekar_NullOrEmptyId_ThrowException_Test()
{
// Act & Assert
Assert.That(() => _pekarBusinessLogicContract.RestorePekar(null), Throws.TypeOf<ArgumentNullException>());
Assert.That(() => _pekarBusinessLogicContract.RestorePekar(string.Empty),
Throws.TypeOf<ArgumentNullException>());
2025-02-27 02:25:39 +04:00
_pekarStorageContact.Verify(x => x.RestoreElement(It.IsAny<string>()), Times.Never);
2025-02-26 23:49:16 +04:00
}
[Test]
public void RestorePekar_InvalidId_ThrowException_Test()
{
2025-02-27 02:25:39 +04:00
// Arrange
var id = "invalid";
// No need to setup GetElementById for invalid ID since validation should prevent it
2025-02-26 23:49:16 +04:00
// Act & Assert
2025-02-27 02:25:39 +04:00
Assert.That(() => _pekarBusinessLogicContract.RestorePekar(id), Throws.TypeOf<ValidationException>());
_pekarStorageContact.Verify(x => x.GetElementById(id), Times.Never);
2025-02-26 23:49:16 +04:00
}
[Test]
public void RestorePekar_StorageThrowError_ThrowException_Test()
{
// Arrange
2025-02-27 02:25:39 +04:00
var id = Guid.NewGuid().ToString();
var pekar = new PekarDataModel(
id,
"Ivan Ivanov",
Guid.NewGuid().ToString(),
1.5m,
new List<ProductDataModel>()
);
_pekarStorageContact.Setup(x => x.GetElementById(id)).Returns(pekar);
_pekarStorageContact.Setup(x => x.RestoreElement(id))
2025-02-26 23:49:16 +04:00
.Throws(new StorageException(new InvalidOperationException()));
// Act & Assert
2025-02-27 02:25:39 +04:00
Assert.That(() => _pekarBusinessLogicContract.RestorePekar(id), Throws.TypeOf<StorageException>());
_pekarStorageContact.Verify(x => x.GetElementById(id), Times.Once);
_pekarStorageContact.Verify(x => x.RestoreElement(id), Times.Once);
2025-02-26 23:49:16 +04:00
}
}
}