diff --git a/TheCyclopsProject/TheCyclopsContracts/DataModels/ComponentDataModel.cs b/TheCyclopsProject/TheCyclopsContracts/DataModels/ComponentDataModel.cs new file mode 100644 index 0000000..fe8ddaa --- /dev/null +++ b/TheCyclopsProject/TheCyclopsContracts/DataModels/ComponentDataModel.cs @@ -0,0 +1,38 @@ +using TheCyclopsContracts.Enums; +using TheCyclopsContracts.Exceptions; +using TheCyclopsContracts.Extensions; +using TheCyclopsContracts.Infrastructure; + +namespace TheCyclopsContracts.DataModels; + +public class ComponentDataModel(string id, string componentName, + ComponentType componentType, double price, bool isDeleted) : IValidation +{ + public string Id { get; private set; } = id; + + public string ComponentName { get; private set; } = componentName; + + public ComponentType ComponentType { get; private set; } = componentType; + + 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 (ComponentName.IsEmpty()) + throw new ValidationException("Field ComponentName is empty"); + + if (ComponentType == ComponentType.None) + throw new ValidationException("Field ComponentType is empty"); + + if (Price <= 0) + throw new ValidationException("Field Price is less than or equal to 0"); + } +} diff --git a/TheCyclopsProject/TheCyclopsContracts/DataModels/EmployeeDataModel.cs b/TheCyclopsProject/TheCyclopsContracts/DataModels/EmployeeDataModel.cs new file mode 100644 index 0000000..03a03df --- /dev/null +++ b/TheCyclopsProject/TheCyclopsContracts/DataModels/EmployeeDataModel.cs @@ -0,0 +1,57 @@ +using System.Text.RegularExpressions; +using TheCyclopsContracts.Exceptions; +using TheCyclopsContracts.Extensions; +using TheCyclopsContracts.Infrastructure; + +namespace TheCyclopsContracts.DataModels; + +public class EmployeeDataModel(string id, string fio, string email, 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 Email { get; private set; } = email; + + 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 (Email.IsEmpty()) + throw new ValidationException("Field Email is empty"); + + if (!Regex.IsMatch(Email, @"^\S+@\S+\.\S+$")) + throw new ValidationException("Field Email is not an email address"); + + 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/TheCyclopsProject/TheCyclopsContracts/DataModels/InstallationComponentDataModel.cs b/TheCyclopsProject/TheCyclopsContracts/DataModels/InstallationComponentDataModel.cs new file mode 100644 index 0000000..baf7f7d --- /dev/null +++ b/TheCyclopsProject/TheCyclopsContracts/DataModels/InstallationComponentDataModel.cs @@ -0,0 +1,33 @@ +using TheCyclopsContracts.Exceptions; +using TheCyclopsContracts.Extensions; +using TheCyclopsContracts.Infrastructure; + +namespace TheCyclopsContracts.DataModels; + +public class InstallationComponentDataModel(string installationId, + string componentId, int count) : IValidation +{ + public string InstallationId { get; private set; } = installationId; + + public string ComponentId { get; private set; } = componentId; + + public int Count { get; private set; } = count; + + public void Validate() + { + if (InstallationId.IsEmpty()) + throw new ValidationException("Field InstallationId is empty"); + + if (!InstallationId.IsGuid()) + throw new ValidationException("The value in the field InstallationId is not a unique identifier"); + + if (ComponentId.IsEmpty()) + throw new ValidationException("Field ComponentId is empty"); + + if (!ComponentId.IsGuid()) + throw new ValidationException("The value in the field ComponentId is not a unique identifier"); + + if (Count <= 0) + throw new ValidationException("Field Count is less than or equal to 0"); + } +} diff --git a/TheCyclopsProject/TheCyclopsContracts/DataModels/InstallationDataModel.cs b/TheCyclopsProject/TheCyclopsContracts/DataModels/InstallationDataModel.cs new file mode 100644 index 0000000..04e6642 --- /dev/null +++ b/TheCyclopsProject/TheCyclopsContracts/DataModels/InstallationDataModel.cs @@ -0,0 +1,50 @@ +using TheCyclopsContracts.Exceptions; +using TheCyclopsContracts.Extensions; +using TheCyclopsContracts.Infrastructure; + +namespace TheCyclopsContracts.DataModels; + +public class InstallationDataModel(string id, string employeeId, + DateTime installationDate, double installationPrice, double sum, bool isCanceled, + List components) : IValidation +{ + public string Id { get; private set; } = id; + + public string EmployeeId { get; private set; } = employeeId; + + public DateTime SaleDate { get; private set; } = DateTime.UtcNow; + + public DateTime InstallationDate { get; private set; } = installationDate; + + public double InstallationPrice { get; private set; } = installationPrice; + + public double Sum { get; private set; } = sum; + + public bool IsCanceled { get; private set; } = isCanceled; + + public List Components { get; private set; } = components; + + 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 (EmployeeId.IsEmpty()) + throw new ValidationException("Field EmployeeId is empty"); + + if (!EmployeeId.IsGuid()) + throw new ValidationException("The value in the field EmployeeId is not a unique identifier"); + + if (InstallationPrice <= 0) + throw new ValidationException("Field InstallationPrice is less than or equal to 0"); + + if (Sum <= 0) + throw new ValidationException("Field Sum is less than or equal to 0"); + + if ((Components?.Count ?? 0) == 0) + throw new ValidationException("The installation must include components"); + } +} diff --git a/TheCyclopsProject/TheCyclopsContracts/DataModels/PostDataModel.cs b/TheCyclopsProject/TheCyclopsContracts/DataModels/PostDataModel.cs new file mode 100644 index 0000000..6025af1 --- /dev/null +++ b/TheCyclopsProject/TheCyclopsContracts/DataModels/PostDataModel.cs @@ -0,0 +1,46 @@ +using TheCyclopsContracts.Enums; +using TheCyclopsContracts.Exceptions; +using TheCyclopsContracts.Extensions; + +namespace TheCyclopsContracts.DataModels; + +public class PostDataModel(string id, string PostId, string postName, PostType postType, double salary, bool isActual, DateTime changeDate) +{ + public string Id { get; private set; } = id; + + public string PostId { get; private set; } = PostId; + + public string PostName { get; private set; } = postName; + + public PostType PostType { get; private set; } = postType; + + public double Salary { get; private set; } = salary; + + public bool IsActual { get; private set; } = isActual; + + public DateTime ChangeDate { get; private set; } = changeDate; + + public void Validate() + { + if (Id.IsEmpty()) + throw new ValidationException("Field Id is empty"); + + if (!Id.IsGuid()) + throw new ValidationException("The value in the field Id is not a unique identifier"); + + if (PostId.IsEmpty()) + throw new ValidationException("Field PostId is empty"); + + if (!PostId.IsGuid()) + throw new ValidationException("The value in the field PostId is not a unique identifier"); + + if (PostName.IsEmpty()) + throw new ValidationException("Field PostName is empty"); + + if (PostType == PostType.None) + throw new ValidationException("Field PostType is empty"); + + if (Salary <= 0) + throw new ValidationException("Field Salary is empty"); + } +} diff --git a/TheCyclopsProject/TheCyclopsContracts/DataModels/SalaryDataModel.cs b/TheCyclopsProject/TheCyclopsContracts/DataModels/SalaryDataModel.cs new file mode 100644 index 0000000..d45c4c4 --- /dev/null +++ b/TheCyclopsProject/TheCyclopsContracts/DataModels/SalaryDataModel.cs @@ -0,0 +1,26 @@ +using TheCyclopsContracts.Exceptions; +using TheCyclopsContracts.Extensions; +using TheCyclopsContracts.Infrastructure; + +namespace TheCyclopsContracts.DataModels; + +public class SalaryDataModel(string employeeId, DateTime salaryDate, double employeeSalary) : IValidation +{ + public string EmployeeId { get; private set; } = employeeId; + + public DateTime SalaryDate { get; private set; } = salaryDate; + + public double Salary { get; private set; } = employeeSalary; + + public void Validate() + { + if (EmployeeId.IsEmpty()) + throw new ValidationException("Field EmployeeId is empty"); + + if (!EmployeeId.IsGuid()) + throw new ValidationException("The value in the field EmployeeId is not a unique identifier"); + + if (Salary <= 0) + throw new ValidationException("Field Salary is less than or equal to 0"); + } +} diff --git a/TheCyclopsProject/TheCyclopsContracts/Enums/ComponentType.cs b/TheCyclopsProject/TheCyclopsContracts/Enums/ComponentType.cs new file mode 100644 index 0000000..ca2cb65 --- /dev/null +++ b/TheCyclopsProject/TheCyclopsContracts/Enums/ComponentType.cs @@ -0,0 +1,11 @@ +namespace TheCyclopsContracts.Enums; + +public enum ComponentType +{ + None = 0, + Camera = 1, + Sensor = 2, + ContropPanel = 3, + Cable = 4, + Other = 5 +} diff --git a/TheCyclopsProject/TheCyclopsContracts/Enums/PostType.cs b/TheCyclopsProject/TheCyclopsContracts/Enums/PostType.cs new file mode 100644 index 0000000..51e142a --- /dev/null +++ b/TheCyclopsProject/TheCyclopsContracts/Enums/PostType.cs @@ -0,0 +1,10 @@ +namespace TheCyclopsContracts.Enums; + +public enum PostType +{ + None = 0, + Designer = 1, + Installer = 2, + Seller = 3, + Administrator = 4 +} diff --git a/TheCyclopsProject/TheCyclopsContracts/Exceptions/ValidationException.cs b/TheCyclopsProject/TheCyclopsContracts/Exceptions/ValidationException.cs new file mode 100644 index 0000000..b601647 --- /dev/null +++ b/TheCyclopsProject/TheCyclopsContracts/Exceptions/ValidationException.cs @@ -0,0 +1,5 @@ +namespace TheCyclopsContracts.Exceptions; + +public class ValidationException(string message) : Exception(message) +{ +} diff --git a/TheCyclopsProject/TheCyclopsContracts/Extensions/StringExtensions.cs b/TheCyclopsProject/TheCyclopsContracts/Extensions/StringExtensions.cs new file mode 100644 index 0000000..09942d1 --- /dev/null +++ b/TheCyclopsProject/TheCyclopsContracts/Extensions/StringExtensions.cs @@ -0,0 +1,14 @@ +namespace TheCyclopsContracts.Extensions; + +public static class StringExtensions +{ + public static bool IsEmpty(this string str) + { + return string.IsNullOrEmpty(str); + } + + public static bool IsGuid(this string str) + { + return Guid.TryParse(str, out _); + } +} diff --git a/TheCyclopsProject/TheCyclopsContracts/Infrastructure/IValidation.cs b/TheCyclopsProject/TheCyclopsContracts/Infrastructure/IValidation.cs new file mode 100644 index 0000000..ec8eb55 --- /dev/null +++ b/TheCyclopsProject/TheCyclopsContracts/Infrastructure/IValidation.cs @@ -0,0 +1,6 @@ +namespace TheCyclopsContracts.Infrastructure; + +public interface IValidation +{ + void Validate(); +} diff --git a/TheCyclopsProject/TheCyclopsProject.sln b/TheCyclopsProject/TheCyclopsProject.sln index 0fc1d92..4fa5a94 100644 --- a/TheCyclopsProject/TheCyclopsProject.sln +++ b/TheCyclopsProject/TheCyclopsProject.sln @@ -5,6 +5,8 @@ VisualStudioVersion = 17.12.35707.178 d17.12 MinimumVisualStudioVersion = 10.0.40219.1 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TheCyclopsContracts", "TheCyclopsContracts\TheCyclopsContracts.csproj", "{01934235-715E-4B95-B859-48E8C89DA7CB}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TheCyclopsTests", "TheCyclopsTests\TheCyclopsTests.csproj", "{78C9F1B1-ABD3-4ED2-A5DC-84766DEEFC77}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -15,6 +17,10 @@ Global {01934235-715E-4B95-B859-48E8C89DA7CB}.Debug|Any CPU.Build.0 = Debug|Any CPU {01934235-715E-4B95-B859-48E8C89DA7CB}.Release|Any CPU.ActiveCfg = Release|Any CPU {01934235-715E-4B95-B859-48E8C89DA7CB}.Release|Any CPU.Build.0 = Release|Any CPU + {78C9F1B1-ABD3-4ED2-A5DC-84766DEEFC77}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {78C9F1B1-ABD3-4ED2-A5DC-84766DEEFC77}.Debug|Any CPU.Build.0 = Debug|Any CPU + {78C9F1B1-ABD3-4ED2-A5DC-84766DEEFC77}.Release|Any CPU.ActiveCfg = Release|Any CPU + {78C9F1B1-ABD3-4ED2-A5DC-84766DEEFC77}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/TheCyclopsProject/TheCyclopsTests/DataModelsTests/ComponentDataModelTests.cs b/TheCyclopsProject/TheCyclopsTests/DataModelsTests/ComponentDataModelTests.cs new file mode 100644 index 0000000..f0c233a --- /dev/null +++ b/TheCyclopsProject/TheCyclopsTests/DataModelsTests/ComponentDataModelTests.cs @@ -0,0 +1,80 @@ +using TheCyclopsContracts.DataModels; +using TheCyclopsContracts.Enums; +using TheCyclopsContracts.Exceptions; + +namespace TheCyclopsTests.DataModelsTests; + +[TestFixture] +internal class ComponentDataModelTests +{ + [Test] + public void IdIsNullOrEmptyTest() + { + var component = CreateDataModel(null, "name", ComponentType.Other, 10, false); + Assert.That(() => component.Validate(), Throws.TypeOf()); + + component = CreateDataModel(string.Empty, "name", ComponentType.Other, 10, false); + Assert.That(() => component.Validate(), Throws.TypeOf()); + } + + [Test] + public void IdIsNotGuidTest() + { + var component = CreateDataModel("id", "name", ComponentType.Other, 10, false); + Assert.That(() => component.Validate(), Throws.TypeOf()); + } + + [Test] + public void ProductNameIsEmptyTest() + { + var component = CreateDataModel(Guid.NewGuid().ToString(), null, ComponentType.Other, 10, false); + Assert.That(() => component.Validate(), Throws.TypeOf()); + + component = CreateDataModel(Guid.NewGuid().ToString(), string.Empty, ComponentType.Other, 10, false); + Assert.That(() => component.Validate(), Throws.TypeOf()); + } + + [Test] + public void ComponentTypeIsNoneTest() + { + var component = CreateDataModel(Guid.NewGuid().ToString(), null, ComponentType.None, 10, false); + Assert.That(() => component.Validate(), Throws.TypeOf()); + } + + [Test] + public void PriceIsLessOrZeroTest() + { + var component = CreateDataModel(Guid.NewGuid().ToString(), "name", ComponentType.Other, 0, false); + Assert.That(() => component.Validate(), Throws.TypeOf()); + + component = CreateDataModel(Guid.NewGuid().ToString(), "name", ComponentType.Other, -10, false); + Assert.That(() => component.Validate(), Throws.TypeOf()); + } + + [Test] + public void AllFieldsIsCorrectTest() + { + var componentId = Guid.NewGuid().ToString(); + var componentName = "name"; + var componentType = ComponentType.Other; + var componentManufacturerId = Guid.NewGuid().ToString(); + var componentPrice = 10; + var componentIsDelete = false; + var component = CreateDataModel(componentId, componentName, componentType, componentPrice, componentIsDelete); + + Assert.That(() => component.Validate(), Throws.Nothing); + + Assert.Multiple(() => + { + Assert.That(component.Id, Is.EqualTo(componentId)); + Assert.That(component.ComponentName, Is.EqualTo(componentName)); + Assert.That(component.ComponentType, Is.EqualTo(componentType)); + Assert.That(component.Price, Is.EqualTo(componentPrice)); + Assert.That(component.IsDeleted, Is.EqualTo(componentIsDelete)); + }); + } + + private static ComponentDataModel CreateDataModel(string? id, string? componentName, + ComponentType componentType, double price, bool isDeleted) => + new(id, componentName, componentType, price, isDeleted); +} diff --git a/TheCyclopsProject/TheCyclopsTests/DataModelsTests/EmployeeDataModelTests.cs b/TheCyclopsProject/TheCyclopsTests/DataModelsTests/EmployeeDataModelTests.cs new file mode 100644 index 0000000..c83268f --- /dev/null +++ b/TheCyclopsProject/TheCyclopsTests/DataModelsTests/EmployeeDataModelTests.cs @@ -0,0 +1,130 @@ +using TheCyclopsContracts.DataModels; +using TheCyclopsContracts.Exceptions; + +namespace TheCyclopsTests.DataModelsTests; + +[TestFixture] +internal class EmployeeDataModelTests +{ + [Test] + public void IdIsNullOrEmptyTest() + { + var employee = CreateDataModel(null, "fio", "mail@mail.com", Guid.NewGuid().ToString(), + DateTime.Now.AddYears(-18), DateTime.Now, false); + Assert.That(() => employee.Validate(), Throws.TypeOf()); + + employee = CreateDataModel(null, "fio", "mail@mail.com", Guid.NewGuid().ToString(), + DateTime.Now.AddYears(-18), DateTime.Now, false); + Assert.That(() => employee.Validate(), Throws.TypeOf()); + } + + [Test] + public void IdIsNotGuidTest() + { + var employee = CreateDataModel("id", "fio", "mail@mail.com", Guid.NewGuid().ToString(), + DateTime.Now.AddYears(-18), DateTime.Now, false); + Assert.That(() => employee.Validate(), Throws.TypeOf()); + } + + [Test] + public void FIOIsNullOrEmptyTest() + { + var employee = CreateDataModel(Guid.NewGuid().ToString(), null, "mail@mail.com", Guid.NewGuid().ToString(), + DateTime.Now.AddYears(-18), DateTime.Now, false); + Assert.That(() => employee.Validate(), Throws.TypeOf()); + + employee = CreateDataModel(Guid.NewGuid().ToString(), string.Empty, "mail@mail.com", Guid.NewGuid().ToString(), + DateTime.Now.AddYears(-18), DateTime.Now, false); + Assert.That(() => employee.Validate(), Throws.TypeOf()); + } + + [Test] + public void EmailIsNullOrEmptyTest() + { + var employee = CreateDataModel(Guid.NewGuid().ToString(), "fio", null, Guid.NewGuid().ToString(), + DateTime.Now.AddYears(-18), DateTime.Now, false); + Assert.That(() => employee.Validate(), Throws.TypeOf()); + + employee = CreateDataModel(Guid.NewGuid().ToString(), "fio", string.Empty, Guid.NewGuid().ToString(), + DateTime.Now.AddYears(-18), DateTime.Now, false); + Assert.That(() => employee.Validate(), Throws.TypeOf()); + } + + [Test] + public void EmailIsIncorrectTest() + { + var employee = CreateDataModel(Guid.NewGuid().ToString(), "fio", "mail@mail", Guid.NewGuid().ToString(), + DateTime.Now.AddYears(-18), DateTime.Now, false); + Assert.That(() => employee.Validate(), Throws.TypeOf()); + } + + [Test] + public void PostIdIsNullOrEmptyTest() + { + var employee = CreateDataModel(Guid.NewGuid().ToString(), "fio", "mail@mail.com", null, + DateTime.Now.AddYears(-18), DateTime.Now, false); + Assert.That(() => employee.Validate(), Throws.TypeOf()); + + employee = CreateDataModel(Guid.NewGuid().ToString(), "fio", "mail@mail.com", string.Empty, + DateTime.Now.AddYears(-18), DateTime.Now, false); + Assert.That(() => employee.Validate(), Throws.TypeOf()); + } + + [Test] + public void PostIdIsNotGuidTest() + { + var employee = CreateDataModel(Guid.NewGuid().ToString(), "fio", "mail@mail.com", + "postId", DateTime.Now.AddYears(-18), DateTime.Now, false); + Assert.That(() => employee.Validate(), Throws.TypeOf()); + } + + [Test] + public void BirthDateIsNotCorrectTest() + { + var employee = CreateDataModel(Guid.NewGuid().ToString(), "fio", "mail@mail.com", + Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(1), DateTime.Now, false); + Assert.That(() => employee.Validate(), Throws.TypeOf()); + } + + [Test] + public void BirthDateAndEmploymentDateIsNotCorrectTest() + { + var employee = CreateDataModel(Guid.NewGuid().ToString(), "fio", "mail@mail.com", + Guid.NewGuid().ToString(), DateTime.Now.AddYears(-18), DateTime.Now.AddYears(-18).AddDays(-1), false); + Assert.That(() => employee.Validate(), Throws.TypeOf()); + + employee = CreateDataModel(Guid.NewGuid().ToString(), "fio", "mail@mail.com", + Guid.NewGuid().ToString(), DateTime.Now.AddYears(-18), DateTime.Now.AddYears(-16), false); + Assert.That(() => employee.Validate(), Throws.TypeOf()); + } + + [Test] + public void AllFieldsIsCorrectTest() + { + var employeeId = Guid.NewGuid().ToString(); + var fio = "fio"; + var email = "mail@mail.com"; + var postId = Guid.NewGuid().ToString(); + var birthDate = DateTime.Now.AddYears(-16).AddDays(-1); + var employmentDate = DateTime.Now; + var isDeleted = false; + var employee = CreateDataModel(employeeId, fio, email, postId, birthDate, employmentDate, isDeleted); + + Assert.That(() => employee.Validate(), Throws.Nothing); + + Assert.Multiple(() => + { + Assert.That(employee.Id, Is.EqualTo(employeeId)); + Assert.That(employee.FIO, Is.EqualTo(fio)); + Assert.That(employee.Email, Is.EqualTo(email)); + Assert.That(employee.PostId, Is.EqualTo(postId)); + Assert.That(employee.BirthDate, Is.EqualTo(birthDate)); + Assert.That(employee.EmploymentDate, Is.EqualTo(employmentDate)); + Assert.That(employee.IsDeleted, Is.EqualTo(isDeleted)); + }); + } + + private static EmployeeDataModel CreateDataModel(string? id, string? fio, string? email, + string? postId, DateTime birthDate, DateTime employmentDate, bool isDeleted) => + new(id, fio, email, postId, birthDate, employmentDate, isDeleted); +} diff --git a/TheCyclopsProject/TheCyclopsTests/DataModelsTests/InstallationComponentDataModelTests.cs b/TheCyclopsProject/TheCyclopsTests/DataModelsTests/InstallationComponentDataModelTests.cs new file mode 100644 index 0000000..a8e594b --- /dev/null +++ b/TheCyclopsProject/TheCyclopsTests/DataModelsTests/InstallationComponentDataModelTests.cs @@ -0,0 +1,73 @@ +using TheCyclopsContracts.DataModels; +using TheCyclopsContracts.Exceptions; + +namespace TheCyclopsTests.DataModelsTests; + +[TestFixture] +internal class InstallationComponentDataModelTests +{ + [Test] + public void InstallationIdIsNullOrEmptyTest() + { + var installationComponent = CreateDataModel(null, Guid.NewGuid().ToString(), 10); + Assert.That(() => installationComponent.Validate(), Throws.TypeOf()); + + installationComponent = CreateDataModel(string.Empty, Guid.NewGuid().ToString(), 10); + Assert.That(() => installationComponent.Validate(), Throws.TypeOf()); + } + + [Test] + public void InstallationIdIsNotGuidTest() + { + var installationComponent = CreateDataModel("installationId", Guid.NewGuid().ToString(), 10); + Assert.That(() => installationComponent.Validate(), Throws.TypeOf()); + } + + [Test] + public void ComponentIdIsNullOrEmptyTest() + { + var installationComponent = CreateDataModel(Guid.NewGuid().ToString(), null, 10); + Assert.That(() => installationComponent.Validate(), Throws.TypeOf()); + + installationComponent = CreateDataModel(string.Empty, Guid.NewGuid().ToString(), 10); + Assert.That(() => installationComponent.Validate(), Throws.TypeOf()); + } + + [Test] + public void ComponentIdIsNotGuidTest() + { + var installationComponent = CreateDataModel(Guid.NewGuid().ToString(), "componentId", 10); + Assert.That(() => installationComponent.Validate(), Throws.TypeOf()); + } + + [Test] + public void CountIsLessOrZeroTest() + { + var installationComponent = CreateDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 0); + Assert.That(() => installationComponent.Validate(), Throws.TypeOf()); + + installationComponent = CreateDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), -10); + Assert.That(() => installationComponent.Validate(), Throws.TypeOf()); + } + + [Test] + public void AllFieldsIsCorrectTest() + { + var installationId = Guid.NewGuid().ToString(); + var componentId = Guid.NewGuid().ToString(); + var count = 10; + var installationComponent = CreateDataModel(installationId, componentId, count); + + Assert.That(() => installationComponent.Validate(), Throws.Nothing); + + Assert.Multiple(() => + { + Assert.That(installationComponent.InstallationId, Is.EqualTo(installationId)); + Assert.That(installationComponent.ComponentId, Is.EqualTo(componentId)); + Assert.That(installationComponent.Count, Is.EqualTo(count)); + }); + } + + private static InstallationComponentDataModel CreateDataModel(string? installationId, string? componentId, int count) => + new(installationId, componentId, count); +} diff --git a/TheCyclopsProject/TheCyclopsTests/DataModelsTests/InstallationDataModelTests.cs b/TheCyclopsProject/TheCyclopsTests/DataModelsTests/InstallationDataModelTests.cs new file mode 100644 index 0000000..968e447 --- /dev/null +++ b/TheCyclopsProject/TheCyclopsTests/DataModelsTests/InstallationDataModelTests.cs @@ -0,0 +1,116 @@ +using TheCyclopsContracts.DataModels; +using TheCyclopsContracts.Exceptions; + +namespace TheCyclopsTests.DataModelsTests; + +[TestFixture] +internal class InstallationDataModelTests +{ + [Test] + public void IdIsNullOrEmptyTest() + { + var installation = CreateDataModel(null, Guid.NewGuid().ToString(), + DateTime.UtcNow, 10, 20, false, CreateSubDataModel()); + Assert.That(() => installation.Validate(), Throws.TypeOf()); + + installation = CreateDataModel(string.Empty, Guid.NewGuid().ToString(), + DateTime.UtcNow, 10, 20, false, CreateSubDataModel()); + Assert.That(() => installation.Validate(), Throws.TypeOf()); + } + + [Test] + public void IdIsNotGuidTest() + { + var installation = CreateDataModel("id", Guid.NewGuid().ToString(), + DateTime.UtcNow, 10, 20, false, CreateSubDataModel()); + Assert.That(() => installation.Validate(), Throws.TypeOf()); + } + + [Test] + public void EmployeeIdIsNullOrEmptyTest() + { + var installation = CreateDataModel(Guid.NewGuid().ToString(), null, + DateTime.UtcNow, 10, 20, false, CreateSubDataModel()); + Assert.That(() => installation.Validate(), Throws.TypeOf()); + + installation = CreateDataModel(Guid.NewGuid().ToString(), string.Empty, + DateTime.UtcNow, 10, 20, false, CreateSubDataModel()); + Assert.That(() => installation.Validate(), Throws.TypeOf()); + } + + [Test] + public void EmployeeIdIsNotGuidTest() + { + var installation = CreateDataModel(Guid.NewGuid().ToString(), "employeeId", + DateTime.UtcNow, 10, 20, false, CreateSubDataModel()); + Assert.That(() => installation.Validate(), Throws.TypeOf()); + } + + [Test] + public void InstallationPriceIsLessOrZeroTest() + { + var installation = CreateDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), + DateTime.UtcNow, 0, 20, false, CreateSubDataModel()); + Assert.That(() => installation.Validate(), Throws.TypeOf()); + + installation = CreateDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), + DateTime.UtcNow, -10, 20, false, CreateSubDataModel()); + Assert.That(() => installation.Validate(), Throws.TypeOf()); + } + + [Test] + public void SumIsLessOrZeroTest() + { + var installation = CreateDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), + DateTime.UtcNow, 10, 0, false, CreateSubDataModel()); + Assert.That(() => installation.Validate(), Throws.TypeOf()); + + installation = CreateDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), + DateTime.UtcNow, 10, -20, false, CreateSubDataModel()); + Assert.That(() => installation.Validate(), Throws.TypeOf()); + } + + [Test] + public void ProductsIsNullOrEmptyTest() + { + var installation = CreateDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), + DateTime.UtcNow, 10, 20, false, null); + Assert.That(() => installation.Validate(), Throws.TypeOf()); + + installation = CreateDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), + DateTime.UtcNow, 10, 20, false, []); + Assert.That(() => installation.Validate(), Throws.TypeOf()); + } + + [Test] + public void AllFieldsIsCorrectTest() + { + var installationId = Guid.NewGuid().ToString(); + var employeeId = Guid.NewGuid().ToString(); + var installationPrice = 10; + var sum = 20; + var isCanceled = true; + var components = CreateSubDataModel(); + var installation = CreateDataModel(installationId, employeeId, + DateTime.UtcNow, installationPrice, sum, isCanceled, components); + + Assert.That(() => installation.Validate(), Throws.Nothing); + + Assert.Multiple(() => + { + Assert.That(installation.Id, Is.EqualTo(installationId)); + Assert.That(installation.EmployeeId, Is.EqualTo(employeeId)); + Assert.That(installation.Sum, Is.EqualTo(sum)); + Assert.That(installation.IsCanceled, Is.EqualTo(isCanceled)); + Assert.That(installation.Components, Is.EquivalentTo(components)); + }); + } + + private static InstallationDataModel CreateDataModel(string id, string employeeId, + DateTime installationDate, double installationPrice, double sum, bool isCanceled, + List components) => + new(id, employeeId, installationDate, installationPrice, sum, isCanceled, components); + + private static List CreateSubDataModel() + => [new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 1)]; +} diff --git a/TheCyclopsProject/TheCyclopsTests/DataModelsTests/PostDataModelTests.cs b/TheCyclopsProject/TheCyclopsTests/DataModelsTests/PostDataModelTests.cs new file mode 100644 index 0000000..d942348 --- /dev/null +++ b/TheCyclopsProject/TheCyclopsTests/DataModelsTests/PostDataModelTests.cs @@ -0,0 +1,110 @@ +using TheCyclopsContracts.DataModels; +using TheCyclopsContracts.Enums; +using TheCyclopsContracts.Exceptions; + +namespace TheCyclopsTests.DataModelsTests; + +[TestFixture] +internal class PostDataModelTests +{ + [Test] + public void IdIsNullOrEmptyTest() + { + var post = CreateDataModel(null, Guid.NewGuid().ToString(), "post", + PostType.Designer, 10, true, DateTime.UtcNow); + Assert.That(() => post.Validate(), Throws.TypeOf()); + + post = CreateDataModel(string.Empty, Guid.NewGuid().ToString(), "post", + PostType.Designer, 10, true, DateTime.UtcNow); + Assert.That(() => post.Validate(), Throws.TypeOf()); + } + + [Test] + public void IdIsNotGuidTest() + { + var post = CreateDataModel("id", Guid.NewGuid().ToString(), "post", + PostType.Designer, 10, true, DateTime.UtcNow); + Assert.That(() => post.Validate(), Throws.TypeOf()); + } + + [Test] + public void PostIdIsNullEmptyTest() + { + var post = CreateDataModel(Guid.NewGuid().ToString(), null, "post", + PostType.Designer, 10, true, DateTime.UtcNow); + Assert.That(() => post.Validate(), Throws.TypeOf()); + + post = CreateDataModel(Guid.NewGuid().ToString(), string.Empty, "post", + PostType.Designer, 10, true, DateTime.UtcNow); + Assert.That(() => post.Validate(), Throws.TypeOf()); + } + + [Test] + public void PostIdIsNotGuidTest() + { + var post = CreateDataModel(Guid.NewGuid().ToString(), "postId", "post", + PostType.Designer, 10, true, DateTime.UtcNow); + Assert.That(() => post.Validate(), Throws.TypeOf()); + } + + [Test] + public void PostNameIsEmptyTest() + { + var manufacturer = CreateDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), null, + PostType.Designer, 10, true, DateTime.UtcNow); + Assert.That(() => manufacturer.Validate(), Throws.TypeOf()); + + manufacturer = CreateDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), string.Empty, + PostType.Designer, 10, true, DateTime.UtcNow); + Assert.That(() => manufacturer.Validate(), Throws.TypeOf()); + } + + [Test] + public void PostTypeIsNoneTest() + { + var post = CreateDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), "post", + PostType.None, 10, true, DateTime.UtcNow); + Assert.That(() => post.Validate(), Throws.TypeOf()); + } + + [Test] + public void SalaryIsLessOrZeroTest() + { + var post = CreateDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), "post", + PostType.Designer, 0, true, DateTime.UtcNow); + Assert.That(() => post.Validate(), Throws.TypeOf()); + + post = CreateDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), "post", + PostType.Designer, -10, true, DateTime.UtcNow); + Assert.That(() => post.Validate(), Throws.TypeOf()); + } + + [Test] + public void AllFieldsIsCorrectTest() + { + var postId = Guid.NewGuid().ToString(); + var postPostId = Guid.NewGuid().ToString(); + var postName = "name"; + var postType = PostType.Designer; + var salary = 10; + var isActual = false; + var changeDate = DateTime.UtcNow.AddDays(-1); + var post = CreateDataModel(postId, postPostId, postName, postType, salary, isActual, changeDate); + + Assert.That(() => post.Validate(), Throws.Nothing); + + Assert.Multiple(() => + { + Assert.That(post.Id, Is.EqualTo(postId)); + Assert.That(post.PostId, Is.EqualTo(postPostId)); + Assert.That(post.PostName, Is.EqualTo(postName)); + Assert.That(post.PostType, Is.EqualTo(postType)); + Assert.That(post.Salary, Is.EqualTo(salary)); + Assert.That(post.IsActual, Is.EqualTo(isActual)); + Assert.That(post.ChangeDate, Is.EqualTo(changeDate)); + }); + } + + private static PostDataModel CreateDataModel(string? id, string? postId, string? postName, PostType postType, double salary, bool isActual, DateTime changeDate) => + new(id, postId, postName, postType, salary, isActual, changeDate); +} diff --git a/TheCyclopsProject/TheCyclopsTests/DataModelsTests/SalaryDataModelTests.cs b/TheCyclopsProject/TheCyclopsTests/DataModelsTests/SalaryDataModelTests.cs new file mode 100644 index 0000000..bb63037 --- /dev/null +++ b/TheCyclopsProject/TheCyclopsTests/DataModelsTests/SalaryDataModelTests.cs @@ -0,0 +1,56 @@ +using TheCyclopsContracts.DataModels; +using TheCyclopsContracts.Exceptions; + +namespace TheCyclopsTests.DataModelsTests; + +[TestFixture] +internal class SalaryDataModelTests +{ + [Test] + public void WorkerIdIsEmptyTest() + { + var salary = CreateDataModel(null, DateTime.Now, 10); + Assert.That(() => salary.Validate(), Throws.TypeOf()); + + salary = CreateDataModel(string.Empty, DateTime.Now, 10); + Assert.That(() => salary.Validate(), Throws.TypeOf()); + } + + [Test] + public void WorkerIdIsNotGuidTest() + { + var salary = CreateDataModel("employeeId", DateTime.Now, 10); + Assert.That(() => salary.Validate(), Throws.TypeOf()); + } + + [Test] + public void PriceIsLessOrZeroTest() + { + var salary = CreateDataModel(Guid.NewGuid().ToString(), DateTime.Now, 0); + Assert.That(() => salary.Validate(), Throws.TypeOf()); + + salary = CreateDataModel(Guid.NewGuid().ToString(), DateTime.Now, -10); + Assert.That(() => salary.Validate(), Throws.TypeOf()); + } + + [Test] + public void AllFieldsIsCorrectTest() + { + var employeeId = Guid.NewGuid().ToString(); + var salaryDate = DateTime.Now.AddDays(-3).AddMinutes(-5); + var employeeSalary = 10; + var salary = CreateDataModel(employeeId, salaryDate, employeeSalary); + + Assert.That(() => salary.Validate(), Throws.Nothing); + + Assert.Multiple(() => + { + Assert.That(salary.EmployeeId, Is.EqualTo(employeeId)); + Assert.That(salary.SalaryDate, Is.EqualTo(salaryDate)); + Assert.That(salary.Salary, Is.EqualTo(employeeSalary)); + }); + } + + private static SalaryDataModel CreateDataModel(string? employeeId, DateTime salaryDate, double workerSalary) => + new(employeeId, salaryDate, workerSalary); +} diff --git a/TheCyclopsProject/TheCyclopsTests/TheCyclopsTests.csproj b/TheCyclopsProject/TheCyclopsTests/TheCyclopsTests.csproj new file mode 100644 index 0000000..349b926 --- /dev/null +++ b/TheCyclopsProject/TheCyclopsTests/TheCyclopsTests.csproj @@ -0,0 +1,27 @@ + + + + net9.0 + latest + enable + enable + false + + + + + + + + + + + + + + + + + + +