Модели и тесты

This commit is contained in:
RozhVan 2025-02-10 00:56:55 +04:00
parent 8a51394ba3
commit f5cfdb9ebc
21 changed files with 862 additions and 1 deletions

View File

@ -1,10 +1,12 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.12.35707.178 d17.12
VisualStudioVersion = 17.12.35707.178
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SeniorPomidorContracts", "SeniorPomidorContracts\SeniorPomidorContracts.csproj", "{8A2B4578-792A-4BCB-816B-34D2F4277419}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SeniorPomidorTests", "SeniorPomidorTests\SeniorPomidorTests.csproj", "{6B124CD9-5257-4E62-B736-9C59CCAC19D4}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@ -15,6 +17,10 @@ Global
{8A2B4578-792A-4BCB-816B-34D2F4277419}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8A2B4578-792A-4BCB-816B-34D2F4277419}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8A2B4578-792A-4BCB-816B-34D2F4277419}.Release|Any CPU.Build.0 = Release|Any CPU
{6B124CD9-5257-4E62-B736-9C59CCAC19D4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{6B124CD9-5257-4E62-B736-9C59CCAC19D4}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6B124CD9-5257-4E62-B736-9C59CCAC19D4}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6B124CD9-5257-4E62-B736-9C59CCAC19D4}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE

View File

@ -0,0 +1,24 @@
using SeniorPomidorContracts.Exceptions;
using SeniorPomidorContracts.Extensions;
using SeniorPomidorContracts.Infrastructure;
namespace SeniorPomidorContracts.DataModels;
public class HarvestDataModel(string id, int amount) : IValidation
{
public string Id { get; private set; } = id;
public int Amount { get; private set; } = amount;
public void Validate()
{
if (Id.IsEmpty())
throw new ValidationException("Field Id is empty");
if (!Id.IsGuid())
throw new ValidationException("The value in the field Id is not a unique identifier");
if (Amount < 0)
throw new ValidationException("Field Amount is less than 0");
}
}

View File

@ -0,0 +1,49 @@
using SeniorPomidorContracts.Exceptions;
using SeniorPomidorContracts.Extensions;
using SeniorPomidorContracts.Infrastructure;
namespace SeniorPomidorContracts.DataModels;
public class ProductDataModel(string id, string productName, string
productTypeId, string harvestId, double price, bool isDeleted) : IValidation
{
public string Id { get; private set; } = id;
public string ProductName { get; private set; } = productName;
public string ProductTypeId { get; private set; } = productTypeId;
public string HarvestId { get; private set; } = harvestId;
public double Price { get; private set; } = price;
public bool IsDeleted { get; private set; } = isDeleted;
public void Validate()
{
if (Id.IsEmpty())
throw new ValidationException("Field Id is empty");
if (!Id.IsGuid())
throw new ValidationException("The value in the field Id isnot a unique identifier");
if (ProductName.IsEmpty())
throw new ValidationException("Field ProductName is empty");
if (ProductTypeId.IsEmpty())
throw new ValidationException("Field ProductTypeId is empty");
if (!ProductTypeId.IsGuid())
throw new ValidationException("The value in the field ProductTypeId is not a unique identifier");
if (HarvestId.IsEmpty())
throw new ValidationException("Field HarvestId is empty");
if (!HarvestId.IsGuid())
throw new ValidationException("The value in the field HarvestId is not a unique identifier");
if (Price <= 0)
throw new ValidationException("Field Price is less than or equal to 0");
}
}

View File

@ -0,0 +1,29 @@
using SeniorPomidorContracts.Extensions;
using SeniorPomidorContracts.Infrastructure;
using SeniorPomidorContracts.Exceptions;
namespace SeniorPomidorContracts.DataModels;
public class ProductHistoryDataModel(string productId, double oldPrice) :
IValidation
{
public string ProductId { get; private set; } = productId;
public double OldPrice { get; private set; } = oldPrice;
public DateTime ChangeDate { get; private set; } = DateTime.UtcNow;
public void Validate()
{
if (ProductId.IsEmpty())
throw new ValidationException("Field ProductId is empty");
if (!ProductId.IsGuid())
throw new ValidationException("The value in the field ProductId is not a unique identifier");
if (OldPrice <= 0)
throw new ValidationException("Field OldPrice is less than or equal to 0");
}
}

View File

@ -0,0 +1,30 @@
using SeniorPomidorContracts.Enums;
using SeniorPomidorContracts.Infrastructure;
using SeniorPomidorContracts.Exceptions;
using SeniorPomidorContracts.Extensions;
namespace SeniorPomidorContracts.DataModels;
public class ProductTypeDataModel(string id, string productTypeName, TypeOfProduct typeOfProduct) : IValidation
{
public string Id { get; private set; } = id;
public string ProductTypeName { get; private set; } = productTypeName;
public TypeOfProduct TypeOfProduct { get; private set; } = typeOfProduct;
public void Validate()
{
if (Id.IsEmpty())
throw new ValidationException("Field Id is empty");
if (!Id.IsGuid())
throw new ValidationException("The value in the field Id is not a unique identifier");
if (ProductTypeName.IsEmpty())
throw new ValidationException("Field ProductTypeName is empty");
if (TypeOfProduct == TypeOfProduct.None)
throw new ValidationException("Field TypeOfProduct is empty");
}
}

View File

@ -0,0 +1,36 @@
using SeniorPomidorContracts.Exceptions;
using SeniorPomidorContracts.Extensions;
using SeniorPomidorContracts.Infrastructure;
using System.Text.RegularExpressions;
namespace SeniorPomidorContracts.DataModels;
public class SupplierDataModel(string id, string supplierName, string phoneNumber, double
discountSize) : IValidation
{
public string Id { get; private set; } = id;
public string SupplierName { get; private set; } = supplierName;
public string PhoneNumber { get; private set; } = phoneNumber;
public double DiscountSize { get; private set; } = discountSize;
public void Validate()
{
if (Id.IsEmpty())
throw new ValidationException("Field Id is empty");
if (!Id.IsGuid())
throw new ValidationException("The value in the field Id is not a unique identifier");
if (SupplierName.IsEmpty())
throw new ValidationException("Field SupplierName is empty");
if (PhoneNumber.IsEmpty())
throw new ValidationException("Field PhoneNumber is empty");
if (!Regex.IsMatch(PhoneNumber, @"^((8|\+7)[\- ]?)?(\(?\d{3}\)?[\- ]?)?[\d\- ]{7,10}$"))
throw new ValidationException("Field PhoneNumber is not a phone number");
}
}

View File

@ -0,0 +1,38 @@
using SeniorPomidorContracts.Infrastructure;
using SeniorPomidorContracts.Exceptions;
using SeniorPomidorContracts.Extensions;
namespace SeniorPomidorContracts.DataModels;
public class SupplyDataModel(string id, string supplierId, int supplyNum, DateTime supplyDate, bool isCancel, List<SupplyProductDataModel> products) : IValidation
{
public string Id { get; private set; } = id;
public string? SupplierId { get; private set; } = supplierId;
public int SupplyNum { get; private set; } = supplyNum;
public DateTime SupplyDate { get; private set; } = supplyDate;
public List<SupplyProductDataModel> Products { get; private set; } = products;
public bool IsCancel { get; private set; } = isCancel;
public void Validate()
{
if (Id.IsEmpty())
throw new ValidationException("Field Id is empty");
if (!Id.IsGuid())
throw new ValidationException("The value in the field Id is not a unique identifier");
if (!SupplierId?.IsGuid() ?? !SupplierId?.IsEmpty() ?? false)
throw new ValidationException("The value in the field SupplierId is not a unique identifier");
if (SupplyNum <= 0)
throw new ValidationException("Field SupplyNum is empty");
if ((Products?.Count ?? 0) == 0)
throw new ValidationException("The sale must include products");
}
}

View File

