lab1-hard merged to lab2-hard-no-logic

This commit is contained in:
2025-03-11 20:45:10 +04:00
parent 6ad09c5ff2
commit 514f2adefb
10 changed files with 566 additions and 1 deletions

View File

@@ -114,8 +114,13 @@
<Compile Include="DataModels\ProductDataModel.cs" />
<Compile Include="DataModels\RecipeDataModel.cs" />
<Compile Include="DataModels\SalaryDataModel.cs" />
<Compile Include="DataModels\StorageDataModel.cs" />
<Compile Include="DataModels\StorageIngredientDataModel.cs" />
<Compile Include="DataModels\SupplyDataModel.cs" />
<Compile Include="DataModels\SupplyIngredientDataModel.cs" />
<Compile Include="Enums\PositionType.cs" />
<Compile Include="Enums\StatusType.cs" />
<Compile Include="Enums\StorageType.cs" />
<Compile Include="Exceptions\DateTimeExtensions.cs" />
<Compile Include="Exceptions\ElementExistsException.cs" />
<Compile Include="Exceptions\ElementNotFoundException.cs" />
@@ -143,7 +148,6 @@
<Compile Include="Interfaces\StoragesContracts\IPositionStorageContact.cs" />
<Compile Include="Interfaces\StoragesContracts\IProductStorageContact.cs" />
<Compile Include="Interfaces\StoragesContracts\ISalaryStorageContact.cs" />
<Compile Include="Enums\StorageType.cs" />
<Compile Include="Program.cs" />
</ItemGroup>
<ItemGroup>

View File

@@ -0,0 +1,29 @@
using CandyHouseBase.Exceptions;
using CandyHouseBase.Extensions;
using CandyHouseBase.Infrastructure;
namespace CandyHouseBase.DataModels
{
public class StorageIngredientDataModel : IValidation
{
public string StorageId { get; set; }
public string IngredientId { get; set; }
public int Quantity { get; set; }
public StorageIngredientDataModel(string storageId, string ingredientId, int quantity)
{
StorageId = storageId;
IngredientId = ingredientId;
Quantity = quantity;
}
public void Validate()
{
if (StorageId.IsEmpty()) throw new ValidationException("Field Id is empty");
if (!StorageId.IsGuid()) throw new ValidationException("Field Id is not a valid GUID");
if (IngredientId.IsEmpty()) throw new ValidationException("Field IngredientId is empty");
if (!IngredientId.IsGuid()) throw new ValidationException("Field IngredientId is not a valid GUID");
if (Quantity <= 0) throw new ValidationException("Field Quantity must be greater than zero");
}
}
}

View File

@@ -0,0 +1,39 @@
using System;
using System.Collections.Generic;
using CandyHouseBase.Exceptions;
using CandyHouseBase.Extensions;
using CandyHouseBase.Infrastructure;
namespace CandyHouseBase.DataModels
{
public class SupplyDataModel : IValidation
{
public string Id { get; set; }
public DateTime Date { get; set; }
public int TotalQuantity { get; set; }
public decimal TotalPrice { get; set; }
public List<IngredientDataModel> Ingredients { get; set; }
public SupplyDataModel(string id, DateTime date, int quantity, decimal totalPrice,
List<IngredientDataModel> ingredients)
{
Id = id;
Date = date;
TotalQuantity = quantity;
TotalPrice = totalPrice;
Ingredients = ingredients;
}
public void Validate()
{
if (Id.IsEmpty()) throw new ValidationException("Supply ID cannot be null or empty");
if (!Id.IsGuid()) throw new ValidationException("Supply ID must be a valid GUID");
if (Date == default) throw new ValidationException("Supply date cannot be null");
if (TotalQuantity <= 0) throw new ValidationException("Supply quantity must be greater than zero");
if (TotalPrice < 0) throw new ValidationException("Supply total price cannot be negative");
if (Ingredients == null || Ingredients.Count == 0)
throw new ValidationException("Supply must have at least one ingredient");
}
}
}

View File

@@ -0,0 +1,29 @@
using CandyHouseBase.Exceptions;
using CandyHouseBase.Extensions;
using CandyHouseBase.Infrastructure;
namespace CandyHouseBase.DataModels
{
public class SupplyIngredientDataModel : IValidation
{
public string SupplyId { get; set; }
public string IngredientId { get; set; }
public int Quantity { get; set; }
public SupplyIngredientDataModel(string supplyId, string ingredientId, int quantity)
{
SupplyId = supplyId;
IngredientId = ingredientId;
Quantity = quantity;
}
public void Validate()
{
if (SupplyId.IsEmpty()) throw new ValidationException("Supply ID cannot be null or empty");
if (!SupplyId.IsGuid()) throw new ValidationException("Supply ID is not a valid GUID");
if (IngredientId.IsEmpty()) throw new ValidationException("Ingredient ID cannot be null or empty");
if (!IngredientId.IsGuid()) throw new ValidationException("Ingredient ID is not a valid GUID");
if (Quantity <= 0) throw new ValidationException("Quantity must be a positive number");
}
}
}

