1
0

Further id tests
All checks were successful
Test Python / Test (push) Successful in 30s

This commit is contained in:
Jon Michael Aanes 2024-09-01 19:53:44 +02:00
parent eeffb23c98
commit 45afd85e60
Signed by: Jmaa
SSH Key Fingerprint: SHA256:Ab0GfHGCblESJx7JRE4fj4bFy/KRpeLhi41y4pF3sNA
2 changed files with 46 additions and 5 deletions

View File

@ -145,7 +145,7 @@ class Asset:
return f'commodity:{self.alpha_vantage_id}'
msg = f'Unsupported asset type: {repr(self)}'
raise NotImplementedError(msg)
raise ValueError(msg)
@staticmethod
def from_string_id(asset_id: str, shortcut_well_known=True) -> 'Asset':
@ -154,7 +154,7 @@ class Asset:
m = re.match(r'^(?:(\w+):)?([^{]+)(?:\{(.*)\})?$', asset_id)
if m is None:
msg = f'Unsupported asset format: {asset_id}'
raise NotImplementedError(msg)
raise ValueError(msg)
prefix = m.group(1)
ticker = m.group(2)
@ -177,9 +177,11 @@ class Asset:
return Index(ticker, **attrs)
if prefix == 'crypto':
return CryptoCurrency(ticker, **attrs)
if prefix == 'commodity':
return Commodity(ticker)
msg = f'Unsupported asset format: {asset_id}'
raise NotImplementedError(msg)
raise ValueError(msg)
def __str__(self):
return self.to_string_id()

View File

@ -2,6 +2,25 @@ import pytest
import fin_defs
VALID_IDS = [
'stock:NVO.NYSE',
'stock:NVO.NYSE{nordnet_id=16256554}',
'currency:USD',
'fiat:USD',
'index:NDX',
'crypto:BTC',
'crypto:BTC{coingecko_id=bitcoin}',
'commodity:ALUMINUM',
]
INVALID_IDS = [
'NVO',
'NVO.NYSE',
'test:test',
'fiat:TEST:TEST',
'index:TEST:TEST',
'commodity:TEST:TEST',
]
def test_parse_attr():
assert fin_defs.parse_attr_data('') == {}
@ -16,10 +35,30 @@ def test_parse_attr():
def test_from_nordnet():
derp = fin_defs.Asset.from_string_id('stock:NVO.NYSE{nordnet_id=123}')
derp = fin_defs.Asset.from_string_id('stock:NVO.NYSE{nordnet_id=16256554}')
assert isinstance(derp, fin_defs.Stock)
assert derp.ticker == 'NVO'
assert derp.nordnet_id == 123
assert derp.nordnet_id == 16256554
@pytest.mark.parametrize('asset_id', VALID_IDS)
def test_from_string_id_shortcut(asset_id: str):
assert (
fin_defs.Asset.from_string_id(asset_id, shortcut_well_known=True)
)
@pytest.mark.parametrize('asset_id', VALID_IDS)
def test_from_string_id(asset_id: str):
assert (
fin_defs.Asset.from_string_id(asset_id, shortcut_well_known=False)
)
@pytest.mark.parametrize('asset_id', INVALID_IDS)
def test_from_string_id_invalid(asset_id: str):
with pytest.raises(ValueError) as excinfo:
fin_defs.Asset.from_string_id(asset_id, shortcut_well_known=False)
@pytest.mark.parametrize('asset', fin_defs.WELL_KNOWN_SYMBOLS.values())