1
0
fin-defs/test/test_asset_amount.py

119 lines
2.8 KiB
Python
Raw Normal View History

2024-09-01 15:39:04 +00:00
from decimal import Decimal
2024-10-27 16:24:26 +00:00
import pytest
2024-09-01 15:39:04 +00:00
import fin_defs
2024-10-27 16:24:12 +00:00
USD_0 = fin_defs.AssetAmount(fin_defs.USD, Decimal(0))
USD_10 = fin_defs.AssetAmount(fin_defs.USD, Decimal(10))
USD_11 = fin_defs.AssetAmount(fin_defs.USD, Decimal(11))
USD_20 = fin_defs.AssetAmount(fin_defs.USD, Decimal(20))
USD_21 = fin_defs.AssetAmount(fin_defs.USD, Decimal(21))
DKK_0 = fin_defs.AssetAmount(fin_defs.DKK, Decimal(0))
DKK_21 = fin_defs.AssetAmount(fin_defs.DKK, Decimal(21))
2024-10-27 16:24:26 +00:00
2024-10-27 16:24:12 +00:00
def test_add_asset():
assert USD_10 + USD_11 == USD_21
assert USD_10 + USD_0 == USD_10
assert DKK_0 + USD_10 == USD_10
2024-10-27 16:24:26 +00:00
2024-10-27 16:24:12 +00:00
def test_sub_asset():
assert USD_21 - USD_10 == USD_11
assert USD_10 - USD_0 == USD_10
assert DKK_0 - USD_10 == -USD_10
2024-10-27 16:24:26 +00:00
2024-10-27 16:24:12 +00:00
def test_div_asset():
assert USD_20 / USD_10 == 2
assert USD_10 / USD_10 == 1
assert USD_10 / USD_20 == 0.5
2024-10-27 16:24:26 +00:00
2024-10-27 16:24:12 +00:00
def test_div_amount():
assert USD_20 / Decimal(2) == USD_10
2024-10-27 16:24:26 +00:00
2024-10-27 16:24:12 +00:00
def test_mult():
assert USD_10 * Decimal(2) == USD_20
2024-10-27 16:24:26 +00:00
2024-10-27 16:24:12 +00:00
def test_negate():
assert USD_20 + (-USD_10) == USD_10
2024-10-27 16:24:26 +00:00
2024-10-27 16:24:12 +00:00
def test_compare():
assert USD_10 < USD_20
assert USD_10 <= USD_20
assert not (USD_10 > USD_20)
assert not (USD_10 >= USD_20)
2024-10-27 16:24:26 +00:00
2024-10-27 16:24:12 +00:00
def test_compare_zero():
assert DKK_0 < USD_20
assert DKK_0 <= USD_20
assert not (DKK_0 > USD_20)
assert not (DKK_0 >= USD_20)
2024-10-27 16:24:26 +00:00
2024-10-27 16:24:12 +00:00
def test_is_zero():
assert USD_0.is_zero()
assert not USD_10.is_zero()
2024-09-01 15:39:04 +00:00
2024-10-27 16:24:26 +00:00
2024-09-01 15:39:04 +00:00
def test_str():
2024-10-27 16:24:12 +00:00
assert str(USD_0) == '$0.000 USD'
assert str(USD_10) == '$10.00 USD'
assert str(USD_11) == '$11.00 USD'
assert str(-USD_10) == '-$10.00 USD'
2024-10-27 16:24:26 +00:00
2024-10-27 16:24:12 +00:00
def test_mul_wrong():
with pytest.raises(TypeError, match='other must be Decimal, but was AssetAmount'):
assert USD_20 * USD_10
2024-10-27 16:24:26 +00:00
2024-10-27 16:24:12 +00:00
def test_add_wrong_type():
with pytest.raises(TypeError, match='other must be AssetAmount, but was Decimal'):
assert USD_20 + Decimal(1)
2024-10-27 16:24:26 +00:00
2024-10-27 16:24:12 +00:00
def test_add_wrong_asset():
2024-10-27 16:24:26 +00:00
with pytest.raises(
ValueError, match='AssetAmount must have same asset, but: fiat:USD != fiat:DKK',
):
2024-10-27 16:24:12 +00:00
assert USD_20 + DKK_21
2024-10-27 16:24:26 +00:00
2024-10-27 16:24:12 +00:00
def test_sub_wrong_type():
with pytest.raises(TypeError, match='other must be AssetAmount, but was Decimal'):
assert USD_20 - Decimal(1)
2024-10-27 16:24:26 +00:00
2024-10-27 16:24:12 +00:00
def test_sub_wrong_asset():
2024-10-27 16:24:26 +00:00
with pytest.raises(
ValueError, match='AssetAmount must have same asset, but: fiat:USD != fiat:DKK',
):
2024-10-27 16:24:12 +00:00
assert USD_20 - DKK_21
2024-10-27 16:24:26 +00:00
2024-10-27 16:24:12 +00:00
def test_cmp_wrong_type():
with pytest.raises(TypeError, match='other must be AssetAmount, but was Decimal'):
2024-10-27 16:24:26 +00:00
assert Decimal(1) > USD_20
2024-10-27 16:24:12 +00:00
def test_cmp_wrong_asset():
2024-10-27 16:24:26 +00:00
with pytest.raises(
ValueError, match='AssetAmount must have same asset, but: fiat:USD != fiat:DKK',
):
2024-10-27 16:24:12 +00:00
assert USD_20 < DKK_21
2024-10-27 16:24:26 +00:00
def test_div_wrong_asset():
with pytest.raises(
ValueError, match='AssetAmount must have same asset, but: fiat:USD != fiat:DKK',
):
2024-10-27 16:24:12 +00:00
assert USD_21 / DKK_21