2025-02-18 18:34:16 +04:00

105 lines
4.3 KiB
C#

using SquirrelBarContracts.DataModels;
using System.ComponentModel.DataAnnotations;
using SquirrelBarContracts.Enums;
namespace SquirrelBarTests.DataModelsTests;
[TestFixture]
internal class ProductDataModelTests
{
[Test]
public void IdIsNullOrEmptyTest()
{
var product = CreateDataModel(null, "name", ProductType.AlcoholDrinks, Guid.NewGuid().ToString(), 10, false);
Assert.That(() => product.Validate(),
Throws.TypeOf<ValidationException>());
product = CreateDataModel(string.Empty, "name", ProductType.AlcoholDrinks, Guid.NewGuid().ToString(), 10, false);
Assert.That(() => product.Validate(),
Throws.TypeOf<ValidationException>());
}
[Test]
public void IdIsNotGuidTest()
{
var product = CreateDataModel("id", "name", ProductType.AlcoholDrinks, Guid.NewGuid().ToString(), 10, false);
Assert.That(() => product.Validate(),
Throws.TypeOf<ValidationException>());
}
[Test]
public void ProductNameIsEmptyTest()
{
var product = CreateDataModel(Guid.NewGuid().ToString(), null,
ProductType.AlcoholDrinks, Guid.NewGuid().ToString(), 10, false);
Assert.That(() => product.Validate(),
Throws.TypeOf<ValidationException>());
product = CreateDataModel(Guid.NewGuid().ToString(), string.Empty,
ProductType.AlcoholDrinks, Guid.NewGuid().ToString(), 10, false);
Assert.That(() => product.Validate(),
Throws.TypeOf<ValidationException>());
}
[Test]
public void ProductTypeIsNoneTest()
{
var product = CreateDataModel(Guid.NewGuid().ToString(), null,
ProductType.None, Guid.NewGuid().ToString(), 10, false);
Assert.That(() => product.Validate(),
Throws.TypeOf<ValidationException>());
}
[Test]
public void ManufacturerIdIsNullOrEmptyTest()
{
var product = CreateDataModel(Guid.NewGuid().ToString(), "name",
ProductType.AlcoholDrinks, null, 10, false);
Assert.That(() => product.Validate(),
Throws.TypeOf<ValidationException>());
product = CreateDataModel(Guid.NewGuid().ToString(), "name",
ProductType.AlcoholDrinks, string.Empty, 10, false);
Assert.That(() => product.Validate(),
Throws.TypeOf<ValidationException>());
}
[Test]
public void ManufacturerIdIsNotGuidTest()
{
var product = CreateDataModel(Guid.NewGuid().ToString(), "name",
ProductType.AlcoholDrinks, "supplierId", 10, false);
Assert.That(() => product.Validate(),
Throws.TypeOf<ValidationException>());
}
[Test]
public void PriceIsLessOrZeroTest()
{
var product = CreateDataModel(Guid.NewGuid().ToString(), "name",
ProductType.AlcoholDrinks, Guid.NewGuid().ToString(), 0, false);
Assert.That(() => product.Validate(),
Throws.TypeOf<ValidationException>());
product = CreateDataModel(Guid.NewGuid().ToString(), "name",
ProductType.AlcoholDrinks, Guid.NewGuid().ToString(), -10, false);
Assert.That(() => product.Validate(),
Throws.TypeOf<ValidationException>());
}
[Test]
public void AllFieldsIsCorrectTest()
{
var productId = Guid.NewGuid().ToString();
var productName = "name";
var productType = ProductType.AlcoholDrinks;
var productSupplierId = Guid.NewGuid().ToString();
var productPrice = 10;
var productIsDelete = false;
var product = CreateDataModel(productId, productName, productType,
productSupplierId, productPrice, productIsDelete);
Assert.That(() => product.Validate(), Throws.Nothing);
Assert.Multiple(() =>
{
Assert.That(product.Id, Is.EqualTo(productId));
Assert.That(product.ProductName, Is.EqualTo(productName));
Assert.That(product.ProductType, Is.EqualTo(productType));
Assert.That(product.SupplierId, Is.EqualTo(productSupplierId));
Assert.That(product.Price, Is.EqualTo(productPrice));
Assert.That(product.IsDeleted, Is.EqualTo(productIsDelete));
});
}
private static ProductDataModel CreateDataModel(string? id, string?
productName, ProductType productType, string? supplierId, double price, bool
isDeleted) =>
new(id, productName, productType, supplierId, price, isDeleted);
}