Advertisement
nvo87

tasks and tests

May 12th, 2021 (edited)
557
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.41 KB | None | 0 0
  1. # tasks.py
  2.  
  3. @celery.task
  4. def notify_lazy_students():
  5.     """ Get the list of lazy students and notify them. Students who were notified recently will be skipped """
  6.  
  7.     date_when_can_send_second_notification = timezone.now() - settings.SKIP_REPETITIVE_LAZY_STUDENT_EMAIL_PERIOD
  8.  
  9.     for student in Customer.objects.get_lazy_students():
  10.         if (student.lazy_student_notification_last_time is None or
  11.                 student.lazy_student_notification_last_time <= date_when_can_send_second_notification):
  12.            
  13.             lazy_student_detected.send(sender=notify_lazy_students, instance=student)
  14.             student.lazy_student_notification_last_time = timezone.now()
  15.             student.save()
  16.  
  17.            
  18. # -----------------
  19. # test_tasks.py
  20.  
  21. class TestPushLazyStudentEmail(ScheduleLessonsFromSubscriptionTestCase):
  22.     """ TestCase for checking celery task is properly being run and signal reaches send email method. """
  23.     def setUp(self):
  24.         super().setUp()
  25.         self.customers_all_types = {
  26.             'lazy': {
  27.                 'obj': create_customer(),
  28.                 'class_dates': [self.tzdatetime(2042, 4, 20, 12, 0),
  29.                                 self.tzdatetime(2042, 4, 21, 12, 0),
  30.                                 self.tzdatetime(2042, 4, 22, 12, 0)]
  31.             },
  32.             'lazy2': {
  33.                 'obj': create_customer(),
  34.                 'class_dates': [self.tzdatetime(2042, 4, 19, 12, 0),
  35.                                 self.tzdatetime(2042, 4, 18, 12, 0),
  36.                                 self.tzdatetime(2042, 4, 17, 12, 0)]
  37.             },
  38.             'not_lazy': {
  39.                 'obj': create_customer(),
  40.                 'class_dates': [self.tzdatetime(2042, 5, 25, 12, 0),
  41.                                 self.tzdatetime(2042, 5, 14, 12, 0),
  42.                                 self.tzdatetime(2042, 5, 13, 12, 0),
  43.                                 self.tzdatetime(2042, 5, 12, 12, 0)]
  44.             },
  45.             'mix': {
  46.                 'obj': create_customer(),
  47.                 'class_dates': [self.tzdatetime(2042, 5, 24, 12, 0),  # future date case
  48.                                 self.tzdatetime(2042, 5, 23, 12, 0),
  49.                                 self.tzdatetime(2042, 5, 4, 12, 0),
  50.                                 self.tzdatetime(2042, 5, 1, 12, 0)]
  51.             },
  52.             'without_first_lesson': {
  53.                 'obj': create_customer(),
  54.                 'class_dates': []  # means subscription is not activated yet
  55.             },
  56.         }
  57.  
  58.     @freeze_time('2042-05-15 15:00')
  59.     @patch('crm.signals.Owl')
  60.     def test_lazy_student_notification(self, owl_send_mock):
  61.         """
  62.        Check that lazy students are detected by task run and are notified with email send.
  63.        """
  64.         expected_number_of_emails = 2
  65.  
  66.         for customer in self.customers_all_types.values():
  67.             subscription = self._buy_subscription(customer=customer['obj'])
  68.             self._schedule_several_lessons(dates=customer['class_dates'], subscription=subscription)
  69.  
  70.         notify_lazy_students.apply()  # run the celery task synchronously
  71.  
  72.         self.assertEqual(owl_send_mock.call_count, expected_number_of_emails)
  73.  
  74.     @patch('crm.signals.Owl')
  75.     def test_skip_repetitive_email(self, owl_send_mock):
  76.         """
  77.        Check that notify_lazy_students will skip the student during the SKIP_REPETITIVE_LAZY_STUDENT_EMAIL_PERIOD
  78.        if he has been already notified.
  79.        """
  80.         expected_number_of_emails = 2
  81.         expected_number_of_emails_when_skip = 0
  82.  
  83.         with freeze_time('2042-05-15 15:00'):
  84.             for customer in self.customers_all_types.values():
  85.                 subscription = self._buy_subscription(customer=customer['obj'])
  86.                 self._schedule_several_lessons(dates=customer['class_dates'], subscription=subscription)
  87.  
  88.             notify_lazy_students.apply()
  89.  
  90.             self.assertEqual(owl_send_mock.call_count, expected_number_of_emails)
  91.  
  92.         with freeze_time('2042-05-16 15:00'):  # skip repetitive emails
  93.             owl_send_mock.call_count = 0
  94.             notify_lazy_students.apply()
  95.  
  96.             self.assertEqual(owl_send_mock.call_count, expected_number_of_emails_when_skip)
  97.  
  98.         with freeze_time('2042-05-20 15:00'):
  99.             owl_send_mock.call_count = 0
  100.             notify_lazy_students.apply()
  101.  
  102.             self.assertEqual(owl_send_mock.call_count, expected_number_of_emails)
  103.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement