PIbd-23 Kobin V.O. First labWork #1

Open
vkobi wants to merge 3 commits from Task_1_Models into main
4 changed files with 48 additions and 37 deletions
Showing only changes of commit 88a1d9935f - Show all commits

View File

@ -9,12 +9,19 @@ using System.Threading.Tasks;
namespace SweetBunsContracts.DataModels;
public class CookingDataModel(string productId, int count) : IValidation
public class CookingDataModel(string workerId, string productId, int count) : IValidation
{
public string WorkerId { get; private set; } = workerId;
public string ProductId { get; private set; } = productId;
public int Count { get; private set; } = count;
public void Validate()
{
if (WorkerId.IsEmpty())
throw new ValidationException("Field WorkerId is empty");
if (!WorkerId.IsGuid())
throw new ValidationException("The value in the field WorkerId is not a unique identifier");
if (ProductId.IsEmpty())
throw new ValidationException("Field ProductId is empty");

View File

@ -11,7 +11,7 @@ using System.Xml;
namespace SweetBunsContracts.DataModels;
public class ProductDataModel(string id, string productName, ProductType productType, string ingredientsId, double price, bool isDeleted) : IValidation
public class ProductDataModel(string id, string productName, ProductType productType, List<RecipeDataModel> recipes, double price, bool isDeleted) : IValidation
{
public string Id { get; private set; } = id;
@ -19,7 +19,7 @@ public class ProductDataModel(string id, string productName, ProductType product
public ProductType ProductType { get; private set; } = productType;
public string IngredientsId { get; private set; } = ingredientsId;
public List<RecipeDataModel> Recipes { get; private set; } = recipes;
public double Price { get; private set; } = price;
@ -36,13 +36,10 @@ public class ProductDataModel(string id, string productName, ProductType product
throw new ValidationException("Field ProductName is empty");
if (ProductType == ProductType.None)
throw new ValidationException("Field ProductType is empty");
throw new ValidationException("Field ProductType is empty");
if (IngredientsId.IsEmpty())
throw new ValidationException("Field IngredientsId is empty");
if (!IngredientsId.IsGuid())
throw new ValidationException("The value in the field IngredientsId is not a unique identifier");
if((Recipes?.Count ?? 0) == 0)
throw new ValidationException("The product must include recipes");
if (Price <= 0)
throw new ValidationException("Field Price is less than or equal to 0");

View File

@ -11,32 +11,45 @@ namespace SweetBunsTests.DataModels_Tests;
[TestFixture]
internal class CookingDataModelTests
{
[Test]
public void WorkerIdIsNullOrEmptyTest()
{
var cooking = CreateDataModel(string.Empty, Guid.NewGuid().ToString(), 10);
Assert.That(() => cooking.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void WorkerIdIsNotGuidTest()
{
var cooking = CreateDataModel("id", Guid.NewGuid().ToString(), 10);
Assert.That(() => cooking.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void ProductIdIsNullOrEmptyTest()
{
var cooking = CreateDataModel(string.Empty, 10);
var cooking = CreateDataModel(Guid.NewGuid().ToString(), string.Empty, 10);
Assert.That(() => cooking.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void ProductIdIsNotGuidTest()
{
var cooking = CreateDataModel("id", 10);
var cooking = CreateDataModel(Guid.NewGuid().ToString(), "id", 10);
Assert.That(() => cooking.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void CountIsLessOrZeroTest()
{
var cooking = CreateDataModel(Guid.NewGuid().ToString(), 0);
var cooking = CreateDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 0);
Assert.That(() => cooking.Validate(), Throws.TypeOf<ValidationException>());
cooking = CreateDataModel(Guid.NewGuid().ToString(), -10);
cooking = CreateDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), -10);
Assert.That(() => cooking.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void AllFieldsIsCorrectTest()
{
var workerId = Guid.NewGuid().ToString();
var productId = Guid.NewGuid().ToString();
var count = 10;
var cooking = CreateDataModel(productId, count);
var cooking = CreateDataModel(workerId, productId, count);
Assert.That(() => cooking.Validate(), Throws.Nothing);
Assert.Multiple(() =>
{
@ -44,5 +57,5 @@ internal class CookingDataModelTests
Assert.That(cooking.Count, Is.EqualTo(count));
});
}
private static CookingDataModel CreateDataModel(string ProductId, int Count) => new(ProductId, Count);
private static CookingDataModel CreateDataModel(string WorkerId, string ProductId, int Count) => new(WorkerId, ProductId, Count);
}

View File

@ -15,57 +15,50 @@ internal class ProductDataModelTests
[Test]
public void IdIsNullOrEmptyTest()
{
var product = CreateDataModel(null, "name", ProductType.Cookies, Guid.NewGuid().ToString(), 10, false);
var product = CreateDataModel(null, "name", ProductType.Cookies, CreateSubDataModel(), 10, false);
Assert.That(() => product.Validate(), Throws.TypeOf<ValidationException>());
product = CreateDataModel(string.Empty, "name", ProductType.Cookies, Guid.NewGuid().ToString(), 10, false);
product = CreateDataModel(string.Empty, "name", ProductType.Cookies, CreateSubDataModel(), 10, false);
Assert.That(() => product.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void IdIsNotGuidTest()
{
var product = CreateDataModel("id", "name", ProductType.Cookies, Guid.NewGuid().ToString(), 10, false);
var product = CreateDataModel("id", "name", ProductType.Cookies, CreateSubDataModel(), 10, false);
Assert.That(() => product.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void ProductNameIsEmptyTest()
{
var product = CreateDataModel(Guid.NewGuid().ToString(), null, ProductType.Cookies, Guid.NewGuid().ToString(), 10, false);
var product = CreateDataModel(Guid.NewGuid().ToString(), null, ProductType.Cookies, CreateSubDataModel(), 10, false);
Assert.That(() => product.Validate(), Throws.TypeOf<ValidationException>());
product = CreateDataModel(Guid.NewGuid().ToString(), string.Empty, ProductType.Cookies, Guid.NewGuid().ToString(), 10, false);
product = CreateDataModel(Guid.NewGuid().ToString(), string.Empty, ProductType.Cookies, CreateSubDataModel(), 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);
var product = CreateDataModel(Guid.NewGuid().ToString(), null, ProductType.None, CreateSubDataModel(), 10, false);
Assert.That(() => product.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void IngredientsIdIsNullOrEmptyTest()
public void RecipesIsNullOrEmptyTest()
{
var product = CreateDataModel(Guid.NewGuid().ToString(), "name", ProductType.Cookies, null, 10, false);
Assert.That(() => product.Validate(), Throws.TypeOf<ValidationException>());
product = CreateDataModel(Guid.NewGuid().ToString(), "name", ProductType.Cookies, string.Empty, 10, false);
product = CreateDataModel(Guid.NewGuid().ToString(), "name", ProductType.Cookies, [], 10, false);
Assert.That(() => product.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void IngredientsIdIsNotGuidTest()
{
var product = CreateDataModel(Guid.NewGuid().ToString(), "name", ProductType.Cookies, "ingredientsId", 10, false);
Assert.That(() => product.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void PriceIsLessOrZeroTest()
{
var product = CreateDataModel(Guid.NewGuid().ToString(), "name", ProductType.Cookies, Guid.NewGuid().ToString(), 0, false);
var product = CreateDataModel(Guid.NewGuid().ToString(), "name", ProductType.Cookies, CreateSubDataModel(), 0, false);
Assert.That(() => product.Validate(), Throws.TypeOf<ValidationException>());
product = CreateDataModel(Guid.NewGuid().ToString(), "name", ProductType.Cookies, Guid.NewGuid().ToString(), -10, false);
product = CreateDataModel(Guid.NewGuid().ToString(), "name", ProductType.Cookies, CreateSubDataModel(), -10, false);
Assert.That(() => product.Validate(), Throws.TypeOf<ValidationException>());
}
@ -75,21 +68,22 @@ internal class ProductDataModelTests
var productId = Guid.NewGuid().ToString();
var productName = "name";
var productType = ProductType.Cookies;
var productIngredientsId = Guid.NewGuid().ToString();
var recipes = CreateSubDataModel();
var productPrice = 10;
var productIsDelete = false;
var product = CreateDataModel(productId, productName, productType, productIngredientsId, productPrice, productIsDelete);
var product = CreateDataModel(productId, productName, productType, recipes, 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.IngredientsId, Is.EqualTo(productIngredientsId));
Assert.That(product.Recipes, Is.EquivalentTo(recipes));
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? ingredientsId, double price, bool isDeleted) => new(id, productName, productType, ingredientsId, price, isDeleted);
private static ProductDataModel CreateDataModel(string? id, string? productName, ProductType productType, List<RecipeDataModel>? recipes, double price, bool isDeleted) => new(id, productName, productType, recipes, price, isDeleted);
private static List<RecipeDataModel> CreateSubDataModel() => [new(Guid.NewGuid().ToString(), 1, Guid.NewGuid().ToString())];
}