From 3eee65892b6eafc5a7b0b9164f4395a2a8f0faac Mon Sep 17 00:00:00 2001 From: David Baumgold Date: Tue, 28 Jan 2014 13:40:56 -0500 Subject: [PATCH] Added command to delete split-mongo course --- .../commands/delete_split_course.py | 31 +++++++++++ .../tests/test_delete_split_course.py | 53 +++++++++++++++++++ .../commands/tests/test_migrate_to_split.py | 2 +- 3 files changed, 85 insertions(+), 1 deletion(-) create mode 100644 cms/djangoapps/contentstore/management/commands/delete_split_course.py create mode 100644 cms/djangoapps/contentstore/management/commands/tests/test_delete_split_course.py diff --git a/cms/djangoapps/contentstore/management/commands/delete_split_course.py b/cms/djangoapps/contentstore/management/commands/delete_split_course.py new file mode 100644 index 0000000000..68230bc1b7 --- /dev/null +++ b/cms/djangoapps/contentstore/management/commands/delete_split_course.py @@ -0,0 +1,31 @@ +""" +Django management command to rollback a migration to split. The way to do this +is to delete the course from the split mongo datastore. +""" +from django.core.management.base import BaseCommand, CommandError +from xmodule.modulestore.django import modulestore +from xmodule.modulestore.exceptions import ItemNotFoundError +from xmodule.modulestore.locator import CourseLocator + + +class Command(BaseCommand): + "Delete a course from the split Mongo datastore" + + help = "Delete a course from the split Mongo datastore" + args = "locator" + + def handle(self, *args, **options): + if len(args) < 1: + raise CommandError( + "delete_split_course requires at least one argument (locator)" + ) + + try: + locator = CourseLocator(url=args[0]) + except ValueError: + raise CommandError("Invalid locator string {}".format(args[0])) + + try: + modulestore('split').delete_course(locator.package_id) + except ItemNotFoundError: + raise CommandError("No course found with locator {}".format(locator)) diff --git a/cms/djangoapps/contentstore/management/commands/tests/test_delete_split_course.py b/cms/djangoapps/contentstore/management/commands/tests/test_delete_split_course.py new file mode 100644 index 0000000000..02db42abc5 --- /dev/null +++ b/cms/djangoapps/contentstore/management/commands/tests/test_delete_split_course.py @@ -0,0 +1,53 @@ +""" +Unittests for deleting a split mongo course +""" +import unittest + +from django.core.management import CommandError, call_command +from django.test.utils import override_settings +from contentstore.management.commands.delete_split_course import Command +from contentstore.tests.modulestore_config import TEST_MODULESTORE +from xmodule.modulestore.tests.django_utils import ModuleStoreTestCase +from xmodule.modulestore.tests.persistent_factories import PersistentCourseFactory +from xmodule.modulestore.django import modulestore +from xmodule.modulestore.exceptions import ItemNotFoundError + + +class TestArgParsing(unittest.TestCase): + def setUp(self): + self.command = Command() + + def test_no_args(self): + errstring = "delete_split_course requires at least one argument" + with self.assertRaisesRegexp(CommandError, errstring): + self.command.handle() + + def test_invalid_locator(self): + errstring = "Invalid locator string !?!" + with self.assertRaisesRegexp(CommandError, errstring): + self.command.handle("!?!") + + def test_nonexistant_locator(self): + errstring = "No course found with locator course/branch/name" + with self.assertRaisesRegexp(CommandError, errstring): + self.command.handle("course/branch/name") + + +@override_settings(MODULESTORE=TEST_MODULESTORE) +class TestDeleteSplitCourse(ModuleStoreTestCase): + """ + Unit tests for deleting a split-mongo course from command line + """ + + def setUp(self): + super(TestDeleteSplitCourse, self).setUp() + self.course = PersistentCourseFactory() + + def test_happy_path(self): + locator = self.course.location + call_command( + "delete_split_course", + str(locator), + ) + with self.assertRaises(ItemNotFoundError): + modulestore('split').get_course(locator) 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 2389120616..d8e90bb9c2 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 @@ -1,5 +1,5 @@ """ -Unittests for importing a course via management command +Unittests for migrating a course to split mongo """ import unittest