Лаба1

This commit is contained in:
Ь Ъ 2025-02-13 13:18:09 +04:00
parent eade380f6e
commit f3422b63e8
4 changed files with 32 additions and 27 deletions

View File

@ -31,7 +31,13 @@ public class EmployeeDataModel(string id, string name, string phone, string post
if (Name.IsEmpty()) if (Name.IsEmpty())
throw new ValidationException("Field Name is empty"); throw new ValidationException("Field Name is empty");
if (Phone.IsEmpty()) { throw new ValidationException("Field PhoneNumber is empty"); } if (!Regex.IsMatch(name, @"^[A-Za-zА\s]+$"))
{
throw new ValidationException("Field Name is not valid");
}
if (Phone.IsEmpty()) { throw new ValidationException("Field Phone is empty"); }
if (!Regex.IsMatch(Phone, @"^\+7[\s-]?\d{3}[\s-]?\d{3}[\s-]?\d{2}[\s-]?\d{2}$")) { throw new ValidationException("Field PhoneNumber is not a phone number"); } if (!Regex.IsMatch(Phone, @"^\+7[\s-]?\d{3}[\s-]?\d{3}[\s-]?\d{2}[\s-]?\d{2}$")) { throw new ValidationException("Field PhoneNumber is not a phone number"); }
@ -47,7 +53,7 @@ public class EmployeeDataModel(string id, string name, string phone, string post
if (EmploymentDate.Date < Dateofbirth.Date) if (EmploymentDate.Date < Dateofbirth.Date)
throw new ValidationException("The date of employment cannot be less than the date of birth"); throw new ValidationException("The date of employment cannot be less than the date of birth");
if ((EmploymentDate - Dateofbirth).TotalDays / 365 < 16) // EmploymentDate.Year - BirthDate.Year if ((EmploymentDate - Dateofbirth).TotalDays / 365 < 18)
throw new ValidationException($"Minors cannot be hired (EmploymentDate - {EmploymentDate.ToShortDateString()}, BirthDate - BirthDate.ToShortDateString())"); throw new ValidationException($"Minors cannot be hired (EmploymentDate - {EmploymentDate.ToShortDateString()}, BirthDate - BirthDate.ToShortDateString())");
} }
} }

View File

@ -10,13 +10,12 @@ using HamContract.Infrastructure;
namespace HamContract.DataModels; namespace HamContract.DataModels;
public class PostDataModel(string id, string postId, string postName, PostType postType, double salary, bool isActual, DateTime changeDate) : IValidation 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 Id { get; private set; } = id;
public string PostId { get; private set; } = postId; public string PostId { get; private set; } = postId;
public string PostName { get; private set; } = postName; public string PostName { get; private set; } = postName;
public PostType PostType { get; private set; } = postType; public PostType PostType { get; private set; } = postType;
public double Salary { get; private set; } = salary;
public bool IsActual { get; private set; } = isActual; public bool IsActual { get; private set; } = isActual;
public DateTime ChangeDate { get; private set; } = changeDate; public DateTime ChangeDate { get; private set; } = changeDate;
@ -40,7 +39,6 @@ public class PostDataModel(string id, string postId, string postName, PostType p
if (PostType == PostType.None) if (PostType == PostType.None)
throw new ValidationException("Field PostType is empty"); throw new ValidationException("Field PostType is empty");
if (Salary <= 0)
throw new ValidationException("Field Salary is empty");
} }
} }

View File

@ -7,11 +7,9 @@ using System.Threading.Tasks;
using HamContract.DataModels; using HamContract.DataModels;
namespace HamTests.DataModelsTest; namespace HamTests.DataModelsTest;
[TestFixture]
internal class EmployeeDataModelTest
{
[TestFixture] [TestFixture]
internal class WorkerDataModelTests internal class EmployeeDataModelTest
{ {
[Test] [Test]
public void IdIsNullOrEmptyTest() public void IdIsNullOrEmptyTest()
@ -48,6 +46,13 @@ internal class EmployeeDataModelTest
Assert.That(() => employee.Validate(), Throws.TypeOf<ValidationException>()); Assert.That(() => employee.Validate(), Throws.TypeOf<ValidationException>());
} }
[Test]
public void NameIsIncorrectTest()
{
var employee = CreateDataModel(Guid.NewGuid().ToString(), "name","777", Guid.NewGuid().ToString(), DateTime.Now.AddYears(-18), DateTime.Now, false);
Assert.That(() => employee.Validate(), Throws.TypeOf<ValidationException>());
}
[Test] [Test]
public void PhoneIsIncorrectTest() public void PhoneIsIncorrectTest()
{ {
@ -55,6 +60,10 @@ internal class EmployeeDataModelTest
Assert.That(() => employee.Validate(), Throws.TypeOf<ValidationException>()); Assert.That(() => employee.Validate(), Throws.TypeOf<ValidationException>());
} }
[Test] [Test]
public void PostIdIsNullOrEmptyTest() public void PostIdIsNullOrEmptyTest()
{ {
@ -92,7 +101,7 @@ internal class EmployeeDataModelTest
{ {
var employeeId = Guid.NewGuid().ToString(); var employeeId = Guid.NewGuid().ToString();
var name = "name"; var name = "name";
var phone = "+7-777-777-77-77"; var phone = "+7-732-234-23-77";
var postId = Guid.NewGuid().ToString(); var postId = Guid.NewGuid().ToString();
var dateofbirth = DateTime.Now.AddYears(-18).AddDays(-1); var dateofbirth = DateTime.Now.AddYears(-18).AddDays(-1);
var employmentDate = DateTime.Now; var employmentDate = DateTime.Now;
@ -114,4 +123,4 @@ internal class EmployeeDataModelTest
new(id, name, phone, postId, dateofbirth, employmentDate, isDeleted); new(id, name, phone, postId, dateofbirth, employmentDate, isDeleted);
} }
}

View File

@ -62,15 +62,8 @@ internal class PostDataModelTests
} }
[Test] [Test]
public void SalaryIsLessOrZeroTest()
{
var post = CreateDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), "name", PostType.Butcher, 0, true, DateTime.UtcNow);
Assert.That(() => post.Validate(), Throws.TypeOf<ValidationException>());
post = CreateDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), "name", PostType.Butcher, -10, true, DateTime.UtcNow);
Assert.That(() => post.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void AllFieldsIsCorrectTest() public void AllFieldsIsCorrectTest()
{ {
var postId = Guid.NewGuid().ToString(); var postId = Guid.NewGuid().ToString();
@ -89,11 +82,10 @@ internal class PostDataModelTests
Assert.That(post.PostId, Is.EqualTo(postPostId)); Assert.That(post.PostId, Is.EqualTo(postPostId));
Assert.That(post.PostName, Is.EqualTo(postName)); Assert.That(post.PostName, Is.EqualTo(postName));
Assert.That(post.PostType, Is.EqualTo(postType)); Assert.That(post.PostType, Is.EqualTo(postType));
Assert.That(post.Salary, Is.EqualTo(salary));
Assert.That(post.IsActual, Is.EqualTo(isActual)); Assert.That(post.IsActual, Is.EqualTo(isActual));
Assert.That(post.ChangeDate, Is.EqualTo(changeDate)); 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) => 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); new(id, postId, postName, postType,isActual, changeDate);
} }