fix: update get_exchange_rate for test mode (#26847)
* fix: no api calls on test mode * fix: make api call, but return 1.0 incase of invalid currency * feat: patch request.get * fix: clear cache after test * fix: add missing test_exchange_value * fix: avoid api call in implicit function call during test * fix: returning 1 while in test causes failure Returning 1.00 causes failure of exchange rate tests. * chore: retab * chore: style
This commit is contained in:
parent
88aa7b824e
commit
94dcbf851a
@ -1,14 +1,14 @@
|
|||||||
# Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
|
# Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
|
||||||
# License: GNU General Public License v3. See license.txt
|
# License: GNU General Public License v3. See license.txt
|
||||||
from __future__ import unicode_literals
|
|
||||||
import frappe, unittest
|
import frappe, unittest
|
||||||
|
from unittest import mock
|
||||||
from frappe.utils import flt
|
from frappe.utils import flt
|
||||||
from erpnext.setup.utils import get_exchange_rate
|
from erpnext.setup.utils import get_exchange_rate
|
||||||
from frappe.utils import cint
|
from frappe.utils import cint
|
||||||
|
|
||||||
test_records = frappe.get_test_records('Currency Exchange')
|
test_records = frappe.get_test_records('Currency Exchange')
|
||||||
|
|
||||||
|
|
||||||
def save_new_records(test_records):
|
def save_new_records(test_records):
|
||||||
for record in test_records:
|
for record in test_records:
|
||||||
# If both selling and buying enabled
|
# If both selling and buying enabled
|
||||||
@ -37,18 +37,45 @@ def save_new_records(test_records):
|
|||||||
curr_exchange.for_selling = record["for_selling"]
|
curr_exchange.for_selling = record["for_selling"]
|
||||||
curr_exchange.insert()
|
curr_exchange.insert()
|
||||||
|
|
||||||
|
test_exchange_values = {
|
||||||
|
'2015-12-15': '66.999',
|
||||||
|
'2016-01-15': '65.1'
|
||||||
|
}
|
||||||
|
|
||||||
|
# Removing API call from get_exchange_rate
|
||||||
|
def patched_requests_get(*args, **kwargs):
|
||||||
|
class PatchResponse:
|
||||||
|
def __init__(self, json_data, status_code):
|
||||||
|
self.json_data = json_data
|
||||||
|
self.status_code = status_code
|
||||||
|
|
||||||
|
def raise_for_status(self):
|
||||||
|
if self.status_code != 200:
|
||||||
|
raise frappe.DoesNotExistError
|
||||||
|
|
||||||
|
def json(self):
|
||||||
|
return self.json_data
|
||||||
|
|
||||||
|
if args[0] == "https://api.exchangerate.host/convert" and kwargs.get('params'):
|
||||||
|
if kwargs['params'].get('date') and kwargs['params'].get('from') and kwargs['params'].get('to'):
|
||||||
|
if test_exchange_values.get(kwargs['params']['date']):
|
||||||
|
return PatchResponse({'result': test_exchange_values[kwargs['params']['date']]}, 200)
|
||||||
|
|
||||||
|
return PatchResponse({'result': None}, 404)
|
||||||
|
|
||||||
|
@mock.patch('requests.get', side_effect=patched_requests_get)
|
||||||
class TestCurrencyExchange(unittest.TestCase):
|
class TestCurrencyExchange(unittest.TestCase):
|
||||||
def clear_cache(self):
|
def clear_cache(self):
|
||||||
cache = frappe.cache()
|
cache = frappe.cache()
|
||||||
key = "currency_exchange_rate:{0}:{1}".format("USD", "INR")
|
for date in test_exchange_values.keys():
|
||||||
cache.delete(key)
|
key = "currency_exchange_rate_{0}:{1}:{2}".format(date, "USD", "INR")
|
||||||
|
cache.delete(key)
|
||||||
|
|
||||||
def tearDown(self):
|
def tearDown(self):
|
||||||
frappe.db.set_value("Accounts Settings", None, "allow_stale", 1)
|
frappe.db.set_value("Accounts Settings", None, "allow_stale", 1)
|
||||||
self.clear_cache()
|
self.clear_cache()
|
||||||
|
|
||||||
def test_exchange_rate(self):
|
def test_exchange_rate(self, mock_get):
|
||||||
save_new_records(test_records)
|
save_new_records(test_records)
|
||||||
|
|
||||||
frappe.db.set_value("Accounts Settings", None, "allow_stale", 1)
|
frappe.db.set_value("Accounts Settings", None, "allow_stale", 1)
|
||||||
@ -69,7 +96,11 @@ class TestCurrencyExchange(unittest.TestCase):
|
|||||||
self.assertFalse(exchange_rate == 60)
|
self.assertFalse(exchange_rate == 60)
|
||||||
self.assertEqual(flt(exchange_rate, 3), 66.999)
|
self.assertEqual(flt(exchange_rate, 3), 66.999)
|
||||||
|
|
||||||
def test_exchange_rate_strict(self):
|
exchange_rate = get_exchange_rate("USD", "INR", "2016-01-20", "for_buying")
|
||||||
|
self.assertFalse(exchange_rate == 60)
|
||||||
|
self.assertEqual(flt(exchange_rate, 3), 65.1)
|
||||||
|
|
||||||
|
def test_exchange_rate_strict(self, mock_get):
|
||||||
# strict currency settings
|
# strict currency settings
|
||||||
frappe.db.set_value("Accounts Settings", None, "allow_stale", 0)
|
frappe.db.set_value("Accounts Settings", None, "allow_stale", 0)
|
||||||
frappe.db.set_value("Accounts Settings", None, "stale_days", 1)
|
frappe.db.set_value("Accounts Settings", None, "stale_days", 1)
|
||||||
@ -79,7 +110,7 @@ class TestCurrencyExchange(unittest.TestCase):
|
|||||||
|
|
||||||
self.clear_cache()
|
self.clear_cache()
|
||||||
exchange_rate = get_exchange_rate("USD", "INR", "2016-01-15", "for_buying")
|
exchange_rate = get_exchange_rate("USD", "INR", "2016-01-15", "for_buying")
|
||||||
self.assertEqual(flt(exchange_rate, 3), 67.235)
|
self.assertEqual(flt(exchange_rate, 3), 65.100)
|
||||||
|
|
||||||
exchange_rate = get_exchange_rate("USD", "INR", "2016-01-30", "for_selling")
|
exchange_rate = get_exchange_rate("USD", "INR", "2016-01-30", "for_selling")
|
||||||
self.assertEqual(exchange_rate, 62.9)
|
self.assertEqual(exchange_rate, 62.9)
|
||||||
@ -89,7 +120,7 @@ class TestCurrencyExchange(unittest.TestCase):
|
|||||||
exchange_rate = get_exchange_rate("USD", "INR", "2015-12-15", "for_buying")
|
exchange_rate = get_exchange_rate("USD", "INR", "2015-12-15", "for_buying")
|
||||||
self.assertEqual(flt(exchange_rate, 3), 66.999)
|
self.assertEqual(flt(exchange_rate, 3), 66.999)
|
||||||
|
|
||||||
def test_exchange_rate_strict_switched(self):
|
def test_exchange_rate_strict_switched(self, mock_get):
|
||||||
# Start with allow_stale is True
|
# Start with allow_stale is True
|
||||||
exchange_rate = get_exchange_rate("USD", "INR", "2016-01-15", "for_buying")
|
exchange_rate = get_exchange_rate("USD", "INR", "2016-01-15", "for_buying")
|
||||||
self.assertEqual(exchange_rate, 65.1)
|
self.assertEqual(exchange_rate, 65.1)
|
||||||
@ -97,7 +128,7 @@ class TestCurrencyExchange(unittest.TestCase):
|
|||||||
frappe.db.set_value("Accounts Settings", None, "allow_stale", 0)
|
frappe.db.set_value("Accounts Settings", None, "allow_stale", 0)
|
||||||
frappe.db.set_value("Accounts Settings", None, "stale_days", 1)
|
frappe.db.set_value("Accounts Settings", None, "stale_days", 1)
|
||||||
|
|
||||||
# Will fetch from fixer.io
|
|
||||||
self.clear_cache()
|
self.clear_cache()
|
||||||
exchange_rate = get_exchange_rate("USD", "INR", "2016-01-15", "for_buying")
|
exchange_rate = get_exchange_rate("USD", "INR", "2016-01-30", "for_buying")
|
||||||
self.assertEqual(flt(exchange_rate, 3), 67.235)
|
self.assertFalse(exchange_rate == 65)
|
||||||
|
self.assertEqual(flt(exchange_rate, 3), 62.9)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user