]> git.ipfire.org Git - thirdparty/psycopg.git/commitdiff
Skip hostaddr test for PG version < 12
authorDaniele Varrazzo <daniele.varrazzo@gmail.com>
Tue, 24 Mar 2020 10:14:34 +0000 (23:14 +1300)
committerDaniele Varrazzo <daniele.varrazzo@gmail.com>
Tue, 24 Mar 2020 10:14:34 +0000 (23:14 +1300)
tests/fix_db.py
tests/pq/test_pgconn.py

index 80d4f419c4c577b171750691d789eebfa8b1ea52..72d07bb9c30a85e75c6a769727ed1c6ac7dc3038 100644 (file)
@@ -1,4 +1,7 @@
 import os
+import re
+import operator
+
 import pytest
 
 
@@ -12,14 +15,61 @@ def pytest_addoption(parser):
     )
 
 
+def pytest_configure(config):
+    # register libpq marker
+    config.addinivalue_line(
+        "markers",
+        "libpq(version_expr): run the test only with matching libpq"
+        " (e.g. '>= 10', '< 9.6')",
+    )
+
+
 @pytest.fixture
-def pq():
+def pq(request):
     """The libpq module wrapper to test."""
     from psycopg3 import pq
 
+    for m in request.node.iter_markers(name="libpq"):
+        if not libpq_version_matches(pq.version(), m.args):
+            pytest.skip(f"skipping test as libpq {pq.version()}")
+
     return pq
 
 
+def libpq_version_matches(got, want):
+    # convert 90603 to (9, 6, 3), 120003 to (12, 0, 3)
+    got, got_fix = divmod(got, 100)
+    got_maj, got_min = divmod(got, 100)
+    got = (got_maj, got_min, got_fix)
+
+    # Parse a spec like "> 9.6"
+    if len(want) != 1:
+        pytest.fail("libpq marker doesn't specify a version")
+    want = want[0]
+    m = re.match(
+        r"^\s*(>=|<=|>|<)\s*(?:(\d+)(?:\.(\d+)(?:\.(\d+))?)?)?\s*$", want
+    )
+    if m is None:
+        pytest.fail(f"bad libpq spec: {want}")
+
+    # convert "9.6" into (9, 6, 0), "10.3" into (10, 0, 3)
+    want_maj = int(m.group(2))
+    want_min = int(m.group(3) or "0")
+    want_fix = int(m.group(4) or "0")
+    if want_maj >= 10:
+        if not want_fix:
+            want_min, want_fix = 0, want_min
+        else:
+            pytest.fail(f"bad libpq version in {want}")
+
+    want = (want_maj, want_min, want_fix)
+
+    op = getattr(
+        operator, {">=": "ge", "<=": "le", ">": "gt", "<": "lt"}[m.group(1)]
+    )
+    return op(got, want)
+
+
 @pytest.fixture
 def dsn(request):
     """Return the dsn used to connect to the `--test-dsn` database."""
index 91fa3fdd161286ae96d5a3b1f9c7d66464be0dfd..131347935e4c8e1338c096c6d69bdceb9d251105 100644 (file)
@@ -2,6 +2,8 @@ from select import select
 
 import pytest
 
+import psycopg3
+
 
 def test_connectdb(pq, dsn):
     conn = pq.PGconn.connect(dsn.encode("utf8"))
@@ -120,9 +122,16 @@ def test_host(pgconn):
     assert isinstance(pgconn.host, bytes)
 
 
+@pytest.mark.libpq(">= 12")
 def test_hostaddr(pgconn):
     # not in info
-    assert isinstance(pgconn.hostaddr, bytes)
+    assert isinstance(pgconn.hostaddr, bytes), pgconn.hostaddr
+
+
+@pytest.mark.libpq("< 12")
+def test_hostaddr_missing(pgconn):
+    with pytest.raises(psycopg3.NotSupportedError):
+        pgconn.hostaddr
 
 
 def test_tty(pgconn):