From d6213112ea3fcf2ab1104cbbd617ae008705cb97 Mon Sep 17 00:00:00 2001
From: Piotr Gawron <piotr.gawron@uni.lu>
Date: Tue, 13 Jun 2017 17:25:14 +0200
Subject: [PATCH] kit requests appointments are ordered by date

---
 smash/web/tests/test_view_kit_request.py | 55 ++++++++++++++++++------
 smash/web/views/kit.py                   |  3 +-
 2 files changed, 44 insertions(+), 14 deletions(-)

diff --git a/smash/web/tests/test_view_kit_request.py b/smash/web/tests/test_view_kit_request.py
index bcba4e87..4a061d4a 100644
--- a/smash/web/tests/test_view_kit_request.py
+++ b/smash/web/tests/test_view_kit_request.py
@@ -3,8 +3,9 @@ import datetime
 from django.core import mail
 from django.urls import reverse
 
-from functions import create_appointment_type, create_appointment
+from functions import create_appointment_type, create_appointment, create_visit
 from web.models import Item, Appointment, AppointmentTypeLink
+from web.views.kit import get_kit_requests
 from web.views.notifications import get_today_midnight_date
 from . import LoggedInTestCase
 
@@ -26,7 +27,6 @@ class ViewFunctionsTests(LoggedInTestCase):
         appointment.save()
         AppointmentTypeLink.objects.create(appointment=appointment, appointment_type=appointment_type)
 
-
         response = self.client.get(reverse('web.views.kit_requests'))
         self.assertEqual(response.status_code, 200)
 
@@ -62,28 +62,57 @@ class ViewFunctionsTests(LoggedInTestCase):
         appointment.save()
         AppointmentTypeLink.objects.create(appointment=appointment, appointment_type=appointment_type)
 
-
         response = self.client.get(reverse('web.views.kit_requests'))
         self.assertEqual(response.status_code, 200)
 
         self.assertTrue(item_name in response.content)
 
-    def test_kit_requests_send_email(self):
+    def test_kit_requests_order(self):
         item_name = "Test item to be ordered"
         item = Item.objects.create(disposable=True, name=item_name)
         appointment_type = create_appointment_type()
         appointment_type.required_equipment.add(item)
         appointment_type.save()
 
-        appointment = create_appointment()
-        appointment.datetime_when = get_today_midnight_date() + datetime.timedelta(days=2)
-        appointment.save()
-        AppointmentTypeLink.objects.create(appointment=appointment, appointment_type=appointment_type)
+        visit = create_visit();
 
-        response = self.client.get(reverse('web.views.kit_requests_send_mail',
-                                           kwargs={'start_date': str(get_today_midnight_date().strftime("%Y-%m-%d"))}))
-        self.assertEqual(response.status_code, 200)
+        appointment1 = create_appointment(visit)
+        appointment1.datetime_when = get_today_midnight_date() + datetime.timedelta(days=3)
+        appointment1.save()
+        AppointmentTypeLink.objects.create(appointment=appointment1, appointment_type=appointment_type)
 
-        self.assertTrue(item_name in response.content)
+        appointment2 = create_appointment(visit)
+        appointment2.datetime_when = get_today_midnight_date() + datetime.timedelta(days=4)
+        appointment2.save()
+        AppointmentTypeLink.objects.create(appointment=appointment2, appointment_type=appointment_type)
+
+        appointment3 = create_appointment(visit)
+        appointment3.datetime_when = get_today_midnight_date() + datetime.timedelta(days=2)
+        appointment3.save()
+        AppointmentTypeLink.objects.create(appointment=appointment3, appointment_type=appointment_type)
+
+        result = get_kit_requests(self.user)
+        self.assertEqual(appointment3, result['appointments'][0])
+        self.assertEqual(appointment1, result['appointments'][1])
+        self.assertEqual(appointment2, result['appointments'][2])
+
+
+def test_kit_requests_send_email(self):
+    item_name = "Test item to be ordered"
+    item = Item.objects.create(disposable=True, name=item_name)
+    appointment_type = create_appointment_type()
+    appointment_type.required_equipment.add(item)
+    appointment_type.save()
+
+    appointment = create_appointment()
+    appointment.datetime_when = get_today_midnight_date() + datetime.timedelta(days=2)
+    appointment.save()
+    AppointmentTypeLink.objects.create(appointment=appointment, appointment_type=appointment_type)
+
+    response = self.client.get(reverse('web.views.kit_requests_send_mail',
+                                       kwargs={'start_date': str(get_today_midnight_date().strftime("%Y-%m-%d"))}))
+    self.assertEqual(response.status_code, 200)
+
+    self.assertTrue(item_name in response.content)
 
-        self.assertEqual(1, len(mail.outbox))
+    self.assertEqual(1, len(mail.outbox))
diff --git a/smash/web/views/kit.py b/smash/web/views/kit.py
index 3afcb4ab..7ade6ba5 100644
--- a/smash/web/views/kit.py
+++ b/smash/web/views/kit.py
@@ -34,7 +34,8 @@ def get_kit_requests(user, start_date=None, end_date=None):
         datetime_when__gt=start_date,
         location__in=get_filter_locations(user),
         status=Appointment.APPOINTMENT_STATUS_SCHEDULED,
-    )
+    ).order_by('datetime_when')
+
     if end_date is not None:
         appointments = appointments.filter(datetime_when__lt=end_date)
 
-- 
GitLab