@ -0,0 +1,33 @@
using SeniorPomidorContracts.Extensions;
using SeniorPomidorContracts.Infrastructure;
using SeniorPomidorContracts.Exceptions;
namespace SeniorPomidorContracts.DataModels;
public class SupplyProductDataModel(string supplyId, string productId, int amount) :
IValidation
{
public string SupplyId { get; private set; } = supplyId;
public string ProductId { get; private set; } = productId;
public int Amount { get; private set; } = amount;
public void Validate()
{
if (SupplyId.IsEmpty())
throw new ValidationException("Field SupplyId is empty");
if (!SupplyId.IsGuid())
throw new ValidationException("The value in the field SupplyId is not a unique identifier");
if (ProductId.IsEmpty())
throw new ValidationException("Field ProductId is empty");
if (!ProductId.IsGuid())
throw new ValidationException("The value in the field ProductId is not a unique identifier");
if (Amount <= 0)
throw new ValidationException("Field Amount is less than or equal to 0");
}
}

View File

@ -0,0 +1,10 @@
namespace SeniorPomidorContracts.Enums;
[Flags]
public enum DiscountType
{
None = 0,
OnSale = 1,
RegularSupplier = 2,
Certificate = 4
}

View File

@ -0,0 +1,9 @@
namespace SeniorPomidorContracts.Enums;
public enum TypeOfProduct
{
None = 0,
Fruit = 1,
Vegetable = 2,
Berry = 3
}

View File

@ -0,0 +1,5 @@
namespace SeniorPomidorContracts.Exceptions;
public class ValidationException(string message) : Exception(message)
{
}

View File

@ -0,0 +1,14 @@
namespace SeniorPomidorContracts.Extensions;
public static class StringExtensions
{
public static bool IsEmpty(this string str)
{
return string.IsNullOrWhiteSpace(str);
}
public static bool IsGuid(this string str)
{
return Guid.TryParse(str, out _);
}
}

View File

@ -0,0 +1,6 @@
namespace SeniorPomidorContracts.Infrastructure;
public interface IValidation
{
void Validate();
}

View File

@ -0,0 +1,53 @@
using SeniorPomidorContracts.DataModels;
using SeniorPomidorContracts.Exceptions;
namespace SeniorPomidorTests.DataModelsTests;
[TestFixture]
internal class HarvestDataModelTest
{
[Test]
public void IdIsNullOrEmptyTest()
{
var harvest = CreateDataModel(null, 10);
Assert.That(() => harvest.Validate(),
Throws.TypeOf<ValidationException>());
harvest = CreateDataModel(string.Empty, 10);
Assert.That(() => harvest.Validate(),
Throws.TypeOf<ValidationException>());
}
[Test]
public void IdIsNotGuidTest()
{
var harvest = CreateDataModel("id", 10);
Assert.That(() => harvest.Validate(),
Throws.TypeOf<ValidationException>());
}
[Test]
public void AmountIsLessZeroTest()
{
var harvest = CreateDataModel(Guid.NewGuid().ToString(), -10);
Assert.That(() => harvest.Validate(),
Throws.TypeOf<ValidationException>());
}
[Test]
public void AllFieldsIsCorrectTest()
{
var harvestId = Guid.NewGuid().ToString();
var amount = 10;
var harvest = CreateDataModel(harvestId, amount);
Assert.That(() => harvest.Validate(), Throws.Nothing);
Assert.Multiple(() =>
{
Assert.That(harvest.Id, Is.EqualTo(harvestId));
Assert.That(harvest.Amount, Is.EqualTo(amount));
});
}
private static HarvestDataModel CreateDataModel(string? id, int amount) =>
new(id, amount);
}

View File

