]> git.ipfire.org Git - thirdparty/psycopg.git/commitdiff
test(shapely): Handle missing SRID support
authorKamil Monicz <kamil@monicz.dev>
Mon, 24 Mar 2025 11:47:23 +0000 (11:47 +0000)
committerDaniele Varrazzo <daniele.varrazzo@gmail.com>
Wed, 26 Mar 2025 18:27:20 +0000 (19:27 +0100)
tests/types/test_shapely.py

index ab5d2536ee0d9c897c5416971cc21372cfbd9999..b2c61c03674e585ffd6253f3126c8f5bd1e99519 100644 (file)
@@ -7,9 +7,15 @@ from psycopg.types import TypeInfo
 
 pytest.importorskip("shapely")
 
-from shapely import get_srid, set_srid
 from shapely.geometry import MultiPolygon, Point, Polygon
 
+try:
+    from shapely import get_srid, set_srid
+except ImportError:
+    # Shapely<2 compatibility, no notion of SRID
+    get_srid = None  # type: ignore[assignment]
+    set_srid = None  # type: ignore[assignment]
+
 from psycopg.types.shapely import register_shapely
 
 pytestmark = [
@@ -107,7 +113,8 @@ def test_with_adapter(shapely_conn):
 def test_write_read_shape(shapely_conn, fmt_in, fmt_out):
     SAMPLE_POINT = Point(1.2, 3.4)
     SAMPLE_POLYGON_4326 = Polygon([(0, 0), (1, 1), (1, 0)])
-    set_srid(SAMPLE_POLYGON_4326, 4326)
+    if set_srid is not None:
+        SAMPLE_POLYGON_4326 = set_srid(SAMPLE_POLYGON_4326, 4326)
 
     with shapely_conn.cursor(binary=fmt_out) as cur:
         cur.execute(
@@ -130,12 +137,14 @@ def test_write_read_shape(shapely_conn, fmt_in, fmt_out):
         cur.execute("select geom from sample_geoms where id=1")
         result = cur.fetchone()[0]
         assert result == SAMPLE_POINT
-        assert get_srid(result) == 0
+        if get_srid is not None:
+            assert get_srid(result) == 0
 
         cur.execute("select geom from sample_geoms where id=2")
         result = cur.fetchone()[0]
         assert result == SAMPLE_POLYGON_4326
-        assert get_srid(result) == 4326
+        if get_srid is not None:
+            assert get_srid(result) == 4326
 
 
 @pytest.mark.parametrize("fmt_out", Format)