From 7cb3d78a5fc7c8faba9eb9567b5b1c1409f84f3b Mon Sep 17 00:00:00 2001 From: Matthias Mair Date: Tue, 11 Aug 2026 23:44:49 +0200 Subject: [PATCH] refactor(backend): remove network dep from test (#12624) --- src/backend/InvenTree/InvenTree/tests.py | 42 +++++++++++++----------- 1 file changed, 23 insertions(+), 19 deletions(-) diff --git a/src/backend/InvenTree/InvenTree/tests.py b/src/backend/InvenTree/InvenTree/tests.py index a0fe391bec..d3d04c9194 100644 --- a/src/backend/InvenTree/InvenTree/tests.py +++ b/src/backend/InvenTree/InvenTree/tests.py @@ -2,7 +2,6 @@ import base64 import os -import time from datetime import datetime, timedelta from decimal import Decimal from io import BytesIO @@ -22,6 +21,7 @@ from django.urls import reverse from django.utils import timezone import pint.errors +import requests_mock from djmoney.contrib.exchange.exceptions import MissingRate from djmoney.contrib.exchange.models import Rate, convert_money from djmoney.money import Money @@ -1171,8 +1171,26 @@ class TestVersionNumber(TestCase): class CurrencyTests(TestCase): """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.""" + # 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 rates = Rate.objects.all() @@ -1185,24 +1203,10 @@ class CurrencyTests(TestCase): with self.assertRaises(MissingRate): 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 - for idx in range(10): - 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) + rates = Rate.objects.all() + self.assertEqual(rates.count(), len(currency_codes())) # Now that we have some exchange rate information, we can perform conversions