2025-02-26 23:49:16 +04:00

816 lines
32 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",
"Baker",
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",
"Pastry Chef",
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]);
// 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() && !p.Position.IsEmpty() &&
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<PekarHistoryDataModel>
{
new PekarHistoryDataModel(
pekarId,
"Ivan Ivanov",
positionId.ToString(),
1.5m,
DateTime.Now.AddDays(-30)
),
new PekarHistoryDataModel(
pekarId,
"Ivan Ivanov",
positionId.ToString(),
1.7m,
DateTime.Now.AddDays(-15)
)
};
var pekarsWithHistory = new List<PekarDataModel>
{
new PekarDataModel(
pekarId,
"Ivan Ivanov",
"Baker",
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(list.Count,
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 = "nonexistent";
_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 = "999"; // Invalid position ID
var pekarsWithHistory = new List<PekarDataModel>
{
new PekarDataModel(
pekarId,
"Ivan Ivanov",
"Baker",
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 pekar = new PekarDataModel(
id,
"Ivan Ivanov",
"Baker",
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]);
// 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(!element.Position.IsEmpty());
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 = "nonexistent";
_pekarStorageContact.Setup(x => x.GetElementById(id)).Returns((PekarDataModel)null);
// Act & Assert
Assert.That(() => _pekarBusinessLogicContract.GetPekarByData(id),
Throws.TypeOf<ElementNotFoundException>());
_pekarStorageContact.Verify(x => x.GetElementById(It.IsAny<string>()), 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 flag = false;
var pekar = new PekarDataModel(
Guid.NewGuid().ToString(),
"Ivan Ivanov",
"Baker",
1.5m,
new List<ProductDataModel>
{
new ProductDataModel(
productId,
"Cake",
"Delicious cake",
new List<IngredientDataModel>
{
new IngredientDataModel(ingredientId, "Sugar", "kg", 100)
}
)
}
);
_pekarStorageContact.Setup(x => x.AddElement(It.IsAny<PekarDataModel>()))
.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]);
// Act
_pekarBusinessLogicContract.InsertPekar(pekar);
// Assert
_pekarStorageContact.Verify(x => x.AddElement(It.IsAny<PekarDataModel>()), 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 InsertPekar_RecordWithExistsData_ThrowException_Test()
{
// Arrange
var pekar = new PekarDataModel(
Guid.NewGuid().ToString(),
"Ivan Ivanov",
"Baker",
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.AddElement(It.IsAny<PekarDataModel>()))
.Throws(new ElementExistsException("ID", pekar.Id));
// Act & Assert
Assert.That(() => _pekarBusinessLogicContract.InsertPekar(pekar), Throws.TypeOf<ElementExistsException>());
_pekarStorageContact.Verify(x => x.AddElement(It.IsAny<PekarDataModel>()), 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",
"",
-1.0m,
new List<ProductDataModel>
{
new ProductDataModel(
"",
"",
"",
new List<IngredientDataModel>
{
new IngredientDataModel("", "Sugar", "kg", -100) // Invalid Ingredient
}
)
}
)), Throws.TypeOf<ValidationException>());
_pekarStorageContact.Verify(x => x.AddElement(It.IsAny<PekarDataModel>()), Times.Never);
}
[Test]
public void InsertPekar_StorageThrowError_ThrowException_Test()
{
// Arrange
var pekar = new PekarDataModel(
Guid.NewGuid().ToString(),
"Ivan Ivanov",
"Baker",
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.AddElement(It.IsAny<PekarDataModel>()))
.Throws(new StorageException(new InvalidOperationException()));
// Act & Assert
Assert.That(() => _pekarBusinessLogicContract.InsertPekar(pekar), Throws.TypeOf<StorageException>());
_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 flag = false;
var pekar = new PekarDataModel(
Guid.NewGuid().ToString(),
"Ivan Ivanov",
"Baker",
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.IsAny<PekarDataModel>()))
.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]);
// Act
_pekarBusinessLogicContract.UpdatePekar(pekar);
// Assert
_pekarStorageContact.Verify(x => x.UpdateElement(It.IsAny<PekarDataModel>()), 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 UpdatePekar_RecordNotFound_ThrowException_Test()
{
// Arrange
var pekar = new PekarDataModel(
Guid.NewGuid().ToString(),
"Ivan Ivanov",
"Baker",
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.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",
"",
-1.0m,
new List<ProductDataModel>
{
new ProductDataModel(
"",
"",
"",
new List<IngredientDataModel>
{
new IngredientDataModel("", "Sugar", "kg", -100) // Invalid Ingredient
}
)
}
)), Throws.TypeOf<ValidationException>());
_pekarStorageContact.Verify(x => x.UpdateElement(It.IsAny<PekarDataModel>()), Times.Never);
}
[Test]
public void UpdatePekar_StorageThrowError_ThrowException_Test()
{
// Arrange
var pekar = new PekarDataModel(
Guid.NewGuid().ToString(),
"Ivan Ivanov",
"Baker",
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.UpdateElement(It.IsAny<PekarDataModel>()))
.Throws(new StorageException(new InvalidOperationException()));
// 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",
"Baker",
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 = "nonexistent";
_pekarStorageContact.Setup(x => x.GetElementById(id)).Returns((PekarDataModel)null);
// Act & Assert
Assert.That(() => _pekarBusinessLogicContract.DeletePekar(id), Throws.TypeOf<ElementNotFoundException>());
_pekarStorageContact.Verify(x => x.GetElementById(It.IsAny<string>()), 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<PekarDataModel>().Id), Times.Never);
}
[Test]
public void DeletePekar_InvalidId_ThrowException_Test()
{
// Act & Assert
Assert.That(() => _pekarBusinessLogicContract.DeletePekar("invalid"), Throws.TypeOf<ValidationException>());
_pekarStorageContact.Verify(x => x.DeleteElement(It.IsAny<PekarDataModel>().Id), Times.Never);
}
[Test]
public void DeletePekar_StorageThrowError_ThrowException_Test()
{
// Arrange
_pekarStorageContact.Setup(x => x.DeleteElement(It.IsAny<PekarDataModel>().Id))
.Throws(new StorageException(new InvalidOperationException()));
// Act & Assert
Assert.That(() => _pekarBusinessLogicContract.DeletePekar(Guid.NewGuid().ToString()),
Throws.TypeOf<StorageException>());
_pekarStorageContact.Verify(x => x.DeleteElement(It.IsAny<PekarDataModel>().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",
"Baker",
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 = "nonexistent";
_pekarStorageContact.Setup(x => x.GetElementById(id)).Returns((PekarDataModel)null);
// Act & Assert
Assert.That(() => _pekarBusinessLogicContract.RestorePekar(id), Throws.TypeOf<ElementNotFoundException>());
_pekarStorageContact.Verify(x => x.GetElementById(It.IsAny<string>()), 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<PekarDataModel>().Id), Times.Never);
}
[Test]
public void RestorePekar_InvalidId_ThrowException_Test()
{
// Act & Assert
Assert.That(() => _pekarBusinessLogicContract.RestorePekar("invalid"),
Throws.TypeOf<ValidationException>());
_pekarStorageContact.Verify(x => x.RestoreElement(It.IsAny<PekarDataModel>().Id), Times.Never);
}
[Test]
public void RestorePekar_StorageThrowError_ThrowException_Test()
{
// Arrange
_pekarStorageContact.Setup(x => x.RestoreElement(It.IsAny<PekarDataModel>().Id))
.Throws(new StorageException(new InvalidOperationException()));
// Act & Assert
Assert.That(() => _pekarBusinessLogicContract.RestorePekar(Guid.NewGuid().ToString()),
Throws.TypeOf<StorageException>());
_pekarStorageContact.Verify(x => x.RestoreElement(It.IsAny<PekarDataModel>().Id), Times.Once);
}
}
}