Skip to content

Commit

Permalink
✨ Add more available currencies
Browse files Browse the repository at this point in the history
  • Loading branch information
esemi committed May 8, 2021
1 parent 1539322 commit 6caf5aa
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
29 changes: 26 additions & 3 deletions investments/currency.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,41 @@ class Currency(Enum):
USD = (('$', 'USD'), '840', 'R01235')
RUB = (('₽', 'RUB', 'RUR'), '643', '')
EUR = (('€', 'EUR'), '978', 'R01239')
AUD = (('AUD',), '036', 'R01010')
GBP = (('GBP',), '826', 'R01035')
CAD = (('CAD',), '124', 'R01350')
CZK = (('CZK',), '203', 'R01760')
DKK = (('DKK',), '208', 'R01215')
HKD = (('HKD',), '344', 'R01200')
HUF = (('HUF',), '348', 'R01135')
YEN = (('YEN',), '392', 'R01820')
KRW = (('KRW',), '410', 'R01815')
NOK = (('NOK',), '578', 'R01535')
PLN = (('PLN',), '985', 'R01565')
SGD = (('SGD',), '702', 'R01625')
ZAR = (('ZAR',), '710', 'R01810')
SEK = (('SEK',), '752', 'R01770')
CHF = (('CHF',), '756', 'R01775')
TRY = (('TRY',), '949', 'R01700J')

# unknown currency for cbr.ru
# CNH = (('CNH',), 'unknown', 'unknown')
# ILS = (('ILS',), '376', 'unknown')
# MXN = (('MXN',), '484', 'unknown')
# NZD = (('NZD',), '554', 'unknown')

def __init__(self, aliases: Tuple[str], iso_code: str, cbr_code: str):
self._iso_code = iso_code
self._cbr_code = cbr_code
self.aliases = aliases

@staticmethod
def parse(strval: str):
def parse(search: str):
try:
return [item for _, item in Currency.__members__.items() if strval in item.aliases][0]
return [currency_item for _, currency_item in Currency.__members__.items() # noqa: WPS609
if search in currency_item.aliases][0]
except IndexError:
raise ValueError(strval)
raise ValueError(search)

def __str__(self):
return str(self.aliases[0])
Expand Down
4 changes: 4 additions & 0 deletions tests/currency_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
('RUR', Currency.RUB),
('RUB', Currency.RUB),
('₽', Currency.RUB),
('CAD', Currency.CAD),
])
def test_parse(search_value: str, res: Currency):
assert Currency.parse(search_value) is res
Expand All @@ -22,6 +23,7 @@ def test_parse_failure():
@pytest.mark.parametrize('currency,expected_repr', [
(Currency.USD, '$'),
(Currency.RUB, '₽'),
(Currency.CAD, 'CAD'),
])
def test_repr(currency: Currency, expected_repr: str):
assert str(currency) == expected_repr
Expand All @@ -30,6 +32,7 @@ def test_repr(currency: Currency, expected_repr: str):
@pytest.mark.parametrize('currency,expected', [
(Currency.USD, '840'),
(Currency.RUB, '643'),
(Currency.CAD, '124'),
])
def test_iso_numeric_code(currency: Currency, expected: str):
assert currency.iso_numeric_code == expected
Expand All @@ -39,6 +42,7 @@ def test_iso_numeric_code(currency: Currency, expected: str):
(Currency.USD, 'R01235'),
(Currency.RUB, ''),
(Currency.EUR, 'R01239'),
(Currency.CAD, 'R01350'),
])
def test_cbr_code(currency: Currency, expected: str):
assert currency.cbr_code == expected

0 comments on commit 6caf5aa

Please sign in to comment.