63 lines
1.5 KiB
Python
63 lines
1.5 KiB
Python
from decimal import Decimal
|
|
|
|
from domain.marketplace.money import Money
|
|
|
|
|
|
def test_money_creation():
|
|
money = Money(amount=Decimal("99.99"))
|
|
assert money.amount == Decimal("99.99")
|
|
|
|
|
|
def test_money_is_immutable():
|
|
money = Money(amount=Decimal("10.00"))
|
|
|
|
try:
|
|
money.amount = Decimal("20.00") # type: ignore
|
|
assert False, "Should have raised FrozenInstanceError"
|
|
except AttributeError:
|
|
pass
|
|
|
|
|
|
def test_money_equality():
|
|
money1 = Money(amount=Decimal("50.00"))
|
|
money2 = Money(amount=Decimal("50.00"))
|
|
assert money1 == money2
|
|
|
|
|
|
def test_money_add():
|
|
money1 = Money(amount=Decimal("2.00"))
|
|
money2 = Money(amount=Decimal("3.00"))
|
|
|
|
assert (money1 + money2) == Money(amount=Decimal("5.00"))
|
|
assert money1 == Money(amount=Decimal("2.00"))
|
|
assert money2 == Money(amount=Decimal("3.00"))
|
|
|
|
|
|
def test_money_substract():
|
|
money1 = Money(amount=Decimal("5.00"))
|
|
money2 = Money(amount=Decimal("3.00"))
|
|
assert (money1 - money2) == Money(amount=Decimal("2.00"))
|
|
|
|
|
|
def test_money_can_not_have_more_than_two_decimals():
|
|
try:
|
|
Money(amount=Decimal("1.234"))
|
|
assert False, "Should have raised ValueError"
|
|
except ValueError:
|
|
pass
|
|
|
|
|
|
def test_money_allows_two_decimals():
|
|
money = Money(amount=Decimal("1.23"))
|
|
assert money.amount == Decimal("1.23")
|
|
|
|
|
|
def test_money_allows_one_decimal():
|
|
money = Money(amount=Decimal("1.2"))
|
|
assert money.amount == Decimal("1.2")
|
|
|
|
|
|
def test_money_allows_no_decimals():
|
|
money = Money(amount=Decimal(1))
|
|
assert money.amount == Decimal(1)
|