diff --git a/cms/djangoapps/contentstore/management/commands/delete_course.py b/cms/djangoapps/contentstore/management/commands/delete_course.py index ec42635cc2..8527b32a11 100644 --- a/cms/djangoapps/contentstore/management/commands/delete_course.py +++ b/cms/djangoapps/contentstore/management/commands/delete_course.py @@ -24,32 +24,19 @@ class Command(BaseCommand): """ help = '''Delete a MongoDB backed course''' - def handle(self, *args, **options): - if len(args) == 0: - raise CommandError("Arguments missing: 'org/number/run commit'") + def add_arguments(self, parser): + parser.add_argument('course_key', help="ID of the course to delete.") - if len(args) == 1: - if args[0] == 'commit': - raise CommandError("Delete_course requires a course_key argument.") - else: - raise CommandError("Delete_course requires a commit argument at the end") - elif len(args) == 2: - try: - course_key = CourseKey.from_string(args[0]) - except InvalidKeyError: - try: - course_key = SlashSeparatedCourseKey.from_deprecated_string(args[0]) - except InvalidKeyError: - raise CommandError("Invalid course_key: '%s'. Proper syntax: 'org/number/run commit' " % args[0]) - if args[1] != 'commit': - raise CommandError("Delete_course requires a commit argument at the end") - elif len(args) > 2: - raise CommandError("Too many arguments! Expected ") + def handle(self, *args, **options): + try: + course_key = CourseKey.from_string(options['course_key']) + except InvalidKeyError: + raise CommandError("Invalid course_key: '%s'. Proper syntax: 'org/number/run' " % options['course_key']) if not modulestore().get_course(course_key): - raise CommandError("Course with '%s' key not found." % args[0]) + raise CommandError("Course with '%s' key not found." % options['course_key']) - print 'Actually going to delete the %s course from DB....' % args[0] + print 'Going to delete the %s course from DB....' % options['course_key'] if query_yes_no("Deleting course {0}. Confirm?".format(course_key), default="no"): if query_yes_no("Are you sure. This action cannot be undone!", default="no"): delete_course_and_groups(course_key, ModuleStoreEnum.UserID.mgmt_command) diff --git a/cms/djangoapps/contentstore/management/commands/tests/test_delete_course.py b/cms/djangoapps/contentstore/management/commands/tests/test_delete_course.py index 21b4bbb37a..0ecc34c5aa 100644 --- a/cms/djangoapps/contentstore/management/commands/tests/test_delete_course.py +++ b/cms/djangoapps/contentstore/management/commands/tests/test_delete_course.py @@ -6,72 +6,12 @@ import unittest import mock from opaque_keys.edx.locations import SlashSeparatedCourseKey -from django.core.management import CommandError -from contentstore.management.commands.delete_course import Command +from django.core.management import call_command, CommandError from contentstore.tests.utils import CourseTestCase from xmodule.modulestore.tests.factories import CourseFactory from xmodule.modulestore.django import modulestore -class TestArgParsing(unittest.TestCase): - """ - Tests for parsing arguments for the 'delete_course' management command - """ - - def setUp(self): - super(TestArgParsing, self).setUp() - - self.command = Command() - - def test_no_args(self): - """ - Testing 'delete_course' command with no arguments provided - """ - errstring = "Arguments missing: 'org/number/run commit'" - with self.assertRaisesRegexp(CommandError, errstring): - self.command.handle() - - def test_no_course_key(self): - """ - Testing 'delete_course' command with no course key provided - """ - errstring = "Delete_course requires a course_key argument." - with self.assertRaisesRegexp(CommandError, errstring): - self.command.handle("commit") - - def test_commit_argument(self): - """ - Testing 'delete_course' command without 'commit' argument - """ - errstring = "Delete_course requires a commit argument at the end" - with self.assertRaisesRegexp(CommandError, errstring): - self.command.handle("TestX/TS01/run") - - def test_invalid_course_key(self): - """ - Testing 'delete_course' command with an invalid course key argument - """ - errstring = "Invalid course_key: 'TestX/TS01'. Proper syntax: 'org/number/run commit' " - with self.assertRaisesRegexp(CommandError, errstring): - self.command.handle("TestX/TS01", "commit") - - def test_missing_commit_argument(self): - """ - Testing 'delete_course' command with misspelled 'commit' argument - """ - errstring = "Delete_course requires a commit argument at the end" - with self.assertRaisesRegexp(CommandError, errstring): - self.command.handle("TestX/TS01/run", "comit") - - def test_too_many_arguments(self): - """ - Testing 'delete_course' command with more than 2 arguments - """ - errstring = "Too many arguments! Expected " - with self.assertRaisesRegexp(CommandError, errstring): - self.command.handle("TestX/TS01/run", "commit", "invalid") - - class DeleteCourseTest(CourseTestCase): """ Test for course deleting functionality of the 'delete_course' command @@ -82,8 +22,6 @@ class DeleteCourseTest(CourseTestCase): def setUp(self): super(DeleteCourseTest, self).setUp() - self.command = Command() - org = 'TestX' course_number = 'TS01' course_run = '2015_Q1' @@ -99,9 +37,9 @@ class DeleteCourseTest(CourseTestCase): """ Test for when a non-existing course key is entered """ - errstring = "Course with 'TestX/TS01/2015_Q7' key not found." + errstring = "Invalid course_key: 'TestX/TS01/2015_Q7'. Proper syntax: 'org/number/run' " with self.assertRaisesRegexp(CommandError, errstring): - self.command.handle('TestX/TS01/2015_Q7', "commit") + call_command('delete_course','TestX/TS01/2015_Q7') def test_course_deleted(self): """ @@ -113,5 +51,5 @@ class DeleteCourseTest(CourseTestCase): with mock.patch(self.YESNO_PATCH_LOCATION) as patched_yes_no: patched_yes_no.return_value = True - self.command.handle('TestX/TS01/2015_Q1', "commit") + call_command('delete_course','TestX/TS01/2015_Q1', "commit") self.assertIsNone(modulestore().get_course(SlashSeparatedCourseKey("TestX", "TS01", "2015_Q1")))