From 085cceb7637f919f2c20a93cc4994f6c59a9e49f Mon Sep 17 00:00:00 2001 From: Paul de Raaij Date: Sun, 26 Jul 2026 07:53:43 -0400 Subject: [PATCH] feat: add Price value object extending Money --- domain/marketplace/price.py | 10 ++++++++++ tests/test_price.py | 31 +++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+) create mode 100644 domain/marketplace/price.py create mode 100644 tests/test_price.py 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