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

Database connection issue #1066

Open
ColeDCrawford opened this issue May 23, 2023 · 0 comments
Open

Database connection issue #1066

ColeDCrawford opened this issue May 23, 2023 · 0 comments

Comments

@ColeDCrawford
Copy link

ColeDCrawford commented May 23, 2023

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) ======================================================================================================================
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant