Add a warning, so adapt the test to capture it
This commit is contained in:
Glandos 2022-07-10 15:36:41 +02:00
parent 7a55fb23fa
commit 9341dc292e
2 changed files with 16 additions and 3 deletions

View file

@ -1,3 +1,6 @@
import traceback
import warnings
from cachetools import TTLCache, cached
import requests
@ -21,7 +24,14 @@ class CurrencyConverter(object, metaclass=Singleton):
@cached(cache=TTLCache(maxsize=1, ttl=86400))
def get_rates(self):
rates = requests.get(self.api_url).json()["rates"]
try:
rates = requests.get(self.api_url).json()["rates"]
except Exception as e:
warnings.warn(
f"Call to {self.api_url} failed: {traceback.format_exception_only(e)[0].strip()}"
)
# In case of any exception, let's have an empty value
rates = {}
rates[self.no_currency] = 1.0
return rates

View file

@ -383,11 +383,14 @@ class TestCurrencyConverter(unittest.TestCase):
self.assertEqual(result, 80.0)
def test_failing_remote(self):
with patch("requests.Response.json", new=lambda _: {}):
rates = {}
with patch("requests.Response.json", new=lambda _: {}), self.assertWarns(
UserWarning
):
# we need a non-patched converter, but it seems that MagickMock
# is mocking EVERY instance of the class method. Too bad.
rates = CurrencyConverter.get_rates(self.converter)
self.assertDictEqual(rates, {CurrencyConverter.no_currency: 1})
self.assertDictEqual(rates, {CurrencyConverter.no_currency: 1})
if __name__ == "__main__":