From 956a960c636bd5c10936482d8a6cdcfde8ddafe5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Andr=C3=A9s=20Rocha?= Date: Thu, 8 Aug 2013 18:39:57 -0400 Subject: [PATCH] Add command to dump the course_ids available to the LMS --- .../management/commands/dump_course_ids.py | 36 +++++++++++++++ .../courseware/tests/test_commands.py | 45 +++++++++++++++++++ 2 files changed, 81 insertions(+) create mode 100644 lms/djangoapps/courseware/management/commands/dump_course_ids.py create mode 100644 lms/djangoapps/courseware/tests/test_commands.py diff --git a/lms/djangoapps/courseware/management/commands/dump_course_ids.py b/lms/djangoapps/courseware/management/commands/dump_course_ids.py new file mode 100644 index 0000000000..749b64f6a7 --- /dev/null +++ b/lms/djangoapps/courseware/management/commands/dump_course_ids.py @@ -0,0 +1,36 @@ +# pylint: disable=missing-docstring + +from optparse import make_option +from textwrap import dedent + +from django.core.management.base import BaseCommand, CommandError + +from xmodule.modulestore.django import modulestore + + +class Command(BaseCommand): + """ + Simple command to dump the course_ids available to the lms. + """ + help = dedent(__doc__).strip() + option_list = BaseCommand.option_list + ( + make_option('--modulestore', + action='store', + default='default', + help='Name of the modulestore to use'), + ) + + def handle(self, *args, **options): + output = [] + + try: + name = options['modulestore'] + store = modulestore(name) + except KeyError: + raise CommandError("Unknown modulestore {}".format(name)) + + for course in store.get_courses(): + course_id = course.location.course_id + output.append(course_id) + + return '\n'.join(output) + '\n' diff --git a/lms/djangoapps/courseware/tests/test_commands.py b/lms/djangoapps/courseware/tests/test_commands.py new file mode 100644 index 0000000000..7ae7abe483 --- /dev/null +++ b/lms/djangoapps/courseware/tests/test_commands.py @@ -0,0 +1,45 @@ +"""Tests for Django management commands""" + +from StringIO import StringIO + +from django.core.management import call_command +from django.test.utils import override_settings + +from courseware.tests.tests import TEST_DATA_MONGO_MODULESTORE + +from xmodule.modulestore.django import modulestore +from xmodule.modulestore.tests.django_utils import ModuleStoreTestCase +from xmodule.modulestore.xml_importer import import_from_xml + +DATA_DIR = 'common/test/data/' + + +@override_settings(MODULESTORE=TEST_DATA_MONGO_MODULESTORE) +class CommandTestCase(ModuleStoreTestCase): + """Parent class with helpers for testing management commands""" + + def load_courses(self): + """Load test courses and return list of ids""" + store = modulestore() + import_from_xml(store, DATA_DIR, ['toy', 'simple']) + return [course.id for course in store.get_courses()] + + def call_command(self, name, *args, **kwargs): + """Call management command and return output""" + out = StringIO() # To Capture the output of the command + call_command(name, *args, stdout=out, **kwargs) + out.seek(0) + return out.read() + + +class CommandsTestCase(CommandTestCase): + """Test case for management commands""" + + def setUp(self): + self.loaded_courses = self.load_courses() + + def test_dump_course_ids(self): + kwargs = {'modulestore': 'default'} + output = self.call_command('dump_course_ids', **kwargs) + dumped_courses = output.strip().split('\n') + self.assertEqual(self.loaded_courses, dumped_courses)