From 54386fcdd3056a48226fe74c13d98428266b2074 Mon Sep 17 00:00:00 2001 From: Lebaud Antoine Date: Wed, 3 Apr 2024 20:34:53 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=A9=B9(backend)=20address=20test=20flakin?= =?UTF-8?q?ess=20while=20sorting=20Team=20accesses?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Previously, there was a difference between Django's `order_by` behavior and Python's `sorted` function, leading to test failures under specific conditions. For example, entries such as 'Jose Smith' and 'Joseph Walker' were not consistently sorted in the same order between the two methods. To resolve this issue, we've ensured that sorting the expected results in the TeamAccess tests are both case-insensitive and space-insensitive. This adjustment fix tests flakiness --- .../tests/team_accesses/test_api_team_accesses_list.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/backend/core/tests/team_accesses/test_api_team_accesses_list.py b/src/backend/core/tests/team_accesses/test_api_team_accesses_list.py index a76213d..ac04b30 100644 --- a/src/backend/core/tests/team_accesses/test_api_team_accesses_list.py +++ b/src/backend/core/tests/team_accesses/test_api_team_accesses_list.py @@ -266,11 +266,15 @@ def test_api_team_accesses_list_authenticated_ordering_user(ordering_fields): assert response.status_code == HTTP_200_OK assert response.json()["count"] == 21 + def normalize(x): + """Mimic Django order_by, which is case-insensitive and space-insensitive""" + return x.casefold().replace(" ", "") + results = [ team_access["user"][ordering_fields] for team_access in response.json()["results"] ] - assert sorted(results) == results + assert sorted(results, key=normalize) == results response = client.get( f"/api/v1.0/teams/{team.id!s}/accesses/?ordering=-{ordering_fields}", @@ -282,4 +286,4 @@ def test_api_team_accesses_list_authenticated_ordering_user(ordering_fields): team_access["user"][ordering_fields] for team_access in response.json()["results"] ] - assert sorted(results, reverse=True) == results + assert sorted(results, reverse=True, key=normalize) == results