1
0
fin-defs/test/test_ids.py

112 lines
3.0 KiB
Python
Raw Normal View History

import pytest
import fin_defs
2024-10-27 16:24:12 +00:00
NVO_ID = 'stock:NVO.NYSE{nordnet_id=16256554}'
NVO = fin_defs.Asset.from_string_id(NVO_ID)
2024-09-01 17:53:44 +00:00
VALID_IDS = [
2024-10-27 15:18:21 +00:00
'stock:NVO.NYSE',
2024-10-27 16:24:12 +00:00
NVO_ID,
2024-10-27 15:18:21 +00:00
'currency:USD',
'fiat:USD',
'index:NDX',
'crypto:BTC',
'crypto:BTC{coingecko_id=bitcoin}',
2024-10-27 16:24:12 +00:00
'crypto:MPC',
'crypto:LOLCOIN',
2024-10-27 15:18:21 +00:00
'commodity:ALUMINUM',
2024-10-27 16:24:12 +00:00
'unknown:XXX',
'crypto: ', # CCXT actually found one with only whitespace. Why?
2024-09-01 17:53:44 +00:00
]
2024-10-27 16:24:12 +00:00
2024-10-27 16:24:26 +00:00
ASSETS = list(fin_defs.WELL_KNOWN_SYMBOLS.values()) + [
fin_defs.Asset.from_string_id(vid) for vid in VALID_IDS
]
2024-10-27 16:24:12 +00:00
2024-10-27 16:24:26 +00:00
ASSETS_POLYGON_PRESERVES_FULL_ID = frozenset(
a
for a in ASSETS
if not isinstance(
2024-10-27 16:26:03 +00:00
a,
fin_defs.CryptoCurrency | fin_defs.Commodity | fin_defs.UnknownAsset,
2024-10-27 16:24:26 +00:00
)
) - frozenset([NVO])
2024-10-27 16:24:12 +00:00
2024-09-01 17:53:44 +00:00
INVALID_IDS = [
2024-10-27 15:18:21 +00:00
'NVO',
'NVO.NYSE',
'test:test',
'fiat:TEST:TEST',
'index:TEST:TEST',
'commodity:TEST:TEST',
2024-10-27 16:24:12 +00:00
'!!!!',
'::::',
'',
' ',
2024-09-01 17:53:44 +00:00
]
2024-08-09 14:56:28 +00:00
2024-10-27 15:18:21 +00:00
2024-09-01 17:35:44 +00:00
def test_parse_attr():
2024-10-27 15:18:21 +00:00
assert fin_defs.parse_id_attr_data('') == {}
assert fin_defs.parse_id_attr_data(' ') == {}
assert fin_defs.parse_id_attr_data('abc=abc') == {'abc': 'abc'}
assert fin_defs.parse_id_attr_data('abc=123') == {'abc': 123}
assert fin_defs.parse_id_attr_data('abc=123,xyz=abc') == {'abc': 123, 'xyz': 'abc'}
assert fin_defs.parse_id_attr_data(' abc=123 , xyz=abc ') == {
2024-09-01 17:37:28 +00:00
'abc': 123,
'xyz': 'abc',
}
2024-09-01 17:35:44 +00:00
def test_from_nordnet():
2024-10-27 16:24:12 +00:00
asset = NVO
assert isinstance(asset, fin_defs.Stock)
assert asset.ticker == 'NVO'
assert asset.nordnet_id == 16256554
2024-09-01 17:53:44 +00:00
2024-10-27 16:24:12 +00:00
@pytest.mark.parametrize('asset', ASSETS)
def test_raw_short_name(asset: fin_defs.Asset):
assert asset.raw_short_name() is not None
2024-09-01 17:53:44 +00:00
@pytest.mark.parametrize('asset_id', VALID_IDS)
def test_from_string_id(asset_id: str):
2024-10-27 15:18:21 +00:00
assert fin_defs.Asset.from_string_id(asset_id)
2024-09-01 17:53:44 +00:00
@pytest.mark.parametrize('asset_id', INVALID_IDS)
def test_from_string_id_invalid(asset_id: str):
2024-10-27 15:18:21 +00:00
with pytest.raises(ValueError, match='.*format.*'):
fin_defs.Asset.from_string_id(asset_id)
2024-09-01 17:35:44 +00:00
2024-10-27 16:24:12 +00:00
@pytest.mark.parametrize('asset', ASSETS)
def test_to_from_string_id(asset: fin_defs.Asset):
2024-10-27 15:18:21 +00:00
assert fin_defs.Asset.from_string_id(asset.to_string_id()) == asset
2024-08-09 14:56:28 +00:00
2024-10-27 16:24:12 +00:00
@pytest.mark.parametrize('asset', ASSETS)
def test_to_from_polygon_id_works(asset: fin_defs.Asset):
if isinstance(asset, fin_defs.Commodity | fin_defs.UnknownAsset):
2024-09-01 17:35:44 +00:00
return
2024-10-27 16:24:12 +00:00
assert fin_defs.Asset.from_polygon_id(asset.to_polygon_id()) is not None
2024-10-27 16:24:26 +00:00
2024-10-27 16:24:12 +00:00
@pytest.mark.parametrize('asset', ASSETS_POLYGON_PRESERVES_FULL_ID)
def test_to_from_polygon_id_preserves_id(asset: fin_defs.Asset):
assert fin_defs.Asset.from_polygon_id(asset.to_polygon_id()) == asset
2024-10-27 16:24:12 +00:00
2024-10-27 16:24:26 +00:00
2024-10-27 16:24:12 +00:00
def test_to_polygon_id_fail_for_commodity():
with pytest.raises(TypeError, match='Unknown type: Commodity'):
assert fin_defs.Commodity.CORN.to_polygon_id()
2024-10-27 16:24:26 +00:00
2024-10-27 16:24:12 +00:00
def test_to_string_id_wrong_type():
with pytest.raises(TypeError, match='Unsupported asset type: int'):
assert fin_defs.Asset.to_string_id(1)