Commit 2c80b1b4 by Nimisha Asthagiri Committed by GitHub

Merge pull request #16339 from edx/cale/dry-schedule-tests

Dry schedule tests
parents 764e598f b461ce0c
...@@ -9,6 +9,7 @@ from openedx.core.djangoapps.schedules.utils import PrefixedDebugLoggerMixin ...@@ -9,6 +9,7 @@ from openedx.core.djangoapps.schedules.utils import PrefixedDebugLoggerMixin
class SendEmailBaseCommand(PrefixedDebugLoggerMixin, BaseCommand): class SendEmailBaseCommand(PrefixedDebugLoggerMixin, BaseCommand):
async_send_task = None # define in subclass async_send_task = None # define in subclass
offsets = () # define in subclass
def add_arguments(self, parser): def add_arguments(self, parser):
parser.add_argument( parser.add_argument(
...@@ -37,9 +38,6 @@ class SendEmailBaseCommand(PrefixedDebugLoggerMixin, BaseCommand): ...@@ -37,9 +38,6 @@ class SendEmailBaseCommand(PrefixedDebugLoggerMixin, BaseCommand):
override_recipient_email = options.get('override_recipient_email') override_recipient_email = options.get('override_recipient_email')
self.send_emails(site, current_date, override_recipient_email) self.send_emails(site, current_date, override_recipient_email)
def send_emails(self, *args, **kwargs):
raise NotImplementedError
def enqueue(self, day_offset, site, current_date, override_recipient_email=None): def enqueue(self, day_offset, site, current_date, override_recipient_email=None):
self.async_send_task.enqueue( self.async_send_task.enqueue(
site, site,
...@@ -47,3 +45,7 @@ class SendEmailBaseCommand(PrefixedDebugLoggerMixin, BaseCommand): ...@@ -47,3 +45,7 @@ class SendEmailBaseCommand(PrefixedDebugLoggerMixin, BaseCommand):
day_offset, day_offset,
override_recipient_email, override_recipient_email,
) )
def send_emails(self, *args, **kwargs):
for offset in self.offsets:
self.enqueue(offset, *args, **kwargs)
...@@ -4,11 +4,5 @@ from openedx.core.djangoapps.schedules.tasks import ScheduleCourseUpdate ...@@ -4,11 +4,5 @@ from openedx.core.djangoapps.schedules.tasks import ScheduleCourseUpdate
class Command(SendEmailBaseCommand): class Command(SendEmailBaseCommand):
async_send_task = ScheduleCourseUpdate async_send_task = ScheduleCourseUpdate
log_prefix = 'Course Update'
def __init__(self, *args, **kwargs): offsets = xrange(-7, -77, -7)
super(Command, self).__init__(*args, **kwargs)
self.log_prefix = 'Upgrade Reminder'
def send_emails(self, *args, **kwargs):
for day_offset in xrange(-7, -77, -7):
self.enqueue(day_offset, *args, **kwargs)
...@@ -4,11 +4,5 @@ from openedx.core.djangoapps.schedules.tasks import ScheduleRecurringNudge ...@@ -4,11 +4,5 @@ from openedx.core.djangoapps.schedules.tasks import ScheduleRecurringNudge
class Command(SendEmailBaseCommand): class Command(SendEmailBaseCommand):
async_send_task = ScheduleRecurringNudge async_send_task = ScheduleRecurringNudge
log_prefix = 'Scheduled Nudge'
def __init__(self, *args, **kwargs): offsets = (-3, -10)
super(Command, self).__init__(*args, **kwargs)
self.log_prefix = 'Scheduled Nudge'
def send_emails(self, *args, **kwargs):
for day_offset in (-3, -10):
self.enqueue(day_offset, *args, **kwargs)
...@@ -4,10 +4,5 @@ from openedx.core.djangoapps.schedules.tasks import ScheduleUpgradeReminder ...@@ -4,10 +4,5 @@ from openedx.core.djangoapps.schedules.tasks import ScheduleUpgradeReminder
class Command(SendEmailBaseCommand): class Command(SendEmailBaseCommand):
async_send_task = ScheduleUpgradeReminder async_send_task = ScheduleUpgradeReminder
log_prefix = 'Upgrade Reminder'
def __init__(self, *args, **kwargs): offsets = (2,)
super(Command, self).__init__(*args, **kwargs)
self.log_prefix = 'Upgrade Reminder'
def send_emails(self, *args, **kwargs):
self.enqueue(2, *args, **kwargs)
...@@ -4,7 +4,7 @@ from unittest import skipUnless ...@@ -4,7 +4,7 @@ from unittest import skipUnless
import ddt import ddt
import pytz import pytz
from django.conf import settings from django.conf import settings
from mock import patch from mock import patch, DEFAULT, Mock
from openedx.core.djangoapps.schedules.management.commands import SendEmailBaseCommand from openedx.core.djangoapps.schedules.management.commands import SendEmailBaseCommand
from openedx.core.djangoapps.site_configuration.tests.factories import SiteFactory, SiteConfigurationFactory from openedx.core.djangoapps.site_configuration.tests.factories import SiteFactory, SiteConfigurationFactory
...@@ -29,3 +29,18 @@ class TestSendEmailBaseCommand(CacheIsolationTestCase): ...@@ -29,3 +29,18 @@ class TestSendEmailBaseCommand(CacheIsolationTestCase):
datetime.datetime(2017, 9, 29, tzinfo=pytz.UTC), datetime.datetime(2017, 9, 29, tzinfo=pytz.UTC),
None None
) )
def test_send_emails(self):
with patch.multiple(
self.command,
offsets=(1, 3, 5),
enqueue=DEFAULT,
):
arg = Mock(name='arg')
kwarg = Mock(name='kwarg')
self.command.send_emails(arg, kwarg=kwarg)
self.assertFalse(arg.called)
self.assertFalse(kwarg.called)
for offset in self.command.offsets:
self.command.enqueue.assert_any_call(offset, arg, kwarg=kwarg)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment