Skip to content

Commit 4083069

Browse files
committed
Trying to fix test
1 parent 7b695dd commit 4083069

File tree

2 files changed

+36
-11
lines changed

2 files changed

+36
-11
lines changed

appointment/tests/utils/test_db_helpers.py

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -185,18 +185,12 @@ def test_another_staff_member_no_day_off(self):
185185
self.assertFalse(check_day_off_for_staff(self.staff_member2, "2023-10-06"))
186186

187187

188-
class TestCreateAndSaveAppointment(BaseTest):
189-
188+
class TestCreateAndSaveAppointment(BaseTest, TestCase):
190189
def setUp(self):
191-
super().setUp() # Call the parent class setup
192-
# Specific setups for this test class
193-
self.ar = self.create_appt_request_for_sm1()
190+
super().setUp()
194191
self.factory = RequestFactory()
195192
self.request = self.factory.get('/')
196-
197-
def tearDown(self):
198-
Appointment.objects.all().delete()
199-
AppointmentRequest.objects.all().delete()
193+
self.ar = self.create_appt_request_for_sm1()
200194

201195
def test_create_and_save_appointment(self):
202196
client_data = {
@@ -210,7 +204,8 @@ def test_create_and_save_appointment(self):
210204
'additional_info': 'Please bring a Zat gun.'
211205
}
212206

213-
appointment = create_and_save_appointment(self.ar, client_data, appointment_data, self.request)
207+
with patch('appointment.utils.db_helpers.schedule_email_reminder') as mock_schedule_reminder:
208+
appointment = create_and_save_appointment(self.ar, client_data, appointment_data, self.request)
214209

215210
self.assertIsNotNone(appointment)
216211
self.assertEqual(appointment.client.email, client_data['email'])
@@ -219,6 +214,32 @@ def test_create_and_save_appointment(self):
219214
self.assertEqual(appointment.address, appointment_data['address'])
220215
self.assertEqual(appointment.additional_info, appointment_data['additional_info'])
221216

217+
if DJANGO_Q_AVAILABLE:
218+
mock_schedule_reminder.assert_called_once()
219+
else:
220+
mock_schedule_reminder.assert_not_called()
221+
222+
@patch('appointment.utils.db_helpers.DJANGO_Q_AVAILABLE', False)
223+
def test_create_and_save_appointment_without_django_q(self):
224+
client_data = {
225+
'email': '[email protected]',
226+
'name': 'samantha.carter',
227+
}
228+
appointment_data = {
229+
'phone': '987654321',
230+
'want_reminder': True,
231+
'address': '456, SGC, Colorado Springs, USA',
232+
'additional_info': 'Bring naquadah generator.'
233+
}
234+
235+
with patch('appointment.utils.db_helpers.logger.warning') as mock_logger_warning:
236+
appointment = create_and_save_appointment(self.ar, client_data, appointment_data, self.request)
237+
238+
self.assertIsNotNone(appointment)
239+
self.assertEqual(appointment.client.email, client_data['email'])
240+
mock_logger_warning.assert_called_with(
241+
f"Email reminder requested for appointment {appointment.id}, but django-q is not available.")
242+
222243

223244
def get_mock_reverse(url_name, **kwargs):
224245
"""A mocked version of the reverse function."""

appointment/utils/db_helpers.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from urllib.parse import urlparse
1212

1313
from django.apps import apps
14+
from django.conf import settings
1415
from django.contrib.auth import get_user_model
1516
from django.core.cache import cache
1617
from django.core.exceptions import FieldDoesNotExist
@@ -26,7 +27,10 @@
2627

2728
logger = get_logger(__name__)
2829

29-
# Check if django-q is installed
30+
# Check if django-q is installed in settings
31+
DJANGO_Q_AVAILABLE = 'django_q' in settings.INSTALLED_APPS
32+
33+
# Check if django-q is installed as a dependency
3034
try:
3135
from django_q.models import Schedule
3236
from django_q.tasks import schedule

0 commit comments

Comments
 (0)