Fix CI test to actually use the Postgres database#9954
Fix CI test to actually use the Postgres database#9954browniebroke wants to merge 14 commits intoencode:mainfrom
Conversation
…tent PKs instead of complex URL builders
There was a problem hiding this comment.
Pull request overview
This PR completes the CI/PostgreSQL test setup by ensuring tox passes DATABASE_URL through, adjusts pytest execution to avoid migration-related failures, and makes test behavior deterministic across database backends (notably PostgreSQL sequence behavior and queryset ordering).
Changes:
- Configure tox to pass
DATABASE_URLinto test environments so PostgreSQL-required tests are exercised. - Run pytest with
--no-migrationsand adjust tests/fixtures to avoid Postgres-specific failures (missing tables, sequence/PK differences). - Stabilize tests by enforcing deterministic queryset ordering and backend-aware validator expectations.
Reviewed changes
Copilot reviewed 9 out of 9 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
tox.ini |
Passes DATABASE_URL into tox environments to enable Postgres-backed test runs. |
pyproject.toml |
Adds --no-migrations to default pytest options to avoid migration/setup issues. |
tests/conftest.py |
Adds an autouse fixture to reset Postgres sequences between tests for predictable PKs. |
tests/test_validators.py |
Makes validator-count expectation backend-aware using connection.ops.integer_field_range. |
tests/test_relations_slug.py |
Adds order_by('pk') to avoid nondeterministic ordering across DBs. |
tests/test_relations_pk.py |
Adds order_by('pk') to avoid nondeterministic ordering across DBs. |
tests/test_relations_hyperlink.py |
Adds order_by('pk') to avoid nondeterministic ordering across DBs. |
tests/test_filters.py |
Increases a CharField(max_length=...) to avoid Postgres-enforced length failures. |
rest_framework/test.py |
Attempts to prevent DB connection closing during RequestsClient WSGI calls by mutating Django signal handlers. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
This reverts commit 8b41cc5.
a4d2f30 to
d8df0f1
Compare
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 14 out of 14 changed files in this pull request and generated 1 comment.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| request_started.disconnect(close_old_connections) | ||
| request_finished.disconnect(close_old_connections) |
There was a problem hiding this comment.
_keep_connections_open() disconnects close_old_connections and then unconditionally reconnects it in finally. This can mutate global signal state if the receiver was not connected to begin with (or if the context is nested), and it is not thread-safe because signals are process-global. Track whether each disconnect actually removed a receiver (and only reconnect when needed), and consider making the context re-entrant (eg, a counter) to avoid inner contexts re-enabling connection-closing while an outer context is still active.
There was a problem hiding this comment.
I don't understand when that would be a problem in practice... As far as I understand, this is mainly used when running tests, and Django --parrallel test option runs each worker in separate processes, not threads (source).
Django itself does this disconnect/connect in ClientHandler
Description
After merging #9949, I realised that the setup wasn't complete and Postgres tests are in fact skipped:
This is because we run tests with tox and it needs to be instructed to pass environment variables down with the
pass_envoption.Implementation notes
pass_envto the main test env.testenv:baseruns without optional deps (without psycopg), so I unset thepass_envfrom the main test env.psycopg.errors.UndefinedTable: relation "auth_user" does not exist, which I fixed by passing--no-migrationsto pytest..order_by()to fix them.Blocks #9385