View File

@@ -0,0 +1,9 @@
namespace CandyHouseBase.Enums
{
public enum StorageType
{
Fridge,
Shelf,
Cabinet
}
}

View File

@@ -162,6 +162,10 @@
<Compile Include="DataModelsTests\ProductDataModelTests.cs" />
<Compile Include="DataModelsTests\RecipeDataModelTests.cs" />
<Compile Include="DataModelsTests\SalaryDataModelTests.cs" />
<Compile Include="DataModelsTests\StorageDataModelTests.cs" />
<Compile Include="DataModelsTests\StorageIngredientDataModelTests.cs" />
<Compile Include="DataModelsTests\SupplyDataModelTests.cs" />
<Compile Include="DataModelsTests\SupplyIngredientDataModelTests.cs" />
</ItemGroup>
<ItemGroup>
<None Include="App.config" />

View File

@@ -0,0 +1,92 @@
using System;
using System.Collections.Generic;
using NUnit.Framework;
using CandyHouseBase.DataModels;
using CandyHouseBase.Enums;
using CandyHouseBase.Exceptions;
namespace CandyHouseTests.DataModelsTests
{
[TestFixture]
public class StorageDataModelTests
{
[Test]
public void CreateStorageDataModel_ValidData_ShouldCreateSuccessfully()
{
var id = Guid.NewGuid().ToString();
var title = "Main Warehouse";
var storageType = StorageType.Fridge;
var ingredients = new List<IngredientDataModel>
{
new IngredientDataModel(Guid.NewGuid().ToString(), "Sugar", "kg", 10)
};
var storage = new StorageDataModel(id, title, storageType, ingredients);
Assert.AreEqual(id, storage.Id);
Assert.AreEqual(title, storage.Title);
Assert.AreEqual(storageType, storage.StorageType);
Assert.AreEqual(ingredients, storage.Ingredients);
}
[Test]
public void Validate_ValidData_ShouldNotThrowException()
{
var storage = new StorageDataModel(
Guid.NewGuid().ToString(),
"Main Warehouse",
StorageType.Fridge,
new List<IngredientDataModel>());
Assert.DoesNotThrow(() => storage.Validate());
}
[Test]
public void Validate_EmptyId_ShouldThrowValidationException()
{
var storage = new StorageDataModel(
"",
"Main Warehouse",
StorageType.Fridge,
new List<IngredientDataModel>());
Assert.Throws<ValidationException>(() => storage.Validate());
}
[Test]
public void Validate_InvalidIdFormat_ShouldThrowValidationException()
{
var storage = new StorageDataModel(
"invalid-guid",
"Main Warehouse",
StorageType.Fridge,
new List<IngredientDataModel>());
Assert.Throws<ValidationException>(() => storage.Validate());
}
[Test]
public void Validate_EmptyTitle_ShouldThrowValidationException()
{
var storage = new StorageDataModel(
Guid.NewGuid().ToString(),
"",
StorageType.Fridge,
new List<IngredientDataModel>());
Assert.Throws<ValidationException>(() => storage.Validate());
}
[Test]
public void Validate_InvalidStorageType_ShouldThrowValidationException()
{
var storage = new StorageDataModel(
Guid.NewGuid().ToString(),
"Main Warehouse",
(StorageType)999,
new List<IngredientDataModel>());
Assert.Throws<ValidationException>(() => storage.Validate());
}
}
}

View File

