]> git.ipfire.org Git - thirdparty/psycopg.git/commitdiff
Import modules for shapely test only at runtime
authorJacopo Farina <jacopo.farina@flixbus.com>
Wed, 15 Sep 2021 13:55:32 +0000 (15:55 +0200)
committerDaniele Varrazzo <daniele.varrazzo@gmail.com>
Tue, 21 Sep 2021 18:02:18 +0000 (19:02 +0100)
tests/test_shapely.py

index 09fc7880c777a2e7b6cbcc4ae74df1a21cb980c3..6e410793e78c0bc540a6c1ce3d2d92ccdff964c0 100644 (file)
@@ -2,9 +2,8 @@ import pytest
 
 from psycopg.pq import Format
 from psycopg.types import TypeInfo
-from psycopg.types.shapely import register_shapely
+
 from psycopg import ProgrammingError
-from shapely.geometry import Point, Polygon
 
 pytestmark = [pytest.mark.postgis]
 pytest.importorskip("shapely")
@@ -51,24 +50,30 @@ MULTIPOLYGON_GEOJSON = """
 
 SAMPLE_POINT_GEOJSON = '{"type":"Point","coordinates":[1.2, 3.4]}'
 
-SAMPLE_POINT = Point(1.2, 3.4)
-SAMPLE_POLYGON = Polygon([(0, 0), (1, 1), (1, 0)])
-
 
 @pytest.fixture
 def shapely_conn(conn):
+    from psycopg.types.shapely import register_shapely
+
     info = TypeInfo.fetch(conn, "geometry")
     register_shapely(info, conn)
     return conn
 
 
 def test_no_adapter(conn):
+    from shapely.geometry import Point
+
     point = Point(1.2, 3.4)
     with pytest.raises(ProgrammingError, match="cannot adapt type Point"):
         conn.execute("SELECT pg_typeof(%s)", [point]).fetchone()[0]
 
 
 def test_with_adapter(shapely_conn):
+    from shapely.geometry import Point, Polygon
+
+    SAMPLE_POINT = Point(1.2, 3.4)
+    SAMPLE_POLYGON = Polygon([(0, 0), (1, 1), (1, 0)])
+
     assert (
         shapely_conn.execute(
             "SELECT pg_typeof(%s)",
@@ -88,6 +93,10 @@ def test_with_adapter(shapely_conn):
 
 @pytest.mark.parametrize("fmt_out", [Format.TEXT, Format.BINARY])
 def test_write_read_shape(shapely_conn, fmt_out):
+    from shapely.geometry import Point, Polygon
+
+    SAMPLE_POINT = Point(1.2, 3.4)
+    SAMPLE_POLYGON = Polygon([(0, 0), (1, 1), (1, 0)])
     with shapely_conn.cursor(binary=fmt_out) as cur:
         cur.execute(
             """
@@ -116,6 +125,9 @@ def test_write_read_shape(shapely_conn, fmt_out):
 
 @pytest.mark.parametrize("fmt_out", [Format.TEXT, Format.BINARY])
 def test_match_geojson(shapely_conn, fmt_out):
+    from shapely.geometry import Point
+
+    SAMPLE_POINT = Point(1.2, 3.4)
     with shapely_conn.cursor(binary=fmt_out) as cur:
         cur.execute(
             f"""