Skip to content

Commit 9711b3b

Browse files
authored
tests: Move asyncpg under toxgen (#4757)
- remove hardcoded asyncpg config, let toxgen take care of generating it - isolate DB so that multiple asyncpg test suites for different envs can run at the same time without touching the same DB - update instructions on running the test suite locally Ref #4506
1 parent 58a9827 commit 9711b3b

File tree

6 files changed

+53
-34
lines changed

6 files changed

+53
-34
lines changed

.github/workflows/test-integrations-dbs.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ jobs:
2929
strategy:
3030
fail-fast: false
3131
matrix:
32-
python-version: ["3.7","3.8","3.11","3.12","3.13"]
32+
python-version: ["3.7","3.12","3.13"]
3333
# python3.6 reached EOL and is no longer being supported on
3434
# new versions of hosted runners on Github Actions
3535
# ubuntu-20.04 is the last version that supported python3.6

scripts/populate_tox/config.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,13 @@
3636
"<=0.23": ["pydantic<2"],
3737
},
3838
},
39+
"asyncpg": {
40+
"package": "asyncpg",
41+
"deps": {
42+
"*": ["pytest-asyncio"],
43+
},
44+
"python": ">=3.7",
45+
},
3946
"beam": {
4047
"package": "apache-beam",
4148
"python": ">=3.7",

scripts/populate_tox/populate_tox.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@
6767
"potel",
6868
# Integrations that can be migrated -- we should eventually remove all
6969
# of these from the IGNORE list
70-
"asyncpg",
7170
"boto3",
7271
"chalice",
7372
"gcp",

scripts/populate_tox/tox.jinja

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,6 @@ envlist =
3939
# Asgi
4040
{py3.7,py3.12,py3.13}-asgi
4141

42-
# asyncpg
43-
{py3.7,py3.10}-asyncpg-v{0.23}
44-
{py3.8,py3.11,py3.12}-asyncpg-latest
45-
4642
# AWS Lambda
4743
{py3.8,py3.9,py3.11,py3.13}-aws_lambda
4844

@@ -160,11 +156,6 @@ deps =
160156
asgi: pytest-asyncio
161157
asgi: async-asgi-testclient
162158
163-
# Asyncpg
164-
asyncpg-v0.23: asyncpg~=0.23.0
165-
asyncpg-latest: asyncpg
166-
asyncpg: pytest-asyncio
167-
168159
# AWS Lambda
169160
aws_lambda: aws-cdk-lib
170161
aws_lambda: aws-sam-cli

tests/integrations/asyncpg/test_asyncpg.py

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,13 @@
33
44
Tests need a local postgresql instance running, this can best be done using
55
```sh
6-
docker run --rm --name some-postgres -e POSTGRES_USER=foo -e POSTGRES_PASSWORD=bar -d -p 5432:5432 postgres
6+
docker run --rm --name some-postgres -e POSTGRES_USER=postgres -e POSTGRES_PASSWORD=sentry -d -p 5432:5432 postgres
77
```
88
99
The tests use the following credentials to establish a database connection.
1010
"""
1111

1212
import os
13-
14-
15-
PG_HOST = os.getenv("SENTRY_PYTHON_TEST_POSTGRES_HOST", "localhost")
16-
PG_PORT = int(os.getenv("SENTRY_PYTHON_TEST_POSTGRES_PORT", "5432"))
17-
PG_USER = os.getenv("SENTRY_PYTHON_TEST_POSTGRES_USER", "postgres")
18-
PG_PASSWORD = os.getenv("SENTRY_PYTHON_TEST_POSTGRES_PASSWORD", "sentry")
19-
PG_NAME = os.getenv("SENTRY_PYTHON_TEST_POSTGRES_NAME", "postgres")
20-
2113
import datetime
2214
from contextlib import contextmanager
2315
from unittest import mock
@@ -33,6 +25,19 @@
3325
from sentry_sdk.tracing_utils import record_sql_queries
3426
from tests.conftest import ApproxDict
3527

28+
PG_HOST = os.getenv("SENTRY_PYTHON_TEST_POSTGRES_HOST", "localhost")
29+
PG_PORT = int(os.getenv("SENTRY_PYTHON_TEST_POSTGRES_PORT", "5432"))
30+
PG_USER = os.getenv("SENTRY_PYTHON_TEST_POSTGRES_USER", "postgres")
31+
PG_PASSWORD = os.getenv("SENTRY_PYTHON_TEST_POSTGRES_PASSWORD", "sentry")
32+
PG_NAME_BASE = os.getenv("SENTRY_PYTHON_TEST_POSTGRES_NAME", "postgres")
33+
34+
35+
def _get_db_name():
36+
pid = os.getpid()
37+
return f"{PG_NAME_BASE}_{pid}"
38+
39+
40+
PG_NAME = _get_db_name()
3641

3742
PG_CONNECTION_URI = "postgresql://{}:{}@{}/{}".format(
3843
PG_USER, PG_PASSWORD, PG_HOST, PG_NAME
@@ -55,6 +60,21 @@
5560

5661
@pytest_asyncio.fixture(autouse=True)
5762
async def _clean_pg():
63+
# Create the test database if it doesn't exist
64+
default_conn = await connect(
65+
"postgresql://{}:{}@{}".format(PG_USER, PG_PASSWORD, PG_HOST)
66+
)
67+
try:
68+
# Check if database exists, create if not
69+
result = await default_conn.fetchval(
70+
"SELECT 1 FROM pg_database WHERE datname = $1", PG_NAME
71+
)
72+
if not result:
73+
await default_conn.execute(f'CREATE DATABASE "{PG_NAME}"')
74+
finally:
75+
await default_conn.close()
76+
77+
# Now connect to our test database and set up the table
5878
conn = await connect(PG_CONNECTION_URI)
5979
await conn.execute("DROP TABLE IF EXISTS users")
6080
await conn.execute(

tox.ini

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
# The file (and all resulting CI YAMLs) then need to be regenerated via
1111
# "scripts/generate-test-files.sh".
1212
#
13-
# Last generated: 2025-09-04T12:59:44.328902+00:00
13+
# Last generated: 2025-09-04T13:56:54.117272+00:00
1414

1515
[tox]
1616
requires =
@@ -39,10 +39,6 @@ envlist =
3939
# Asgi
4040
{py3.7,py3.12,py3.13}-asgi
4141

42-
# asyncpg
43-
{py3.7,py3.10}-asyncpg-v{0.23}
44-
{py3.8,py3.11,py3.12}-asyncpg-latest
45-
4642
# AWS Lambda
4743
{py3.8,py3.9,py3.11,py3.13}-aws_lambda
4844

@@ -135,12 +131,12 @@ envlist =
135131
{py3.8,py3.11,py3.12}-openai-base-v1.0.1
136132
{py3.8,py3.11,py3.12}-openai-base-v1.36.1
137133
{py3.8,py3.11,py3.12}-openai-base-v1.71.0
138-
{py3.8,py3.12,py3.13}-openai-base-v1.105.0
134+
{py3.8,py3.12,py3.13}-openai-base-v1.106.0
139135

140136
{py3.8,py3.11,py3.12}-openai-notiktoken-v1.0.1
141137
{py3.8,py3.11,py3.12}-openai-notiktoken-v1.36.1
142138
{py3.8,py3.11,py3.12}-openai-notiktoken-v1.71.0
143-
{py3.8,py3.12,py3.13}-openai-notiktoken-v1.105.0
139+
{py3.8,py3.12,py3.13}-openai-notiktoken-v1.106.0
144140

145141
{py3.9,py3.12,py3.13}-langgraph-v0.6.6
146142
{py3.10,py3.12,py3.13}-langgraph-v1.0.0a2
@@ -157,6 +153,11 @@ envlist =
157153

158154

159155
# ~~~ DBs ~~~
156+
{py3.7,py3.8,py3.9}-asyncpg-v0.23.0
157+
{py3.7,py3.9,py3.10}-asyncpg-v0.25.0
158+
{py3.7,py3.9,py3.10}-asyncpg-v0.27.0
159+
{py3.8,py3.11,py3.12}-asyncpg-v0.30.0
160+
160161
{py3.7,py3.11,py3.12}-clickhouse_driver-v0.2.9
161162

162163
{py3.6}-pymongo-v3.5.1
@@ -362,11 +363,6 @@ deps =
362363
asgi: pytest-asyncio
363364
asgi: async-asgi-testclient
364365

365-
# Asyncpg
366-
asyncpg-v0.23: asyncpg~=0.23.0
367-
asyncpg-latest: asyncpg
368-
asyncpg: pytest-asyncio
369-
370366
# AWS Lambda
371367
aws_lambda: aws-cdk-lib
372368
aws_lambda: aws-sam-cli
@@ -514,7 +510,7 @@ deps =
514510
openai-base-v1.0.1: openai==1.0.1
515511
openai-base-v1.36.1: openai==1.36.1
516512
openai-base-v1.71.0: openai==1.71.0
517-
openai-base-v1.105.0: openai==1.105.0
513+
openai-base-v1.106.0: openai==1.106.0
518514
openai-base: pytest-asyncio
519515
openai-base: tiktoken
520516
openai-base-v1.0.1: httpx<0.28
@@ -523,7 +519,7 @@ deps =
523519
openai-notiktoken-v1.0.1: openai==1.0.1
524520
openai-notiktoken-v1.36.1: openai==1.36.1
525521
openai-notiktoken-v1.71.0: openai==1.71.0
526-
openai-notiktoken-v1.105.0: openai==1.105.0
522+
openai-notiktoken-v1.106.0: openai==1.106.0
527523
openai-notiktoken: pytest-asyncio
528524
openai-notiktoken-v1.0.1: httpx<0.28
529525
openai-notiktoken-v1.36.1: httpx<0.28
@@ -544,6 +540,12 @@ deps =
544540

545541

546542
# ~~~ DBs ~~~
543+
asyncpg-v0.23.0: asyncpg==0.23.0
544+
asyncpg-v0.25.0: asyncpg==0.25.0
545+
asyncpg-v0.27.0: asyncpg==0.27.0
546+
asyncpg-v0.30.0: asyncpg==0.30.0
547+
asyncpg: pytest-asyncio
548+
547549
clickhouse_driver-v0.2.9: clickhouse-driver==0.2.9
548550

549551
pymongo-v3.5.1: pymongo==3.5.1

0 commit comments

Comments
 (0)