FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Make CurrencyCodes Iterable by Robbie-Palmer · Pull Request #111 · MicroPyramid/forex-python · GitHub

Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (3) All 1 file type selected
Only manifest files
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
20 changes: 16 additions & 4 deletions forex_python/converter.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import os
from decimal import Decimal
from pathlib import Path

import requests
import simplejson as json
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions setup.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
install_requires=[
'requests',
'simplejson',
'pathlib'
],
classifiers=[
'Intended Audience :: Developers',
Expand Down
27 changes: 23 additions & 4 deletions tests/test.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -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):
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)

Back | FazBrowse Home | New Git URL