From 7c1d37a0163e5ecd7a9cdec7917defbed72fa124 Mon Sep 17 00:00:00 2001 From: artur-kalimullin <144933634+artur-kalimullin@users.noreply.github.com> Date: Mon, 10 Feb 2025 16:16:05 +0400 Subject: [PATCH 1/4] =?UTF-8?q?=D0=9C=D0=BE=D0=B4=D0=B5=D0=BB=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../DataModels/BuyerDataModel.cs | 35 +++++++++++++ .../DataModels/ManufacturerDataModel.cs | 28 ++++++++++ .../DataModels/PostDataModel.cs | 47 +++++++++++++++++ .../DataModels/ProductDataModel.cs | 45 ++++++++++++++++ .../DataModels/ProductHistoryDataModel.cs | 26 ++++++++++ .../DataModels/SaleDataModel.cs | 52 +++++++++++++++++++ .../DataModels/SaleProductDataModel.cs | 32 ++++++++++++ .../DataModels/WorkerDataModel.cs | 47 +++++++++++++++++ .../Enums/DiscountType.cs | 10 ++++ .../SharikGiftShopContratcs/Enums/PostType.cs | 9 ++++ .../Enums/ProductType.cs | 11 ++++ .../Exceptions/ValidationException.cs | 5 ++ .../Extensions/StringExtensions.cs | 14 +++++ .../Infrastructure/IValidation.cs | 6 +++ 14 files changed, 367 insertions(+) create mode 100644 TheSharikGiftShopProject/SharikGiftShopContratcs/DataModels/BuyerDataModel.cs create mode 100644 TheSharikGiftShopProject/SharikGiftShopContratcs/DataModels/ManufacturerDataModel.cs create mode 100644 TheSharikGiftShopProject/SharikGiftShopContratcs/DataModels/PostDataModel.cs create mode 100644 TheSharikGiftShopProject/SharikGiftShopContratcs/DataModels/ProductDataModel.cs create mode 100644 TheSharikGiftShopProject/SharikGiftShopContratcs/DataModels/ProductHistoryDataModel.cs create mode 100644 TheSharikGiftShopProject/SharikGiftShopContratcs/DataModels/SaleDataModel.cs create mode 100644 TheSharikGiftShopProject/SharikGiftShopContratcs/DataModels/SaleProductDataModel.cs create mode 100644 TheSharikGiftShopProject/SharikGiftShopContratcs/DataModels/WorkerDataModel.cs create mode 100644 TheSharikGiftShopProject/SharikGiftShopContratcs/Enums/DiscountType.cs create mode 100644 TheSharikGiftShopProject/SharikGiftShopContratcs/Enums/PostType.cs create mode 100644 TheSharikGiftShopProject/SharikGiftShopContratcs/Enums/ProductType.cs create mode 100644 TheSharikGiftShopProject/SharikGiftShopContratcs/Exceptions/ValidationException.cs create mode 100644 TheSharikGiftShopProject/SharikGiftShopContratcs/Extensions/StringExtensions.cs create mode 100644 TheSharikGiftShopProject/SharikGiftShopContratcs/Infrastructure/IValidation.cs diff --git a/TheSharikGiftShopProject/SharikGiftShopContratcs/DataModels/BuyerDataModel.cs b/TheSharikGiftShopProject/SharikGiftShopContratcs/DataModels/BuyerDataModel.cs new file mode 100644 index 0000000..03c2276 --- /dev/null +++ b/TheSharikGiftShopProject/SharikGiftShopContratcs/DataModels/BuyerDataModel.cs @@ -0,0 +1,35 @@ +using SharikGiftShopContratcs.Exceptions; +using SharikGiftShopContratcs.Extensions; +using SharikGiftShopContratcs.Infrastructure; +using System.Text.RegularExpressions; + +namespace SharikGiftShopContratcs.DataModels; + +public class BuyerDataModel(string id, string fio, string phoneNumber, double discountSize) : IValidation +{ + public string Id { get; private set; } = id; + + public string FIO { get; private set; } = fio; + + 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 (FIO.IsEmpty()) + throw new ValidationException("Field FIO 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"); + } +} \ No newline at end of file diff --git a/TheSharikGiftShopProject/SharikGiftShopContratcs/DataModels/ManufacturerDataModel.cs b/TheSharikGiftShopProject/SharikGiftShopContratcs/DataModels/ManufacturerDataModel.cs new file mode 100644 index 0000000..66ea03d --- /dev/null +++ b/TheSharikGiftShopProject/SharikGiftShopContratcs/DataModels/ManufacturerDataModel.cs @@ -0,0 +1,28 @@ +using SharikGiftShopContratcs.Exceptions; +using SharikGiftShopContratcs.Extensions; +using SharikGiftShopContratcs.Infrastructure; + +namespace SharikGiftShopContratcs.DataModels; + +public class ManufacturerDataModel(string id, string manufacturerName, string? prevManufacturerName, string? prevPrevManufacturerName) : IValidation +{ + public string Id { get; private set; } = id; + + public string ManufacturerName { get; private set; } = manufacturerName; + + public string? PrevManufacturerName { get; private set; } = prevManufacturerName; + + public string? PrevPrevManufacturerName { get; private set; } = prevPrevManufacturerName; + + 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 (ManufacturerName.IsEmpty()) + throw new ValidationException("Field ManufacturerName is empty"); + } +} \ No newline at end of file diff --git a/TheSharikGiftShopProject/SharikGiftShopContratcs/DataModels/PostDataModel.cs b/TheSharikGiftShopProject/SharikGiftShopContratcs/DataModels/PostDataModel.cs new file mode 100644 index 0000000..ab2ded1 --- /dev/null +++ b/TheSharikGiftShopProject/SharikGiftShopContratcs/DataModels/PostDataModel.cs @@ -0,0 +1,47 @@ +using SharikGiftShopContratcs.Enums; +using SharikGiftShopContratcs.Exceptions; +using SharikGiftShopContratcs.Extensions; +using SharikGiftShopContratcs.Infrastructure; + +namespace SharikGiftShopContratcs.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"); + } +} \ No newline at end of file diff --git a/TheSharikGiftShopProject/SharikGiftShopContratcs/DataModels/ProductDataModel.cs b/TheSharikGiftShopProject/SharikGiftShopContratcs/DataModels/ProductDataModel.cs new file mode 100644 index 0000000..943266b --- /dev/null +++ b/TheSharikGiftShopProject/SharikGiftShopContratcs/DataModels/ProductDataModel.cs @@ -0,0 +1,45 @@ +using SharikGiftShopContratcs.Enums; +using SharikGiftShopContratcs.Exceptions; +using SharikGiftShopContratcs.Extensions; +using SharikGiftShopContratcs.Infrastructure; + +namespace SharikGiftShopContratcs.DataModels; + +public class ProductDataModel(string id, string productName, ProductType productType, string manufacturerId, 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 string ManufacturerId { get; private set; } = manufacturerId; + + 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 (ManufacturerId.IsEmpty()) + throw new ValidationException("Field ManufacturerId is empty"); + + if (!ManufacturerId.IsGuid()) + throw new ValidationException("The value in the field ManufacturerId is not a unique identifier"); + + if (Price <= 0) + throw new ValidationException("Field Price is less than or equal to 0"); + } +} \ No newline at end of file diff --git a/TheSharikGiftShopProject/SharikGiftShopContratcs/DataModels/ProductHistoryDataModel.cs b/TheSharikGiftShopProject/SharikGiftShopContratcs/DataModels/ProductHistoryDataModel.cs new file mode 100644 index 0000000..50df7b5 --- /dev/null +++ b/TheSharikGiftShopProject/SharikGiftShopContratcs/DataModels/ProductHistoryDataModel.cs @@ -0,0 +1,26 @@ +using SharikGiftShopContratcs.Exceptions; +using SharikGiftShopContratcs.Extensions; +using SharikGiftShopContratcs.Infrastructure; + +namespace SharikGiftShopContratcs.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"); + } +} \ No newline at end of file diff --git a/TheSharikGiftShopProject/SharikGiftShopContratcs/DataModels/SaleDataModel.cs b/TheSharikGiftShopProject/SharikGiftShopContratcs/DataModels/SaleDataModel.cs new file mode 100644 index 0000000..53d3a3c --- /dev/null +++ b/TheSharikGiftShopProject/SharikGiftShopContratcs/DataModels/SaleDataModel.cs @@ -0,0 +1,52 @@ +using SharikGiftShopContratcs.Enums; +using SharikGiftShopContratcs.Exceptions; +using SharikGiftShopContratcs.Extensions; +using SharikGiftShopContratcs.Infrastructure; + +namespace SharikGiftShopContratcs.DataModels; + +public class SaleDataModel(string id, string workerId, string? buyerId, double sum, + DiscountType discountType, double discount, bool isCancel, List products) : IValidation +{ + public string Id { get; private set; } = id; + + public string WorkerId { get; private set; } = workerId; + + public string? BuyerId { get; private set; } = buyerId; + + public DateTime SaleDate { get; private set; } = DateTime.UtcNow; + + public double Sum { get; private set; } = sum; + + public DiscountType DiscountType { get; private set; } = discountType; + + public double Discount { get; private set; } = discount; + + 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 (!BuyerId?.IsGuid() ?? !BuyerId?.IsEmpty() ?? false) + throw new ValidationException("The value in the field BuyerId 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"); + } +} \ No newline at end of file diff --git a/TheSharikGiftShopProject/SharikGiftShopContratcs/DataModels/SaleProductDataModel.cs b/TheSharikGiftShopProject/SharikGiftShopContratcs/DataModels/SaleProductDataModel.cs new file mode 100644 index 0000000..0f974b0 --- /dev/null +++ b/TheSharikGiftShopProject/SharikGiftShopContratcs/DataModels/SaleProductDataModel.cs @@ -0,0 +1,32 @@ +using SharikGiftShopContratcs.Exceptions; +using SharikGiftShopContratcs.Extensions; +using SharikGiftShopContratcs.Infrastructure; + +namespace SharikGiftShopContratcs.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"); + } +} \ No newline at end of file diff --git a/TheSharikGiftShopProject/SharikGiftShopContratcs/DataModels/WorkerDataModel.cs b/TheSharikGiftShopProject/SharikGiftShopContratcs/DataModels/WorkerDataModel.cs new file mode 100644 index 0000000..2adc0bc --- /dev/null +++ b/TheSharikGiftShopProject/SharikGiftShopContratcs/DataModels/WorkerDataModel.cs @@ -0,0 +1,47 @@ +using SharikGiftShopContratcs.Exceptions; +using SharikGiftShopContratcs.Extensions; +using SharikGiftShopContratcs.Infrastructure; + +namespace SharikGiftShopContratcs.DataModels; + +public class WorkerDataModel(string id, string fio, 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 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 (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) + throw new ValidationException($"Minors cannot be hired (EmploymentDate - {EmploymentDate.ToShortDateString()}, BirthDate - {BirthDate.ToShortDateString()})"); + } +} \ No newline at end of file diff --git a/TheSharikGiftShopProject/SharikGiftShopContratcs/Enums/DiscountType.cs b/TheSharikGiftShopProject/SharikGiftShopContratcs/Enums/DiscountType.cs new file mode 100644 index 0000000..6d9529f --- /dev/null +++ b/TheSharikGiftShopProject/SharikGiftShopContratcs/Enums/DiscountType.cs @@ -0,0 +1,10 @@ +namespace SharikGiftShopContratcs.Enums; + +[Flags] +public enum DiscountType +{ + None = 0, + OnSale = 1, + RegularCustomer = 2, + Certificate = 4 +} \ No newline at end of file diff --git a/TheSharikGiftShopProject/SharikGiftShopContratcs/Enums/PostType.cs b/TheSharikGiftShopProject/SharikGiftShopContratcs/Enums/PostType.cs new file mode 100644 index 0000000..b8b4b45 --- /dev/null +++ b/TheSharikGiftShopProject/SharikGiftShopContratcs/Enums/PostType.cs @@ -0,0 +1,9 @@ +namespace SharikGiftShopContratcs.Enums; + +public enum PostType +{ + None = 0, + Supervisor = 1, + CashierConsultant = 2, + Assistant = 3 +} \ No newline at end of file diff --git a/TheSharikGiftShopProject/SharikGiftShopContratcs/Enums/ProductType.cs b/TheSharikGiftShopProject/SharikGiftShopContratcs/Enums/ProductType.cs new file mode 100644 index 0000000..f32400c --- /dev/null +++ b/TheSharikGiftShopProject/SharikGiftShopContratcs/Enums/ProductType.cs @@ -0,0 +1,11 @@ +namespace SharikGiftShopContratcs.Enums; + +public enum ProductType +{ + None = 0, + MenGifts = 1, + WomenGifts = 2, + HomeGifts = 3, + Toy = 4, + Accessory = 5 +} \ No newline at end of file diff --git a/TheSharikGiftShopProject/SharikGiftShopContratcs/Exceptions/ValidationException.cs b/TheSharikGiftShopProject/SharikGiftShopContratcs/Exceptions/ValidationException.cs new file mode 100644 index 0000000..ec76cb1 --- /dev/null +++ b/TheSharikGiftShopProject/SharikGiftShopContratcs/Exceptions/ValidationException.cs @@ -0,0 +1,5 @@ +namespace SharikGiftShopContratcs.Exceptions; + +public class ValidationException(string message) : Exception(message) +{ +} \ No newline at end of file diff --git a/TheSharikGiftShopProject/SharikGiftShopContratcs/Extensions/StringExtensions.cs b/TheSharikGiftShopProject/SharikGiftShopContratcs/Extensions/StringExtensions.cs new file mode 100644 index 0000000..d23c084 --- /dev/null +++ b/TheSharikGiftShopProject/SharikGiftShopContratcs/Extensions/StringExtensions.cs @@ -0,0 +1,14 @@ +namespace SharikGiftShopContratcs.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 _); + } +} \ No newline at end of file diff --git a/TheSharikGiftShopProject/SharikGiftShopContratcs/Infrastructure/IValidation.cs b/TheSharikGiftShopProject/SharikGiftShopContratcs/Infrastructure/IValidation.cs new file mode 100644 index 0000000..540cd4e --- /dev/null +++ b/TheSharikGiftShopProject/SharikGiftShopContratcs/Infrastructure/IValidation.cs @@ -0,0 +1,6 @@ +namespace SharikGiftShopContratcs.Infrastructure; + +public interface IValidation +{ + void Validate(); +} \ No newline at end of file -- 2.25.1 From 24eb9d76dc58480e5f739a282e72b3dc804f0e8e Mon Sep 17 00:00:00 2001 From: artur-kalimullin <144933634+artur-kalimullin@users.noreply.github.com> Date: Mon, 10 Feb 2025 16:57:50 +0400 Subject: [PATCH 2/4] =?UTF-8?q?=D0=A2=D0=B5=D1=81=D1=82=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../DataModelsTests/BuyerDataModelTests.cs | 70 +++++++++++++ .../ManufacturerDataModelTests.cs | 54 +++++++++++ .../DataModelsTests/PostDataModelTests.cs | 93 ++++++++++++++++++ .../DataModelsTests/ProductDataModelTests.cs | 91 +++++++++++++++++ .../ProductHistoryDataModelTests.cs | 52 ++++++++++ .../DataModelsTests/SaleDataModelTests.cs | 97 +++++++++++++++++++ .../SaleProductDataModelTests.cs | 68 +++++++++++++ .../DataModelsTests/WorkerDataModelTests.cs | 90 +++++++++++++++++ .../SharikGiftShopTests.csproj | 27 ++++++ .../TheSharikGiftShopProject.sln | 6 ++ 10 files changed, 648 insertions(+) create mode 100644 TheSharikGiftShopProject/SharikGiftShopTests/DataModelsTests/BuyerDataModelTests.cs create mode 100644 TheSharikGiftShopProject/SharikGiftShopTests/DataModelsTests/ManufacturerDataModelTests.cs create mode 100644 TheSharikGiftShopProject/SharikGiftShopTests/DataModelsTests/PostDataModelTests.cs create mode 100644 TheSharikGiftShopProject/SharikGiftShopTests/DataModelsTests/ProductDataModelTests.cs create mode 100644 TheSharikGiftShopProject/SharikGiftShopTests/DataModelsTests/ProductHistoryDataModelTests.cs create mode 100644 TheSharikGiftShopProject/SharikGiftShopTests/DataModelsTests/SaleDataModelTests.cs create mode 100644 TheSharikGiftShopProject/SharikGiftShopTests/DataModelsTests/SaleProductDataModelTests.cs create mode 100644 TheSharikGiftShopProject/SharikGiftShopTests/DataModelsTests/WorkerDataModelTests.cs create mode 100644 TheSharikGiftShopProject/SharikGiftShopTests/SharikGiftShopTests.csproj diff --git a/TheSharikGiftShopProject/SharikGiftShopTests/DataModelsTests/BuyerDataModelTests.cs b/TheSharikGiftShopProject/SharikGiftShopTests/DataModelsTests/BuyerDataModelTests.cs new file mode 100644 index 0000000..314f884 --- /dev/null +++ b/TheSharikGiftShopProject/SharikGiftShopTests/DataModelsTests/BuyerDataModelTests.cs @@ -0,0 +1,70 @@ +using SharikGiftShopContratcs.DataModels; +using SharikGiftShopContratcs.Exceptions; + +namespace SharikGiftShopTests.DataModelsTests; + +[TestFixture] +internal class BuyerDataModelTests +{ + [Test] + public void IdIsNullOrEmptyTest() + { + var buyer = CreateDataModel(null, "fio", "number", 10); + Assert.That(() => buyer.Validate(), Throws.TypeOf()); + buyer = CreateDataModel(string.Empty, "fio", "number", 10); + Assert.That(() => buyer.Validate(), Throws.TypeOf()); + } + + [Test] + public void IdIsNotGuidTest() + { + var buyer = CreateDataModel("id", "fio", "number", 10); + Assert.That(() => buyer.Validate(), Throws.TypeOf()); + } + + [Test] + public void FIOIsNullOrEmptyTest() + { + var buyer = CreateDataModel(Guid.NewGuid().ToString(), null, "number", 10); + Assert.That(() => buyer.Validate(), Throws.TypeOf()); + buyer = CreateDataModel(Guid.NewGuid().ToString(), string.Empty, "number", 10); + Assert.That(() => buyer.Validate(), Throws.TypeOf()); + } + + [Test] + public void PhoneNumberIsNullOrEmptyTest() + { + var buyer = CreateDataModel(Guid.NewGuid().ToString(), "fio", null, 10); + Assert.That(() => buyer.Validate(), Throws.TypeOf()); + buyer = CreateDataModel(Guid.NewGuid().ToString(), "fio", string.Empty, 10); + Assert.That(() => buyer.Validate(), Throws.TypeOf()); + } + + [Test] + public void PhoneNumberIsIncorrectTest() + { + var buyer = CreateDataModel(Guid.NewGuid().ToString(), "fio", "777", 10); + Assert.That(() => buyer.Validate(), Throws.TypeOf()); + } + + [Test] + public void AllFieldsIsCorrectTest() + { + var buyerId = Guid.NewGuid().ToString(); + var fio = "Fio"; + var phoneNumber = "+7-777-777-77-77"; + var discountSize = 11; + var buyer = CreateDataModel(buyerId, fio, phoneNumber, discountSize); + Assert.That(() => buyer.Validate(), Throws.Nothing); + Assert.Multiple(() => + { + Assert.That(buyer.Id, Is.EqualTo(buyerId)); + Assert.That(buyer.FIO, Is.EqualTo(fio)); + Assert.That(buyer.PhoneNumber, Is.EqualTo(phoneNumber)); + Assert.That(buyer.DiscountSize, Is.EqualTo(discountSize)); + }); + } + + private static BuyerDataModel CreateDataModel(string? id, string? fio, string? phoneNumber, double discountSize) => + new(id, fio, phoneNumber, discountSize); +} \ No newline at end of file diff --git a/TheSharikGiftShopProject/SharikGiftShopTests/DataModelsTests/ManufacturerDataModelTests.cs b/TheSharikGiftShopProject/SharikGiftShopTests/DataModelsTests/ManufacturerDataModelTests.cs new file mode 100644 index 0000000..254b8f2 --- /dev/null +++ b/TheSharikGiftShopProject/SharikGiftShopTests/DataModelsTests/ManufacturerDataModelTests.cs @@ -0,0 +1,54 @@ +using SharikGiftShopContratcs.DataModels; +using SharikGiftShopContratcs.Exceptions; + +namespace SharikGiftShopTests.DataModelsTests; + +[TestFixture] +internal class ManufacturerDataModelTests +{ + [Test] + public void IdIsNullEmptyTest() + { + var manufacturer = CreateDataModel(null, "name"); + Assert.That(() => manufacturer.Validate(), Throws.TypeOf()); + manufacturer = CreateDataModel(string.Empty, "name"); + Assert.That(() => manufacturer.Validate(), Throws.TypeOf()); + } + + [Test] + public void IdIsNotGuidTest() + { + var manufacturer = CreateDataModel("id", "name"); + Assert.That(() => manufacturer.Validate(), Throws.TypeOf()); + } + + [Test] + public void ManufacturerNameIsNullOrEmptyTest() + { + var manufacturer = CreateDataModel(Guid.NewGuid().ToString(), null); + Assert.That(() => manufacturer.Validate(), Throws.TypeOf()); + manufacturer = CreateDataModel(Guid.NewGuid().ToString(), string.Empty); + Assert.That(() => manufacturer.Validate(), Throws.TypeOf()); + } + + [Test] + public void AllFieldsIsCorrectTest() + { + var manufacturerId = Guid.NewGuid().ToString(); + var manufacturerName = "name"; + var prevManufacturerName = "prevManufacturerName"; + var prevPrevManufacturerName = "prevPrevManufacturerName"; + var manufacturer = CreateDataModel(manufacturerId, manufacturerName, prevManufacturerName, prevPrevManufacturerName); + Assert.That(() => manufacturer.Validate(), Throws.Nothing); + Assert.Multiple(() => + { + Assert.That(manufacturer.Id, Is.EqualTo(manufacturerId)); + Assert.That(manufacturer.ManufacturerName, Is.EqualTo(manufacturerName)); + Assert.That(manufacturer.PrevManufacturerName, Is.EqualTo(prevManufacturerName)); + Assert.That(manufacturer.PrevPrevManufacturerName, Is.EqualTo(prevPrevManufacturerName)); + }); + } + + private static ManufacturerDataModel CreateDataModel(string? id, string? manufacturerName, string? prevManufacturerName = null, string? prevPrevManufacturerName = null) => + new(id, manufacturerName, prevManufacturerName, prevPrevManufacturerName); +} \ No newline at end of file diff --git a/TheSharikGiftShopProject/SharikGiftShopTests/DataModelsTests/PostDataModelTests.cs b/TheSharikGiftShopProject/SharikGiftShopTests/DataModelsTests/PostDataModelTests.cs new file mode 100644 index 0000000..6ef2662 --- /dev/null +++ b/TheSharikGiftShopProject/SharikGiftShopTests/DataModelsTests/PostDataModelTests.cs @@ -0,0 +1,93 @@ +using SharikGiftShopContratcs.DataModels; +using SharikGiftShopContratcs.Enums; +using SharikGiftShopContratcs.Exceptions; + +namespace SharikGiftShopTests.DataModelsTests; + +[TestFixture] +internal class PostDataModelTests +{ + [Test] + public void IdIsNullOrEmptyTest() + { + var post = CreateDataModel(null, Guid.NewGuid().ToString(), "name", PostType.Assistant, 10, true, DateTime.UtcNow); + Assert.That(() => post.Validate(), Throws.TypeOf()); + post = CreateDataModel(string.Empty, Guid.NewGuid().ToString(), "name", PostType.Assistant, 10, true, DateTime.UtcNow); + Assert.That(() => post.Validate(), Throws.TypeOf()); + } + + [Test] + public void IdIsNotGuidTest() + { + var post = CreateDataModel("id", Guid.NewGuid().ToString(), "name", PostType.Assistant, 10, true, DateTime.UtcNow); + Assert.That(() => post.Validate(), Throws.TypeOf()); + } + + [Test] + public void PostIdIsNullEmptyTest() + { + var post = CreateDataModel(Guid.NewGuid().ToString(), null, "name", PostType.Assistant, 10, true, DateTime.UtcNow); + Assert.That(() => post.Validate(), Throws.TypeOf()); + post = CreateDataModel(Guid.NewGuid().ToString(), string.Empty, "name", PostType.Assistant, 10, true, DateTime.UtcNow); + Assert.That(() => post.Validate(), Throws.TypeOf()); + } + + [Test] + public void PostIdIsNotGuidTest() + { + var post = CreateDataModel(Guid.NewGuid().ToString(), "postId", "name", PostType.Assistant, 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.Assistant, 10, true, DateTime.UtcNow); + Assert.That(() => manufacturer.Validate(), Throws.TypeOf()); + manufacturer = CreateDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), string.Empty, PostType.Assistant, 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.Assistant, 0, true, DateTime.UtcNow); + Assert.That(() => post.Validate(), Throws.TypeOf()); + post = CreateDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), "name", PostType.Assistant, -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.Assistant; + 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); +} \ No newline at end of file diff --git a/TheSharikGiftShopProject/SharikGiftShopTests/DataModelsTests/ProductDataModelTests.cs b/TheSharikGiftShopProject/SharikGiftShopTests/DataModelsTests/ProductDataModelTests.cs new file mode 100644 index 0000000..44b530b --- /dev/null +++ b/TheSharikGiftShopProject/SharikGiftShopTests/DataModelsTests/ProductDataModelTests.cs @@ -0,0 +1,91 @@ +using SharikGiftShopContratcs.DataModels; +using SharikGiftShopContratcs.Enums; +using SharikGiftShopContratcs.Exceptions; + +namespace SharikGiftShopTests.DataModelsTests; + +[TestFixture] +internal class ProductDataModelTests +{ + [Test] + public void IdIsNullOrEmptyTest() + { + var product = CreateDataModel(null, "name", ProductType.Accessory, Guid.NewGuid().ToString(), 10, false); + Assert.That(() => product.Validate(), Throws.TypeOf()); + product = CreateDataModel(string.Empty, "name", ProductType.Accessory, Guid.NewGuid().ToString(), 10, false); + Assert.That(() => product.Validate(), Throws.TypeOf()); + } + + [Test] + public void IdIsNotGuidTest() + { + var product = CreateDataModel("id", "name", ProductType.Accessory, Guid.NewGuid().ToString(), 10, false); + Assert.That(() => product.Validate(), Throws.TypeOf()); + } + + [Test] + public void ProductNameIsEmptyTest() + { + var product = CreateDataModel(Guid.NewGuid().ToString(), null, ProductType.Accessory, Guid.NewGuid().ToString(), 10, false); + Assert.That(() => product.Validate(), Throws.TypeOf()); + product = CreateDataModel(Guid.NewGuid().ToString(), string.Empty, ProductType.Accessory, Guid.NewGuid().ToString(), 10, false); + Assert.That(() => product.Validate(), Throws.TypeOf()); + } + + [Test] + public void ProductTypeIsNoneTest() + { + var product = CreateDataModel(Guid.NewGuid().ToString(), null, ProductType.None, Guid.NewGuid().ToString(), 10, false); + Assert.That(() => product.Validate(), Throws.TypeOf()); + } + + [Test] + public void ManufacturerIdIsNullOrEmptyTest() + { + var product = CreateDataModel(Guid.NewGuid().ToString(), "name", ProductType.Accessory, null, 10, false); + Assert.That(() => product.Validate(), Throws.TypeOf()); + product = CreateDataModel(Guid.NewGuid().ToString(), "name", ProductType.Accessory, string.Empty, 10, false); + Assert.That(() => product.Validate(), Throws.TypeOf()); + } + + [Test] + public void ManufacturerIdIsNotGuidTest() + { + var product = CreateDataModel(Guid.NewGuid().ToString(), "name", ProductType.Accessory, "manufacturerId", 10, false); + Assert.That(() => product.Validate(), Throws.TypeOf()); + } + + [Test] + public void PriceIsLessOrZeroTest() + { + var product = CreateDataModel(Guid.NewGuid().ToString(), "name", ProductType.Accessory, Guid.NewGuid().ToString(), 0, false); + Assert.That(() => product.Validate(), Throws.TypeOf()); + product = CreateDataModel(Guid.NewGuid().ToString(), "name", ProductType.Accessory, Guid.NewGuid().ToString(), -10, false); + Assert.That(() => product.Validate(), Throws.TypeOf()); + } + + [Test] + public void AllFieldsIsCorrectTest() + { + var productId = Guid.NewGuid().ToString(); + var productName = "name"; + var productType = ProductType.Accessory; + var productManufacturerId = Guid.NewGuid().ToString(); + var productPrice = 10; + var productIsDelete = false; + var product = CreateDataModel(productId, productName, productType, productManufacturerId, 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.ManufacturerId, Is.EqualTo(productManufacturerId)); + 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? manufacturerId, double price, bool isDeleted) => + new(id, productName, productType, manufacturerId, price, isDeleted); +} \ No newline at end of file diff --git a/TheSharikGiftShopProject/SharikGiftShopTests/DataModelsTests/ProductHistoryDataModelTests.cs b/TheSharikGiftShopProject/SharikGiftShopTests/DataModelsTests/ProductHistoryDataModelTests.cs new file mode 100644 index 0000000..29adeac --- /dev/null +++ b/TheSharikGiftShopProject/SharikGiftShopTests/DataModelsTests/ProductHistoryDataModelTests.cs @@ -0,0 +1,52 @@ +using SharikGiftShopContratcs.DataModels; +using SharikGiftShopContratcs.Exceptions; + +namespace SharikGiftShopTests.DataModelsTests; + +[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); +} \ No newline at end of file diff --git a/TheSharikGiftShopProject/SharikGiftShopTests/DataModelsTests/SaleDataModelTests.cs b/TheSharikGiftShopProject/SharikGiftShopTests/DataModelsTests/SaleDataModelTests.cs new file mode 100644 index 0000000..71fe8c2 --- /dev/null +++ b/TheSharikGiftShopProject/SharikGiftShopTests/DataModelsTests/SaleDataModelTests.cs @@ -0,0 +1,97 @@ +using SharikGiftShopContratcs.DataModels; +using SharikGiftShopContratcs.Enums; +using SharikGiftShopContratcs.Exceptions; + +namespace SharikGiftShopTests.DataModelsTests; + +[TestFixture] +internal class SaleDataModelTests +{ + [Test] + public void IdIsNullOrEmptyTest() + { + var sale = CreateDataModel(null, Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 10, DiscountType.OnSale, 10, false, CreateSubDataModel()); + Assert.That(() => sale.Validate(), Throws.TypeOf()); + sale = CreateDataModel(string.Empty, Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 10, DiscountType.OnSale, 10, false, CreateSubDataModel()); + Assert.That(() => sale.Validate(), Throws.TypeOf()); + } + + [Test] + public void IdIsNotGuidTest() + { + var sale = CreateDataModel("id", Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 10, DiscountType.OnSale, 10, false, CreateSubDataModel()); + Assert.That(() => sale.Validate(), Throws.TypeOf()); + } + [Test] + public void WorkerIdIsNullOrEmptyTest() + { + var sale = CreateDataModel(Guid.NewGuid().ToString(), null, Guid.NewGuid().ToString(), 10, DiscountType.OnSale, 10, false, CreateSubDataModel()); + Assert.That(() => sale.Validate(), Throws.TypeOf()); + sale = CreateDataModel(Guid.NewGuid().ToString(), string.Empty, Guid.NewGuid().ToString(), 10, DiscountType.OnSale, 10, false, CreateSubDataModel()); + Assert.That(() => sale.Validate(), Throws.TypeOf()); + } + + [Test] + public void WorkerIdIsNotGuidTest() + { + var sale = CreateDataModel(Guid.NewGuid().ToString(), "workerId", Guid.NewGuid().ToString(), 10, DiscountType.OnSale, 10, false, CreateSubDataModel()); + Assert.That(() => sale.Validate(), Throws.TypeOf()); + } + + [Test] + public void BuyerIdIsNotGuidTest() + { + var sale = CreateDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), "buyerId", 10, DiscountType.OnSale, 10, false, CreateSubDataModel()); + Assert.That(() => sale.Validate(), Throws.TypeOf()); + } + + [Test] + public void SumIsLessOrZeroTest() + { + var sale = CreateDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 0, DiscountType.OnSale, 10, false, CreateSubDataModel()); + Assert.That(() => sale.Validate(), Throws.TypeOf()); + sale = CreateDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), -10, DiscountType.OnSale, 10, false, CreateSubDataModel()); + Assert.That(() => sale.Validate(), Throws.TypeOf()); + } + + [Test] + public void ProductsIsNullOrEmptyTest() + { + var sale = CreateDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 10, DiscountType.OnSale, 10, false, null); + Assert.That(() => sale.Validate(), Throws.TypeOf()); + sale = CreateDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 10, DiscountType.OnSale, 10, false, []); + Assert.That(() => sale.Validate(), Throws.TypeOf()); + } + + [Test] + public void AllFieldsIsCorrectTest() + { + var saleId = Guid.NewGuid().ToString(); + var workerId = Guid.NewGuid().ToString(); + var buyerId = Guid.NewGuid().ToString(); + var sum = 10; + var discountType = DiscountType.Certificate; + var discount = 1; + var isCancel = true; + var products = CreateSubDataModel(); + var sale = CreateDataModel(saleId, workerId, buyerId, sum, discountType, discount, 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.BuyerId, Is.EqualTo(buyerId)); + Assert.That(sale.Sum, Is.EqualTo(sum)); + Assert.That(sale.DiscountType, Is.EqualTo(discountType)); + Assert.That(sale.Discount, Is.EqualTo(discount)); + Assert.That(sale.IsCancel, Is.EqualTo(isCancel)); + Assert.That(sale.Products, Is.EquivalentTo(products)); + }); + } + + private static SaleDataModel CreateDataModel(string? id, string? workerId, string? buyerId, double sum, DiscountType discountType, double discount, bool isCancel, List? products) => + new(id, workerId, buyerId, sum, discountType, discount, isCancel, products); + + private static List CreateSubDataModel() + => [new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 1)]; +} \ No newline at end of file diff --git a/TheSharikGiftShopProject/SharikGiftShopTests/DataModelsTests/SaleProductDataModelTests.cs b/TheSharikGiftShopProject/SharikGiftShopTests/DataModelsTests/SaleProductDataModelTests.cs new file mode 100644 index 0000000..f01412f --- /dev/null +++ b/TheSharikGiftShopProject/SharikGiftShopTests/DataModelsTests/SaleProductDataModelTests.cs @@ -0,0 +1,68 @@ +using SharikGiftShopContratcs.DataModels; +using SharikGiftShopContratcs.Exceptions; + +namespace SharikGiftShopTests.DataModelsTests; + +[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); +} \ No newline at end of file diff --git a/TheSharikGiftShopProject/SharikGiftShopTests/DataModelsTests/WorkerDataModelTests.cs b/TheSharikGiftShopProject/SharikGiftShopTests/DataModelsTests/WorkerDataModelTests.cs new file mode 100644 index 0000000..81569f6 --- /dev/null +++ b/TheSharikGiftShopProject/SharikGiftShopTests/DataModelsTests/WorkerDataModelTests.cs @@ -0,0 +1,90 @@ +using SharikGiftShopContratcs.DataModels; +using SharikGiftShopContratcs.Exceptions; + +namespace SharikGiftShopTests.DataModelsTests; + +[TestFixture] +internal class WorkerDataModelTests +{ + [Test] + public void IdIsNullOrEmptyTest() + { + var worker = CreateDataModel(null, "fio", Guid.NewGuid().ToString(), DateTime.Now.AddYears(-18), DateTime.Now, false); + Assert.That(() => worker.Validate(), Throws.TypeOf()); + worker = CreateDataModel(string.Empty, "fio", 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", 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, Guid.NewGuid().ToString(), DateTime.Now.AddYears(-18), DateTime.Now, false); + Assert.That(() => worker.Validate(), Throws.TypeOf()); + worker = CreateDataModel(Guid.NewGuid().ToString(), string.Empty, Guid.NewGuid().ToString(), 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, DateTime.Now.AddYears(-18), DateTime.Now, false); + Assert.That(() => worker.Validate(), Throws.TypeOf()); + worker = CreateDataModel(Guid.NewGuid().ToString(), "fio", 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", "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", 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", 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", 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 postId = Guid.NewGuid().ToString(); + var birthDate = DateTime.Now.AddYears(-16).AddDays(-1); + var employmentDate = DateTime.Now; + var isDelete = false; + var worker = CreateDataModel(workerId, fio, 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.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? postId, DateTime birthDate, DateTime employmentDate, bool isDeleted) => + new(id, fio, postId, birthDate, employmentDate, isDeleted); +} \ No newline at end of file diff --git a/TheSharikGiftShopProject/SharikGiftShopTests/SharikGiftShopTests.csproj b/TheSharikGiftShopProject/SharikGiftShopTests/SharikGiftShopTests.csproj new file mode 100644 index 0000000..5cdb382 --- /dev/null +++ b/TheSharikGiftShopProject/SharikGiftShopTests/SharikGiftShopTests.csproj @@ -0,0 +1,27 @@ + + + + net9.0 + latest + enable + enable + false + + + + + + + + + + + + + + + + + + + diff --git a/TheSharikGiftShopProject/TheSharikGiftShopProject.sln b/TheSharikGiftShopProject/TheSharikGiftShopProject.sln index ff6dc1f..fc91eaf 100644 --- a/TheSharikGiftShopProject/TheSharikGiftShopProject.sln +++ b/TheSharikGiftShopProject/TheSharikGiftShopProject.sln @@ -5,6 +5,8 @@ VisualStudioVersion = 17.12.35707.178 d17.12 MinimumVisualStudioVersion = 10.0.40219.1 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SharikGiftShopContratcs", "SharikGiftShopContratcs\SharikGiftShopContratcs.csproj", "{2771DF29-F7B7-4293-B174-79B0F576A545}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SharikGiftShopTests", "SharikGiftShopTests\SharikGiftShopTests.csproj", "{775F8DE2-3C02-4222-A3B1-DB5E877CD53F}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -15,6 +17,10 @@ Global {2771DF29-F7B7-4293-B174-79B0F576A545}.Debug|Any CPU.Build.0 = Debug|Any CPU {2771DF29-F7B7-4293-B174-79B0F576A545}.Release|Any CPU.ActiveCfg = Release|Any CPU {2771DF29-F7B7-4293-B174-79B0F576A545}.Release|Any CPU.Build.0 = Release|Any CPU + {775F8DE2-3C02-4222-A3B1-DB5E877CD53F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {775F8DE2-3C02-4222-A3B1-DB5E877CD53F}.Debug|Any CPU.Build.0 = Debug|Any CPU + {775F8DE2-3C02-4222-A3B1-DB5E877CD53F}.Release|Any CPU.ActiveCfg = Release|Any CPU + {775F8DE2-3C02-4222-A3B1-DB5E877CD53F}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE -- 2.25.1 From 4c15ad05dba8d177382611621f352287de5f1cd3 Mon Sep 17 00:00:00 2001 From: artur-kalimullin <144933634+artur-kalimullin@users.noreply.github.com> Date: Mon, 10 Feb 2025 19:13:03 +0400 Subject: [PATCH 3/4] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D0=B8=D0=B5=20=D0=BD=D0=BE=D0=B2=D0=BE=D0=B9=20=D0=BF?= =?UTF-8?q?=D1=80=D0=BE=D0=B2=D0=B5=D1=80=D0=BA=D0=B8=20=D1=81=20=D0=B8?= =?UTF-8?q?=D1=81=D0=BF=D0=BE=D0=BB=D1=8C=D0=B7=D0=BE=D0=B2=D0=B0=D0=BD?= =?UTF-8?q?=D0=B8=D0=B5=D0=BC=20=D1=80=D0=B5=D0=B3=D1=83=D0=BB=D1=8F=D1=80?= =?UTF-8?q?=D0=BD=D0=BE=D0=B3=D0=BE=20=D0=B2=D1=8B=D1=80=D0=B0=D0=B6=D0=B5?= =?UTF-8?q?=D0=BD=D0=B8=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../DataModels/BuyerDataModel.cs | 10 ++++- .../DataModelsTests/BuyerDataModelTests.cs | 41 ++++++++++++++----- 2 files changed, 39 insertions(+), 12 deletions(-) diff --git a/TheSharikGiftShopProject/SharikGiftShopContratcs/DataModels/BuyerDataModel.cs b/TheSharikGiftShopProject/SharikGiftShopContratcs/DataModels/BuyerDataModel.cs index 03c2276..92164b0 100644 --- a/TheSharikGiftShopProject/SharikGiftShopContratcs/DataModels/BuyerDataModel.cs +++ b/TheSharikGiftShopProject/SharikGiftShopContratcs/DataModels/BuyerDataModel.cs @@ -5,7 +5,7 @@ using System.Text.RegularExpressions; namespace SharikGiftShopContratcs.DataModels; -public class BuyerDataModel(string id, string fio, string phoneNumber, double discountSize) : IValidation +public class BuyerDataModel(string id, string fio, string phoneNumber, string email, double discountSize) : IValidation { public string Id { get; private set; } = id; @@ -13,6 +13,8 @@ public class BuyerDataModel(string id, string fio, string phoneNumber, double di public string PhoneNumber { get; private set; } = phoneNumber; + public string Email { get; private set; } = email; + public double DiscountSize { get; private set; } = discountSize; public void Validate() @@ -31,5 +33,11 @@ public class BuyerDataModel(string id, string fio, string phoneNumber, double di if (!Regex.IsMatch(PhoneNumber, @"^((8|\+7)[\- ]?)?(\(?\d{3}\)?[\- ]?)?[\d\- ]{7,10}$")) throw new ValidationException("Field PhoneNumber is not a phone number"); + + 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 a valid email address"); } } \ No newline at end of file diff --git a/TheSharikGiftShopProject/SharikGiftShopTests/DataModelsTests/BuyerDataModelTests.cs b/TheSharikGiftShopProject/SharikGiftShopTests/DataModelsTests/BuyerDataModelTests.cs index 314f884..3e364ff 100644 --- a/TheSharikGiftShopProject/SharikGiftShopTests/DataModelsTests/BuyerDataModelTests.cs +++ b/TheSharikGiftShopProject/SharikGiftShopTests/DataModelsTests/BuyerDataModelTests.cs @@ -9,41 +9,57 @@ internal class BuyerDataModelTests [Test] public void IdIsNullOrEmptyTest() { - var buyer = CreateDataModel(null, "fio", "number", 10); + var buyer = CreateDataModel(null, "fio", "number", "email@example.com", 10); Assert.That(() => buyer.Validate(), Throws.TypeOf()); - buyer = CreateDataModel(string.Empty, "fio", "number", 10); + buyer = CreateDataModel(string.Empty, "fio", "number", "email@example.com", 10); Assert.That(() => buyer.Validate(), Throws.TypeOf()); } [Test] public void IdIsNotGuidTest() { - var buyer = CreateDataModel("id", "fio", "number", 10); + var buyer = CreateDataModel("id", "fio", "number", "email@example.com", 10); Assert.That(() => buyer.Validate(), Throws.TypeOf()); } [Test] public void FIOIsNullOrEmptyTest() { - var buyer = CreateDataModel(Guid.NewGuid().ToString(), null, "number", 10); + var buyer = CreateDataModel(Guid.NewGuid().ToString(), null, "number", "email@example.com", 10); Assert.That(() => buyer.Validate(), Throws.TypeOf()); - buyer = CreateDataModel(Guid.NewGuid().ToString(), string.Empty, "number", 10); + buyer = CreateDataModel(Guid.NewGuid().ToString(), string.Empty, "number", "email@example.com", 10); Assert.That(() => buyer.Validate(), Throws.TypeOf()); } [Test] public void PhoneNumberIsNullOrEmptyTest() { - var buyer = CreateDataModel(Guid.NewGuid().ToString(), "fio", null, 10); + var buyer = CreateDataModel(Guid.NewGuid().ToString(), "fio", null, "email@example.com", 10); Assert.That(() => buyer.Validate(), Throws.TypeOf()); - buyer = CreateDataModel(Guid.NewGuid().ToString(), "fio", string.Empty, 10); + buyer = CreateDataModel(Guid.NewGuid().ToString(), "fio", string.Empty, "email@example.com", 10); Assert.That(() => buyer.Validate(), Throws.TypeOf()); } [Test] public void PhoneNumberIsIncorrectTest() { - var buyer = CreateDataModel(Guid.NewGuid().ToString(), "fio", "777", 10); + var buyer = CreateDataModel(Guid.NewGuid().ToString(), "fio", "777", "email@example.com", 10); + Assert.That(() => buyer.Validate(), Throws.TypeOf()); + } + + [Test] + public void EmailIsNullOrEmptyTest() + { + var buyer = CreateDataModel(Guid.NewGuid().ToString(), "fio", "number", null, 10); + Assert.That(() => buyer.Validate(), Throws.TypeOf()); + buyer = CreateDataModel(Guid.NewGuid().ToString(), "fio", "number", string.Empty, 10); + Assert.That(() => buyer.Validate(), Throws.TypeOf()); + } + + [Test] + public void EmailIsIncorrectTest() + { + var buyer = CreateDataModel(Guid.NewGuid().ToString(), "fio", "number", "invalid-email", 10); Assert.That(() => buyer.Validate(), Throws.TypeOf()); } @@ -53,18 +69,21 @@ internal class BuyerDataModelTests var buyerId = Guid.NewGuid().ToString(); var fio = "Fio"; var phoneNumber = "+7-777-777-77-77"; + var email = "email@example.com"; var discountSize = 11; - var buyer = CreateDataModel(buyerId, fio, phoneNumber, discountSize); + var buyer = CreateDataModel(buyerId, fio, phoneNumber, email, discountSize); Assert.That(() => buyer.Validate(), Throws.Nothing); Assert.Multiple(() => { Assert.That(buyer.Id, Is.EqualTo(buyerId)); Assert.That(buyer.FIO, Is.EqualTo(fio)); Assert.That(buyer.PhoneNumber, Is.EqualTo(phoneNumber)); + Assert.That(buyer.Email, Is.EqualTo(email)); Assert.That(buyer.DiscountSize, Is.EqualTo(discountSize)); }); } - private static BuyerDataModel CreateDataModel(string? id, string? fio, string? phoneNumber, double discountSize) => - new(id, fio, phoneNumber, discountSize); + private static BuyerDataModel CreateDataModel(string? id, string? fio, string? phoneNumber, + string? email, double discountSize) => + new(id, fio, phoneNumber, email, discountSize); } \ No newline at end of file -- 2.25.1 From 4720698b7a84e3f0090a4a9456d85b2f522222d4 Mon Sep 17 00:00:00 2001 From: artur-kalimullin <144933634+artur-kalimullin@users.noreply.github.com> Date: Thu, 13 Feb 2025 22:52:27 +0400 Subject: [PATCH 4/4] =?UTF-8?q?=D0=98=D0=B7=D0=BC=D0=B5=D0=BD=D0=B5=D0=BD?= =?UTF-8?q?=D0=B8=D1=8F=20=D0=BF=D0=BE=D1=81=D0=BB=D0=B5=20=D0=BF=D0=B0?= =?UTF-8?q?=D1=80=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../DataModels/BuyerDiscountDataModel.cs | 26 ++++++++++ .../BuyerDiscountDataModelTests.cs | 50 +++++++++++++++++++ 2 files changed, 76 insertions(+) create mode 100644 TheSharikGiftShopProject/SharikGiftShopContratcs/DataModels/BuyerDiscountDataModel.cs create mode 100644 TheSharikGiftShopProject/SharikGiftShopTests/DataModelsTests/BuyerDiscountDataModelTests.cs diff --git a/TheSharikGiftShopProject/SharikGiftShopContratcs/DataModels/BuyerDiscountDataModel.cs b/TheSharikGiftShopProject/SharikGiftShopContratcs/DataModels/BuyerDiscountDataModel.cs new file mode 100644 index 0000000..1e1aa86 --- /dev/null +++ b/TheSharikGiftShopProject/SharikGiftShopContratcs/DataModels/BuyerDiscountDataModel.cs @@ -0,0 +1,26 @@ +using SharikGiftShopContratcs.Exceptions; +using SharikGiftShopContratcs.Extensions; +using SharikGiftShopContratcs.Infrastructure; + +namespace SharikGiftShopContratcs.DataModels; + +public class BuyerDiscountDataModel(string buyerId, DateTime discountDate, double discountAmount) : IValidation +{ + public string BuyerId { get; private set; } = buyerId; + + public DateTime DiscountDate { get; private set; } = discountDate; + + public double DiscountAmount { get; private set; } = discountAmount; + + public void Validate() + { + if (BuyerId.IsEmpty()) + throw new ValidationException("Field BuyerId is empty"); + + if (!BuyerId.IsGuid()) + throw new ValidationException("The value in the field BuyerId is not a unique identifier"); + + if (DiscountAmount < 0) + throw new ValidationException("Field DiscountAmount in the field is less than 0"); + } +} \ No newline at end of file diff --git a/TheSharikGiftShopProject/SharikGiftShopTests/DataModelsTests/BuyerDiscountDataModelTests.cs b/TheSharikGiftShopProject/SharikGiftShopTests/DataModelsTests/BuyerDiscountDataModelTests.cs new file mode 100644 index 0000000..b400940 --- /dev/null +++ b/TheSharikGiftShopProject/SharikGiftShopTests/DataModelsTests/BuyerDiscountDataModelTests.cs @@ -0,0 +1,50 @@ +using SharikGiftShopContratcs.DataModels; +using SharikGiftShopContratcs.Exceptions; + +namespace SharikGiftShopTests.DataModelsTests; + +[TestFixture] +internal class BuyerDiscountDataModelTests +{ + [Test] + public void BuyerIdIsEmptyTest() + { + var discount = CreateDataModel(null, DateTime.Now, 10); + Assert.That(() => discount.Validate(), Throws.TypeOf()); + discount = CreateDataModel(string.Empty, DateTime.Now, 10); + Assert.That(() => discount.Validate(), Throws.TypeOf()); + } + + [Test] + public void BuyerIdIsNotGuidTest() + { + var discount = CreateDataModel("buyerId", DateTime.Now, 10); + Assert.That(() => discount.Validate(), Throws.TypeOf()); + } + + [Test] + public void DiscountAmountLessThanZeroTest() + { + var discount = CreateDataModel(Guid.NewGuid().ToString(), DateTime.Now, -10); + Assert.That(() => discount.Validate(), Throws.TypeOf()); + } + + [Test] + public void AllFieldsIsCorrectTest() + { + var buyerId = Guid.NewGuid().ToString(); + var discountDate = DateTime.Now.AddDays(-3).AddMinutes(-5); + var discountAmount = 10; + var discount = CreateDataModel(buyerId, discountDate, discountAmount); + Assert.That(() => discount.Validate(), Throws.Nothing); + Assert.Multiple(() => + { + Assert.That(discount.BuyerId, Is.EqualTo(buyerId)); + Assert.That(discount.DiscountDate, Is.EqualTo(discountDate)); + Assert.That(discount.DiscountAmount, Is.EqualTo(discountAmount)); + }); + } + + private static BuyerDiscountDataModel CreateDataModel(string? buyerId, DateTime discountDate, double discountAmount) => + new(buyerId, discountDate, discountAmount); +} \ No newline at end of file -- 2.25.1