1
0
fin-defs/test/test_ids.py

85 lines
2.4 KiB
Python
Raw Permalink Normal View History

import pytest
import fin_defs
2024-09-01 17:53:44 +00:00
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',
]
2024-08-09 14:56:28 +00:00
2024-09-01 17:35:44 +00:00
def test_parse_attr():
assert fin_defs.parse_attr_data('') == {}
assert fin_defs.parse_attr_data(' ') == {}
2024-09-01 17:37:28 +00:00
assert fin_defs.parse_attr_data('abc=abc') == {'abc': 'abc'}
assert fin_defs.parse_attr_data('abc=123') == {'abc': 123}
assert fin_defs.parse_attr_data('abc=123,xyz=abc') == {'abc': 123, 'xyz': 'abc'}
assert fin_defs.parse_attr_data(' abc=123 , xyz=abc ') == {
'abc': 123,
'xyz': 'abc',
}
2024-09-01 17:35:44 +00:00
def test_from_nordnet():
2024-09-01 17:53:44 +00:00
derp = fin_defs.Asset.from_string_id('stock:NVO.NYSE{nordnet_id=16256554}')
2024-09-01 17:35:44 +00:00
assert isinstance(derp, fin_defs.Stock)
assert derp.ticker == 'NVO'
2024-09-01 17:53:44 +00:00
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)
2024-09-01 17:35:44 +00:00
@pytest.mark.parametrize('asset', fin_defs.WELL_KNOWN_SYMBOLS.values())
def test_to_from_string_id_shortcut(asset: fin_defs.Asset):
2024-09-01 17:37:28 +00:00
assert (
fin_defs.Asset.from_string_id(asset.to_string_id(), shortcut_well_known=True)
== asset
)
2024-09-01 17:35:44 +00:00
@pytest.mark.parametrize('asset', fin_defs.WELL_KNOWN_SYMBOLS.values())
def test_to_from_string_id(asset: fin_defs.Asset):
2024-09-01 17:37:28 +00:00
assert (
fin_defs.Asset.from_string_id(asset.to_string_id(), shortcut_well_known=False)
== asset
)
2024-08-09 14:56:28 +00:00
@pytest.mark.parametrize('asset', fin_defs.WELL_KNOWN_SYMBOLS.values())
def test_to_from_polygon_id(asset: fin_defs.Asset):
2024-09-01 17:35:44 +00:00
if isinstance(asset, fin_defs.CryptoCurrency):
return
assert fin_defs.Asset.from_polygon_id(asset.to_polygon_id()) == asset