3
3
4
4
import datetime
5
5
from unittest import skip
6
- from unittest .mock import ANY , MagicMock , Mock , PropertyMock , patch
6
+ from unittest .mock import MagicMock , PropertyMock , patch
7
7
8
8
from django .apps import apps
9
9
from django .conf import settings
16
16
17
17
from appointment .logger_config import get_logger
18
18
from appointment .models import Config , DayOff , PaymentInfo
19
+ from appointment .settings import check_q_cluster
19
20
from appointment .tests .base .base_test import BaseTest
20
21
from appointment .tests .mixins .base_mixin import ConfigMixin
21
22
from appointment .utils .db_helpers import (
@@ -251,14 +252,14 @@ def get_mock_reverse(url_name, **kwargs):
251
252
return reverse (url_name , ** kwargs )
252
253
253
254
254
- # Mock the django_q module
255
- mock_django_q = Mock ()
256
- mock_django_q .Schedule = Mock ()
257
- mock_django_q .Schedule .ONCE = 'O'
258
-
259
-
260
- @patch .dict ('sys.modules' , {'django_q' : mock_django_q })
261
255
class ScheduleEmailReminderTest (BaseTest ):
256
+ @classmethod
257
+ def setUpClass (cls ):
258
+ if not DJANGO_Q_AVAILABLE :
259
+ import unittest
260
+ raise unittest .SkipTest ("Django-Q is not available" )
261
+ super ().setUpClass ()
262
+
262
263
def setUp (self ):
263
264
super ().setUp ()
264
265
self .factory = RequestFactory ()
@@ -270,36 +271,19 @@ def tearDown(self):
270
271
AppointmentRequest .objects .all ().delete ()
271
272
super ().tearDown ()
272
273
273
- @patch ('appointment.utils.db_helpers.DJANGO_Q_AVAILABLE' , True )
274
- @patch ('appointment.utils.db_helpers.schedule' )
275
- @patch ('appointment.utils.db_helpers.logger' )
276
- @patch ('appointment.utils.db_helpers.get_absolute_url_' )
277
- @patch ('appointment.utils.db_helpers.reverse' )
278
- def test_schedule_email_reminder_django_q_available (self , mock_reverse , mock_get_absolute_url , mock_logger ,
279
- mock_schedule ):
280
- mock_reverse .return_value = '/reschedule'
281
- mock_get_absolute_url .return_value = "https://test.com/reschedule"
282
-
283
- schedule_email_reminder (self .appointment , self .request )
284
-
285
- mock_logger .info .assert_called_once ()
286
- mock_schedule .assert_called_once_with (
287
- 'appointment.tasks.send_email_reminder' ,
288
- to_email = self .appointment .client .email ,
289
- name = f"reminder_{ self .appointment .id_request } " ,
290
- first_name = self .appointment .client .first_name ,
291
- reschedule_link = "https://test.com/reschedule" ,
292
- appointment_id = self .appointment .id ,
293
- schedule_type = 'O' ,
294
- next_run = ANY
295
- )
296
-
297
- @patch ('appointment.utils.db_helpers.DJANGO_Q_AVAILABLE' , False )
298
- @patch ('appointment.utils.db_helpers.logger' )
299
- def test_schedule_email_reminder_django_q_not_available (self , mock_logger ):
300
- schedule_email_reminder (self .appointment , self .request )
301
- mock_logger .warning .assert_called_once_with (
302
- "Django-Q is not available. Email reminder will not be scheduled." )
274
+ def test_schedule_email_reminder_cluster_running (self ):
275
+ with patch ('appointment.settings.check_q_cluster' , return_value = True ), \
276
+ patch ('appointment.utils.db_helpers.schedule' ) as mock_schedule :
277
+ schedule_email_reminder (self .appointment , self .request )
278
+ mock_schedule .assert_called_once ()
279
+ # Further assertions can be made here based on the arguments passed to schedule
280
+
281
+ # def test_schedule_email_reminder_cluster_not_running(self):
282
+ # with patch('appointment.settings.check_q_cluster', return_value=False), \
283
+ # patch('appointment.utils.db_helpers.logger') as mock_logger:
284
+ # schedule_email_reminder(self.appointment, self.request)
285
+ # mock_logger.warning.assert_called_with(
286
+ # "Django-Q cluster is not running. Email reminder will not be scheduled.")
303
287
304
288
305
289
class UpdateAppointmentReminderTest (BaseTest , TestCase ):
0 commit comments