From 65251038fd11ddc8bcb67a0ae034fdd3c91ca689 Mon Sep 17 00:00:00 2001 From: Daniel Miller Date: Mon, 1 Jul 2024 17:10:51 -0400 Subject: [PATCH] Remove django monkey patch from reusedb --- corehq/tests/pytest_plugins/reusedb.py | 12 +++++------- corehq/tests/pytest_plugins/util.py | 12 ++++++++++++ 2 files changed, 17 insertions(+), 7 deletions(-) create mode 100644 corehq/tests/pytest_plugins/util.py diff --git a/corehq/tests/pytest_plugins/reusedb.py b/corehq/tests/pytest_plugins/reusedb.py index e803ee6d93a12..cf1d73f110ae2 100644 --- a/corehq/tests/pytest_plugins/reusedb.py +++ b/corehq/tests/pytest_plugins/reusedb.py @@ -20,6 +20,7 @@ from unittest.mock import Mock, patch import pytest +from pytest_django import fixtures as django_fixtures from pytest_django.fixtures import ( db as django_db, django_db_modify_db_settings, @@ -43,6 +44,7 @@ from corehq.util.test_utils import timelimit, unit_testing_only +from .util import override_fixture from ..tools import nottest log = logging.getLogger(__name__) @@ -172,18 +174,14 @@ def is_still_sorted(items, key): return all(new_key(a) <= new_key(b) for a, b in zip(items, it)) -@pytest.fixture(scope="session") +@override_fixture(django_fixtures.django_db_setup) @use(django_db_modify_db_settings) def django_db_setup(): - """Override pytest_django's django_db_setup fixture + """Override pytest-django's django_db_setup fixture - Replace pytest_django's database setup/teardown with + Replace pytest-django's database setup/teardown with DeferredDatabaseContext, which handles other databases including Couch, Elasticsearch, BlobDB, and Redis. - - There appears to be no explicit dependency between this and the - pytest-django fixture. Is there a race to determine which will - override the other? Beware of magic. """ try: yield diff --git a/corehq/tests/pytest_plugins/util.py b/corehq/tests/pytest_plugins/util.py new file mode 100644 index 0000000000000..31c791264ecbb --- /dev/null +++ b/corehq/tests/pytest_plugins/util.py @@ -0,0 +1,12 @@ +from functools import wraps + + +def override_fixture(old_fixture): + """Override a pytest magic fixture with an unmagic fixture""" + def apply(new_fixture): + @wraps(new_fixture) + def fixture(*a, **k): + yield from new_fixture(*a, **k) + old_fixture.__pytest_wrapped__.obj = fixture + return new_fixture + return apply