Skip to content

Commit 80a1432

Browse files
author
ravigadila
committed
return decimal rate when force decimal used for get_rate with same currency
1 parent e2ebcc5 commit 80a1432

File tree

3 files changed

+19
-6
lines changed

3 files changed

+19
-6
lines changed

README.rst

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ forex-python
1515
:target: https://pypi.python.org/pypi/forex-python
1616

1717

18-
`Forex Python`_ is a Free Foreign exchange rates and currency conversion.
18+
Forex Python is a Free Foreign exchange rates and currency conversion.
1919

2020
Features:
2121
---------
@@ -148,7 +148,6 @@ Visit our Python Development page `Here`_
148148
We welcome your feedback and support, raise `github ticket`_ if you want to report a bug. Need new features? `Contact us here`_
149149

150150
.. _contact us here: https://micropyramid.com/contact-us/
151-
.. _Forex Python: https://micropyramid.com/oss/
152151
.. _github ticket: https://github.com/MicroPyramid/forex-python/issues
153152
.. _Documentation Here: http://forex-python.readthedocs.org/en/latest/?badge=latest
154153
.. _Here: https://micropyramid.com/python-development-services/

forex_python/converter.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ def get_rates(self, base_cur, date_obj=None):
5757

5858
def get_rate(self, base_cur, dest_cur, date_obj=None):
5959
if base_cur == dest_cur:
60+
if self._force_decimal:
61+
return Decimal(1)
6062
return 1.
6163
date_str = self._get_date_string(date_obj)
6264
payload = {'base': base_cur, 'symbols': dest_cur}
@@ -71,13 +73,16 @@ def get_rate(self, base_cur, dest_cur, date_obj=None):
7173
raise RatesNotAvailableError("Currency Rates Source Not Ready")
7274

7375
def convert(self, base_cur, dest_cur, amount, date_obj=None):
74-
if base_cur == dest_cur:
75-
return amount
7676
if isinstance(amount, Decimal):
7777
use_decimal = True
7878
else:
7979
use_decimal = self._force_decimal
8080

81+
if base_cur == dest_cur: # Return same amount if both base_cur, dest_cur are same
82+
if use_decimal:
83+
return Decimal(amount)
84+
return float(amount)
85+
8186
date_str = self._get_date_string(date_obj)
8287
payload = {'base': base_cur, 'symbols': dest_cur}
8388
source_url = self._source_url() + date_str

tests/test.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ def test_amount_convert_valid_currency(self):
8181

8282
def test_amount_convert_valid_currency_same_currency(self):
8383
amount = convert('USD', 'USD', 10)
84-
self.assertEqual(amount, 10)
84+
self.assertEqual(amount, float(10))
8585

8686

8787
def test_amount_convert_date(self):
@@ -106,9 +106,12 @@ def setUp(self):
106106

107107
def test_amount_decimal_convert(self):
108108
amount = self.c.convert('USD', 'INR', Decimal('10.45'))
109-
110109
self.assertTrue(isinstance(amount, Decimal))
111110

111+
def test_amount_decimal_convert_same_currency(self):
112+
amount = self.c.convert('USD', 'USD', Decimal('10.45'))
113+
self.assertEqual(amount, Decimal('10.45'))
114+
112115
def test_amount_decimal_convert_date(self):
113116
date_obj = datetime.datetime.strptime('2010-05-10', "%Y-%m-%d").date()
114117
amount = self.c.convert('USD', 'INR', Decimal('10.45'), date_obj)
@@ -145,6 +148,12 @@ def test_decimal_get_rate_with_valid_codes(self):
145148
# check if return value is Decimal
146149
self.assertTrue(isinstance(rate, Decimal))
147150

151+
def test_decimal_get_rate_with_valid_same_codes(self):
152+
rate = self.c.get_rate('USD', 'USD')
153+
# check if return value is Decimal
154+
self.assertEqual(rate, Decimal(1))
155+
156+
148157
def test_decimal_get_rate_with_date(self):
149158
date_obj = datetime.datetime.strptime('2010-05-10', "%Y-%m-%d").date()
150159
rate = self.c.get_rate('USD', 'INR', date_obj)

0 commit comments

Comments
 (0)