Compare commits
1 Commits
Task4_Api
...
Task1Hard_
| Author | SHA1 | Date | |
|---|---|---|---|
| 333aae9d2a |
@@ -0,0 +1,24 @@
|
||||
using SPiluSZharuContracts.Exceptions;
|
||||
using SPiluSZharuContracts.Extensions;
|
||||
using SPiluSZharuContracts.Infrastructure;
|
||||
|
||||
namespace SPiluSZharuContracts.DataModels;
|
||||
|
||||
public class DarkstoreDataModel(string id, List<SupplyProductDataModel> products) : IValidation
|
||||
{
|
||||
public string Id { get; private set; } = id;
|
||||
|
||||
public int ProductsCount => Products.Sum(x => x.Count);
|
||||
|
||||
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 (ProductsCount < 10)
|
||||
throw new ValidationException("Count of Products is not enough for sale");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
using SPiluSZharuContracts.Exceptions;
|
||||
using SPiluSZharuContracts.Extensions;
|
||||
using SPiluSZharuContracts.Infrastructure;
|
||||
|
||||
namespace SPiluSZharuContracts.DataModels;
|
||||
|
||||
public class SupplyDataModel(string id, string darkstoreId, List<SupplyProductDataModel> products) : IValidation
|
||||
{
|
||||
public string Id { get; private set; } = id;
|
||||
|
||||
public string DarkstoreId { get; private set; } = darkstoreId;
|
||||
|
||||
public DateTime SupplyDate { get; private set; } = DateTime.UtcNow;
|
||||
|
||||
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 (DarkstoreId.IsEmpty())
|
||||
throw new ValidationException("Field WorkerId is empty");
|
||||
if (!DarkstoreId.IsGuid())
|
||||
throw new ValidationException("The value in the field DarkstoreId is not a unique identifier");
|
||||
if ((Products?.Count ?? 0) == 0)
|
||||
throw new ValidationException("The supply must include products");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
using SPiluSZharuContracts.Exceptions;
|
||||
using SPiluSZharuContracts.Extensions;
|
||||
using SPiluSZharuContracts.Infrastructure;
|
||||
|
||||
namespace SPiluSZharuContracts.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 SupplyId is empty");
|
||||
|
||||
if (!SupplyId.IsGuid())
|
||||
throw new ValidationException("The value in the field SupplyId 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");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
using SPiluSZharuContracts.DataModels;
|
||||
using SPiluSZharuContracts.Exceptions;
|
||||
|
||||
namespace SPiluSZharuTests.DataModelTests;
|
||||
|
||||
[TestFixture]
|
||||
internal class DarkstoreDataModelTests
|
||||
{
|
||||
[Test]
|
||||
public void IdIsNullOrEmptyTest()
|
||||
{
|
||||
var darkstore = CreateDataModel(null, CreateSubDataModel());
|
||||
Assert.That(() => darkstore.Validate(), Throws.TypeOf<ValidationException>());
|
||||
darkstore = CreateDataModel(string.Empty, CreateSubDataModel());
|
||||
Assert.That(() => darkstore.Validate(), Throws.TypeOf<ValidationException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void IdIsNotGuidTest()
|
||||
{
|
||||
var darkstore = CreateDataModel("id", CreateSubDataModel());
|
||||
Assert.That(() => darkstore.Validate(), Throws.TypeOf<ValidationException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ProductsCountsIsNotEnoughForSaleTest()
|
||||
{
|
||||
var products1 = new SupplyProductDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 1);
|
||||
var products2 = new SupplyProductDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 2);
|
||||
var products3 = new SupplyProductDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 3);
|
||||
var darkstore = CreateDataModel(Guid.NewGuid().ToString(), [products1, products2, products3]);
|
||||
Assert.That(() => darkstore.Validate(), Throws.TypeOf<ValidationException>());
|
||||
}
|
||||
|
||||
private static DarkstoreDataModel CreateDataModel(string? id, List<SupplyProductDataModel>? products) =>
|
||||
new(id, products);
|
||||
|
||||
private static List<SupplyProductDataModel> CreateSubDataModel()
|
||||
=> [new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 1)];
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
using SPiluSZharuContracts.DataModels;
|
||||
using SPiluSZharuContracts.Exceptions;
|
||||
|
||||
namespace SPiluSZharuTests.DataModelTests;
|
||||
|
||||
[TestFixture]
|
||||
internal class SupplyDataModelTests
|
||||
{
|
||||
[Test]
|
||||
public void IdIsNullOrEmptyTest()
|
||||
{
|
||||
var supply = CreateDataModel(null, Guid.NewGuid().ToString(), CreateSubDataModel());
|
||||
Assert.That(() => supply.Validate(), Throws.TypeOf<ValidationException>());
|
||||
supply = CreateDataModel(string.Empty, Guid.NewGuid().ToString(), CreateSubDataModel());
|
||||
Assert.That(() => supply.Validate(), Throws.TypeOf<ValidationException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void IdIsNotGuidTest()
|
||||
{
|
||||
var supply = CreateDataModel("id", Guid.NewGuid().ToString(), CreateSubDataModel());
|
||||
Assert.That(() => supply.Validate(), Throws.TypeOf<ValidationException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DarkstoreIdIsNullOrEmptyTest()
|
||||
{
|
||||
var supply = CreateDataModel(Guid.NewGuid().ToString(), null, CreateSubDataModel());
|
||||
Assert.That(() => supply.Validate(), Throws.TypeOf<ValidationException>());
|
||||
supply = CreateDataModel(Guid.NewGuid().ToString(), string.Empty, CreateSubDataModel());
|
||||
Assert.That(() => supply.Validate(), Throws.TypeOf<ValidationException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DarkstoreIdIsNotGuidTest()
|
||||
{
|
||||
var supply = CreateDataModel(Guid.NewGuid().ToString(), "darkstoreId", CreateSubDataModel());
|
||||
Assert.That(() => supply.Validate(), Throws.TypeOf<ValidationException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ProductsIsNullOrEmptyTest()
|
||||
{
|
||||
var supply = CreateDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), null);
|
||||
Assert.That(() => supply.Validate(), Throws.TypeOf<ValidationException>());
|
||||
supply = CreateDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), []);
|
||||
Assert.That(() => supply.Validate(), Throws.TypeOf<ValidationException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void AllFieldsIsCorrectTest()
|
||||
{
|
||||
var supplyId = Guid.NewGuid().ToString();
|
||||
var darkstoreId = Guid.NewGuid().ToString();
|
||||
var products = CreateSubDataModel();
|
||||
var supply = CreateDataModel(supplyId, darkstoreId, products);
|
||||
Assert.That(() => supply.Validate(), Throws.Nothing);
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(supply.Id, Is.EqualTo(supplyId));
|
||||
Assert.That(supply.DarkstoreId, Is.EqualTo(darkstoreId));
|
||||
Assert.That(supply.Products, Is.EquivalentTo(products));
|
||||
});
|
||||
}
|
||||
|
||||
private static SupplyDataModel CreateDataModel(string? id, string? darkstoreId, List<SupplyProductDataModel>? products) =>
|
||||
new(id, darkstoreId, products);
|
||||
|
||||
private static List<SupplyProductDataModel> CreateSubDataModel()
|
||||
=> [new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 1)];
|
||||
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
using SPiluSZharuContracts.DataModels;
|
||||
using SPiluSZharuContracts.Exceptions;
|
||||
|
||||
namespace SPiluSZharuTests.DataModelTests;
|
||||
|
||||
|
||||
[TestFixture]
|
||||
internal class SupplyProductDataModelTests
|
||||
{
|
||||
[Test]
|
||||
public void SupplyIdIsNullOrEmptyTest()
|
||||
{
|
||||
var supplyProduct = CreateDataModel(null, Guid.NewGuid().ToString(), 10);
|
||||
Assert.That(() => supplyProduct.Validate(), Throws.TypeOf<ValidationException>());
|
||||
supplyProduct = CreateDataModel(string.Empty, Guid.NewGuid().ToString(), 10);
|
||||
Assert.That(() => supplyProduct.Validate(), Throws.TypeOf<ValidationException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void SupplyIdIsNotGuidTest()
|
||||
{
|
||||
var supplyProduct = CreateDataModel("supplyId", Guid.NewGuid().ToString(), 10);
|
||||
Assert.That(() => supplyProduct.Validate(), Throws.TypeOf<ValidationException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ProductIdIsNullOrEmptyTest()
|
||||
{
|
||||
var supplyProduct = CreateDataModel(Guid.NewGuid().ToString(), null, 10);
|
||||
Assert.That(() => supplyProduct.Validate(), Throws.TypeOf<ValidationException>());
|
||||
supplyProduct = CreateDataModel(string.Empty, Guid.NewGuid().ToString(), 10);
|
||||
Assert.That(() => supplyProduct.Validate(), Throws.TypeOf<ValidationException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ProductIdIsNotGuidTest()
|
||||
{
|
||||
var supplyProduct = CreateDataModel(Guid.NewGuid().ToString(), "productId", 10);
|
||||
Assert.That(() => supplyProduct.Validate(), Throws.TypeOf<ValidationException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void CountIsLessOrZeroTest()
|
||||
{
|
||||
var supplyProduct = CreateDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 0);
|
||||
Assert.That(() => supplyProduct.Validate(), Throws.TypeOf<ValidationException>());
|
||||
supplyProduct = CreateDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), -10);
|
||||
Assert.That(() => supplyProduct.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);
|
||||
}
|
||||
Reference in New Issue
Block a user