PIbd-22_Khaliullov_R.M_ LabWork01 #1

Open
rakhaliullov wants to merge 2 commits from Task_1_Models into main
24 changed files with 947 additions and 1 deletions

View File

@ -0,0 +1,42 @@
using SoftBedContracts.Exceptions;
using SoftBedContracts.Extensions;
using SoftBedContracts.Infrastructure;
namespace SoftBedContracts.DataModels;
public class CollectDataModel(string id, string workerId, string furnitureId, double sum) : IValidation
{
public string Id { get; private set; } = id;
public string WorkerId { get; private set; } = workerId;
public string FurnitureId { get; private set; } = furnitureId;
public DateTime SaleDate { get; private set; } = DateTime.UtcNow;
public double Sum { get; private set; } = sum;
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 (FurnitureId.IsEmpty())
throw new ValidationException("Field FurnitureId is empty");
if (!FurnitureId.IsGuid())
throw new ValidationException("The value in the field FurnitureId is not a unique identifier");
if (Sum <= 0)
throw new ValidationException("Field Sum is less than or equal to 0");
}
}

View File

@ -0,0 +1,42 @@
using SoftBedContracts.Enums;
using SoftBedContracts.Exceptions;
using SoftBedContracts.Extensions;
using SoftBedContracts.Infrastructure;
namespace SoftBedContracts.DataModels;
public class FurnitureDataModel(string id, string furnitureName, FurnitureType furnitureType, double price, bool isDeleted, List<FurnitureWorkPieceDataModel> workPieces) : IValidation
{
public string Id { get; private set; } = id;
public string FurnitureName { get; private set; } = furnitureName;
public FurnitureType FurnitureType { get; private set; } = furnitureType;
public double Price { get; private set; } = price;
public bool IsDeleted { get; private set; } = isDeleted;
public List<FurnitureWorkPieceDataModel> WorkPieces { get; private set; } = workPieces;
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 (FurnitureName.IsEmpty())
throw new ValidationException("Field FurnitureName is empty");
if (FurnitureType == FurnitureType.None)
throw new ValidationException("Field FurnitureType is empty");
if (Price <= 0)
throw new ValidationException("Field Price is less than or equal to 0");
if ((WorkPieces?.Count ?? 0) == 0)
throw new ValidationException("The sale must include Work Pieces");
}
}

View File