@ -0,0 +1,131 @@
using SeniorPomidorContracts.DataModels;
using SeniorPomidorContracts.Exceptions;
namespace SeniorPomidorTests.DataModelsTests;
[TestFixture]
internal class ProductDataModelTests
{
[Test]
public void IdIsNullOrEmptyTest()
{
var product = CreateDataModel(null, "name", Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 10, false);
Assert.That(() => product.Validate(),
Throws.TypeOf<ValidationException>());
product = CreateDataModel(string.Empty, "name",
Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 10, false);
Assert.That(() => product.Validate(),
Throws.TypeOf<ValidationException>());
}
[Test]
public void IdIsNotGuidTest()
{
var product = CreateDataModel("id", "name", Guid.NewGuid().ToString(),
Guid.NewGuid().ToString(), 10, false);
Assert.That(() => product.Validate(),
Throws.TypeOf<ValidationException>());
}
[Test]
public void ProductNameIsEmptyTest()
{
var product = CreateDataModel(Guid.NewGuid().ToString(), null,
Guid.NewGuid().ToString(),
Guid.NewGuid().ToString(), 10, false);
Assert.That(() => product.Validate(),
Throws.TypeOf<ValidationException>());
product = CreateDataModel(Guid.NewGuid().ToString(), string.Empty,
Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 10, false);
Assert.That(() => product.Validate(),
Throws.TypeOf<ValidationException>());
}
[Test]
public void ProductTypeIdIsNullOrEmptyTest()
{
var product = CreateDataModel(Guid.NewGuid().ToString(), "name", null, Guid.NewGuid().ToString(), 10, false);
Assert.That(() => product.Validate(),
Throws.TypeOf<ValidationException>());
product = CreateDataModel(Guid.NewGuid().ToString(), "name", string.Empty,
Guid.NewGuid().ToString(), 10, false);
Assert.That(() => product.Validate(),
Throws.TypeOf<ValidationException>());
}
[Test]
public void ProductTypeIdIsNotGuidTest()
{
var product = CreateDataModel(Guid.NewGuid().ToString(), "name", "productTypeId", Guid.NewGuid().ToString(), 10, false);
Assert.That(() => product.Validate(),
Throws.TypeOf<ValidationException>());
}
[Test]
public void HarvestIdIsNullOrEmptyTest()
{
var product = CreateDataModel(Guid.NewGuid().ToString(), "name", Guid.NewGuid().ToString(), null, 10, false);
Assert.That(() => product.Validate(),
Throws.TypeOf<ValidationException>());
product = CreateDataModel(Guid.NewGuid().ToString(), "name",
Guid.NewGuid().ToString(), string.Empty, 10, false);
Assert.That(() => product.Validate(),
Throws.TypeOf<ValidationException>());
}
[Test]
public void HarvestIdIsNotGuidTest()
{
var product = CreateDataModel(Guid.NewGuid().ToString(), "name", Guid.NewGuid().ToString(), "harvestId", 10, false);
Assert.That(() => product.Validate(),
Throws.TypeOf<ValidationException>());
}
[Test]
public void PriceIsLessOrZeroTest()
{
var product = CreateDataModel(Guid.NewGuid().ToString(), "name",
Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 0, false);
Assert.That(() => product.Validate(),
Throws.TypeOf<ValidationException>());
product = CreateDataModel(Guid.NewGuid().ToString(), "name",
Guid.NewGuid().ToString(), 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 productTypeId = Guid.NewGuid().ToString();
var harvestId = Guid.NewGuid().ToString();
var productPrice = 10;
var productIsDelete = false;
var product = CreateDataModel(productId, productName, productTypeId,
harvestId, 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.ProductTypeId, Is.EqualTo(productTypeId));
Assert.That(product.HarvestId, Is.EqualTo(harvestId));
Assert.That(product.Price, Is.EqualTo(productPrice));
Assert.That(product.IsDeleted, Is.EqualTo(productIsDelete));
});
}
private static ProductDataModel CreateDataModel(string? id, string? productName, string? productTypeId, string? harvestId, double price, bool isDeleted) =>
new(id, productName, productTypeId, harvestId, price, isDeleted);
}

View File

