2025-02-27 02:25:39 +04:00

875 lines
36 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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",
Guid.NewGuid().ToString(), // Use Guid for Position
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",
Guid.NewGuid().ToString(), // Use Guid for Position
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]);
_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"));
// Act
var list = _pekarBusinessLogicContract.GetAllPekars();
// Assert
Assert.That(list, Is.Not.Null);
Assert.That(list, Is.EquivalentTo(pekars));
Assert.That(
list.All(p =>
Guid.TryParse(p.Id, out _) && !p.FIO.IsEmpty() && Guid.TryParse(p.Position, out _) &&
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();
var historyRecords = new List<PekarDataModel>
{
new PekarDataModel(pekarId, "Ivan Ivanov", positionId, 1.5m, new List<ProductDataModel>()),
new PekarDataModel(pekarId, "Ivan Ivanov", positionId, 1.7m, new List<ProductDataModel>())
};
var pekarsWithHistory = new List<PekarDataModel>
{
new PekarDataModel(
pekarId,
"Ivan Ivanov",
positionId,
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);
Assert.That(2, Is.EqualTo(historyRecords.Count));
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
var pekarId = Guid.NewGuid().ToString();
_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();
var positionId = Guid.NewGuid().ToString();
var pekarsWithHistory = new List<PekarDataModel>
{
new PekarDataModel(
pekarId,
"Ivan Ivanov",
positionId,
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();
var positionId = Guid.NewGuid().ToString();
var pekar = new PekarDataModel(
id,
"Ivan Ivanov",
positionId,
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]);
_positionStorageContact.Setup(x => x.GetElementById(positionId))
.Returns(new PositionDataModel(positionId, PositionType.Cool, "Baking position"));
// 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);
Assert.That(Guid.TryParse(element.Position, out _), Is.True);
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
var id = Guid.NewGuid().ToString();
_pekarStorageContact.Setup(x => x.GetElementById(id)).Returns((PekarDataModel)null);
// Act & Assert
Assert.That(() => _pekarBusinessLogicContract.GetPekarByData(id),
Throws.TypeOf<ElementNotFoundException>());
_pekarStorageContact.Verify(x => x.GetElementById(id), Times.Once);
}
[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();
var positionId = Guid.NewGuid().ToString();
var flag = false;
var pekar = new PekarDataModel(
Guid.NewGuid().ToString(),
"Ivan Ivanov",
positionId,
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(pekar.Id))
.Returns((PekarDataModel)null);
_pekarStorageContact.Setup(x => x.AddElement(It.Is<PekarDataModel>(p => p.Id == pekar.Id)))
.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]);
_positionStorageContact.Setup(x => x.GetElementById(positionId))
.Returns(new PositionDataModel(positionId, PositionType.Cool, "Baking position"));
// Act
_pekarBusinessLogicContract.InsertPekar(pekar);
// Assert
_pekarStorageContact.Verify(x => x.AddElement(It.Is<PekarDataModel>(p => p.Id == pekar.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(Guid.TryParse(pekar.Position, out _), Is.True);
Assert.That(pekar.BonusCoefficient > 0);
}
[Test]
public void InsertPekar_RecordWithExistsData_ThrowException_Test()
{
// Arrange
var positionId = Guid.NewGuid().ToString();
var productId = Guid.NewGuid().ToString();
var ingredientId = Guid.NewGuid().ToString();
var pekar = new PekarDataModel(
Guid.NewGuid().ToString(),
"Ivan Ivanov",
positionId,
1.5m,
new List<ProductDataModel>
{
new ProductDataModel(
productId,
"Cake",
"Delicious cake",
new List<IngredientDataModel>
{
new IngredientDataModel(ingredientId, "Sugar", "kg", 100)
}
)
}
);
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);
_pekarStorageContact.Setup(x => x.AddElement(It.IsAny<PekarDataModel>()))
.Throws(new ElementExistsException("ID", pekar.Id));
_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"));
// Act & Assert
Assert.That(() => _pekarBusinessLogicContract.InsertPekar(pekar), Throws.TypeOf<ElementExistsException>());
_pekarStorageContact.Verify(x => x.GetElementById(pekar.Id), Times.Once);
}
[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(
"",
"123",
Guid.NewGuid().ToString(),
-1.0m,
new List<ProductDataModel>
{
new ProductDataModel(
"",
"",
"",
new List<IngredientDataModel>
{
new IngredientDataModel("", "Sugar", "kg", -100)
}
)
}
)), Throws.TypeOf<ValidationException>());
_pekarStorageContact.Verify(x => x.AddElement(It.IsAny<PekarDataModel>()), Times.Never);
}
[Test]
public void InsertPekar_StorageThrowError_ThrowException_Test()
{
// Arrange
var positionId = Guid.NewGuid().ToString();
var productId = Guid.NewGuid().ToString();
var ingredientId = Guid.NewGuid().ToString();
var pekar = new PekarDataModel(
Guid.NewGuid().ToString(),
"Ivan Ivanov",
positionId,
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(pekar.Id)).Returns((PekarDataModel)null);
_pekarStorageContact.Setup(x => x.AddElement(It.IsAny<PekarDataModel>()))
.Throws(new StorageException(new InvalidOperationException()));
_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"));
// Act & Assert
Assert.That(() => _pekarBusinessLogicContract.InsertPekar(pekar), Throws.TypeOf<StorageException>());
_pekarStorageContact.Verify(x => x.GetElementById(pekar.Id), Times.Once);
_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();
var positionId = Guid.NewGuid().ToString();
var flag = false;
var pekar = new PekarDataModel(
Guid.NewGuid().ToString(),
"Ivan Ivanov",
positionId,
1.5m,
new List<ProductDataModel>
{
new ProductDataModel(
productId,
"Cake",
"Delicious cake",
new List<IngredientDataModel>
{
new IngredientDataModel(ingredientId, "Sugar", "kg", 100)
}
)
}
);
_pekarStorageContact.Setup(x => x.UpdateElement(It.Is<PekarDataModel>(p => p.Id == pekar.Id)))
.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]);
_positionStorageContact.Setup(x => x.GetElementById(positionId))
.Returns(new PositionDataModel(positionId, PositionType.Cool, "Baking position"));
// Act
_pekarBusinessLogicContract.UpdatePekar(pekar);
// Assert
_pekarStorageContact.Verify(x => x.UpdateElement(It.Is<PekarDataModel>(p => p.Id == pekar.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(Guid.TryParse(pekar.Position, out _), Is.True);
Assert.That(pekar.BonusCoefficient > 0);
}
[Test]
public void UpdatePekar_RecordNotFound_ThrowException_Test()
{
// Arrange
var positionId = Guid.NewGuid().ToString();
var pekar = new PekarDataModel(
Guid.NewGuid().ToString(),
"Ivan Ivanov",
positionId,
1.5m,
new List<ProductDataModel>
{
new ProductDataModel(
Guid.NewGuid().ToString(),
"Cake",
"Delicious cake",
new List<IngredientDataModel>
{
new IngredientDataModel(Guid.NewGuid().ToString(), "Sugar", "kg", 100)
}
)
}
);
_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"));
_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",
Guid.NewGuid().ToString(),
-1.0m,
new List<ProductDataModel>
{
new ProductDataModel(
"",
"",
"",
new List<IngredientDataModel>
{
new IngredientDataModel("", "Sugar", "kg", -100)
}
)
}
)), Throws.TypeOf<ValidationException>());
_pekarStorageContact.Verify(x => x.UpdateElement(It.IsAny<PekarDataModel>()), Times.Never);
}
[Test]
public void UpdatePekar_StorageThrowError_ThrowException_Test()
{
// Arrange
var positionId = Guid.NewGuid().ToString();
var pekar = new PekarDataModel(
Guid.NewGuid().ToString(),
"Ivan Ivanov",
positionId,
1.5m,
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.GetElementById(pekar.Id)).Returns(pekar); // Ensure Pekar exists
_pekarStorageContact.Setup(x => x.UpdateElement(It.IsAny<PekarDataModel>()))
.Throws(new StorageException(new InvalidOperationException()));
_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]);
// 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",
Guid.NewGuid().ToString(),
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
var id = Guid.NewGuid().ToString();
_pekarStorageContact.Setup(x => x.GetElementById(id)).Returns((PekarDataModel)null);
// Act & Assert
Assert.That(() => _pekarBusinessLogicContract.DeletePekar(id), Throws.TypeOf<ElementNotFoundException>());
_pekarStorageContact.Verify(x => x.GetElementById(id), Times.Once);
}
[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>());
_pekarStorageContact.Verify(x => x.DeleteElement(It.IsAny<string>()), Times.Never);
}
[Test]
public void DeletePekar_InvalidId_ThrowException_Test()
{
// Arrange
var id = "invalid";
// No need to setup GetElementById for invalid ID since validation should prevent it
// Act & Assert
Assert.That(() => _pekarBusinessLogicContract.DeletePekar(id), Throws.TypeOf<ValidationException>());
_pekarStorageContact.Verify(x => x.GetElementById(id), Times.Never);
}
[Test]
public void DeletePekar_StorageThrowError_ThrowException_Test()
{
// Arrange
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))
.Throws(new StorageException(new InvalidOperationException()));
// Act & Assert
Assert.That(() => _pekarBusinessLogicContract.DeletePekar(id), Throws.TypeOf<StorageException>());
_pekarStorageContact.Verify(x => x.GetElementById(id), Times.Once);
_pekarStorageContact.Verify(x => x.DeleteElement(id), Times.Once);
}
[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",
Guid.NewGuid().ToString(),
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
var id = Guid.NewGuid().ToString();
_pekarStorageContact.Setup(x => x.GetElementById(id)).Returns((PekarDataModel)null);
// Act & Assert
Assert.That(() => _pekarBusinessLogicContract.RestorePekar(id), Throws.TypeOf<ElementNotFoundException>());
_pekarStorageContact.Verify(x => x.GetElementById(id), Times.Once);
}
[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>());
_pekarStorageContact.Verify(x => x.RestoreElement(It.IsAny<string>()), Times.Never);
}
[Test]
public void RestorePekar_InvalidId_ThrowException_Test()
{
// Arrange
var id = "invalid";
// No need to setup GetElementById for invalid ID since validation should prevent it
// Act & Assert
Assert.That(() => _pekarBusinessLogicContract.RestorePekar(id), Throws.TypeOf<ValidationException>());
_pekarStorageContact.Verify(x => x.GetElementById(id), Times.Never);
}
[Test]
public void RestorePekar_StorageThrowError_ThrowException_Test()
{
// Arrange
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))
.Throws(new StorageException(new InvalidOperationException()));
// Act & Assert
Assert.That(() => _pekarBusinessLogicContract.RestorePekar(id), Throws.TypeOf<StorageException>());
_pekarStorageContact.Verify(x => x.GetElementById(id), Times.Once);
_pekarStorageContact.Verify(x => x.RestoreElement(id), Times.Once);
}
}
}