Skip to content
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

plugin: unconfigure should restore not unblock #1151

Merged
merged 3 commits into from
Sep 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
.PHONY: docs test clean fix

test:
tox -e py311-dj42-sqlite_file
tox -e py-dj42-sqlite_file

docs:
tox -e docs
Expand Down
21 changes: 17 additions & 4 deletions pytest_django/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -477,10 +477,13 @@ def get_order_number(test: pytest.Item) -> int:


def pytest_unconfigure(config: pytest.Config) -> None:
if blocking_manager_key not in config.stash:
return
blocking_manager = config.stash[blocking_manager_key]
blocking_manager.unblock()
# Undo the block() in _setup_django(), if it happenned.
# It's also possible the user forgot to call restore().
# We can warn about it, but let's just clean it up.
if blocking_manager_key in config.stash:
blocking_manager = config.stash[blocking_manager_key]
while blocking_manager.is_active:
blocking_manager.restore()


@pytest.fixture(autouse=True, scope="session")
Expand Down Expand Up @@ -845,8 +848,18 @@ def block(self) -> ContextManager[None]:
return _DatabaseBlockerContextManager(self)

def restore(self) -> None:
"""Undo a previous call to block() or unblock().

Consider using block() and unblock() as context managers instead of
manually calling restore().
"""
self._dj_db_wrapper.ensure_connection = self._history.pop()

@property
def is_active(self) -> bool:
"""Whether a block() or unblock() is currently active."""
return bool(self._history)


# On Config.stash.
blocking_manager_key = pytest.StashKey[DjangoDbBlocker]()
Expand Down