]> git.ipfire.org Git - thirdparty/psycopg.git/commitdiff
tests: check that OperationalError raised by connect() holds a pgconn
authorDenis Laxalde <denis.laxalde@dalibo.com>
Tue, 16 May 2023 11:28:37 +0000 (13:28 +0200)
committerDaniele Varrazzo <daniele.varrazzo@gmail.com>
Tue, 13 Jun 2023 12:14:54 +0000 (14:14 +0200)
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.

tests/test_generators.py

index 8aba73f6ded435b5853cab20fc887e0d4eba2448..f9b77616fd80aee18c51abf1c4dd4bc0013ff5a6 100644 (file)
@@ -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