Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # tasks.py
- @celery.task
- def notify_lazy_students():
- """ Get the list of lazy students and notify them. Students who were notified recently will be skipped """
- date_when_can_send_second_notification = timezone.now() - settings.SKIP_REPETITIVE_LAZY_STUDENT_EMAIL_PERIOD
- for student in Customer.objects.get_lazy_students():
- if (student.lazy_student_notification_last_time is None or
- student.lazy_student_notification_last_time <= date_when_can_send_second_notification):
- lazy_student_detected.send(sender=notify_lazy_students, instance=student)
- student.lazy_student_notification_last_time = timezone.now()
- student.save()
- # -----------------
- # test_tasks.py
- class TestPushLazyStudentEmail(ScheduleLessonsFromSubscriptionTestCase):
- """ TestCase for checking celery task is properly being run and signal reaches send email method. """
- def setUp(self):
- super().setUp()
- self.customers_all_types = {
- 'lazy': {
- 'obj': create_customer(),
- 'class_dates': [self.tzdatetime(2042, 4, 20, 12, 0),
- self.tzdatetime(2042, 4, 21, 12, 0),
- self.tzdatetime(2042, 4, 22, 12, 0)]
- },
- 'lazy2': {
- 'obj': create_customer(),
- 'class_dates': [self.tzdatetime(2042, 4, 19, 12, 0),
- self.tzdatetime(2042, 4, 18, 12, 0),
- self.tzdatetime(2042, 4, 17, 12, 0)]
- },
- 'not_lazy': {
- 'obj': create_customer(),
- 'class_dates': [self.tzdatetime(2042, 5, 25, 12, 0),
- self.tzdatetime(2042, 5, 14, 12, 0),
- self.tzdatetime(2042, 5, 13, 12, 0),
- self.tzdatetime(2042, 5, 12, 12, 0)]
- },
- 'mix': {
- 'obj': create_customer(),
- 'class_dates': [self.tzdatetime(2042, 5, 24, 12, 0), # future date case
- self.tzdatetime(2042, 5, 23, 12, 0),
- self.tzdatetime(2042, 5, 4, 12, 0),
- self.tzdatetime(2042, 5, 1, 12, 0)]
- },
- 'without_first_lesson': {
- 'obj': create_customer(),
- 'class_dates': [] # means subscription is not activated yet
- },
- }
- @freeze_time('2042-05-15 15:00')
- @patch('crm.signals.Owl')
- def test_lazy_student_notification(self, owl_send_mock):
- """
- Check that lazy students are detected by task run and are notified with email send.
- """
- expected_number_of_emails = 2
- for customer in self.customers_all_types.values():
- subscription = self._buy_subscription(customer=customer['obj'])
- self._schedule_several_lessons(dates=customer['class_dates'], subscription=subscription)
- notify_lazy_students.apply() # run the celery task synchronously
- self.assertEqual(owl_send_mock.call_count, expected_number_of_emails)
- @patch('crm.signals.Owl')
- def test_skip_repetitive_email(self, owl_send_mock):
- """
- Check that notify_lazy_students will skip the student during the SKIP_REPETITIVE_LAZY_STUDENT_EMAIL_PERIOD
- if he has been already notified.
- """
- expected_number_of_emails = 2
- expected_number_of_emails_when_skip = 0
- with freeze_time('2042-05-15 15:00'):
- for customer in self.customers_all_types.values():
- subscription = self._buy_subscription(customer=customer['obj'])
- self._schedule_several_lessons(dates=customer['class_dates'], subscription=subscription)
- notify_lazy_students.apply()
- self.assertEqual(owl_send_mock.call_count, expected_number_of_emails)
- with freeze_time('2042-05-16 15:00'): # skip repetitive emails
- owl_send_mock.call_count = 0
- notify_lazy_students.apply()
- self.assertEqual(owl_send_mock.call_count, expected_number_of_emails_when_skip)
- with freeze_time('2042-05-20 15:00'):
- owl_send_mock.call_count = 0
- notify_lazy_students.apply()
- self.assertEqual(owl_send_mock.call_count, expected_number_of_emails)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement