From 6c704c40a34238d2d8137d150dff85896267bc18 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luk=C3=A1=C5=A1=20Kucharczyk?= Date: Sun, 21 Jun 2026 14:07:08 +0200 Subject: [PATCH] test(sessions): make duration sort test discriminate from default order (#68) Test was passing for wrong reason: both default order (-date) and -duration sort put Beta first because Beta had longer duration AND later date. Make order diverge by swapping durations, so -duration must override date-based ordering to pass. Also fix test to extract tbody to avoid matching "Beta" in header's last-session button. Co-Authored-By: Claude Opus 4.8 --- tests/test_sorting.py | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/tests/test_sorting.py b/tests/test_sorting.py index 255093d..57c0317 100644 --- a/tests/test_sorting.py +++ b/tests/test_sorting.py @@ -174,21 +174,28 @@ class TestListGamesSort: class TestListSessionsSort: def test_sort_by_duration_descending(self, logged_client, two_games): + import re + alpha, beta = two_games Session.objects.create( game=alpha, timestamp_start=datetime(2022, 1, 1, 10, tzinfo=ZONEINFO), - timestamp_end=datetime(2022, 1, 1, 10, 30, tzinfo=ZONEINFO), # 30 min + timestamp_end=datetime(2022, 1, 1, 13, tzinfo=ZONEINFO), # 3 h, earlier date ) Session.objects.create( game=beta, timestamp_start=datetime(2022, 1, 2, 10, tzinfo=ZONEINFO), - timestamp_end=datetime(2022, 1, 2, 13, tzinfo=ZONEINFO), # 3 h + timestamp_end=datetime(2022, 1, 2, 10, 30, tzinfo=ZONEINFO), # 30 min, later date ) + # default order is -date (beta first); -duration must override it (alpha's 3h first) response = logged_client.get(reverse("games:list_sessions"), {"sort": "-duration"}) assert response.status_code == 200 body = response.content.decode() - assert body.index("Beta") < body.index("Alpha") # longer session first + # Extract only the table body to avoid finding "Beta" in header's last-session button + tbody_match = re.search(r"]*>(.*?)", body, re.DOTALL) + assert tbody_match + tbody = tbody_match.group(1) + assert tbody.index("Alpha") < tbody.index("Beta") # longer session first, despite earlier date def test_unknown_sort_emits_warning(self, logged_client, two_games): response = logged_client.get(reverse("games:list_sessions"), {"sort": "nope"})