1
0
fin-defs/test/test_ids.py
Jon Michael Aanes 45afd85e60
All checks were successful
Test Python / Test (push) Successful in 30s
Further id tests
2024-09-01 19:53:44 +02:00

85 lines
2.4 KiB
Python

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('') == {}
assert fin_defs.parse_attr_data(' ') == {}
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',
}
def test_from_nordnet():
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 == 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())
def test_to_from_string_id_shortcut(asset: fin_defs.Asset):
assert (
fin_defs.Asset.from_string_id(asset.to_string_id(), shortcut_well_known=True)
== asset
)
@pytest.mark.parametrize('asset', fin_defs.WELL_KNOWN_SYMBOLS.values())
def test_to_from_string_id(asset: fin_defs.Asset):
assert (
fin_defs.Asset.from_string_id(asset.to_string_id(), shortcut_well_known=False)
== asset
)
@pytest.mark.parametrize('asset', fin_defs.WELL_KNOWN_SYMBOLS.values())
def test_to_from_polygon_id(asset: fin_defs.Asset):
if isinstance(asset, fin_defs.CryptoCurrency):
return
assert fin_defs.Asset.from_polygon_id(asset.to_polygon_id()) == asset