Skip to content

Commit 2421219

Browse files
committed
fix: add PostgreSQL dependencies for psycopg_pool tests
Signed-off-by: JaeHyuck Sa <wogur981208@gmail.com>
1 parent e67babb commit 2421219

2 files changed

Lines changed: 35 additions & 8 deletions

File tree

tests/instrumentation/psycopg_pool_tests.py

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,21 +32,39 @@
3232

3333
import pytest
3434

35-
pytestmark = [pytest.mark.psycopg_pool, pytest.mark.integrationtest]
35+
from elasticapm.conf.constants import SPAN
3636

37-
pytest.importorskip("psycopg")
37+
psycopg = pytest.importorskip("psycopg")
3838
pool_mod = pytest.importorskip("psycopg_pool")
3939

40+
pytestmark = [pytest.mark.psycopg_pool, pytest.mark.integrationtest]
41+
42+
has_postgres_configured = "POSTGRES_DB" in os.environ
43+
44+
45+
def connect_kwargs():
46+
return {
47+
"dbname": os.environ.get("POSTGRES_DB", "elasticapm_test"),
48+
"user": os.environ.get("POSTGRES_USER", "postgres"),
49+
"password": os.environ.get("POSTGRES_PASSWORD", "postgres"),
50+
"host": os.environ.get("POSTGRES_HOST", None),
51+
"port": os.environ.get("POSTGRES_PORT", None),
52+
}
53+
54+
55+
def make_conninfo():
56+
kw = connect_kwargs()
57+
host = kw["host"] or "localhost"
58+
port = kw["port"] or "5432"
59+
return f"postgresql://{kw['user']}:{kw['password']}@{host}:{port}/{kw['dbname']}"
60+
4061

62+
@pytest.mark.skipif(not has_postgres_configured, reason="PostgreSQL not configured")
4163
def test_pool_generates_spans(instrument, elasticapm_client):
4264
with pool_mod.ConnectionPool(
43-
os.environ.get(
44-
"PSYCOPG_TEST_DSN",
45-
"postgresql://postgres:postgres@127.0.0.1:5432/postgres",
46-
),
65+
make_conninfo(),
4766
min_size=1,
4867
max_size=2,
49-
open=True,
5068
) as pool:
5169
pool.wait()
5270

@@ -59,5 +77,8 @@ def test_pool_generates_spans(instrument, elasticapm_client):
5977
finally:
6078
elasticapm_client.end_transaction("200")
6179

62-
spans = elasticapm_client.events["span"]
80+
spans = elasticapm_client.events[SPAN]
81+
# Verify that connect span and query span are generated
82+
assert len(spans) >= 2
83+
assert any(span.get("action") == "connect" for span in spans)
6384
assert any(span.get("context", {}).get("db", {}).get("type") == "sql" for span in spans)

tests/scripts/envs/psycopg_pool.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,7 @@
11
export PYTEST_MARKER="-m psycopg_pool"
2+
export DOCKER_DEPS="postgres"
3+
export POSTGRES_HOST="postgres"
4+
export POSTGRES_USER="postgres"
5+
export POSTGRES_PASSWORD="postgres"
6+
export POSTGRES_DB="elasticapm_test"
7+
export POSTGRES_PORT="5432"

0 commit comments

Comments
 (0)