From: Denis Laxalde Date: Tue, 16 May 2023 11:28:37 +0000 (+0200) Subject: tests: check that OperationalError raised by connect() holds a pgconn X-Git-Tag: 3.1.10~14^2~2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=4457a2bcff6e495aee5f82347ea2649f96181a13;p=thirdparty%2Fpsycopg.git tests: check that OperationalError raised by connect() holds a pgconn Sort of a non-regression test for the part of commit 9220293dc023b757f2a57702c14b1460ff8f25b0 concerning generators. It used roughly the same logic as tests/pq/test_pgconn.py::test_used_password() to determine if the test connection needs a password and gets skipped otherwise. --- diff --git a/tests/test_generators.py b/tests/test_generators.py index 8aba73f6d..f9b77616f 100644 --- a/tests/test_generators.py +++ b/tests/test_generators.py @@ -7,6 +7,35 @@ import pytest import psycopg from psycopg import waiting from psycopg import pq +from psycopg.conninfo import conninfo_to_dict, make_conninfo + + +def test_connect_operationalerror_pgconn(generators, dsn, monkeypatch): + """Check that when generators.connect() fails, the resulting + OperationalError has a pgconn attribute set with needs_password. + """ + gen = generators.connect(dsn) + pgconn = waiting.wait_conn(gen) + if not pgconn.used_password: + pytest.skip("test connection needs no password") + + with monkeypatch.context() as m: + try: + m.delenv("PGPASSWORD", raising=True) + except KeyError: + info = conninfo_to_dict(dsn) + del info["password"] # should not raise per check above. + dsn = make_conninfo(**info) + + gen = generators.connect(dsn) + with pytest.raises( + psycopg.OperationalError, match="connection failed:" + ) as excinfo: + waiting.wait_conn(gen) + + pgconn = excinfo.value.pgconn + assert pgconn is not None + assert pgconn.needs_password @pytest.fixture