From 45afd85e60cbaf763d3764e30ecd3aedc5cb7f00 Mon Sep 17 00:00:00 2001 From: Jon Michael Aanes Date: Sun, 1 Sep 2024 19:53:44 +0200 Subject: [PATCH] Further id tests --- fin_defs/__init__.py | 8 +++++--- test/test_ids.py | 43 +++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 46 insertions(+), 5 deletions(-) diff --git a/fin_defs/__init__.py b/fin_defs/__init__.py index c155c8b..61b50c8 100644 --- a/fin_defs/__init__.py +++ b/fin_defs/__init__.py @@ -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() diff --git a/test/test_ids.py b/test/test_ids.py index 87b4d06..b3ca652 100644 --- a/test/test_ids.py +++ b/test/test_ids.py @@ -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())