Skip to content

Commit bf45db8

Browse files
author
Lode Rosseel
committed
Remove sync code
1 parent 19505bc commit bf45db8

File tree

5 files changed

+7
-66
lines changed

5 files changed

+7
-66
lines changed

docs/database.rst

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ When using the database in a test with transaction rollback, you must ensure tha
118118
database access is only done from the same thread that the test is running on.
119119

120120
To avoid your fixtures/tests making changes outside the test thread, and as a result, the transaction, pytest-django
121-
actively restricts where database connections may be opened:
121+
actively restricts where database connections may be opened in async tests:
122122

123123
- In async tests using ``db``: database access is only allowed from the single
124124
thread used by ``SyncToAsync``. Using sync fixtures that touch the database in
@@ -132,14 +132,6 @@ actively restricts where database connections may be opened:
132132
or by requesting ``transactional_db`` if you must keep sync fixtures.
133133
See :ref:`async-db-behavior` for more details.
134134

135-
- In sync tests: database access is only allowed from the main thread. Attempting to use the database connection
136-
from a different thread will raise::
137-
138-
RuntimeError: Database access is only allowed in the main thread, modify your
139-
test fixtures to be sync or use the transactional_db fixture.
140-
141-
Fix this by ensuring all database transactions run in the main thread (e.g., avoiding the use of async fixtures),
142-
or use ``transactional_db`` to allow mixing.
143135

144136

145137
.. _`multi-db`:

pytest_django/fixtures.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ def _sync_django_db_helper(
282282
"django_db_serialized_rollback" in request.fixturenames
283283
)
284284

285-
with django_db_blocker.unblock(sync_only=not transactional):
285+
with django_db_blocker.unblock():
286286
import django.db
287287
import django.test
288288

pytest_django/plugin.py

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@
5757

5858

5959
if TYPE_CHECKING:
60-
from typing import Any, NoReturn, Callable
60+
from typing import Any, Callable, NoReturn
6161

6262
import django
6363

@@ -857,7 +857,7 @@ def _unblocked_async_only(self, wrapper_self: Any, *args, **kwargs):
857857
"modify your test fixtures to be async or use the transactional_db fixture."
858858
"See https://pytest-django.readthedocs.io/en/latest/database.html#db-thread-safeguards for more information."
859859
)
860-
elif self._real_ensure_connection is not None:
860+
if self._real_ensure_connection is not None:
861861
self._real_ensure_connection(wrapper_self, *args, **kwargs)
862862

863863
def _unblocked_sync_only(self, wrapper_self: Any, *args, **kwargs):
@@ -868,21 +868,13 @@ def _unblocked_sync_only(self, wrapper_self: Any, *args, **kwargs):
868868
"modify your test fixtures to be sync or use the transactional_db fixture."
869869
"See https://pytest-django.readthedocs.io/en/latest/database.html#db-thread-safeguards for more information."
870870
)
871-
elif self._real_ensure_connection is not None:
871+
if self._real_ensure_connection is not None:
872872
self._real_ensure_connection(wrapper_self, *args, **kwargs)
873873

874-
def unblock(self, sync_only=False, async_only=False) -> AbstractContextManager[None]:
874+
def unblock(self, async_only=False) -> AbstractContextManager[None]:
875875
"""Enable access to the Django database."""
876-
if sync_only and async_only:
877-
raise ValueError("Cannot use both sync_only and async_only. Choose at most one.")
878876
self._save_active_wrapper()
879-
if sync_only:
880-
881-
def _method(wrapper_self, *args, **kwargs):
882-
return self._unblocked_sync_only(wrapper_self, *args, **kwargs)
883-
884-
self._dj_db_wrapper.ensure_connection = _method
885-
elif async_only:
877+
if async_only:
886878

887879
def _method(wrapper_self, *args, **kwargs):
888880
return self._unblocked_async_only(wrapper_self, *args, **kwargs)

tests/test_db_thread_safeguards.py

Lines changed: 0 additions & 36 deletions
This file was deleted.

tests/test_fixtures.py

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -788,13 +788,6 @@ def test_unblock_with_block(self, django_db_blocker: DjangoDbBlocker) -> None:
788788
with django_db_blocker.unblock():
789789
Item.objects.exists()
790790

791-
def test_unblock_with_both_flags_raises_valueerror(
792-
self, django_db_blocker: DjangoDbBlocker
793-
) -> None:
794-
# When both sync_only and async_only are True, unblock should reject with ValueError
795-
with pytest.raises(ValueError, match="Cannot use both sync_only and async_only"):
796-
django_db_blocker.unblock(sync_only=True, async_only=True)
797-
798791

799792
def test_mail(mailoutbox) -> None:
800793
assert mailoutbox is mail.outbox # check that mail.outbox and fixture value is same object

0 commit comments

Comments
 (0)