import psycopg
from psycopg import pq
+from psycopg.pq._debug import PGconnDebug
from psycopg import sql
from .utils import check_postgres_version
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):
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`."""