Skip to content

Commit

Permalink
made some fixtures yield, and restructured django_db_blocker fixture
Browse files Browse the repository at this point in the history
  • Loading branch information
SalmonMode committed Jan 18, 2020
1 parent debecc8 commit 63a1d57
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 16 deletions.
18 changes: 9 additions & 9 deletions pytest_django/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def django_db_createdb(request):
def django_db_setup(
request,
django_test_environment,
django_db_blocker,
_django_db_blocker,
django_db_use_migrations,
django_db_keepdb,
django_db_createdb,
Expand All @@ -101,15 +101,16 @@ def django_db_setup(
if django_db_keepdb and not django_db_createdb:
setup_databases_args["keepdb"] = True

with django_db_blocker.unblock():
with _django_db_blocker.unblock():
db_cfg = setup_databases(
verbosity=request.config.option.verbose,
interactive=False,
**setup_databases_args
)

def teardown_database():
with django_db_blocker.unblock():
yield
if not django_db_keepdb:
with _django_db_blocker.unblock():
try:
teardown_databases(db_cfg, verbosity=request.config.option.verbose)
except Exception as exc:
Expand All @@ -119,9 +120,6 @@ def teardown_database():
)
)

if not django_db_keepdb:
request.addfinalizer(teardown_database)


def _django_db_fixture_helper(
request, django_db_blocker, transactional=False, reset_sequences=False
Expand Down Expand Up @@ -191,7 +189,7 @@ def _set_suffix_to_test_databases(suffix):


@pytest.fixture(scope="function")
def db(request, django_db_setup, django_db_blocker):
def db(request, django_db_blocker):
"""Require a django test database.
This database will be setup with the default fixtures and will have
Expand Down Expand Up @@ -429,13 +427,15 @@ def _live_server_helper(request):
It will also override settings only for the duration of the test.
"""
if "live_server" not in request.fixturenames:
yield
return

request.getfixturevalue("transactional_db")

live_server = request.getfixturevalue("live_server")
live_server._live_server_modified_settings.enable()
request.addfinalizer(live_server._live_server_modified_settings.disable)
yield
live_server._live_server_modified_settings.disable()


@contextmanager
Expand Down
30 changes: 26 additions & 4 deletions pytest_django/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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):
"""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``).
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.
Expand All @@ -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
Expand All @@ -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()
Expand Down Expand Up @@ -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):
Expand Down
2 changes: 1 addition & 1 deletion tests/test_db_access_in_repr.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ def test_db_access_with_repr_in_report(django_testdir):
from .app.models import Item
def test_via_db_blocker(django_db_setup, django_db_blocker):
def test_via_db_blocker(django_db_blocker):
with django_db_blocker.unblock():
Item.objects.get(name='This one is not there')
Expand Down
2 changes: 0 additions & 2 deletions tests/test_fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -617,15 +617,13 @@ def test_block_with_block(self, django_db_blocker):
with pytest.raises(RuntimeError):
Item.objects.exists()

@pytest.mark.django_db
def test_unblock_manually(self, django_db_blocker):
try:
django_db_blocker.unblock()
Item.objects.exists()
finally:
django_db_blocker.restore()

@pytest.mark.django_db
def test_unblock_with_block(self, django_db_blocker):
with django_db_blocker.unblock():
Item.objects.exists()
Expand Down

0 comments on commit 63a1d57

Please sign in to comment.