@ -0,0 +1,53 @@
using SeniorPomidorContracts.DataModels;
using SeniorPomidorContracts.Exceptions;
namespace SeniorPomidorTests.DataModelsTests;
[TestFixture]
internal class ProductHistoryDataModelTests
{
[Test]
public void ProductIdIsNullOrEmptyTest()
{
var product = CreateDataModel(null, 10);
Assert.That(() => product.Validate(),
Throws.TypeOf<ValidationException>());
product = CreateDataModel(string.Empty, 10);
Assert.That(() => product.Validate(),
Throws.TypeOf<ValidationException>());
}
[Test]
public void ProductIdIsNotGuidTest()
{
var product = CreateDataModel("id", 10);
Assert.That(() => product.Validate(),
Throws.TypeOf<ValidationException>());
}
[Test]
public void OldPriceIsLessOrZeroTest()
{
var product = CreateDataModel(Guid.NewGuid().ToString(), 0);
Assert.That(() => product.Validate(),
Throws.TypeOf<ValidationException>());
product = CreateDataModel(Guid.NewGuid().ToString(), -10);
Assert.That(() => product.Validate(),
Throws.TypeOf<ValidationException>());
}
[Test]
public void AllFieldsIsCorrectTest()
{
var productId = Guid.NewGuid().ToString();
var oldPrice = 10;
var productHistory = CreateDataModel(productId, oldPrice);
Assert.That(() => productHistory.Validate(), Throws.Nothing);
Assert.Multiple(() =>
{
Assert.That(productHistory.ProductId, Is.EqualTo(productId));
Assert.That(productHistory.OldPrice, Is.EqualTo(oldPrice));
Assert.That(productHistory.ChangeDate, Is.LessThan(DateTime.UtcNow));
Assert.That(productHistory.ChangeDate, Is.GreaterThan(DateTime.UtcNow.AddMinutes(-1)));
});
}
private static ProductHistoryDataModel CreateDataModel(string? productId, double oldPrice) => new(productId, oldPrice);
}

View File

