Compare commits
2 Commits
b720fd04e7
...
42ec581aca
Author | SHA1 | Date | |
---|---|---|---|
42ec581aca | |||
ef7a0e8078 |
|
@ -506,6 +506,12 @@ class AssetInformation:
|
||||||
THREE_DECIMALS_UNDER_THIS_AMOUNT = 0.10
|
THREE_DECIMALS_UNDER_THIS_AMOUNT = 0.10
|
||||||
|
|
||||||
|
|
||||||
|
def assert_same_asset(do: str, a_asset: 'AssetAmount', b_asset: 'AssetAmount'):
|
||||||
|
if a_asset.asset != b_asset.asset:
|
||||||
|
msg = f'Attempting to {do} {a_asset} and {b_asset}, but these must have the same asset'
|
||||||
|
raise ValueError(msg)
|
||||||
|
|
||||||
|
|
||||||
@enforce_typing.enforce_types
|
@enforce_typing.enforce_types
|
||||||
@dataclasses.dataclass(frozen=True, eq=True, slots=True)
|
@dataclasses.dataclass(frozen=True, eq=True, slots=True)
|
||||||
class AssetAmount:
|
class AssetAmount:
|
||||||
|
@ -547,11 +553,7 @@ class AssetAmount:
|
||||||
return other
|
return other
|
||||||
if other.is_zero():
|
if other.is_zero():
|
||||||
return self
|
return self
|
||||||
if self.asset != other.asset:
|
assert_same_asset('add', self, other)
|
||||||
msg = (
|
|
||||||
f'AssetAmount must have same asset, but: {self.asset} != {other.asset}'
|
|
||||||
)
|
|
||||||
raise ValueError(msg)
|
|
||||||
|
|
||||||
return AssetAmount(self.asset, self.amount + other.amount)
|
return AssetAmount(self.asset, self.amount + other.amount)
|
||||||
|
|
||||||
|
@ -564,18 +566,12 @@ class AssetAmount:
|
||||||
return -other
|
return -other
|
||||||
if other.is_zero():
|
if other.is_zero():
|
||||||
return self
|
return self
|
||||||
if self.asset != other.asset:
|
assert_same_asset('subtract', self, other)
|
||||||
msg = (
|
|
||||||
f'AssetAmount must have same asset, but: {self.asset} != {other.asset}'
|
|
||||||
)
|
|
||||||
raise ValueError(msg)
|
|
||||||
return AssetAmount(self.asset, self.amount - other.amount)
|
return AssetAmount(self.asset, self.amount - other.amount)
|
||||||
|
|
||||||
def __truediv__(self, other: 'Decimal | AssetAmount') -> 'Decimal | AssetAmount':
|
def __truediv__(self, other: 'Decimal | AssetAmount') -> 'Decimal | AssetAmount':
|
||||||
if isinstance(other, AssetAmount):
|
if isinstance(other, AssetAmount):
|
||||||
if self.asset != other.asset:
|
assert_same_asset('divide', self, other)
|
||||||
msg = f'Attempting to divide {self} by {other}, but these must have the same asset'
|
|
||||||
raise ValueError(msg)
|
|
||||||
return self.amount / other.amount
|
return self.amount / other.amount
|
||||||
return AssetAmount(self.asset, self.amount / other)
|
return AssetAmount(self.asset, self.amount / other)
|
||||||
|
|
||||||
|
@ -588,11 +584,7 @@ class AssetAmount:
|
||||||
raise TypeError(msg)
|
raise TypeError(msg)
|
||||||
if self.is_zero() or other.is_zero():
|
if self.is_zero() or other.is_zero():
|
||||||
return self.amount - other.amount
|
return self.amount - other.amount
|
||||||
if self.asset != other.asset:
|
assert_same_asset('compare', self, other)
|
||||||
msg = (
|
|
||||||
f'AssetAmount must have same asset, but: {self.asset} != {other.asset}'
|
|
||||||
)
|
|
||||||
raise ValueError(msg)
|
|
||||||
return self.amount - other.amount
|
return self.amount - other.amount
|
||||||
|
|
||||||
def is_zero(self) -> bool:
|
def is_zero(self) -> bool:
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
import re
|
||||||
from decimal import Decimal
|
from decimal import Decimal
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
@ -83,7 +84,9 @@ def test_add_wrong_type():
|
||||||
def test_add_wrong_asset():
|
def test_add_wrong_asset():
|
||||||
with pytest.raises(
|
with pytest.raises(
|
||||||
ValueError,
|
ValueError,
|
||||||
match='AssetAmount must have same asset, but: fiat:USD != fiat:DKK',
|
match=re.escape(
|
||||||
|
'Attempting to add $20.00 USD and 21.00 DKK, but these must have the same asset',
|
||||||
|
),
|
||||||
):
|
):
|
||||||
assert USD_20 + DKK_21
|
assert USD_20 + DKK_21
|
||||||
|
|
||||||
|
@ -96,7 +99,9 @@ def test_sub_wrong_type():
|
||||||
def test_sub_wrong_asset():
|
def test_sub_wrong_asset():
|
||||||
with pytest.raises(
|
with pytest.raises(
|
||||||
ValueError,
|
ValueError,
|
||||||
match='AssetAmount must have same asset, but: fiat:USD != fiat:DKK',
|
match=re.escape(
|
||||||
|
'Attempting to subtract $20.00 USD and 21.00 DKK, but these must have the same asset',
|
||||||
|
),
|
||||||
):
|
):
|
||||||
assert USD_20 - DKK_21
|
assert USD_20 - DKK_21
|
||||||
|
|
||||||
|
@ -109,7 +114,9 @@ def test_cmp_wrong_type():
|
||||||
def test_cmp_wrong_asset():
|
def test_cmp_wrong_asset():
|
||||||
with pytest.raises(
|
with pytest.raises(
|
||||||
ValueError,
|
ValueError,
|
||||||
match='AssetAmount must have same asset, but: fiat:USD != fiat:DKK',
|
match=re.escape(
|
||||||
|
'Attempting to compare $20.00 USD and 21.00 DKK, but these must have the same asset',
|
||||||
|
),
|
||||||
):
|
):
|
||||||
assert USD_20 < DKK_21
|
assert USD_20 < DKK_21
|
||||||
|
|
||||||
|
@ -117,6 +124,8 @@ def test_cmp_wrong_asset():
|
||||||
def test_div_wrong_asset():
|
def test_div_wrong_asset():
|
||||||
with pytest.raises(
|
with pytest.raises(
|
||||||
ValueError,
|
ValueError,
|
||||||
match='AssetAmount must have same asset, but: fiat:USD != fiat:DKK',
|
match=re.escape(
|
||||||
|
'Attempting to divide $21.00 USD and 21.00 DKK, but these must have the same asset',
|
||||||
|
),
|
||||||
):
|
):
|
||||||
assert USD_21 / DKK_21
|
assert USD_21 / DKK_21
|
||||||
|
|
Loading…
Reference in New Issue
Block a user