TST: expand assert messages in assert_geoseries_equal (#1808)

* expanded assert messages. Refactor of duplicated code

* black formatted

* fix forgotten if

* fix forgotten ifs

* reformat code, add indeces to error and full first geometries

* fix typo

* expand tests and check for error message

* truncate string

Co-authored-by: ImanolUr <imanol@live-eo.com>
Co-authored-by: Martin Fleischmann <martin@martinfleischmann.net>
Co-authored-by: Joris Van den Bossche <jorisvandenbossche@gmail.com>
This commit is contained in:
Imanol
2021-02-25 22:51:52 +00:00
committed by GitHub
co-authored by ImanolUr Martin Fleischmann Joris Van den Bossche
parent f7d8bd13e7
commit 6e8f6f91cb
2 changed files with 68 additions and 13 deletions
+52 -12
View File
@@ -133,15 +133,58 @@ def assert_geoseries_equal(
if not check_crs:
with warnings.catch_warnings():
warnings.filterwarnings("ignore", "CRS mismatch", UserWarning)
if check_less_precise:
assert geom_almost_equals(left, right)
else:
assert geom_equals(left, right)
_check_equality(left, right, check_less_precise)
else:
if check_less_precise:
assert geom_almost_equals(left, right)
else:
assert geom_equals(left, right)
_check_equality(left, right, check_less_precise)
def _truncated_string(geom):
"""Truncated WKT repr of geom"""
s = str(geom)
if len(s) > 100:
return s[:100] + "..."
else:
return s
def _check_equality(left, right, check_less_precise):
assert_error_message = (
"{0} out of {1} geometries are not {3}equal.\n"
"Indices where geometries are not {3}equal: {2} \n"
"The first not {3}equal geometry:\n"
"Left: {4}\n"
"Right: {5}\n"
)
if check_less_precise:
precise = "almost "
if not geom_almost_equals(left, right):
unequal_left_geoms = left[~left.geom_almost_equals(right)]
unequal_right_geoms = right[~left.geom_almost_equals(right)]
raise AssertionError(
assert_error_message.format(
len(unequal_left_geoms),
len(left),
unequal_left_geoms.index.to_list(),
precise,
_truncated_string(unequal_left_geoms.iloc[0]),
_truncated_string(unequal_right_geoms.iloc[0]),
)
)
else:
precise = ""
if not geom_equals(left, right):
unequal_left_geoms = left[~left.geom_almost_equals(right)]
unequal_right_geoms = right[~left.geom_almost_equals(right)]
raise AssertionError(
assert_error_message.format(
len(unequal_left_geoms),
len(left),
unequal_left_geoms.index.to_list(),
precise,
_truncated_string(unequal_left_geoms.iloc[0]),
_truncated_string(unequal_right_geoms.iloc[0]),
)
)
def assert_geodataframe_equal(
@@ -212,10 +255,7 @@ def assert_geodataframe_equal(
"GeoDataFrame shape mismatch, left: {lshape!r}, right: {rshape!r}.\n"
"Left columns: {lcols!r}, right columns: {rcols!r}"
).format(
lshape=left.shape,
rshape=right.shape,
lcols=left.columns,
rcols=right.columns,
lshape=left.shape, rshape=right.shape, lcols=left.columns, rcols=right.columns
)
if check_like:
+16 -1
View File
@@ -46,6 +46,14 @@ s4 = s1.copy()
s4.crs = 4326
s5 = s2.copy()
s5.crs = 27700
s6 = GeoSeries(
[
Polygon([(0, 3), (0, 0), (2, 0), (2, 2)]),
Polygon([(2, 2), (4, 2), (4, 4), (2, 4)]),
]
)
df4 = GeoDataFrame(
{"col1": [1, 2], "geometry": s1.copy(), "geom2": s4.copy(), "geom3": s5.copy()},
crs=3857,
@@ -63,8 +71,15 @@ def test_geoseries():
assert_geoseries_equal(s3, s2, check_series_type=False, check_dtype=False)
assert_geoseries_equal(s1, s4, check_series_type=False)
with pytest.raises(AssertionError):
with pytest.raises(AssertionError) as error:
assert_geoseries_equal(s1, s2, check_less_precise=True)
assert "1 out of 2 geometries are not almost equal" in str(error.value)
assert "not almost equal: [0]" in str(error.value)
with pytest.raises(AssertionError) as error:
assert_geoseries_equal(s2, s6, check_less_precise=False)
assert "1 out of 2 geometries are not equal" in str(error.value)
assert "not equal: [0]" in str(error.value)
def test_geodataframe():