1
0
fin-defs/test/test_asset_amount.py
Jon Michael Aanes a322770205
All checks were successful
Python Ruff Code Quality / ruff (push) Successful in 22s
Run Python tests (through Pytest) / Test (push) Successful in 25s
Verify Python project can be installed, loaded and have version checked / Test (push) Successful in 22s
Code quality
2024-10-27 17:26:03 +01:00

123 lines
2.9 KiB
Python

from decimal import Decimal
import pytest
import fin_defs
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()
def test_str():
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 Decimal(1) > USD_20
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