Skip to content

Commit d93631f

Browse files
committed
Export pytest_django.DjangoDbBlocker for typing purposes
For users who want to type `django_db_blocker` in their tests.
1 parent 53eead4 commit d93631f

File tree

4 files changed

+37
-17
lines changed

4 files changed

+37
-17
lines changed

docs/database.rst

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -295,19 +295,21 @@ access for the specified block::
295295

296296
You can also manage the access manually via these methods:
297297

298-
.. py:method:: django_db_blocker.unblock()
298+
.. py:class:: pytest_django.DjangoDbBlocker
299299
300-
Enable database access. Should be followed by a call to
301-
:func:`~django_db_blocker.restore`.
300+
.. py:method:: django_db_blocker.unblock()
302301
303-
.. py:method:: django_db_blocker.block()
302+
Enable database access. Should be followed by a call to
303+
:func:`~django_db_blocker.restore` or used as a context manager.
304304

305-
Disable database access. Should be followed by a call to
306-
:func:`~django_db_blocker.restore`.
305+
.. py:method:: django_db_blocker.block()
307306
308-
.. py:method:: django_db_blocker.restore()
307+
Disable database access. Should be followed by a call to
308+
:func:`~django_db_blocker.restore` or used as a context manager.
309309

310-
Restore the previous state of the database blocking.
310+
.. py:method:: django_db_blocker.restore()
311+
312+
Restore the previous state of the database blocking.
311313

312314
Examples
313315
########

pytest_django/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
__version__ = "unknown"
66

77

8+
from .plugin import DjangoDbBlocker
9+
10+
811
__all__ = [
912
"__version__",
13+
"DjangoDbBlocker",
1014
]

pytest_django/fixtures.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
import django
2929
import django.test
3030

31+
from . import DjangoDbBlocker
32+
3133

3234
_DjangoDbDatabases = Optional[Union[Literal["__all__"], Iterable[str]]]
3335
_DjangoDbAvailableApps = Optional[List[str]]
@@ -114,7 +116,7 @@ def django_db_createdb(request: pytest.FixtureRequest) -> bool:
114116
def django_db_setup(
115117
request: pytest.FixtureRequest,
116118
django_test_environment: None,
117-
django_db_blocker,
119+
django_db_blocker: DjangoDbBlocker,
118120
django_db_use_migrations: bool,
119121
django_db_keepdb: bool,
120122
django_db_createdb: bool,
@@ -154,7 +156,7 @@ def django_db_setup(
154156
def _django_db_helper(
155157
request: pytest.FixtureRequest,
156158
django_db_setup: None,
157-
django_db_blocker,
159+
django_db_blocker: DjangoDbBlocker,
158160
) -> Generator[None, None, None]:
159161
from django import VERSION
160162

pytest_django/plugin.py

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import os
1111
import pathlib
1212
import sys
13+
import types
1314
from functools import reduce
1415
from typing import TYPE_CHECKING, ContextManager, Generator, List, NoReturn
1516

@@ -495,7 +496,7 @@ def django_test_environment(request: pytest.FixtureRequest) -> Generator[None, N
495496

496497

497498
@pytest.fixture(scope="session")
498-
def django_db_blocker() -> _DatabaseBlocker | None:
499+
def django_db_blocker() -> DjangoDbBlocker | None:
499500
"""Wrapper around Django's database access.
500501
501502
This object can be used to re-enable database access. This fixture is used
@@ -525,7 +526,7 @@ def _django_db_marker(request: pytest.FixtureRequest) -> None:
525526
@pytest.fixture(autouse=True, scope="class")
526527
def _django_setup_unittest(
527528
request: pytest.FixtureRequest,
528-
django_db_blocker: _DatabaseBlocker,
529+
django_db_blocker: DjangoDbBlocker,
529530
) -> Generator[None, None, None]:
530531
"""Setup a django unittest, internal to pytest-django."""
531532
if not django_settings_is_configured() or not is_django_unittest(request):
@@ -743,23 +744,34 @@ def _django_clear_site_cache() -> None:
743744

744745

745746
class _DatabaseBlockerContextManager:
746-
def __init__(self, db_blocker) -> None:
747+
def __init__(self, db_blocker: DjangoDbBlocker) -> None:
747748
self._db_blocker = db_blocker
748749

749750
def __enter__(self) -> None:
750751
pass
751752

752-
def __exit__(self, exc_type, exc_value, traceback) -> None:
753+
def __exit__(
754+
self,
755+
exc_type: type[BaseException] | None,
756+
exc_value: BaseException | None,
757+
traceback: types.TracebackType | None,
758+
) -> None:
753759
self._db_blocker.restore()
754760

755761

756-
class _DatabaseBlocker:
762+
class DjangoDbBlocker:
757763
"""Manager for django.db.backends.base.base.BaseDatabaseWrapper.
758764
759765
This is the object returned by django_db_blocker.
760766
"""
761767

762-
def __init__(self) -> None:
768+
def __init__(self, *, _ispytest: bool = False) -> None:
769+
if not _ispytest: # pragma: no cover
770+
raise TypeError(
771+
"The DjangoDbBlocker constructor is private. "
772+
"use the django_db_blocker fixture instead."
773+
)
774+
763775
self._history = [] # type: ignore[var-annotated]
764776
self._real_ensure_connection = None
765777

@@ -801,7 +813,7 @@ def restore(self) -> None:
801813
self._dj_db_wrapper.ensure_connection = self._history.pop()
802814

803815

804-
_blocking_manager = _DatabaseBlocker()
816+
_blocking_manager = DjangoDbBlocker(_ispytest=True)
805817

806818

807819
def validate_urls(marker) -> list[str]:

0 commit comments

Comments
 (0)