pytest-dev / pytest-dev/pytest-django
django-pytest raising DatabaseError in all tests
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 1.5k
- Forks
- 367
- PR merge metrics
- No merged PRs in 30d
Description
Hi,
I was using django-pytest version 3.3.2 and my tests using Django TestCase were running just fine.
Now I started a new project and decided do use the latest django-pytest version, but all my tests are raising an error related to the default database being created in one thread but used in another.
Python: 3.7.2+and 3.6.9
Django Version: 2.2.9 and 2.2
pytest Version: 5.3.2
pytest-django Version: 3.8.0
psycopg2-binary: 2.8.4 (I'm using Postgres)
pytest.ini:
[pytest]
DJANGO_SETTINGS_MODULE = data_api.settings
python_files = tests.py test_*.py *_tests.py
The error:
request = <SubRequest '_django_setup_unittest' for <TestCaseFunction test_data_insert_event_dispatched>>
django_db_blocker = <pytest_django.plugin._DatabaseBlocker object at 0x7fbe81b51550>
@pytest.fixture(autouse=True, scope="class")
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
return
from _pytest.unittest import TestCaseFunction
if "debug" in TestCaseFunction.runtest.__code__.co_names:
# Fix pytest (https://github.com/pytest-dev/pytest/issues/5991), only
# if "self._testcase.debug()" is being used (forward compatible).
from _pytest.monkeypatch import MonkeyPatch
def non_debugging_runtest(self):
self._testcase(result=self)
mp_debug = MonkeyPatch()
mp_debug.setattr("_pytest.unittest.TestCaseFunction.runtest", non_debugging_runtest)
else:
mp_debug = None
> request.getfixturevalue("django_db_setup")
/home/mendes/.virtualenvs/data_domain/lib/python3.7/site-packages/pytest_django/plugin.py:524:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
/home/mendes/.virtualenvs/data_domain/lib/python3.7/site-packages/pytest_django/fixtures.py:108: in django_db_setup
**setup_databases_args
/home/mendes/.virtualenvs/data_domain/lib/python3.7/site-packages/django/test/utils.py:174: in setup_databases
serialize=connection.settings_dict.get('TEST', {}).get('SERIALIZE', True),
/home/mendes/.virtualenvs/data_domain/lib/python3.7/site-packages/django/db/backends/base/creation.py:60: in create_test_db
self.connection.close()
/home/mendes/.virtualenvs/data_domain/lib/python3.7/site-packages/django/db/backends/base/base.py:279: in close
self.validate_thread_sharing()
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
self = <django.db.backends.postgresql.base.DatabaseWrapper object at 0x7fbe810e3a90>
def validate_thread_sharing(self):
"""
Validate that the connection isn't accessed by another thread than the
one which originally created it, unless the connection was explicitly
authorized to be shared between threads (via the `inc_thread_sharing()`
method). Raise an exception if the validation fails.
"""
if not (self.allow_thread_sharing or self._thread_ident == _thread.get_ident()):
raise DatabaseError(
"DatabaseWrapper objects created in a "
"thread can only be used in that same thread. The object "
"with alias '%s' was created in thread id %s and this is "
"thread id %s."
> % (self.alias, self._thread_ident, _thread.get_ident())
)
E django.db.utils.DatabaseError: DatabaseWrapper objects created in a thread can only be used in that same thread. The object with alias 'default' was created in thread id 140456219838272 and this is thread id 140456135896760.
/home/mendes/.virtualenvs/data_domain/lib/python3.7/site-packages/django/db/backends/base/base.py:547: DatabaseError
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
The traceback identifies pytest_django/plugin.py:524 and fixtures.py:108, with Django's setup_databases and PostgreSQL DatabaseWrapper in the call path. Reproduce using the listed Python, Django, pytest, pytest-django, and psycopg2 versions with a Django TestCase; inspect which thread creates and closes the default database connection, and consider the issue done when those tests complete without DatabaseError.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- django, postgresql, python
- Domain
- backend, databases, testing
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100