From 80a14321ea8ecc74fc277dcd3770c685d6442cb3 Mon Sep 17 00:00:00 2001 From: ravigadila Date: Mon, 27 Feb 2017 20:10:57 +0530 Subject: [PATCH] return decimal rate when force decimal used for get_rate with same currency --- README.rst | 3 +-- forex_python/converter.py | 9 +++++++-- tests/test.py | 13 +++++++++++-- 3 files changed, 19 insertions(+), 6 deletions(-) diff --git a/README.rst b/README.rst index 37f2350..15f3316 100644 --- a/README.rst +++ b/README.rst @@ -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: --------- @@ -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/ diff --git a/forex_python/converter.py b/forex_python/converter.py index 316f0cf..6a9a818 100644 --- a/forex_python/converter.py +++ b/forex_python/converter.py @@ -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} @@ -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 diff --git a/tests/test.py b/tests/test.py index 2501aa9..610657a 100644 --- a/tests/test.py +++ b/tests/test.py @@ -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): @@ -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) @@ -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)