From 0185270308fb2ebd044c95fec6325fbecc010dbb Mon Sep 17 00:00:00 2001 From: Chris Hill-Scott Date: Wed, 23 Oct 2019 15:05:26 +0100 Subject: [PATCH] Fix sorting of events MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Events should be sorted reverse-chronologically, no matter what order they come back from the API in, or which field in the API response they’ve been extracted from. --- app/main/views/history.py | 3 ++- tests/app/main/views/test_history.py | 14 +++++++++++--- tests/conftest.py | 14 +++++++++++++- 3 files changed, 26 insertions(+), 5 deletions(-) diff --git a/app/main/views/history.py b/app/main/views/history.py index 07614db3a..ca6dd60fb 100644 --- a/app/main/views/history.py +++ b/app/main/views/history.py @@ -1,4 +1,5 @@ from collections import defaultdict +from operator import attrgetter from flask import render_template, request @@ -36,7 +37,7 @@ def _chunk_events_by_day(events): days = defaultdict(list) - for event in events: + for event in sorted(events, key=attrgetter('time'), reverse=True): days[format_date_numeric(event.time)].append(event) return sorted(days.items(), reverse=True) diff --git a/tests/app/main/views/test_history.py b/tests/app/main/views/test_history.py index 8b0e64d13..f25898f39 100644 --- a/tests/app/main/views/test_history.py +++ b/tests/app/main/views/test_history.py @@ -9,8 +9,10 @@ from tests.conftest import SERVICE_ONE_ID, normalize_spaces ( '12 December', ( + 'Test User 1:13pm ' + 'Renamed this service from ‘Before lunch’ to ‘After lunch’ ' 'Test User 12:12pm ' - 'Renamed this service from ‘Example service’ to ‘Real service’' + 'Renamed this service from ‘Example service’ to ‘Before lunch’' ), ), ( @@ -32,6 +34,8 @@ from tests.conftest import SERVICE_ONE_ID, normalize_spaces ( 'Test User 11:10am ' 'Created an API key called ‘Good key’ ' + 'Test User 10:09am ' + 'Created an API key called ‘Key event returned in non-chronological order’ ' 'Unknown 2:01am ' 'Created this service and called it ‘Example service’' ), @@ -56,7 +60,9 @@ from tests.conftest import SERVICE_ONE_ID, normalize_spaces '10 October 2010', ( 'Test User 11:10am ' - 'Created an API key called ‘Good key’' + 'Created an API key called ‘Good key’ ' + 'Test User 10:09am ' + 'Created an API key called ‘Key event returned in non-chronological order’' ), ), ]), @@ -64,8 +70,10 @@ from tests.conftest import SERVICE_ONE_ID, normalize_spaces ( '12 December', ( + 'Test User 1:13pm ' + 'Renamed this service from ‘Before lunch’ to ‘After lunch’ ' 'Test User 12:12pm ' - 'Renamed this service from ‘Example service’ to ‘Real service’' + 'Renamed this service from ‘Example service’ to ‘Before lunch’' ), ), ( diff --git a/tests/conftest.py b/tests/conftest.py index 271c6001f..6d45b0cc3 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -3514,11 +3514,17 @@ def mock_get_service_history(mocker): 'created_by_id': uuid4(), }, { - 'name': 'Real service', + 'name': 'Before lunch', 'created_at': '2010-10-10T01:01:01.000000Z', 'updated_at': '2012-12-12T12:12:12.000000Z', 'created_by_id': sample_uuid(), }, + { + 'name': 'After lunch', + 'created_at': '2010-10-10T01:01:01.000000Z', + 'updated_at': '2012-12-12T13:13:13.000000Z', + 'created_by_id': sample_uuid(), + }, ], 'api_key_history': [ { @@ -3539,6 +3545,12 @@ def mock_get_service_history(mocker): 'created_at': '2011-11-11T11:11:11.000000Z', 'created_by_id': sample_uuid(), }, + { + 'name': 'Key event returned in non-chronological order', + 'updated_at': None, + 'created_at': '2010-10-10T09:09:09.000000Z', + 'created_by_id': sample_uuid(), + }, ], 'events': [], })