From: Denis Laxalde Date: Thu, 6 Oct 2022 19:14:47 +0000 (+0200) Subject: test: add --pq-debug pytest option to use PGconnDebug X-Git-Tag: 3.1.4~9 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=663f74315fb3a2422b73bfc1676fc3e024633900;p=thirdparty%2Fpsycopg.git test: add --pq-debug pytest option to use PGconnDebug --- diff --git a/tests/fix_db.py b/tests/fix_db.py index a81f6f837..3aded672f 100644 --- a/tests/fix_db.py +++ b/tests/fix_db.py @@ -8,6 +8,7 @@ from typing import List, Optional import psycopg from psycopg import pq +from psycopg.pq._debug import PGconnDebug from psycopg import sql from .utils import check_postgres_version @@ -33,6 +34,12 @@ def pytest_addoption(parser): default=None, help="Generate a libpq trace to TRACEFILE or STDERR.", ) + parser.addoption( + "--pq-debug", + action="store_true", + default=False, + help="Log PGconn access. (Requires PSYCOPG_IMPL=python.)", + ) def pytest_report_header(config): @@ -153,6 +160,18 @@ def maybe_trace(pgconn, tracefile, function): pgconn.untrace() +@pytest.fixture(autouse=True) +def pgconn_debug(request): + if not request.config.getoption("--pq-debug"): + return + if pq.__impl__ != "python": + raise pytest.UsageError("set PSYCOPG_IMPL=python to use --pq-debug") + logging.basicConfig(level=logging.INFO, format="%(message)s") + logger = logging.getLogger("psycopg.debug") + logger.setLevel(logging.INFO) + pq.PGconn = PGconnDebug + + @pytest.fixture def pgconn(dsn, request, tracefile): """Return a PGconn connection open to `--test-dsn`."""