diff --git a/Romashki/Romashki.sln b/Romashki/Romashki.sln index 2c1405b..5397809 100644 --- a/Romashki/Romashki.sln +++ b/Romashki/Romashki.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}") = "RomashkiContract", "RomashkiContract\RomashkiContract.csproj", "{A35E02F7-07DD-428B-A6B5-8BD3DD05268F}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RomashkiTests", "RomashkiTests\RomashkiTests.csproj", "{2D70C332-09B5-457D-95C9-30582CBA73E6}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -15,6 +17,10 @@ Global {A35E02F7-07DD-428B-A6B5-8BD3DD05268F}.Debug|Any CPU.Build.0 = Debug|Any CPU {A35E02F7-07DD-428B-A6B5-8BD3DD05268F}.Release|Any CPU.ActiveCfg = Release|Any CPU {A35E02F7-07DD-428B-A6B5-8BD3DD05268F}.Release|Any CPU.Build.0 = Release|Any CPU + {2D70C332-09B5-457D-95C9-30582CBA73E6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {2D70C332-09B5-457D-95C9-30582CBA73E6}.Debug|Any CPU.Build.0 = Debug|Any CPU + {2D70C332-09B5-457D-95C9-30582CBA73E6}.Release|Any CPU.ActiveCfg = Release|Any CPU + {2D70C332-09B5-457D-95C9-30582CBA73E6}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/Romashki/RomashkiContract/DataModels/BuyerDataModel.cs b/Romashki/RomashkiContract/DataModels/BuyerDataModel.cs new file mode 100644 index 0000000..e03bfc7 --- /dev/null +++ b/Romashki/RomashkiContract/DataModels/BuyerDataModel.cs @@ -0,0 +1,39 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Numerics; +using System.Text; +using System.Text.RegularExpressions; +using System.Threading.Tasks; +using RomashkiContracts.Extensions; +using RomashkiContracts.Infrastructure; +using static System.Runtime.InteropServices.JavaScript.JSType; +using System.Xml; +using RomashkiContracts.Exceptions; + +namespace RomashkiContracts.DataModels; + +public class BuyerDataModel(string id, string fio, string email, float totalSpend, double discountSize) : IValidation +{ + public string Id { get; private set; } = id; + public string FIO { get; private set; } = fio; + public string Email { get; private set; } = email; + public float TotalSpend { get; private set; } = totalSpend; + 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 (Email.IsEmpty()) + throw new ValidationException("Field PhoneNumber is empty"); + if (TotalSpend < 0) + throw new ValidationException("Field TotalSpend cannot be less than 0"); + if (!Regex.IsMatch(Email, @"([a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.[a-zA-Z0-9_-]+)")) + throw new ValidationException("Field Email is not an email"); + } +} + diff --git a/Romashki/RomashkiContract/DataModels/PostDataModel.cs b/Romashki/RomashkiContract/DataModels/PostDataModel.cs new file mode 100644 index 0000000..4035a0a --- /dev/null +++ b/Romashki/RomashkiContract/DataModels/PostDataModel.cs @@ -0,0 +1,39 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using RomashkiContracts.Enums; +using RomashkiContracts.Extensions; +using RomashkiContracts.Infrastructure; +using System.Xml; +using RomashkiContracts.Exceptions; + +namespace RomashkiContracts.DataModels; + +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/Romashki/RomashkiContract/DataModels/ProductDataModel.cs b/Romashki/RomashkiContract/DataModels/ProductDataModel.cs new file mode 100644 index 0000000..6fe14d6 --- /dev/null +++ b/Romashki/RomashkiContract/DataModels/ProductDataModel.cs @@ -0,0 +1,36 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using RomashkiContracts.Enums; +using RomashkiContracts.Extensions; +using RomashkiContracts.Infrastructure; +using System.Xml; +using RomashkiContracts.Exceptions; + +namespace RomashkiContracts.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/Romashki/RomashkiContract/DataModels/ProductHistoryDataModel.cs b/Romashki/RomashkiContract/DataModels/ProductHistoryDataModel.cs new file mode 100644 index 0000000..154eed4 --- /dev/null +++ b/Romashki/RomashkiContract/DataModels/ProductHistoryDataModel.cs @@ -0,0 +1,27 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using RomashkiContracts.Exceptions; +using RomashkiContracts.Extensions; +using RomashkiContracts.Infrastructure; + +namespace RomashkiContracts.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/Romashki/RomashkiContract/DataModels/SaleDataModel.cs b/Romashki/RomashkiContract/DataModels/SaleDataModel.cs new file mode 100644 index 0000000..d6aa8dd --- /dev/null +++ b/Romashki/RomashkiContract/DataModels/SaleDataModel.cs @@ -0,0 +1,43 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using RomashkiContracts.Enums; +using RomashkiContracts.Extensions; +using RomashkiContracts.Infrastructure; +using System.Xml; +using RomashkiContracts.Exceptions; + +namespace RomashkiContracts.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"); + } +} diff --git a/Romashki/RomashkiContract/DataModels/SaleProductDataModel.cs b/Romashki/RomashkiContract/DataModels/SaleProductDataModel.cs new file mode 100644 index 0000000..bdc42f9 --- /dev/null +++ b/Romashki/RomashkiContract/DataModels/SaleProductDataModel.cs @@ -0,0 +1,31 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using RomashkiContracts.Exceptions; +using RomashkiContracts.Extensions; +using RomashkiContracts.Infrastructure; + +namespace RomashkiContracts.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/Romashki/RomashkiContract/DataModels/WorkerDataModel.cs b/Romashki/RomashkiContract/DataModels/WorkerDataModel.cs new file mode 100644 index 0000000..6004abd --- /dev/null +++ b/Romashki/RomashkiContract/DataModels/WorkerDataModel.cs @@ -0,0 +1,42 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using RomashkiContracts.Extensions; +using RomashkiContracts.Infrastructure; +using static System.Runtime.InteropServices.JavaScript.JSType; +using System.Xml; +using RomashkiContracts.Exceptions; + +namespace RomashkiContracts.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()})"); + } +} diff --git a/Romashki/RomashkiContract/Enums/DiscountType.cs b/Romashki/RomashkiContract/Enums/DiscountType.cs new file mode 100644 index 0000000..be53cc6 --- /dev/null +++ b/Romashki/RomashkiContract/Enums/DiscountType.cs @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace RomashkiContracts.Enums; + +[Flags] +public enum DiscountType +{ + None = 0, + OnSale = 1, + RegularCustomer = 2, + Certificate = 4 +} diff --git a/Romashki/RomashkiContract/Enums/PostType.cs b/Romashki/RomashkiContract/Enums/PostType.cs new file mode 100644 index 0000000..b1d1b19 --- /dev/null +++ b/Romashki/RomashkiContract/Enums/PostType.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace RomashkiContracts.Enums; + +public enum PostType +{ + None = 0, + Supervisor = 1, + Florist = 2, + Assistant = 3 +} diff --git a/Romashki/RomashkiContract/Enums/ProductType.cs b/Romashki/RomashkiContract/Enums/ProductType.cs new file mode 100644 index 0000000..53660e1 --- /dev/null +++ b/Romashki/RomashkiContract/Enums/ProductType.cs @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace RomashkiContracts.Enums; + +public enum ProductType +{ + None = 0, + HomemadeFlowers = 1, + BeautifulFlowers = 2, + Accessory = 3 +} + diff --git a/Romashki/RomashkiContract/Exceptions/ValidationException.cs b/Romashki/RomashkiContract/Exceptions/ValidationException.cs new file mode 100644 index 0000000..47c3536 --- /dev/null +++ b/Romashki/RomashkiContract/Exceptions/ValidationException.cs @@ -0,0 +1,11 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace RomashkiContracts.Exceptions; + +public class ValidationException(string message) : Exception(message) +{ +} diff --git a/Romashki/RomashkiContract/Extensions/StringExtensions.cs b/Romashki/RomashkiContract/Extensions/StringExtensions.cs new file mode 100644 index 0000000..1e7f083 --- /dev/null +++ b/Romashki/RomashkiContract/Extensions/StringExtensions.cs @@ -0,0 +1,19 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace RomashkiContracts.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/Romashki/RomashkiContract/Infrastructure/IValidation.cs b/Romashki/RomashkiContract/Infrastructure/IValidation.cs new file mode 100644 index 0000000..0e0c29a --- /dev/null +++ b/Romashki/RomashkiContract/Infrastructure/IValidation.cs @@ -0,0 +1,12 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace RomashkiContracts.Infrastructure; + +public interface IValidation +{ + void Validate(); +} diff --git a/Romashki/RomashkiTests/BuyerDataModelTests.cs b/Romashki/RomashkiTests/BuyerDataModelTests.cs new file mode 100644 index 0000000..e66e81b --- /dev/null +++ b/Romashki/RomashkiTests/BuyerDataModelTests.cs @@ -0,0 +1,70 @@ +using RomashkiContracts.DataModels; +using RomashkiContracts.Exceptions; + +namespace RomashkiTests +{ + [TestFixture] + internal class BuyerDataModelTests + { + [Test] + public void IdIsNullOrEmptyTest() + { + var buyer = CreateDataModel(null, "fio", "email@123.qwe", 10, 10); + Assert.That(() => buyer.Validate(), + Throws.TypeOf()); + buyer = CreateDataModel(string.Empty, "fio", "email@123.qwe", 10, 10); + Assert.That(() => buyer.Validate(), + Throws.TypeOf()); + } + [Test] + public void IdIsNotGuidTest() + { + var buyer = CreateDataModel("id", "fio", "email@123.qwe", 10, 10); + Assert.That(() => buyer.Validate(), Throws.TypeOf()); + } + [Test] + public void FIOIsNullOrEmptyTest() + { + var buyer = CreateDataModel(Guid.NewGuid().ToString(), null, "email@123.qwe", 10, 10); + Assert.That(() => buyer.Validate(), Throws.TypeOf()); + buyer = CreateDataModel(Guid.NewGuid().ToString(), string.Empty, "email@123.qwe", 10, 10); + Assert.That(() => buyer.Validate(), Throws.TypeOf()); + } + [Test] + public void EmailIsNullOrEmptyTest() + { + var buyer = CreateDataModel(Guid.NewGuid().ToString(), "fio", null, 10, 10); + Assert.That(() => buyer.Validate(), Throws.TypeOf()); + buyer = CreateDataModel(Guid.NewGuid().ToString(), "fio", string.Empty, 10, 10); + Assert.That(() => buyer.Validate(), Throws.TypeOf()); + } + [Test] + public void PhoneNumberIsIncorrectTest() + { + var buyer = CreateDataModel(Guid.NewGuid().ToString(), "fio", "email", 10, 10); + Assert.That(() => buyer.Validate(), Throws.TypeOf()); + } + [Test] + public void AllFieldsIsCorrectTest() + { + var buyerId = Guid.NewGuid().ToString(); + var fio = "Fio"; + var email = "email@123.qwe"; + var totalSpend = 10; + var discountSize = 11; + var buyer = CreateDataModel(buyerId, fio, email, totalSpend, 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.Email, Is.EqualTo(email)); + Assert.That(buyer.TotalSpend, Is.EqualTo(totalSpend)); + Assert.That(buyer.DiscountSize, Is.EqualTo(discountSize)); + }); + } + private static BuyerDataModel CreateDataModel(string? id, string? fio, string? email, + float totalSpend, double discountSize) => new(id, fio, email, totalSpend, discountSize); + } + +} diff --git a/Romashki/RomashkiTests/PostDataModelTests.cs b/Romashki/RomashkiTests/PostDataModelTests.cs new file mode 100644 index 0000000..1cba732 --- /dev/null +++ b/Romashki/RomashkiTests/PostDataModelTests.cs @@ -0,0 +1,95 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using RomashkiContracts.DataModels; +using RomashkiContracts.Enums; +using RomashkiContracts.Exceptions; + +namespace RomashkiTests +{ + [TestFixture] + internal class PostDataModelTests + { + [Test] + public void IdIsNullOrEmptyTest() + { + var post = CreateDataModel(null, Guid.NewGuid().ToString(), "name", + PostType.Assistant, true, DateTime.UtcNow); + Assert.That(() => post.Validate(), + Throws.TypeOf()); + post = CreateDataModel(string.Empty, Guid.NewGuid().ToString(), "name", PostType.Assistant, true, DateTime.UtcNow); + Assert.That(() => post.Validate(), + Throws.TypeOf()); + } + [Test] + public void IdIsNotGuidTest() + { + var post = CreateDataModel("id", Guid.NewGuid().ToString(), "name", + PostType.Assistant, true, DateTime.UtcNow); + Assert.That(() => post.Validate(), + Throws.TypeOf()); + } + [Test] + public void PostIdIsNullEmptyTest() + { + var post = CreateDataModel(Guid.NewGuid().ToString(), null, "name", + PostType.Assistant, true, DateTime.UtcNow); + Assert.That(() => post.Validate(), Throws.TypeOf()); + post = CreateDataModel(Guid.NewGuid().ToString(), string.Empty, + "name", PostType.Assistant, true, DateTime.UtcNow); + Assert.That(() => post.Validate(), Throws.TypeOf()); + } + [Test] + public void PostIdIsNotGuidTest() + { + var post = CreateDataModel(Guid.NewGuid().ToString(), "postId", + "name", PostType.Assistant , 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, true, DateTime.UtcNow); + Assert.That(() => manufacturer.Validate(), Throws.TypeOf()); + manufacturer = CreateDataModel(Guid.NewGuid().ToString(), + Guid.NewGuid().ToString(), string.Empty, PostType.Assistant, 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 AllFieldsIsCorrectTest() + { + var postId = Guid.NewGuid().ToString(); + var postPostId = Guid.NewGuid().ToString(); + var postName = "name"; + var postType = PostType.Assistant; + 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.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/Romashki/RomashkiTests/ProductDataModelTests.cs b/Romashki/RomashkiTests/ProductDataModelTests.cs new file mode 100644 index 0000000..8b7899c --- /dev/null +++ b/Romashki/RomashkiTests/ProductDataModelTests.cs @@ -0,0 +1,76 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using RomashkiContracts.DataModels; +using RomashkiContracts.Enums; +using RomashkiContracts.Exceptions; + +namespace RomashkiTests +{ + [TestFixture] + internal class ProductDataModelTests + { + [Test] + public void IdIsNullOrEmptyTest() + { + var product = CreateDataModel(null, "name", ProductType.Accessory, 10, false); + Assert.That(() => product.Validate(), Throws.TypeOf()); + product = CreateDataModel(string.Empty, "name", ProductType.Accessory, 10, false); + Assert.That(() => product.Validate(), Throws.TypeOf()); + } + [Test] + public void IdIsNotGuidTest() + { + var product = CreateDataModel("id", "name", ProductType.Accessory, 10, false); + Assert.That(() => product.Validate(), Throws.TypeOf()); + } + [Test] + public void ProductNameIsEmptyTest() + { + var product = CreateDataModel(Guid.NewGuid().ToString(), null, ProductType.Accessory, 10, false); + Assert.That(() => product.Validate(), Throws.TypeOf()); + product = CreateDataModel(Guid.NewGuid().ToString(), string.Empty, ProductType.Accessory, 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.Accessory, 0, false); + Assert.That(() => product.Validate(), Throws.TypeOf()); + product = CreateDataModel(Guid.NewGuid().ToString(), "name", ProductType.Accessory, -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 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/Romashki/RomashkiTests/ProductHistoryDataModelTests.cs b/Romashki/RomashkiTests/ProductHistoryDataModelTests.cs new file mode 100644 index 0000000..bbe41e7 --- /dev/null +++ b/Romashki/RomashkiTests/ProductHistoryDataModelTests.cs @@ -0,0 +1,56 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using RomashkiContracts.DataModels; +using RomashkiContracts.Exceptions; + +namespace RomashkiTests +{ + [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/Romashki/RomashkiTests/RomashkiTests.csproj b/Romashki/RomashkiTests/RomashkiTests.csproj new file mode 100644 index 0000000..5fcda62 --- /dev/null +++ b/Romashki/RomashkiTests/RomashkiTests.csproj @@ -0,0 +1,27 @@ + + + + net9.0 + latest + enable + enable + false + + + + + + + + + + + + + + + + + + + diff --git a/Romashki/RomashkiTests/SaleDataModelTests.cs b/Romashki/RomashkiTests/SaleDataModelTests.cs new file mode 100644 index 0000000..005f799 --- /dev/null +++ b/Romashki/RomashkiTests/SaleDataModelTests.cs @@ -0,0 +1,111 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using RomashkiContracts.DataModels; +using RomashkiContracts.Enums; +using RomashkiContracts.Exceptions; + +namespace RomashkiTests +{ + [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)]; + } + +} diff --git a/Romashki/RomashkiTests/SaleProductDataModelTests.cs b/Romashki/RomashkiTests/SaleProductDataModelTests.cs new file mode 100644 index 0000000..a48b93f --- /dev/null +++ b/Romashki/RomashkiTests/SaleProductDataModelTests.cs @@ -0,0 +1,71 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using RomashkiContracts.DataModels; +using RomashkiContracts.Exceptions; + +namespace RomashkiTests +{ + [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/Romashki/RomashkiTests/WorkerDataModelTests.cs b/Romashki/RomashkiTests/WorkerDataModelTests.cs new file mode 100644 index 0000000..19c8803 --- /dev/null +++ b/Romashki/RomashkiTests/WorkerDataModelTests.cs @@ -0,0 +1,96 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using RomashkiContracts.DataModels; +using RomashkiContracts.Exceptions; + +namespace RomashkiTests +{ + [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); + } +}