2
2
# Path: appointment/tests/utils/test_db_helpers.py
3
3
4
4
import datetime
5
- from unittest import skip
6
5
from unittest .mock import MagicMock , PropertyMock , patch
7
6
8
7
from django .apps import apps
13
12
from django .test .client import RequestFactory
14
13
from django .urls import reverse
15
14
from django .utils import timezone
15
+ from django_q .models import Schedule
16
16
17
- from appointment .logger_config import get_logger
18
17
from appointment .models import Config , DayOff , PaymentInfo
19
18
from appointment .tests .base .base_test import BaseTest
20
19
from appointment .tests .mixins .base_mixin import ConfigMixin
32
31
staff_change_allowed_on_reschedule , update_appointment_reminder , username_in_user_model , working_hours_exist
33
32
)
34
33
35
- logger = get_logger (__name__ )
36
-
37
- # Check if django-q is installed
38
- try :
39
- from django_q .models import Schedule
40
- from django_q .tasks import schedule
41
-
42
- DJANGO_Q_AVAILABLE = True
43
- except ImportError :
44
- DJANGO_Q_AVAILABLE = False
45
- Schedule = None
46
- schedule = None
47
-
48
-
49
- @skip ("Django-Q is not available" )
50
- class DjangoQUnavailableTest (TestCase ):
51
- def test_placeholder (self ):
52
- self .skipTest ("Django-Q is not available" )
53
-
54
34
55
35
class TestCalculateSlots (TestCase ):
56
36
def setUp (self ):
@@ -185,12 +165,18 @@ def test_another_staff_member_no_day_off(self):
185
165
self .assertFalse (check_day_off_for_staff (self .staff_member2 , "2023-10-06" ))
186
166
187
167
188
- class TestCreateAndSaveAppointment (BaseTest , TestCase ):
168
+ class TestCreateAndSaveAppointment (BaseTest ):
169
+
189
170
def setUp (self ):
190
- super ().setUp ()
171
+ super ().setUp () # Call the parent class setup
172
+ # Specific setups for this test class
173
+ self .ar = self .create_appt_request_for_sm1 ()
191
174
self .factory = RequestFactory ()
192
175
self .request = self .factory .get ('/' )
193
- self .ar = self .create_appt_request_for_sm1 ()
176
+
177
+ def tearDown (self ):
178
+ Appointment .objects .all ().delete ()
179
+ AppointmentRequest .objects .all ().delete ()
194
180
195
181
def test_create_and_save_appointment (self ):
196
182
client_data = {
@@ -204,8 +190,7 @@ def test_create_and_save_appointment(self):
204
190
'additional_info' : 'Please bring a Zat gun.'
205
191
}
206
192
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 )
193
+ appointment = create_and_save_appointment (self .ar , client_data , appointment_data , self .request )
209
194
210
195
self .assertIsNotNone (appointment )
211
196
self .assertEqual (appointment .client .email , client_data ['email' ])
@@ -214,32 +199,6 @@ def test_create_and_save_appointment(self):
214
199
self .assertEqual (appointment .address , appointment_data ['address' ])
215
200
self .assertEqual (appointment .additional_info , appointment_data ['additional_info' ])
216
201
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
-
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
-
243
202
244
203
def get_mock_reverse (url_name , ** kwargs ):
245
204
"""A mocked version of the reverse function."""
@@ -249,13 +208,6 @@ def get_mock_reverse(url_name, **kwargs):
249
208
250
209
251
210
class ScheduleEmailReminderTest (BaseTest ):
252
- @classmethod
253
- def setUpClass (cls ):
254
- if not DJANGO_Q_AVAILABLE :
255
- import unittest
256
- raise unittest .SkipTest ("Django-Q is not available" )
257
- super ().setUpClass ()
258
-
259
211
def setUp (self ):
260
212
super ().setUp ()
261
213
self .factory = RequestFactory ()
@@ -265,7 +217,6 @@ def setUp(self):
265
217
def tearDown (self ):
266
218
Appointment .objects .all ().delete ()
267
219
AppointmentRequest .objects .all ().delete ()
268
- super ().tearDown ()
269
220
270
221
def test_schedule_email_reminder_cluster_running (self ):
271
222
with patch ('appointment.settings.check_q_cluster' , return_value = True ), \
@@ -282,14 +233,7 @@ def test_schedule_email_reminder_cluster_not_running(self):
282
233
"Django-Q cluster is not running. Email reminder will not be scheduled." )
283
234
284
235
285
- class UpdateAppointmentReminderTest (BaseTest , TestCase ):
286
- @classmethod
287
- def setUpClass (cls ):
288
- if not DJANGO_Q_AVAILABLE :
289
- import unittest
290
- raise unittest .SkipTest ("Django-Q is not available" )
291
- super ().setUpClass ()
292
-
236
+ class UpdateAppointmentReminderTest (BaseTest ):
293
237
def setUp (self ):
294
238
super ().setUp ()
295
239
self .factory = RequestFactory ()
@@ -299,7 +243,6 @@ def setUp(self):
299
243
def tearDown (self ):
300
244
Appointment .objects .all ().delete ()
301
245
AppointmentRequest .objects .all ().delete ()
302
- super ().tearDown ()
303
246
304
247
def test_update_appointment_reminder_date_time_changed (self ):
305
248
appointment = self .create_appt_for_sm1 ()
@@ -324,7 +267,7 @@ def test_update_appointment_reminder_no_change(self):
324
267
mock_cancel_existing_reminder .assert_not_called ()
325
268
mock_schedule_email_reminder .assert_not_called ()
326
269
327
- @patch ('appointment.utils.db_helpers.logger' )
270
+ @patch ('appointment.utils.db_helpers.logger' ) # Adjust the import path as necessary
328
271
def test_reminder_not_scheduled_due_to_user_preference (self , mock_logger ):
329
272
# Scenario where user does not want a reminder
330
273
want_reminder = False
@@ -338,7 +281,7 @@ def test_reminder_not_scheduled_due_to_user_preference(self, mock_logger):
338
281
f"Reminder for appointment { self .appointment .id } is not scheduled per user's preference or past datetime."
339
282
)
340
283
341
- @patch ('appointment.utils.db_helpers.logger' )
284
+ @patch ('appointment.utils.db_helpers.logger' ) # Adjust the import path as necessary
342
285
def test_reminder_not_scheduled_due_to_past_datetime (self , mock_logger ):
343
286
# Scenario where the new datetime is in the past
344
287
want_reminder = True
@@ -428,8 +371,6 @@ def test_staff_change_not_allowed(self, mock_config_first):
428
371
429
372
class CancelExistingReminderTest (BaseTest ):
430
373
def test_cancel_existing_reminder (self ):
431
- if not DJANGO_Q_AVAILABLE :
432
- return
433
374
appointment = self .create_appt_for_sm1 ()
434
375
Schedule .objects .create (func = 'appointment.tasks.send_email_reminder' , name = f"reminder_{ appointment .id_request } " )
435
376
0 commit comments