diff --git a/Carpentry/Carpentry/DataModels/ManufacturerDataModel.cs b/Carpentry/Carpentry/DataModels/ManufacturerDataModel.cs deleted file mode 100644 index 26070e4..0000000 --- a/Carpentry/Carpentry/DataModels/ManufacturerDataModel.cs +++ /dev/null @@ -1,28 +0,0 @@ -using Carpentry.Extensions; -using Carpentry.Exceptions; -using Carpentry.Infrastructure; - -namespace Carpentry.DataModels; - -public class ManufacturerDataModel(string id, string manufacturerName, string? prevManufacturerName, string? prevPrevManufacturerName) : IValidation -{ - public string Id { get; private set; } = id; - - public string ManufacturerName { get; private set; } = manufacturerName; - - public string? PrevManufacturerName { get; private set; } = prevManufacturerName; - - public string? PrevPrevManufacturerName { get; private set; } = prevPrevManufacturerName; - - 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 (ManufacturerName.IsEmpty()) - throw new ValidationException("Field ManufacturerName is empty"); - } -} \ No newline at end of file diff --git a/Carpentry/Carpentry/DataModels/ProductHistoryDataModel.cs b/Carpentry/Carpentry/DataModels/ProductHistoryDataModel.cs deleted file mode 100644 index 5358900..0000000 --- a/Carpentry/Carpentry/DataModels/ProductHistoryDataModel.cs +++ /dev/null @@ -1,26 +0,0 @@ -using Carpentry.Extensions; -using Carpentry.Exceptions; -using Carpentry.Infrastructure; - -namespace Carpentry.DataModels; - -public class ProductHistoryDataModel(string productId, double oldPrice) : IValidation -{ - public string ProductId { get; private set; } = productId; - - public double OldPrice { get; private set; } = oldPrice; - - public DateTime ChangeDate { get; private set; } = DateTime.UtcNow; - - public void Validate() - { - 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 (OldPrice <= 0) - throw new ValidationException("Field OldPrice is less than or equal to 0"); - } -} \ No newline at end of file diff --git a/Carpentry/Carpentry/DataModels/SaleDataModel.cs b/Carpentry/Carpentry/DataModels/SaleDataModel.cs deleted file mode 100644 index 950e66f..0000000 --- a/Carpentry/Carpentry/DataModels/SaleDataModel.cs +++ /dev/null @@ -1,51 +0,0 @@ -using Carpentry.Extensions; -using Carpentry.Enums; -using Carpentry.Exceptions; -using Carpentry.Infrastructure; - -namespace Carpentry.DataModels; - -public class SaleDataModel(string id, string workerId, string? buyerId, double sum, DiscountType discountType, double discount, bool isCancel, List products) : IValidation -{ - public string Id { get; private set; } = id; - - public string WorkerId { get; private set; } = workerId; - - public string? BuyerId { get; private set; } = buyerId; - - public DateTime SaleDate { get; private set; } = DateTime.UtcNow; - - public double Sum { get; private set; } = sum; - - public DiscountType DiscountType { get; private set; } = discountType; - - public double Discount { get; private set; } = discount; - - public bool IsCancel { get; private set; } = isCancel; - - public List 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 (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 ((Products?.Count ?? 0) == 0) - throw new ValidationException("The sale must include products"); - } -} \ No newline at end of file diff --git a/Carpentry/Carpentry/DataModels/SaleProductDataModel.cs b/Carpentry/Carpentry/DataModels/SaleProductDataModel.cs deleted file mode 100644 index 8c2d73a..0000000 --- a/Carpentry/Carpentry/DataModels/SaleProductDataModel.cs +++ /dev/null @@ -1,32 +0,0 @@ -using Carpentry.Extensions; -using Carpentry.Exceptions; -using Carpentry.Infrastructure; - -namespace Carpentry.DataModels; - -public class SaleProductDataModel(string saleId, string productId, int count) : IValidation -{ - public string SaleId { get; private set; } = saleId; - - public string ProductId { get; private set; } = productId; - - 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 (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"); - } -} \ No newline at end of file diff --git a/Carpentry/Carpentry/Enums/DiscountType.cs b/Carpentry/Carpentry/Enums/DiscountType.cs deleted file mode 100644 index 34eb443..0000000 --- a/Carpentry/Carpentry/Enums/DiscountType.cs +++ /dev/null @@ -1,10 +0,0 @@ -namespace Carpentry.Enums; - -[Flags] -public enum DiscountType -{ - None = 0, - OnSale = 1, - RegularCustomer = 2, - Certificate = 4 -} \ No newline at end of file diff --git a/Carpentry/CarpentryWorkshopTest/DataModelsTests/ProductDataModelTests.cs b/Carpentry/CarpentryWorkshopTest/DataModelsTests/ProductDataModelTests.cs new file mode 100644 index 0000000..83373f5 --- /dev/null +++ b/Carpentry/CarpentryWorkshopTest/DataModelsTests/ProductDataModelTests.cs @@ -0,0 +1,102 @@ +using Carpentry.DataModels; +using Carpentry.Enums; +using Carpentry.Exceptions; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace CarpentryWorkshopTest.DataModelsTests; + +[TestFixture] +internal class ProductDataModelTests +{ + [Test] + public void IdIsNullOrEmptyTest() + { + var product = CreateDataModel(null, "productName", ProductType.Furniture, Guid.NewGuid().ToString(), 10.0, false); + Assert.That(() => product.Validate(), Throws.TypeOf()); + + product = CreateDataModel(string.Empty, "productName", ProductType.Furniture, Guid.NewGuid().ToString(), 10.0, false); + Assert.That(() => product.Validate(), Throws.TypeOf()); + } + + [Test] + public void IdIsNotGuidTest() + { + var product = CreateDataModel("invalid-id", "productName", ProductType.Furniture, Guid.NewGuid().ToString(), 10.0, false); + Assert.That(() => product.Validate(), Throws.TypeOf()); + } + + [Test] + public void ProductNameIsEmptyTest() + { + var product = CreateDataModel(Guid.NewGuid().ToString(), null, ProductType.Furniture, Guid.NewGuid().ToString(), 10.0, false); + Assert.That(() => product.Validate(), Throws.TypeOf()); + + product = CreateDataModel(Guid.NewGuid().ToString(), string.Empty, ProductType.Furniture, Guid.NewGuid().ToString(), 10.0, false); + Assert.That(() => product.Validate(), Throws.TypeOf()); + } + + [Test] + public void ProductTypeIsNoneTest() + { + var product = CreateDataModel(Guid.NewGuid().ToString(), "productName", ProductType.None, Guid.NewGuid().ToString(), 10.0, false); + Assert.That(() => product.Validate(), Throws.TypeOf()); + } + + [Test] + public void ManufacturerIdIsNullOrEmptyTest() + { + var product = CreateDataModel(Guid.NewGuid().ToString(), "productName", ProductType.Furniture, null, 10.0, false); + Assert.That(() => product.Validate(), Throws.TypeOf()); + + product = CreateDataModel(Guid.NewGuid().ToString(), "productName", ProductType.Furniture, string.Empty, 10.0, false); + Assert.That(() => product.Validate(), Throws.TypeOf()); + } + + [Test] + public void ManufacturerIdIsNotGuidTest() + { + var product = CreateDataModel(Guid.NewGuid().ToString(), "productName", ProductType.Furniture, "invalid-manufacturer-id", 10.0, false); + Assert.That(() => product.Validate(), Throws.TypeOf()); + } + + [Test] + public void PriceIsLessOrZeroTest() + { + var product = CreateDataModel(Guid.NewGuid().ToString(), "productName", ProductType.Furniture, Guid.NewGuid().ToString(), 0, false); + Assert.That(() => product.Validate(), Throws.TypeOf()); + + product = CreateDataModel(Guid.NewGuid().ToString(), "productName", ProductType.Furniture, Guid.NewGuid().ToString(), -10.0, false); + Assert.That(() => product.Validate(), Throws.TypeOf()); + } + + [Test] + public void AllFieldsIsCorrectTest() + { + var productId = Guid.NewGuid().ToString(); + var productName = "productName"; + var productType = ProductType.Furniture; + var manufacturerId = Guid.NewGuid().ToString(); + var price = 10.0; + var isDeleted = false; + + var product = CreateDataModel(productId, productName, productType, manufacturerId, price, isDeleted); + + 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(manufacturerId)); + Assert.That(product.Price, Is.EqualTo(price)); + Assert.That(product.IsDeleted, Is.EqualTo(isDeleted)); + }); + } + + private static ProductDataModel CreateDataModel(string? id, string? productName, ProductType productType, string? manufacturerId, double price, bool isDeleted) => + new(id, productName, productType, manufacturerId, price, isDeleted); +}