@ -0,0 +1,26 @@
using SoftBedContracts.Exceptions;
using SoftBedContracts.Extensions;
using SoftBedContracts.Infrastructure;
namespace SoftBedContracts.DataModels;
public class FurnitureHistoryDataModel(string furnitureId, double oldPrice) : IValidation
{
public string FurnitureId { get; private set; } = furnitureId;
public double OldPrice { get; private set; } = oldPrice;
public DateTime ChangeDate { get; private set; } = DateTime.UtcNow;
public void Validate()
{
if (FurnitureId.IsEmpty())
throw new ValidationException("Field FurnitureId is empty");
if (!FurnitureId.IsGuid())
throw new ValidationException("The value in the field FurnitureId 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,31 @@
using SoftBedContracts.Exceptions;
using SoftBedContracts.Extensions;
using SoftBedContracts.Infrastructure;
namespace SoftBedContracts.DataModels;
public class FurnitureWorkPieceDataModel(string furnitureId, string workPieceId, int count) : IValidation
{
public string FurnitureId { get; private set; } = furnitureId;
public string WorkPieceId { get; private set; } = workPieceId;
public int Count { get; private set; } = count;
public void Validate()
{
if (FurnitureId.IsEmpty())
throw new ValidationException("Field FurnitureId is empty");
if (!FurnitureId.IsGuid())
throw new ValidationException("The value in the field FurnitureId is not a unique identifier");
if (WorkPieceId.IsEmpty())
throw new ValidationException("Field WorkPieceId is empty");
if (!WorkPieceId.IsGuid())
throw new ValidationException("The value in the field WorkPieceId 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,39 @@
using SoftBedContracts.Enums;
using SoftBedContracts.Exceptions;
using SoftBedContracts.Extensions;
using SoftBedContracts.Infrastructure;
namespace SoftBedContracts.DataModels;
public class PostDataModel(string id, string postName, PostType postType, double salary, bool isActual, DateTime changeDate) : IValidation
{
public string Id { get; private set; } = id;
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 (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,26 @@
using SoftBedContracts.Exceptions;
using SoftBedContracts.Extensions;
using SoftBedContracts.Infrastructure;
namespace SoftBedContracts.DataModels;
public class SalaryDataModel(string workerId, DateTime salaryDate, double salary) : IValidation
{
public string WorkerId { get; private set; } = workerId;
public DateTime SalaryDate { get; private set; } = salaryDate;
public double Salary { get; private set; } = salary;
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,33 @@
using SoftBedContracts.Exceptions;
using SoftBedContracts.Extensions;
using SoftBedContracts.Infrastructure;
using System.Text.RegularExpressions;
namespace SoftBedContracts.DataModels;
public class WorkPieceDataModel(string id, string size, double weight) : IValidation
{
public string Id { get; private set; } = id;
public string Size { get; private set; } = size;
public double Weight { get; private set; } = weight;
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 (Size.IsEmpty())
throw new ValidationException("Field Size is empty");
if (!Regex.IsMatch(Size, @"^\d+x\d+$"))
throw new ValidationException("Field Size has an invalid format. Expected format: number x number (e.g., '120x50').");
if (Weight <= 0)
throw new ValidationException("Field Weight is less than or equal to 0");
}
}

View File

@ -0,0 +1,45 @@
using SoftBedContracts.Exceptions;
using SoftBedContracts.Extensions;
using SoftBedContracts.Infrastructure;
namespace SoftBedContracts.DataModels;
public class WorkerDataModel(string id, string fio, string postId, DateTime birthDate, DateTime employmentDate) : 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 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()})");
}
}

View File

@ -0,0 +1,9 @@
namespace SoftBedContracts.Enums;
public enum FurnitureType
{
None = 0,
UpholsteredFurniture = 1,
CabinetFurniture = 2,
FoldingFurniture = 3
}

View File

@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SoftBedContracts.Enums
{
internal class MaterialType
{
}
}

View File

@ -0,0 +1,9 @@
namespace SoftBedContracts.Enums;
public enum PostType
{
None = 0,
Assembler = 1,
Loader = 2,
Assistent = 3
}

View File

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

View File

@ -0,0 +1,14 @@
namespace SoftBedContracts.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 SoftBedContracts.Infrastructure;
public interface IValidation
{
void Validate();
}

View File

@ -0,0 +1,85 @@
using SoftBedContracts.DataModels;
using SoftBedContracts.Enums;
using SoftBedContracts.Exceptions;
namespace SoftBedTests.DataModelsTests;
[TestFixture]
internal class CollectDataModelTest
{
[Test]
public void IdIsNullOrEmptyTest()
{
var collect = CreateDataModel(null, Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 10);
Assert.That(() => collect.Validate(), Throws.TypeOf<ValidationException>());
collect = CreateDataModel(string.Empty, Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 10);
Assert.That(() => collect.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void IdIsNotGuidTest()
{
var collect = CreateDataModel("id", Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 10);
Assert.That(() => collect.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void WorkerIdIsNullOrEmptyTest()
{
var collect = CreateDataModel(Guid.NewGuid().ToString(), null, Guid.NewGuid().ToString(), 10);
Assert.That(() => collect.Validate(), Throws.TypeOf<ValidationException>());
collect = CreateDataModel(Guid.NewGuid().ToString(), string.Empty, Guid.NewGuid().ToString(), 10);
Assert.That(() => collect.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void WorkerIdIsNotGuidTest()
{
var collect = CreateDataModel(Guid.NewGuid().ToString(), "workerId", Guid.NewGuid().ToString(), 10);
Assert.That(() => collect.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void FurnitureIdIsNullOrEmptyTest()
{
var collect = CreateDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), null, 10);
Assert.That(() => collect.Validate(), Throws.TypeOf<ValidationException>());
collect = CreateDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), string.Empty, 10);
Assert.That(() => collect.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void FurnitureIdIsNotGuidTest()
{
var collect = CreateDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), "furnitureId", 10);
Assert.That(() => collect.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void SumIsLessOrZeroTest()
{
var collect = CreateDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 0);
Assert.That(() => collect.Validate(), Throws.TypeOf<ValidationException>());
collect = CreateDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), -10);
Assert.That(() => collect.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void AllFieldsIsCorrectTest()
{
var collectId = Guid.NewGuid().ToString();
var workerId = Guid.NewGuid().ToString();
var furnitureId = Guid.NewGuid().ToString();
var sum = 10;
var collect = CreateDataModel(collectId, workerId, furnitureId, sum);
Assert.That(() => collect.Validate(), Throws.Nothing);
Assert.Multiple(() =>
{
Assert.That(collect.Id, Is.EqualTo(collectId));
Assert.That(collect.WorkerId, Is.EqualTo(workerId));
Assert.That(collect.FurnitureId, Is.EqualTo(furnitureId));
Assert.That(collect.Sum, Is.EqualTo(sum));
});
}
public static CollectDataModel CreateDataModel(string? id, string? workerId, string? furnitureId, double sum) =>
new(id, workerId, furnitureId, sum);
}

View File

@ -0,0 +1,82 @@
using SoftBedContracts.DataModels;
using SoftBedContracts.Enums;
using SoftBedContracts.Exceptions;
namespace SoftBedTests.DataModelsTests;
[TestFixture]
internal class FurnitureDataModelTests
{
[Test]
public void IdIsNullOrEmptyTest()
{
var furniture = CreateDataModel(null, "furnitureName", FurnitureType.CabinetFurniture, 10, false, CreateSubDataModel());
Assert.That(() => furniture.Validate(), Throws.TypeOf<ValidationException>());
furniture = CreateDataModel(string.Empty, "furnitureName", FurnitureType.CabinetFurniture, 10, false, CreateSubDataModel());
Assert.That(() => furniture.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void IdIsNotGuidTest()
{
var furniture = CreateDataModel("id", "furnitureName", FurnitureType.CabinetFurniture, 10, false, CreateSubDataModel());
Assert.That(() => furniture.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void FurnitureNameIsEmptyTest()
{
var furniture = CreateDataModel(Guid.NewGuid().ToString(), null, FurnitureType.CabinetFurniture, 10, false, CreateSubDataModel());
Assert.That(() => furniture.Validate(), Throws.TypeOf<ValidationException>());
furniture = CreateDataModel(Guid.NewGuid().ToString(), string.Empty, FurnitureType.CabinetFurniture, 10, false, CreateSubDataModel());
Assert.That(() => furniture.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void FurnitureTypeIsNoneTest()
{
var furniture = CreateDataModel(Guid.NewGuid().ToString(), "furnitureName", FurnitureType.None, 10, false, CreateSubDataModel());
Assert.That(() => furniture.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void PriceIsLessOrZeroTest()
{
var furniture = CreateDataModel(Guid.NewGuid().ToString(), "furnitureName", FurnitureType.CabinetFurniture, 0, false, CreateSubDataModel());
Assert.That(() => furniture.Validate(), Throws.TypeOf<ValidationException>());
furniture = CreateDataModel(Guid.NewGuid().ToString(), "furnitureName", FurnitureType.CabinetFurniture, -10, false, CreateSubDataModel());
Assert.That(() => furniture.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void WorkPiecesIsNullOrEmptyTest()
{
var furniture = CreateDataModel(Guid.NewGuid().ToString(), "furnitureName", FurnitureType.CabinetFurniture, 0, false, null);
Assert.That(() => furniture.Validate(), Throws.TypeOf<ValidationException>());
furniture = CreateDataModel(Guid.NewGuid().ToString(), "furnitureName", FurnitureType.CabinetFurniture, 0, false, []);
Assert.That(() => furniture.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void AllFieldsIsCorrectTest()
{
var furnitureId = Guid.NewGuid().ToString();
var furnitureName = "furnitureName";
var furnitureType = FurnitureType.CabinetFurniture;
var price = 10;
var isDeleted = false;
var workPieces = CreateSubDataModel();
var furniture = CreateDataModel(furnitureId, furnitureName, furnitureType, price, isDeleted, workPieces);
Assert.That(() => furniture.Validate(), Throws.Nothing);
Assert.Multiple(() =>
{
Assert.That(furniture.Id, Is.EqualTo(furnitureId));
Assert.That(furniture.FurnitureName, Is.EqualTo(furnitureName));
Assert.That(furniture.FurnitureType, Is.EqualTo(furnitureType));
Assert.That(furniture.Price, Is.EqualTo(price));
Assert.That(furniture.IsDeleted, Is.EqualTo(isDeleted));
});
}
public static FurnitureDataModel CreateDataModel(string? id, string? furnitureName, FurnitureType furnitureType, double price, bool isDeleted, List<FurnitureWorkPieceDataModel> workPieces) =>
new(id, furnitureName, furnitureType, price, isDeleted, workPieces);
private static List<FurnitureWorkPieceDataModel> CreateSubDataModel()
=> [new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 1)];
}

View File

@ -0,0 +1,52 @@
using SoftBedContracts.DataModels;
using SoftBedContracts.Exceptions;
namespace SoftBedTests.DataModelsTests;
[TestFixture]
internal class FurnitureHistoryDataModelTests
{
[Test]
public void ProductIdIsNullOrEmptyTest()
{
var furniture = CreateDataModel(null, 10);
Assert.That(() => furniture.Validate(), Throws.TypeOf<ValidationException>());
furniture = CreateDataModel(string.Empty, 10);
Assert.That(() => furniture.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void ProductIdIsNotGuidTest()
{
var furniture = CreateDataModel("id", 10);
Assert.That(() => furniture.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void OldPriceIsLessOrZeroTest()
{
var furniture = CreateDataModel(Guid.NewGuid().ToString(), 0);
Assert.That(() => furniture.Validate(), Throws.TypeOf<ValidationException>());
furniture = CreateDataModel(Guid.NewGuid().ToString(), -10);
Assert.That(() => furniture.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void AllFieldsIsCorrectTest()
{
var furnitureId = Guid.NewGuid().ToString();
var oldPrice = 10;
var furnitureHistory = CreateDataModel(furnitureId, oldPrice);
Assert.That(() => furnitureHistory.Validate(), Throws.Nothing);
Assert.Multiple(() =>
{
Assert.That(furnitureHistory.FurnitureId, Is.EqualTo(furnitureId));
Assert.That(furnitureHistory.OldPrice, Is.EqualTo(oldPrice));
Assert.That(furnitureHistory.ChangeDate, Is.LessThan(DateTime.UtcNow));
Assert.That(furnitureHistory.ChangeDate, Is.GreaterThan(DateTime.UtcNow.AddMinutes(-1)));
});
}
private static FurnitureHistoryDataModel CreateDataModel(string? furnitureId, double oldPrice) =>
new(furnitureId, oldPrice);
}

View File

@ -0,0 +1,66 @@
using SoftBedContracts.DataModels;
using SoftBedContracts.Exceptions;
namespace SoftBedTests.DataModelsTests;
[TestFixture]
internal class FurnitureWorkPieceDataModelTests
{
[Test]
public void FurnitureIdIsNullOrEmptyTest()
{
var furnitureWorkPiece = CreateDataModel(null, Guid.NewGuid().ToString(), 10);
Assert.That(() => furnitureWorkPiece.Validate(), Throws.TypeOf<ValidationException>());
furnitureWorkPiece = CreateDataModel(string.Empty, Guid.NewGuid().ToString(), 10);
Assert.That(() => furnitureWorkPiece.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void FurnitureIdIsNotGuidTest()
{
var furnitureWorkPiece = CreateDataModel("furnitureId", Guid.NewGuid().ToString(), 10);
Assert.That(() => furnitureWorkPiece.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void WorkPieceIdIsNullOrEmptyTest()
{
var furnitureWorkPiece = CreateDataModel(Guid.NewGuid().ToString(), null, 10);
Assert.That(() => furnitureWorkPiece.Validate(), Throws.TypeOf<ValidationException>());
furnitureWorkPiece = CreateDataModel(Guid.NewGuid().ToString(), string.Empty, 10);
Assert.That(() => furnitureWorkPiece.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void WorkPieceIdIsNotGuidTest()
{
var furnitureWorkPiece = CreateDataModel(Guid.NewGuid().ToString(), "workPieceId", 10);
Assert.That(() => furnitureWorkPiece.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void CountIsLessOrZeroTest()
{
var furnitureWorkPiece = CreateDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 0);
Assert.That(() => furnitureWorkPiece.Validate(), Throws.TypeOf<ValidationException>());
furnitureWorkPiece = CreateDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), -10);
Assert.That(() => furnitureWorkPiece.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void AllFieldsIsCorrectTest()
{
var furnitureId = Guid.NewGuid().ToString();
var workPieceId = Guid.NewGuid().ToString();
var count = 10;
var furnitureWorkPiece = CreateDataModel(furnitureId, workPieceId, count);
Assert.That(() => furnitureWorkPiece.Validate(), Throws.Nothing);
Assert.Multiple(() =>
{
Assert.That(furnitureWorkPiece.FurnitureId, Is.EqualTo(furnitureId));
Assert.That(furnitureWorkPiece.WorkPieceId, Is.EqualTo(workPieceId));
Assert.That(furnitureWorkPiece.Count, Is.EqualTo(count));
});
}
public static FurnitureWorkPieceDataModel CreateDataModel(string? furnitureId, string? workPieceId, int count) =>
new(furnitureId, workPieceId, count);
}

View File

@ -0,0 +1,74 @@
using SoftBedContracts.DataModels;
using SoftBedContracts.Enums;
using SoftBedContracts.Exceptions;
using System.Data;
namespace SoftBedTests.DataModelsTests;
[TestFixture]
internal class PostDataModelTests
{
[Test]
public void IdIsNullOrEmptyTest()
{
var post = CreateDataModel(null, "postName", PostType.Loader, 10, true, DateTime.UtcNow);
Assert.That(() => post.Validate(), Throws.TypeOf<ValidationException>());
post = CreateDataModel(string.Empty, "postName", PostType.Loader, 10, true, DateTime.UtcNow);
Assert.That(() => post.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void IdIsNotGuidTest()
{
var post = CreateDataModel("id", "postName", PostType.Loader, 10, true, DateTime.UtcNow);
Assert.That(() => post.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void PostNameIsEmptyTest()
{
var post = CreateDataModel(Guid.NewGuid().ToString(), null, PostType.Loader, 10, true, DateTime.UtcNow);
Assert.That(() => post.Validate(), Throws.TypeOf<ValidationException>());
post = CreateDataModel(Guid.NewGuid().ToString(), string.Empty, PostType.Loader, 10, true, DateTime.UtcNow);
Assert.That(() => post.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void PostTypeIsNoneTest()
{
var post = CreateDataModel(Guid.NewGuid().ToString(), "postName", PostType.None, 10, true, DateTime.UtcNow);
Assert.That(() => post.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void SalaryIsLessOrZeroTest()
{
var post = CreateDataModel(Guid.NewGuid().ToString(), "postName", PostType.Loader, 0, true, DateTime.UtcNow);
Assert.That(() => post.Validate(), Throws.TypeOf<ValidationException>());
post = CreateDataModel(Guid.NewGuid().ToString(), "postName", PostType.Loader, -10, true, DateTime.UtcNow);
Assert.That(() => post.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void AllFieldsIsCorrectTest()
{
var postId = Guid.NewGuid().ToString();
var postName = Guid.NewGuid().ToString();
var postType = PostType.Loader;
var salary = 10;
var isActual = true;
var changeDate = DateTime.UtcNow.AddDays(-1);
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.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? postName, PostType postType, double salary, bool isActual, DateTime changeDate) =>
new(id, postName, postType, salary, isActual, changeDate);
}

View File

@ -0,0 +1,51 @@
using SoftBedContracts.DataModels;
using SoftBedContracts.Exceptions;
namespace SoftBedTests.DataModelsTests;
internal class SalaryDataModelTests
{
[Test]
public void WorkerIdIsEmptyTest()
{
var salary = CreateDataModel(null, DateTime.Now, 10);
Assert.That(() => salary.Validate(), Throws.TypeOf<ValidationException>());
salary = CreateDataModel(string.Empty, DateTime.Now, 10);
Assert.That(() => salary.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void WorkerIdIsNotGuidTest()
{
var salary = CreateDataModel("workerId", DateTime.Now, 10);
Assert.That(() => salary.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void PriceIsLessOrZeroTest()
{
var salary = CreateDataModel(Guid.NewGuid().ToString(), DateTime.Now, 0);
Assert.That(() => salary.Validate(), Throws.TypeOf<ValidationException>());
salary = CreateDataModel(Guid.NewGuid().ToString(), DateTime.Now, -10);
Assert.That(() => salary.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void AllFieldsIsCorrectTest()
{
var workerId = Guid.NewGuid().ToString();
var salaryDate = DateTime.Now.AddDays(-3).AddMinutes(-5);
var workerSalary = 10;
var salary = CreateDataModel(workerId, salaryDate, workerSalary);
Assert.That(() => salary.Validate(), Throws.Nothing);
Assert.Multiple(() =>
{
Assert.That(salary.WorkerId, Is.EqualTo(workerId));
Assert.That(salary.SalaryDate, Is.EqualTo(salaryDate));
Assert.That(salary.Salary, Is.EqualTo(workerSalary));
});
}
private static SalaryDataModel CreateDataModel(string? workerId, DateTime salaryDate, double workerSalary) =>
new(workerId, salaryDate, workerSalary);
}

View File

@ -0,0 +1,75 @@
using SoftBedContracts.DataModels;
using SoftBedContracts.Exceptions;
namespace SoftBedTests.DataModelsTests;
[TestFixture]
internal class WorkPieceDataModelTests
{
[Test]
public void IdIsNullOrEmptyTest()
{
var workPiece = CreateDataModel(null, "size", 10);
Assert.That(() => workPiece.Validate(), Throws.TypeOf<ValidationException>());
workPiece = CreateDataModel(string.Empty, "size", 10);
Assert.That(() => workPiece.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void IdIsNotGuidTest()
{
var workPiece = CreateDataModel("id", "size", 10);
Assert.That(() => workPiece.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void SizeIsNullOrEmptyTest()
{
var workPiece = CreateDataModel(Guid.NewGuid().ToString(), null, 10);
Assert.That(() => workPiece.Validate(), Throws.TypeOf<ValidationException>());
workPiece = CreateDataModel(Guid.NewGuid().ToString(), string.Empty, 10);
Assert.That(() => workPiece.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void SizeIsIncorrectTest()
{
var workPiece = CreateDataModel(Guid.NewGuid().ToString(), "1010", 10);
Assert.That(() => workPiece.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void PriceIsLessOrZeroTest()
{
var workPiece = CreateDataModel(Guid.NewGuid().ToString(), "size", 0);
Assert.That(() => workPiece.Validate(), Throws.TypeOf<ValidationException>());
workPiece = CreateDataModel(Guid.NewGuid().ToString(), "size", -10);
Assert.That(() => workPiece.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void ProductsIsNullOrEmptyTest()
{
var workPiece = CreateDataModel(Guid.NewGuid().ToString(), "size", 0);
Assert.That(() => workPiece.Validate(), Throws.TypeOf<ValidationException>());
workPiece = CreateDataModel(Guid.NewGuid().ToString(), "size", 0);
Assert.That(() => workPiece.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void AllFieldsIsCorrectTest()
{
var workPieceId = Guid.NewGuid().ToString();
var size = "10x10";
var weight = 10;
var workPiece = CreateDataModel(workPieceId, size, weight);
Assert.That(() => workPiece.Validate(), Throws.Nothing);
Assert.Multiple(() =>
{
Assert.That(workPiece.Id, Is.EqualTo(workPieceId));
Assert.That(workPiece.Size, Is.EqualTo(size));
Assert.That(workPiece.Weight, Is.EqualTo(weight));
});
}
private static WorkPieceDataModel CreateDataModel(string? id, string? size, double weight) =>
new(id, size, weight);
}

View File

@ -0,0 +1,88 @@
using SoftBedContracts.DataModels;
using SoftBedContracts.Exceptions;
namespace SoftBedTests.DataModelsTests;
[TestFixture]
internal class WorkerDataModelTests
{
[Test]
public void IdIsNullOrEmptyTest()
{
var worker = CreateDataModel(null, "fio", Guid.NewGuid().ToString(), DateTime.Now.AddYears(-18), DateTime.Now);
Assert.That(() => worker.Validate(), Throws.TypeOf<ValidationException>());
worker = CreateDataModel(string.Empty, "fio", Guid.NewGuid().ToString(), DateTime.Now.AddYears(-18), DateTime.Now);
Assert.That(() => worker.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void IdIsNotGuidTest()
{
var worker = CreateDataModel("id", "fio", Guid.NewGuid().ToString(), DateTime.Now.AddYears(-18), DateTime.Now);
Assert.That(() => worker.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void FIOIsNullOrEmptyTest()
{
var worker = CreateDataModel(Guid.NewGuid().ToString(), null, Guid.NewGuid().ToString(), DateTime.Now.AddYears(-18), DateTime.Now);
Assert.That(() => worker.Validate(), Throws.TypeOf<ValidationException>());
worker = CreateDataModel(Guid.NewGuid().ToString(), string.Empty, Guid.NewGuid().ToString(), DateTime.Now.AddYears(-18), DateTime.Now);
Assert.That(() => worker.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void PostIdIsNullOrEmptyTest()
{
var worker = CreateDataModel(Guid.NewGuid().ToString(), "fio", null, DateTime.Now.AddYears(-18), DateTime.Now);
Assert.That(() => worker.Validate(), Throws.TypeOf<ValidationException>());
worker = CreateDataModel(Guid.NewGuid().ToString(), "fio", string.Empty, DateTime.Now.AddYears(-18), DateTime.Now);
Assert.That(() => worker.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void PostIdIsNotGuidTest()
{
var worker = CreateDataModel(Guid.NewGuid().ToString(), "fio", "postId", DateTime.Now.AddYears(-18), DateTime.Now);
Assert.That(() => worker.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void BirthDateIsNotCorrectTest()
{
var worker = CreateDataModel(Guid.NewGuid().ToString(), "fio", Guid.NewGuid().ToString(), DateTime.Now.AddYears(-18).AddDays(1), DateTime.Now);
Assert.That(() => worker.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void BirthDateAndEmploymentDateIsNotCorrectTest()
{
var worker = CreateDataModel(Guid.NewGuid().ToString(), "fio", Guid.NewGuid().ToString(), DateTime.Now.AddYears(-18), DateTime.Now.AddYears(-18).AddDays(-1));
Assert.That(() => worker.Validate(), Throws.TypeOf<ValidationException>());
worker = CreateDataModel(Guid.NewGuid().ToString(), "fio", Guid.NewGuid().ToString(), DateTime.Now.AddYears(-18), DateTime.Now.AddYears(-16));
Assert.That(() => worker.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void AllFieldsIsCorrectTest()
{
var workerId = Guid.NewGuid().ToString();
var fio = "fio";
var postId = Guid.NewGuid().ToString();
var birthDate = DateTime.Now.AddYears(-18).AddDays(-1);
var employmentDate = DateTime.Now;
var worker = CreateDataModel(workerId, fio, postId, birthDate, employmentDate);
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));
});
}
private static WorkerDataModel CreateDataModel(string? id, string? fio, string? postId, DateTime birthDate, DateTime employmentDate) =>
new(id, fio, postId, birthDate, employmentDate);
}

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="..\SoftBedContracts\SoftBedContracts.csproj" />
</ItemGroup>
<ItemGroup>
<Using Include="NUnit.Framework" />
</ItemGroup>
</Project>

View File

@ -3,7 +3,9 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17 # Visual Studio Version 17
VisualStudioVersion = 17.9.34728.123 VisualStudioVersion = 17.9.34728.123
MinimumVisualStudioVersion = 10.0.40219.1 MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SoftBedContracts", "SoftBedContracts\SoftBedContracts.csproj", "{B0AF813F-CC99-4892-AA65-4CDB225B7AA3}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SoftBedContracts", "SoftBedContracts\SoftBedContracts.csproj", "{B0AF813F-CC99-4892-AA65-4CDB225B7AA3}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SoftBedTests", "SoftBedTests\SoftBedTests.csproj", "{0E56A9DB-177A-4114-B437-DE0F6C9711F4}"
EndProject EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
@ -15,6 +17,10 @@ Global
{B0AF813F-CC99-4892-AA65-4CDB225B7AA3}.Debug|Any CPU.Build.0 = Debug|Any CPU {B0AF813F-CC99-4892-AA65-4CDB225B7AA3}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B0AF813F-CC99-4892-AA65-4CDB225B7AA3}.Release|Any CPU.ActiveCfg = Release|Any CPU {B0AF813F-CC99-4892-AA65-4CDB225B7AA3}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B0AF813F-CC99-4892-AA65-4CDB225B7AA3}.Release|Any CPU.Build.0 = Release|Any CPU {B0AF813F-CC99-4892-AA65-4CDB225B7AA3}.Release|Any CPU.Build.0 = Release|Any CPU
{0E56A9DB-177A-4114-B437-DE0F6C9711F4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{0E56A9DB-177A-4114-B437-DE0F6C9711F4}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0E56A9DB-177A-4114-B437-DE0F6C9711F4}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0E56A9DB-177A-4114-B437-DE0F6C9711F4}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection EndGlobalSection
GlobalSection(SolutionProperties) = preSolution GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE HideSolutionNode = FALSE