From 5c0d17e6fb7444e7e47fa293983b93d00e675494 Mon Sep 17 00:00:00 2001
From: Piotr Gawron <piotr.gawron@uni.lu>
Date: Thu, 23 Mar 2017 16:35:23 +0100
Subject: [PATCH] unfinished appointments list is sorted by date

---
 smash/web/tests/test_view_notifications.py | 22 ++++++++++++++++++++++
 smash/web/views/notifications.py           |  2 +-
 2 files changed, 23 insertions(+), 1 deletion(-)

diff --git a/smash/web/tests/test_view_notifications.py b/smash/web/tests/test_view_notifications.py
index a11d5202..d0a95c20 100644
--- a/smash/web/tests/test_view_notifications.py
+++ b/smash/web/tests/test_view_notifications.py
@@ -17,6 +17,7 @@ from web.views.notifications import \
     get_subjects_with_reminder_count, \
     get_visits_without_appointments_count, \
     get_today_midnight_date, \
+    get_unfinished_appointments, \
     get_unfinished_appointments_count
 
 
@@ -209,6 +210,27 @@ class NotificationViewTests(TestCase):
         notification = get_unfinished_appointments_count(self.user)
         self.assertEquals(original_notification.count, notification.count)
 
+    def test_get_unfinished_appointments_order(self):
+        subject = create_subject()
+        visit = create_visit(subject)
+
+        appointment = create_appointment(visit, get_today_midnight_date() + datetime.timedelta(days=-10))
+        appointment.status = Appointment.APPOINTMENT_STATUS_SCHEDULED
+        appointment.save()
+        appointment = create_appointment(visit, get_today_midnight_date() + datetime.timedelta(days=-8))
+        appointment.status = Appointment.APPOINTMENT_STATUS_SCHEDULED
+        appointment.save()
+        appointment = create_appointment(visit, get_today_midnight_date() + datetime.timedelta(days=-12))
+        appointment.status = Appointment.APPOINTMENT_STATUS_SCHEDULED
+        appointment.save()
+
+        appointments = get_unfinished_appointments(self.user)
+        self.assertEquals(3, appointments.count())
+
+        # check sort order
+        self.assertTrue(appointments[0].datetime_when < appointments[1].datetime_when)
+        self.assertTrue(appointments[1].datetime_when < appointments[2].datetime_when)
+
     def test_get_subject_with_no_visit_notifications_count_for_many_locations(self):
         create_location("l1")
         create_location("l2")
diff --git a/smash/web/views/notifications.py b/smash/web/views/notifications.py
index e881d908..553faf7a 100644
--- a/smash/web/views/notifications.py
+++ b/smash/web/views/notifications.py
@@ -192,7 +192,7 @@ def get_unfinished_appointments(user):
         datetime_when__lt=get_today_midnight_date(),
         status=Appointment.APPOINTMENT_STATUS_SCHEDULED,
         location__in=get_filter_locations(user),
-    )
+    ).order_by('datetime_when')
 
 
 def waiting_for_appointment(visit):
-- 
GitLab