From 6e8f6f91cbe0c40ca72efecaed95912beb23548d Mon Sep 17 00:00:00 2001 From: Imanol Date: Thu, 25 Feb 2021 23:51:52 +0100 Subject: [PATCH] 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 Co-authored-by: Martin Fleischmann Co-authored-by: Joris Van den Bossche --- geopandas/testing.py | 64 ++++++++++++++++++++++++++------- geopandas/tests/test_testing.py | 17 ++++++++- 2 files changed, 68 insertions(+), 13 deletions(-) diff --git a/geopandas/testing.py b/geopandas/testing.py index 437af0d..7e0daa7 100644 --- a/geopandas/testing.py +++ b/geopandas/testing.py @@ -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: diff --git a/geopandas/tests/test_testing.py b/geopandas/tests/test_testing.py index 582c01b..e3cec21 100644 --- a/geopandas/tests/test_testing.py +++ b/geopandas/tests/test_testing.py @@ -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():