Skip to content

Commit 034a6a4

Browse files
authored
Add some Python client tests (#1441)
This PR adds some unit tests for the Svix Python OpenAPI client to avoid breaking things (see #1440).
2 parents 1cf8a7f + 403fea3 commit 034a6a4

File tree

6 files changed

+477
-43
lines changed

6 files changed

+477
-43
lines changed

.github/workflows/python-tests.yml

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
name: Python Tests
2+
on:
3+
push:
4+
branches:
5+
- main
6+
paths:
7+
- "python/**"
8+
- "openapi.json"
9+
- "server/**"
10+
- ".github/workflows/python-tests.yml"
11+
pull_request:
12+
paths:
13+
- "python/**"
14+
- "openapi.json"
15+
- "server/**"
16+
- ".github/workflows/python-tests.yml"
17+
jobs:
18+
build:
19+
runs-on: ubuntu-latest
20+
steps:
21+
- uses: actions/checkout@v4
22+
23+
- name: Build svix server image
24+
run: docker compose build
25+
working-directory: ./server
26+
27+
- uses: actions/setup-python@v5
28+
name: Install Python
29+
with:
30+
python-version: "3.11"
31+
32+
- name: Install deps
33+
run: |
34+
python -m pip install --upgrade pip
35+
python -m pip install -r requirements.txt .
36+
python -m pip install -r requirements-dev.txt .
37+
working-directory: ./python
38+
39+
- name: Regen openapi libs
40+
run: ./scripts/generate_openapi.sh
41+
working-directory: ./python
42+
43+
- name: Check typing on client tests
44+
run: mypy tests/test_client.py
45+
working-directory: ./python
46+
47+
- name: Run Python tests
48+
run: pytest -sv
49+
working-directory: ./python

python/requirements-dev.txt

Lines changed: 58 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -4,106 +4,121 @@
44
#
55
# pip-compile --output-file=requirements-dev.txt requirements.in/development.txt
66
#
7-
anyio==3.5.0
7+
anyio==4.6.0
88
# via httpcore
9-
attrs==21.4.0
9+
attrs==24.2.0
1010
# via
1111
# openapi-python-client
12-
# pytest
13-
autoflake==1.4
12+
# pytest-docker
13+
autoflake==2.3.1
1414
# via openapi-python-client
15-
black==23.3.0
15+
black==24.8.0
1616
# via openapi-python-client
17-
build==0.10.0
17+
build==1.2.2
1818
# via pip-tools
19-
certifi==2024.7.4
19+
certifi==2024.8.30
2020
# via
2121
# httpcore
2222
# httpx
23-
click==8.0.1
23+
# requests
24+
charset-normalizer==3.3.2
25+
# via requests
26+
click==8.1.7
2427
# via
2528
# black
2629
# pip-tools
2730
# typer
28-
h11==0.12.0
31+
h11==0.14.0
2932
# via httpcore
30-
httpcore==0.15.0
33+
httpcore==0.17.3
3134
# via httpx
32-
httpx==0.23.0
35+
httpx==0.24.1
3336
# via
3437
# -r requirements.in/development.txt
3538
# openapi-python-client
36-
idna==3.3
39+
idna==3.10
3740
# via
3841
# anyio
39-
# rfc3986
40-
iniconfig==1.1.1
42+
# httpx
43+
# requests
44+
iniconfig==2.0.0
4145
# via pytest
42-
isort==5.8.0
46+
isort==5.13.2
4347
# via openapi-python-client
44-
jinja2==3.1.3
48+
jinja2==3.1.4
4549
# via
4650
# -r requirements.in/development.txt
4751
# openapi-python-client
48-
markupsafe==2.1.0
49-
# via jinja2
50-
mypy==1.4.0
52+
markupsafe==2.1.5
53+
# via
54+
# jinja2
55+
# werkzeug
56+
mypy==1.11.2
5157
# via -r requirements.in/development.txt
5258
mypy-extensions==1.0.0
5359
# via
5460
# black
5561
# mypy
5662
openapi-python-client==0.14.1
5763
# via -r requirements.in/development.txt
58-
packaging==23.1
64+
packaging==24.1
5965
# via
6066
# black
6167
# build
6268
# pytest
63-
pathspec==0.11.1
69+
pathspec==0.12.1
6470
# via black
65-
pip-tools==6.13.0
71+
pip-tools==7.4.1
6672
# via -r requirements.in/development.txt
67-
platformdirs==3.5.1
73+
platformdirs==4.3.6
6874
# via black
69-
pluggy==0.13.1
70-
# via pytest
71-
py==1.10.0
75+
pluggy==1.5.0
7276
# via pytest
73-
pydantic==1.10.13
77+
pydantic==1.10.18
7478
# via openapi-python-client
75-
pyflakes==2.3.1
79+
pyflakes==3.2.0
7680
# via autoflake
77-
pyproject-hooks==1.0.0
78-
# via build
79-
pytest==6.2.4
81+
pyproject-hooks==1.1.0
82+
# via
83+
# build
84+
# pip-tools
85+
pytest==8.3.3
86+
# via
87+
# -r requirements.in/development.txt
88+
# pytest-docker
89+
pytest-docker==3.1.1
8090
# via -r requirements.in/development.txt
81-
python-dateutil==2.8.2
91+
pytest-httpserver==1.1.0
92+
# via -r requirements.in/development.txt
93+
python-dateutil==2.9.0.post0
8294
# via openapi-python-client
83-
pyyaml==6.0.1
95+
pyyaml==6.0.2
8496
# via openapi-python-client
85-
rfc3986[idna2008]==1.5.0
86-
# via httpx
87-
ruff==0.4.8
97+
requests==2.32.3
8898
# via -r requirements.in/development.txt
89-
shellingham==1.4.0
99+
ruff==0.6.8
100+
# via -r requirements.in/development.txt
101+
shellingham==1.5.4
90102
# via openapi-python-client
91103
six==1.16.0
92104
# via python-dateutil
93-
sniffio==1.2.0
105+
sniffio==1.3.1
94106
# via
95107
# anyio
96108
# httpcore
97109
# httpx
98-
toml==0.10.2
99-
# via pytest
100-
typer==0.7.0
110+
typer==0.9.4
101111
# via openapi-python-client
102-
typing-extensions==4.6.3
112+
typing-extensions==4.12.2
103113
# via
104114
# mypy
105115
# pydantic
106-
wheel==0.40.0
116+
# typer
117+
urllib3==2.2.3
118+
# via requests
119+
werkzeug==3.0.4
120+
# via pytest-httpserver
121+
wheel==0.44.0
107122
# via pip-tools
108123

109124
# The following packages are considered to be unsafe in a requirements file:

python/requirements.in/development.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,6 @@ pytest
55
httpx>=0.23.0
66
openapi-python-client>=0.14.1,<0.15 # I think version 0.15 is now dangerous for us? https://github.com/openapi-generators/openapi-python-client/pull/775#issuecomment-1646977834
77
jinja2>=3.1.3
8+
pytest-docker
9+
pytest-httpserver
10+
requests

python/tests/conftest.py

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
import os
2+
import shutil
3+
from subprocess import CalledProcessError, check_output
4+
5+
import pytest
6+
import requests
7+
from requests.adapters import HTTPAdapter
8+
from svix.api import Svix, SvixOptions
9+
from urllib3.util.retry import Retry
10+
11+
SVIX_ORG_ID = "org_svix_python_tests"
12+
13+
14+
def pytest_collection_modifyitems(config, items):
15+
"""Client tests require docker compose (v2 or v1) so skip them if it is not
16+
installed on host."""
17+
skipper = None
18+
if shutil.which("docker") is None:
19+
skipper = pytest.mark.skip(reason="skipping test as docker command is missing")
20+
else:
21+
docker_compose_available = False
22+
try:
23+
# check if docker compose v2 if available
24+
check_output(["docker", "compose", "version"])
25+
docker_compose_available = True
26+
except CalledProcessError:
27+
# check if docker compose v1 if available
28+
docker_compose_available = shutil.which("docker-compose") is not None
29+
finally:
30+
if not docker_compose_available:
31+
skipper = pytest.mark.skip(
32+
reason="skipping test as docker compose is missing"
33+
)
34+
if skipper is not None:
35+
for item in items:
36+
if item.module.__name__ == "tests.test_client":
37+
item.add_marker(skipper)
38+
39+
40+
@pytest.fixture(scope="session")
41+
def docker_compose_command():
42+
try:
43+
# use docker compose v2 if available
44+
check_output(["docker", "compose", "version"])
45+
return "docker compose"
46+
except Exception:
47+
# fallback on v1 otherwise
48+
return "docker-compose"
49+
50+
51+
@pytest.fixture(scope="session")
52+
def docker_compose_file():
53+
return [
54+
os.path.join(os.path.dirname(__file__), "../../server/docker-compose.yml"),
55+
os.path.join(os.path.dirname(__file__), "docker-compose.override.yml"),
56+
]
57+
58+
59+
@pytest.fixture(scope="session")
60+
def docker_compose(docker_services):
61+
return docker_services._docker_compose
62+
63+
64+
@pytest.fixture(scope="session")
65+
def httpserver_listen_address():
66+
# Use IP address in the docker bridge network as server hostname in order for
67+
# the svix server executed in a docker container to successfully send webhooks
68+
# to the HTTP server executed on the host
69+
return ("172.17.0.1", 0)
70+
71+
72+
@pytest.fixture(scope="session")
73+
def svix_server_url(docker_services):
74+
# svix server container exposes a free port to the docker host,
75+
# we use the docker network gateway IP in case the tests are also
76+
# executed in a container
77+
svix_server_port = docker_services.port_for("backend", 8071)
78+
return f"http://172.17.0.1:{svix_server_port}"
79+
80+
81+
@pytest.fixture(autouse=True, scope="session")
82+
def svix_server(svix_server_url):
83+
"""Spawn a Svix server for the tests session using docker compose"""
84+
# wait for the svix backend service to be up and responding
85+
request_session = requests.Session()
86+
retries = Retry(total=10, backoff_factor=0.1, status_forcelist=[500, 502, 503, 504])
87+
request_session.mount("http://", HTTPAdapter(max_retries=retries))
88+
api_url = f"{svix_server_url}/api/v1/health/"
89+
response = request_session.get(api_url)
90+
assert response
91+
92+
93+
@pytest.fixture(autouse=True)
94+
def svix_wiper(docker_compose):
95+
"""Ensure stateless tests"""
96+
yield
97+
# wipe svix database after each test to ensure stateless tests
98+
docker_compose.execute(
99+
f"exec -T backend svix-server wipe --yes-i-know-what-im-doing {SVIX_ORG_ID}"
100+
)
101+
102+
103+
@pytest.fixture(scope="session")
104+
def svix_api(svix_server_url, docker_compose):
105+
# generate bearer token to authorize communication with the svix server
106+
exec_output = docker_compose.execute(
107+
f"exec -T backend svix-server jwt generate {SVIX_ORG_ID}"
108+
)
109+
svix_auth_token = (
110+
exec_output.decode()
111+
.replace("Token (Bearer): ", "")
112+
.replace("\r", "")
113+
.replace("\n", "")
114+
)
115+
return Svix(
116+
svix_auth_token,
117+
SvixOptions(server_url=svix_server_url),
118+
)
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
version: "3.7"
2+
services:
3+
backend:
4+
environment:
5+
SVIX_JWT_SECRET: "x"
6+
SVIX_ENDPOINT_HTTPS_ONLY: "false"
7+
SVIX_WHITELIST_SUBNETS: "[127.0.0.1/32, 172.17.0.0/16]"
8+
pgbouncer:
9+
ports:
10+
- "8079:5432" # Needed for sqlx
11+
redis:
12+
ports:
13+
- "8078:6379" # Needed for sqlx

0 commit comments

Comments
 (0)