2024-10-27 16:24:12 +00:00
|
|
|
import pytest
|
|
|
|
|
2024-09-01 15:39:04 +00:00
|
|
|
from decimal import Decimal
|
|
|
|
|
|
|
|
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))
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
def test_div_asset():
|
|
|
|
assert USD_20 / USD_10 == 2
|
|
|
|
assert USD_10 / USD_10 == 1
|
|
|
|
assert USD_10 / USD_20 == 0.5
|
|
|
|
|
|
|
|
def test_div_amount():
|
|
|
|
assert USD_20 / Decimal(2) == USD_10
|
|
|
|
|
|
|
|
def test_mult():
|
|
|
|
assert USD_10 * Decimal(2) == USD_20
|
|
|
|
|
|
|
|
def test_negate():
|
|
|
|
assert USD_20 + (-USD_10) == USD_10
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
def test_is_zero():
|
|
|
|
assert USD_0.is_zero()
|
|
|
|
assert not USD_10.is_zero()
|
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'
|
|
|
|
|
|
|
|
def test_mul_wrong():
|
|
|
|
with pytest.raises(TypeError, match='other must be Decimal, but was AssetAmount'):
|
|
|
|
assert USD_20 * USD_10
|
|
|
|
|
|
|
|
def test_add_wrong_type():
|
|
|
|
with pytest.raises(TypeError, match='other must be AssetAmount, but was Decimal'):
|
|
|
|
assert USD_20 + Decimal(1)
|
|
|
|
|
|
|
|
def test_add_wrong_asset():
|
|
|
|
with pytest.raises(ValueError, match='AssetAmount must have same asset, but: fiat:USD != fiat:DKK'):
|
|
|
|
assert USD_20 + DKK_21
|
|
|
|
|
|
|
|
def test_sub_wrong_type():
|
|
|
|
with pytest.raises(TypeError, match='other must be AssetAmount, but was Decimal'):
|
|
|
|
assert USD_20 - Decimal(1)
|
|
|
|
|
|
|
|
def test_sub_wrong_asset():
|
|
|
|
with pytest.raises(ValueError, match='AssetAmount must have same asset, but: fiat:USD != fiat:DKK'):
|
|
|
|
assert USD_20 - DKK_21
|
|
|
|
|
|
|
|
def test_cmp_wrong_type():
|
|
|
|
with pytest.raises(TypeError, match='other must be AssetAmount, but was Decimal'):
|
|
|
|
assert USD_20 < Decimal(1)
|
|
|
|
|
|
|
|
def test_cmp_wrong_asset():
|
|
|
|
with pytest.raises(ValueError, match='AssetAmount must have same asset, but: fiat:USD != fiat:DKK'):
|
|
|
|
assert USD_20 < DKK_21
|
|
|
|
|
|
|
|
def test_div_wrong_asset ():
|
|
|
|
with pytest.raises(ValueError, match='AssetAmount must have same asset, but: fiat:USD != fiat:DKK'):
|
|
|
|
assert USD_21 / DKK_21
|