From 853577dde214dd55e3d14a0d15c7bf052bd57a46 Mon Sep 17 00:00:00 2001 From: Sergei Maertens Date: Fri, 9 Jun 2023 10:59:46 +0200 Subject: [PATCH] :green_heart: [#2762] -- disable requests logging in tests/CI --- docs/developers/backend/tests.rst | 26 ++++++++++++++++++++++++++ src/openforms/conf/ci.py | 6 ++++++ src/openforms/conf/dev.py | 3 +++ 3 files changed, 35 insertions(+) diff --git a/docs/developers/backend/tests.rst b/docs/developers/backend/tests.rst index e940bb3cab..034fd21159 100644 --- a/docs/developers/backend/tests.rst +++ b/docs/developers/backend/tests.rst @@ -107,3 +107,29 @@ Example custom command: NO_E2E_HEADLESS=1 E2E_DRIVER=firefox python src/manage.py test src --tag=e2e .. note:: Only the presence of the ``NO_E2E_HEADLESS`` is checked, not the value + +Known issues +============ + +**AssertionError: Database queries to 'default' are not allowed in SimpleTestCase subclasses.** + +These are often caused by django-solo ``SingletonModel`` sucblasses that are being +called somewhere, e.g. ``GlobalConfiguration.get_solo``. Sometimes they fetch from +cache, sometimes there is a cache miss and a database query is needed (e.g. when running +tests in reverse). + +This is typically a test-isolation smell and the root cause should be fixed. This may +also be caused indirectly if you have ``LOG_REQUESTS`` set to ``True`` in your local +``.env``, as it also results in a django-solo lookup. + +The preferred approach to mitigate these kind of issues is to mock the ``get_solo`` call +to prevent cache or DB hits: + +.. code-block:: python + + @unittest.mock.patch( + "path.to.module.using_the_model.GlobalConfiguration.get_solo", + return_value=GlobalConfiguration(...), + ) + def test_something(self, mock_get_solo): + ... diff --git a/src/openforms/conf/ci.py b/src/openforms/conf/ci.py index 5284e49338..75620e9ff7 100644 --- a/src/openforms/conf/ci.py +++ b/src/openforms/conf/ci.py @@ -8,6 +8,12 @@ os.environ.setdefault("IS_HTTPS", "no") os.environ.setdefault("SECRET_KEY", "dummy") +# Do not log requests in CI/tests: +# +# * overhead making tests slower +# * it conflicts with SimpleTestCase in some cases when the run-time configuration is +# looked up from the django-solo model +os.environ.setdefault("LOG_REQUESTS", "no") from .base import * # noqa isort:skip diff --git a/src/openforms/conf/dev.py b/src/openforms/conf/dev.py index 58f7f492ba..2cede36e57 100644 --- a/src/openforms/conf/dev.py +++ b/src/openforms/conf/dev.py @@ -22,6 +22,9 @@ ) os.environ.setdefault("RELEASE", "dev") os.environ.setdefault("SDK_RELEASE", "latest") +# otherwise the test suite is flaky due to logging config lookups to the DB in +# non-DB test cases +os.environ.setdefault("LOG_REQUESTS", "no") from .base import * # noqa isort:skip