From c3b8d52dcd7a5883864662a15543e4a9ada94d2e Mon Sep 17 00:00:00 2001 From: David Baumgold Date: Tue, 28 Jan 2014 10:13:07 -0500 Subject: [PATCH] move user-finding logic out of command --- .../management/commands/migrate_to_split.py | 35 ++++++++++--------- .../commands/tests/test_migrate_to_split.py | 18 +++++++--- 2 files changed, 32 insertions(+), 21 deletions(-) diff --git a/cms/djangoapps/contentstore/management/commands/migrate_to_split.py b/cms/djangoapps/contentstore/management/commands/migrate_to_split.py index 87197886e2..283193dd0f 100644 --- a/cms/djangoapps/contentstore/management/commands/migrate_to_split.py +++ b/cms/djangoapps/contentstore/management/commands/migrate_to_split.py @@ -11,6 +11,21 @@ from xmodule.modulestore import InvalidLocationError from xmodule.modulestore.django import loc_mapper +def user_from_str(s): + """ + Return a user identified by the given string. The string could be an email + address, or a stringified integer corresponding to the ID of the user in + the database. If no user could be found, a User.DoesNotExist exception + will be raised. + """ + try: + user_id = int(s) + except ValueError: + return User.objects.get(email=s) + else: + return User.objects.get(id=user_id) + + class Command(BaseCommand): help = "Migrate a course from old-Mongo to split-Mongo" args = "location email " @@ -32,24 +47,10 @@ class Command(BaseCommand): except InvalidLocationError: raise CommandError("Invalid location string {}".format(args[0])) - user_id = None - email = None try: - user_id = int(args[1]) - except ValueError: - email = args[1] - if user_id: - try: - user = User.objects.get(pk=user_id) - except User.DoesNotExist: - raise CommandError("No user exists with ID {}".format(user_id)) - else: - try: - user = User.objects.get(email=email) - except User.DoesNotExist: - raise CommandError("No user exists with email {}".format(email)) - - assert user, "User doesn't exist! That shouldn't happen..." + user = user_from_str(args[1]) + except User.DoesNotExist: + raise CommandError("No user found identified by {}".format(args[1])) try: locator_string = args[2] diff --git a/cms/djangoapps/contentstore/management/commands/tests/test_migrate_to_split.py b/cms/djangoapps/contentstore/management/commands/tests/test_migrate_to_split.py index 7839d8c2e9..2389120616 100644 --- a/cms/djangoapps/contentstore/management/commands/tests/test_migrate_to_split.py +++ b/cms/djangoapps/contentstore/management/commands/tests/test_migrate_to_split.py @@ -29,12 +29,12 @@ class TestArgParsing(unittest.TestCase): self.command.handle("foo", "bar") def test_nonexistant_user_id(self): - errstring = "No user exists with ID 99" + errstring = "No user found identified by 99" with self.assertRaisesRegexp(CommandError, errstring): self.command.handle("i4x://org/course/category/name", "99") def test_nonexistant_user_email(self): - errstring = "No user exists with email fake@example.com" + errstring = "No user found identified by fake@example.com" with self.assertRaisesRegexp(CommandError, errstring): self.command.handle("i4x://org/course/category/name", "fake@example.com") @@ -53,11 +53,21 @@ class TestMigrateToSplit(ModuleStoreTestCase): self.user = User.objects.create_user(uname, email, password) self.course = CourseFactory() - def test_happy_path(self): + def test_happy_path_email(self): call_command( "migrate_to_split", str(self.course.location), - self.user.email, + str(self.user.email), + ) + locator = loc_mapper().translate_location(self.course.id, self.course.location) + course_from_split = modulestore('split').get_course(locator) + self.assertIsNotNone(course_from_split) + + def test_happy_path_user_id(self): + call_command( + "migrate_to_split", + str(self.course.location), + str(self.user.id), ) locator = loc_mapper().translate_location(self.course.id, self.course.location) course_from_split = modulestore('split').get_course(locator)