refactor(backend): remove network dep from test (#12624)

This commit is contained in:
Matthias Mair
2026-08-12 07:44:49 +10:00
committed by GitHub
parent 77d8470141
commit 7cb3d78a5f
+23 -19
View File
@@ -2,7 +2,6 @@
import base64 import base64
import os import os
import time
from datetime import datetime, timedelta from datetime import datetime, timedelta
from decimal import Decimal from decimal import Decimal
from io import BytesIO from io import BytesIO
@@ -22,6 +21,7 @@ from django.urls import reverse
from django.utils import timezone from django.utils import timezone
import pint.errors import pint.errors
import requests_mock
from djmoney.contrib.exchange.exceptions import MissingRate from djmoney.contrib.exchange.exceptions import MissingRate
from djmoney.contrib.exchange.models import Rate, convert_money from djmoney.contrib.exchange.models import Rate, convert_money
from djmoney.money import Money from djmoney.money import Money
@@ -1171,8 +1171,26 @@ class TestVersionNumber(TestCase):
class CurrencyTests(TestCase): class CurrencyTests(TestCase):
"""Unit tests for currency / exchange rate functionality.""" """Unit tests for currency / exchange rate functionality."""
def test_rates(self): RATES = {
'AUD': 1.5,
'CAD': 1.35,
'CNY': 7.2,
'EUR': 0.9,
'GBP': 0.8,
'JPY': 150.0,
'NZD': 1.65,
'USD': 1.0,
}
@requests_mock.Mocker()
def test_rates(self, requests_mocker):
"""Test exchange rate update.""" """Test exchange rate update."""
# Patch setting for remote url
response_rates = {code: self.RATES.get(code, 1.0) for code in currency_codes()}
requests_mocker.get(
'https://api.frankfurter.app/latest', json={'rates': response_rates}
)
# Initially, there will not be any exchange rate information # Initially, there will not be any exchange rate information
rates = Rate.objects.all() rates = Rate.objects.all()
@@ -1185,24 +1203,10 @@ class CurrencyTests(TestCase):
with self.assertRaises(MissingRate): with self.assertRaises(MissingRate):
convert_money(Money(100, 'AUD'), 'USD') convert_money(Money(100, 'AUD'), 'USD')
update_successful = False InvenTree.tasks.update_exchange_rates()
# Note: the update sometimes fails in CI, let's give it a few chances rates = Rate.objects.all()
for idx in range(10): self.assertEqual(rates.count(), len(currency_codes()))
InvenTree.tasks.update_exchange_rates()
rates = Rate.objects.all()
if rates.count() == len(currency_codes()):
update_successful = True
break
else: # pragma: no cover
print('Exchange rate update failed - retrying')
print(f'Expected {currency_codes()}, got {[a.currency for a in rates]}')
time.sleep(1 + idx)
self.assertTrue(update_successful)
# Now that we have some exchange rate information, we can perform conversions # Now that we have some exchange rate information, we can perform conversions