feat: add Price value object extending Money

This commit is contained in:
2026-07-26 07:53:43 -04:00
parent 3569270690
commit 085cceb763
2 changed files with 41 additions and 0 deletions

View File

@@ -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}")

31
tests/test_price.py Normal file
View File

@@ -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