diff --git a/TheButcherShopContracts/ButcherShopTests/ButcherShopTests.csproj b/TheButcherShopContracts/ButcherShopTests/ButcherShopTests.csproj new file mode 100644 index 0000000..d04c4a5 --- /dev/null +++ b/TheButcherShopContracts/ButcherShopTests/ButcherShopTests.csproj @@ -0,0 +1,28 @@ + + + + net8.0 + enable + enable + + false + true + + + + + + + + + + + + + + + + + + + diff --git a/TheButcherShopContracts/ButcherShopTests/DataModelTests/PostDataModelTests.cs b/TheButcherShopContracts/ButcherShopTests/DataModelTests/PostDataModelTests.cs new file mode 100644 index 0000000..d097d85 --- /dev/null +++ b/TheButcherShopContracts/ButcherShopTests/DataModelTests/PostDataModelTests.cs @@ -0,0 +1,98 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using TheButcherShopContracts.DataModels; +using TheButcherShopContracts.Enums; +using TheButcherShopContracts.Exceptions; + +namespace ButcherShopTests.DataModelTests; + +[TestFixture] +internal class PostDataModelTests +{ + [Test] + public void IdIsNullOrEmptyTest() + { + var post = CreateDataModel(null, Guid.NewGuid().ToString(), "name", PostType.Butcher, 10, true, DateTime.UtcNow); + Assert.That(() => post.Validate(), Throws.TypeOf()); + post = CreateDataModel(string.Empty, Guid.NewGuid().ToString(), "name", PostType.Butcher, 10, true, DateTime.UtcNow); + Assert.That(() => post.Validate(), Throws.TypeOf()); + } + + [Test] + public void IdIsNotGuidTest() + { + var post = CreateDataModel("id", Guid.NewGuid().ToString(), "name", PostType.Butcher, 10, true, DateTime.UtcNow); + Assert.That(() => post.Validate(), Throws.TypeOf()); + } + + [Test] + public void PostIdIsNullEmptyTest() + { + var post = CreateDataModel(Guid.NewGuid().ToString(), null, "name", PostType.Butcher, 10, true, DateTime.UtcNow); + Assert.That(() => post.Validate(), Throws.TypeOf()); + post = CreateDataModel(Guid.NewGuid().ToString(), string.Empty, "name", PostType.Butcher, 10, true, DateTime.UtcNow); + Assert.That(() => post.Validate(), Throws.TypeOf()); + } + + [Test] + public void PostIdIsNotGuidTest() + { + var post = CreateDataModel(Guid.NewGuid().ToString(), "postId", "name", PostType.Butcher, 10, true, DateTime.UtcNow); + Assert.That(() => post.Validate(), Throws.TypeOf()); + } + + [Test] + public void PostNameIsEmptyTest() + { + var manufacturer = CreateDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), null, PostType.Butcher, 10, true, DateTime.UtcNow); + Assert.That(() => manufacturer.Validate(), Throws.TypeOf()); + manufacturer = CreateDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), string.Empty, PostType.Butcher, 10, true, DateTime.UtcNow); + Assert.That(() => manufacturer.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.Butcher, 0, true, DateTime.UtcNow); + Assert.That(() => post.Validate(), Throws.TypeOf()); + post = CreateDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), "name", PostType.Butcher, -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.Butcher; + 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/TheButcherShopContracts/ButcherShopTests/DataModelTests/ProductDataModelTests.cs b/TheButcherShopContracts/ButcherShopTests/DataModelTests/ProductDataModelTests.cs new file mode 100644 index 0000000..30c6369 --- /dev/null +++ b/TheButcherShopContracts/ButcherShopTests/DataModelTests/ProductDataModelTests.cs @@ -0,0 +1,78 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using TheButcherShopContracts.Enums; +using TheButcherShopContracts.Exceptions; +using TheButcherShopContracts.DataModels; + +namespace ButcherShopTests.DataModelTests; + +[TestFixture] +internal class ProductDataModelTests +{ + [Test] + public void IdIsNullOrEmptyTest() + { + var product = CreateDataModel(null, "name", ProductType.FreshMeat, 10, false); + Assert.That(() => product.Validate(), Throws.TypeOf()); + product = CreateDataModel(string.Empty, "name", ProductType.FreshMeat, 10, false); + Assert.That(() => product.Validate(), Throws.TypeOf()); + } + + [Test] + public void IdIsNotGuidTest() + { + var product = CreateDataModel("id", "name", ProductType.FreshMeat, 10, false); + Assert.That(() => product.Validate(), Throws.TypeOf()); + } + + [Test] + public void ProductNameIsEmptyTest() + { + var product = CreateDataModel(Guid.NewGuid().ToString(), null, ProductType.FreshMeat, 10, false); + Assert.That(() => product.Validate(), Throws.TypeOf()); + product = CreateDataModel(Guid.NewGuid().ToString(), string.Empty, ProductType.FreshMeat, 10, false); + Assert.That(() => product.Validate(), Throws.TypeOf()); + } + + [Test] + public void ProductTypeIsNoneTest() + { + var product = CreateDataModel(Guid.NewGuid().ToString(), null, ProductType.None, 10, false); + Assert.That(() => product.Validate(), Throws.TypeOf()); + } + + [Test] + public void PriceIsLessOrZeroTest() + { + var product = CreateDataModel(Guid.NewGuid().ToString(), "name", ProductType.FreshMeat, 0, false); + Assert.That(() => product.Validate(), Throws.TypeOf()); + product = CreateDataModel(Guid.NewGuid().ToString(), "name", ProductType.FreshMeat, -10, false); + Assert.That(() => product.Validate(), Throws.TypeOf()); + } + + [Test] + public void AllFieldsIsCorrectTest() + { + var productId = Guid.NewGuid().ToString(); + var productName = "name"; + var productType = ProductType.FreshMeat; + var productPrice = 10; + var productIsDelete = false; + var product = CreateDataModel(productId, productName, productType, 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.Price, Is.EqualTo(productPrice)); + Assert.That(product.IsDeleted, Is.EqualTo(productIsDelete)); + }); + } + private static ProductDataModel CreateDataModel(string? id, string? productName, ProductType productType, double price, bool isDeleted) => + new(id, productName, productType, price, isDeleted); +} + diff --git a/TheButcherShopContracts/ButcherShopTests/DataModelTests/ProductHistoryDataModelTests.cs b/TheButcherShopContracts/ButcherShopTests/DataModelTests/ProductHistoryDataModelTests.cs new file mode 100644 index 0000000..b7c9456 --- /dev/null +++ b/TheButcherShopContracts/ButcherShopTests/DataModelTests/ProductHistoryDataModelTests.cs @@ -0,0 +1,56 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using TheButcherShopContracts.DataModels; +using TheButcherShopContracts.Exceptions; + +namespace ButcherShopTests.DataModelTests; + +[TestFixture] +internal class ProductHistoryDataModelTests +{ + [Test] + public void ProductIdIsNullOrEmptyTest() + { + var product = CreateDataModel(null, 10); + Assert.That(() => product.Validate(), Throws.TypeOf()); + product = CreateDataModel(string.Empty, 10); + Assert.That(() => product.Validate(), Throws.TypeOf()); + } + + [Test] + public void ProductIdIsNotGuidTest() + { + var product = CreateDataModel("id", 10); + Assert.That(() => product.Validate(), Throws.TypeOf()); + } + + [Test] + public void OldPriceIsLessOrZeroTest() + { + var product = CreateDataModel(Guid.NewGuid().ToString(), 0); + Assert.That(() => product.Validate(), Throws.TypeOf()); + product = CreateDataModel(Guid.NewGuid().ToString(), -10); + Assert.That(() => product.Validate(), Throws.TypeOf()); + } + + [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); +} diff --git a/TheButcherShopContracts/ButcherShopTests/DataModelTests/SalaryDataModelTests.cs b/TheButcherShopContracts/ButcherShopTests/DataModelTests/SalaryDataModelTests.cs new file mode 100644 index 0000000..adb6db8 --- /dev/null +++ b/TheButcherShopContracts/ButcherShopTests/DataModelTests/SalaryDataModelTests.cs @@ -0,0 +1,56 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using TheButcherShopContracts.DataModels; +using TheButcherShopContracts.Exceptions; + +namespace ButcherShopTests.DataModelTests; + +[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 PriceIsLessOrZeroTest() + { + 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/TheButcherShopContracts/ButcherShopTests/DataModelTests/SaleDataModelTests.cs b/TheButcherShopContracts/ButcherShopTests/DataModelTests/SaleDataModelTests.cs new file mode 100644 index 0000000..2def6ca --- /dev/null +++ b/TheButcherShopContracts/ButcherShopTests/DataModelTests/SaleDataModelTests.cs @@ -0,0 +1,90 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using TheButcherShopContracts.DataModels; +using TheButcherShopContracts.Exceptions; + +namespace ButcherShopTests.DataModelTests; + +[TestFixture] +internal class SaleDataModelTests +{ + [Test] + public void IdIsNullOrEmptyTest() + { + var sale = CreateDataModel(null, Guid.NewGuid().ToString(), 10, false, CreateSubDataModel()); + Assert.That(() => sale.Validate(), Throws.TypeOf()); + sale = CreateDataModel(string.Empty, Guid.NewGuid().ToString(), 10, false, CreateSubDataModel()); + Assert.That(() => sale.Validate(), Throws.TypeOf()); + } + + [Test] + public void IdIsNotGuidTest() + { + var sale = CreateDataModel("id", Guid.NewGuid().ToString(), 10, false, CreateSubDataModel()); + Assert.That(() => sale.Validate(),Throws.TypeOf()); + } + + [Test] + public void WorkerIdIsNullOrEmptyTest() + { + var sale = CreateDataModel(Guid.NewGuid().ToString(), null, 10, false, CreateSubDataModel()); + Assert.That(() => sale.Validate(), Throws.TypeOf()); + sale = CreateDataModel(Guid.NewGuid().ToString(), string.Empty, 10, false, CreateSubDataModel()); + Assert.That(() => sale.Validate(), Throws.TypeOf()); + } + + [Test] + public void WorkerIdIsNotGuidTest() + { + var sale = CreateDataModel(Guid.NewGuid().ToString(), "workerId", 10, false, CreateSubDataModel()); + Assert.That(() => sale.Validate(), Throws.TypeOf()); + } + + [Test] + public void SumIsLessOrZeroTest() + { + var sale = CreateDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 0, false, CreateSubDataModel()); + Assert.That(() => sale.Validate(), Throws.TypeOf()); + sale = CreateDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), -10, false, CreateSubDataModel()); + Assert.That(() => sale.Validate(), Throws.TypeOf()); + } + + [Test] + public void ProductsIsNullOrEmptyTest() + { + var sale = CreateDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 10, false, null); + Assert.That(() => sale.Validate(), Throws.TypeOf()); + sale = CreateDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 10, false, []); + Assert.That(() => sale.Validate(), Throws.TypeOf()); + } + + [Test] + public void AllFieldsIsCorrectTest() + { + var saleId = Guid.NewGuid().ToString(); + var workerId = Guid.NewGuid().ToString(); + var sum = 10; + var isCancel = true; + var products = CreateSubDataModel(); + var sale = CreateDataModel(saleId, workerId, sum, isCancel, products); + Assert.That(() => sale.Validate(), Throws.Nothing); + Assert.Multiple(() => + { + Assert.That(sale.Id, Is.EqualTo(saleId)); + Assert.That(sale.WorkerId, Is.EqualTo(workerId)); + Assert.That(sale.Sum, Is.EqualTo(sum)); + Assert.That(sale.IsCancel, Is.EqualTo(isCancel)); + Assert.That(sale.Products, Is.EquivalentTo(products)); + }); + } + private static SaleDataModel CreateDataModel(string? id, string? workerId,double sum, bool isCancel, List? products) + =>new(id, workerId, sum, isCancel, products); + private static List CreateSubDataModel() + => [new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 1)]; +} + + + diff --git a/TheButcherShopContracts/ButcherShopTests/DataModelTests/SaleProductDataModelTests.cs b/TheButcherShopContracts/ButcherShopTests/DataModelTests/SaleProductDataModelTests.cs new file mode 100644 index 0000000..52fc71c --- /dev/null +++ b/TheButcherShopContracts/ButcherShopTests/DataModelTests/SaleProductDataModelTests.cs @@ -0,0 +1,71 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using TheButcherShopContracts.DataModels; +using TheButcherShopContracts.Exceptions; + +namespace ButcherShopTests.DataModelTests; + +[TestFixture] +internal class SaleProductDataModelTests +{ + [Test] + public void SaleIdIsNullOrEmptyTest() + { + var saleProduct = CreateDataModel(null, Guid.NewGuid().ToString(), 10); + Assert.That(() => saleProduct.Validate(), Throws.TypeOf()); + saleProduct = CreateDataModel(string.Empty, Guid.NewGuid().ToString(), 10); + Assert.That(() => saleProduct.Validate(), Throws.TypeOf()); + } + + [Test] + public void SaleIdIsNotGuidTest() + { + var saleProduct = CreateDataModel("saleId", Guid.NewGuid().ToString(), 10); + Assert.That(() => saleProduct.Validate(), Throws.TypeOf()); + } + + [Test] + public void ProductIdIsNullOrEmptyTest() + { + var saleProduct = CreateDataModel(Guid.NewGuid().ToString(), null, 10); + Assert.That(() => saleProduct.Validate(), Throws.TypeOf()); + saleProduct = CreateDataModel(string.Empty, Guid.NewGuid().ToString(), 10); + Assert.That(() => saleProduct.Validate(), Throws.TypeOf()); + } + + [Test] + public void ProductIdIsNotGuidTest() + { + var saleProduct = CreateDataModel(Guid.NewGuid().ToString(), "productId", 10); + Assert.That(() => saleProduct.Validate(), Throws.TypeOf()); + } + + [Test] + public void CountIsLessOrZeroTest() + { + var saleProduct = CreateDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 0); + Assert.That(() => saleProduct.Validate(), Throws.TypeOf()); saleProduct = CreateDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), -10); + Assert.That(() => saleProduct.Validate(), Throws.TypeOf()); + } + + [Test] + public void AllFieldsIsCorrectTest() + { + var saleId = Guid.NewGuid().ToString(); + var productId = Guid.NewGuid().ToString(); + var count = 10; + var saleProduct = CreateDataModel(saleId, productId, count); + Assert.That(() => saleProduct.Validate(), Throws.Nothing); + Assert.Multiple(() => + { + Assert.That(saleProduct.SaleId, Is.EqualTo(saleId)); + Assert.That(saleProduct.ProductId, Is.EqualTo(productId)); + Assert.That(saleProduct.Count, Is.EqualTo(count)); + }); + } + private static SaleProductDataModel CreateDataModel(string? saleId, string? productId, int count) => + new(saleId, productId, count); +} diff --git a/TheButcherShopContracts/ButcherShopTests/DataModelTests/WorkerDataModelTests.cs b/TheButcherShopContracts/ButcherShopTests/DataModelTests/WorkerDataModelTests.cs new file mode 100644 index 0000000..d921f17 --- /dev/null +++ b/TheButcherShopContracts/ButcherShopTests/DataModelTests/WorkerDataModelTests.cs @@ -0,0 +1,113 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using TheButcherShopContracts.DataModels; +using TheButcherShopContracts.Exceptions; + +namespace ButcherShopTests.DataModelTests; + +[TestFixture] +internal class WorkerDataModelTests +{ + [Test] + public void IdIsNullOrEmptyTest() + { + var worker = CreateDataModel(null, "fio", "number", Guid.NewGuid().ToString(), DateTime.Now.AddYears(-18), DateTime.Now, false); + Assert.That(() => worker.Validate(), Throws.TypeOf()); + worker = CreateDataModel(string.Empty, "fio", "number", Guid.NewGuid().ToString(), DateTime.Now.AddYears(-18), DateTime.Now, false); + Assert.That(() => worker.Validate(), Throws.TypeOf()); + } + + [Test] + public void IdIsNotGuidTest() + { + var worker = CreateDataModel("id", "fio", "number", Guid.NewGuid().ToString(), DateTime.Now.AddYears(-18), DateTime.Now, false); + Assert.That(() => worker.Validate(), Throws.TypeOf()); + } + + [Test] + public void FIOIsNullOrEmptyTest() + { + var worker = CreateDataModel(Guid.NewGuid().ToString(), null, "number", Guid.NewGuid().ToString(), DateTime.Now.AddYears(-18), DateTime.Now, false); + Assert.That(() => worker.Validate(), Throws.TypeOf()); + worker = CreateDataModel(Guid.NewGuid().ToString(), string.Empty, "number", Guid.NewGuid().ToString(), DateTime.Now.AddYears(-18), DateTime.Now, false); + Assert.That(() => worker.Validate(), Throws.TypeOf()); + } + + [Test] + public void PhoneNumberIsNullOrEmptyTest() + { + var buyer = CreateDataModel(Guid.NewGuid().ToString(), "fio", null, Guid.NewGuid().ToString(), DateTime.Now.AddYears(-18), DateTime.Now, false); + Assert.That(() => buyer.Validate(), Throws.TypeOf()); + buyer = CreateDataModel(Guid.NewGuid().ToString(), "fio", string.Empty, Guid.NewGuid().ToString(), DateTime.Now.AddYears(-18), DateTime.Now, false); + Assert.That(() => buyer.Validate(), Throws.TypeOf()); + } + + [Test] + public void PhoneNumberIsIncorrectTest() + { + var buyer = CreateDataModel(Guid.NewGuid().ToString(), "fio", "777", Guid.NewGuid().ToString(), DateTime.Now.AddYears(-18), DateTime.Now, false); + Assert.That(() => buyer.Validate(), Throws.TypeOf()); + } + + [Test] + public void PostIdIsNullOrEmptyTest() + { + var worker = CreateDataModel(Guid.NewGuid().ToString(), "fio", "number", null, DateTime.Now.AddYears(-18), DateTime.Now, false); + Assert.That(() => worker.Validate(), Throws.TypeOf()); + worker = CreateDataModel(Guid.NewGuid().ToString(), "fio", "number", string.Empty, DateTime.Now.AddYears(-18), DateTime.Now, false); + Assert.That(() => worker.Validate(), Throws.TypeOf()); + } + + [Test] + public void PostIdIsNotGuidTest() + { + var worker = CreateDataModel(Guid.NewGuid().ToString(), "fio", "number", "postId", DateTime.Now.AddYears(-18), DateTime.Now, false); + Assert.That(() => worker.Validate(), Throws.TypeOf()); + } + + [Test] + public void BirthDateIsNotCorrectTest() + { + var worker = CreateDataModel(Guid.NewGuid().ToString(), "fio", "number", Guid.NewGuid().ToString(), 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", "number", Guid.NewGuid().ToString(), DateTime.Now.AddYears(-18), DateTime.Now.AddYears(-18).AddDays(-1), false); + Assert.That(() => worker.Validate(), Throws.TypeOf()); + worker = CreateDataModel(Guid.NewGuid().ToString(), "fio", "number", Guid.NewGuid().ToString(), 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 phoneNumber = "+7-777-777-77-77"; + var postId = Guid.NewGuid().ToString(); + var birthDate = DateTime.Now.AddYears(-16).AddDays(-1); + var employmentDate = DateTime.Now; + var isDelete = false; + var worker = CreateDataModel(workerId, fio, phoneNumber, postId, 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.PhoneNumber, Is.EqualTo(phoneNumber)); + Assert.That(worker.PostId, Is.EqualTo(postId)); + 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? phoneNumber, string? postId, DateTime birthDate, DateTime employmentDate, bool isDeleted) => + new(id, fio, phoneNumber, postId, birthDate, employmentDate, isDeleted); + +} diff --git a/TheButcherShopContracts/TheButcherShopContracts.sln b/TheButcherShopContracts/TheButcherShopContracts.sln index ed77f05..2146b6c 100644 --- a/TheButcherShopContracts/TheButcherShopContracts.sln +++ b/TheButcherShopContracts/TheButcherShopContracts.sln @@ -3,7 +3,9 @@ Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio Version 17 VisualStudioVersion = 17.11.35222.181 MinimumVisualStudioVersion = 10.0.40219.1 -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TheButcherShopContracts", "TheButcherShopContracts\TheButcherShopContracts.csproj", "{7F6E84F5-9CC5-4B63-BFCB-D87ACC271662}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TheButcherShopContracts", "TheButcherShopContracts\TheButcherShopContracts.csproj", "{7F6E84F5-9CC5-4B63-BFCB-D87ACC271662}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ButcherShopTests", "ButcherShopTests\ButcherShopTests.csproj", "{CE3DE04B-7F25-450D-8AEC-E28DF2B23818}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -15,6 +17,10 @@ Global {7F6E84F5-9CC5-4B63-BFCB-D87ACC271662}.Debug|Any CPU.Build.0 = Debug|Any CPU {7F6E84F5-9CC5-4B63-BFCB-D87ACC271662}.Release|Any CPU.ActiveCfg = Release|Any CPU {7F6E84F5-9CC5-4B63-BFCB-D87ACC271662}.Release|Any CPU.Build.0 = Release|Any CPU + {CE3DE04B-7F25-450D-8AEC-E28DF2B23818}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {CE3DE04B-7F25-450D-8AEC-E28DF2B23818}.Debug|Any CPU.Build.0 = Debug|Any CPU + {CE3DE04B-7F25-450D-8AEC-E28DF2B23818}.Release|Any CPU.ActiveCfg = Release|Any CPU + {CE3DE04B-7F25-450D-8AEC-E28DF2B23818}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/TheButcherShopContracts/TheButcherShopContracts/DataModels/PostDataModel.cs b/TheButcherShopContracts/TheButcherShopContracts/DataModels/PostDataModel.cs new file mode 100644 index 0000000..a01c419 --- /dev/null +++ b/TheButcherShopContracts/TheButcherShopContracts/DataModels/PostDataModel.cs @@ -0,0 +1,48 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Xml; +using TheButcherShopContracts.Enums; +using TheButcherShopContracts.Exceptions; +using TheButcherShopContracts.Extensions; +using TheButcherShopContracts.Infrastructure; + +namespace TheButcherShopContracts.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/TheButcherShopContracts/TheButcherShopContracts/DataModels/ProductDataModel.cs b/TheButcherShopContracts/TheButcherShopContracts/DataModels/ProductDataModel.cs new file mode 100644 index 0000000..385d7aa --- /dev/null +++ b/TheButcherShopContracts/TheButcherShopContracts/DataModels/ProductDataModel.cs @@ -0,0 +1,40 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Xml; +using TheButcherShopContracts.Enums; +using TheButcherShopContracts.Exceptions; +using TheButcherShopContracts.Extensions; +using TheButcherShopContracts.Infrastructure; + +namespace TheButcherShopContracts.DataModels; + +public class ProductDataModel(string id, string productName, ProductType productType, 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 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 (Price <= 0) + throw new ValidationException("Field Price is less than or equal to 0"); + } +} diff --git a/TheButcherShopContracts/TheButcherShopContracts/DataModels/ProductHistoryDataModel.cs b/TheButcherShopContracts/TheButcherShopContracts/DataModels/ProductHistoryDataModel.cs new file mode 100644 index 0000000..72e9aa2 --- /dev/null +++ b/TheButcherShopContracts/TheButcherShopContracts/DataModels/ProductHistoryDataModel.cs @@ -0,0 +1,29 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using TheButcherShopContracts.Exceptions; +using TheButcherShopContracts.Extensions; +using TheButcherShopContracts.Infrastructure; + +namespace TheButcherShopContracts.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"); + } +} diff --git a/TheButcherShopContracts/TheButcherShopContracts/DataModels/SalaryDataModel.cs b/TheButcherShopContracts/TheButcherShopContracts/DataModels/SalaryDataModel.cs new file mode 100644 index 0000000..8cdb4e0 --- /dev/null +++ b/TheButcherShopContracts/TheButcherShopContracts/DataModels/SalaryDataModel.cs @@ -0,0 +1,29 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using TheButcherShopContracts.Exceptions; +using TheButcherShopContracts.Extensions; +using TheButcherShopContracts.Infrastructure; + +namespace TheButcherShopContracts.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/TheButcherShopContracts/TheButcherShopContracts/DataModels/SaleDataModel.cs b/TheButcherShopContracts/TheButcherShopContracts/DataModels/SaleDataModel.cs new file mode 100644 index 0000000..391652f --- /dev/null +++ b/TheButcherShopContracts/TheButcherShopContracts/DataModels/SaleDataModel.cs @@ -0,0 +1,42 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Xml; +using TheButcherShopContracts.Exceptions; +using TheButcherShopContracts.Extensions; +using TheButcherShopContracts.Infrastructure; + +namespace TheButcherShopContracts.DataModels; + +public class SaleDataModel(string id, string workerId, double sum, bool isCancel, List products) : IValidation +{ + public string Id { get; private set; } = id; + public string WorkerId { get; private set; } = workerId; + public DateTime SaleDate { get; private set; } = DateTime.UtcNow; + public double Sum { get; private set; } = sum; + public bool IsCancel { get; private set; } = isCancel; + public List Products { get; private set; } = products; + + 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 (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 (Sum <= 0) + throw new ValidationException("Field Sum is less than or equal to 0"); + + if ((Products?.Count ?? 0) == 0) + throw new ValidationException("The sale must include products"); + } +} diff --git a/TheButcherShopContracts/TheButcherShopContracts/DataModels/SaleProductDataModel.cs b/TheButcherShopContracts/TheButcherShopContracts/DataModels/SaleProductDataModel.cs new file mode 100644 index 0000000..fd1c066 --- /dev/null +++ b/TheButcherShopContracts/TheButcherShopContracts/DataModels/SaleProductDataModel.cs @@ -0,0 +1,34 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using TheButcherShopContracts.Exceptions; +using TheButcherShopContracts.Extensions; +using TheButcherShopContracts.Infrastructure; + +namespace TheButcherShopContracts.DataModels; + +public class SaleProductDataModel(string saleId, string productId, int count) : IValidation +{ + public string SaleId { get; private set; } = saleId; + public string ProductId { get; private set; } = productId; + public int Count { get; private set; } = count; + public void Validate() + { + if (SaleId.IsEmpty()) + throw new ValidationException("Field SaleId is empty"); + + if (!SaleId.IsGuid()) + throw new ValidationException("The value in the field SaleId 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/TheButcherShopContracts/TheButcherShopContracts/DataModels/WorkerDataModel.cs b/TheButcherShopContracts/TheButcherShopContracts/DataModels/WorkerDataModel.cs new file mode 100644 index 0000000..c641146 --- /dev/null +++ b/TheButcherShopContracts/TheButcherShopContracts/DataModels/WorkerDataModel.cs @@ -0,0 +1,55 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Text.RegularExpressions; +using System.Threading.Tasks; +using System.Xml; +using TheButcherShopContracts.Exceptions; +using TheButcherShopContracts.Extensions; +using TheButcherShopContracts.Infrastructure; +using static System.Runtime.InteropServices.JavaScript.JSType; + +namespace TheButcherShopContracts.DataModels; + +public class WorkerDataModel(string id, string fio, string phoneNumber, string postId, DateTime birthDate, DateTime employmentDate, bool isDeleted) : IValidation +{ + public string Id { get; private set; } = id; + public string FIO { get; private set; } = fio; + public string PhoneNumber { get; private set; } = phoneNumber; + public string PostId { get; private set; } = postId; + 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 (PhoneNumber.IsEmpty()) { throw new ValidationException("Field PhoneNumber is empty"); } + + if (!Regex.IsMatch(PhoneNumber, @"^\+7[\s-]?\d{3}[\s-]?\d{3}[\s-]?\d{2}[\s-]?\d{2}$")) { throw new ValidationException("Field PhoneNumber is not a phone number"); } + + 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 (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) // EmploymentDate.Year - BirthDate.Year + throw new ValidationException($"Minors cannot be hired (EmploymentDate - { EmploymentDate.ToShortDateString() }, BirthDate - BirthDate.ToShortDateString())"); + } +} diff --git a/TheButcherShopContracts/TheButcherShopContracts/Enums/PostType.cs b/TheButcherShopContracts/TheButcherShopContracts/Enums/PostType.cs new file mode 100644 index 0000000..46cfa1f --- /dev/null +++ b/TheButcherShopContracts/TheButcherShopContracts/Enums/PostType.cs @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace TheButcherShopContracts.Enums; + +public enum PostType +{ + None = 0, + Butcher = 1 , // Мясник (занимается разделкой мяса) + Packer = 2, // Фасовщик (занимается фасовкой товаров) + Salesperson = 3, // Продавец (занимается продажей товаров) + Administrator = 4, // Администратор (административная работа) +} diff --git a/TheButcherShopContracts/TheButcherShopContracts/Enums/ProductType.cs b/TheButcherShopContracts/TheButcherShopContracts/Enums/ProductType.cs new file mode 100644 index 0000000..5791d77 --- /dev/null +++ b/TheButcherShopContracts/TheButcherShopContracts/Enums/ProductType.cs @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace TheButcherShopContracts.Enums; + +public enum ProductType +{ + None = 0, + FreshMeat = 1, // Свежее мясо + SemiFinished = 2, // Полуфабрикаты + SausagesAndSmoked = 3, // Колбасные изделия и копчености + OffalAndDelicacies = 4 // Субпродукты и деликатесы +} diff --git a/TheButcherShopContracts/TheButcherShopContracts/Exceptions/ValidationException.cs b/TheButcherShopContracts/TheButcherShopContracts/Exceptions/ValidationException.cs new file mode 100644 index 0000000..51db8cd --- /dev/null +++ b/TheButcherShopContracts/TheButcherShopContracts/Exceptions/ValidationException.cs @@ -0,0 +1,11 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace TheButcherShopContracts.Exceptions; + +public class ValidationException(string message) : Exception(message) +{ +} diff --git a/TheButcherShopContracts/TheButcherShopContracts/Extensions/StringExtensions.cs b/TheButcherShopContracts/TheButcherShopContracts/Extensions/StringExtensions.cs new file mode 100644 index 0000000..b5547b8 --- /dev/null +++ b/TheButcherShopContracts/TheButcherShopContracts/Extensions/StringExtensions.cs @@ -0,0 +1,21 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace TheButcherShopContracts.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/TheButcherShopContracts/TheButcherShopContracts/Infrastructure/IValidation.cs b/TheButcherShopContracts/TheButcherShopContracts/Infrastructure/IValidation.cs new file mode 100644 index 0000000..e76964f --- /dev/null +++ b/TheButcherShopContracts/TheButcherShopContracts/Infrastructure/IValidation.cs @@ -0,0 +1,12 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace TheButcherShopContracts.Infrastructure; + +public interface IValidation +{ + void Validate(); +}