From 1ad8cd01f529a6808f7f01d0bbe2d9337ca43e9b Mon Sep 17 00:00:00 2001 From: Nimisha Asthagiri Date: Thu, 26 Oct 2017 09:04:58 -0400 Subject: [PATCH] Move test_multiple_enrollments into the schedules management command base test class --- .../tests/test_send_recurring_nudge.py | 23 -------------- .../tests/test_send_upgrade_reminder.py | 30 ------------------- .../management/commands/tests/tools.py | 25 ++++++++++++++++ 3 files changed, 25 insertions(+), 53 deletions(-) diff --git a/openedx/core/djangoapps/schedules/management/commands/tests/test_send_recurring_nudge.py b/openedx/core/djangoapps/schedules/management/commands/tests/test_send_recurring_nudge.py index e1c033ad48..50398a0287 100644 --- a/openedx/core/djangoapps/schedules/management/commands/tests/test_send_recurring_nudge.py +++ b/openedx/core/djangoapps/schedules/management/commands/tests/test_send_recurring_nudge.py @@ -57,29 +57,6 @@ class TestSendRecurringNudge(ScheduleBaseEmailTestBase): enqueue_config = 'enqueue_recurring_nudge' expected_offsets = (-3, -10) - @patch.object(tasks, 'ace') - @patch.object(tested_task, 'async_send_task') - def test_multiple_enrollments(self, mock_schedule_send, mock_ace): - user = UserFactory.create() - schedules = [ - ScheduleFactory.create( - start=datetime.datetime(2017, 8, 3, 19, 44, 30, tzinfo=pytz.UTC), - enrollment__user=user, - enrollment__course__id=CourseLocator('edX', 'toy', 'Course{}'.format(course_num)) - ) - for course_num in (1, 2, 3) - ] - - test_datetime = datetime.datetime(2017, 8, 3, 19, 44, 30, tzinfo=pytz.UTC) - test_datetime_str = serialize(test_datetime) - with self.assertNumQueries(NUM_QUERIES_WITH_MATCHES + NUM_QUERIES_NO_ORG_LIST, table_blacklist=WAFFLE_TABLES): - self.tested_task.apply(kwargs=dict( - site_id=self.site_config.site.id, target_day_str=test_datetime_str, day_offset=-3, - bin_num=user.id % resolvers.RECURRING_NUDGE_NUM_BINS, - )) - self.assertEqual(mock_schedule_send.apply_async.call_count, 1) - self.assertFalse(mock_ace.send.called) - @ddt.data(*itertools.product((1, 10, 100), (-3, -10))) @ddt.unpack def test_templates(self, message_count, day): diff --git a/openedx/core/djangoapps/schedules/management/commands/tests/test_send_upgrade_reminder.py b/openedx/core/djangoapps/schedules/management/commands/tests/test_send_upgrade_reminder.py index 2e6ab215f3..b73894f64d 100644 --- a/openedx/core/djangoapps/schedules/management/commands/tests/test_send_upgrade_reminder.py +++ b/openedx/core/djangoapps/schedules/management/commands/tests/test_send_upgrade_reminder.py @@ -75,36 +75,6 @@ class TestUpgradeReminder(ScheduleBaseEmailTestBase): expiration_datetime=datetime.datetime.now(pytz.UTC) + datetime.timedelta(days=30), ) - @patch.object(tasks, 'ace') - @patch.object(tested_task, 'async_send_task') - def test_multiple_enrollments(self, mock_schedule_send, mock_ace): - user = UserFactory.create() - schedules = [ - ScheduleFactory.create( - upgrade_deadline=datetime.datetime(2017, 8, 3, 19, 44, 30, tzinfo=pytz.UTC), - enrollment__user=user, - enrollment__course__self_paced=True, - enrollment__course__id=CourseLocator('edX', 'toy', 'Course{}'.format(course_num)) - ) - for course_num in (1, 2, 3) - ] - - course_switch_queries = len(set(s.enrollment.course.id for s in schedules)) - org_switch_queries = len(set(s.enrollment.course.id.org for s in schedules)) - - test_datetime = datetime.datetime(2017, 8, 3, 19, 44, 30, tzinfo=pytz.UTC) - test_datetime_str = serialize(test_datetime) - expected_query_count = ( - NUM_QUERIES_FIRST_MATCH + course_switch_queries + org_switch_queries + NUM_QUERIES_NO_ORG_LIST - ) - with self.assertNumQueries(expected_query_count, table_blacklist=WAFFLE_TABLES): - self.tested_task.apply(kwargs=dict( - site_id=self.site_config.site.id, target_day_str=test_datetime_str, day_offset=2, - bin_num=self._calculate_bin_for_user(user), - )) - self.assertEqual(mock_schedule_send.apply_async.call_count, 1) - self.assertFalse(mock_ace.send.called) - @ddt.data(1, 10, 100) def test_templates(self, message_count): now = datetime.datetime.now(pytz.UTC) diff --git a/openedx/core/djangoapps/schedules/management/commands/tests/tools.py b/openedx/core/djangoapps/schedules/management/commands/tests/tools.py index a8b1d31879..8d1f6b9be1 100644 --- a/openedx/core/djangoapps/schedules/management/commands/tests/tools.py +++ b/openedx/core/djangoapps/schedules/management/commands/tests/tools.py @@ -291,3 +291,28 @@ class ScheduleBaseEmailTestBase(SharedModuleStoreTestCase): self.assertFalse(mock_schedule_send.apply_async.called) else: self.assertTrue(mock_schedule_send.apply_async.called) + + @patch.object(tasks, 'ace') + def test_multiple_enrollments(self, mock_ace): + user = UserFactory.create() + current_day, offset, target_day = self._get_dates() + num_courses = 3 + for course_index in range(num_courses): + ScheduleFactory.create( + start=target_day, + upgrade_deadline=target_day, + enrollment__course__self_paced=True, + enrollment__user=user, + enrollment__course__id=CourseKey.from_string('edX/toy/course{}'.format(course_index)) + ) + + course_queries = num_courses if self.has_course_queries else 0 + expected_query_count = NUM_QUERIES_FIRST_MATCH + course_queries + NUM_QUERIES_NO_ORG_LIST + with self.assertNumQueries(expected_query_count, table_blacklist=WAFFLE_TABLES): + with patch.object(self.tested_task, 'async_send_task') as mock_schedule_send: + self.tested_task.apply(kwargs=dict( + site_id=self.site_config.site.id, target_day_str=serialize(target_day), day_offset=offset, + bin_num=self._calculate_bin_for_user(user), + )) + self.assertEqual(mock_schedule_send.apply_async.call_count, 1) + self.assertFalse(mock_ace.send.called)