pytest-dev / pytest-dev/pytest-django
Database connection issue
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 1.5k
- Forks
- 367
- PR merge metrics
- No merged PRs in 30d
Description
I am using pytest-django and pytest-docker; pretty sure my issue is with pytest-django but wanted to mention that.
Any tests I run that enable a database connection using the db fixture or @pytest.mark.django_db mark error with django.db.utils.OperationalError: connection is bad: nodename nor servname provided, or not known. I can run the test container fine, and a basic fixture I am using to wait to run tests until the application is running also works. I am running my migrations in an entrypoint script; I can see from Docker Desktop logs that they are running correctly. The entrypoint also ensures that the Django container waits for Postgres to be available before trying to run the migrations or server. I can see that http_service fixture which waits for Django to be available before running tests is succeeding, so Django itself seems to be running correctly (returns a 200 when visiting the root of the site and a 404 for a non-existent URL). But any tests which use pytest.mark.django_db error out. I'm not finding much for this connection is bad error; the RuntimeWarning is confusing, as the container runs fine and I can exec in and run the exact same commands as my test without issue.
E django.db.utils.OperationalError: connection is bad: nodename nor servname provided, or not known
../amendvenv/lib/python3.11/site-packages/psycopg/connection.py:728: OperationalError
------------------------------------------------------------------------------------------------------------------------------------ Captured stdout setup ------------------------------------------------------------------------------------------------------------------------------------
<Response [404]>
------------------------------------------------------------------------------------------------------------------------------------ Captured stderr setup ------------------------------------------------------------------------------------------------------------------------------------
Creating test database for alias 'default' ('test_amend')...
====================================================================================================================================== warnings summary =======================================================================================================================================
tests/unit/test_data.py::test_insert_state
/Users/colecrawford/GitHub/amendments-project/amendvenv/lib/python3.11/site-packages/django/db/backends/postgresql/base.py:405: RuntimeWarning: Normally Django will use a connection to the 'postgres' database to avoid running initialization queries against the production database when it's not needed (for example, when running tests). Django was unable to create a connection to the 'postgres' database and will use the first PostgreSQL database instead.
warnings.warn(
-- Docs: https://docs.pytest.org/en/stable/how-to/capture-warnings.html
=================================================================================================================================== short test summary info ===================================================================================================================================
ERROR tests/unit/test_data.py::test_insert_state - django.db.utils.OperationalError: connection is bad: nodename nor servname provided, or not known
====================================================================================================================== 1 passed, 1 warning, 1 error in 107.75s (0:01:47) ======================================================================================================================
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
Reproduce tests/unit/test_data.py::test_insert_state with the db fixture or django_db mark, then compare the test connection settings with the Docker entrypoint and the working commands run inside the container. Done means the database-enabled test creates its test database and passes without the OperationalError or warning.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- django, docker, 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