Skip to content

Commit

Permalink
Do not unblock db for SimpleTestCase tests
Browse files Browse the repository at this point in the history
  • Loading branch information
millerdev committed Jul 26, 2024
1 parent a9d02d7 commit a0fb589
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
20 changes: 19 additions & 1 deletion corehq/tests/pytest_plugins/reusedb.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@
import logging
import os
import sys
from contextlib import ExitStack, contextmanager
from contextlib import ExitStack, contextmanager, nullcontext
from pathlib import Path
from unittest.mock import Mock, patch

import pytest
from pytest_django import fixtures as django_fixtures
from pytest_django import plugin as django_plugin
from pytest_django.fixtures import (
db as django_db,
django_db_modify_db_settings,
Expand Down Expand Up @@ -189,6 +190,23 @@ def django_db_setup():
_db_context.teardown_databases()


@override_fixture(django_plugin._django_setup_unittest)
def _django_setup_unittest():
"""Do not unblock db for SimpleTestCase tests
Why is this not the default behavior of pytest-django?
"""
from django.test import SimpleTestCase
request = get_fixture_value("request")
test_class = getattr(request, "cls", None)
if test_class and issubclass(test_class, SimpleTestCase):
class db_blocker:
unblock = nullcontext
else:
db_blocker = get_fixture_value("django_db_blocker")
yield from _django_setup_unittest.super(request, db_blocker)


class DeferredDatabaseContext:

def setup_before_first_db_test(self, tests, is_db_test, session):
Expand Down
7 changes: 6 additions & 1 deletion corehq/tests/pytest_plugins/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,16 @@


def override_fixture(old_fixture):
"""Override a pytest magic fixture with an unmagic fixture"""
"""Override a pytest magic fixture with an unmagic fixture
The overriding fixture function will be assigned a 'super'
attribute that references the overridden fixture function.
"""
def apply(new_fixture):
@wraps(new_fixture)
def fixture(*a, **k):
yield from new_fixture(*a, **k)
new_fixture.super = old_fixture.__pytest_wrapped__.obj
old_fixture.__pytest_wrapped__.obj = fixture
return new_fixture
return apply

0 comments on commit a0fb589

Please sign in to comment.