иииууууу

This commit is contained in:
Anitonchik 2025-02-18 14:19:03 +04:00
parent 094c7f18f6
commit 9177ab1014
6 changed files with 292 additions and 0 deletions

View File

@ -0,0 +1,32 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WildPlum.Extensions;
using WildPlum.Infrastructure;
namespace WildPlum.DataModels;
public class SupplyDataModel(string id, string warehouseId, DateTime date, List<SupplyProductDataModel> products) : IValidation
{
public string Id { get; private set; } = id;
public string WarehouseId { get; private set; } = warehouseId;
public DateTime Date { get; private set; } = date;
public List<SupplyProductDataModel> Products { get; private set; } = products;
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 (WarehouseId.IsEmpty())
throw new ValidationException("Field WarehouseId is empty");
if (!WarehouseId.IsGuid())
throw new ValidationException("The value in the field WarehouseId is not a unique identifier");
if ((Products?.Count ?? 0) == 0)
throw new ValidationException("The sale must include products");
}
}

View File

@ -0,0 +1,30 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WildPlum.Extensions;
using WildPlum.Infrastructure;
namespace WildPlum.DataModels;
public class SupplyProductDataModel(string supplyId, string productId, int count) : IValidation
{
public string SupplyId { get; private set; } = supplyId;
public string ProductId { get; private set; } = productId;
public int Count { get; private set; } = count;
public void Validate()
{
if (SupplyId.IsEmpty())
throw new ValidationException("Field Id is empty");
if (!SupplyId.IsGuid())
throw new ValidationException("The value in the field Id is not a unique identifier");
if (ProductId.IsEmpty())
throw new ValidationException("Field ProductId is empty");
if (!ProductId.IsGuid())
throw new ValidationException("The value in the field ProductId 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,30 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
using WildPlum.Extensions;
using WildPlum.Infrastructure;
namespace WildPlum.DataModels;
public class WarehouseDataModel(string id, string name, string address) : IValidation
{
public string Id { get; private set; } = id;
public string Name { get; private set; } = name;
public string Address { get; private set; } = address;
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 (Name.IsEmpty())
throw new ValidationException("Field Name is empty");
if (Address.IsEmpty())
throw new ValidationException("Field Adress is empty");
}
}

View File

@ -0,0 +1,78 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WildPlum.DataModels;
namespace WildPlumTests.DataModelsTests;
[TestFixture]
internal class SupplyDataModelTests
{
[Test]
public void IdIsNullOrEmptyTest()
{
var supply = CreateDataModel(null, Guid.NewGuid().ToString(), DateTime.UtcNow, CreateSubDataModel());
Assert.That(() => supply.Validate(), Throws.TypeOf<ValidationException>());
supply = CreateDataModel(string.Empty, Guid.NewGuid().ToString(), DateTime.UtcNow, CreateSubDataModel());
Assert.That(() => supply.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void IdIsNotGuidTest()
{
var supply = CreateDataModel("id", Guid.NewGuid().ToString(), DateTime.UtcNow, CreateSubDataModel());
Assert.That(() => supply.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void WarehouseIdIsNullOrEmptyTest()
{
var supply = CreateDataModel(Guid.NewGuid().ToString(), null, DateTime.UtcNow, CreateSubDataModel());
Assert.That(() => supply.Validate(), Throws.TypeOf<ValidationException>());
supply = CreateDataModel(Guid.NewGuid().ToString(), string.Empty, DateTime.UtcNow, CreateSubDataModel());
Assert.That(() => supply.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void WarehouseIdIsNotGuidTest()
{
var supply = CreateDataModel(Guid.NewGuid().ToString(), "warehouseId", DateTime.UtcNow, CreateSubDataModel());
Assert.That(() => supply.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void ProductsIsNullOrEmptyTest()
{
var supply = CreateDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), DateTime.UtcNow, null);
Assert.That(() => supply.Validate(), Throws.TypeOf<ValidationException>());
supply = CreateDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), DateTime.UtcNow, []);
Assert.That(() => supply.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void AllFieldsIsCorrectTest()
{
var id = Guid.NewGuid().ToString();
var warehouseId = Guid.NewGuid().ToString();
var date = DateTime.UtcNow;
var products = CreateSubDataModel();
var supply = CreateDataModel(id, warehouseId, date, products);
Assert.That(() => supply.Validate(), Throws.Nothing);
Assert.Multiple(() =>
{
Assert.That(supply.Id, Is.EqualTo(id));
Assert.That(supply.WarehouseId, Is.EqualTo(warehouseId));
Assert.That(supply.Date, Is.EqualTo(date));
Assert.That(supply.Products, Is.EqualTo(products));
});
}
private static SupplyDataModel CreateDataModel(string? id, string? warehouseId, DateTime date, List<SupplyProductDataModel> products) =>
new (id, warehouseId, date, products);
private static List<SupplyProductDataModel> CreateSubDataModel() => [new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 1)];
}

View File

@ -0,0 +1,71 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WildPlum.DataModels;
namespace WildPlumTests.DataModelsTests;
[TestFixture]
internal class SupplyProductDataModelTests
{
[Test]
public void SupplyIdIsNullOrEmptyTest()
{
var supply = CreateDataModel(null, Guid.NewGuid().ToString(), 10);
Assert.That(() => supply.Validate(), Throws.TypeOf<ValidationException>());
supply = CreateDataModel(string.Empty, Guid.NewGuid().ToString(), 10);
Assert.That(() => supply.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void SupplyIdIsNotGuidTest()
{
var supply = CreateDataModel("supplyId", Guid.NewGuid().ToString(), 10);
Assert.That(() => supply.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void ProductIdIsNullOrEmptyTest()
{
var supply = CreateDataModel(Guid.NewGuid().ToString(), null, 10);
Assert.That(() => supply.Validate(), Throws.TypeOf<ValidationException>());
supply = CreateDataModel(Guid.NewGuid().ToString(), string.Empty, 10);
Assert.That(() => supply.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void ProductIdIsNotGuidTest()
{
var supply = CreateDataModel(Guid.NewGuid().ToString(), "productId", 10);
Assert.That(() => supply.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void CountNotCorrectTest()
{
var supply = CreateDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), -10);
Assert.That(() => supply.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void AllFieldsIsCorrectTest()
{
var supplyId = Guid.NewGuid().ToString();
var productId = Guid.NewGuid().ToString();
var count = 10;
var supplyProduct = CreateDataModel(supplyId, productId, count);
Assert.That(() => supplyProduct.Validate(), Throws.Nothing);
Assert.Multiple(() =>
{
Assert.That(supplyProduct.SupplyId, Is.EqualTo(supplyId));
Assert.That(supplyProduct.ProductId, Is.EqualTo(productId));
Assert.That(supplyProduct.Count, Is.EqualTo(count));
});
}
private static SupplyProductDataModel CreateDataModel(string? id, string? productId, int count) =>
new(id, productId, count);
}

View File

@ -0,0 +1,51 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WildPlum.DataModels;
namespace WildPlumTests.DataModelsTests;
[TestFixture]
internal class WarehouseDataModelTests
{
[Test]
public void IdIsNullOrEmptyTest()
{
var warehouse = CreateDataModel(null, "name", "address");
Assert.That(() => warehouse.Validate(), Throws.TypeOf<ValidationException>());
warehouse = CreateDataModel(string.Empty, "name", "address");
Assert.That(() => warehouse.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void IdIsNotGuidTest()
{
var warehouse = CreateDataModel("id", "name", "address");
Assert.That(() => warehouse.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void NameIsNullOrEmptyTest()
{
var warehouse = CreateDataModel(Guid.NewGuid().ToString(), null, "address");
Assert.That(() => warehouse.Validate(), Throws.TypeOf<ValidationException>());
warehouse = CreateDataModel(Guid.NewGuid().ToString(), string.Empty, "address");
Assert.That(() => warehouse.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void AddressIsNullOrEmptyTest()
{
var warehouse = CreateDataModel(Guid.NewGuid().ToString(), "name", null);
Assert.That(() => warehouse.Validate(), Throws.TypeOf<ValidationException>());
warehouse = CreateDataModel(Guid.NewGuid().ToString(), "name", string.Empty);
Assert.That(() => warehouse.Validate(), Throws.TypeOf<ValidationException>());
}
private static WarehouseDataModel CreateDataModel(string? id, string? name, string? adress) =>
new(id, name, adress);
}