Лаба 1 (Тесты + Модельки)

This commit is contained in:
5_fG 2025-02-12 13:29:42 +04:00
parent 8c9b8971d3
commit 61ede4b402
26 changed files with 1109 additions and 0 deletions

View File

@ -0,0 +1,27 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<LangVersion>latest</LangVersion>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="coverlet.collector" Version="6.0.2" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
<PackageReference Include="NUnit" Version="4.2.2" />
<PackageReference Include="NUnit.Analyzers" Version="4.4.0" />
<PackageReference Include="NUnit3TestAdapter" Version="4.6.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\ChamomileСontracts\ChamomileСontracts.csproj" />
</ItemGroup>
<ItemGroup>
<Using Include="NUnit.Framework" />
</ItemGroup>
</Project>

View File

@ -0,0 +1,68 @@
using ChamomileСontracts.DataModels;
using ChamomileСontracts.Exceptions;
namespace ChamomileTests.DataModelsTests;
[TestFixture]
internal class BuyerDataModelTests
{
[Test]
public void IdIsNullOrEmptyTest()
{
var buyer = CreateDataModel(null, "fio", "number");
Assert.That(() => buyer.Validate(), Throws.TypeOf<ValidationException>());
buyer = CreateDataModel(string.Empty, "fio", "number");
Assert.That(() => buyer.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void IdIsNotGuidTest()
{
var buyer = CreateDataModel("id", "fio", "number");
Assert.That(() => buyer.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void FIOIsNullOrEmptyTest()
{
var buyer = CreateDataModel(Guid.NewGuid().ToString(), null, "number");
Assert.That(() => buyer.Validate(), Throws.TypeOf<ValidationException>());
buyer = CreateDataModel(Guid.NewGuid().ToString(), string.Empty, "number");
Assert.That(() => buyer.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void PhoneNumberIsNullOrEmptyTest()
{
var buyer = CreateDataModel(Guid.NewGuid().ToString(), "fio", null);
Assert.That(() => buyer.Validate(), Throws.TypeOf<ValidationException>());
buyer = CreateDataModel(Guid.NewGuid().ToString(), "fio", string.Empty);
Assert.That(() => buyer.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void PhoneNumberIsIncorrectTest()
{
var buyer = CreateDataModel(Guid.NewGuid().ToString(), "fio", "777");
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 buyer = CreateDataModel(buyerId, fio, phoneNumber);
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));
});
}
private static BuyerDataModel CreateDataModel(string? id, string? fio, string? phoneNumber) =>
new(id, fio, phoneNumber);
}

View File

@ -0,0 +1,91 @@
using ChamomileСontracts.DataModels;
using ChamomileСontracts.Enums;
using ChamomileСontracts.Exceptions;
namespace ChamomileTests.DataModelsTests;
[TestFixture]
internal class ComponentDataModelTests
{
[Test]
public void IdIsNullOrEmptyTest()
{
var component = CreateDataModel(null, "name", ComponentType.Flower, Guid.NewGuid().ToString(), 10, false);
Assert.That(() => component.Validate(), Throws.TypeOf<ValidationException>());
component = CreateDataModel(string.Empty, "name", ComponentType.Flower, Guid.NewGuid().ToString(), 10, false);
Assert.That(() => component.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void IdIsNotGuidTest()
{
var component = CreateDataModel("id", "name", ComponentType.Flower, Guid.NewGuid().ToString(), 10, false);
Assert.That(() => component.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void ComponentNameIsEmptyTest()
{
var component = CreateDataModel(Guid.NewGuid().ToString(), null, ComponentType.Flower, Guid.NewGuid().ToString(), 10, false);
Assert.That(() => component.Validate(), Throws.TypeOf<ValidationException>());
component = CreateDataModel(Guid.NewGuid().ToString(), string.Empty, ComponentType.Flower, Guid.NewGuid().ToString(), 10, false);
Assert.That(() => component.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void ComponentTypeIsNoneTest()
{
var component = CreateDataModel(Guid.NewGuid().ToString(), null, ComponentType.None, Guid.NewGuid().ToString(), 10, false);
Assert.That(() => component.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void componentSupplierIdIsNullOrEmptyTest()
{
var component = CreateDataModel(Guid.NewGuid().ToString(), "name", ComponentType.Flower, null, 10, false);
Assert.That(() => component.Validate(), Throws.TypeOf<ValidationException>());
component = CreateDataModel(Guid.NewGuid().ToString(), "name", ComponentType.Flower, string.Empty, 10, false);
Assert.That(() => component.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void componentSupplierIdIsNotGuidTest()
{
var component = CreateDataModel(Guid.NewGuid().ToString(), "name", ComponentType.Flower, "manufacturerId", 10, false);
Assert.That(() => component.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void PriceIsLessOrZeroTest()
{
var component = CreateDataModel(Guid.NewGuid().ToString(), "name", ComponentType.Flower, Guid.NewGuid().ToString(), 0, false);
Assert.That(() => component.Validate(), Throws.TypeOf<ValidationException>());
component = CreateDataModel(Guid.NewGuid().ToString(), "name", ComponentType.Flower, Guid.NewGuid().ToString(), -10, false);
Assert.That(() => component.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void AllFieldsIsCorrectTest()
{
var componentId = Guid.NewGuid().ToString();
var componentName = "name";
var componentType = ComponentType.Flower;
var componentSupplierId = Guid.NewGuid().ToString();
var componentPrice = 10;
var componentIsDelete = false;
var component = CreateDataModel(componentId, componentName, componentType, componentSupplierId, 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.SupplierId, Is.EqualTo(componentSupplierId));
Assert.That(component.Price, Is.EqualTo(componentPrice));
Assert.That(component.IsDeleted, Is.EqualTo(componentIsDelete));
});
}
private static ComponentDataModel CreateDataModel(string? id, string? componentName, ComponentType componentType, string? supplierId, double price, bool isDeleted) =>
new(id, componentName, componentType, supplierId, price, isDeleted);
}

View File

@ -0,0 +1,52 @@
using ChamomileСontracts.DataModels;
using ChamomileСontracts.Exceptions;
namespace ChamomileTests.DataModelsTests;
[TestFixture]
internal class ComponentHistoryDataModelTests
{
[Test]
public void ComponentIdIsNullOrEmptyTest()
{
var component = CreateDataModel(null, 10);
Assert.That(() => component.Validate(), Throws.TypeOf<ValidationException>());
component = CreateDataModel(string.Empty, 10);
Assert.That(() => component.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void ComponentIdIsNotGuidTest()
{
var component = CreateDataModel("id", 10);
Assert.That(() => component.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void OldPriceIsLessOrZeroTest()
{
var component = CreateDataModel(Guid.NewGuid().ToString(), 0);
Assert.That(() => component.Validate(), Throws.TypeOf<ValidationException>());
component = CreateDataModel(Guid.NewGuid().ToString(), -10);
Assert.That(() => component.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void AllFieldsIsCorrectTest()
{
var componentId = Guid.NewGuid().ToString();
var oldPrice = 10;
var componentHistory = CreateDataModel(componentId, oldPrice);
Assert.That(() => componentHistory.Validate(), Throws.Nothing);
Assert.Multiple(() =>
{
Assert.That(componentHistory.ComponentId, Is.EqualTo(componentId));
Assert.That(componentHistory.OldPrice, Is.EqualTo(oldPrice));
Assert.That(componentHistory.ChangeDate, Is.LessThan(DateTime.UtcNow));
Assert.That(componentHistory.ChangeDate, Is.GreaterThan(DateTime.UtcNow.AddMinutes(-1)));
});
}
private static ComponentHistoryDataModel CreateDataModel(string? componentId, double oldPrice) =>
new(componentId, oldPrice);
}

View File

@ -0,0 +1,48 @@
using ChamomileСontracts.DataModels;
using ChamomileСontracts.Exceptions;
namespace ChamomileTests.DataModelsTests;
[TestFixture]
internal class DiscountDataModelTests
{
[Test]
public void IdIsNullOrEmptyTest()
{
var discount = CreateDataModel(null, 5, true);
Assert.That(() => discount.Validate(), Throws.TypeOf<ValidationException>());
discount = CreateDataModel(string.Empty, 5, true);
Assert.That(() => discount.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void IdIsNotGuidTest()
{
var discount = CreateDataModel("buyerId", 5, true);
Assert.That(() => discount.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void DiscountSizeIsNegativeTest()
{
var discount = CreateDataModel("buyerId", -5, true);
Assert.That(() => discount.Validate(), Throws.TypeOf<ValidationException>());
}
public void AllFieldsIsCorrectTest()
{
var buyerId = Guid.NewGuid().ToString();
var discountSize = 5;
var isActual = false;
var discount = CreateDataModel(buyerId, discountSize, isActual);
Assert.That(() => discount.Validate(), Throws.Nothing);
Assert.Multiple(() =>
{
Assert.That(discount.BuyerId, Is.EqualTo(buyerId));
Assert.That(discount.DiscountSize, Is.EqualTo(discountSize));
});
}
private static DiscountDataModel CreateDataModel(string? buyerId, double discountSize,bool isActual) =>
new(buyerId, discountSize, isActual);
}

View File

@ -0,0 +1,92 @@
using ChamomileСontracts.DataModels;
using ChamomileСontracts.Enums;
using ChamomileСontracts.Exceptions;
namespace ChamomileTests.DataModelsTests;
[TestFixture]
internal class PostDataModelTests
{
[Test]
public void IdIsNullOrEmptyTest()
{
var post = CreateDataModel(null, Guid.NewGuid().ToString(), "name", PostType.Assistant, 10, true, DateTime.UtcNow);
Assert.That(() => post.Validate(), Throws.TypeOf<ValidationException>());
post = CreateDataModel(string.Empty, Guid.NewGuid().ToString(), "name", PostType.Assistant, 10, true, DateTime.UtcNow);
Assert.That(() => post.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void IdIsNotGuidTest()
{
var post = CreateDataModel("id", Guid.NewGuid().ToString(), "name", PostType.Assistant, 10, true, DateTime.UtcNow);
Assert.That(() => post.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void PostIdIsNullEmptyTest()
{
var post = CreateDataModel(Guid.NewGuid().ToString(), null, "name", PostType.Assistant, 10, true, DateTime.UtcNow);
Assert.That(() => post.Validate(), Throws.TypeOf<ValidationException>());
post = CreateDataModel(Guid.NewGuid().ToString(), string.Empty, "name", PostType.Assistant, 10, true, DateTime.UtcNow);
Assert.That(() => post.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void PostIdIsNotGuidTest()
{
var post = CreateDataModel(Guid.NewGuid().ToString(), "postId", "name", PostType.Assistant, 10, true, DateTime.UtcNow);
Assert.That(() => post.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void PostNameIsEmptyTest()
{
var manufacturer = CreateDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), null, PostType.Assistant, 10, true, DateTime.UtcNow);
Assert.That(() => manufacturer.Validate(), Throws.TypeOf<ValidationException>());
manufacturer = CreateDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), string.Empty, PostType.Assistant, 10, true, DateTime.UtcNow);
Assert.That(() => manufacturer.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void PostTypeIsNoneTest()
{
var post = CreateDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), "name", PostType.None, 10, true, DateTime.UtcNow);
Assert.That(() => post.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void SalaryIsLessOrZeroTest()
{
var post = CreateDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), "name", PostType.Assistant, 0, true, DateTime.UtcNow);
Assert.That(() => post.Validate(), Throws.TypeOf<ValidationException>());
post = CreateDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), "name", PostType.Assistant, -10, true, DateTime.UtcNow);
Assert.That(() => post.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void AllFieldsIsCorrectTest()
{
var postId = Guid.NewGuid().ToString();
var postPostId = Guid.NewGuid().ToString();
var postName = "name";
var postType = PostType.Assistant;
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);
}

View File

@ -0,0 +1,68 @@
using ChamomileСontracts.DataModels;
using ChamomileСontracts.Exceptions;
namespace ChamomileTests.DataModelsTests;
[TestFixture]
internal class SaleComponentDataModelTests
{
[Test]
public void SaleIdIsNullOrEmptyTest()
{
var saleComponent = CreateDataModel(null, Guid.NewGuid().ToString(), 10);
Assert.That(() => saleComponent.Validate(), Throws.TypeOf<ValidationException>());
saleComponent = CreateDataModel(string.Empty, Guid.NewGuid().ToString(), 10);
Assert.That(() => saleComponent.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void SaleIdIsNotGuidTest()
{
var saleComponent = CreateDataModel("saleId", Guid.NewGuid().ToString(), 10);
Assert.That(() => saleComponent.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void ComponentIdIsNullOrEmptyTest()
{
var saleComponent = CreateDataModel(Guid.NewGuid().ToString(), null, 10);
Assert.That(() => saleComponent.Validate(), Throws.TypeOf<ValidationException>());
saleComponent = CreateDataModel(string.Empty, Guid.NewGuid().ToString(), 10);
Assert.That(() => saleComponent.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void ComponentIdIsNotGuidTest()
{
var saleComponent = CreateDataModel(Guid.NewGuid().ToString(), "componentId", 10);
Assert.That(() => saleComponent.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void CountIsLessOrZeroTest()
{
var saleComponent = CreateDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 0);
Assert.That(() => saleComponent.Validate(), Throws.TypeOf<ValidationException>());
saleComponent = CreateDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), -10);
Assert.That(() => saleComponent.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void AllFieldsIsCorrectTest()
{
var saleId = Guid.NewGuid().ToString();
var componentId = Guid.NewGuid().ToString();
var count = 10;
var saleComponent = CreateDataModel(saleId, componentId, count);
Assert.That(() => saleComponent.Validate(), Throws.Nothing);
Assert.Multiple(() =>
{
Assert.That(saleComponent.SaleId, Is.EqualTo(saleId));
Assert.That(saleComponent.ComponentId, Is.EqualTo(componentId));
Assert.That(saleComponent.Count, Is.EqualTo(count));
});
}
private static SaleComponentDataModel CreateDataModel(string? saleId, string? componentId, int count) =>
new(saleId, componentId, count);
}

View File

@ -0,0 +1,112 @@
using ChamomileСontracts.DataModels;
using ChamomileСontracts.Enums;
using ChamomileСontracts.Exceptions;
namespace ChamomileTests.DataModelsTests;
[TestFixture]
internal class SaleDataModelTests
{
[Test]
public void IdIsNullOrEmptyTest()
{
var sale = CreateDataModel(null, Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 10, 5, AssemblyType.Сomposition, false, CreateSubDataModel());
Assert.That(() => sale.Validate(), Throws.TypeOf<ValidationException>());
sale = CreateDataModel(string.Empty, Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 10, 5, AssemblyType.Сomposition, false, CreateSubDataModel());
Assert.That(() => sale.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void IdIsNotGuidTest()
{
var sale = CreateDataModel("id", Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 10, 5,AssemblyType.Сomposition, false, CreateSubDataModel());
Assert.That(() => sale.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void WorkerIdIsNullOrEmptyTest()
{
var sale = CreateDataModel(Guid.NewGuid().ToString(), null, null, 10, 5, AssemblyType.Сomposition, false, CreateSubDataModel());
Assert.That(() => sale.Validate(), Throws.TypeOf<ValidationException>());
sale = CreateDataModel(Guid.NewGuid().ToString(), string.Empty, Guid.NewGuid().ToString(), 10,5, AssemblyType.Сomposition, false, CreateSubDataModel());
Assert.That(() => sale.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void WorkerIdIsNotGuidTest()
{
var sale = CreateDataModel(Guid.NewGuid().ToString(), "workerId", Guid.NewGuid().ToString(), 10, 5, AssemblyType.Сomposition, false, CreateSubDataModel());
Assert.That(() => sale.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void BuyerIdIsNotGuidTest()
{
var sale = CreateDataModel("buyerId", Guid.NewGuid().ToString(), "buyerId", 10, 5, AssemblyType.Сomposition, false, CreateSubDataModel());
Assert.That(() => sale.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void SumIsLessOrZeroTest()
{
var sale = CreateDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 0, 5, AssemblyType.Сomposition, false, CreateSubDataModel());
Assert.That(() => sale.Validate(), Throws.TypeOf<ValidationException>());
sale = CreateDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), -10, 5, AssemblyType.Сomposition, false, CreateSubDataModel());
Assert.That(() => sale.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void DiscountIsNegative()
{
var sale = CreateDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 0, -5, AssemblyType.Сomposition, false, CreateSubDataModel());
Assert.That(() => sale.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void ComponentsIsNullOrEmptyTest()
{
var sale = CreateDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 10, 5, AssemblyType.Сomposition, false, null);
Assert.That(() => sale.Validate(), Throws.TypeOf<ValidationException>());
sale = CreateDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 10, 5, AssemblyType.Сomposition, false, []);
Assert.That(() => sale.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void AssemblyTypeIsNoneTest()
{
var sale = CreateDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 10, 5, AssemblyType.None, false, CreateSubDataModel());
Assert.That(() => sale.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void AllFieldsIsCorrectTest()
{
var saleId = Guid.NewGuid().ToString();
var workerId = Guid.NewGuid().ToString();
var buyerId = Guid.NewGuid().ToString();
var sum = 10;
var discount = 5;
var assemblyType = AssemblyType.Bouquet;
var isCancel = true;
var components = CreateSubDataModel();
var sale = CreateDataModel(saleId, workerId, buyerId, sum, discount, assemblyType, isCancel, components);
Assert.That(() => sale.Validate(), Throws.Nothing);
Assert.Multiple(() =>
{
Assert.That(sale.Id, Is.EqualTo(saleId));
Assert.That(sale.WorkerId, Is.EqualTo(workerId));
Assert.That(sale.BuyerId, Is.EqualTo(buyerId));
Assert.That(sale.Sum, Is.EqualTo(sum));
Assert.That(sale.Discount, Is.EqualTo(discount));
Assert.That(sale.AssemblyType, Is.EqualTo(assemblyType));
Assert.That(sale.Discount, Is.EqualTo(discount));
Assert.That(sale.IsCancel, Is.EqualTo(isCancel));
Assert.That(sale.Components, Is.EquivalentTo(components));
});
}
private static SaleDataModel CreateDataModel(string? id, string? workerId, string? buyerId, double sum, double discount, AssemblyType assemblyType, bool isCancel, List<SaleComponentDataModel> components) =>
new(id, workerId, buyerId, sum, discount, assemblyType, isCancel, components);
private static List<SaleComponentDataModel> CreateSubDataModel()
=> [new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 1)];
}

View File

@ -0,0 +1,50 @@
using ChamomileСontracts.DataModels;
using ChamomileСontracts.Exceptions;
namespace ChamomileTests.DataModelsTests;
[TestFixture]
internal class SupplierDataModelTests
{
[Test]
public void IdIsNullOrEmptyTest()
{
var supplier = CreateDataModel(null, "name");
Assert.That(() => supplier.Validate(), Throws.TypeOf<ValidationException>());
supplier = CreateDataModel(string.Empty, "name");
Assert.That(() => supplier.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void IdIsNotGuidTest()
{
var supplier = CreateDataModel("id", "name");
Assert.That(() => supplier.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void SupplierNameIsNullOrEmptyTest()
{
var supplier = CreateDataModel(Guid.NewGuid().ToString(), null);
Assert.That(() => supplier.Validate(), Throws.TypeOf<ValidationException>());
supplier = CreateDataModel(Guid.NewGuid().ToString(), string.Empty);
Assert.That(() => supplier.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void AllFieldsIsCorrectTest()
{
var supplierId = Guid.NewGuid().ToString();
var supplierName = "name";
var supplier = CreateDataModel(supplierId, supplierName);
Assert.That(() => supplier.Validate(), Throws.Nothing);
Assert.Multiple(() =>
{
Assert.That(supplier.Id, Is.EqualTo(supplierId));
Assert.That(supplier.SupplierName, Is.EqualTo(supplierName));
});
}
private static SupplierDataModel CreateDataModel(string? id, string? supplierName, string? prevSupplierName = null, string? prevPrevSupplierName = null) =>
new(id, supplierName, prevSupplierName, prevPrevSupplierName);
}

View File

@ -0,0 +1,88 @@
using ChamomileСontracts.DataModels;
using ChamomileСontracts.Exceptions;
namespace ChamomileTests.DataModelsTests;
[TestFixture]
internal class WorkerDataModelTests
{
[Test]
public void IdIsNullOrEmptyTest()
{
var worker = CreateDataModel(null, "fio", Guid.NewGuid().ToString(), DateTime.Now.AddYears(-18), DateTime.Now, false);
Assert.That(() => worker.Validate(), Throws.TypeOf<ValidationException>());
worker = CreateDataModel(string.Empty, "fio", Guid.NewGuid().ToString(), DateTime.Now.AddYears(-18), DateTime.Now, false);
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, false);
Assert.That(() => worker.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void FioIsNotFullNameEmptyTest()
{
var worker = CreateDataModel(Guid.NewGuid().ToString(), "аап пав", Guid.NewGuid().ToString(), DateTime.Now.AddYears(-18), DateTime.Now, false);
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, false);
Assert.That(() => worker.Validate(), Throws.TypeOf<ValidationException>());
worker = CreateDataModel(Guid.NewGuid().ToString(), "fio", string.Empty, DateTime.Now.AddYears(-18), DateTime.Now, false);
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, false);
Assert.That(() => worker.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void BirthDateIsNotCorrectTest()
{
var worker = CreateDataModel(Guid.NewGuid().ToString(), "fio", Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(1), DateTime.Now, false);
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), false);
Assert.That(() => worker.Validate(), Throws.TypeOf<ValidationException>());
worker = CreateDataModel(Guid.NewGuid().ToString(), "fio", Guid.NewGuid().ToString(), DateTime.Now.AddYears(-18), DateTime.Now.AddYears(-16), false);
Assert.That(() => worker.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void AllFieldsIsCorrectTest()
{
var workerId = Guid.NewGuid().ToString();
var fio = "Фамилия Имя Отчество";
var postId = Guid.NewGuid().ToString();
var birthDate = DateTime.Now.AddYears(-16).AddDays(-1);
var employmentDate = DateTime.Now;
var isDelete = false;
var worker = CreateDataModel(workerId, fio, postId, birthDate, employmentDate, isDelete);
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));
Assert.That(worker.IsDeleted, Is.EqualTo(isDelete));
});
}
private static WorkerDataModel CreateDataModel(string? id, string? fio, string? postId, DateTime birthDate, DateTime employmentDate, bool isDeleted) =>
new(id, fio, postId, birthDate, employmentDate, isDeleted);
}

View File

@ -5,6 +5,8 @@ VisualStudioVersion = 17.12.35707.178 d17.12
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ChamomileСontracts", "ChamomileСontracts\ChamomileСontracts.csproj", "{308A7586-A19E-4565-B0DF-7CC73FA7790C}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ChamomileTests", "ChamomileTests\ChamomileTests.csproj", "{4BB61363-9D2A-4A9D-90D0-8162B0B40DC5}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@ -15,6 +17,10 @@ Global
{308A7586-A19E-4565-B0DF-7CC73FA7790C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{308A7586-A19E-4565-B0DF-7CC73FA7790C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{308A7586-A19E-4565-B0DF-7CC73FA7790C}.Release|Any CPU.Build.0 = Release|Any CPU
{4BB61363-9D2A-4A9D-90D0-8162B0B40DC5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{4BB61363-9D2A-4A9D-90D0-8162B0B40DC5}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4BB61363-9D2A-4A9D-90D0-8162B0B40DC5}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4BB61363-9D2A-4A9D-90D0-8162B0B40DC5}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE

View File

@ -0,0 +1,34 @@
using ChamomileСontracts.Exceptions;
using ChamomileСontracts.Extensions;
using ChamomileСontracts.Infrastructure;
using System.Text.RegularExpressions;
namespace ChamomileСontracts.DataModels;
public class BuyerDataModel(string id, string fio, string phoneNumber) : IValidation
{
public string Id { get; private set; } = id; // Чтобы не переводить в GUID и UUID представляем в виде строки
public string FIO { get; private set; } = fio;
public string PhoneNumber { get; private set; } = phoneNumber;
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");
}
}

View File

@ -0,0 +1,45 @@
using ChamomileСontracts.Enums;
using ChamomileСontracts.Exceptions;
using ChamomileСontracts.Extensions;
using ChamomileСontracts.Infrastructure;
namespace ChamomileСontracts.DataModels;
public class ComponentDataModel(string id, string componentName, ComponentType componentType, string supplierId, 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 string SupplierId { get; private set; } = supplierId;
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 (SupplierId.IsEmpty())
throw new ValidationException("Field SupplierId is empty");
if (!SupplierId.IsGuid())
throw new ValidationException("The value in the field SupplierId 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,27 @@
using ChamomileСontracts.Exceptions;
using ChamomileСontracts.Extensions;
using ChamomileСontracts.Infrastructure;
namespace ChamomileСontracts.DataModels;
public class ComponentHistoryDataModel(string componentId, double oldPrice) : IValidation
{
public string ComponentId { get; private set; } = componentId;
public double OldPrice { get; private set; } = oldPrice;
//дата изменения
public DateTime ChangeDate { get; private set; } = DateTime.UtcNow;
public void Validate()
{
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 (OldPrice <= 0)
throw new ValidationException("Field OldPrice is less than or equal to 0");
}
}

View File

@ -0,0 +1,28 @@
using ChamomileСontracts.Enums;
using ChamomileСontracts.Exceptions;
using ChamomileСontracts.Extensions;
using ChamomileСontracts.Infrastructure;
namespace ChamomileСontracts.DataModels;
public class DiscountDataModel(string buyerId, double discountSize, bool isActual) : IValidation
{
public string BuyerId { get; private set; } = buyerId;
public double DiscountSize { get; private set; } = discountSize;
//признак актуальности
public bool IsActual { get; private set; } = isActual;
public void Validate()
{
if (BuyerId.IsEmpty())
throw new ValidationException("Field WorkerId is empty");
if (!BuyerId.IsGuid())
throw new ValidationException("The value in the field WorkerId is not a unique identifier");
if (DiscountSize < 0)
throw new ValidationException("Field DiscountSize cannot be negative");
}
}

View File

@ -0,0 +1,50 @@
using ChamomileСontracts.Enums;
using ChamomileСontracts.Exceptions;
using ChamomileСontracts.Extensions;
using ChamomileСontracts.Infrastructure;
namespace ChamomileСontracts.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,32 @@
using ChamomileСontracts.Exceptions;
using ChamomileСontracts.Extensions;
using ChamomileСontracts.Infrastructure;
namespace ChamomileСontracts.DataModels;
public class SaleComponentDataModel(string saleId, string componentId, int count) : IValidation
{
public string SaleId { get; private set; } = saleId;
public string ComponentId { get; private set; } = componentId;
public int Count { get; private set; } = count;
public void Validate()
{
if (SaleId.IsEmpty())
throw new ValidationException("Field SaleId is empty");
if (!SaleId.IsGuid())
throw new ValidationException("The value in the field SaleId 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");
}
}

View File

@ -0,0 +1,59 @@
using ChamomileСontracts.Enums;
using ChamomileСontracts.Exceptions;
using ChamomileСontracts.Extensions;
using ChamomileСontracts.Infrastructure;
namespace ChamomileСontracts.DataModels;
public class SaleDataModel(string id, string workerId, string? buyerId, double sum, double discount, AssemblyType assemblyType, bool isCancel, List<SaleComponentDataModel> components) : IValidation
{
public string Id { get; private set; } = id;
public string WorkerId { get; private set; } = workerId;
public string? BuyerId { get; private set; } = buyerId;
public double Sum { get; private set; } = sum;
public double Discount { get; private set; } = discount;
public AssemblyType AssemblyType { get; private set; } = assemblyType;
public DateTime SaleDate { get; private set; } = DateTime.UtcNow;
public bool IsCancel { get; private set; } = isCancel;
public List<SaleComponentDataModel> 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 (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 (Discount < 0)
throw new ValidationException("Field Discount cannot be negative");
if ((Components?.Count ?? 0) == 0)
throw new ValidationException("The sale must include components");
if (AssemblyType == AssemblyType.None)
throw new ValidationException("Field AssemblyType is empty");
}
}

View File

@ -0,0 +1,28 @@
using ChamomileСontracts.Exceptions;
using ChamomileСontracts.Extensions;
using ChamomileСontracts.Infrastructure;
namespace ChamomileСontracts.DataModels;
public class SupplierDataModel(string id, string supplierName, string? prevSupplierName, string? prevPrevSupplierName) : IValidation
{
public string Id { get; private set; } = id;
public string SupplierName { get; private set; } = supplierName;
public string? PrevSupplierName { get; private set; } = prevSupplierName;
public string? PrevPrevSupplierName { get; private set; } = prevPrevSupplierName;
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 (SupplierName.IsEmpty())
throw new ValidationException("Field SupplierName is empty");
}
}

View File

@ -0,0 +1,50 @@
using ChamomileСontracts.Exceptions;
using ChamomileСontracts.Extensions;
using ChamomileСontracts.Infrastructure;
using System.Text.RegularExpressions;
namespace ChamomileСontracts.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 (!Regex.IsMatch(FIO, @"^([А-ЯЁ][а-яё]+)\s+([А-ЯЁ][а-яё]+)\s+([А-ЯЁ][а-яё]+)$"))
throw new ValidationException("Field FIO is not a full name");
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) // EmploymentDate.Year - BirthDate.Year
throw new ValidationException($"Minors cannot be hired (EmploymentDate - {EmploymentDate.ToShortDateString()}, BirthDate - {BirthDate.ToShortDateString()})");
}
}

View File

@ -0,0 +1,9 @@
namespace ChamomileСontracts.Enums;
[Flags]
public enum AssemblyType // тип сборки
{
None = 0,
Bouquet = 1, //букет
Сomposition = 2 // композиция
}

View File

@ -0,0 +1,9 @@
namespace ChamomileСontracts.Enums;
public enum ComponentType
{
None = 0,
Flower = 1,
Decor = 2,
СareСhemistry = 3 //химимя для ухода за цветами
}

View File

@ -0,0 +1,9 @@
namespace ChamomileСontracts.Enums;
public enum PostType
{
None = 0,
Administrator = 1,
Florist = 2,
Assistant = 3 //подсобный рабочий
}

View File

@ -0,0 +1,5 @@
namespace ChamomileСontracts.Exceptions;
public class ValidationException(string massege) : Exception(massege)
{
}

View File

@ -0,0 +1,16 @@
namespace ChamomileСontracts.Extensions;
public static class StringExtensions
{
//строка пуста
public static bool IsEmpty(this string str)
{
return string.IsNullOrWhiteSpace(str);
}
//строка представляет собой Guid выражение (128-битное число, используемое для уникальной идентификации информации в компьютерных системах)
public static bool IsGuid(this string str)
{
return Guid.TryParse(str, out _);
}
}

View File

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