Skip to content

Commit

Permalink
fix #1037
Browse files Browse the repository at this point in the history
Add a warning, so adapt the test to capture it
  • Loading branch information
Glandos committed Jul 10, 2022
1 parent 7a55fb2 commit 9341dc2
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
12 changes: 11 additions & 1 deletion ihatemoney/currency_convertor.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import traceback
import warnings

from cachetools import TTLCache, cached
import requests

Expand All @@ -21,7 +24,14 @@ def __init__(self):

@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

Expand Down
7 changes: 5 additions & 2 deletions ihatemoney/tests/main_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -383,11 +383,14 @@ def test_exchange_currency(self):
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__":
Expand Down

0 comments on commit 9341dc2

Please sign in to comment.