|
| 1 | +from django.test import TestCase |
| 2 | +from datetime import date, timedelta, time |
| 3 | +from appointment.models import Service, AppointmentRequest, Config, Appointment |
| 4 | +from appointment.utils import Utility |
| 5 | +from appointment.views import get_appointments_and_slots |
| 6 | + |
| 7 | + |
| 8 | +class SlotAvailabilityTest(TestCase): |
| 9 | + def setUp(self): |
| 10 | + self.service = Service.objects.create(name="Test Service", duration=timedelta(hours=2), price=100) |
| 11 | + self.start_time = time(11, 0) |
| 12 | + self.finish_time = time(14, 0) |
| 13 | + self.slot_duration = 120 |
| 14 | + self.appointment_buffer_time = 0 |
| 15 | + self.config = Config.objects.create( |
| 16 | + lead_time=self.start_time, |
| 17 | + finish_time=self.finish_time, |
| 18 | + slot_duration=self.slot_duration, |
| 19 | + appointment_buffer_time=self.appointment_buffer_time |
| 20 | + ) |
| 21 | + self.user_model = Utility.get_user_model() |
| 22 | + self.user = self.user_model.objects.create_user(first_name="Tester", |
| 23 | + |
| 24 | + username="test_user", password="Kfdqi3!?n") |
| 25 | + self.test_date = date.today() + timedelta(days=1) # Use tomorrow's date for the tests |
| 26 | + |
| 27 | + def test_slot_availability_without_appointments(self): |
| 28 | + _, available_slots = get_appointments_and_slots(self.test_date, self.service) |
| 29 | + expected_slots = ['11:00 AM', '01:00 PM'] |
| 30 | + self.assertEqual(available_slots, expected_slots) |
| 31 | + |
| 32 | + def test_slot_availability_with_first_slot_booked(self): |
| 33 | + ar = AppointmentRequest.objects.create(date=self.test_date, start_time=self.start_time, end_time=time(13, 0), |
| 34 | + service=self.service) |
| 35 | + Appointment.objects.create(client=self.user, appointment_request=ar) |
| 36 | + _, available_slots = get_appointments_and_slots(self.test_date, self.service) |
| 37 | + expected_slots = ['01:00 PM'] |
| 38 | + self.assertEqual(available_slots, expected_slots) |
| 39 | + |
| 40 | + def test_slot_availability_with_second_slot_booked(self): |
| 41 | + ar = AppointmentRequest.objects.create(date=self.test_date, start_time=time(13, 0), end_time=time(15, 0), |
| 42 | + service=self.service) |
| 43 | + Appointment.objects.create(client=self.user, appointment_request=ar) |
| 44 | + _, available_slots = get_appointments_and_slots(self.test_date, self.service) |
| 45 | + expected_slots = ['11:00 AM'] |
| 46 | + self.assertEqual(available_slots, expected_slots) |
| 47 | + |
| 48 | + def test_slot_availability_with_both_slots_booked(self): |
| 49 | + ar1 = AppointmentRequest.objects.create(date=self.test_date, start_time=self.start_time, end_time=time(13, 0), |
| 50 | + service=self.service) |
| 51 | + ar2 = AppointmentRequest.objects.create(date=self.test_date, start_time=time(13, 0), end_time=time(15, 0), |
| 52 | + service=self.service) |
| 53 | + Appointment.objects.create(client=self.user, appointment_request=ar1) |
| 54 | + Appointment.objects.create(client=self.user, appointment_request=ar2) |
| 55 | + _, available_slots = get_appointments_and_slots(self.test_date, self.service) |
| 56 | + expected_slots = [] |
| 57 | + self.assertEqual(available_slots, expected_slots) |
0 commit comments