From 9341dc292eed75a1deefac3dd1c9689eeccf6e91 Mon Sep 17 00:00:00 2001 From: Glandos Date: Sun, 10 Jul 2022 15:36:41 +0200 Subject: [PATCH] fix #1037 Add a warning, so adapt the test to capture it --- ihatemoney/currency_convertor.py | 12 +++++++++++- ihatemoney/tests/main_test.py | 7 +++++-- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/ihatemoney/currency_convertor.py b/ihatemoney/currency_convertor.py index 881d5428..3ff21ec3 100644 --- a/ihatemoney/currency_convertor.py +++ b/ihatemoney/currency_convertor.py @@ -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 diff --git a/ihatemoney/tests/main_test.py b/ihatemoney/tests/main_test.py index b2f0c1eb..97ab82ac 100644 --- a/ihatemoney/tests/main_test.py +++ b/ihatemoney/tests/main_test.py @@ -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__":