Ruff
This commit is contained in:
parent
b995d3ebe1
commit
6856343f4c
|
@ -184,7 +184,7 @@ class Asset:
|
||||||
if m := re.match(r'^(\w+)(?:\.(\w+))?$', ticker):
|
if m := re.match(r'^(\w+)(?:\.(\w+))?$', ticker):
|
||||||
exchange = EXCHANGES_BY_IDS[m.group(2) or 'NYSE'] # TODO?
|
exchange = EXCHANGES_BY_IDS[m.group(2) or 'NYSE'] # TODO?
|
||||||
return Stock(m.group(1), exchange, **attrs)
|
return Stock(m.group(1), exchange, **attrs)
|
||||||
if prefix in {'currency','fiat'}:
|
if prefix in {'currency', 'fiat'}:
|
||||||
return FiatCurrency(ticker, **attrs)
|
return FiatCurrency(ticker, **attrs)
|
||||||
if prefix == 'index':
|
if prefix == 'index':
|
||||||
return Index(ticker, **attrs)
|
return Index(ticker, **attrs)
|
||||||
|
@ -211,6 +211,7 @@ class UnknownAsset(Asset):
|
||||||
def raw_short_name(self) -> str:
|
def raw_short_name(self) -> str:
|
||||||
return '???'
|
return '???'
|
||||||
|
|
||||||
|
|
||||||
@enforce_typing.enforce_types
|
@enforce_typing.enforce_types
|
||||||
@dataclasses.dataclass(frozen=True)
|
@dataclasses.dataclass(frozen=True)
|
||||||
class Currency(Asset):
|
class Currency(Asset):
|
||||||
|
@ -278,7 +279,7 @@ class Stock(Asset):
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def from_polygon_id(polygon_id: str) -> 'Stock':
|
def from_polygon_id(polygon_id: str) -> 'Stock':
|
||||||
return Stock(polygon_id, NYSE) # TODO: Add support for non-NYSE exchanges.
|
return Stock(polygon_id, NYSE) # TODO: Add support for non-NYSE exchanges.
|
||||||
|
|
||||||
def raw_short_name(self) -> str:
|
def raw_short_name(self) -> str:
|
||||||
return self.ticker
|
return self.ticker
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
import pytest
|
|
||||||
|
|
||||||
from decimal import Decimal
|
from decimal import Decimal
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
|
||||||
import fin_defs
|
import fin_defs
|
||||||
|
|
||||||
USD_0 = fin_defs.AssetAmount(fin_defs.USD, Decimal(0))
|
USD_0 = fin_defs.AssetAmount(fin_defs.USD, Decimal(0))
|
||||||
|
@ -13,80 +13,106 @@ USD_21 = fin_defs.AssetAmount(fin_defs.USD, Decimal(21))
|
||||||
DKK_0 = fin_defs.AssetAmount(fin_defs.DKK, Decimal(0))
|
DKK_0 = fin_defs.AssetAmount(fin_defs.DKK, Decimal(0))
|
||||||
DKK_21 = fin_defs.AssetAmount(fin_defs.DKK, Decimal(21))
|
DKK_21 = fin_defs.AssetAmount(fin_defs.DKK, Decimal(21))
|
||||||
|
|
||||||
|
|
||||||
def test_add_asset():
|
def test_add_asset():
|
||||||
assert USD_10 + USD_11 == USD_21
|
assert USD_10 + USD_11 == USD_21
|
||||||
assert USD_10 + USD_0 == USD_10
|
assert USD_10 + USD_0 == USD_10
|
||||||
assert DKK_0 + USD_10 == USD_10
|
assert DKK_0 + USD_10 == USD_10
|
||||||
|
|
||||||
|
|
||||||
def test_sub_asset():
|
def test_sub_asset():
|
||||||
assert USD_21 - USD_10 == USD_11
|
assert USD_21 - USD_10 == USD_11
|
||||||
assert USD_10 - USD_0 == USD_10
|
assert USD_10 - USD_0 == USD_10
|
||||||
assert DKK_0 - USD_10 == -USD_10
|
assert DKK_0 - USD_10 == -USD_10
|
||||||
|
|
||||||
|
|
||||||
def test_div_asset():
|
def test_div_asset():
|
||||||
assert USD_20 / USD_10 == 2
|
assert USD_20 / USD_10 == 2
|
||||||
assert USD_10 / USD_10 == 1
|
assert USD_10 / USD_10 == 1
|
||||||
assert USD_10 / USD_20 == 0.5
|
assert USD_10 / USD_20 == 0.5
|
||||||
|
|
||||||
|
|
||||||
def test_div_amount():
|
def test_div_amount():
|
||||||
assert USD_20 / Decimal(2) == USD_10
|
assert USD_20 / Decimal(2) == USD_10
|
||||||
|
|
||||||
|
|
||||||
def test_mult():
|
def test_mult():
|
||||||
assert USD_10 * Decimal(2) == USD_20
|
assert USD_10 * Decimal(2) == USD_20
|
||||||
|
|
||||||
|
|
||||||
def test_negate():
|
def test_negate():
|
||||||
assert USD_20 + (-USD_10) == USD_10
|
assert USD_20 + (-USD_10) == USD_10
|
||||||
|
|
||||||
|
|
||||||
def test_compare():
|
def test_compare():
|
||||||
assert USD_10 < USD_20
|
assert USD_10 < USD_20
|
||||||
assert USD_10 <= USD_20
|
assert USD_10 <= USD_20
|
||||||
assert not (USD_10 > USD_20)
|
assert not (USD_10 > USD_20)
|
||||||
assert not (USD_10 >= USD_20)
|
assert not (USD_10 >= USD_20)
|
||||||
|
|
||||||
|
|
||||||
def test_compare_zero():
|
def test_compare_zero():
|
||||||
assert DKK_0 < USD_20
|
assert DKK_0 < USD_20
|
||||||
assert DKK_0 <= USD_20
|
assert DKK_0 <= USD_20
|
||||||
assert not (DKK_0 > USD_20)
|
assert not (DKK_0 > USD_20)
|
||||||
assert not (DKK_0 >= USD_20)
|
assert not (DKK_0 >= USD_20)
|
||||||
|
|
||||||
|
|
||||||
def test_is_zero():
|
def test_is_zero():
|
||||||
assert USD_0.is_zero()
|
assert USD_0.is_zero()
|
||||||
assert not USD_10.is_zero()
|
assert not USD_10.is_zero()
|
||||||
|
|
||||||
|
|
||||||
def test_str():
|
def test_str():
|
||||||
assert str(USD_0) == '$0.000 USD'
|
assert str(USD_0) == '$0.000 USD'
|
||||||
assert str(USD_10) == '$10.00 USD'
|
assert str(USD_10) == '$10.00 USD'
|
||||||
assert str(USD_11) == '$11.00 USD'
|
assert str(USD_11) == '$11.00 USD'
|
||||||
assert str(-USD_10) == '-$10.00 USD'
|
assert str(-USD_10) == '-$10.00 USD'
|
||||||
|
|
||||||
|
|
||||||
def test_mul_wrong():
|
def test_mul_wrong():
|
||||||
with pytest.raises(TypeError, match='other must be Decimal, but was AssetAmount'):
|
with pytest.raises(TypeError, match='other must be Decimal, but was AssetAmount'):
|
||||||
assert USD_20 * USD_10
|
assert USD_20 * USD_10
|
||||||
|
|
||||||
|
|
||||||
def test_add_wrong_type():
|
def test_add_wrong_type():
|
||||||
with pytest.raises(TypeError, match='other must be AssetAmount, but was Decimal'):
|
with pytest.raises(TypeError, match='other must be AssetAmount, but was Decimal'):
|
||||||
assert USD_20 + Decimal(1)
|
assert USD_20 + Decimal(1)
|
||||||
|
|
||||||
|
|
||||||
def test_add_wrong_asset():
|
def test_add_wrong_asset():
|
||||||
with pytest.raises(ValueError, match='AssetAmount must have same asset, but: fiat:USD != fiat:DKK'):
|
with pytest.raises(
|
||||||
|
ValueError, match='AssetAmount must have same asset, but: fiat:USD != fiat:DKK',
|
||||||
|
):
|
||||||
assert USD_20 + DKK_21
|
assert USD_20 + DKK_21
|
||||||
|
|
||||||
|
|
||||||
def test_sub_wrong_type():
|
def test_sub_wrong_type():
|
||||||
with pytest.raises(TypeError, match='other must be AssetAmount, but was Decimal'):
|
with pytest.raises(TypeError, match='other must be AssetAmount, but was Decimal'):
|
||||||
assert USD_20 - Decimal(1)
|
assert USD_20 - Decimal(1)
|
||||||
|
|
||||||
|
|
||||||
def test_sub_wrong_asset():
|
def test_sub_wrong_asset():
|
||||||
with pytest.raises(ValueError, match='AssetAmount must have same asset, but: fiat:USD != fiat:DKK'):
|
with pytest.raises(
|
||||||
|
ValueError, match='AssetAmount must have same asset, but: fiat:USD != fiat:DKK',
|
||||||
|
):
|
||||||
assert USD_20 - DKK_21
|
assert USD_20 - DKK_21
|
||||||
|
|
||||||
|
|
||||||
def test_cmp_wrong_type():
|
def test_cmp_wrong_type():
|
||||||
with pytest.raises(TypeError, match='other must be AssetAmount, but was Decimal'):
|
with pytest.raises(TypeError, match='other must be AssetAmount, but was Decimal'):
|
||||||
assert USD_20 < Decimal(1)
|
assert Decimal(1) > USD_20
|
||||||
|
|
||||||
|
|
||||||
def test_cmp_wrong_asset():
|
def test_cmp_wrong_asset():
|
||||||
with pytest.raises(ValueError, match='AssetAmount must have same asset, but: fiat:USD != fiat:DKK'):
|
with pytest.raises(
|
||||||
|
ValueError, match='AssetAmount must have same asset, but: fiat:USD != fiat:DKK',
|
||||||
|
):
|
||||||
assert USD_20 < DKK_21
|
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'):
|
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
|
assert USD_21 / DKK_21
|
||||||
|
|
|
@ -1,11 +1,11 @@
|
||||||
import pytest
|
|
||||||
|
|
||||||
import fin_defs
|
import fin_defs
|
||||||
|
|
||||||
|
|
||||||
def test_from_currency_symbol():
|
def test_from_currency_symbol():
|
||||||
assert fin_defs.FiatCurrency.from_currency_symbol('$') == fin_defs.USD
|
assert fin_defs.FiatCurrency.from_currency_symbol('$') == fin_defs.USD
|
||||||
assert fin_defs.FiatCurrency.from_currency_symbol('TEST') is None
|
assert fin_defs.FiatCurrency.from_currency_symbol('TEST') is None
|
||||||
|
|
||||||
|
|
||||||
def test_currency_symbol():
|
def test_currency_symbol():
|
||||||
assert fin_defs.USD.to_currency_symbol() == '$'
|
assert fin_defs.USD.to_currency_symbol() == '$'
|
||||||
|
|
||||||
|
|
|
@ -13,7 +13,6 @@ def test_valid_tickers(ticker: str):
|
||||||
assert asset.to_polygon_id() is not None
|
assert asset.to_polygon_id() is not None
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize('ticker', BAD_STOCK_TICKERS)
|
@pytest.mark.parametrize('ticker', BAD_STOCK_TICKERS)
|
||||||
def test_bad_tickers(ticker: str):
|
def test_bad_tickers(ticker: str):
|
||||||
with pytest.raises(ValueError, match='ticker was not in correct format:.*'):
|
with pytest.raises(ValueError, match='ticker was not in correct format:.*'):
|
||||||
|
|
|
@ -1,19 +1,23 @@
|
||||||
import pytest
|
|
||||||
|
|
||||||
import datetime
|
import datetime
|
||||||
from decimal import Decimal
|
from decimal import Decimal
|
||||||
|
|
||||||
import fin_defs
|
import fin_defs
|
||||||
|
|
||||||
NOW = datetime.datetime.now()
|
NOW = datetime.datetime.now()
|
||||||
THEN = NOW - datetime.timedelta(days=1)
|
THEN = NOW - datetime.timedelta(days=1)
|
||||||
|
|
||||||
SAMPLE_AVERAGE = fin_defs.ExchangeRateSample(NOW,
|
SAMPLE_AVERAGE = fin_defs.ExchangeRateSample(
|
||||||
(fin_defs.FiatCurrency.DKK,fin_defs.FiatCurrency.USD),
|
NOW, (fin_defs.FiatCurrency.DKK, fin_defs.FiatCurrency.USD), _average=Decimal(1),
|
||||||
_average = Decimal(1))
|
)
|
||||||
|
|
||||||
|
SAMPLE_HIGH_LOW = fin_defs.ExchangeRateSample(
|
||||||
|
NOW,
|
||||||
|
(fin_defs.FiatCurrency.DKK, fin_defs.FiatCurrency.USD),
|
||||||
|
high=Decimal(1),
|
||||||
|
low=Decimal('0.5'),
|
||||||
|
)
|
||||||
|
|
||||||
SAMPLE_HIGH_LOW = fin_defs.ExchangeRateSample(NOW,
|
|
||||||
(fin_defs.FiatCurrency.DKK,fin_defs.FiatCurrency.USD),
|
|
||||||
high = Decimal(1), low=Decimal('0.5'))
|
|
||||||
|
|
||||||
def test_sample_average():
|
def test_sample_average():
|
||||||
assert SAMPLE_AVERAGE.average == 1
|
assert SAMPLE_AVERAGE.average == 1
|
||||||
|
@ -22,7 +26,7 @@ def test_sample_average():
|
||||||
|
|
||||||
def test_invert_sample():
|
def test_invert_sample():
|
||||||
inverted = SAMPLE_HIGH_LOW.invert_exchange_rate()
|
inverted = SAMPLE_HIGH_LOW.invert_exchange_rate()
|
||||||
assert inverted.asset_pair == (fin_defs.FiatCurrency.USD,fin_defs.FiatCurrency.DKK)
|
assert inverted.asset_pair == (fin_defs.FiatCurrency.USD, fin_defs.FiatCurrency.DKK)
|
||||||
assert inverted.high == 2
|
assert inverted.high == 2
|
||||||
assert inverted.low == 1
|
assert inverted.low == 1
|
||||||
assert inverted.average == 1.5
|
assert inverted.average == 1.5
|
||||||
|
|
|
@ -20,13 +20,18 @@ VALID_IDS = [
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
ASSETS = list(fin_defs.WELL_KNOWN_SYMBOLS.values()) + [fin_defs.Asset.from_string_id(vid) for vid in VALID_IDS]
|
ASSETS = list(fin_defs.WELL_KNOWN_SYMBOLS.values()) + [
|
||||||
|
fin_defs.Asset.from_string_id(vid) for vid in VALID_IDS
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
ASSETS_POLYGON_PRESERVES_FULL_ID = frozenset(a for a in ASSETS if not isinstance(a,
|
ASSETS_POLYGON_PRESERVES_FULL_ID = frozenset(
|
||||||
fin_defs.CryptoCurrency
|
a
|
||||||
| fin_defs.Commodity
|
for a in ASSETS
|
||||||
| fin_defs.UnknownAsset)) - frozenset([NVO])
|
if not isinstance(
|
||||||
|
a, fin_defs.CryptoCurrency | fin_defs.Commodity | fin_defs.UnknownAsset,
|
||||||
|
)
|
||||||
|
) - frozenset([NVO])
|
||||||
|
|
||||||
INVALID_IDS = [
|
INVALID_IDS = [
|
||||||
'NVO',
|
'NVO',
|
||||||
|
@ -89,14 +94,17 @@ def test_to_from_polygon_id_works(asset: fin_defs.Asset):
|
||||||
return
|
return
|
||||||
assert fin_defs.Asset.from_polygon_id(asset.to_polygon_id()) is not None
|
assert fin_defs.Asset.from_polygon_id(asset.to_polygon_id()) is not None
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize('asset', ASSETS_POLYGON_PRESERVES_FULL_ID)
|
@pytest.mark.parametrize('asset', ASSETS_POLYGON_PRESERVES_FULL_ID)
|
||||||
def test_to_from_polygon_id_preserves_id(asset: fin_defs.Asset):
|
def test_to_from_polygon_id_preserves_id(asset: fin_defs.Asset):
|
||||||
assert fin_defs.Asset.from_polygon_id(asset.to_polygon_id()) == asset
|
assert fin_defs.Asset.from_polygon_id(asset.to_polygon_id()) == asset
|
||||||
|
|
||||||
|
|
||||||
def test_to_polygon_id_fail_for_commodity():
|
def test_to_polygon_id_fail_for_commodity():
|
||||||
with pytest.raises(TypeError, match='Unknown type: Commodity'):
|
with pytest.raises(TypeError, match='Unknown type: Commodity'):
|
||||||
assert fin_defs.Commodity.CORN.to_polygon_id()
|
assert fin_defs.Commodity.CORN.to_polygon_id()
|
||||||
|
|
||||||
|
|
||||||
def test_to_string_id_wrong_type():
|
def test_to_string_id_wrong_type():
|
||||||
with pytest.raises(TypeError, match='Unsupported asset type: int'):
|
with pytest.raises(TypeError, match='Unsupported asset type: int'):
|
||||||
assert fin_defs.Asset.to_string_id(1)
|
assert fin_defs.Asset.to_string_id(1)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user