LabWork 1.0.1 (Модели и первые тесты)

This commit is contained in:
Aidar 2025-02-05 22:13:05 +04:00
parent 00906583d7
commit f5537fc5dc
17 changed files with 386 additions and 1 deletions

View File

@ -0,0 +1,28 @@
using BitterlyAndExclamationMarkContracts.Extensions;
using BitterlyAndExclamationMarkContracts.Infrastructure;
using BitterlyAndExclamationMarkContracts.Exceptions;
using System.Text.RegularExpressions;
namespace BitterlyAndExclamationMarkContracts.DataModels;
public class BuyerDataModel(string id, string fio, string phoneNumber, int scoreCount) : IValidation
{
public string Id { get; private set; } = id;
public string FIO { get; private set; } = fio;
public string PhoneNumber { get; private set; } = phoneNumber;
public int ScoreCount { get; private set; } = scoreCount;
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");
if (ScoreCount < 0)
throw new ValidationException("The value in the field ScoreCount is a negative number");
}
}

View File

@ -0,0 +1,31 @@
using BitterlyAndExclamationMarkContracts.Enums;
using BitterlyAndExclamationMarkContracts.Extensions;
using BitterlyAndExclamationMarkContracts.Infrastructure;
using System.ComponentModel.DataAnnotations;
public class DishDataModel(string id, string dishName, DishType dishType, string сookId, double price, bool isDeleted) : IValidation
{
public string Id { get; private set; } = id;
public string DishName { get; private set; } = dishName;
public DishType DishType { get; private set; } = dishType;
public string СookId { get; private set; } = сookId;
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 (DishName.IsEmpty())
throw new ValidationException("Field DishName is empty");
if (DishType == DishType.None)
throw new ValidationException("Field DishType is empty");
if (СookId.IsEmpty())
throw new ValidationException("Field СookId is empty");
if (!СookId.IsGuid())
throw new ValidationException("The value in the field СookId is not a unique identifier");
if (Price <= 0)
throw new ValidationException("Field Price is less than or equal to 0");
}
}

View File

@ -0,0 +1,20 @@
using BitterlyAndExclamationMarkContracts.Extensions;
using BitterlyAndExclamationMarkContracts.Infrastructure;
using System.ComponentModel.DataAnnotations;
namespace BitterlyAndExclamationMarkContracts.DataModels;
public class DishHistoryDataModel(string dishId, double oldPrice) : IValidation
{
public string DishId { get; private set; } = dishId;
public double OldPrice { get; private set; } = oldPrice;
public DateTime ChangeDate { get; private set; } = DateTime.UtcNow;
public void Validate()
{
if (DishId.IsEmpty())
throw new ValidationException("Field DishId is empty");
if (!DishId.IsGuid())
throw new ValidationException("The value in the field DishId is not a unique identifier");
if (OldPrice <= 0)
throw new ValidationException("Field OldPrice is less than or equal to 0");
}
}

View File

@ -0,0 +1,35 @@
using BitterlyAndExclamationMarkContracts.Enums;
using BitterlyAndExclamationMarkContracts.Extensions;
using BitterlyAndExclamationMarkContracts.Infrastructure;
using System.ComponentModel.DataAnnotations;
namespace BitterlyAndExclamationMarkContracts.DataModels;
public class OrderDataModel(string id, string workerId, string? buyerId, double sum, DiscountType discountType, double discount, bool isCancel, List<OrderDishDataModel> dishes) : IValidation
{
public string Id { get; private set; } = id;
public string WorkerId { get; private set; } = workerId;
public string? BuyerId { get; private set; } = buyerId;
public DateTime OrderDate { 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<OrderDishDataModel> Dishes { get; private set; } = dishes;
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 ((Dishes?.Count ?? 0) == 0)
throw new ValidationException("The order must include dishes");
}
}

View File

@ -0,0 +1,24 @@
using BitterlyAndExclamationMarkContracts.Extensions;
using BitterlyAndExclamationMarkContracts.Infrastructure;
using System.ComponentModel.DataAnnotations;
namespace BitterlyAndExclamationMarkContracts.DataModels;
public class OrderDishDataModel(string orderId, string dishId, int count) : IValidation
{
public string OrderId { get; private set; } = orderId;
public string DishId { get; private set; } = dishId;
public int Count { get; private set; } = count;
public void Validate()
{
if (OrderId.IsEmpty())
throw new ValidationException("Field OrderId is empty");
if (!OrderId.IsGuid())
throw new ValidationException("The value in the field OrderId is not a unique identifier");
if (DishId.IsEmpty())
throw new ValidationException("Field DishId is empty");
if (!DishId.IsGuid())
throw new ValidationException("The value in the field DishId is not a unique identifier");
if (Count <= 0)
throw new ValidationException("Field Count is less than or equal to 0");
}
}