@@ -0,0 +1,102 @@
using System;
using NUnit.Framework;
using CandyHouseBase.DataModels;
using CandyHouseBase.Exceptions;
namespace CandyHouseTests.DataModelsTests
{
[TestFixture]
public class StorageIngredientDataModelTests
{
[Test]
public void CreateStorageIngredientDataModel_ValidData_ShouldCreateSuccessfully()
{
var storageId = Guid.NewGuid().ToString();
var ingredientId = Guid.NewGuid().ToString();
var quantity = 5;
var storageIngredient = new StorageIngredientDataModel(storageId, ingredientId, quantity);
Assert.AreEqual(storageId, storageIngredient.StorageId);
Assert.AreEqual(ingredientId, storageIngredient.IngredientId);
Assert.AreEqual(quantity, storageIngredient.Quantity);
}
[Test]
public void Validate_ValidData_ShouldNotThrowException()
{
var storageIngredient = new StorageIngredientDataModel(
Guid.NewGuid().ToString(),
Guid.NewGuid().ToString(),
10);
Assert.DoesNotThrow(() => storageIngredient.Validate());
}
[Test]
public void Validate_EmptyStorageId_ShouldThrowValidationException()
{
var storageIngredient = new StorageIngredientDataModel(
"",
Guid.NewGuid().ToString(),
10);
Assert.Throws<ValidationException>(() => storageIngredient.Validate());
}
[Test]
public void Validate_InvalidStorageIdFormat_ShouldThrowValidationException()
{
var storageIngredient = new StorageIngredientDataModel(
"invalid-guid",
Guid.NewGuid().ToString(),
10);
Assert.Throws<ValidationException>(() => storageIngredient.Validate());
}
[Test]
public void Validate_EmptyIngredientId_ShouldThrowValidationException()
{
var storageIngredient = new StorageIngredientDataModel(
Guid.NewGuid().ToString(),
"",
10);
Assert.Throws<ValidationException>(() => storageIngredient.Validate());
}
[Test]
public void Validate_InvalidIngredientIdFormat_ShouldThrowValidationException()
{
var storageIngredient = new StorageIngredientDataModel(
Guid.NewGuid().ToString(),
"not-a-guid",
10);
Assert.Throws<ValidationException>(() => storageIngredient.Validate());
}
[Test]
public void Validate_NonPositiveQuantity_ShouldThrowValidationException()
{
var storageIngredient = new StorageIngredientDataModel(
Guid.NewGuid().ToString(),
Guid.NewGuid().ToString(),
0);
Assert.Throws<ValidationException>(() => storageIngredient.Validate());
}
[Test]
public void Validate_NegativeQuantity_ShouldThrowValidationException()
{
var storageIngredient = new StorageIngredientDataModel(
Guid.NewGuid().ToString(),
Guid.NewGuid().ToString(),
-5);
Assert.Throws<ValidationException>(() => storageIngredient.Validate());
}
}
}

View File

@@ -0,0 +1,155 @@
using System;
using System.Collections.Generic;
using NUnit.Framework;
using CandyHouseBase.DataModels;
using CandyHouseBase.Exceptions;
namespace CandyHouseTests.DataModelsTests
{
[TestFixture]
public class SupplyDataModelTests
{
[Test]
public void CreateSupplyDataModel_ValidData_ShouldCreateSuccessfully()
{
var id = Guid.NewGuid().ToString();
var date = DateTime.Now;
var totalQuantity = 100;
var totalPrice = 150.75m;
var ingredients = new List<IngredientDataModel>
{
new IngredientDataModel(Guid.NewGuid().ToString(), "Sugar", "kg", 10)
};
var supply = new SupplyDataModel(id, date, totalQuantity, totalPrice, ingredients);
Assert.AreEqual(id, supply.Id);
Assert.AreEqual(date, supply.Date);
Assert.AreEqual(totalQuantity, supply.TotalQuantity);
Assert.AreEqual(totalPrice, supply.TotalPrice);
Assert.AreEqual(ingredients, supply.Ingredients);
}
[Test]
public void Validate_ValidData_ShouldNotThrowException()
{
var supply = new SupplyDataModel(
Guid.NewGuid().ToString(),
DateTime.Now,
50,
75.25m,
new List<IngredientDataModel>
{
new IngredientDataModel(Guid.NewGuid().ToString(), "Sugar", "kg", 10)
});
Assert.DoesNotThrow(() => supply.Validate());
}
[Test]
public void Validate_EmptyId_ShouldThrowValidationException()
{
var supply = new SupplyDataModel(
"",
DateTime.Now,
50,
75.25m,
new List<IngredientDataModel>
{
new IngredientDataModel(Guid.NewGuid().ToString(), "Sugar", "kg", 10)
});
Assert.Throws<ValidationException>(() => supply.Validate());
}
[Test]
public void Validate_InvalidIdFormat_ShouldThrowValidationException()
{
var supply = new SupplyDataModel(
"not-a-guid",
DateTime.Now,
50,
75.25m,
new List<IngredientDataModel>
{
new IngredientDataModel(Guid.NewGuid().ToString(), "Sugar", "kg", 10)
});
Assert.Throws<ValidationException>(() => supply.Validate());
}
[Test]
public void Validate_DefaultDate_ShouldThrowValidationException()
{
var supply = new SupplyDataModel(
Guid.NewGuid().ToString(),
default(DateTime),
50,
75.25m,
new List<IngredientDataModel>
{
new IngredientDataModel(Guid.NewGuid().ToString(), "Sugar", "kg", 10)
});
Assert.Throws<ValidationException>(() => supply.Validate());
}
[Test]
public void Validate_NonPositiveQuantity_ShouldThrowValidationException()
{
var supply = new SupplyDataModel(
Guid.NewGuid().ToString(),
DateTime.Now,
0,
75.25m,
new List<IngredientDataModel>
{
new IngredientDataModel(Guid.NewGuid().ToString(), "Sugar", "kg", 10)
});
Assert.Throws<ValidationException>(() => supply.Validate());
}
[Test]
public void Validate_NegativeTotalPrice_ShouldThrowValidationException()
{
var supply = new SupplyDataModel(
Guid.NewGuid().ToString(),
DateTime.Now,
50,
-10.50m,
new List<IngredientDataModel>
{
new IngredientDataModel(Guid.NewGuid().ToString(), "Sugar", "kg", 10)
});
Assert.Throws<ValidationException>(() => supply.Validate());
}
[Test]
public void Validate_NullIngredients_ShouldThrowValidationException()
{
var supply = new SupplyDataModel(
Guid.NewGuid().ToString(),
DateTime.Now,
50,
75.25m,
null);
Assert.Throws<ValidationException>(() => supply.Validate());
}
[Test]
public void Validate_EmptyIngredients_ShouldThrowValidationException()
{
var supply = new SupplyDataModel(
Guid.NewGuid().ToString(),
DateTime.Now,
50,
75.25m,
new List<IngredientDataModel>());
Assert.Throws<ValidationException>(() => supply.Validate());
}
}
}

