Skip to content

Commit

Permalink
Fix tests using Postgres (#2362)
Browse files Browse the repository at this point in the history
- 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...)
  • Loading branch information
antonpirker authored Sep 11, 2023
1 parent 87d582d commit 34232eb
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 11 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/test-integration-asyncpg.yml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ jobs:
- name: Setup Test Env
run: |
pip install coverage "tox>=3,<4"
psql postgresql://postgres:sentry@localhost:5432 -c "create database ${SENTRY_PYTHON_TEST_POSTGRES_NAME};" || true
psql postgresql://postgres:sentry@localhost:5432 -c "grant all privileges on database ${SENTRY_PYTHON_TEST_POSTGRES_NAME} to ${SENTRY_PYTHON_TEST_POSTGRES_USER};" || true
- name: Test asyncpg
uses: nick-fields/retry@v2
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/test-integration-django.yml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ jobs:
- name: Setup Test Env
run: |
pip install coverage "tox>=3,<4"
psql postgresql://postgres:sentry@localhost:5432 -c "create database ${SENTRY_PYTHON_TEST_POSTGRES_NAME};" || true
psql postgresql://postgres:sentry@localhost:5432 -c "grant all privileges on database ${SENTRY_PYTHON_TEST_POSTGRES_NAME} to ${SENTRY_PYTHON_TEST_POSTGRES_USER};" || true
- name: Test django
uses: nick-fields/retry@v2
Expand Down
2 changes: 2 additions & 0 deletions scripts/split-tox-gh-actions/ci-yaml-setup-db.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
psql postgresql://postgres:sentry@localhost:5432 -c "create database ${SENTRY_PYTHON_TEST_POSTGRES_NAME};" || true
psql postgresql://postgres:sentry@localhost:5432 -c "grant all privileges on database ${SENTRY_PYTHON_TEST_POSTGRES_NAME} to ${SENTRY_PYTHON_TEST_POSTGRES_USER};" || true
1 change: 1 addition & 0 deletions scripts/split-tox-gh-actions/ci-yaml-test-snippet.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
- name: Setup Test Env
run: |
pip install coverage "tox>=3,<4"
{{ setup_postgres }}

- name: Test {{ framework }}
uses: nick-fields/retry@v2
Expand Down
6 changes: 6 additions & 0 deletions scripts/split-tox-gh-actions/split-tox-gh-actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
TEMPLATE_DIR = Path(__file__).resolve().parent
TEMPLATE_FILE = TEMPLATE_DIR / "ci-yaml.txt"
TEMPLATE_FILE_SERVICES = TEMPLATE_DIR / "ci-yaml-services.txt"
TEMPLATE_FILE_SETUP_DB = TEMPLATE_DIR / "ci-yaml-setup-db.txt"
TEMPLATE_SNIPPET_TEST = TEMPLATE_DIR / "ci-yaml-test-snippet.txt"
TEMPLATE_SNIPPET_TEST_PY27 = TEMPLATE_DIR / "ci-yaml-test-py27-snippet.txt"

Expand Down Expand Up @@ -113,6 +114,11 @@ def write_yaml_file(
out += "".join(lines)
f.close()

elif template_line.strip() == "{{ setup_postgres }}":
if current_framework in FRAMEWORKS_NEEDING_POSTGRES:
f = open(TEMPLATE_FILE_SETUP_DB, "r")
out += "".join(f.readlines())

elif template_line.strip() == "{{ check_needs }}":
if py27_supported:
out += CHECK_NEEDS_PY27
Expand Down
8 changes: 4 additions & 4 deletions tests/integrations/asyncpg/test_asyncpg.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@
CRUMBS_CONNECT = {
"category": "query",
"data": {
"db.name": "postgres",
"db.name": PG_NAME,
"db.system": "postgresql",
"db.user": "foo",
"server.address": "localhost",
"server.port": 5432,
"db.user": PG_USER,
"server.address": PG_HOST,
"server.port": PG_PORT,
},
"message": "connect",
"type": "default",
Expand Down
12 changes: 10 additions & 2 deletions tests/integrations/django/myapp/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,16 +121,24 @@ def middleware(request):
try:
import psycopg2 # noqa

db_engine = "django.db.backends.postgresql"
try:
from django.db.backends import postgresql # noqa: F401
except ImportError:
db_engine = "django.db.backends.postgresql_psycopg2"

DATABASES["postgres"] = {
"ENGINE": "django.db.backends.postgresql",
"ENGINE": db_engine,
"NAME": os.environ["SENTRY_PYTHON_TEST_POSTGRES_NAME"],
"USER": os.environ["SENTRY_PYTHON_TEST_POSTGRES_USER"],
"PASSWORD": os.environ["SENTRY_PYTHON_TEST_POSTGRES_PASSWORD"],
"HOST": os.environ.get("SENTRY_PYTHON_TEST_POSTGRES_HOST", "localhost"),
"PORT": 5432,
}
except (ImportError, KeyError):
pass
from sentry_sdk.utils import logger

logger.warn("No psycopg2 found, testing with SQLite.")


# Password validation
Expand Down
15 changes: 10 additions & 5 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,8 @@ envlist =
{py3.7}-gcp

# Grpc
{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}
{py3.7,py3.8,py3.9,py3.10}-grpc-v{1.40,1.44,1.48}
{py3.7,py3.8,py3.9,py3.10,py3.11}-grpc-v{1.54,1.56,1.58}

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

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

{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
{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
{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
{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

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

django-v{4.0,4.1}: djangorestframework
django-v{4.0,4.1}: pytest-asyncio
django-v{4.0,4.1}: psycopg2-binary
django-v{4.0,4.1}: pytest-django
django-v{4.0,4.1}: Werkzeug

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

# Grpc
grpc: grpcio-tools
grpc-v1.40: grpcio-tools>=1.40.0,<1.41.0
grpc-v1.44: grpcio-tools>=1.44.0,<1.45.0
grpc-v1.48: grpcio-tools>=1.48.0,<1.49.0
grpc-v1.54: grpcio-tools>=1.54.0,<1.55.0
grpc-v1.56: grpcio-tools>=1.56.0,<1.57.0
grpc-v1.58: grpcio-tools>=1.58.0,<1.59.0
grpc: protobuf
grpc: mypy-protobuf
grpc: types-protobuf
Expand Down

0 comments on commit 34232eb

Please sign in to comment.