-
-
Notifications
You must be signed in to change notification settings - Fork 7.1k
Fix CI test to actually use the Postgres database #9954
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
base: main
Are you sure you want to change the base?
Changes from 9 commits
7b4b2f2
85ea3cb
ee1164c
e6b6e98
bb7559b
2ce3d4d
6b361df
8064ef6
c0c4d13
8b41cc5
2bcb063
7fbbf9b
d8df0f1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,7 +3,36 @@ | |
| import dj_database_url | ||
| import django | ||
| import pytest | ||
| from django.apps import apps | ||
| from django.core import management | ||
| from django.core.management.color import no_style | ||
| from django.db import connection | ||
| from django.test import TestCase, TransactionTestCase | ||
|
|
||
|
|
||
| @pytest.fixture(autouse=True) | ||
| def _reset_sequences(request): | ||
| """Reset all database sequences so PKs start from 1 in each test. | ||
|
|
||
| PostgreSQL sequences are non-transactional and persist across | ||
| TestCase's transaction rollbacks. This fixture ensures every test | ||
| gets predictable PKs starting from 1 regardless of execution order. | ||
| No-op on SQLite and skipped for tests that don't use the database. | ||
| """ | ||
| if connection.vendor != 'postgresql': | ||
| return | ||
| # Only run for tests that actually have database access. | ||
| if not (request.cls and issubclass(request.cls, (TestCase, TransactionTestCase))): | ||
| if 'db' not in request.fixturenames and 'transactional_db' not in request.fixturenames: | ||
| return | ||
|
|
||
| table_names = set(connection.introspection.table_names()) | ||
| models = [m for m in apps.get_models() if m._meta.db_table in table_names] | ||
| sql_list = connection.ops.sequence_reset_sql(no_style(), models) | ||
| if sql_list: | ||
| with connection.cursor() as cursor: | ||
| for sql in sql_list: | ||
| cursor.execute(sql) | ||
|
Comment on lines
+29
to
+35
|
||
|
|
||
|
|
||
| def pytest_addoption(parser): | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Temporarily disconnecting/reconnecting global
request_started/request_finishedsignal receivers insideDjangoTestAdapter.send()is not thread-safe: concurrent requests in the same process could run while the handlers are disconnected, changing behavior outside this request. Also, ifclose_old_connectionswas connected with adispatch_uid(or otherwise not matched by thedisconnect()call), theconnect()infinallycan add a duplicate receiver. A safer approach is to avoid mutating global signals here (e.g., use Django’sClientHandlerfor the WSGI call path, or otherwise ensure disconnect/connect is idempotent via the samedispatch_uidthat Django uses).