From db7b1bb8ed01e43cc91b5825b5bc0a0fedb4ffdb Mon Sep 17 00:00:00 2001 From: Shoham Elias <116083498+shohamazon@users.noreply.github.com> Date: Mon, 4 Nov 2024 20:57:25 +0200 Subject: [PATCH] Python: Round map values in tests for floating-point comparison (#2592) --------- Signed-off-by: Ubuntu Signed-off-by: Shoham Elias Co-authored-by: Ubuntu --- python/python/tests/test_async_client.py | 10 ++++++++++ python/python/tests/utils/utils.py | 5 +++++ 2 files changed, 15 insertions(+) diff --git a/python/python/tests/test_async_client.py b/python/python/tests/test_async_client.py index c9744157d6..24768423b3 100644 --- a/python/python/tests/test_async_client.py +++ b/python/python/tests/test_async_client.py @@ -105,6 +105,7 @@ get_random_string, is_single_response, parse_info_response, + round_values, ) @@ -2687,6 +2688,7 @@ async def test_geosearchstore_by_box(self, glide_client: TGlideClient): ) expected_map = {member: value[1] for member, value in result.items()} sorted_expected_map = dict(sorted(expected_map.items(), key=lambda x: x[1])) + zrange_map = round_values(zrange_map, 10) assert compare_maps(zrange_map, sorted_expected_map) is True # Test storing results of a box search, unit: kilometes, from a geospatial data, with distance @@ -2706,6 +2708,8 @@ async def test_geosearchstore_by_box(self, glide_client: TGlideClient): ) expected_map = {member: value[0] for member, value in result.items()} sorted_expected_map = dict(sorted(expected_map.items(), key=lambda x: x[1])) + zrange_map = round_values(zrange_map, 10) + sorted_expected_map = round_values(sorted_expected_map, 10) assert compare_maps(zrange_map, sorted_expected_map) is True # Test storing results of a box search, unit: kilometes, from a geospatial data, with count @@ -2746,6 +2750,8 @@ async def test_geosearchstore_by_box(self, glide_client: TGlideClient): b"Palermo": 166274.15156960033, b"edge2": 236529.17986494553, } + zrange_map = round_values(zrange_map, 9) + expected_distances = round_values(expected_distances, 9) assert compare_maps(zrange_map, expected_distances) is True # Test search by box, unit: feet, from a member, with limited ANY count to 2, with hash @@ -2827,6 +2833,8 @@ async def test_geosearchstore_by_radius(self, glide_client: TGlideClient): b"Catania": 0.0, b"Palermo": 166274.15156960033, } + zrange_map = round_values(zrange_map, 9) + expected_distances = round_values(expected_distances, 9) assert compare_maps(zrange_map, expected_distances) is True # Test search by radius, unit: miles, from a geospatial data @@ -2860,6 +2868,8 @@ async def test_geosearchstore_by_radius(self, glide_client: TGlideClient): ) expected_map = {member: value[0] for member, value in result.items()} sorted_expected_map = dict(sorted(expected_map.items(), key=lambda x: x[1])) + zrange_map = round_values(zrange_map, 10) + sorted_expected_map = round_values(sorted_expected_map, 10) assert compare_maps(zrange_map, sorted_expected_map) is True # Test storing results of a radius search, unit: kilometers, from a geospatial data, with limited ANY count to 1 diff --git a/python/python/tests/utils/utils.py b/python/python/tests/utils/utils.py index 497342b5c7..f912d5f6bd 100644 --- a/python/python/tests/utils/utils.py +++ b/python/python/tests/utils/utils.py @@ -137,6 +137,11 @@ def compare_maps( ) +def round_values(map_data: dict, decimal_places: int) -> dict: + """Round the values in a map to the specified number of decimal places.""" + return {key: round(value, decimal_places) for key, value in map_data.items()} + + def convert_bytes_to_string_object( # TODO: remove the str options byte_string_dict: Optional[