@ -0,0 +1,65 @@
using SeniorPomidorContracts.DataModels;
using SeniorPomidorContracts.Exceptions;
using SeniorPomidorContracts.Enums;
namespace SeniorPomidorTests.DataModelsTests;
[TestFixture]
internal class ProductTypeDataModelTest
{
[Test]
public void IdIsNullOrEmptyTest()
{
var product = CreateDataModel(null, "name", TypeOfProduct.Fruit);
Assert.That(() => product.Validate(), Throws.TypeOf<ValidationException>());
product = CreateDataModel(string.Empty, "name", TypeOfProduct.Fruit);
Assert.That(() => product.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void IdIsNotGuidTest()
{
var product = CreateDataModel("id", "name", TypeOfProduct.Fruit);
Assert.That(() => product.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void ProductTypeNameIsEmptyTest()
{
var product = CreateDataModel(Guid.NewGuid().ToString(), null, TypeOfProduct.Fruit);
Assert.That(() => product.Validate(), Throws.TypeOf<ValidationException>());
product = CreateDataModel(Guid.NewGuid().ToString(), string.Empty, TypeOfProduct.Fruit);
Assert.That(() => product.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void TypeOfProductIsNoneTest()
{
var product = CreateDataModel(Guid.NewGuid().ToString(), "name", TypeOfProduct.None);
Assert.That(() => product.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void AllFieldsIsCorrectTest()
{
var productTypeId = Guid.NewGuid().ToString();
var productTypeName = "name";
var typeOfProduct = TypeOfProduct.Fruit;
var product = CreateDataModel(productTypeId, productTypeName, typeOfProduct);
Assert.That(() => product.Validate(), Throws.Nothing);
Assert.Multiple(() =>
{
Assert.That(product.Id, Is.EqualTo(productTypeId));
Assert.That(product.ProductTypeName, Is.EqualTo(productTypeName));
Assert.That(product.TypeOfProduct, Is.EqualTo(typeOfProduct));
});
}
private static ProductTypeDataModel CreateDataModel(string? id, string? productTypeName, TypeOfProduct typeOfProduct) =>
new(id, productTypeName, typeOfProduct);
}

View File

@ -0,0 +1,75 @@
using SeniorPomidorContracts.DataModels;
using SeniorPomidorContracts.Exceptions;
namespace SeniorPomidorTests.DataModelsTests;
[TestFixture]
internal class SupplierDataModelTest
{
[Test]
public void IdIsNullOrEmptyTest()
{
var buyer = CreateDataModel(null, "name", "number", 10);
Assert.That(() => buyer.Validate(), Throws.TypeOf<ValidationException>());
buyer = CreateDataModel(string.Empty, "name", "number", 10);
Assert.That(() => buyer.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void IdIsNotGuidTest()
{
var buyer = CreateDataModel("id", "name", "number", 10);
Assert.That(() => buyer.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void NameIsNullOrEmptyTest()
{
var buyer = CreateDataModel(Guid.NewGuid().ToString(), null, "number", 10);
Assert.That(() => buyer.Validate(), Throws.TypeOf<ValidationException>());
buyer = CreateDataModel(Guid.NewGuid().ToString(), string.Empty, "number", 10);
Assert.That(() => buyer.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void PhoneNumberIsNullOrEmptyTest()
{
var buyer = CreateDataModel(Guid.NewGuid().ToString(), "name", null, 10);
Assert.That(() => buyer.Validate(), Throws.TypeOf<ValidationException>());
buyer = CreateDataModel(Guid.NewGuid().ToString(), "name", string.Empty, 10);
Assert.That(() => buyer.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void PhoneNumberIsIncorrectTest()
{
var buyer = CreateDataModel(Guid.NewGuid().ToString(), "name", "777", 10);
Assert.That(() => buyer.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void AllFieldsIsCorrectTest()
{
var buyerId = Guid.NewGuid().ToString();
var supplierName = "name";
var phoneNumber = "+7-777-777-77-77";
var discountSize = 11;
var buyer = CreateDataModel(buyerId, supplierName, phoneNumber, discountSize);
Assert.That(() => buyer.Validate(), Throws.Nothing);
Assert.Multiple(() =>
{
Assert.That(buyer.Id, Is.EqualTo(buyerId));
Assert.That(buyer.SupplierName, Is.EqualTo(supplierName));
Assert.That(buyer.PhoneNumber, Is.EqualTo(phoneNumber));
Assert.That(buyer.DiscountSize, Is.EqualTo(discountSize));
});
}
private static SupplierDataModel CreateDataModel(string? id, string? supplierName, string? phoneNumber, double discountSize) =>
new(id, supplierName, phoneNumber, discountSize);
}

View File

@ -0,0 +1,95 @@
using SeniorPomidorContracts.DataModels;
using SeniorPomidorContracts.Exceptions;
namespace SeniorPomidorTests.DataModelsTests;
[TestFixture]
internal class SupplyDataModelTests
{
[Test]
public void IdIsNullOrEmptyTest()
{
var sale = CreateDataModel(null, Guid.NewGuid().ToString(), 1, DateTime.UtcNow, false,
CreateSubDataModel());
Assert.That(() => sale.Validate(), Throws.TypeOf<ValidationException>());
sale = CreateDataModel(string.Empty, Guid.NewGuid().ToString(), 1, DateTime.UtcNow, false,
CreateSubDataModel());
Assert.That(() => sale.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void IdIsNotGuidTest()
{
var sale = CreateDataModel("id", Guid.NewGuid().ToString(), 1, DateTime.UtcNow, false,
CreateSubDataModel());
Assert.That(() => sale.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void SupplierIdIsNotGuidTest()
{
var sale = CreateDataModel(Guid.NewGuid().ToString(),
"supplierId", 1, DateTime.UtcNow, false,
CreateSubDataModel());
Assert.That(() => sale.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void NumIsLessOrZeroTest()
{
var sale = CreateDataModel(Guid.NewGuid().ToString(),
Guid.NewGuid().ToString(), 0, DateTime.UtcNow, false,
CreateSubDataModel());
Assert.That(() => sale.Validate(), Throws.TypeOf<ValidationException>());
sale = CreateDataModel(Guid.NewGuid().ToString(),
Guid.NewGuid().ToString(), -10, DateTime.UtcNow, false,
CreateSubDataModel());
Assert.That(() => sale.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void ProductsIsNullOrEmptyTest()
{
var sale = CreateDataModel(Guid.NewGuid().ToString(),
Guid.NewGuid().ToString(), 1, DateTime.UtcNow, false,
null);
Assert.That(() => sale.Validate(), Throws.TypeOf<ValidationException>());
sale = CreateDataModel(Guid.NewGuid().ToString(),
Guid.NewGuid().ToString(), 1, DateTime.UtcNow, false,
[]);
Assert.That(() => sale.Validate(), Throws.TypeOf<ValidationException>()); }
[Test]
public void AllFieldsIsCorrectTest()
{
var id = Guid.NewGuid().ToString();
var supplierId = Guid.NewGuid().ToString();
var num = 1;
var supplyDate = DateTime.UtcNow;
var isCancel = true;
var products = CreateSubDataModel();
var supply = CreateDataModel(id, supplierId, num, supplyDate, isCancel, products);
Assert.That(() => supply.Validate(), Throws.Nothing);
Assert.Multiple(() =>
{
Assert.That(supply.Id, Is.EqualTo(id));
Assert.That(supply.SupplierId, Is.EqualTo(supplierId));
Assert.That(supply.SupplyNum, Is.EqualTo(num));
Assert.That(supply.SupplyDate, Is.EqualTo(supplyDate));
Assert.That(supply.IsCancel, Is.EqualTo(isCancel));
Assert.That(supply.Products, Is.EquivalentTo(products));
});
}
private static SupplyDataModel CreateDataModel(string? id, string? supplierId, int num, DateTime supplyDate, bool
isCancel, List<SupplyProductDataModel>? products) =>
new(id, supplierId, num, supplyDate, isCancel, products);
private static List<SupplyProductDataModel> CreateSubDataModel()
=> [new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 1)];
}

View File

@ -0,0 +1,73 @@
using SeniorPomidorContracts.DataModels;
using SeniorPomidorContracts.Exceptions;
namespace SeniorPomidorTests.DataModelsTests;
[TestFixture]
internal class SupplyProductDataModelTest
{
[Test]
public void SupplyIdIsNullOrEmptyTest()
{
var supplyProduct = CreateDataModel(null, Guid.NewGuid().ToString(), 10);
Assert.That(() => supplyProduct.Validate(), Throws.TypeOf<ValidationException>());
supplyProduct = CreateDataModel(string.Empty, Guid.NewGuid().ToString(), 10);
Assert.That(() => supplyProduct.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void SupplyIdIsNotGuidTest()
{
var supplyProduct = CreateDataModel("supplyId", Guid.NewGuid().ToString(), 10);
Assert.That(() => supplyProduct.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void ProductIdIsNullOrEmptyTest()
{
var supplyProduct = CreateDataModel(Guid.NewGuid().ToString(), null, 10);
Assert.That(() => supplyProduct.Validate(), Throws.TypeOf<ValidationException>());
supplyProduct = CreateDataModel(string.Empty, Guid.NewGuid().ToString(), 10);
Assert.That(() => supplyProduct.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void ProductIdIsNotGuidTest()
{
var supplyProduct = CreateDataModel(Guid.NewGuid().ToString(), "productId", 10);
Assert.That(() => supplyProduct.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void CountIsLessOrZeroTest()
{
var supplyProduct = CreateDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 0);
Assert.That(() => supplyProduct.Validate(), Throws.TypeOf<ValidationException>());
supplyProduct = CreateDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), -10);
Assert.That(() => supplyProduct.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void AllFieldsIsCorrectTest()
{
var supplyId = Guid.NewGuid().ToString();
var productId = Guid.NewGuid().ToString();
var amount = 10;
var supplyProduct = CreateDataModel(supplyId, productId, amount);
Assert.That(() => supplyProduct.Validate(), Throws.Nothing);
Assert.Multiple(() =>
{
Assert.That(supplyProduct.SupplyId, Is.EqualTo(supplyId));
Assert.That(supplyProduct.ProductId, Is.EqualTo(productId));
Assert.That(supplyProduct.Amount, Is.EqualTo(amount));
});
}
private static SupplyProductDataModel CreateDataModel(string? supplyId, string? productId, int amount) => new(supplyId, productId, amount);
}

View File

@ -0,0 +1,27 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<LangVersion>latest</LangVersion>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="coverlet.collector" Version="6.0.2" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
<PackageReference Include="NUnit" Version="4.2.2" />
<PackageReference Include="NUnit.Analyzers" Version="4.4.0" />
<PackageReference Include="NUnit3TestAdapter" Version="4.6.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\SeniorPomidorContracts\SeniorPomidorContracts.csproj" />
</ItemGroup>
<ItemGroup>
<Using Include="NUnit.Framework" />
</ItemGroup>
</Project>