-
Notifications
You must be signed in to change notification settings - Fork 347
Restores the expected behavior of certain fixtures, while fixing a bug related to fixtures now tearing down properly #807
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
9b11582
870112d
8be7989
a5b8acb
5e53f0a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -462,7 +462,7 @@ def django_test_environment(request): | |
|
||
|
||
@pytest.fixture(scope="session") | ||
def django_db_blocker(): | ||
def _django_db_blocker(): | ||
"""Wrapper around Django's database access. | ||
|
||
This object can be used to re-enable database access. This fixture is used | ||
|
@@ -481,6 +481,28 @@ def django_db_blocker(): | |
return _blocking_manager | ||
|
||
|
||
@pytest.fixture(scope="session") | ||
def django_db_blocker(django_db_setup, _django_db_blocker): # noqa: F811 | ||
"""Wrapper around _django_db_blocker to serve as convenience reference. | ||
|
||
The ``_django_db_blocker`` fixture must be available for the ``django_db_setup`` | ||
fixture, so ``django_db_setup`` must request the ``_django_db_blocker`` fixture. But | ||
in order for ``_django_db_blocker`` to be used, ``django_db_setup`` must also have | ||
been executed, suggesting that ``_django_db_blocker`` should request | ||
``django_db_setup``, especially since it is possible for ``_django_db_blocker`` to | ||
be needed when ``django_db_setup`` wouldn't normally have been run (e.g. if a test | ||
isn't marked with ``pytest.mark.django_db``). | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
This now implicitly "marks" the test then, doesn't it? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe it's almost the same. The only difference being that the DB is still blocked if you don't use the mark. |
||
|
||
This would normally cause a catch-22, but to circumvent this, the | ||
`_django_db_blocker`` fixture is used behind the scenes, while ``django_db_blocker`` | ||
serves as the fixture used by everything that would normally need the blocker (aside | ||
from ``django_db_setup``). This fixture helps coordinate between both | ||
``django_db_setup`` and ``_django_db_blocker``, so that whenever | ||
``django_db_blocker`` gets used, it ensures ``django_db_setup`` is run first. | ||
""" | ||
return _django_db_blocker | ||
|
||
|
||
@pytest.fixture(autouse=True) | ||
def _django_db_marker(request): | ||
"""Implement the django_db marker, internal to pytest-django. | ||
|
@@ -500,7 +522,7 @@ def _django_db_marker(request): | |
|
||
|
||
@pytest.fixture(autouse=True, scope="class") | ||
def _django_setup_unittest(request, django_db_blocker): | ||
def _django_setup_unittest(request, _django_db_blocker): | ||
"""Setup a django unittest, internal to pytest-django.""" | ||
if not django_settings_is_configured() or not is_django_unittest(request): | ||
yield | ||
|
@@ -525,7 +547,7 @@ def non_debugging_runtest(self): | |
|
||
cls = request.node.cls | ||
|
||
with django_db_blocker.unblock(): | ||
with _django_db_blocker.unblock(): | ||
if _handle_unittest_methods: | ||
_restore_class_methods(cls) | ||
cls.setUpClass() | ||
|
@@ -736,7 +758,7 @@ def __exit__(self, exc_type, exc_value, traceback): | |
class _DatabaseBlocker(object): | ||
"""Manager for django.db.backends.base.base.BaseDatabaseWrapper. | ||
|
||
This is the object returned by django_db_blocker. | ||
This is the object returned by _django_db_blocker and django_db_blocker. | ||
""" | ||
|
||
def __init__(self): | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
from .settings_base import * # noqa | ||
from .settings_base import * # noqa: F401 F403 | ||
|
||
DATABASES = { | ||
"default": { | ||
|
Uh oh!
There was an error while loading. Please reload this page.