Skip to content

Commit 27712ca

Browse files
Test: Add test in test_functional.py for custom Geometry that uses WKT elements (#525)
* Test: Add test in test_functional.py for custom Geometry that uses WKT elements * Fix MySQL and remove MyPy from PyPy job
1 parent b319a15 commit 27712ca

File tree

2 files changed

+69
-2
lines changed

2 files changed

+69
-2
lines changed

tests/test_functional.py

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1195,3 +1195,69 @@ def test_to_metadata(self, Lake):
11951195

11961196
# Check that the spatial index was not duplicated
11971197
assert len(new_Lake.indexes) == 1
1198+
1199+
1200+
class TestAsBinaryWKT:
1201+
def test_create_insert(self, conn, dialect_name):
1202+
class GeometryWkt(Geometry):
1203+
"""Geometry type that uses WKT strings."""
1204+
1205+
from_text = "ST_GeomFromEWKT"
1206+
as_binary = "ST_AsText"
1207+
ElementType = WKTElement
1208+
1209+
dialects_with_srid = ["geopackage", "mysql", "mariadb"]
1210+
1211+
# Define the table
1212+
cols = [
1213+
Column("id", Integer, primary_key=True),
1214+
]
1215+
cols.append(Column("geom_with_srid", GeometryWkt(geometry_type="LINESTRING", srid=4326)))
1216+
if dialect_name not in dialects_with_srid:
1217+
cols.append(Column("geom", GeometryWkt(geometry_type="LINESTRING")))
1218+
t = Table("use_wkt", MetaData(), *cols)
1219+
1220+
# Create the table
1221+
t.drop(bind=conn, checkfirst=True)
1222+
t.create(bind=conn)
1223+
1224+
# Test element insertion
1225+
inserted_values = [
1226+
{"geom_with_srid": v}
1227+
for v in [
1228+
"SRID=4326;LINESTRING(0 0,1 1)",
1229+
WKTElement("LINESTRING(0 0,2 2)", srid=4326),
1230+
WKTElement("SRID=4326;LINESTRING(0 0,3 3)", extended=True),
1231+
from_shape(LineString([[0, 0], [4, 4]]), srid=4326),
1232+
]
1233+
]
1234+
if dialect_name not in dialects_with_srid:
1235+
for i, v in zip(
1236+
inserted_values,
1237+
[
1238+
"LINESTRING(0 0,1 1)",
1239+
WKTElement("LINESTRING(0 0,2 2)"),
1240+
WKTElement("SRID=-1;LINESTRING(0 0,3 3)", extended=True),
1241+
from_shape(LineString([[0, 0], [4, 4]])),
1242+
],
1243+
):
1244+
i["geom"] = v
1245+
1246+
conn.execute(t.insert(), inserted_values)
1247+
1248+
results = conn.execute(t.select())
1249+
rows = results.fetchall()
1250+
1251+
for row_num, row in enumerate(rows):
1252+
for num, element in enumerate(row[1:]):
1253+
assert isinstance(element, WKTElement)
1254+
wkt = conn.execute(element.ST_AsText()).scalar()
1255+
assert format_wkt(wkt) == f"LINESTRING(0 0,{row_num + 1} {row_num + 1})"
1256+
srid = conn.execute(element.ST_SRID()).scalar()
1257+
if num == 1:
1258+
assert srid == 0 if dialect_name != "sqlite" else -1
1259+
else:
1260+
assert srid == 4326
1261+
1262+
# Drop the table
1263+
t.drop(bind=conn)

tox.ini

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@ setenv=
2727
COVERAGE_FILE = {env:COVERAGE_FILE:.coverage-{envname}}
2828
EXPECTED_COV = 93
2929
pypy3: EXPECTED_COV = 85
30-
sqla14: PYTEST_ADDOPTS = {env:PYTEST_ADDOPTS:} --mypy-ignore-missing-imports
30+
sqla14: PYTEST_ADDOPTS = {env:PYTEST_ADDOPTS:} --mypy --mypy-ignore-missing-imports
31+
sqlalatest: PYTEST_ADDOPTS = {env:PYTEST_ADDOPTS:} --mypy
32+
pypy3: PYTEST_ADDOPTS = {env:PYTEST_ADDOPTS:}
3133
deps=
3234
sqla14: SQLAlchemy==1.4.*
3335
sqlalatest: SQLAlchemy
@@ -54,7 +56,6 @@ commands=
5456
--self-contained-html \
5557
--durations 10 \
5658
--durations-min=2.0 \
57-
--mypy \
5859
{posargs}
5960

6061
[testenv:coverage]

0 commit comments

Comments
 (0)