fix post model and post tests

This commit is contained in:
Ivan Gutorov 2025-02-16 05:17:05 +04:00
parent 4141cca5d8
commit c3b6b9602b
2 changed files with 12 additions and 42 deletions

View File

@ -4,12 +4,10 @@ using TheCyclopsContracts.Extensions;
namespace TheCyclopsContracts.DataModels;
public class PostDataModel(string id, string PostId, string postName, PostType postType, double salary, bool isActual, DateTime changeDate)
public class PostDataModel(string id, 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;
@ -28,12 +26,6 @@ public class PostDataModel(string id, string PostId, string postName, PostType p
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");

View File

@ -10,11 +10,11 @@ internal class PostDataModelTests
[Test]
public void IdIsNullOrEmptyTest()
{
var post = CreateDataModel(null, Guid.NewGuid().ToString(), "post",
var post = CreateDataModel(null, "post",
PostType.Designer, 10, true, DateTime.UtcNow);
Assert.That(() => post.Validate(), Throws.TypeOf<ValidationException>());
post = CreateDataModel(string.Empty, Guid.NewGuid().ToString(), "post",
post = CreateDataModel(string.Empty, "post",
PostType.Designer, 10, true, DateTime.UtcNow);
Assert.That(() => post.Validate(), Throws.TypeOf<ValidationException>());
}
@ -22,27 +22,7 @@ internal class PostDataModelTests
[Test]
public void IdIsNotGuidTest()
{
var post = CreateDataModel("id", Guid.NewGuid().ToString(), "post",
PostType.Designer, 10, true, DateTime.UtcNow);
Assert.That(() => post.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void PostIdIsNullEmptyTest()
{
var post = CreateDataModel(Guid.NewGuid().ToString(), null, "post",
PostType.Designer, 10, true, DateTime.UtcNow);
Assert.That(() => post.Validate(), Throws.TypeOf<ValidationException>());
post = CreateDataModel(Guid.NewGuid().ToString(), string.Empty, "post",
PostType.Designer, 10, true, DateTime.UtcNow);
Assert.That(() => post.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void PostIdIsNotGuidTest()
{
var post = CreateDataModel(Guid.NewGuid().ToString(), "postId", "post",
var post = CreateDataModel("id", "post",
PostType.Designer, 10, true, DateTime.UtcNow);
Assert.That(() => post.Validate(), Throws.TypeOf<ValidationException>());
}
@ -50,11 +30,11 @@ internal class PostDataModelTests
[Test]
public void PostNameIsEmptyTest()
{
var manufacturer = CreateDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), null,
var manufacturer = CreateDataModel(Guid.NewGuid().ToString(), null,
PostType.Designer, 10, true, DateTime.UtcNow);
Assert.That(() => manufacturer.Validate(), Throws.TypeOf<ValidationException>());
manufacturer = CreateDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), string.Empty,
manufacturer = CreateDataModel(Guid.NewGuid().ToString(), string.Empty,
PostType.Designer, 10, true, DateTime.UtcNow);
Assert.That(() => manufacturer.Validate(), Throws.TypeOf<ValidationException>());
}
@ -62,7 +42,7 @@ internal class PostDataModelTests
[Test]
public void PostTypeIsNoneTest()
{
var post = CreateDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), "post",
var post = CreateDataModel(Guid.NewGuid().ToString(), "post",
PostType.None, 10, true, DateTime.UtcNow);
Assert.That(() => post.Validate(), Throws.TypeOf<ValidationException>());
}
@ -70,11 +50,11 @@ internal class PostDataModelTests
[Test]
public void SalaryIsLessOrZeroTest()
{
var post = CreateDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), "post",
var post = CreateDataModel(Guid.NewGuid().ToString(), "post",
PostType.Designer, 0, true, DateTime.UtcNow);
Assert.That(() => post.Validate(), Throws.TypeOf<ValidationException>());
post = CreateDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), "post",
post = CreateDataModel(Guid.NewGuid().ToString(), "post",
PostType.Designer, -10, true, DateTime.UtcNow);
Assert.That(() => post.Validate(), Throws.TypeOf<ValidationException>());
}
@ -83,20 +63,18 @@ internal class PostDataModelTests
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);
var post = CreateDataModel(postId, 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));
@ -105,6 +83,6 @@ internal class PostDataModelTests
});
}
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);
private static PostDataModel CreateDataModel(string? id, string? postName, PostType postType, double salary, bool isActual, DateTime changeDate) =>
new(id, postName, postType, salary, isActual, changeDate);
}