Skip to content

Commit 3d60995

Browse files
Fix (mysql): Fix failing test for MySQL dialect (#567)
1 parent 6076708 commit 3d60995

File tree

3 files changed

+36
-20
lines changed

3 files changed

+36
-20
lines changed

geoalchemy2/admin/dialects/mysql.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -157,9 +157,7 @@ def after_drop(table, bind, **kw):
157157
return
158158

159159

160-
_MYSQL_FUNCTIONS = {
161-
"ST_AsEWKB": "ST_AsBinary",
162-
}
160+
_MYSQL_FUNCTIONS = {"ST_AsEWKB": "ST_AsBinary", "ST_SetSRID": "ST_SRID"}
163161

164162

165163
def _compiles_mysql(cls, fn):

tests/schema_fixtures.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from geoalchemy2 import Geography
1212
from geoalchemy2 import Geometry
1313
from geoalchemy2 import Raster
14+
from geoalchemy2.elements import WKTElement
1415

1516

1617
@pytest.fixture
@@ -126,7 +127,22 @@ def column_expression(self, col):
126127
)
127128

128129
def bind_expression(self, bindvalue):
129-
return func.ST_Transform(self.impl.bind_expression(bindvalue), self.db_srid)
130+
return func.ST_Transform(func.ST_GeomFromText(bindvalue, self.app_srid), self.db_srid)
131+
132+
def bind_processor(self, dialect):
133+
"""Specific bind_processor that automatically process spatial elements.
134+
135+
Here we only use WKT representations.
136+
"""
137+
138+
def process(bindvalue):
139+
bindvalue = WKTElement(bindvalue)
140+
bindvalue = bindvalue.as_wkt()
141+
if bindvalue.srid <= 0:
142+
bindvalue.srid = self.srid
143+
return bindvalue.desc
144+
145+
return process
130146

131147

132148
@pytest.fixture

tests/test_functional.py

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -775,19 +775,18 @@ def test_WKBElement(self, session, Lake, setup_tables, dialect_name):
775775
assert srid == 4326
776776

777777
@test_only_with_dialects("postgresql", "mysql", "sqlite-spatialite3", "sqlite-spatialite4")
778-
def test_transform(self, session, LocalPoint, setup_tables):
779-
if session.bind.dialect.name == "mysql":
780-
# Explicitly skip MySQL dialect to show that there is an issue
781-
pytest.skip(
782-
reason=(
783-
"The SRID is not properly retrieved so an exception is raised. TODO: This "
784-
"should be fixed later"
785-
)
786-
)
778+
def test_transform(self, session, LocalPoint, setup_tables, dialect_name):
787779
# Create new point instance
788780
p = LocalPoint()
789-
p.geom = "SRID=4326;POINT(5 45)" # Insert geometry with wrong SRID
790-
p.managed_geom = "SRID=4326;POINT(5 45)" # Insert geometry with wrong SRID
781+
if dialect_name in ["mysql", "mariadb"]:
782+
expected_x = 45
783+
expected_y = 5
784+
else:
785+
expected_x = 5
786+
expected_y = 45
787+
ewkt = f"SRID=4326;POINT({expected_x} {expected_y})"
788+
p.geom = ewkt # Insert geometry with wrong SRID
789+
p.managed_geom = ewkt # Insert geometry with wrong SRID
791790

792791
# Insert point
793792
session.add(p)
@@ -800,11 +799,11 @@ def test_transform(self, session, LocalPoint, setup_tables):
800799
assert pt.geom.srid == 4326
801800
assert pt.managed_geom.srid == 4326
802801
pt_wkb = to_shape(pt.geom)
803-
assert round(pt_wkb.x, 5) == 5
804-
assert round(pt_wkb.y, 5) == 45
802+
assert round(pt_wkb.x, 5) == expected_x
803+
assert round(pt_wkb.y, 5) == expected_y
805804
pt_wkb = to_shape(pt.managed_geom)
806-
assert round(pt_wkb.x, 5) == 5
807-
assert round(pt_wkb.y, 5) == 45
805+
assert round(pt_wkb.x, 5) == expected_x
806+
assert round(pt_wkb.y, 5) == expected_y
808807

809808
# Check that the data is correct in DB using raw query
810809
q = text(
@@ -818,7 +817,10 @@ def test_transform(self, session, LocalPoint, setup_tables):
818817
for i in [res_q.geom, res_q.managed_geom]:
819818
x, y = re.match(r"POINT\((\d+\.\d*) (\d+\.\d*)\)", i).groups()
820819
assert round(float(x), 3) == 857581.899
821-
assert round(float(y), 3) == 6435414.748
820+
if dialect_name in ["mysql", "mariadb"]:
821+
assert round(float(y), 3) == 6434180.796
822+
else:
823+
assert round(float(y), 3) == 6435414.748
822824

823825

824826
class TestUpdateORM:

0 commit comments

Comments
 (0)