From 86596a6c3007aa4c14018a65aa6e33dd5e97fac1 Mon Sep 17 00:00:00 2001 From: Glliza Date: Mon, 10 Feb 2025 01:13:25 +0400 Subject: [PATCH 1/5] =?UTF-8?q?=D0=92=20=D0=BF=D1=80=D0=BE=D1=86=D0=B5?= =?UTF-8?q?=D1=81=D1=81=D0=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../DataModels/BuyerDataModel.cs | 38 +++++++++++++++++++ .../DataModels/PostDataModel.cs | 12 ++++++ .../PuferFishContracts/Enums/PostType.cs | 16 ++++++++ .../Exceptions/ValidationException.cs | 11 ++++++ .../Extensions/StringExtensions.cs | 21 ++++++++++ .../Infrastructure/IValidation.cs | 13 +++++++ 6 files changed, 111 insertions(+) create mode 100644 PuferFishContracts/PuferFishContracts/DataModels/BuyerDataModel.cs create mode 100644 PuferFishContracts/PuferFishContracts/DataModels/PostDataModel.cs create mode 100644 PuferFishContracts/PuferFishContracts/Enums/PostType.cs create mode 100644 PuferFishContracts/PuferFishContracts/Exceptions/ValidationException.cs create mode 100644 PuferFishContracts/PuferFishContracts/Extensions/StringExtensions.cs create mode 100644 PuferFishContracts/PuferFishContracts/Infrastructure/IValidation.cs diff --git a/PuferFishContracts/PuferFishContracts/DataModels/BuyerDataModel.cs b/PuferFishContracts/PuferFishContracts/DataModels/BuyerDataModel.cs new file mode 100644 index 0000000..0d8eb78 --- /dev/null +++ b/PuferFishContracts/PuferFishContracts/DataModels/BuyerDataModel.cs @@ -0,0 +1,38 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Text.RegularExpressions; +using System.Threading.Tasks; +using PuferFishContracts.Extensions; +using PuferFishContracts.Exceptions; +using PuferFishContracts.Infrastructure; + +namespace PuferFishContracts.DataModels +{ + public class BuyerDataModel(string id, string fio, string phoneNumber, double points) : IValidation + { + public string Id { get; private set; } = id; + public string FIO { get; private set; } = fio; + public string PhoneNumber { get; private set; } = phoneNumber; + public double Points { get; private set; } = points; + + 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/PuferFishContracts/PuferFishContracts/DataModels/PostDataModel.cs b/PuferFishContracts/PuferFishContracts/DataModels/PostDataModel.cs new file mode 100644 index 0000000..12de3f4 --- /dev/null +++ b/PuferFishContracts/PuferFishContracts/DataModels/PostDataModel.cs @@ -0,0 +1,12 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace PuferFishContracts.DataModels +{ + internal class PostDataModel + { + } +} diff --git a/PuferFishContracts/PuferFishContracts/Enums/PostType.cs b/PuferFishContracts/PuferFishContracts/Enums/PostType.cs new file mode 100644 index 0000000..3f63881 --- /dev/null +++ b/PuferFishContracts/PuferFishContracts/Enums/PostType.cs @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace PuferFishContracts.Enums +{ + public enum PostType + { + None = 0, + Cashier = 1, + chef = 2, + Packer = 3 + } +} diff --git a/PuferFishContracts/PuferFishContracts/Exceptions/ValidationException.cs b/PuferFishContracts/PuferFishContracts/Exceptions/ValidationException.cs new file mode 100644 index 0000000..0bf2b23 --- /dev/null +++ b/PuferFishContracts/PuferFishContracts/Exceptions/ValidationException.cs @@ -0,0 +1,11 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace PuferFishContracts.Exceptions; + +public class ValidationException(string message) : Exception(message) +{ +} diff --git a/PuferFishContracts/PuferFishContracts/Extensions/StringExtensions.cs b/PuferFishContracts/PuferFishContracts/Extensions/StringExtensions.cs new file mode 100644 index 0000000..9756569 --- /dev/null +++ b/PuferFishContracts/PuferFishContracts/Extensions/StringExtensions.cs @@ -0,0 +1,21 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace PuferFishContracts.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 var _); + } + } +} diff --git a/PuferFishContracts/PuferFishContracts/Infrastructure/IValidation.cs b/PuferFishContracts/PuferFishContracts/Infrastructure/IValidation.cs new file mode 100644 index 0000000..75037b5 --- /dev/null +++ b/PuferFishContracts/PuferFishContracts/Infrastructure/IValidation.cs @@ -0,0 +1,13 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace PuferFishContracts.Infrastructure +{ + public interface IValidation + { + void Validate(); + } +} -- 2.25.1 From e0794e352daccbc82eebe4cdaa180adc05a70c4f Mon Sep 17 00:00:00 2001 From: Glliza Date: Mon, 10 Feb 2025 23:38:41 +0400 Subject: [PATCH 2/5] =?UTF-8?q?=D0=92=20=D0=BF=D1=80=D0=BE=D1=86=D0=B5?= =?UTF-8?q?=D1=81=D1=81=D0=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../DataModels/PostDataModel.cs | 29 ++++++++++++- .../DataModels/ProductDataModel.cs | 34 +++++++++++++++ .../DataModels/ProductHistoryDataModel.cs | 26 ++++++++++++ .../DataModels/SaleProductDataModel.cs | 30 ++++++++++++++ .../DataModels/WorkerDataModel.cs | 41 +++++++++++++++++++ .../PuferFishContracts/Enums/ProductType.cs | 16 ++++++++ 6 files changed, 175 insertions(+), 1 deletion(-) create mode 100644 PuferFishContracts/PuferFishContracts/DataModels/ProductDataModel.cs create mode 100644 PuferFishContracts/PuferFishContracts/DataModels/ProductHistoryDataModel.cs create mode 100644 PuferFishContracts/PuferFishContracts/DataModels/SaleProductDataModel.cs create mode 100644 PuferFishContracts/PuferFishContracts/DataModels/WorkerDataModel.cs create mode 100644 PuferFishContracts/PuferFishContracts/Enums/ProductType.cs diff --git a/PuferFishContracts/PuferFishContracts/DataModels/PostDataModel.cs b/PuferFishContracts/PuferFishContracts/DataModels/PostDataModel.cs index 12de3f4..dfd74d8 100644 --- a/PuferFishContracts/PuferFishContracts/DataModels/PostDataModel.cs +++ b/PuferFishContracts/PuferFishContracts/DataModels/PostDataModel.cs @@ -3,10 +3,37 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; +using PuferFishContracts.Enums; +using PuferFishContracts.Extensions; +using PuferFishContracts.Exceptions; +using PuferFishContracts.Infrastructure; +using System.Xml; namespace PuferFishContracts.DataModels { - internal class PostDataModel + public class PostDataModel(string id, string postId, string postName, PostType postType, 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 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"); + } + } } diff --git a/PuferFishContracts/PuferFishContracts/DataModels/ProductDataModel.cs b/PuferFishContracts/PuferFishContracts/DataModels/ProductDataModel.cs new file mode 100644 index 0000000..9c669cc --- /dev/null +++ b/PuferFishContracts/PuferFishContracts/DataModels/ProductDataModel.cs @@ -0,0 +1,34 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Xml; +using PuferFishContracts.Enums; +using PuferFishContracts.Extensions; +using PuferFishContracts.Exceptions; +using PuferFishContracts.Infrastructure; + +namespace PuferFishContracts.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/PuferFishContracts/PuferFishContracts/DataModels/ProductHistoryDataModel.cs b/PuferFishContracts/PuferFishContracts/DataModels/ProductHistoryDataModel.cs new file mode 100644 index 0000000..c43f929 --- /dev/null +++ b/PuferFishContracts/PuferFishContracts/DataModels/ProductHistoryDataModel.cs @@ -0,0 +1,26 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using PuferFishContracts.Extensions; +using PuferFishContracts.Exceptions; +using PuferFishContracts.Infrastructure; + +namespace PuferFishContracts.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/PuferFishContracts/PuferFishContracts/DataModels/SaleProductDataModel.cs b/PuferFishContracts/PuferFishContracts/DataModels/SaleProductDataModel.cs new file mode 100644 index 0000000..aa84efe --- /dev/null +++ b/PuferFishContracts/PuferFishContracts/DataModels/SaleProductDataModel.cs @@ -0,0 +1,30 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using PuferFishContracts.Extensions; +using PuferFishContracts.Exceptions; +using PuferFishContracts.Infrastructure; + +namespace PuferFishContracts.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/PuferFishContracts/PuferFishContracts/DataModels/WorkerDataModel.cs b/PuferFishContracts/PuferFishContracts/DataModels/WorkerDataModel.cs new file mode 100644 index 0000000..639515f --- /dev/null +++ b/PuferFishContracts/PuferFishContracts/DataModels/WorkerDataModel.cs @@ -0,0 +1,41 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using PuferFishContracts.Extensions; +using PuferFishContracts.Exceptions; +using System.Xml; +using PuferFishContracts.Infrastructure; +using static System.Runtime.InteropServices.JavaScript.JSType; + +namespace PuferFishContracts.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) //EmploymentDate.Year - BirthDate.Year + throw new ValidationException($"Minors cannot be hired (EmploymentDate - { EmploymentDate.ToShortDateString() }, BirthDate - { BirthDate.ToShortDateString()})"); + } +} diff --git a/PuferFishContracts/PuferFishContracts/Enums/ProductType.cs b/PuferFishContracts/PuferFishContracts/Enums/ProductType.cs new file mode 100644 index 0000000..a0459a0 --- /dev/null +++ b/PuferFishContracts/PuferFishContracts/Enums/ProductType.cs @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace PuferFishContracts.Enums +{ + public enum ProductType + { + None = 0, + Sushi = 1, + Rolls = 2, + Onigiri = 3 + } +} -- 2.25.1 From 096e427095b86e6ed631c4676d7b406c32bb5a66 Mon Sep 17 00:00:00 2001 From: Glliza Date: Tue, 11 Feb 2025 15:25:07 +0400 Subject: [PATCH 3/5] =?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/PointsDataModel.cs | 26 ++++++++++++ .../DataModels/SaleDataModel.cs | 41 +++++++++++++++++++ 2 files changed, 67 insertions(+) create mode 100644 PuferFishContracts/PuferFishContracts/DataModels/PointsDataModel.cs create mode 100644 PuferFishContracts/PuferFishContracts/DataModels/SaleDataModel.cs diff --git a/PuferFishContracts/PuferFishContracts/DataModels/PointsDataModel.cs b/PuferFishContracts/PuferFishContracts/DataModels/PointsDataModel.cs new file mode 100644 index 0000000..bf3b0e8 --- /dev/null +++ b/PuferFishContracts/PuferFishContracts/DataModels/PointsDataModel.cs @@ -0,0 +1,26 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using PuferFishContracts.Infrastructure; +using PuferFishContracts.Extensions; +using PuferFishContracts.Exceptions; + +namespace PuferFishContracts.DataModels; + +public class PointsDataModel(string buyerId, DateTime pointsDate, double buyerPoints) : IValidation +{ + public string BuyerId { get; private set; } = buyerId; + public DateTime PointsDate { get; private set; } = pointsDate; + public double Points { get; private set; } = buyerPoints; + 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 (Points <= 0) + throw new ValidationException("Field Points is less than or equal to 0"); + } +} diff --git a/PuferFishContracts/PuferFishContracts/DataModels/SaleDataModel.cs b/PuferFishContracts/PuferFishContracts/DataModels/SaleDataModel.cs new file mode 100644 index 0000000..3ff5b0a --- /dev/null +++ b/PuferFishContracts/PuferFishContracts/DataModels/SaleDataModel.cs @@ -0,0 +1,41 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using PuferFishContracts.Infrastructure; +using PuferFishContracts.Extensions; +using PuferFishContracts.Exceptions; +using System.Xml; + +namespace PuferFishContracts.DataModels; + +public class SaleDataModel(string id, string workerId, string? buyerId, double sum, double points, 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 double Points { get; private set; } = points; + 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"); + } +} -- 2.25.1 From 7e405913214011eab6551074f2245e653dd6d508 Mon Sep 17 00:00:00 2001 From: Glliza Date: Wed, 12 Feb 2025 21:26:30 +0400 Subject: [PATCH 4/5] =?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 --- PuferFishContracts/PuferFishContracts.sln | 8 +- .../DataModels/BuyerDataModel.cs | 2 + .../DataModelsTests/BuyerDataModelTests.cs | 82 ++++++++++++ .../DataModelsTests/PointsDataModelTests.cs | 60 +++++++++ .../DataModelsTests/PostDataModelTests.cs | 112 ++++++++++++++++ .../DataModelsTests/ProductDataModelTests.cs | 88 ++++++++++++ .../ProductHistoryDataModelTests.cs | 61 +++++++++ .../DataModelsTests/SaleDataModelTests.cs | 125 ++++++++++++++++++ .../SaleProductDataModelTests.cs | 81 ++++++++++++ .../DataModelsTests/WorkerDataModelTests.cs | 115 ++++++++++++++++ .../PuferFishTests/PuferFishTests.csproj | 27 ++++ 11 files changed, 760 insertions(+), 1 deletion(-) create mode 100644 PuferFishContracts/PuferFishTests/DataModelsTests/BuyerDataModelTests.cs create mode 100644 PuferFishContracts/PuferFishTests/DataModelsTests/PointsDataModelTests.cs create mode 100644 PuferFishContracts/PuferFishTests/DataModelsTests/PostDataModelTests.cs create mode 100644 PuferFishContracts/PuferFishTests/DataModelsTests/ProductDataModelTests.cs create mode 100644 PuferFishContracts/PuferFishTests/DataModelsTests/ProductHistoryDataModelTests.cs create mode 100644 PuferFishContracts/PuferFishTests/DataModelsTests/SaleDataModelTests.cs create mode 100644 PuferFishContracts/PuferFishTests/DataModelsTests/SaleProductDataModelTests.cs create mode 100644 PuferFishContracts/PuferFishTests/DataModelsTests/WorkerDataModelTests.cs create mode 100644 PuferFishContracts/PuferFishTests/PuferFishTests.csproj diff --git a/PuferFishContracts/PuferFishContracts.sln b/PuferFishContracts/PuferFishContracts.sln index c9f9325..7378568 100644 --- a/PuferFishContracts/PuferFishContracts.sln +++ b/PuferFishContracts/PuferFishContracts.sln @@ -1,10 +1,12 @@  Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio Version 17 -VisualStudioVersion = 17.12.35707.178 d17.12 +VisualStudioVersion = 17.12.35707.178 MinimumVisualStudioVersion = 10.0.40219.1 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PuferFishContracts", "PuferFishContracts\PuferFishContracts.csproj", "{4D964053-55A4-4522-BF52-984A22880D24}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PuferFishTests", "PuferFishTests\PuferFishTests.csproj", "{6A249C1E-4321-4506-841B-2BCE80F550A5}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -15,6 +17,10 @@ Global {4D964053-55A4-4522-BF52-984A22880D24}.Debug|Any CPU.Build.0 = Debug|Any CPU {4D964053-55A4-4522-BF52-984A22880D24}.Release|Any CPU.ActiveCfg = Release|Any CPU {4D964053-55A4-4522-BF52-984A22880D24}.Release|Any CPU.Build.0 = Release|Any CPU + {6A249C1E-4321-4506-841B-2BCE80F550A5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {6A249C1E-4321-4506-841B-2BCE80F550A5}.Debug|Any CPU.Build.0 = Debug|Any CPU + {6A249C1E-4321-4506-841B-2BCE80F550A5}.Release|Any CPU.ActiveCfg = Release|Any CPU + {6A249C1E-4321-4506-841B-2BCE80F550A5}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/PuferFishContracts/PuferFishContracts/DataModels/BuyerDataModel.cs b/PuferFishContracts/PuferFishContracts/DataModels/BuyerDataModel.cs index 0d8eb78..3b61284 100644 --- a/PuferFishContracts/PuferFishContracts/DataModels/BuyerDataModel.cs +++ b/PuferFishContracts/PuferFishContracts/DataModels/BuyerDataModel.cs @@ -33,6 +33,8 @@ namespace PuferFishContracts.DataModels if (!Regex.IsMatch(PhoneNumber, @"^((8|\+7)[\- ]?)?(\(?\d{3}\)?[\- ]?)?[\d\- ]{7,10}$")) throw new ValidationException("Field PhoneNumber is not a phone number"); + if (Points < 0) + throw new ValidationException("The value in the field Points is a negative number"); } } } \ No newline at end of file diff --git a/PuferFishContracts/PuferFishTests/DataModelsTests/BuyerDataModelTests.cs b/PuferFishContracts/PuferFishTests/DataModelsTests/BuyerDataModelTests.cs new file mode 100644 index 0000000..ab09bba --- /dev/null +++ b/PuferFishContracts/PuferFishTests/DataModelsTests/BuyerDataModelTests.cs @@ -0,0 +1,82 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using PuferFishContracts.DataModels; +using PuferFishContracts.Exceptions; + +namespace PuferFishTests.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 points = 11; + var buyer = CreateDataModel(buyerId, fio, phoneNumber, points); + 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.Points, Is.EqualTo(points)); + }); + } + private static BuyerDataModel CreateDataModel(string? id, string? fio, string? phoneNumber, double points) => new(id, fio, phoneNumber, points); + } +} diff --git a/PuferFishContracts/PuferFishTests/DataModelsTests/PointsDataModelTests.cs b/PuferFishContracts/PuferFishTests/DataModelsTests/PointsDataModelTests.cs new file mode 100644 index 0000000..c8ec73e --- /dev/null +++ b/PuferFishContracts/PuferFishTests/DataModelsTests/PointsDataModelTests.cs @@ -0,0 +1,60 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using PuferFishContracts.DataModels; +using PuferFishContracts.Exceptions; + +namespace PuferFishTests.DataModelsTests; + +[TestFixture] +internal class PointsDataModelTests +{ + [Test] + public void WorkerIdIsEmptyTest() + { + var points = CreateDataModel(null, DateTime.Now, 10); + Assert.That(() => points.Validate(), + Throws.TypeOf()); + points = CreateDataModel(string.Empty, DateTime.Now, 10); + Assert.That(() => points.Validate(), + Throws.TypeOf()); + } + [Test] + public void WorkerIdIsNotGuidTest() + { + var points = CreateDataModel("workerId", DateTime.Now, 10); + Assert.That(() => points.Validate(), + Throws.TypeOf()); + } + [Test] + public void PriceIsLessOrZeroTest() + { + var points = CreateDataModel(Guid.NewGuid().ToString(), DateTime.Now, + 0); + Assert.That(() => points.Validate(), + Throws.TypeOf()); + points = CreateDataModel(Guid.NewGuid().ToString(), DateTime.Now, - + 10); + Assert.That(() => points.Validate(), + Throws.TypeOf()); + } + [Test] + public void AllFieldsIsCorrectTest() + { + var buyerId = Guid.NewGuid().ToString(); + var pointsDate = DateTime.Now.AddDays(-3).AddMinutes(-5); + var buyerPoints = 10; + var points = CreateDataModel(buyerId, pointsDate, buyerPoints); + Assert.That(() => points.Validate(), Throws.Nothing); + Assert.Multiple(() => + { + Assert.That(points.BuyerId, Is.EqualTo(buyerId)); + Assert.That(points.PointsDate, Is.EqualTo(pointsDate)); + Assert.That(points.Points, Is.EqualTo(buyerPoints)); + }); + } + private static PointsDataModel CreateDataModel(string? buyerId, DateTime + pointsDate, double buyerPoints) => new(buyerId, pointsDate, buyerPoints); +} diff --git a/PuferFishContracts/PuferFishTests/DataModelsTests/PostDataModelTests.cs b/PuferFishContracts/PuferFishTests/DataModelsTests/PostDataModelTests.cs new file mode 100644 index 0000000..b946281 --- /dev/null +++ b/PuferFishContracts/PuferFishTests/DataModelsTests/PostDataModelTests.cs @@ -0,0 +1,112 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using PuferFishContracts.Enums; +using PuferFishContracts.Exceptions; +using PuferFishContracts.DataModels; + +namespace PuferFishTests.DataModelsTests; + +[TestFixture] +internal class PostDataModelTests +{ + [Test] + public void IdIsNullOrEmptyTest() + { + var post = CreateDataModel(null, Guid.NewGuid().ToString(), "name", + PostType.Cashier, true, DateTime.UtcNow); + Assert.That(() => post.Validate(), + Throws.TypeOf()); + post = CreateDataModel(string.Empty, Guid.NewGuid().ToString(), + "name", PostType.Cashier, true, DateTime.UtcNow); + Assert.That(() => post.Validate(), + Throws.TypeOf()); + } + [Test] + public void IdIsNotGuidTest() + { + var post = CreateDataModel("id", Guid.NewGuid().ToString(), "name", + PostType.Cashier, true, DateTime.UtcNow); + Assert.That(() => post.Validate(), + Throws.TypeOf()); + } + [Test] + public void PostIdIsNullEmptyTest() + { + var post = CreateDataModel(Guid.NewGuid().ToString(), null, "name", + PostType.Cashier, true, DateTime.UtcNow); + Assert.That(() => post.Validate(), + Throws.TypeOf()); + post = CreateDataModel(Guid.NewGuid().ToString(), string.Empty, + "name", PostType.Cashier, true, DateTime.UtcNow); + Assert.That(() => post.Validate(), + Throws.TypeOf()); + } + [Test] + public void PostIdIsNotGuidTest() + { + var post = CreateDataModel(Guid.NewGuid().ToString(), "postId", + "name", PostType.Cashier, true, DateTime.UtcNow); + Assert.That(() => post.Validate(), + Throws.TypeOf()); + } + [Test] + public void PostNameIsEmptyTest() + { + var manufacturer = CreateDataModel(Guid.NewGuid().ToString(), + Guid.NewGuid().ToString(), null, PostType.Cashier, true, DateTime.UtcNow); + Assert.That(() => manufacturer.Validate(), + Throws.TypeOf()); + manufacturer = CreateDataModel(Guid.NewGuid().ToString(), + Guid.NewGuid().ToString(), string.Empty, PostType.Cashier, 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, true, DateTime.UtcNow); + Assert.That(() => post.Validate(), + Throws.TypeOf()); + } + /*[Test] + public void SalaryIsLessOrZeroTest() + { + var post = CreateDataModel(Guid.NewGuid().ToString(), + Guid.NewGuid().ToString(), "name", PostType.Cashier, true, DateTime.UtcNow); + Assert.That(() => post.Validate(), + Throws.TypeOf()); + post = CreateDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), "name", PostType.Cashier, 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.Cashier; + var salary = 10; + var isActual = false; + var changeDate = DateTime.UtcNow.AddDays(-1); + var post = CreateDataModel(postId, postPostId, postName, postType, 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, bool isActual, DateTime changeDate) => new (id, postId, postName, postType, isActual, changeDate); +} diff --git a/PuferFishContracts/PuferFishTests/DataModelsTests/ProductDataModelTests.cs b/PuferFishContracts/PuferFishTests/DataModelsTests/ProductDataModelTests.cs new file mode 100644 index 0000000..3b62232 --- /dev/null +++ b/PuferFishContracts/PuferFishTests/DataModelsTests/ProductDataModelTests.cs @@ -0,0 +1,88 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using PuferFishContracts.DataModels; +using PuferFishContracts.Enums; +using PuferFishContracts.Infrastructure; +using PuferFishContracts.Extensions; +using PuferFishContracts.Exceptions; + +namespace PuferFishTests.DataModelsTests; + +internal class ProductDataModelTests +{ + [Test] + public void IdIsNullOrEmptyTest() + { + var product = CreateDataModel(null, "name", ProductType.Sushi, 10, false); + Assert.That(() => product.Validate(), + Throws.TypeOf()); + product = CreateDataModel(string.Empty, "name", + ProductType.Sushi, 10, false); + Assert.That(() => product.Validate(), + Throws.TypeOf()); + } + [Test] + public void IdIsNotGuidTest() + { + var product = CreateDataModel("id", "name", ProductType.Sushi, 10, false); + Assert.That(() => product.Validate(), + Throws.TypeOf()); + } + [Test] + public void ProductNameIsEmptyTest() + { + var product = CreateDataModel(Guid.NewGuid().ToString(), null, + ProductType.Sushi, 10, false); + Assert.That(() => product.Validate(), + Throws.TypeOf()); + product = CreateDataModel(Guid.NewGuid().ToString(), string.Empty, + ProductType.Sushi, 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.Sushi, 0, false); + Assert.That(() => product.Validate(), + Throws.TypeOf()); + product = CreateDataModel(Guid.NewGuid().ToString(), "name", + ProductType.Sushi, -10, false); + Assert.That(() => product.Validate(), + Throws.TypeOf()); + } + [Test] + public void AllFieldsIsCorrectTest() + { + var productId = Guid.NewGuid().ToString(); + var productName = "name"; + var productType = ProductType.Sushi; + 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/PuferFishContracts/PuferFishTests/DataModelsTests/ProductHistoryDataModelTests.cs b/PuferFishContracts/PuferFishTests/DataModelsTests/ProductHistoryDataModelTests.cs new file mode 100644 index 0000000..86c7d36 --- /dev/null +++ b/PuferFishContracts/PuferFishTests/DataModelsTests/ProductHistoryDataModelTests.cs @@ -0,0 +1,61 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using PuferFishContracts.DataModels; +using PuferFishContracts.Extensions; +using PuferFishContracts.Exceptions; + +namespace PuferFishTests.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); + +} diff --git a/PuferFishContracts/PuferFishTests/DataModelsTests/SaleDataModelTests.cs b/PuferFishContracts/PuferFishTests/DataModelsTests/SaleDataModelTests.cs new file mode 100644 index 0000000..cb003b8 --- /dev/null +++ b/PuferFishContracts/PuferFishTests/DataModelsTests/SaleDataModelTests.cs @@ -0,0 +1,125 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using PuferFishContracts.DataModels; +using PuferFishContracts.Exceptions; + +namespace PuferFishTests.DataModelsTests; + +[TestFixture] +internal class SaleDataModelTests +{ + [Test] + public void IdIsNullOrEmptyTest() + { + var sale = CreateDataModel(null, Guid.NewGuid().ToString(), + Guid.NewGuid().ToString(), 10, 10, false, + CreateSubDataModel()); + Assert.That(() => sale.Validate(), + Throws.TypeOf()); + sale = CreateDataModel(string.Empty, Guid.NewGuid().ToString(), + Guid.NewGuid().ToString(), 10, 10, false, + CreateSubDataModel()); + Assert.That(() => sale.Validate(), + Throws.TypeOf()); + } + [Test] + public void IdIsNotGuidTest() + { + var sale = CreateDataModel("id", Guid.NewGuid().ToString(), + Guid.NewGuid().ToString(), 10, 10, false, + CreateSubDataModel()); + Assert.That(() => sale.Validate(), +Throws.TypeOf()); + } + [Test] + public void WorkerIdIsNullOrEmptyTest() + { + var sale = CreateDataModel(Guid.NewGuid().ToString(), null, + Guid.NewGuid().ToString(), 10, 10, false, + CreateSubDataModel()); + Assert.That(() => sale.Validate(), + Throws.TypeOf()); + sale = CreateDataModel(Guid.NewGuid().ToString(), string.Empty, + Guid.NewGuid().ToString(), 10, 10, false, + CreateSubDataModel()); + Assert.That(() => sale.Validate(), + Throws.TypeOf()); + } + [Test] + public void WorkerIdIsNotGuidTest() + { + var sale = CreateDataModel(Guid.NewGuid().ToString(), "workerId", + Guid.NewGuid().ToString(), 10, 10, false, + CreateSubDataModel()); + Assert.That(() => sale.Validate(), + Throws.TypeOf()); + } + [Test] + public void BuyerIdIsNotGuidTest() + { + var sale = CreateDataModel(Guid.NewGuid().ToString(), + Guid.NewGuid().ToString(), "buyerId", 10, 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, 10, + false, CreateSubDataModel()); + Assert.That(() => sale.Validate(), + Throws.TypeOf()); + sale = CreateDataModel(Guid.NewGuid().ToString(), + Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), -10, + 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, + 10, false, null); + Assert.That(() => sale.Validate(), + Throws.TypeOf()); + sale = CreateDataModel(Guid.NewGuid().ToString(), + Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 10, + 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 points = 1; + var isCancel = true; + var products = CreateSubDataModel(); + var sale = CreateDataModel(saleId, workerId, buyerId, sum, points, 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.Points, Is.EqualTo(points)); + 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, double points, bool + isCancel, List? products) => new(id, workerId, buyerId, sum, points, isCancel, products); + private static List CreateSubDataModel() + => [new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 1)]; +} \ No newline at end of file diff --git a/PuferFishContracts/PuferFishTests/DataModelsTests/SaleProductDataModelTests.cs b/PuferFishContracts/PuferFishTests/DataModelsTests/SaleProductDataModelTests.cs new file mode 100644 index 0000000..40a0b68 --- /dev/null +++ b/PuferFishContracts/PuferFishTests/DataModelsTests/SaleProductDataModelTests.cs @@ -0,0 +1,81 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using PuferFishContracts.DataModels; +using PuferFishContracts.Exceptions; + +namespace PuferFishTests.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); +} diff --git a/PuferFishContracts/PuferFishTests/DataModelsTests/WorkerDataModelTests.cs b/PuferFishContracts/PuferFishTests/DataModelsTests/WorkerDataModelTests.cs new file mode 100644 index 0000000..3524e04 --- /dev/null +++ b/PuferFishContracts/PuferFishTests/DataModelsTests/WorkerDataModelTests.cs @@ -0,0 +1,115 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using PuferFishContracts.DataModels; +using PuferFishContracts.Extensions; +using PuferFishContracts.Exceptions; + +namespace PuferFishTests.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); +} diff --git a/PuferFishContracts/PuferFishTests/PuferFishTests.csproj b/PuferFishContracts/PuferFishTests/PuferFishTests.csproj new file mode 100644 index 0000000..126f72f --- /dev/null +++ b/PuferFishContracts/PuferFishTests/PuferFishTests.csproj @@ -0,0 +1,27 @@ + + + + net9.0 + latest + enable + enable + false + + + + + + + + + + + + + + + + + + + -- 2.25.1 From 72b36bf3f0ca21a23d47bc2d9c9f511c3f02500a Mon Sep 17 00:00:00 2001 From: Glliza Date: Thu, 13 Feb 2025 16:04:45 +0400 Subject: [PATCH 5/5] =?UTF-8?q?=D0=98=D1=82=D0=BE=D0=B3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../DataModels/BuyerDataModel.cs | 3 +++ .../DataModels/WorkerDataModel.cs | 10 ++++++++++ .../DataModelsTests/BuyerDataModelTests.cs | 14 ++++++------- .../DataModelsTests/WorkerDataModelTests.cs | 20 +++++++++---------- 4 files changed, 30 insertions(+), 17 deletions(-) diff --git a/PuferFishContracts/PuferFishContracts/DataModels/BuyerDataModel.cs b/PuferFishContracts/PuferFishContracts/DataModels/BuyerDataModel.cs index 3b61284..6f328fa 100644 --- a/PuferFishContracts/PuferFishContracts/DataModels/BuyerDataModel.cs +++ b/PuferFishContracts/PuferFishContracts/DataModels/BuyerDataModel.cs @@ -28,6 +28,9 @@ namespace PuferFishContracts.DataModels if (FIO.IsEmpty()) throw new ValidationException("Field FIO is empty"); + if (!Regex.IsMatch(FIO, @"\b[А-ЯЁ][а-яё]*\b \b[А-ЯЁ][а-яё]*\b \b[А-ЯЁ][а-яё]*\b")) + throw new ValidationException("Field FIO is not a FIO"); + if (PhoneNumber.IsEmpty()) throw new ValidationException("Field PhoneNumber is empty"); diff --git a/PuferFishContracts/PuferFishContracts/DataModels/WorkerDataModel.cs b/PuferFishContracts/PuferFishContracts/DataModels/WorkerDataModel.cs index 639515f..e1ea449 100644 --- a/PuferFishContracts/PuferFishContracts/DataModels/WorkerDataModel.cs +++ b/PuferFishContracts/PuferFishContracts/DataModels/WorkerDataModel.cs @@ -8,6 +8,7 @@ using PuferFishContracts.Exceptions; using System.Xml; using PuferFishContracts.Infrastructure; using static System.Runtime.InteropServices.JavaScript.JSType; +using System.Text.RegularExpressions; namespace PuferFishContracts.DataModels; @@ -23,16 +24,25 @@ public class WorkerDataModel(string id, string fio, string postId, DateTime birt { 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 (!Regex.IsMatch(FIO, @"\b[А-ЯЁ][а-яё]*\b \b[А-ЯЁ][а-яё]*\b \b[А-ЯЁ][а-яё]*\b")) + throw new ValidationException("Field FIO is not a FIO"); + 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 diff --git a/PuferFishContracts/PuferFishTests/DataModelsTests/BuyerDataModelTests.cs b/PuferFishContracts/PuferFishTests/DataModelsTests/BuyerDataModelTests.cs index ab09bba..fedfb77 100644 --- a/PuferFishContracts/PuferFishTests/DataModelsTests/BuyerDataModelTests.cs +++ b/PuferFishContracts/PuferFishTests/DataModelsTests/BuyerDataModelTests.cs @@ -14,17 +14,17 @@ namespace PuferFishTests.DataModelsTests [Test] public void IdIsNullOrEmptyTest() { - var buyer = CreateDataModel(null, "fio", "number", 10); + var buyer = CreateDataModel(null, "Фамилия Имя Отчество", "number", 10); Assert.That(() => buyer.Validate(), Throws.TypeOf()); - buyer = CreateDataModel(string.Empty, "fio", "number", 10); + buyer = CreateDataModel(string.Empty, "Фамилия Имя Отчество", "number", 10); Assert.That(() => buyer.Validate(), Throws.TypeOf()); } [Test] public void IdIsNotGuidTest() { - var buyer = CreateDataModel("id", "fio", "number", 10); + var buyer = CreateDataModel("id", "Фамилия Имя Отчество", "number", 10); Assert.That(() => buyer.Validate(), Throws.TypeOf()); } @@ -43,11 +43,11 @@ namespace PuferFishTests.DataModelsTests [Test] public void PhoneNumberIsNullOrEmptyTest() { - var buyer = CreateDataModel(Guid.NewGuid().ToString(), "fio", null, + var buyer = CreateDataModel(Guid.NewGuid().ToString(), "Фамилия Имя Отчество", null, 10); Assert.That(() => buyer.Validate(), Throws.TypeOf()); - buyer = CreateDataModel(Guid.NewGuid().ToString(), "fio", + buyer = CreateDataModel(Guid.NewGuid().ToString(), "Фамилия Имя Отчество", string.Empty, 10); Assert.That(() => buyer.Validate(), Throws.TypeOf()); @@ -55,7 +55,7 @@ namespace PuferFishTests.DataModelsTests [Test] public void PhoneNumberIsIncorrectTest() { - var buyer = CreateDataModel(Guid.NewGuid().ToString(), "fio", "777", + var buyer = CreateDataModel(Guid.NewGuid().ToString(), "Фамилия Имя Отчество", "777", 10); Assert.That(() => buyer.Validate(), Throws.TypeOf()); @@ -64,7 +64,7 @@ namespace PuferFishTests.DataModelsTests public void AllFieldsIsCorrectTest() { var buyerId = Guid.NewGuid().ToString(); - var fio = "Fio"; + var fio = "Фамилия Имя Отчество"; var phoneNumber = "+7-777-777-77-77"; var points = 11; var buyer = CreateDataModel(buyerId, fio, phoneNumber, points); diff --git a/PuferFishContracts/PuferFishTests/DataModelsTests/WorkerDataModelTests.cs b/PuferFishContracts/PuferFishTests/DataModelsTests/WorkerDataModelTests.cs index 3524e04..1562aed 100644 --- a/PuferFishContracts/PuferFishTests/DataModelsTests/WorkerDataModelTests.cs +++ b/PuferFishContracts/PuferFishTests/DataModelsTests/WorkerDataModelTests.cs @@ -15,11 +15,11 @@ internal class WorkerDataModelTests [Test] public void IdIsNullOrEmptyTest() { - var worker = CreateDataModel(null, "fio", Guid.NewGuid().ToString(), + var worker = CreateDataModel(null, "Фамилия Имя Отчество", Guid.NewGuid().ToString(), DateTime.Now.AddYears(-18), DateTime.Now, false); Assert.That(() => worker.Validate(), Throws.TypeOf()); - worker = CreateDataModel(string.Empty, "fio", + worker = CreateDataModel(string.Empty, "Фамилия Имя Отчество", Guid.NewGuid().ToString(), DateTime.Now.AddYears(-18), DateTime.Now, false); Assert.That(() => worker.Validate(), Throws.TypeOf()); @@ -27,7 +27,7 @@ internal class WorkerDataModelTests [Test] public void IdIsNotGuidTest() { - var worker = CreateDataModel("id", "fio", Guid.NewGuid().ToString(), + var worker = CreateDataModel("id", "Фамилия Имя Отчество", Guid.NewGuid().ToString(), DateTime.Now.AddYears(-18), DateTime.Now, false); Assert.That(() => worker.Validate(), Throws.TypeOf()); @@ -47,11 +47,11 @@ internal class WorkerDataModelTests [Test] public void PostIdIsNullOrEmptyTest() { - var worker = CreateDataModel(Guid.NewGuid().ToString(), "fio", null, + var worker = CreateDataModel(Guid.NewGuid().ToString(), "Фамилия Имя Отчество", null, DateTime.Now.AddYears(-18), DateTime.Now, false); Assert.That(() => worker.Validate(), Throws.TypeOf()); - worker = CreateDataModel(Guid.NewGuid().ToString(), "fio", + worker = CreateDataModel(Guid.NewGuid().ToString(), "Фамилия Имя Отчество", string.Empty, DateTime.Now.AddYears(-18), DateTime.Now, false); Assert.That(() => worker.Validate(), Throws.TypeOf()); @@ -59,7 +59,7 @@ internal class WorkerDataModelTests [Test] public void PostIdIsNotGuidTest() { - var worker = CreateDataModel(Guid.NewGuid().ToString(), "fio", + var worker = CreateDataModel(Guid.NewGuid().ToString(), "Фамилия Имя Отчество", "postId", DateTime.Now.AddYears(-18), DateTime.Now, false); Assert.That(() => worker.Validate(), Throws.TypeOf()); @@ -67,7 +67,7 @@ internal class WorkerDataModelTests [Test] public void BirthDateIsNotCorrectTest() { - var worker = CreateDataModel(Guid.NewGuid().ToString(), "fio", + var worker = CreateDataModel(Guid.NewGuid().ToString(), "Фамилия Имя Отчество", Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(1), DateTime.Now, false); Assert.That(() => worker.Validate(), @@ -76,12 +76,12 @@ internal class WorkerDataModelTests [Test] public void BirthDateAndEmploymentDateIsNotCorrectTest() { - var worker = CreateDataModel(Guid.NewGuid().ToString(), "fio", + var worker = CreateDataModel(Guid.NewGuid().ToString(), "Фамилия Имя Отчество", 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", + worker = CreateDataModel(Guid.NewGuid().ToString(), "Фамилия Имя Отчество", Guid.NewGuid().ToString(), DateTime.Now.AddYears(-18), DateTime.Now.AddYears(- 16), false); Assert.That(() => worker.Validate(), @@ -91,7 +91,7 @@ internal class WorkerDataModelTests public void AllFieldsIsCorrectTest() { var workerId = Guid.NewGuid().ToString(); - var fio = "fio"; + var fio = "Фамилия Имя Отчество"; var postId = Guid.NewGuid().ToString(); var birthDate = DateTime.Now.AddYears(-16).AddDays(-1); var employmentDate = DateTime.Now; -- 2.25.1