View File

@ -0,0 +1,33 @@
using BitterlyAndExclamationMarkContracts.Enums;
using BitterlyAndExclamationMarkContracts.Extensions;
using BitterlyAndExclamationMarkContracts.Infrastructure;
using System.ComponentModel.DataAnnotations;
namespace BitterlyAndExclamationMarkContracts.DataModels;
public class PostDataModel(string id, string postId, string postName, PostType postType, double salary, bool isActual, DateTime changeDate) : IValidation
{
public string Id { get; private set; } = id;
public string PostId { get; private set; } = postId;
public string PostName { get; private set; } = postName;
public PostType PostType { get; private set; } = postType;
public double Salary { get; private set; } = salary;
public bool IsActual { get; private set; } = isActual;
public DateTime ChangeDate { get; private set; } = changeDate;
public void Validate()
{
if (Id.IsEmpty())
throw new ValidationException("Field Id is empty");
if (!Id.IsGuid())
throw new ValidationException("The value in the field Id is not a unique identifier");
if (PostId.IsEmpty())
throw new ValidationException("Field PostId is empty");
if (!PostId.IsGuid())
throw new ValidationException("The value in the field PostId is not a unique identifier");
if (PostName.IsEmpty())
throw new ValidationException("Field PostName is empty");
if (PostType == PostType.None)
throw new ValidationException("Field PostType is empty");
if (Salary <= 0)
throw new ValidationException("Field Salary is empty");
}
}

View File

@ -0,0 +1,20 @@
using BitterlyAndExclamationMarkContracts.Extensions;
using BitterlyAndExclamationMarkContracts.Infrastructure;
using System.ComponentModel.DataAnnotations;
namespace BitterlyAndExclamationMarkContracts.DataModels;
public class SalaryDataModel(string workerId, DateTime salaryDate, double workerSalary) : IValidation
{
public string WorkerId { get; private set; } = workerId;
public DateTime SalaryDate { get; private set; } = salaryDate;
public double Salary { get; private set; } = workerSalary;
public void Validate()
{
if (WorkerId.IsEmpty())
throw new ValidationException("Field WorkerId is empty");
if (!WorkerId.IsGuid())
throw new ValidationException("The value in the field WorkerId is not a unique identifier");
if (Salary <= 0)
throw new ValidationException("Field Salary is less than or equal to 0");
}
}

View File

@ -0,0 +1,37 @@
using BitterlyAndExclamationMarkContracts.Extensions;
using BitterlyAndExclamationMarkContracts.Infrastructure;
using System.ComponentModel.DataAnnotations;
namespace BitterlyAndExclamationMarkContracts.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(-18).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 < 18) // EmploymentDate.Year - BirthDate.Year
throw new ValidationException($"Minors cannot be hired " +
$"(EmploymentDate - {EmploymentDate.ToShortDateString()}, BirthDate - {BirthDate.ToShortDateString()})");
if ((EmploymentDate - BirthDate).TotalDays / 365 > 80) // EmploymentDate.Year - BirthDate.Year
throw new ValidationException($"Elders cannot be hired " +
$"(EmploymentDate - {EmploymentDate.ToShortDateString()}, BirthDate - {BirthDate.ToShortDateString()})");
}
}

View File

@ -0,0 +1,10 @@
namespace BitterlyAndExclamationMarkContracts.Enums;
[Flags]
public enum DiscountType
{
None = 0,
OnSale = 1,
RegularCustomer = 2,
UsingScores = 4
}

View File

@ -0,0 +1,9 @@
namespace BitterlyAndExclamationMarkContracts.Enums;
public enum DishType
{
None = 0,
Porridge = 1,
Soups = 2,
Pancakes = 3
}

View File

@ -0,0 +1,9 @@
namespace BitterlyAndExclamationMarkContracts.Enums;
public enum PostType
{
None = 0,
Supervisor = 1,
Сook = 2,
Assistant = 3
}

View File

@ -0,0 +1,5 @@
namespace BitterlyAndExclamationMarkContracts.Exceptions;
public class ValidationException(string message) : Exception(message)
{
}

View File

