Лабораторная работа 1. Добавлены компоненты и изменены классы продуктов
This commit is contained in:
parent
24107caa75
commit
c9488b341a
@ -0,0 +1,45 @@
|
|||||||
|
using NorthBridgeContract.Enums;
|
||||||
|
using NorthBridgeContract.Exceptions;
|
||||||
|
using NorthBridgeContract.Extensions;
|
||||||
|
using NorthBridgeContract.Infrastructure;
|
||||||
|
|
||||||
|
namespace NorthBridgeContract.DataModels;
|
||||||
|
|
||||||
|
public class ComponentDataModel(string id, string componentName, ComponentType componentType, string manufacturerId, 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 ManufacturerId { get; private set; } = manufacturerId;
|
||||||
|
|
||||||
|
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 ProductName is empty");
|
||||||
|
|
||||||
|
if (ComponentType == ComponentType.None)
|
||||||
|
throw new ValidationException("Field ProductType is empty");
|
||||||
|
|
||||||
|
if (ManufacturerId.IsEmpty())
|
||||||
|
throw new ValidationException("Field ManufacturerId is empty");
|
||||||
|
|
||||||
|
if (!ManufacturerId.IsGuid())
|
||||||
|
throw new ValidationException("The value in the field ManufacturerId is not a unique identifier");
|
||||||
|
|
||||||
|
if (Price <= 0)
|
||||||
|
throw new ValidationException("Field Price is less than or equal to 0");
|
||||||
|
}
|
||||||
|
}
|
@ -4,9 +4,9 @@ using NorthBridgeContract.Infrastructure;
|
|||||||
|
|
||||||
namespace NorthBridgeContract.DataModels;
|
namespace NorthBridgeContract.DataModels;
|
||||||
|
|
||||||
public class ProductHistoryDataModel(string productId, double oldPrice) : IValidation
|
public class ComponentHistoryDataModel(string componentId, double oldPrice) : IValidation
|
||||||
{
|
{
|
||||||
public string ProductId { get; private set; } = productId;
|
public string ComponentId { get; private set; } = componentId;
|
||||||
|
|
||||||
public double OldPrice { get; private set; } = oldPrice;
|
public double OldPrice { get; private set; } = oldPrice;
|
||||||
|
|
||||||
@ -14,11 +14,11 @@ public class ProductHistoryDataModel(string productId, double oldPrice) : IValid
|
|||||||
|
|
||||||
public void Validate()
|
public void Validate()
|
||||||
{
|
{
|
||||||
if (ProductId.IsEmpty())
|
if (ComponentId.IsEmpty())
|
||||||
throw new ValidationException("Field ProductId is empty");
|
throw new ValidationException("Field ComponentId is empty");
|
||||||
|
|
||||||
if (!ProductId.IsGuid())
|
if (!ComponentId.IsGuid())
|
||||||
throw new ValidationException("The value in the field ProductId is not a unique identifier");
|
throw new ValidationException("The value in the field ComponentId is not a unique identifier");
|
||||||
|
|
||||||
if (OldPrice <= 0)
|
if (OldPrice <= 0)
|
||||||
throw new ValidationException("Field OldPrice is less than or equal to 0");
|
throw new ValidationException("Field OldPrice is less than or equal to 0");
|
@ -0,0 +1,28 @@
|
|||||||
|
using NorthBridgeContract.Exceptions;
|
||||||
|
using NorthBridgeContract.Extensions;
|
||||||
|
using NorthBridgeContract.Infrastructure;
|
||||||
|
|
||||||
|
namespace NorthBridgeContract.DataModels
|
||||||
|
{
|
||||||
|
public class ComponentInProductDataModel(string componentId, string productId) : IValidation
|
||||||
|
{
|
||||||
|
public string ComponentId { get; private set; } = componentId;
|
||||||
|
public string ProductId { get; private set;} = productId;
|
||||||
|
|
||||||
|
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 (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");
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -1,25 +1,24 @@
|
|||||||
using NorthBridgeContract.Enums;
|
using NorthBridgeContract.Exceptions;
|
||||||
using NorthBridgeContract.Exceptions;
|
|
||||||
using NorthBridgeContract.Extensions;
|
using NorthBridgeContract.Extensions;
|
||||||
using NorthBridgeContract.Infrastructure;
|
using NorthBridgeContract.Infrastructure;
|
||||||
|
|
||||||
namespace NorthBridgeContract.DataModels;
|
namespace NorthBridgeContract.DataModels;
|
||||||
|
|
||||||
public class ProductDataModel(string id, string productName, ProductType productType, string manufacturerId, double price, bool isDeleted) : IValidation
|
public class ProductDataModel(string id, string productName, string workerId, bool isDeleted, List<ComponentDataModel> components) : IValidation
|
||||||
{
|
{
|
||||||
public string Id { get; private set; } = id;
|
public string Id { get; private set; } = id;
|
||||||
|
|
||||||
public string ProductName { get; private set; } = productName;
|
public string ProductName { get; private set; } = productName;
|
||||||
|
|
||||||
public ProductType ProductType { get; private set; } = productType;
|
public string WorkerId { get; private set; } = workerId;
|
||||||
|
|
||||||
public string ManufacturerId { get; private set; } = manufacturerId;
|
|
||||||
|
|
||||||
public double Price { get; private set; } = price;
|
|
||||||
|
|
||||||
public bool IsDeleted { get; private set; } = isDeleted;
|
public bool IsDeleted { get; private set; } = isDeleted;
|
||||||
|
|
||||||
public void Validate()
|
public List<ComponentDataModel> Components { get; private set; } = components;
|
||||||
|
|
||||||
|
public double TotalPrice => Components.Sum(x => x.Price);
|
||||||
|
|
||||||
|
public void Validate()
|
||||||
{
|
{
|
||||||
if (Id.IsEmpty())
|
if (Id.IsEmpty())
|
||||||
throw new ValidationException("Field Id is empty");
|
throw new ValidationException("Field Id is empty");
|
||||||
@ -30,16 +29,19 @@ public class ProductDataModel(string id, string productName, ProductType product
|
|||||||
if (ProductName.IsEmpty())
|
if (ProductName.IsEmpty())
|
||||||
throw new ValidationException("Field ProductName is empty");
|
throw new ValidationException("Field ProductName is empty");
|
||||||
|
|
||||||
if (ProductType == ProductType.None)
|
if (WorkerId.IsEmpty())
|
||||||
throw new ValidationException("Field ProductType is empty");
|
|
||||||
|
|
||||||
if (ManufacturerId.IsEmpty())
|
|
||||||
throw new ValidationException("Field ManufacturerId is empty");
|
throw new ValidationException("Field ManufacturerId is empty");
|
||||||
|
|
||||||
if (!ManufacturerId.IsGuid())
|
if (!WorkerId.IsGuid())
|
||||||
throw new ValidationException("The value in the field ManufacturerId is not a unique identifier");
|
throw new ValidationException("The value in the field ManufacturerId is not a unique identifier");
|
||||||
|
|
||||||
if (Price <= 0)
|
if (TotalPrice <= 0)
|
||||||
throw new ValidationException("Field Price is less than or equal to 0");
|
throw new ValidationException("Field Price is less than or equal to 0");
|
||||||
}
|
|
||||||
|
if (TotalPrice != Components.Sum(x => x.Price))
|
||||||
|
throw new ValidationException("The total price of the components does not match");
|
||||||
|
|
||||||
|
if (Components.Count == 0)
|
||||||
|
throw new ValidationException("At least 1 component is needed");
|
||||||
|
}
|
||||||
}
|
}
|
@ -1,6 +1,6 @@
|
|||||||
namespace NorthBridgeContract.Enums;
|
namespace NorthBridgeContract.Enums;
|
||||||
|
|
||||||
public enum ProductType
|
public enum ComponentType
|
||||||
{
|
{
|
||||||
None = 0,
|
None = 0,
|
||||||
GraphicsCard = 1,
|
GraphicsCard = 1,
|
@ -0,0 +1,91 @@
|
|||||||
|
using NorthBridgeContract.DataModels;
|
||||||
|
using NorthBridgeContract.Enums;
|
||||||
|
using NorthBridgeContract.Exceptions;
|
||||||
|
|
||||||
|
namespace NorthBridgeTest.DataModelsTests;
|
||||||
|
|
||||||
|
[TestFixture]
|
||||||
|
internal class ComponentDataModelTests
|
||||||
|
{
|
||||||
|
[Test]
|
||||||
|
public void IdIsNullOrEmptyTest()
|
||||||
|
{
|
||||||
|
var component = CreateDataModel(null, "name", ComponentType.Processor, Guid.NewGuid().ToString(), 10, false);
|
||||||
|
Assert.That(() => component.Validate(), Throws.TypeOf<ValidationException>());
|
||||||
|
component = CreateDataModel(string.Empty, "name", ComponentType.Processor, Guid.NewGuid().ToString(), 10, false);
|
||||||
|
Assert.That(() => component.Validate(), Throws.TypeOf<ValidationException>());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void IdIsNotGuidTest()
|
||||||
|
{
|
||||||
|
var component = CreateDataModel("id", "name", ComponentType.Processor, Guid.NewGuid().ToString(), 10, false);
|
||||||
|
Assert.That(() => component.Validate(), Throws.TypeOf<ValidationException>());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void ComponentNameIsEmptyTest()
|
||||||
|
{
|
||||||
|
var component = CreateDataModel(Guid.NewGuid().ToString(), null, ComponentType.Processor, Guid.NewGuid().ToString(), 10, false);
|
||||||
|
Assert.That(() => component.Validate(), Throws.TypeOf<ValidationException>());
|
||||||
|
component = CreateDataModel(Guid.NewGuid().ToString(), string.Empty, ComponentType.Processor, 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 ManufacturerIdIsNullOrEmptyTest()
|
||||||
|
{
|
||||||
|
var component = CreateDataModel(Guid.NewGuid().ToString(), "name", ComponentType.Processor, null, 10, false);
|
||||||
|
Assert.That(() => component.Validate(), Throws.TypeOf<ValidationException>());
|
||||||
|
component = CreateDataModel(Guid.NewGuid().ToString(), "name", ComponentType.Processor, string.Empty, 10, false);
|
||||||
|
Assert.That(() => component.Validate(), Throws.TypeOf<ValidationException>());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void ManufacturerIdIsNotGuidTest()
|
||||||
|
{
|
||||||
|
var component = CreateDataModel(Guid.NewGuid().ToString(), "name", ComponentType.Processor, "manufacturerId", 10, false);
|
||||||
|
Assert.That(() => component.Validate(), Throws.TypeOf<ValidationException>());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void PriceIsLessOrZeroTest()
|
||||||
|
{
|
||||||
|
var component = CreateDataModel(Guid.NewGuid().ToString(), "name", ComponentType.Processor, Guid.NewGuid().ToString(), 0, false);
|
||||||
|
Assert.That(() => component.Validate(), Throws.TypeOf<ValidationException>());
|
||||||
|
component = CreateDataModel(Guid.NewGuid().ToString(), "name", ComponentType.Processor, 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.Processor;
|
||||||
|
var componentManufacturerId = Guid.NewGuid().ToString();
|
||||||
|
var componentPrice = 10;
|
||||||
|
var componentIsDelete = false;
|
||||||
|
var component = CreateDataModel(componentId, componentName, componentType, componentManufacturerId, 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.ManufacturerId, Is.EqualTo(componentManufacturerId));
|
||||||
|
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? manufacturerId, double price, bool isDeleted) =>
|
||||||
|
new(id, componentName, componentType, manufacturerId, price, isDeleted);
|
||||||
|
}
|
@ -0,0 +1,52 @@
|
|||||||
|
using NorthBridgeContract.DataModels;
|
||||||
|
using NorthBridgeContract.Exceptions;
|
||||||
|
|
||||||
|
namespace NorthBridgeTest.DataModelsTests;
|
||||||
|
|
||||||
|
[TestFixture]
|
||||||
|
internal class ComponentHistoryDataModelTests
|
||||||
|
{
|
||||||
|
[Test]
|
||||||
|
public void ComponentIdIsNullOrEmptyTest()
|
||||||
|
{
|
||||||
|
var product = CreateDataModel(null, 10);
|
||||||
|
Assert.That(() => product.Validate(), Throws.TypeOf<ValidationException>());
|
||||||
|
product = CreateDataModel(string.Empty, 10);
|
||||||
|
Assert.That(() => product.Validate(), Throws.TypeOf<ValidationException>());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void ComponentIdIsNotGuidTest()
|
||||||
|
{
|
||||||
|
var product = CreateDataModel("id", 10);
|
||||||
|
Assert.That(() => product.Validate(), Throws.TypeOf<ValidationException>());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void OldPriceIsLessOrZeroTest()
|
||||||
|
{
|
||||||
|
var product = CreateDataModel(Guid.NewGuid().ToString(), 0);
|
||||||
|
Assert.That(() => product.Validate(), Throws.TypeOf<ValidationException>());
|
||||||
|
product = CreateDataModel(Guid.NewGuid().ToString(), -10);
|
||||||
|
Assert.That(() => product.Validate(), Throws.TypeOf<ValidationException>());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void AllFieldsIsCorrectTest()
|
||||||
|
{
|
||||||
|
var componentId = Guid.NewGuid().ToString();
|
||||||
|
var oldPrice = 10;
|
||||||
|
var productHistory = CreateDataModel(componentId, oldPrice);
|
||||||
|
Assert.That(() => productHistory.Validate(), Throws.Nothing);
|
||||||
|
Assert.Multiple(() =>
|
||||||
|
{
|
||||||
|
Assert.That(productHistory.ComponentId, Is.EqualTo(componentId));
|
||||||
|
Assert.That(productHistory.OldPrice, Is.EqualTo(oldPrice));
|
||||||
|
Assert.That(productHistory.ChangeDate, Is.LessThan(DateTime.UtcNow));
|
||||||
|
Assert.That(productHistory.ChangeDate, Is.GreaterThan(DateTime.UtcNow.AddMinutes(-1)));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
private static ComponentHistoryDataModel CreateDataModel(string? componentId, double oldPrice) =>
|
||||||
|
new(componentId, oldPrice);
|
||||||
|
}
|
@ -0,0 +1,63 @@
|
|||||||
|
using NorthBridgeContract.DataModels;
|
||||||
|
using NorthBridgeContract.Exceptions;
|
||||||
|
using NUnit.Framework;
|
||||||
|
using System;
|
||||||
|
|
||||||
|
namespace NorthBridgeTest.DataModelsTests
|
||||||
|
{
|
||||||
|
[TestFixture]
|
||||||
|
internal class ComponentInProductDataModelTests
|
||||||
|
{
|
||||||
|
[Test]
|
||||||
|
public void ComponentIdIsNullOrEmptyTest()
|
||||||
|
{
|
||||||
|
var componentInProduct = CreateDataModel(null, Guid.NewGuid().ToString());
|
||||||
|
Assert.That(() => componentInProduct.Validate(), Throws.TypeOf<ValidationException>());
|
||||||
|
|
||||||
|
componentInProduct = CreateDataModel(string.Empty, Guid.NewGuid().ToString());
|
||||||
|
Assert.That(() => componentInProduct.Validate(), Throws.TypeOf<ValidationException>());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void ComponentIdIsNotGuidTest()
|
||||||
|
{
|
||||||
|
var componentInProduct = CreateDataModel("invalid_id", Guid.NewGuid().ToString());
|
||||||
|
Assert.That(() => componentInProduct.Validate(), Throws.TypeOf<ValidationException>());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void ProductIdIsNullOrEmptyTest()
|
||||||
|
{
|
||||||
|
var componentInProduct = CreateDataModel(Guid.NewGuid().ToString(), null);
|
||||||
|
Assert.That(() => componentInProduct.Validate(), Throws.TypeOf<ValidationException>());
|
||||||
|
|
||||||
|
componentInProduct = CreateDataModel(Guid.NewGuid().ToString(), string.Empty);
|
||||||
|
Assert.That(() => componentInProduct.Validate(), Throws.TypeOf<ValidationException>());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void ProductIdIsNotGuidTest()
|
||||||
|
{
|
||||||
|
var componentInProduct = CreateDataModel(Guid.NewGuid().ToString(), "invalid_id");
|
||||||
|
Assert.That(() => componentInProduct.Validate(), Throws.TypeOf<ValidationException>());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void AllFieldsAreCorrectTest()
|
||||||
|
{
|
||||||
|
var componentId = Guid.NewGuid().ToString();
|
||||||
|
var productId = Guid.NewGuid().ToString();
|
||||||
|
var componentInProduct = CreateDataModel(componentId, productId);
|
||||||
|
|
||||||
|
Assert.That(() => componentInProduct.Validate(), Throws.Nothing);
|
||||||
|
Assert.Multiple(() =>
|
||||||
|
{
|
||||||
|
Assert.That(componentInProduct.ComponentId, Is.EqualTo(componentId));
|
||||||
|
Assert.That(componentInProduct.ProductId, Is.EqualTo(productId));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
private static ComponentInProductDataModel CreateDataModel(string? componentId, string? productId) =>
|
||||||
|
new(componentId, productId);
|
||||||
|
}
|
||||||
|
}
|
@ -2,90 +2,74 @@
|
|||||||
using NorthBridgeContract.Enums;
|
using NorthBridgeContract.Enums;
|
||||||
using NorthBridgeContract.Exceptions;
|
using NorthBridgeContract.Exceptions;
|
||||||
|
|
||||||
namespace NorthBridgeTest.DataModelsTests;
|
namespace NorthBridgeTest.DataModelsTests
|
||||||
|
|
||||||
[TestFixture]
|
|
||||||
internal class ProductDataModelTests
|
|
||||||
{
|
{
|
||||||
[Test]
|
[TestFixture]
|
||||||
public void IdIsNullOrEmptyTest()
|
internal class ProductDataModelTests
|
||||||
{
|
{
|
||||||
var product = CreateDataModel(null, "name", ProductType.Processor, Guid.NewGuid().ToString(), 10, false);
|
[Test]
|
||||||
Assert.That(() => product.Validate(), Throws.TypeOf<ValidationException>());
|
public void IdIsNullOrEmptyTest()
|
||||||
product = CreateDataModel(string.Empty, "name", ProductType.Processor, Guid.NewGuid().ToString(), 10, false);
|
{
|
||||||
Assert.That(() => product.Validate(), Throws.TypeOf<ValidationException>());
|
var product = CreateDataModel(null, "Processor", Guid.NewGuid().ToString(), false, new List<ComponentDataModel> { new ComponentDataModel(Guid.NewGuid().ToString(), "CPU", ComponentType.Processor, Guid.NewGuid().ToString(), 200, false) });
|
||||||
}
|
Assert.That(() => product.Validate(), Throws.TypeOf<ValidationException>());
|
||||||
|
product = CreateDataModel(string.Empty, "Processor", Guid.NewGuid().ToString(), false, new List<ComponentDataModel> { new ComponentDataModel(Guid.NewGuid().ToString(), "CPU", ComponentType.Processor, Guid.NewGuid().ToString(), 200, false) });
|
||||||
|
Assert.That(() => product.Validate(), Throws.TypeOf<ValidationException>());
|
||||||
|
}
|
||||||
|
|
||||||
[Test]
|
[Test]
|
||||||
public void IdIsNotGuidTest()
|
public void IdIsNotGuidTest()
|
||||||
{
|
{
|
||||||
var product = CreateDataModel("id", "name", ProductType.Processor, Guid.NewGuid().ToString(), 10, false);
|
var product = CreateDataModel("id", "Processor", Guid.NewGuid().ToString(), false, new List<ComponentDataModel> { new ComponentDataModel(Guid.NewGuid().ToString(), "CPU", ComponentType.Processor, Guid.NewGuid().ToString(), 200, false) });
|
||||||
Assert.That(() => product.Validate(), Throws.TypeOf<ValidationException>());
|
Assert.That(() => product.Validate(), Throws.TypeOf<ValidationException>());
|
||||||
}
|
}
|
||||||
|
|
||||||
[Test]
|
[Test]
|
||||||
public void ProductNameIsEmptyTest()
|
public void ProductNameIsNullOrEmptyTest()
|
||||||
{
|
{
|
||||||
var product = CreateDataModel(Guid.NewGuid().ToString(), null, ProductType.Processor, Guid.NewGuid().ToString(), 10, false);
|
var product = CreateDataModel(Guid.NewGuid().ToString(), null, Guid.NewGuid().ToString(), false, new List<ComponentDataModel> { new ComponentDataModel(Guid.NewGuid().ToString(), "CPU", ComponentType.Processor, Guid.NewGuid().ToString(), 200, false) });
|
||||||
Assert.That(() => product.Validate(), Throws.TypeOf<ValidationException>());
|
Assert.That(() => product.Validate(), Throws.TypeOf<ValidationException>());
|
||||||
product = CreateDataModel(Guid.NewGuid().ToString(), string.Empty, ProductType.Processor, Guid.NewGuid().ToString(), 10, false);
|
product = CreateDataModel(Guid.NewGuid().ToString(), string.Empty, Guid.NewGuid().ToString(), false, new List<ComponentDataModel> { new ComponentDataModel(Guid.NewGuid().ToString(), "CPU", ComponentType.Processor, Guid.NewGuid().ToString(), 200, false) });
|
||||||
Assert.That(() => product.Validate(), Throws.TypeOf<ValidationException>());
|
Assert.That(() => product.Validate(), Throws.TypeOf<ValidationException>());
|
||||||
}
|
}
|
||||||
|
|
||||||
[Test]
|
[Test]
|
||||||
public void ProductTypeIsNoneTest()
|
public void WorkerIdIsNullOrEmptyTest()
|
||||||
{
|
{
|
||||||
var product = CreateDataModel(Guid.NewGuid().ToString(), null, ProductType.None, Guid.NewGuid().ToString(), 10, false);
|
var product = CreateDataModel(Guid.NewGuid().ToString(), "Processor", null, false, new List<ComponentDataModel> { new ComponentDataModel(Guid.NewGuid().ToString(), "CPU", ComponentType.Processor, Guid.NewGuid().ToString(), 200, false) });
|
||||||
Assert.That(() => product.Validate(), Throws.TypeOf<ValidationException>());
|
Assert.That(() => product.Validate(), Throws.TypeOf<ValidationException>());
|
||||||
}
|
product = CreateDataModel(Guid.NewGuid().ToString(), "Processor", string.Empty, false, new List<ComponentDataModel> { new ComponentDataModel(Guid.NewGuid().ToString(), "CPU", ComponentType.Processor, Guid.NewGuid().ToString(), 200, false) });
|
||||||
|
Assert.That(() => product.Validate(), Throws.TypeOf<ValidationException>());
|
||||||
|
}
|
||||||
|
|
||||||
[Test]
|
[Test]
|
||||||
public void ManufacturerIdIsNullOrEmptyTest()
|
public void WorkerIdIsNotGuidTest()
|
||||||
{
|
{
|
||||||
var product = CreateDataModel(Guid.NewGuid().ToString(), "name", ProductType.Processor, null, 10, false);
|
var product = CreateDataModel(Guid.NewGuid().ToString(), "Processor", "workerId", false, new List<ComponentDataModel> { new ComponentDataModel(Guid.NewGuid().ToString(), "CPU", ComponentType.Processor, Guid.NewGuid().ToString(), 200, false) });
|
||||||
Assert.That(() => product.Validate(), Throws.TypeOf<ValidationException>());
|
Assert.That(() => product.Validate(), Throws.TypeOf<ValidationException>());
|
||||||
product = CreateDataModel(Guid.NewGuid().ToString(), "name", ProductType.Processor, string.Empty, 10, false);
|
}
|
||||||
Assert.That(() => product.Validate(), Throws.TypeOf<ValidationException>());
|
|
||||||
}
|
|
||||||
|
|
||||||
[Test]
|
[Test]
|
||||||
public void ManufacturerIdIsNotGuidTest()
|
public void ComponentsAreNullOrEmptyTest()
|
||||||
{
|
{
|
||||||
var product = CreateDataModel(Guid.NewGuid().ToString(), "name", ProductType.Processor, "manufacturerId", 10, false);
|
var product = CreateDataModel(Guid.NewGuid().ToString(), "Processor", Guid.NewGuid().ToString(), false, null);
|
||||||
Assert.That(() => product.Validate(), Throws.TypeOf<ValidationException>());
|
Assert.That(() => product.Validate(), Throws.TypeOf<ValidationException>());
|
||||||
}
|
product = CreateDataModel(Guid.NewGuid().ToString(), "Processor", Guid.NewGuid().ToString(), false, new List<ComponentDataModel>());
|
||||||
|
Assert.That(() => product.Validate(), Throws.TypeOf<ValidationException>());
|
||||||
|
}
|
||||||
|
|
||||||
[Test]
|
[Test]
|
||||||
public void PriceIsLessOrZeroTest()
|
public void TotalPriceIsIncorrectTest()
|
||||||
{
|
{
|
||||||
var product = CreateDataModel(Guid.NewGuid().ToString(), "name", ProductType.Processor, Guid.NewGuid().ToString(), 0, false);
|
var components = new List<ComponentDataModel>
|
||||||
Assert.That(() => product.Validate(), Throws.TypeOf<ValidationException>());
|
{
|
||||||
product = CreateDataModel(Guid.NewGuid().ToString(), "name", ProductType.Processor, Guid.NewGuid().ToString(), -10, false);
|
new ComponentDataModel(Guid.NewGuid().ToString(), "CPU", ComponentType.Processor, Guid.NewGuid().ToString(), 200, false),
|
||||||
Assert.That(() => product.Validate(), Throws.TypeOf<ValidationException>());
|
new ComponentDataModel(Guid.NewGuid().ToString(), "GPU", ComponentType.GraphicsCard, Guid.NewGuid().ToString(), 300, false)
|
||||||
}
|
};
|
||||||
|
var product = CreateDataModel(Guid.NewGuid().ToString(), "Processor", Guid.NewGuid().ToString(), false, components);
|
||||||
|
Assert.That(product.TotalPrice, Is.EqualTo(500));
|
||||||
|
}
|
||||||
|
|
||||||
[Test]
|
private static ProductDataModel CreateDataModel(string? id, string? productName, string? workerId, bool isDeleted, List<ComponentDataModel>? components) =>
|
||||||
public void AllFieldsIsCorrectTest()
|
new(id, productName, workerId, isDeleted, components ?? new List<ComponentDataModel>());
|
||||||
{
|
}
|
||||||
var productId = Guid.NewGuid().ToString();
|
}
|
||||||
var productName = "name";
|
|
||||||
var productType = ProductType.Processor;
|
|
||||||
var productManufacturerId = Guid.NewGuid().ToString();
|
|
||||||
var productPrice = 10;
|
|
||||||
var productIsDelete = false;
|
|
||||||
var product = CreateDataModel(productId, productName, productType, productManufacturerId, productPrice, productIsDelete);
|
|
||||||
Assert.That(() => product.Validate(), Throws.Nothing);
|
|
||||||
Assert.Multiple(() =>
|
|
||||||
{
|
|
||||||
Assert.That(product.Id, Is.EqualTo(productId));
|
|
||||||
Assert.That(product.ProductName, Is.EqualTo(productName));
|
|
||||||
Assert.That(product.ProductType, Is.EqualTo(productType));
|
|
||||||
Assert.That(product.ManufacturerId, Is.EqualTo(productManufacturerId));
|
|
||||||
Assert.That(product.Price, Is.EqualTo(productPrice));
|
|
||||||
Assert.That(product.IsDeleted, Is.EqualTo(productIsDelete));
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
private static ProductDataModel CreateDataModel(string? id, string? productName, ProductType productType, string? manufacturerId, double price, bool isDeleted) =>
|
|
||||||
new(id, productName, productType, manufacturerId, price, isDeleted);
|
|
||||||
}
|
|
||||||
|
@ -1,52 +0,0 @@
|
|||||||
using NorthBridgeContract.DataModels;
|
|
||||||
using NorthBridgeContract.Exceptions;
|
|
||||||
|
|
||||||
namespace NorthBridgeTest.DataModelsTests;
|
|
||||||
|
|
||||||
[TestFixture]
|
|
||||||
internal class ProductHistoryDataModelTests
|
|
||||||
{
|
|
||||||
[Test]
|
|
||||||
public void ProductIdIsNullOrEmptyTest()
|
|
||||||
{
|
|
||||||
var product = CreateDataModel(null, 10);
|
|
||||||
Assert.That(() => product.Validate(), Throws.TypeOf<ValidationException>());
|
|
||||||
product = CreateDataModel(string.Empty, 10);
|
|
||||||
Assert.That(() => product.Validate(), Throws.TypeOf<ValidationException>());
|
|
||||||
}
|
|
||||||
|
|
||||||
[Test]
|
|
||||||
public void ProductIdIsNotGuidTest()
|
|
||||||
{
|
|
||||||
var product = CreateDataModel("id", 10);
|
|
||||||
Assert.That(() => product.Validate(), Throws.TypeOf<ValidationException>());
|
|
||||||
}
|
|
||||||
|
|
||||||
[Test]
|
|
||||||
public void OldPriceIsLessOrZeroTest()
|
|
||||||
{
|
|
||||||
var product = CreateDataModel(Guid.NewGuid().ToString(), 0);
|
|
||||||
Assert.That(() => product.Validate(), Throws.TypeOf<ValidationException>());
|
|
||||||
product = CreateDataModel(Guid.NewGuid().ToString(), -10);
|
|
||||||
Assert.That(() => product.Validate(), Throws.TypeOf<ValidationException>());
|
|
||||||
}
|
|
||||||
|
|
||||||
[Test]
|
|
||||||
public void AllFieldsIsCorrectTest()
|
|
||||||
{
|
|
||||||
var productId = Guid.NewGuid().ToString();
|
|
||||||
var oldPrice = 10;
|
|
||||||
var productHistory = CreateDataModel(productId, oldPrice);
|
|
||||||
Assert.That(() => productHistory.Validate(), Throws.Nothing);
|
|
||||||
Assert.Multiple(() =>
|
|
||||||
{
|
|
||||||
Assert.That(productHistory.ProductId, Is.EqualTo(productId));
|
|
||||||
Assert.That(productHistory.OldPrice, Is.EqualTo(oldPrice));
|
|
||||||
Assert.That(productHistory.ChangeDate, Is.LessThan(DateTime.UtcNow));
|
|
||||||
Assert.That(productHistory.ChangeDate, Is.GreaterThan(DateTime.UtcNow.AddMinutes(-1)));
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
private static ProductHistoryDataModel CreateDataModel(string? productId, double oldPrice) =>
|
|
||||||
new(productId, oldPrice);
|
|
||||||
}
|
|
Loading…
x
Reference in New Issue
Block a user