From 277484d419b304951533681e11144b710519c7b2 Mon Sep 17 00:00:00 2001
From: Piotr Gawron <piotr.gawron@uni.lu>
Date: Mon, 3 Apr 2017 18:31:49 +0200
Subject: [PATCH] unit test for appointments api

---
 smash/web/tests/test_api_appointment.py | 65 +++++++++++++++++++++++--
 1 file changed, 62 insertions(+), 3 deletions(-)

diff --git a/smash/web/tests/test_api_appointment.py b/smash/web/tests/test_api_appointment.py
index 2fcd41c4..bf28f7c7 100644
--- a/smash/web/tests/test_api_appointment.py
+++ b/smash/web/tests/test_api_appointment.py
@@ -1,12 +1,16 @@
 # coding=utf-8
 
+import datetime
+
 from django.contrib.auth.models import User
 from django.test import Client
 from django.test import TestCase
 from django.urls import reverse
 
-from web.tests.functions import create_subject, create_worker, create_visit, create_appointment
-from web.views.appointment import APPOINTMENT_LIST_GENERIC
+from web.tests.functions import create_subject, create_worker, create_visit, create_appointment, \
+    create_appointment_type, create_get_suffix
+from web.views.appointment import APPOINTMENT_LIST_GENERIC, APPOINTMENT_LIST_APPROACHING, APPOINTMENT_LIST_UNFINISHED
+from web.views.notifications import get_today_midnight_date
 
 
 class TestApi(TestCase):
@@ -30,8 +34,9 @@ class TestApi(TestCase):
         self.subject.save()
         visit = create_visit(self.subject)
         create_appointment(visit)
-        appointment2 = create_appointment(visit)
+        appointment2 = create_appointment(visit, get_today_midnight_date())
         appointment2.visit = None
+        appointment2.appointment_types.add(create_appointment_type())
         appointment2.save()
 
         url = reverse('web.api.appointments', kwargs={'type': APPOINTMENT_LIST_GENERIC})
@@ -39,3 +44,57 @@ class TestApi(TestCase):
 
         self.assertEqual(response.status_code, 200)
         self.assertTrue(name in response.content)
+
+    def test_appointments_approaching(self):
+        name = "Piotrek"
+        self.subject.first_name = name
+        self.subject.save()
+        visit = create_visit(self.subject)
+        create_appointment(visit, get_today_midnight_date() + datetime.timedelta(days=2))
+
+        url = reverse('web.api.appointments', kwargs={'type': APPOINTMENT_LIST_APPROACHING})
+        response = self.client.get(url)
+
+        self.assertEqual(response.status_code, 200)
+        self.assertTrue(name in response.content)
+
+    def test_appointments_unfinished(self):
+        name = "Piotrek"
+        self.subject.first_name = name
+        self.subject.save()
+        visit = create_visit(self.subject)
+        create_appointment(visit, get_today_midnight_date() + datetime.timedelta(days=-12))
+
+        url = reverse('web.api.appointments', kwargs={'type': APPOINTMENT_LIST_UNFINISHED})
+        response = self.client.get(url)
+
+        self.assertEqual(response.status_code, 200)
+        self.assertTrue(name in response.content)
+
+    def test_get_calendar_appointments(self):
+        name = "Peter"
+        self.subject.first_name = name
+        self.subject.save()
+        visit = create_visit(self.subject)
+        appointment = create_appointment(visit, get_today_midnight_date())
+        appointment.appointment_types.add(create_appointment_type())
+        appointment.save()
+
+        params = {
+            "start_date": (get_today_midnight_date() + datetime.timedelta(days=2)).strftime("%Y-%m-%d"),
+            "end_date": (get_today_midnight_date() + datetime.timedelta(days=3)).strftime("%Y-%m-%d"),
+        }
+        url = ("%s" + create_get_suffix(params)) % reverse('web.api.appointments',
+                                                           kwargs={'type': APPOINTMENT_LIST_GENERIC})
+        response = self.client.get(url)
+
+        self.assertEqual(response.status_code, 200)
+        self.assertFalse(name in response.content)
+
+        params["start_date"] = (get_today_midnight_date() + datetime.timedelta(days=-2)).strftime("%Y-%m-%d")
+        url = ("%s" + create_get_suffix(params)) % reverse('web.api.appointments',
+                                                           kwargs={'type': APPOINTMENT_LIST_GENERIC})
+        response = self.client.get(url)
+
+        self.assertEqual(response.status_code, 200)
+        self.assertTrue(name in response.content)
-- 
GitLab