2024-06-27 16:55:43 +02:00
|
|
|
from decimal import Decimal
|
|
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
from money.currency import Currency
|
|
|
|
|
|
2024-06-27 17:18:43 +02:00
|
|
|
from xexe.money_amount import DEFAULT_MONEY_PRECISION, MoneyAmount
|
2024-06-27 16:55:43 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_money_amount_simple_creation_works():
|
|
|
|
|
|
|
|
|
|
an_amount = MoneyAmount(amount=10, currency=Currency.USD)
|
|
|
|
|
|
|
|
|
|
assert an_amount.amount == 10
|
|
|
|
|
assert an_amount.currency == Currency.USD
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_money_amount_takes_integer_amounts():
|
|
|
|
|
an_amount = MoneyAmount(amount=10, currency=Currency.USD)
|
|
|
|
|
|
|
|
|
|
assert an_amount.amount == 10
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_money_amount_takes_decimal_amounts():
|
|
|
|
|
an_amount = MoneyAmount(amount=Decimal(10.5), currency=Currency.USD)
|
|
|
|
|
|
2024-06-27 17:18:43 +02:00
|
|
|
assert an_amount.amount == Decimal(10.5).quantize(DEFAULT_MONEY_PRECISION)
|
2024-06-27 16:55:43 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_money_amount_takes_proper_strings_amounts():
|
|
|
|
|
an_amount = MoneyAmount(amount="10.55", currency=Currency.USD)
|
|
|
|
|
|
2024-06-27 17:18:43 +02:00
|
|
|
assert an_amount.amount == Decimal(10.55).quantize(DEFAULT_MONEY_PRECISION)
|
2024-06-27 16:55:43 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_money_amount_fails_with_ugly_strings():
|
|
|
|
|
with pytest.raises(ValueError):
|
|
|
|
|
MoneyAmount(amount="not a nuuuuumber", currency=Currency.USD)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_money_amount_takes_string_for_currency():
|
|
|
|
|
an_amount = MoneyAmount(amount="10.55", currency="USD")
|
|
|
|
|
|
|
|
|
|
assert an_amount.currency == Currency.USD
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_money_amount_works_with_8_decimal_positions():
|
|
|
|
|
an_amount = MoneyAmount(amount="1.12345678", currency="USD")
|
|
|
|
|
|
2024-06-27 17:18:43 +02:00
|
|
|
assert an_amount.amount == Decimal(1.12345678).quantize(DEFAULT_MONEY_PRECISION)
|