]> git.ipfire.org Git - thirdparty/psycopg.git/commitdiff
test: add --pq-debug pytest option to use PGconnDebug
authorDenis Laxalde <denis@laxalde.org>
Thu, 6 Oct 2022 19:14:47 +0000 (21:14 +0200)
committerDaniele Varrazzo <daniele.varrazzo@gmail.com>
Tue, 11 Oct 2022 12:17:59 +0000 (13:17 +0100)
tests/fix_db.py

index a81f6f83770cc1ded95b6f8f42f4e1fc88d74fef..3aded672ff276083c8a8535f3007dacc6890c54a 100644 (file)
@@ -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`."""