@ -0,0 +1,14 @@
namespace BitterlyAndExclamationMarkContracts.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 _);
}
}

View File

@ -0,0 +1,6 @@
namespace BitterlyAndExclamationMarkContracts.Infrastructure;
public interface IValidation
{
void Validate();
}

View File

@ -1,10 +1,12 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.12.35514.174 d17.12
VisualStudioVersion = 17.12.35514.174
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BitterlyAndExclamationMarkContracts", "BitterlyAndExclamationMarkContracts\BitterlyAndExclamationMarkContracts.csproj", "{9C9F5E93-E3DE-4D54-9A48-4AA141C31BBA}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BitterlyAndExclamationMarkTests", "BitterlyAndExclamationMarkTests\BitterlyAndExclamationMarkTests.csproj", "{5A60D89F-410A-4EB8-B012-5C4D3B929A17}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@ -15,6 +17,10 @@ Global
{9C9F5E93-E3DE-4D54-9A48-4AA141C31BBA}.Debug|Any CPU.Build.0 = Debug|Any CPU
{9C9F5E93-E3DE-4D54-9A48-4AA141C31BBA}.Release|Any CPU.ActiveCfg = Release|Any CPU
{9C9F5E93-E3DE-4D54-9A48-4AA141C31BBA}.Release|Any CPU.Build.0 = Release|Any CPU
{5A60D89F-410A-4EB8-B012-5C4D3B929A17}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{5A60D89F-410A-4EB8-B012-5C4D3B929A17}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5A60D89F-410A-4EB8-B012-5C4D3B929A17}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5A60D89F-410A-4EB8-B012-5C4D3B929A17}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE

View File

@ -0,0 +1,28 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="coverlet.collector" Version="6.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="NUnit" Version="3.14.0" />
<PackageReference Include="NUnit.Analyzers" Version="3.9.0" />
<PackageReference Include="NUnit3TestAdapter" Version="4.5.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\BitterlyAndExclamationMarkContracts\BitterlyAndExclamationMarkContracts.csproj" />
</ItemGroup>
<ItemGroup>
<Using Include="NUnit.Framework" />
</ItemGroup>
</Project>

View File

@ -0,0 +1,70 @@
using BitterlyAndExclamationMarkContracts.DataModels;
using BitterlyAndExclamationMarkContracts.Exceptions;
namespace BitterlyAndExclamationMarkTests.DataModelsTests;
[TestFixture]
internal class BuyerDataModelTests
{
[Test]
public void IdIsNullOrEmptyTest()
{
var buyer = CreateDataModel(null, "fio", "number", 10);
Assert.That(() => buyer.Validate(),
Throws.TypeOf<ValidationException>());
buyer = CreateDataModel(string.Empty, "fio", "number", 10);
Assert.That(() => buyer.Validate(),
Throws.TypeOf<ValidationException>());
}
[Test]
public void IdIsNotGuidTest()
{
var buyer = CreateDataModel("id", "fio", "number", 10);
Assert.That(() => buyer.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void FIOIsNullOrEmptyTest()
{
var buyer = CreateDataModel(Guid.NewGuid().ToString(), null, "number", 10);
Assert.That(() => buyer.Validate(), Throws.TypeOf<ValidationException>());
buyer = CreateDataModel(Guid.NewGuid().ToString(), string.Empty, "number", 10);
Assert.That(() => buyer.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void PhoneNumberIsNullOrEmptyTest()
{
var buyer = CreateDataModel(Guid.NewGuid().ToString(), "fio", null, 10);
Assert.That(() => buyer.Validate(), Throws.TypeOf<ValidationException>());
buyer = CreateDataModel(Guid.NewGuid().ToString(), "fio", string.Empty, 10);
Assert.That(() => buyer.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void PhoneNumberIsIncorrectTest()
{
var buyer = CreateDataModel(Guid.NewGuid().ToString(), "fio", "777", 10);
Assert.That(() => buyer.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void AllFieldsIsCorrectTest()
{
var buyerId = Guid.NewGuid().ToString();
var fio = "Fio";
var phoneNumber = "+7-777-777-77-77";
var scoreCount = 11;
var buyer = CreateDataModel(buyerId, fio, phoneNumber, scoreCount);
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.ScoreCount, Is.EqualTo(scoreCount));
});
}
private static BuyerDataModel CreateDataModel(string? id, string? fio, string? phoneNumber, int scoreCount) =>
new(id, fio, phoneNumber, scoreCount);
}