From 6f5b3fa1bb86fa08f20ce74f0beca4da1d4b5b03 Mon Sep 17 00:00:00 2001 From: Jay Zoldak Date: Wed, 14 Nov 2012 15:12:09 -0500 Subject: [PATCH] Create factory for Xmodule items --- .../contentstore/tests/factories.py | 49 ++++++++++++++++++- cms/djangoapps/contentstore/tests/tests.py | 13 +++++ 2 files changed, 61 insertions(+), 1 deletion(-) diff --git a/cms/djangoapps/contentstore/tests/factories.py b/cms/djangoapps/contentstore/tests/factories.py index bfb7e7fc20..624b792096 100644 --- a/cms/djangoapps/contentstore/tests/factories.py +++ b/cms/djangoapps/contentstore/tests/factories.py @@ -54,4 +54,51 @@ class CourseFactory(XModuleCourseFactory): template = 'i4x://edx/templates/course/Empty' org = 'MITx' number = '999' - display_name = 'Robot Super Course' \ No newline at end of file + display_name = 'Robot Super Course' + +class XModuleItemFactory(Factory): + """ + Factory for XModule items. + """ + + ABSTRACT_FACTORY = True + + @classmethod + def _create(cls, target_class, *args, **kwargs): + + DETACHED_CATEGORIES = ['about', 'static_tab', 'course_info'] + + parent_location = Location(kwargs.get('parent_location')) + template = Location(kwargs.get('template')) + display_name = kwargs.get('display_name') + + store = modulestore('direct') + + parent = store.get_item(parent_location) + dest_location = parent_location._replace(category=template.category, name=uuid4().hex) + + new_item = store.clone_item(template, dest_location) + + # TODO: This needs to be deleted when we have proper storage for static content + new_item.metadata['data_dir'] = parent.metadata['data_dir'] + + # replace the display name with an optional parameter passed in from the caller + if display_name is not None: + new_item.metadata['display_name'] = display_name + + store.update_metadata(new_item.location.url(), new_item.own_metadata) + + if new_item.location.category not in DETACHED_CATEGORIES: + store.update_children(parent_location, parent.definition.get('children', []) + [new_item.location.url()]) + + return new_item + +class Item: + pass + +class ItemFactory(XModuleItemFactory): + FACTORY_FOR = Item + + parent_location = 'i4x://MITx/999/course/Robot_Super_Course' + template = 'i4x://edx/templates/chapter/Empty' + display_name = 'Section One' \ No newline at end of file diff --git a/cms/djangoapps/contentstore/tests/tests.py b/cms/djangoapps/contentstore/tests/tests.py index b0bb6c86a1..72909cbdf1 100644 --- a/cms/djangoapps/contentstore/tests/tests.py +++ b/cms/djangoapps/contentstore/tests/tests.py @@ -323,3 +323,16 @@ class ContentStoreTest(TestCase): def test_edit_unit_full(self): self.check_edit_unit('full') + def test_factory(self): + + from factories import * + + course = CourseFactory.create() + print '\n' + print course + print '\n' + section = ItemFactory.create() + + print '\n' + print section + print '\n'