Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ forex-python
:target: https://pypi.python.org/pypi/forex-python


`Forex Python`_ is a Free Foreign exchange rates and currency conversion.
Forex Python is a Free Foreign exchange rates and currency conversion.

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

.. _contact us here: https://micropyramid.com/contact-us/
.. _Forex Python: https://micropyramid.com/oss/
.. _github ticket: https://github.com/MicroPyramid/forex-python/issues
.. _Documentation Here: http://forex-python.readthedocs.org/en/latest/?badge=latest
.. _Here: https://micropyramid.com/python-development-services/
9 changes: 7 additions & 2 deletions forex_python/converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ def get_rates(self, base_cur, date_obj=None):

def get_rate(self, base_cur, dest_cur, date_obj=None):
if base_cur == dest_cur:
if self._force_decimal:
return Decimal(1)
return 1.
date_str = self._get_date_string(date_obj)
payload = {'base': base_cur, 'symbols': dest_cur}
Expand All @@ -71,13 +73,16 @@ def get_rate(self, base_cur, dest_cur, date_obj=None):
raise RatesNotAvailableError("Currency Rates Source Not Ready")

def convert(self, base_cur, dest_cur, amount, date_obj=None):
if base_cur == dest_cur:
return amount
if isinstance(amount, Decimal):
use_decimal = True
else:
use_decimal = self._force_decimal

if base_cur == dest_cur: # Return same amount if both base_cur, dest_cur are same
if use_decimal:
return Decimal(amount)
return float(amount)

date_str = self._get_date_string(date_obj)
payload = {'base': base_cur, 'symbols': dest_cur}
source_url = self._source_url() + date_str
Expand Down
13 changes: 11 additions & 2 deletions tests/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def test_amount_convert_valid_currency(self):

def test_amount_convert_valid_currency_same_currency(self):
amount = convert('USD', 'USD', 10)
self.assertEqual(amount, 10)
self.assertEqual(amount, float(10))


def test_amount_convert_date(self):
Expand All @@ -106,9 +106,12 @@ def setUp(self):

def test_amount_decimal_convert(self):
amount = self.c.convert('USD', 'INR', Decimal('10.45'))

self.assertTrue(isinstance(amount, Decimal))

def test_amount_decimal_convert_same_currency(self):
amount = self.c.convert('USD', 'USD', Decimal('10.45'))
self.assertEqual(amount, Decimal('10.45'))

def test_amount_decimal_convert_date(self):
date_obj = datetime.datetime.strptime('2010-05-10', "%Y-%m-%d").date()
amount = self.c.convert('USD', 'INR', Decimal('10.45'), date_obj)
Expand Down Expand Up @@ -145,6 +148,12 @@ def test_decimal_get_rate_with_valid_codes(self):
# check if return value is Decimal
self.assertTrue(isinstance(rate, Decimal))

def test_decimal_get_rate_with_valid_same_codes(self):
rate = self.c.get_rate('USD', 'USD')
# 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