1
0
fin-defs/test/test_ids.py
Jon Michael Aanes a12fcacf77
All checks were successful
Python Ruff Code Quality / ruff (push) Successful in 22s
Run Python tests (through Pytest) / Test (push) Successful in 25s
Verify Python project can be installed, loaded and have version checked / Test (push) Successful in 22s
Code quality
2024-10-27 16:18:21 +01:00

76 lines
2.2 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_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 ') == {
'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)
@pytest.mark.parametrize('asset_id', VALID_IDS)
def test_from_string_id(asset_id: str):
assert fin_defs.Asset.from_string_id(asset_id)
@pytest.mark.parametrize('asset_id', INVALID_IDS)
def test_from_string_id_invalid(asset_id: str):
with pytest.raises(ValueError, match='.*format.*'):
fin_defs.Asset.from_string_id(asset_id)
@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()) == 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()) == 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