Commit faadafee by Adam Committed by GitHub

Merge pull request #12928 from edx/hotfix/2016-07-01

Fix `bulk_change_enrollment` command to emit event
parents c5392cb8 438f10b0
...@@ -83,15 +83,19 @@ class Command(BaseCommand): ...@@ -83,15 +83,19 @@ class Command(BaseCommand):
course_key_str = unicode(course_key) course_key_str = unicode(course_key)
try: try:
with transaction.atomic(): course_enrollments = CourseEnrollment.objects.filter(course_id=course_key, mode=from_mode)
queryset = CourseEnrollment.objects.filter(course_id=course_key, mode=from_mode)
logger.info( logger.info(
'Moving %d users from %s to %s in course %s.', queryset.count(), from_mode, to_mode, course_key_str 'Moving %d users from %s to %s in course %s.',
course_enrollments.count(), from_mode, to_mode, course_key_str
) )
queryset.update(mode=to_mode)
if not commit: if not commit:
logger.info('Dry run, changes have not been saved. Run again with "commit" argument to save changes')
raise Exception('The --commit flag was not given; forcing rollback.') raise Exception('The --commit flag was not given; forcing rollback.')
with transaction.atomic():
# call `change_mode` which will change the mode and also emit tracking event
for enrollment in course_enrollments:
enrollment.change_mode(mode=to_mode)
logger.info('Finished moving users from %s to %s in course %s.', from_mode, to_mode, course_key_str) logger.info('Finished moving users from %s to %s in course %s.', from_mode, to_mode, course_key_str)
except Exception: # pylint: disable=broad-except except Exception: # pylint: disable=broad-except
logger.info('No users moved.') logger.info('No users moved.')
...@@ -2,9 +2,10 @@ ...@@ -2,9 +2,10 @@
import ddt import ddt
from django.core.management import call_command from django.core.management import call_command
from django.core.management.base import CommandError from django.core.management.base import CommandError
from mock import patch, call
from student.tests.factories import UserFactory, CourseModeFactory, CourseEnrollmentFactory from student.tests.factories import UserFactory, CourseModeFactory, CourseEnrollmentFactory
from student.models import CourseEnrollment from student.models import CourseEnrollment, EVENT_NAME_ENROLLMENT_MODE_CHANGED
from xmodule.modulestore.tests.django_utils import SharedModuleStoreTestCase from xmodule.modulestore.tests.django_utils import SharedModuleStoreTestCase
from xmodule.modulestore.tests.factories import CourseFactory from xmodule.modulestore.tests.factories import CourseFactory
...@@ -18,9 +19,10 @@ class BulkChangeEnrollmentTests(SharedModuleStoreTestCase): ...@@ -18,9 +19,10 @@ class BulkChangeEnrollmentTests(SharedModuleStoreTestCase):
self.course = CourseFactory.create() self.course = CourseFactory.create()
self.users = UserFactory.create_batch(5) self.users = UserFactory.create_batch(5)
@patch('student.models.tracker')
@ddt.data(('audit', 'honor'), ('honor', 'audit')) @ddt.data(('audit', 'honor'), ('honor', 'audit'))
@ddt.unpack @ddt.unpack
def test_bulk_convert(self, from_mode, to_mode): def test_bulk_convert(self, from_mode, to_mode, mock_tracker):
"""Verify that enrollments are changed correctly.""" """Verify that enrollments are changed correctly."""
self._enroll_users(from_mode) self._enroll_users(from_mode)
CourseModeFactory(course_id=self.course.id, mode_slug=to_mode) CourseModeFactory(course_id=self.course.id, mode_slug=to_mode)
...@@ -41,6 +43,16 @@ class BulkChangeEnrollmentTests(SharedModuleStoreTestCase): ...@@ -41,6 +43,16 @@ class BulkChangeEnrollmentTests(SharedModuleStoreTestCase):
for user in self.users: for user in self.users:
CourseEnrollment.objects.get(mode=to_mode, course_id=self.course.id, user=user) CourseEnrollment.objects.get(mode=to_mode, course_id=self.course.id, user=user)
# Confirm the analytics event was emitted.
mock_tracker.emit.assert_has_calls( # pylint: disable=maybe-no-member
[
call(
EVENT_NAME_ENROLLMENT_MODE_CHANGED,
{'course_id': unicode(self.course.id), 'user_id': user.id, 'mode': to_mode}
),
]
)
def test_without_commit(self): def test_without_commit(self):
"""Verify that nothing happens when the `commit` flag is not given.""" """Verify that nothing happens when the `commit` flag is not given."""
self._enroll_users('audit') self._enroll_users('audit')
......
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