Coverage for value_object_pattern/usables/money/currency_code_value_object.py: 62.50%
14 statements
« prev ^ index » next coverage.py v7.15.4, created at 2026-11-21 14:31 +0000
« prev ^ index » next coverage.py v7.15.4, created at 2026-11-21 14:31 +0000
1"""
2Provide a current ISO 4217 alphabetic currency-code value object.
3"""
5from typing import NoReturn 1abcdefghij
7from value_object_pattern.decorators import process, validation 1abcdefghij
8from value_object_pattern.usables import NotEmptyStringValueObject, TrimmedStringValueObject 1abcdefghij
10from .utils import get_iso4217_alpha3_codes 1abcdefghij
13class CurrencyCodeValueObject(NotEmptyStringValueObject, TrimmedStringValueObject): 1abcdefghij
14 """
15 Validate and normalize a current ISO 4217 three-letter currency or fund code.
17 Validation uses ISO 4217 List One as published by the SIX maintenance agency on 2026-01-01. Codes are
18 case-insensitive on input and stored in uppercase. Historical codes are not accepted.
20 References:
21 ISO 4217: https://www.iso.org/iso-4217-currency-codes.html
22 SIX List One: https://www.six-group.com/dam/download/financial-information/data-center/iso-currrency/lists/list-one.xml
24 Example:
25 ```python
26 from value_object_pattern.usables.money import CurrencyCodeValueObject
28 currency = CurrencyCodeValueObject(value='eur')
29 print(repr(currency))
30 # >>> CurrencyCodeValueObject(value='EUR')
31 ```
32 """
34 @process(order=0) 1abcdefghij
35 def _normalize_currency_code(self, value: str) -> str: 1abcdefghij
36 """
37 Normalize the currency code to uppercase.
39 Args:
40 value (str): The validated currency code.
42 Returns:
43 str: The uppercase currency code.
44 """
45 return value.upper()
47 @validation(order=1) 1abcdefghij
48 def _ensure_value_is_currency_code(self, value: str) -> None: 1abcdefghij
49 """
50 Ensure the value is a current ISO 4217 alphabetic code.
52 Args:
53 value (str): The currency code to validate.
55 Raises:
56 ValueError: If the value is not in the current ISO 4217 catalog.
57 """
58 if value.upper() not in get_iso4217_alpha3_codes():
59 self._raise_value_is_not_currency_code(value=value)
61 def _raise_value_is_not_currency_code(self, value: str) -> NoReturn: 1abcdefghij
62 """
63 Raise an error for an invalid or historical currency code.
65 Args:
66 value (str): The invalid currency code.
68 Raises:
69 ValueError: Always raised with the invalid value.
70 """
71 raise ValueError(f'CurrencyCodeValueObject value <<<{value}>>> is not a current ISO 4217 currency code.')