Skip to content

Commit 34232eb

Browse files
authored
Fix tests using Postgres (#2362)
- Most Django tests did not use Postgres at all but SQLite. Fixes this - Updates test matrix config so that the test db is always created for frameworks that need a Postgres DB - Fixes the asyncpg tests. - Fixes also Grpc tests (they where running way to often and always did the same thing...)
1 parent 87d582d commit 34232eb

File tree

8 files changed

+37
-11
lines changed

8 files changed

+37
-11
lines changed

.github/workflows/test-integration-asyncpg.yml

+2
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ jobs:
6666
- name: Setup Test Env
6767
run: |
6868
pip install coverage "tox>=3,<4"
69+
psql postgresql://postgres:sentry@localhost:5432 -c "create database ${SENTRY_PYTHON_TEST_POSTGRES_NAME};" || true
70+
psql postgresql://postgres:sentry@localhost:5432 -c "grant all privileges on database ${SENTRY_PYTHON_TEST_POSTGRES_NAME} to ${SENTRY_PYTHON_TEST_POSTGRES_USER};" || true
6971
7072
- name: Test asyncpg
7173
uses: nick-fields/retry@v2

.github/workflows/test-integration-django.yml

+2
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ jobs:
6666
- name: Setup Test Env
6767
run: |
6868
pip install coverage "tox>=3,<4"
69+
psql postgresql://postgres:sentry@localhost:5432 -c "create database ${SENTRY_PYTHON_TEST_POSTGRES_NAME};" || true
70+
psql postgresql://postgres:sentry@localhost:5432 -c "grant all privileges on database ${SENTRY_PYTHON_TEST_POSTGRES_NAME} to ${SENTRY_PYTHON_TEST_POSTGRES_USER};" || true
6971
7072
- name: Test django
7173
uses: nick-fields/retry@v2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
psql postgresql://postgres:sentry@localhost:5432 -c "create database ${SENTRY_PYTHON_TEST_POSTGRES_NAME};" || true
2+
psql postgresql://postgres:sentry@localhost:5432 -c "grant all privileges on database ${SENTRY_PYTHON_TEST_POSTGRES_NAME} to ${SENTRY_PYTHON_TEST_POSTGRES_USER};" || true

scripts/split-tox-gh-actions/ci-yaml-test-snippet.txt

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
- name: Setup Test Env
1515
run: |
1616
pip install coverage "tox>=3,<4"
17+
{{ setup_postgres }}
1718

1819
- name: Test {{ framework }}
1920
uses: nick-fields/retry@v2

scripts/split-tox-gh-actions/split-tox-gh-actions.py

+6
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
TEMPLATE_DIR = Path(__file__).resolve().parent
2828
TEMPLATE_FILE = TEMPLATE_DIR / "ci-yaml.txt"
2929
TEMPLATE_FILE_SERVICES = TEMPLATE_DIR / "ci-yaml-services.txt"
30+
TEMPLATE_FILE_SETUP_DB = TEMPLATE_DIR / "ci-yaml-setup-db.txt"
3031
TEMPLATE_SNIPPET_TEST = TEMPLATE_DIR / "ci-yaml-test-snippet.txt"
3132
TEMPLATE_SNIPPET_TEST_PY27 = TEMPLATE_DIR / "ci-yaml-test-py27-snippet.txt"
3233

@@ -113,6 +114,11 @@ def write_yaml_file(
113114
out += "".join(lines)
114115
f.close()
115116

117+
elif template_line.strip() == "{{ setup_postgres }}":
118+
if current_framework in FRAMEWORKS_NEEDING_POSTGRES:
119+
f = open(TEMPLATE_FILE_SETUP_DB, "r")
120+
out += "".join(f.readlines())
121+
116122
elif template_line.strip() == "{{ check_needs }}":
117123
if py27_supported:
118124
out += CHECK_NEEDS_PY27

tests/integrations/asyncpg/test_asyncpg.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,11 @@
3333
CRUMBS_CONNECT = {
3434
"category": "query",
3535
"data": {
36-
"db.name": "postgres",
36+
"db.name": PG_NAME,
3737
"db.system": "postgresql",
38-
"db.user": "foo",
39-
"server.address": "localhost",
40-
"server.port": 5432,
38+
"db.user": PG_USER,
39+
"server.address": PG_HOST,
40+
"server.port": PG_PORT,
4141
},
4242
"message": "connect",
4343
"type": "default",

tests/integrations/django/myapp/settings.py

+10-2
Original file line numberDiff line numberDiff line change
@@ -121,16 +121,24 @@ def middleware(request):
121121
try:
122122
import psycopg2 # noqa
123123

124+
db_engine = "django.db.backends.postgresql"
125+
try:
126+
from django.db.backends import postgresql # noqa: F401
127+
except ImportError:
128+
db_engine = "django.db.backends.postgresql_psycopg2"
129+
124130
DATABASES["postgres"] = {
125-
"ENGINE": "django.db.backends.postgresql",
131+
"ENGINE": db_engine,
126132
"NAME": os.environ["SENTRY_PYTHON_TEST_POSTGRES_NAME"],
127133
"USER": os.environ["SENTRY_PYTHON_TEST_POSTGRES_USER"],
128134
"PASSWORD": os.environ["SENTRY_PYTHON_TEST_POSTGRES_PASSWORD"],
129135
"HOST": os.environ.get("SENTRY_PYTHON_TEST_POSTGRES_HOST", "localhost"),
130136
"PORT": 5432,
131137
}
132138
except (ImportError, KeyError):
133-
pass
139+
from sentry_sdk.utils import logger
140+
141+
logger.warn("No psycopg2 found, testing with SQLite.")
134142

135143

136144
# Password validation

tox.ini

+10-5
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,8 @@ envlist =
9191
{py3.7}-gcp
9292

9393
# Grpc
94-
{py3.7,py3.8,py3.9,py3.10,py3.11}-grpc-v{1.21.1,1.22.1,1.23.1,1.24.1,1.25.0,1.26.0,1.27.1,1.28.1,1.29.0,1.30.0,1.31.0,1.32.0,1.33.1,1.34.0,1.36.0,1.37.0,1.38.0,1.39.0,1.40.0,1.41.1,1.43.0,1.44.0,1.46.1,1.48.1,1.51.3,1.53.0}
94+
{py3.7,py3.8,py3.9,py3.10}-grpc-v{1.40,1.44,1.48}
95+
{py3.7,py3.8,py3.9,py3.10,py3.11}-grpc-v{1.54,1.56,1.58}
9596

9697
# HTTPX
9798
{py3.6,py3.7,py3.8,py3.9}-httpx-v{0.16,0.17,0.18}
@@ -248,20 +249,19 @@ deps =
248249
{py3.8}-chalice: botocore~=1.31
249250

250251
# Django
252+
django: psycopg2-binary
251253
django: Werkzeug<2.1.0
252254
django-v{1.11,2.0,2.1,2.2,3.0,3.1,3.2}: djangorestframework>=3.0.0,<4.0.0
253255

254-
{py3.7,py3.8,py3.9,py3.10,py3.11}-django-v{1.11,2.0,2.1,2.2,3.0,3.1,3.2}: channels[daphne]>2
255256
{py3.7,py3.8,py3.9,py3.10,py3.11}-django-v{1.11,2.0,2.1,2.2,3.0,3.1,3.2}: pytest-asyncio
256-
{py2.7,py3.7,py3.8,py3.9,py3.10,py3.11}-django-v{1.11,2.2,3.0,3.1,3.2}: psycopg2-binary
257+
{py3.7,py3.8,py3.9,py3.10,py3.11}-django-v{1.11,2.0,2.1,2.2,3.0,3.1,3.2}: channels[daphne]>2
257258

258259
django-v{1.8,1.9,1.10,1.11,2.0,2.1}: pytest-django<4.0
259260
django-v{2.2,3.0,3.1,3.2}: pytest-django>=4.0
260261
django-v{2.2,3.0,3.1,3.2}: Werkzeug<2.0
261262

262263
django-v{4.0,4.1}: djangorestframework
263264
django-v{4.0,4.1}: pytest-asyncio
264-
django-v{4.0,4.1}: psycopg2-binary
265265
django-v{4.0,4.1}: pytest-django
266266
django-v{4.0,4.1}: Werkzeug
267267

@@ -310,7 +310,12 @@ deps =
310310
{py2.7,py3.6,py3.7,py3.8,py3.9,py3.10,py3.11}-gevent: gevent>=22.10.0, <22.11.0
311311

312312
# Grpc
313-
grpc: grpcio-tools
313+
grpc-v1.40: grpcio-tools>=1.40.0,<1.41.0
314+
grpc-v1.44: grpcio-tools>=1.44.0,<1.45.0
315+
grpc-v1.48: grpcio-tools>=1.48.0,<1.49.0
316+
grpc-v1.54: grpcio-tools>=1.54.0,<1.55.0
317+
grpc-v1.56: grpcio-tools>=1.56.0,<1.57.0
318+
grpc-v1.58: grpcio-tools>=1.58.0,<1.59.0
314319
grpc: protobuf
315320
grpc: mypy-protobuf
316321
grpc: types-protobuf

0 commit comments

Comments
 (0)