diff --git a/TheSweetBunsProject/SweetBunsContracts/DataModels/CookingDataModel.cs b/TheSweetBunsProject/SweetBunsContracts/DataModels/CookingDataModel.cs new file mode 100644 index 0000000..d69c267 --- /dev/null +++ b/TheSweetBunsProject/SweetBunsContracts/DataModels/CookingDataModel.cs @@ -0,0 +1,34 @@ +using SweetBunsContracts.Exceptions; +using SweetBunsContracts.Extensions; +using SweetBunsContracts.Infrastructure; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace SweetBunsContracts.DataModels; + +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"); + + if (!ProductId.IsGuid()) + throw new ValidationException("The value in the field ProductId is not a unique identifier"); + + if (Count <= 0) + throw new ValidationException("Field Count is less than or equal to 0"); + } +} diff --git a/TheSweetBunsProject/SweetBunsContracts/DataModels/IngredientsDataModel.cs b/TheSweetBunsProject/SweetBunsContracts/DataModels/IngredientsDataModel.cs new file mode 100644 index 0000000..bde5cad --- /dev/null +++ b/TheSweetBunsProject/SweetBunsContracts/DataModels/IngredientsDataModel.cs @@ -0,0 +1,31 @@ +using SweetBunsContracts.Exceptions; +using SweetBunsContracts.Extensions; +using SweetBunsContracts.Infrastructure; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Xml; + +namespace SweetBunsContracts.DataModels; + +public class IngredientsDataModel(string id, string ingredientName, string? prevIngredientName, string? prevPrevIngredientName) : IValidation +{ + public string Id { get; private set; } = id; + + public string IngredientName { get; private set; } = ingredientName; + + public string? PrevIngredientName { get; private set; } = prevIngredientName; + + public string? PrevPrevIngredientName { get; private set; } = prevPrevIngredientName; + 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 (IngredientName.IsEmpty()) + throw new ValidationException("Field IngredientName is empty"); + } +} diff --git a/TheSweetBunsProject/SweetBunsContracts/DataModels/PostDataModel.cs b/TheSweetBunsProject/SweetBunsContracts/DataModels/PostDataModel.cs new file mode 100644 index 0000000..a53c9be --- /dev/null +++ b/TheSweetBunsProject/SweetBunsContracts/DataModels/PostDataModel.cs @@ -0,0 +1,52 @@ +using SweetBunsContracts.Enums; +using SweetBunsContracts.Exceptions; +using SweetBunsContracts.Extensions; +using SweetBunsContracts.Infrastructure; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Xml; + +namespace SweetBunsContracts.DataModels; + +public class PostDataModel(string id, string postId, string postName, PostType postType, double salary, bool isActual, DateTime changeDate) : IValidation +{ + public string Id { get; private set; } = id; + + public string PostId { get; private set; } = postId; + + public string PostName { get; private set; } = postName; + + public PostType PostType { get; private set; } = postType; + + public double Salary { get; private set; } = salary; + + public bool IsActual { get; private set; } = isActual; + + public DateTime ChangeDate { get; private set; } = changeDate; + 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 (PostId.IsEmpty()) + throw new ValidationException("Field PostId is empty"); + + if (!PostId.IsGuid()) + throw new ValidationException("The value in the field PostId is not a unique identifier"); + + if (PostName.IsEmpty()) + throw new ValidationException("Field PostName is empty"); + + if (PostType == PostType.None) + throw new ValidationException("Field PostType is empty"); + + if (Salary <= 0) + throw new ValidationException("Field Salary is empty"); + } +} diff --git a/TheSweetBunsProject/SweetBunsContracts/DataModels/ProductDataModel.cs b/TheSweetBunsProject/SweetBunsContracts/DataModels/ProductDataModel.cs new file mode 100644 index 0000000..75142e7 --- /dev/null +++ b/TheSweetBunsProject/SweetBunsContracts/DataModels/ProductDataModel.cs @@ -0,0 +1,47 @@ +using SweetBunsContracts.Enums; +using SweetBunsContracts.Exceptions; +using SweetBunsContracts.Extensions; +using SweetBunsContracts.Infrastructure; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Xml; + +namespace SweetBunsContracts.DataModels; + +public class ProductDataModel(string id, string productName, ProductType productType, List recipes, double price, bool isDeleted) : IValidation +{ + public string Id { get; private set; } = id; + + public string ProductName { get; private set; } = productName; + + public ProductType ProductType { get; private set; } = productType; + + public List Recipes { get; private set; } = recipes; + + 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 is not a unique identifier"); + + if (ProductName.IsEmpty()) + throw new ValidationException("Field ProductName is empty"); + + if (ProductType == ProductType.None) + throw new ValidationException("Field ProductType is empty"); + + 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"); + } +} diff --git a/TheSweetBunsProject/SweetBunsContracts/DataModels/RecipeDataModel.cs b/TheSweetBunsProject/SweetBunsContracts/DataModels/RecipeDataModel.cs new file mode 100644 index 0000000..320c1e6 --- /dev/null +++ b/TheSweetBunsProject/SweetBunsContracts/DataModels/RecipeDataModel.cs @@ -0,0 +1,34 @@ +using SweetBunsContracts.Exceptions; +using SweetBunsContracts.Extensions; +using SweetBunsContracts.Infrastructure; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace SweetBunsContracts.DataModels; + +public class RecipeDataModel(string ingredientsId, int count, string productId) : IValidation +{ + public string IngredientsId { get; private set; } = ingredientsId; + public int Count { get; private set; } = count; + public string ProductId { get; private set; } = productId; + public void Validate() + { + 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 (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 (Count <= 0) + throw new ValidationException("Field Count is less than or equal to 0"); + } +} diff --git a/TheSweetBunsProject/SweetBunsContracts/DataModels/SalaryDataModel.cs b/TheSweetBunsProject/SweetBunsContracts/DataModels/SalaryDataModel.cs new file mode 100644 index 0000000..3b3f67e --- /dev/null +++ b/TheSweetBunsProject/SweetBunsContracts/DataModels/SalaryDataModel.cs @@ -0,0 +1,30 @@ +using SweetBunsContracts.Exceptions; +using SweetBunsContracts.Extensions; +using SweetBunsContracts.Infrastructure; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace SweetBunsContracts.DataModels; + +public class SalaryDataModel(string workerId, DateTime salaryDate, double workerSalary) : IValidation +{ + public string WorkerId { get; private set; } = workerId; + + public DateTime SalaryDate { get; private set; } = salaryDate; + + public double Salary { get; private set; } = workerSalary; + 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 (Salary <= 0) + throw new ValidationException("Field Salary is less than or equal to 0"); + } +} diff --git a/TheSweetBunsProject/SweetBunsContracts/DataModels/WorkerDataModel.cs b/TheSweetBunsProject/SweetBunsContracts/DataModels/WorkerDataModel.cs new file mode 100644 index 0000000..f07a186 --- /dev/null +++ b/TheSweetBunsProject/SweetBunsContracts/DataModels/WorkerDataModel.cs @@ -0,0 +1,63 @@ +using SweetBunsContracts.Extensions; +using SweetBunsContracts.Infrastructure; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using static System.Runtime.InteropServices.JavaScript.JSType; +using System.Xml; +using SweetBunsContracts.Exceptions; +using System.Numerics; +using System.Text.RegularExpressions; + +namespace SweetBunsContracts.DataModels; + +public class WorkerDataModel(string id, string fio, string postId, string email, DateTime birthDate, DateTime employmentDate, bool isDeleted) : IValidation +{ + public string Id { get; private set; } = id; + + public string FIO { get; private set; } = fio; + + public string PostId { get; private set; } = postId; + + public string Email { get; private set; } = email; + + public DateTime BirthDate { get; private set; } = birthDate; + + public DateTime EmploymentDate { get; private set; } = employmentDate; + + 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 is not a unique identifier"); + + if (FIO.IsEmpty()) + throw new ValidationException("Field FIO is empty"); + + if (PostId.IsEmpty()) + throw new ValidationException("Field PostId is empty"); + + if (!PostId.IsGuid()) + throw new ValidationException("The value in the field PostId is not a unique identifier"); + + if (Email.IsEmpty()) + throw new ValidationException("Field Email is empty"); + + if (!Regex.IsMatch(Email, @"^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}$")) + throw new ValidationException("Field Email is not an email"); + + if (BirthDate.Date > DateTime.Now.AddYears(-16).Date) + throw new ValidationException($"Minors cannot be hired (BirthDate = {BirthDate.ToShortDateString()})"); + + if (EmploymentDate.Date < BirthDate.Date) + throw new ValidationException("The date of employment cannot be less than the date of birth"); + + if ((EmploymentDate - BirthDate).TotalDays / 365 < 16) + throw new ValidationException($"Minors cannot be hired (EmploymentDate - {EmploymentDate.ToShortDateString()}, BirthDate - {BirthDate.ToShortDateString()})"); + } +} diff --git a/TheSweetBunsProject/SweetBunsContracts/Enums/PostType.cs b/TheSweetBunsProject/SweetBunsContracts/Enums/PostType.cs new file mode 100644 index 0000000..ee73a10 --- /dev/null +++ b/TheSweetBunsProject/SweetBunsContracts/Enums/PostType.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace SweetBunsContracts.Enums; + +public enum PostType +{ + None = 0, + Preparation = 1, + Production = 2, + Packaging = 3 +} diff --git a/TheSweetBunsProject/SweetBunsContracts/Enums/ProductType.cs b/TheSweetBunsProject/SweetBunsContracts/Enums/ProductType.cs new file mode 100644 index 0000000..a94ccbb --- /dev/null +++ b/TheSweetBunsProject/SweetBunsContracts/Enums/ProductType.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace SweetBunsContracts.Enums; + +public enum ProductType +{ + None = 0, + Pie = 1, + Cookies = 2, + Croissant = 3 +} diff --git a/TheSweetBunsProject/SweetBunsContracts/Exceptions/ValidationException.cs b/TheSweetBunsProject/SweetBunsContracts/Exceptions/ValidationException.cs new file mode 100644 index 0000000..3cf6586 --- /dev/null +++ b/TheSweetBunsProject/SweetBunsContracts/Exceptions/ValidationException.cs @@ -0,0 +1,11 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace SweetBunsContracts.Exceptions; + +public class ValidationException(string message) : Exception(message) +{ +} diff --git a/TheSweetBunsProject/SweetBunsContracts/Extensions/StringExtensions.cs b/TheSweetBunsProject/SweetBunsContracts/Extensions/StringExtensions.cs new file mode 100644 index 0000000..85556d8 --- /dev/null +++ b/TheSweetBunsProject/SweetBunsContracts/Extensions/StringExtensions.cs @@ -0,0 +1,19 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace SweetBunsContracts.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 _); + } +} diff --git a/TheSweetBunsProject/SweetBunsContracts/Infrastructure/IValidation.cs b/TheSweetBunsProject/SweetBunsContracts/Infrastructure/IValidation.cs new file mode 100644 index 0000000..80eff46 --- /dev/null +++ b/TheSweetBunsProject/SweetBunsContracts/Infrastructure/IValidation.cs @@ -0,0 +1,6 @@ +namespace SweetBunsContracts.Infrastructure; + +public interface IValidation +{ + void Validate(); +} diff --git a/TheSweetBunsProject/SweetBunsTests/DataModels Tests/CookingDataModelTests.cs b/TheSweetBunsProject/SweetBunsTests/DataModels Tests/CookingDataModelTests.cs new file mode 100644 index 0000000..304a364 --- /dev/null +++ b/TheSweetBunsProject/SweetBunsTests/DataModels Tests/CookingDataModelTests.cs @@ -0,0 +1,61 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using SweetBunsContracts.DataModels; +using SweetBunsContracts.Exceptions; + +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()); + } + [Test] + public void WorkerIdIsNotGuidTest() + { + var cooking = CreateDataModel("id", Guid.NewGuid().ToString(), 10); + Assert.That(() => cooking.Validate(), Throws.TypeOf()); + } + [Test] + public void ProductIdIsNullOrEmptyTest() + { + var cooking = CreateDataModel(Guid.NewGuid().ToString(), string.Empty, 10); + Assert.That(() => cooking.Validate(), Throws.TypeOf()); + } + [Test] + public void ProductIdIsNotGuidTest() + { + var cooking = CreateDataModel(Guid.NewGuid().ToString(), "id", 10); + Assert.That(() => cooking.Validate(), Throws.TypeOf()); + } + [Test] + public void CountIsLessOrZeroTest() + { + var cooking = CreateDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 0); + Assert.That(() => cooking.Validate(), Throws.TypeOf()); + cooking = CreateDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), -10); + Assert.That(() => cooking.Validate(), Throws.TypeOf()); + } + [Test] + public void AllFieldsIsCorrectTest() + { + var workerId = Guid.NewGuid().ToString(); + var productId = Guid.NewGuid().ToString(); + var count = 10; + var cooking = CreateDataModel(workerId, productId, count); + Assert.That(() => cooking.Validate(), Throws.Nothing); + Assert.Multiple(() => + { + Assert.That(cooking.ProductId, Is.EqualTo(productId)); + Assert.That(cooking.Count, Is.EqualTo(count)); + }); + } + private static CookingDataModel CreateDataModel(string WorkerId, string ProductId, int Count) => new(WorkerId, ProductId, Count); +} diff --git a/TheSweetBunsProject/SweetBunsTests/DataModels Tests/IngredientsDataModelTests.cs b/TheSweetBunsProject/SweetBunsTests/DataModels Tests/IngredientsDataModelTests.cs new file mode 100644 index 0000000..347c39d --- /dev/null +++ b/TheSweetBunsProject/SweetBunsTests/DataModels Tests/IngredientsDataModelTests.cs @@ -0,0 +1,58 @@ +using SweetBunsContracts.DataModels; +using SweetBunsContracts.Exceptions; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace SweetBunsTests.DataModels_Tests; + +[TestFixture] +internal class IngredientsDataModelTests +{ + [Test] + public void IdIsNullEmptyTest() + { + var ingredient = CreateDataModel(null, "name"); + Assert.That(() => ingredient.Validate(), Throws.TypeOf()); + ingredient = CreateDataModel(string.Empty, "name"); + Assert.That(() => ingredient.Validate(), Throws.TypeOf()); + } + + [Test] + public void IdIsNotGuidTest() + { + var ingredient = CreateDataModel("id", "name"); + Assert.That(() => ingredient.Validate(), Throws.TypeOf()); + } + + [Test] + public void IngredientNameIsNullOrEmptyTest() + { + var ingredient = CreateDataModel(Guid.NewGuid().ToString(), null); + Assert.That(() => ingredient.Validate(), Throws.TypeOf()); + ingredient = CreateDataModel(Guid.NewGuid().ToString(), string.Empty); + Assert.That(() => ingredient.Validate(), Throws.TypeOf()); + } + + [Test] + public void AllFieldsIsCorrectTest() + { + var ingredientId = Guid.NewGuid().ToString(); + var ingredientName = "name"; + var prevIngredientName = "prevIngredientName"; + var prevPrevIngredientName = "prevPrevIngredientName"; + var ingredient = CreateDataModel(ingredientId, ingredientName, prevIngredientName, prevPrevIngredientName); + Assert.That(() => ingredient.Validate(), Throws.Nothing); + Assert.Multiple(() => + { + Assert.That(ingredient.Id, Is.EqualTo(ingredientId)); + Assert.That(ingredient.IngredientName, Is.EqualTo(ingredientName)); + Assert.That(ingredient.PrevIngredientName, Is.EqualTo(prevIngredientName)); + Assert.That(ingredient.PrevPrevIngredientName, Is.EqualTo(prevPrevIngredientName)); + }); + } + + private static IngredientsDataModel CreateDataModel(string? id, string? ingredientName, string? prevIngredientName = null, string? prevPrevIngredientName = null) => new(id, ingredientName, prevIngredientName, prevPrevIngredientName); +} diff --git a/TheSweetBunsProject/SweetBunsTests/DataModels Tests/PostDataModelTests.cs b/TheSweetBunsProject/SweetBunsTests/DataModels Tests/PostDataModelTests.cs new file mode 100644 index 0000000..3f1bed4 --- /dev/null +++ b/TheSweetBunsProject/SweetBunsTests/DataModels Tests/PostDataModelTests.cs @@ -0,0 +1,98 @@ +using SweetBunsContracts.DataModels; +using SweetBunsContracts.Enums; +using SweetBunsContracts.Exceptions; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace SweetBunsTests.DataModels_Tests; + +[TestFixture] +internal class PostDataModelTests +{ + [Test] + public void IdIsNullOrEmptyTest() + { + var post = CreateDataModel(null, Guid.NewGuid().ToString(), "name", PostType.Production, 10, true, DateTime.UtcNow); + Assert.That(() => post.Validate(), Throws.TypeOf()); + post = CreateDataModel(string.Empty, Guid.NewGuid().ToString(), "name", PostType.Production, 10, true, DateTime.UtcNow); + Assert.That(() => post.Validate(), Throws.TypeOf()); + } + + [Test] + public void IdIsNotGuidTest() + { + var post = CreateDataModel("id", Guid.NewGuid().ToString(), "name", PostType.Production, 10, true, DateTime.UtcNow); + Assert.That(() => post.Validate(), Throws.TypeOf()); + } + + [Test] + public void PostIdIsNullEmptyTest() + { + var post = CreateDataModel(Guid.NewGuid().ToString(), null, "name", PostType.Production, 10, true, DateTime.UtcNow); + Assert.That(() => post.Validate(), Throws.TypeOf()); + post = CreateDataModel(Guid.NewGuid().ToString(), string.Empty, "name", PostType.Production, 10, true, DateTime.UtcNow); + Assert.That(() => post.Validate(), Throws.TypeOf()); + } + + [Test] + public void PostIdIsNotGuidTest() + { + var post = CreateDataModel(Guid.NewGuid().ToString(), "postId", "name", PostType.Production, 10, true, DateTime.UtcNow); + Assert.That(() => post.Validate(), Throws.TypeOf()); + } + + [Test] + public void PostNameIsEmptyTest() + { + var post = CreateDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), null, PostType.Production, 10, true, DateTime.UtcNow); + Assert.That(() => post.Validate(), Throws.TypeOf()); + post = CreateDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), string.Empty, PostType.Production, 10, true, DateTime.UtcNow); + Assert.That(() => post.Validate(), Throws.TypeOf()); + } + + [Test] + public void PostTypeIsNoneTest() + { + var post = CreateDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), "name", PostType.None, 10, true, DateTime.UtcNow); + Assert.That(() => post.Validate(), Throws.TypeOf()); + } + + [Test] + public void SalaryIsLessOrZeroTest() + { + var post = CreateDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), "name", PostType.Production, 0, true, DateTime.UtcNow); + Assert.That(() => post.Validate(), Throws.TypeOf()); + post = CreateDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), "name", PostType.Production, -10, true, DateTime.UtcNow); + Assert.That(() => post.Validate(), Throws.TypeOf()); + } + + [Test] + public void AllFieldsIsCorrectTest() + { + var postId = Guid.NewGuid().ToString(); + var postPostId = Guid.NewGuid().ToString(); + var postName = "name"; + var postType = PostType.Production; + var salary = 10; + var isActual = false; + var changeDate = DateTime.UtcNow.AddDays(-1); + var post = CreateDataModel(postId, postPostId, postName, postType, + salary, isActual, changeDate); + Assert.That(() => post.Validate(), Throws.Nothing); + Assert.Multiple(() => + { + Assert.That(post.Id, Is.EqualTo(postId)); + Assert.That(post.PostId, Is.EqualTo(postPostId)); + Assert.That(post.PostName, Is.EqualTo(postName)); + Assert.That(post.PostType, Is.EqualTo(postType)); + Assert.That(post.Salary, Is.EqualTo(salary)); + Assert.That(post.IsActual, Is.EqualTo(isActual)); + Assert.That(post.ChangeDate, Is.EqualTo(changeDate)); + }); + } + + private static PostDataModel CreateDataModel(string? id, string? postId, string? postName, PostType postType, double salary, bool isActual, DateTime changeDate) => new(id, postId, postName, postType, salary, isActual, changeDate); +} diff --git a/TheSweetBunsProject/SweetBunsTests/DataModels Tests/ProductDataModelTests.cs b/TheSweetBunsProject/SweetBunsTests/DataModels Tests/ProductDataModelTests.cs new file mode 100644 index 0000000..19ce83b --- /dev/null +++ b/TheSweetBunsProject/SweetBunsTests/DataModels Tests/ProductDataModelTests.cs @@ -0,0 +1,89 @@ +using SweetBunsContracts.DataModels; +using SweetBunsContracts.Enums; +using SweetBunsContracts.Exceptions; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace SweetBunsTests.DataModels_Tests; + +[TestFixture] +internal class ProductDataModelTests +{ + [Test] + public void IdIsNullOrEmptyTest() + { + var product = CreateDataModel(null, "name", ProductType.Cookies, CreateSubDataModel(), 10, false); + Assert.That(() => product.Validate(), Throws.TypeOf()); + product = CreateDataModel(string.Empty, "name", ProductType.Cookies, CreateSubDataModel(), 10, false); + Assert.That(() => product.Validate(), Throws.TypeOf()); + } + + [Test] + public void IdIsNotGuidTest() + { + var product = CreateDataModel("id", "name", ProductType.Cookies, CreateSubDataModel(), 10, false); + Assert.That(() => product.Validate(), Throws.TypeOf()); + } + + [Test] + public void ProductNameIsEmptyTest() + { + var product = CreateDataModel(Guid.NewGuid().ToString(), null, ProductType.Cookies, CreateSubDataModel(), 10, false); + Assert.That(() => product.Validate(), Throws.TypeOf()); + product = CreateDataModel(Guid.NewGuid().ToString(), string.Empty, ProductType.Cookies, CreateSubDataModel(), 10, false); + Assert.That(() => product.Validate(), Throws.TypeOf()); + } + + [Test] + public void ProductTypeIsNoneTest() + { + var product = CreateDataModel(Guid.NewGuid().ToString(), null, ProductType.None, CreateSubDataModel(), 10, false); + Assert.That(() => product.Validate(), Throws.TypeOf()); + } + + [Test] + public void RecipesIsNullOrEmptyTest() + { + var product = CreateDataModel(Guid.NewGuid().ToString(), "name", ProductType.Cookies, null, 10, false); + Assert.That(() => product.Validate(), Throws.TypeOf()); + product = CreateDataModel(Guid.NewGuid().ToString(), "name", ProductType.Cookies, [], 10, false); + Assert.That(() => product.Validate(), Throws.TypeOf()); + } + + [Test] + public void PriceIsLessOrZeroTest() + { + var product = CreateDataModel(Guid.NewGuid().ToString(), "name", ProductType.Cookies, CreateSubDataModel(), 0, false); + Assert.That(() => product.Validate(), Throws.TypeOf()); + product = CreateDataModel(Guid.NewGuid().ToString(), "name", ProductType.Cookies, CreateSubDataModel(), -10, false); + Assert.That(() => product.Validate(), Throws.TypeOf()); + } + + [Test] + public void AllFieldsIsCorrectTest() + { + var productId = Guid.NewGuid().ToString(); + var productName = "name"; + var productType = ProductType.Cookies; + var recipes = CreateSubDataModel(); + var productPrice = 10; + var productIsDelete = false; + 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.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, List? recipes, double price, bool isDeleted) => new(id, productName, productType, recipes, price, isDeleted); + private static List CreateSubDataModel() => [new(Guid.NewGuid().ToString(), 1, Guid.NewGuid().ToString())]; +} \ No newline at end of file diff --git a/TheSweetBunsProject/SweetBunsTests/DataModels Tests/RecipeDataModelTests.cs b/TheSweetBunsProject/SweetBunsTests/DataModels Tests/RecipeDataModelTests.cs new file mode 100644 index 0000000..d920b9f --- /dev/null +++ b/TheSweetBunsProject/SweetBunsTests/DataModels Tests/RecipeDataModelTests.cs @@ -0,0 +1,72 @@ +using SweetBunsContracts.DataModels; +using SweetBunsContracts.Exceptions; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace SweetBunsTests.DataModels_Tests; + +[TestFixture] +internal class RecipeDataModelTests +{ + [Test] + public void IngredientsIdIsNullOrEmptyTest() + { + var recipe = CreateDataModel(null, 10, Guid.NewGuid().ToString()); + Assert.That(() => recipe.Validate(), Throws.TypeOf()); + recipe = CreateDataModel(string.Empty, 10, Guid.NewGuid().ToString()); + Assert.That(() => recipe.Validate(), Throws.TypeOf()); + } + + [Test] + public void IngredientsIdIsNotGuidTest() + { + var recipe = CreateDataModel("id", 10, Guid.NewGuid().ToString()); + Assert.That(() => recipe.Validate(), Throws.TypeOf()); + } + + [Test] + public void CountIsLessOrZeroTest() + { + var recipe = CreateDataModel(Guid.NewGuid().ToString(), 0, Guid.NewGuid().ToString()); + Assert.That(() => recipe.Validate(), Throws.TypeOf()); + recipe = CreateDataModel(Guid.NewGuid().ToString(), -10, Guid.NewGuid().ToString()); + Assert.That(() => recipe.Validate(), Throws.TypeOf()); + } + + [Test] + public void ProductIdIsNullOrEmptyTest() + { + var recipe = CreateDataModel(Guid.NewGuid().ToString(), 10, null); + Assert.That(() => recipe.Validate(), Throws.TypeOf()); + recipe = CreateDataModel(Guid.NewGuid().ToString(), 10, string.Empty); + Assert.That(() => recipe.Validate(), Throws.TypeOf()); + } + + [Test] + public void ProductIdIsNotGuidTest() + { + var recipe = CreateDataModel(Guid.NewGuid().ToString(), 10, "id"); + Assert.That(() => recipe.Validate(), Throws.TypeOf()); + } + + [Test] + public void AllFieldsIsCorrectTest() + { + var ingredientsId = Guid.NewGuid().ToString(); + var count = 10; + var productId = Guid.NewGuid().ToString(); + var recipe = CreateDataModel(ingredientsId, count, productId); + Assert.That(() => recipe.Validate(), Throws.Nothing); + Assert.Multiple(() => + { + Assert.That(recipe.IngredientsId, Is.EqualTo(ingredientsId)); + Assert.That(recipe.Count, Is.EqualTo(count)); + Assert.That(recipe.ProductId, Is.EqualTo(productId)); + }); + } + + private static RecipeDataModel CreateDataModel(string? ingredientsId, int count, string? productId) => new(ingredientsId, count, productId); +} diff --git a/TheSweetBunsProject/SweetBunsTests/DataModels Tests/SalaryDataModelTests.cs b/TheSweetBunsProject/SweetBunsTests/DataModels Tests/SalaryDataModelTests.cs new file mode 100644 index 0000000..bea0013 --- /dev/null +++ b/TheSweetBunsProject/SweetBunsTests/DataModels Tests/SalaryDataModelTests.cs @@ -0,0 +1,56 @@ +using SweetBunsContracts.DataModels; +using SweetBunsContracts.Exceptions; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace SweetBunsTests.DataModels_Tests; + +[TestFixture] +internal class SalaryDataModelTests +{ + [Test] + public void WorkerIdIsEmptyTest() + { + var salary = CreateDataModel(null, DateTime.Now, 10); + Assert.That(() => salary.Validate(), Throws.TypeOf()); + salary = CreateDataModel(string.Empty, DateTime.Now, 10); + Assert.That(() => salary.Validate(), Throws.TypeOf()); + } + + [Test] + public void WorkerIdIsNotGuidTest() + { + var salary = CreateDataModel("workerId", DateTime.Now, 10); + Assert.That(() => salary.Validate(), Throws.TypeOf()); + } + + [Test] + public void WorkerSalaryIsLessOrZeroTest() + { + var salary = CreateDataModel(Guid.NewGuid().ToString(), DateTime.Now, 0); + Assert.That(() => salary.Validate(), Throws.TypeOf()); + salary = CreateDataModel(Guid.NewGuid().ToString(), DateTime.Now, -10); + Assert.That(() => salary.Validate(), Throws.TypeOf()); + } + + [Test] + public void AllFieldsIsCorrectTest() + { + var workerId = Guid.NewGuid().ToString(); + var salaryDate = DateTime.Now.AddDays(-3).AddMinutes(-5); + var workerSalary = 10; + var salary = CreateDataModel(workerId, salaryDate, workerSalary); + Assert.That(() => salary.Validate(), Throws.Nothing); + Assert.Multiple(() => + { + Assert.That(salary.WorkerId, Is.EqualTo(workerId)); + Assert.That(salary.SalaryDate, Is.EqualTo(salaryDate)); + Assert.That(salary.Salary, Is.EqualTo(workerSalary)); + }); + } + + private static SalaryDataModel CreateDataModel(string? workerId, DateTime salaryDate, double workerSalary) => new(workerId, salaryDate, workerSalary); +} diff --git a/TheSweetBunsProject/SweetBunsTests/DataModels Tests/WorkerDataModelTests.cs b/TheSweetBunsProject/SweetBunsTests/DataModels Tests/WorkerDataModelTests.cs new file mode 100644 index 0000000..5cca5b7 --- /dev/null +++ b/TheSweetBunsProject/SweetBunsTests/DataModels Tests/WorkerDataModelTests.cs @@ -0,0 +1,112 @@ +using SweetBunsContracts.DataModels; +using SweetBunsContracts.Exceptions; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace SweetBunsTests.DataModels_Tests; + +[TestFixture] +internal class WorkerDataModelTests +{ + [Test] + public void IdIsNullOrEmptyTest() + { + var worker = CreateDataModel(null, "fio", Guid.NewGuid().ToString(), "email@email.com", DateTime.Now.AddYears(-18), DateTime.Now, false); + Assert.That(() => worker.Validate(), Throws.TypeOf()); + worker = CreateDataModel(string.Empty, "fio", Guid.NewGuid().ToString(), "email@email.com", DateTime.Now.AddYears(-18), DateTime.Now, false); + Assert.That(() => worker.Validate(), Throws.TypeOf()); + } + + [Test] + public void IdIsNotGuidTest() + { + var worker = CreateDataModel("id", "fio", Guid.NewGuid().ToString(), "email@email.com", DateTime.Now.AddYears(-18), DateTime.Now, false); + Assert.That(() => worker.Validate(), Throws.TypeOf()); + } + + [Test] + public void FIOIsNullOrEmptyTest() + { + var worker = CreateDataModel(Guid.NewGuid().ToString(), null, Guid.NewGuid().ToString(), "email@email.com", DateTime.Now.AddYears(-18), DateTime.Now, false); + Assert.That(() => worker.Validate(), Throws.TypeOf()); + worker = CreateDataModel(Guid.NewGuid().ToString(), string.Empty, Guid.NewGuid().ToString(), "email@email.com", DateTime.Now.AddYears(-18), DateTime.Now, false); + Assert.That(() => worker.Validate(), Throws.TypeOf()); + } + + [Test] + public void PostIdIsNullOrEmptyTest() + { + var worker = CreateDataModel(Guid.NewGuid().ToString(), "fio", null, "email@email.com", DateTime.Now.AddYears(-18), DateTime.Now, false); + Assert.That(() => worker.Validate(), Throws.TypeOf()); + worker = CreateDataModel(Guid.NewGuid().ToString(), "fio", string.Empty, "email@email.com", DateTime.Now.AddYears(-18), DateTime.Now, false); + Assert.That(() => worker.Validate(), Throws.TypeOf()); + } + + [Test] + public void PostIdIsNotGuidTest() + { + var worker = CreateDataModel("id", "fio", "postId", "email@email.com", DateTime.Now.AddYears(-18), DateTime.Now, false); + Assert.That(() => worker.Validate(), Throws.TypeOf()); + } + + [Test] + public void EmailIsNullOrEmptyTest() + { + var worker = CreateDataModel(Guid.NewGuid().ToString(), "fio", Guid.NewGuid().ToString(), null, DateTime.Now.AddYears(-18), DateTime.Now, false); + Assert.That(() => worker.Validate(), Throws.TypeOf()); + worker = CreateDataModel(Guid.NewGuid().ToString(), "fio", Guid.NewGuid().ToString(), string.Empty, DateTime.Now.AddYears(-18), DateTime.Now, false); + Assert.That(() => worker.Validate(), Throws.TypeOf()); + } + + [Test] + public void EmailIsIncorrectTest() + { + var worker = CreateDataModel(Guid.NewGuid().ToString(), "fio", Guid.NewGuid().ToString(), "edww@dqd,com", DateTime.Now.AddYears(-18), DateTime.Now, false); + Assert.That(() => worker.Validate(), Throws.TypeOf()); + } + + [Test] + public void BirthDateIsNotCorrectTest() + { + var worker = CreateDataModel(Guid.NewGuid().ToString(), "fio", Guid.NewGuid().ToString(), "email@email.com", DateTime.Now.AddYears(-16).AddDays(1), DateTime.Now, false); + Assert.That(() => worker.Validate(), Throws.TypeOf()); + } + + [Test] + public void BirthDateAndEmploymentDateIsNotCorrectTest() + { + var worker = CreateDataModel(Guid.NewGuid().ToString(), "fio", Guid.NewGuid().ToString(), "email@email.com", DateTime.Now.AddYears(-18), DateTime.Now.AddYears(-18).AddDays(-1), false); + Assert.That(() => worker.Validate(), Throws.TypeOf()); + worker = CreateDataModel(Guid.NewGuid().ToString(), "fio", Guid.NewGuid().ToString(), "email@email.com", DateTime.Now.AddYears(-18), DateTime.Now.AddYears(-16), false); + Assert.That(() => worker.Validate(), Throws.TypeOf()); + } + + [Test] + public void AllFieldsIsCorrectTest() + { + var workerId = Guid.NewGuid().ToString(); + var fio = "fio"; + var postId = Guid.NewGuid().ToString(); + var email = "email@email.com"; + var birthDate = DateTime.Now.AddYears(-16).AddDays(-1); + var employmentDate = DateTime.Now; + var isDelete = false; + var worker = CreateDataModel(workerId, fio, postId, email, birthDate, employmentDate, isDelete); + Assert.That(() => worker.Validate(), Throws.Nothing); + Assert.Multiple(() => + { + Assert.That(worker.Id, Is.EqualTo(workerId)); + Assert.That(worker.FIO, Is.EqualTo(fio)); + Assert.That(worker.PostId, Is.EqualTo(postId)); + Assert.That(worker.Email, Is.EqualTo(email)); + Assert.That(worker.BirthDate, Is.EqualTo(birthDate)); + Assert.That(worker.EmploymentDate, Is.EqualTo(employmentDate)); + Assert.That(worker.IsDeleted, Is.EqualTo(isDelete)); + }); + } + + private static WorkerDataModel CreateDataModel(string? id, string? fio, string? postId, string? email, DateTime birthDate, DateTime employmentDate, bool isDeleted) => new(id, fio, postId, email, birthDate, employmentDate, isDeleted); +} diff --git a/TheSweetBunsProject/SweetBunsTests/SweetBunsTests.csproj b/TheSweetBunsProject/SweetBunsTests/SweetBunsTests.csproj new file mode 100644 index 0000000..944a343 --- /dev/null +++ b/TheSweetBunsProject/SweetBunsTests/SweetBunsTests.csproj @@ -0,0 +1,27 @@ + + + + net9.0 + latest + enable + enable + false + + + + + + + + + + + + + + + + + + + diff --git a/TheSweetBunsProject/TheSweetBunsProject.sln b/TheSweetBunsProject/TheSweetBunsProject.sln index f5c285d..bc1edf8 100644 --- a/TheSweetBunsProject/TheSweetBunsProject.sln +++ b/TheSweetBunsProject/TheSweetBunsProject.sln @@ -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}") = "SweetBunsContracts", "SweetBunsContracts\SweetBunsContracts.csproj", "{EC2711C2-3C5B-4B61-B205-33C87C822197}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SweetBunsTests", "SweetBunsTests\SweetBunsTests.csproj", "{94AE5144-46DE-4C5A-AE9D-854B84A4ACD2}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -15,6 +17,10 @@ Global {EC2711C2-3C5B-4B61-B205-33C87C822197}.Debug|Any CPU.Build.0 = Debug|Any CPU {EC2711C2-3C5B-4B61-B205-33C87C822197}.Release|Any CPU.ActiveCfg = Release|Any CPU {EC2711C2-3C5B-4B61-B205-33C87C822197}.Release|Any CPU.Build.0 = Release|Any CPU + {94AE5144-46DE-4C5A-AE9D-854B84A4ACD2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {94AE5144-46DE-4C5A-AE9D-854B84A4ACD2}.Debug|Any CPU.Build.0 = Debug|Any CPU + {94AE5144-46DE-4C5A-AE9D-854B84A4ACD2}.Release|Any CPU.ActiveCfg = Release|Any CPU + {94AE5144-46DE-4C5A-AE9D-854B84A4ACD2}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE