diff --git a/forex_python/converter.py b/forex_python/converter.py index 390a11a..8b77bfb 100644 --- a/forex_python/converter.py +++ b/forex_python/converter.py @@ -1,5 +1,5 @@ -import os from decimal import Decimal +from pathlib import Path import requests import simplejson as json @@ -123,11 +123,23 @@ def __init__(self): @property def _currency_data(self): if self.__currency_data is None: - file_path = os.path.dirname(os.path.abspath(__file__)) - with open(file_path + '/raw_data/currencies.json') as f: - self.__currency_data = json.loads(f.read()) + currency_codes_file_path = Path(__file__).parent / 'raw_data/currencies.json' + self.__currency_data = json.loads(currency_codes_file_path.read_text()) return self.__currency_data + def __iter__(self): + self._iter_idx = 0 + return self + + def __next__(self) -> dict: + if not hasattr(self, '_iter_idx'): + self.__iter__() + if self._iter_idx < len(self._currency_data): + next_data = self._currency_data[self._iter_idx] + self._iter_idx += 1 + return next_data + raise StopIteration + def _get_data(self, currency_code): currency_dict = next((item for item in self._currency_data if item["cc"] == currency_code), None) return currency_dict diff --git a/setup.py b/setup.py index de66932..c609186 100644 --- a/setup.py +++ b/setup.py @@ -32,6 +32,7 @@ install_requires=[ 'requests', 'simplejson', + 'pathlib' ], classifiers=[ 'Intended Audience :: Developers', diff --git a/tests/test.py b/tests/test.py index 4add1db..dfcb64d 100644 --- a/tests/test.py +++ b/tests/test.py @@ -1,9 +1,13 @@ import datetime +import json from decimal import Decimal +from pathlib import Path from unittest import TestCase + from forex_python.converter import (get_rates, get_rate, convert, get_symbol, get_currency_name, RatesNotAvailableError, - CurrencyRates, DecimalFloatMismatchError) + CurrencyRates, DecimalFloatMismatchError, + CurrencyCodes) class TestGetRates(TestCase): @@ -54,7 +58,7 @@ def test_get_rate_with_valid_codes(self): # check if return value is float self.assertTrue(isinstance(rate, float)) - + def test_get_rate_with_valid_codes_same_currency(self): rate = get_rate('USD', 'USD') # rate should be 1. @@ -92,7 +96,6 @@ def test_amount_convert_valid_currency_same_currency(self): amount = convert('USD', 'USD', 10) self.assertEqual(amount, float(10)) - def test_amount_convert_date(self): date_obj = datetime.datetime.strptime('2010-05-10', "%Y-%m-%d").date() amount = convert('USD', 'INR', 10, date_obj) @@ -162,7 +165,6 @@ def test_decimal_get_rate_with_valid_same_codes(self): # check if return value is Decimal self.assertEqual(rate, Decimal(1)) - def test_decimal_get_rate_with_date(self): date_obj = datetime.datetime.strptime('2010-05-10', "%Y-%m-%d").date() rate = self.c.get_rate('USD', 'INR', date_obj) @@ -196,3 +198,20 @@ def test_with_valid_currency_code(self): def test_with_invalid_currency_code(self): self.assertFalse(get_currency_name('XYZ')) + + +class TestCurrencyData(TestCase): + def setUp(self): + currency_codes_path = Path(__file__).parent.parent / 'forex_python/raw_data/currencies.json' + self.expected_codes = json.loads(currency_codes_path.read_text()) + self.actual_codes = CurrencyCodes() + + def test_for_loop_over_currency_data(self): + for (actual_currency_code, expected_currency_code) in zip(self.actual_codes, self.expected_codes): + assert expected_currency_code == actual_currency_code, f'{expected_currency_code} != {actual_currency_code}' + + def test_nexting_currency_data(self): + for expected_currency_code in self.expected_codes: + actual_currency_code = next(self.actual_codes) + assert expected_currency_code == actual_currency_code, f'{expected_currency_code} != {actual_currency_code}' + self.assertRaises(StopIteration, next, self.actual_codes)