Upgrade mock BOM-596 (#21717)

This commit is contained in:
Jeremy Bowman
2019-09-24 10:14:17 -04:00
committed by GitHub
parent 4c9af183cb
commit 553d35e53e
24 changed files with 67 additions and 116 deletions

View File

@@ -4,7 +4,6 @@ Tests for cohorts
# pylint: disable=no-member
from __future__ import absolute_import
import before_after
import ddt
from django.contrib.auth.models import AnonymousUser, User
from django.db import IntegrityError
@@ -691,45 +690,6 @@ class TestCohorts(ModuleStoreTestCase):
lambda: cohorts.add_user_to_cohort(first_cohort, "non_existent_username")
)
@patch("openedx.core.djangoapps.course_groups.cohorts.tracker")
def add_user_to_cohorts_race_condition(self, mock_tracker):
"""
Makes use of before_after to force a race condition, in order to
confirm handling of such conditions is done correctly.
"""
course_user = UserFactory(username="Username", email="a@b.com")
course = modulestore().get_course(self.toy_course_key)
CourseEnrollment.enroll(course_user, self.toy_course_key)
first_cohort = CohortFactory(course_id=course.id, name="FirstCohort")
second_cohort = CohortFactory(course_id=course.id, name="SecondCohort")
# This before_after contextmanager allows for reliable reproduction of a race condition.
# It will break before the first save() call creates an entry, and then run add_user_to_cohort again.
# Because this second call will write before control is returned, the first call will be writing stale data.
# This test confirms that the first add_user_to_cohort call can handle this stale read condition properly.
# Proper handling is defined as treating calls as sequential, with write time deciding the order.
with before_after.before_after(
'django.db.models.Model.save',
after_ftn=cohorts.add_user_to_cohort(second_cohort, course_user.username),
autospec=True
):
# This method will read, then break, then try to write stale data.
# It should fail at that, then retry with refreshed data
cohorts.add_user_to_cohort(first_cohort, course_user.username)
mock_tracker.emit.assert_any_call(
"edx.cohort.user_add_requested",
{
"user_id": course_user.id,
"cohort_id": first_cohort.id,
"cohort_name": first_cohort.name,
"previous_cohort_id": second_cohort.id,
"previous_cohort_name": second_cohort.name,
}
)
# Note that the following get() will fail with MultipleObjectsReturned if race condition is not handled.
self.assertEqual(first_cohort.users.get(), course_user)
def test_set_cohorted_with_invalid_data_type(self):
"""
Test that cohorts.set_course_cohorted raises exception if argument is not a boolean.

View File

@@ -145,7 +145,7 @@ class AwardProgramCertificatesTestCase(CatalogIntegrationMixin, CredentialsApiCo
programs.
"""
tasks.award_program_certificates.delay(self.student.username).get()
mock_get_completed_programs.assert_called(self.site, self.student)
mock_get_completed_programs.assert_any_call(self.site, self.student)
@ddt.data(
([1], [2, 3]),

View File

@@ -343,7 +343,6 @@ class BackpopulateProgramCredentialsTests(CatalogIntegrationMixin, CredentialsAp
call_command('backpopulate_program_credentials', commit=True)
mock_task.assert_called_once_with(self.alice.username)
mock_task.assert_not_called(self.bob.username)
@mock.patch(COMMAND_MODULE + '.logger.exception')
def test_handle_enqueue_failure(self, mock_log, mock_task, mock_get_programs):

View File

@@ -8,7 +8,7 @@ from unittest import skipUnless
import ddt
from django.conf import settings
from edx_ace.utils.date import serialize
from mock import _is_started, patch
from mock import patch
from six.moves import range
from openedx.core.djangoapps.schedules import resolvers, tasks
@@ -60,7 +60,7 @@ class TestSendCourseUpdate(ScheduleUpsellTestMixin, ScheduleSendEmailTestMixin,
Stops the patcher for the get_week_highlights method
if the patch is still in progress.
"""
if _is_started(self.highlights_patcher):
if self.highlights_patcher is not None:
self.highlights_patcher.stop()
@ddt.data(
@@ -75,6 +75,7 @@ class TestSendCourseUpdate(ScheduleUpsellTestMixin, ScheduleSendEmailTestMixin,
@patch('openedx.core.djangoapps.schedules.signals.get_current_site')
def test_with_course_data(self, mock_get_current_site):
self.highlights_patcher.stop()
self.highlights_patcher = None
mock_get_current_site.return_value = self.site_config.site
course = CourseFactory(highlights_enabled_for_messaging=True, self_paced=True)

View File

@@ -200,8 +200,9 @@ class TestDeactivateLogout(RetirementTestCase):
def build_post(self, password):
return {'password': password}
@mock.patch('openedx.core.djangolib.oauth2_retirement_utils')
def test_user_can_deactivate_self(self, retirement_utils_mock):
@mock.patch('openedx.core.djangoapps.user_api.accounts.views.retire_dot_oauth2_models')
@mock.patch('openedx.core.djangoapps.user_api.accounts.views.retire_dop_oauth2_models')
def test_user_can_deactivate_self(self, mock_retire_dop, mock_retire_dot):
"""
Verify a user calling the deactivation endpoint logs out the user, deletes all their SSO tokens,
and creates a user retirement row.
@@ -218,8 +219,8 @@ class TestDeactivateLogout(RetirementTestCase):
self.assertEqual(list(Registration.objects.filter(user=self.test_user)), [])
self.assertEqual(len(UserRetirementStatus.objects.filter(user_id=self.test_user.id)), 1)
# these retirement utils are tested elsewhere; just make sure we called them
retirement_utils_mock.retire_dop_oauth2_models.assertCalledWith(self.test_user)
retirement_utils_mock.retire_dot_oauth2_models.assertCalledWith(self.test_user)
mock_retire_dop.assert_called_with(self.test_user)
mock_retire_dot.assert_called_with(self.test_user)
# make sure the user cannot log in
self.assertFalse(self.client.login(username=self.test_user.username, password=self.test_password))
# make sure that an email has been sent

View File

@@ -364,8 +364,6 @@ class TestEnterpriseApi(EnterpriseServiceMockMixin, CacheIsolationTestCase):
self.check_data_sharing_consent(consent_required=True, consent_url=consent_url)
mock_get_consent_url.assert_called_once()
mock_enterprise_enabled.assert_called_once()
mock_consent_necessary.assert_called_once()
@httpretty.activate
@mock.patch('openedx.features.enterprise_support.api.enterprise_customer_uuid_for_request')

View File

@@ -121,7 +121,7 @@ class ScorableCompletionHandlerTestCase(CompletionSetUpMixin, TestCase):
self.assertFalse(completion.exists())
def test_signal_calls_handler(self):
with patch('completion.handlers.scorable_block_completion') as mock_handler:
with patch('completion.handlers.BlockCompletion.objects.submit_completion') as mock_handler:
grades_signals.PROBLEM_WEIGHTED_SCORE_CHANGED.send_robust(
sender=self,
user_id=self.user.id,