Files
lasuite-docs/src/backend/core/tests/utils/urls.py
T
Manuel Raynaud a832ac2b82 (backend) correctly reload urls in tests
After removing most of the usage of S3 in the tests, these ones are
faster and make some flakyness more relevant. For example, in tests
related to the external api we have to reload the urls based on the
settings. We now have some race conditions where tests collapsed and
urls are not correctly reloaded.
2026-08-13 14:32:12 +02:00

52 lines
1.4 KiB
Python

"""Utils for testing URLs."""
import importlib
from django.urls import clear_url_caches
class _URLConf:
"""
Whether a test reloaded the URLs of this process.
The URLconf is module-level state: a reload outlives the test that did it
and every test running after it in the same worker sees its routes — which
ones share a worker changes from one run to the next. `restore_urls` puts
the default back, and this flag keeps it to the tests that need it.
"""
reloaded = False
def _reload():
"""Reload the URL modules and drop the resolver caches."""
import core.urls # pylint:disable=import-outside-toplevel # noqa: PLC0415
import impress.urls # pylint:disable=import-outside-toplevel # noqa: PLC0415
importlib.reload(core.urls)
importlib.reload(impress.urls)
clear_url_caches()
def reload_urls():
"""
Reload the URLs. Since the URLs are loaded based on a
settings value, we need to reload them to make the
URL settings based condition effective.
"""
_URLConf.reloaded = True
_reload()
def restore_urls():
"""
Reload the URLs of a test that changed them, so the next one starts clean.
Called once the settings of the test are restored, so the routes are the
ones the settings of the project declare.
"""
if _URLConf.reloaded:
_URLConf.reloaded = False
_reload()