Skip to content

Commit 98fe67e

Browse files
committed
Added the rest of the test
1 parent 2d1dd68 commit 98fe67e

22 files changed

+5160
-28
lines changed

appointment/models.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -611,7 +611,7 @@ def is_owner(self, staff_user_id):
611611
def to_dict(self):
612612
return {
613613
"id": self.id,
614-
"client_name": self.client.get_full_name(),
614+
"client_name": self.get_client_name(),
615615
"client_email": self.client.email,
616616
"start_time": self.appointment_request.start_time.strftime('%Y-%m-%d %H:%M'),
617617
"end_time": self.appointment_request.end_time.strftime('%Y-%m-%d %H:%M'),
Lines changed: 224 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,224 @@
1+
from copy import deepcopy
2+
from datetime import datetime, time, timedelta
3+
4+
from django.core.exceptions import ValidationError
5+
from django.utils import timezone
6+
from django.utils.translation import gettext as _
7+
8+
from appointment.models import Appointment, DayOff, WorkingHours
9+
from appointment.tests.base.base_test import BaseTest
10+
from appointment.utils.date_time import get_weekday_num
11+
12+
13+
class AppointmentCreationTestCase(BaseTest):
14+
@classmethod
15+
def setUpClass(cls):
16+
super().setUpClass()
17+
18+
@classmethod
19+
def tearDownClass(cls):
20+
super().tearDownClass()
21+
22+
def setUp(self):
23+
self.address = 'Stargate Command, Cheyenne Mountain Complex, Colorado Springs, CO'
24+
self.ar = self.create_appt_request_for_sm1()
25+
self.appointment = self.create_appt_for_sm1(appointment_request=self.ar)
26+
self.client_ = self.users['client1']
27+
self.expected_end_time = datetime.combine(self.ar.date, self.ar.end_time)
28+
self.expected_service_name = 'Stargate Activation'
29+
self.expected_service_price = 100000
30+
self.expected_start_time = datetime.combine(self.ar.date, self.ar.start_time)
31+
self.phone = '+12392340543'
32+
return super().setUp()
33+
34+
def tearDown(self):
35+
self.appointment.delete()
36+
return super().tearDown()
37+
38+
def test_default_attributes_on_creation(self):
39+
"""Test default attributes when an appointment is created."""
40+
self.assertIsNotNone(self.appointment)
41+
self.assertIsNotNone(self.appointment.created_at)
42+
self.assertIsNotNone(self.appointment.updated_at)
43+
self.assertIsNotNone(self.appointment.get_appointment_id_request())
44+
self.assertIsNone(self.appointment.additional_info)
45+
self.assertEqual(self.appointment.client, self.users['client1'])
46+
self.assertEqual(self.appointment.phone, self.phone)
47+
self.assertEqual(self.appointment.address, self.address)
48+
self.assertFalse(self.appointment.want_reminder)
49+
50+
def test_str_representation(self):
51+
"""Test if an appointment's string representation is correct."""
52+
expected_str = f"{self.client_} - {self.ar.start_time.strftime('%Y-%m-%d %H:%M')} to " \
53+
f"{self.ar.end_time.strftime('%Y-%m-%d %H:%M')}"
54+
self.assertEqual(str(self.appointment), expected_str)
55+
56+
def test_appointment_getters(self):
57+
"""Test getter methods for appointment details."""
58+
self.assertEqual(self.appointment.get_start_time(), self.expected_start_time)
59+
self.assertEqual(self.appointment.get_end_time(), self.expected_end_time)
60+
self.assertEqual(self.appointment.get_service_name(), self.expected_service_name)
61+
self.assertEqual(self.appointment.get_service_price(), self.expected_service_price)
62+
self.assertEqual(self.appointment.is_paid_text(), _('No'))
63+
self.assertEqual(self.appointment.get_appointment_amount_to_pay(), self.expected_service_price)
64+
self.assertEqual(self.appointment.get_service_down_payment(), self.service1.get_down_payment())
65+
self.assertEqual(self.appointment.get_service_description(), self.service1.description)
66+
self.assertEqual(self.appointment.get_appointment_date(), self.ar.date)
67+
self.assertEqual(self.appointment.get_service_duration(), "1 hour")
68+
self.assertEqual(self.appointment.get_appointment_currency(), "USD")
69+
self.assertEqual(self.appointment.get_appointment_amount_to_pay(), self.ar.get_service_price())
70+
self.assertEqual(self.appointment.get_service_img_url(), "")
71+
self.assertEqual(self.appointment.get_staff_member_name(), self.staff_member1.get_staff_member_name())
72+
self.assertTrue(self.appointment.service_is_paid())
73+
self.assertFalse(self.appointment.is_paid())
74+
75+
def test_conversion_to_dict(self):
76+
response = {
77+
'id': 1,
78+
'client_name': self.client_.first_name,
79+
'client_email': self.client_.email,
80+
'start_time': '1900-01-01 09:00',
81+
'end_time': '1900-01-01 10:00',
82+
'service_name': self.expected_service_name,
83+
'address': self.address,
84+
'want_reminder': False,
85+
'additional_info': None,
86+
'paid': False,
87+
'amount_to_pay': self.expected_service_price,
88+
}
89+
actual_response = self.appointment.to_dict()
90+
actual_response.pop('id_request', None)
91+
self.assertEqual(actual_response, response)
92+
93+
94+
class AppointmentValidDateTestCase(BaseTest):
95+
def setUp(self):
96+
super().setUp()
97+
self.weekday = "Monday" # Example weekday
98+
self.weekday_num = get_weekday_num(self.weekday)
99+
self.wh = WorkingHours.objects.create(staff_member=self.staff_member1, day_of_week=self.weekday_num,
100+
start_time=time(9, 0), end_time=time(17, 0))
101+
self.appt_date = timezone.now().date() + timedelta(days=(self.weekday_num - timezone.now().weekday()) % 7)
102+
self.start_time = timezone.now().replace(hour=10, minute=0, second=0, microsecond=0)
103+
self.current_appointment_id = None
104+
105+
def tearDown(self):
106+
self.wh.delete()
107+
return super().tearDown()
108+
109+
def test_staff_member_works_on_given_day(self):
110+
is_valid, message = Appointment.is_valid_date(self.appt_date, self.start_time, self.staff_member1,
111+
self.current_appointment_id, self.weekday)
112+
self.assertTrue(is_valid)
113+
114+
def test_staff_member_does_not_work_on_given_day(self):
115+
non_working_day = "Sunday"
116+
non_working_day_num = get_weekday_num(non_working_day)
117+
appt_date = self.appt_date + timedelta(days=(non_working_day_num - self.weekday_num) % 7)
118+
is_valid, message = Appointment.is_valid_date(appt_date, self.start_time, self.staff_member1,
119+
self.current_appointment_id, non_working_day)
120+
self.assertFalse(is_valid)
121+
self.assertIn("does not work on this day", message)
122+
123+
def test_start_time_outside_working_hours(self):
124+
early_start_time = timezone.now().replace(hour=8, minute=0) # Before working hours
125+
is_valid, message = Appointment.is_valid_date(self.appt_date, early_start_time, self.staff_member1,
126+
self.current_appointment_id, self.weekday)
127+
self.assertFalse(is_valid)
128+
self.assertIn("outside of", message)
129+
130+
def test_staff_member_has_day_off(self):
131+
DayOff.objects.create(staff_member=self.staff_member1, start_date=self.appt_date, end_date=self.appt_date)
132+
is_valid, message = Appointment.is_valid_date(self.appt_date, self.start_time, self.staff_member1,
133+
self.current_appointment_id, self.weekday)
134+
self.assertFalse(is_valid)
135+
self.assertIn("has a day off on this date", message)
136+
137+
138+
class AppointmentValidationTestCase(BaseTest):
139+
@classmethod
140+
def setUpClass(cls):
141+
super().setUpClass()
142+
143+
@classmethod
144+
def tearDownClass(cls):
145+
super().tearDownClass()
146+
147+
def setUp(self):
148+
self.tomorrow = timezone.now().date() + timedelta(days=1)
149+
self.ar = self.create_appointment_request_(service=self.service2, staff_member=self.staff_member2,
150+
date_=self.tomorrow)
151+
self.appointment = self.create_appt_for_sm2(appointment_request=self.ar)
152+
self.client_ = self.users['client1']
153+
return super().setUp()
154+
155+
def tearDown(self):
156+
self.appointment.delete()
157+
return super().tearDown()
158+
159+
def test_invalid_phone_number(self):
160+
"""Test that an appointment cannot be created with an invalid phone number."""
161+
self.appointment.phone = "1234" # Invalid phone number
162+
with self.assertRaises(ValidationError):
163+
self.appointment.full_clean()
164+
165+
def test_set_paid_status(self):
166+
"""Test if an appointment's paid status can be set."""
167+
appointment = deepcopy(self.appointment)
168+
appointment.set_appointment_paid_status(True)
169+
self.assertTrue(appointment.is_paid())
170+
appointment.set_appointment_paid_status(False)
171+
self.assertFalse(appointment.is_paid())
172+
173+
def test_save_with_down_payment(self):
174+
"""Test if an appointment can be saved with a down payment."""
175+
ar = self.create_appointment_request_(service=self.service2, staff_member=self.staff_member2,
176+
date_=self.tomorrow)
177+
appointment = self.create_appt_for_sm2(appointment_request=ar)
178+
ar.payment_type = 'down'
179+
ar.save()
180+
appointment.save()
181+
self.assertEqual(appointment.get_service_down_payment(), self.service2.get_down_payment())
182+
183+
def test_creation_without_appointment_request(self):
184+
"""Test that an appointment cannot be created without an appointment request."""
185+
with self.assertRaises(ValidationError): # Assuming model validation prevents this
186+
Appointment.objects.create(client=self.client_)
187+
188+
def test_creation_without_client(self):
189+
"""Test that an appointment can be created without a client."""
190+
ar = self.create_appointment_request_(service=self.service2, staff_member=self.staff_member2,
191+
date_=self.tomorrow)
192+
appt = Appointment.objects.create(appointment_request=ar)
193+
self.assertIsNone(appt.client)
194+
195+
def test_creation_without_required_fields(self):
196+
"""Test that an appointment cannot be created without the required fields."""
197+
with self.assertRaises(ValidationError):
198+
Appointment.objects.create()
199+
200+
def test_get_staff_member_name_without_staff_member(self):
201+
"""Test if you get_staff_member_name method returns an empty string when no staff member is associated."""
202+
ar = self.create_appointment_request_(service=self.service2, staff_member=self.staff_member2,
203+
date_=self.tomorrow)
204+
appointment = self.create_appt_for_sm2(appointment_request=ar)
205+
appointment.appointment_request.staff_member = None
206+
appointment.appointment_request.save()
207+
self.assertEqual(appointment.get_staff_member_name(), "")
208+
209+
def test_rescheduling(self):
210+
"""Simulate appointment rescheduling by changing the appointment date and times."""
211+
ar = self.create_appointment_request_(service=self.service2, staff_member=self.staff_member2,
212+
date_=self.tomorrow)
213+
appointment = self.create_appt_for_sm2(appointment_request=ar)
214+
new_date = ar.date + timedelta(days=1)
215+
new_start_time = time(10, 0)
216+
new_end_time = time(11, 0)
217+
ar.date = new_date
218+
ar.start_time = new_start_time
219+
ar.end_time = new_end_time
220+
ar.save()
221+
222+
self.assertEqual(appointment.get_date(), new_date)
223+
self.assertEqual(appointment.get_start_time().time(), new_start_time)
224+
self.assertEqual(appointment.get_end_time().time(), new_end_time)

appointment/tests/models/test_appointment_request.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def tearDown(self):
2424
self.ar.delete()
2525
super().tearDown()
2626

27-
def test_appointment_request_is_properly_created(self):
27+
def test_default_attributes_on_creation(self):
2828
self.assertIsNotNone(self.ar)
2929
self.assertEqual(self.ar.service, self.service1)
3030
self.assertEqual(self.ar.staff_member, self.staff_member1)

appointment/tests/models/test_config.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ def test_default_attributes_on_creation(self):
3030
self.assertEqual(self.config.finish_time, time(17, 0))
3131
self.assertEqual(self.config.appointment_buffer_time, 2.0)
3232
self.assertEqual(self.config.website_name, "Stargate Command")
33+
self.assertIsNotNone(Config.get_instance())
3334

3435
def test_multiple_config_creation(self):
3536
"""Test that only one configuration can be created."""

0 commit comments

Comments
 (0)