feat: add Price value object extending Money
This commit is contained in:
10
domain/marketplace/price.py
Normal file
10
domain/marketplace/price.py
Normal 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
31
tests/test_price.py
Normal 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
|
||||
Reference in New Issue
Block a user