194 lines
5.0 KiB
C#
194 lines
5.0 KiB
C#
using MyBiz.Core;
|
|
|
|
namespace MyBiz.Tests;
|
|
|
|
public class ProductTypeTests
|
|
{
|
|
[Fact]
|
|
public void ProductType_Creation_ShouldInitializeProperties()
|
|
{
|
|
// Arrange & Act
|
|
var productType = new ProductType
|
|
{
|
|
Id = "food_bread",
|
|
Name = "Bread",
|
|
Description = "Fresh baked bread",
|
|
Category = ProductCategory.ConsumerGoods,
|
|
BasePrice = 10m,
|
|
BaseDemand = 100,
|
|
AvailableFromYear = 1950
|
|
};
|
|
|
|
// Assert
|
|
Assert.Equal("food_bread", productType.Id);
|
|
Assert.Equal("Bread", productType.Name);
|
|
Assert.Equal(ProductCategory.ConsumerGoods, productType.Category);
|
|
Assert.Equal(10m, productType.BasePrice);
|
|
Assert.Equal(1950, productType.AvailableFromYear);
|
|
Assert.False(productType.IsPerishable);
|
|
}
|
|
|
|
[Fact]
|
|
public void ProductType_Clone_ShouldCreateIndependentCopy()
|
|
{
|
|
// Arrange
|
|
var original = new ProductType
|
|
{
|
|
Id = "test",
|
|
Name = "Test Product",
|
|
BasePrice = 50m
|
|
};
|
|
original.RequiredTechnologies.Add("tech1");
|
|
|
|
// Act
|
|
var clone = original.Clone();
|
|
clone.Name = "Modified";
|
|
clone.RequiredTechnologies.Add("tech2");
|
|
|
|
// Assert
|
|
Assert.Equal("Test Product", original.Name);
|
|
Assert.Equal("Modified", clone.Name);
|
|
Assert.Single(original.RequiredTechnologies);
|
|
Assert.Equal(2, clone.RequiredTechnologies.Count);
|
|
}
|
|
}
|
|
|
|
public class ProductTests
|
|
{
|
|
[Fact]
|
|
public void Product_Add_ShouldIncreaseQuantity()
|
|
{
|
|
// Arrange
|
|
var product = new Product
|
|
{
|
|
Type = new ProductType { Id = "test", BasePrice = 10m },
|
|
Quantity = 5
|
|
};
|
|
|
|
// Act
|
|
product.Add(10);
|
|
|
|
// Assert
|
|
Assert.Equal(15, product.Quantity);
|
|
}
|
|
|
|
[Fact]
|
|
public void Product_Remove_ShouldDecreaseQuantity()
|
|
{
|
|
// Arrange
|
|
var product = new Product
|
|
{
|
|
Type = new ProductType { Id = "test", BasePrice = 10m },
|
|
Quantity = 20
|
|
};
|
|
|
|
// Act
|
|
var removed = product.Remove(15);
|
|
|
|
// Assert
|
|
Assert.Equal(15, removed);
|
|
Assert.Equal(5, product.Quantity);
|
|
}
|
|
|
|
[Fact]
|
|
public void Product_Remove_MoreThanAvailable_ShouldRemoveAll()
|
|
{
|
|
// Arrange
|
|
var product = new Product
|
|
{
|
|
Type = new ProductType { Id = "test", BasePrice = 10m },
|
|
Quantity = 10
|
|
};
|
|
|
|
// Act
|
|
var removed = product.Remove(50);
|
|
|
|
// Assert
|
|
Assert.Equal(10, removed);
|
|
Assert.Equal(0, product.Quantity);
|
|
Assert.True(product.IsSpoiled);
|
|
}
|
|
|
|
[Fact]
|
|
public void Product_Perishable_ShouldSpoilAfterShelfLife()
|
|
{
|
|
// Arrange
|
|
var product = new Product
|
|
{
|
|
Type = new ProductType { Id = "food", BasePrice = 10m, ShelfLife = 5 },
|
|
Quantity = 10,
|
|
CreatedAtTick = 0
|
|
};
|
|
|
|
// Act
|
|
product.Update(6); // 6 ticks later
|
|
|
|
// Assert
|
|
Assert.True(product.IsSpoiled);
|
|
Assert.False(product.IsUsable);
|
|
}
|
|
}
|
|
|
|
public class ProductRegistryTests
|
|
{
|
|
[Fact]
|
|
public void Registry_Register_ShouldAddProductType()
|
|
{
|
|
// Arrange
|
|
var registry = new ProductRegistry();
|
|
var productType = new ProductType { Id = "test", Name = "Test" };
|
|
|
|
// Act
|
|
registry.Register(productType);
|
|
|
|
// Assert
|
|
Assert.Equal(1, registry.Count);
|
|
Assert.Same(productType, registry.GetById("test"));
|
|
}
|
|
|
|
[Fact]
|
|
public void Registry_GetById_UnknownId_ShouldReturnNull()
|
|
{
|
|
// Arrange
|
|
var registry = new ProductRegistry();
|
|
|
|
// Act
|
|
var result = registry.GetById("unknown");
|
|
|
|
// Assert
|
|
Assert.Null(result);
|
|
}
|
|
|
|
[Fact]
|
|
public void Registry_GetByCategory_ShouldFilterProducts()
|
|
{
|
|
// Arrange
|
|
var registry = new ProductRegistry();
|
|
registry.Register(new ProductType { Id = "raw1", Category = ProductCategory.RawMaterial });
|
|
registry.Register(new ProductType { Id = "raw2", Category = ProductCategory.RawMaterial });
|
|
registry.Register(new ProductType { Id = "consumer1", Category = ProductCategory.ConsumerGoods });
|
|
|
|
// Act
|
|
var rawMaterials = registry.GetByCategory(ProductCategory.RawMaterial);
|
|
|
|
// Assert
|
|
Assert.Equal(2, rawMaterials.Count());
|
|
}
|
|
|
|
[Fact]
|
|
public void Registry_GetAvailableInYear_ShouldFilterByYear()
|
|
{
|
|
// Arrange
|
|
var registry = new ProductRegistry();
|
|
registry.Register(new ProductType { Id = "old", AvailableFromYear = 1950 });
|
|
registry.Register(new ProductType { Id = "new", AvailableFromYear = 2000 });
|
|
|
|
// Act
|
|
var availableIn1970 = registry.GetAvailableInYear(1970);
|
|
|
|
// Assert
|
|
Assert.Single(availableIn1970);
|
|
Assert.Equal("old", availableIn1970.First().Id);
|
|
}
|
|
}
|