diff --git a/domain/marketplace/price.py b/domain/marketplace/price.py new file mode 100644 index 0000000..bf3ee57 --- /dev/null +++ b/domain/marketplace/price.py @@ -0,0 +1,10 @@ +from dataclasses import dataclass + +from domain.marketplace.money import Money + + +@dataclass(frozen=True) +class Price(Money): + def __post_init__(self): + if self.amount < 0: + raise ValueError(f"Price cannot be negative: {self.amount}") diff --git a/tests/test_price.py b/tests/test_price.py new file mode 100644 index 0000000..86f4fd0 --- /dev/null +++ b/tests/test_price.py @@ -0,0 +1,31 @@ +from decimal import Decimal + +import pytest + +from domain.marketplace.price import Price + + +def test_price_creation(): + price = Price(amount=Decimal("99.99")) + assert price.amount == Decimal("99.99") + + +def test_price_is_immutable(): + price = Price(amount=Decimal("10.00")) + + try: + price.amount = Decimal("20.00") # type: ignore + assert False, "Should have raised FrozenInstanceError" + except AttributeError: + pass + + +def test_price_can_not_be_negative(): + with pytest.raises(ValueError): + Price(amount=Decimal("-10.00")) + + +def test_price_equality(): + price1 = Price(amount=Decimal("50.00")) + price2 = Price(amount=Decimal("50.00")) + assert price1 == price2