View File

@@ -0,0 +1,102 @@
using System;
using NUnit.Framework;
using CandyHouseBase.DataModels;
using CandyHouseBase.Exceptions;
namespace CandyHouseTests.DataModelsTests
{
[TestFixture]
public class SupplyIngredientDataModelTests
{
[Test]
public void CreateSupplyIngredientDataModel_ValidData_ShouldCreateSuccessfully()
{
var supplyId = Guid.NewGuid().ToString();
var ingredientId = Guid.NewGuid().ToString();
var quantity = 10;
var supplyIngredient = new SupplyIngredientDataModel(supplyId, ingredientId, quantity);
Assert.AreEqual(supplyId, supplyIngredient.SupplyId);
Assert.AreEqual(ingredientId, supplyIngredient.IngredientId);
Assert.AreEqual(quantity, supplyIngredient.Quantity);
}
[Test]
public void Validate_ValidData_ShouldNotThrowException()
{
var supplyIngredient = new SupplyIngredientDataModel(
Guid.NewGuid().ToString(),
Guid.NewGuid().ToString(),
5);
Assert.DoesNotThrow(() => supplyIngredient.Validate());
}
[Test]
public void Validate_EmptySupplyId_ShouldThrowValidationException()
{
var supplyIngredient = new SupplyIngredientDataModel(
"",
Guid.NewGuid().ToString(),
5);
Assert.Throws<ValidationException>(() => supplyIngredient.Validate());
}
[Test]
public void Validate_InvalidSupplyIdFormat_ShouldThrowValidationException()
{
var supplyIngredient = new SupplyIngredientDataModel(
"invalid-guid",
Guid.NewGuid().ToString(),
5);
Assert.Throws<ValidationException>(() => supplyIngredient.Validate());
}
[Test]
public void Validate_EmptyIngredientId_ShouldThrowValidationException()
{
var supplyIngredient = new SupplyIngredientDataModel(
Guid.NewGuid().ToString(),
"",
5);
Assert.Throws<ValidationException>(() => supplyIngredient.Validate());
}
[Test]
public void Validate_InvalidIngredientIdFormat_ShouldThrowValidationException()
{
var supplyIngredient = new SupplyIngredientDataModel(
Guid.NewGuid().ToString(),
"not-a-guid",
5);
Assert.Throws<ValidationException>(() => supplyIngredient.Validate());
}
[Test]
public void Validate_NonPositiveQuantity_ShouldThrowValidationException()
{
var supplyIngredient = new SupplyIngredientDataModel(
Guid.NewGuid().ToString(),
Guid.NewGuid().ToString(),
0);
Assert.Throws<ValidationException>(() => supplyIngredient.Validate());
}
[Test]
public void Validate_NegativeQuantity_ShouldThrowValidationException()
{
var supplyIngredient = new SupplyIngredientDataModel(
Guid.NewGuid().ToString(),
Guid.NewGuid().ToString(),
-3);
Assert.Throws<ValidationException>(() => supplyIngredient.Validate());
}
}
}