From: Jacopo Farina Date: Wed, 15 Sep 2021 13:55:32 +0000 (+0200) Subject: Import modules for shapely test only at runtime X-Git-Tag: 3.0~71^2~10 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=530d223ebd7e9b317fdc7f16f1385106446617d6;p=thirdparty%2Fpsycopg.git Import modules for shapely test only at runtime --- diff --git a/tests/test_shapely.py b/tests/test_shapely.py index 09fc7880c..6e410793e 100644 --- a/tests/test_shapely.py +++ b/tests/test_shapely.py @@ -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"""