From 1a1635d402ea2140b6a6d4307ff4cb23e08cb57a Mon Sep 17 00:00:00 2001 From: Chris Dodge Date: Wed, 3 Apr 2013 11:28:25 -0400 Subject: [PATCH] Fix tests and extend export/import unit test with draft testing. --- .../contentstore/tests/test_contentstore.py | 29 ++++++++++++++++--- .../xmodule/modulestore/xml_exporter.py | 13 +++++---- .../xmodule/modulestore/xml_importer.py | 10 ++++--- 3 files changed, 38 insertions(+), 14 deletions(-) diff --git a/cms/djangoapps/contentstore/tests/test_contentstore.py b/cms/djangoapps/contentstore/tests/test_contentstore.py index 49a609a879..b2962d717d 100644 --- a/cms/djangoapps/contentstore/tests/test_contentstore.py +++ b/cms/djangoapps/contentstore/tests/test_contentstore.py @@ -11,6 +11,7 @@ import json from fs.osfs import OSFS import copy from json import loads +import traceback from django.contrib.auth.models import User from contentstore.utils import get_modulestore @@ -284,17 +285,27 @@ class ContentStoreToyCourseTest(ModuleStoreTestCase): def test_export_course(self): module_store = modulestore('direct') + draft_store = modulestore('draft') content_store = contentstore() import_from_xml(module_store, 'common/test/data/', ['full']) location = CourseDescriptor.id_to_location('edX/full/6.002_Spring_2012') + # get a vertical (and components in it) to put into 'draft' + vertical = module_store.get_item(Location(['i4x', 'edX', 'full', + 'vertical', 'vertical_66', None]), depth=1) + + draft_store.clone_item(vertical.location, vertical.location) + + for child in vertical.get_children(): + draft_store.clone_item(child.location, child.location) + root_dir = path(mkdtemp_clean()) print 'Exporting to tempdir = {0}'.format(root_dir) # export out to a tempdir - export_to_xml(module_store, content_store, location, root_dir, 'test_export') + export_to_xml(module_store, content_store, location, root_dir, 'test_export', draft_modulestore=draft_store) # check for static tabs self.verify_content_existence(module_store, root_dir, location, 'tabs', 'static_tab', '.html') @@ -328,15 +339,24 @@ class ContentStoreToyCourseTest(ModuleStoreTestCase): delete_course(module_store, content_store, location) # reimport - import_from_xml(module_store, root_dir, ['test_export']) + import_from_xml(module_store, root_dir, ['test_export'], draft_store=draft_store) items = module_store.get_items(Location(['i4x', 'edX', 'full', 'vertical', None])) self.assertGreater(len(items), 0) for descriptor in items: print "Checking {0}....".format(descriptor.location.url()) - resp = self.client.get(reverse('edit_unit', kwargs={'location': descriptor.location.url()})) + non_draft_loc = descriptor.location._replace(revision=None) + resp = self.client.get(reverse('edit_unit', kwargs={'location': non_draft_loc.url()})) self.assertEqual(resp.status_code, 200) + # verify that we have the content in the draft store as well + vertical = draft_store.get_item(Location(['i4x', 'edX', 'full', + 'vertical', 'vertical_66', None]), depth=1) + + self.assertTrue(hasattr(vertical, 'is_draft')) + for child in vertical.get_children(): + self.assertTrue(hasattr(child, 'is_draft')) + shutil.rmtree(root_dir) def test_course_handouts_rewrites(self): @@ -404,7 +424,8 @@ class ContentStoreToyCourseTest(ModuleStoreTestCase): try: export_to_xml(module_store, content_store, location, root_dir, 'test_export') exported = True - except Exception: + except Exception,e: + print 'Exception thrown: {0}'.format(traceback.format_exc()) pass self.assertTrue(exported) diff --git a/common/lib/xmodule/xmodule/modulestore/xml_exporter.py b/common/lib/xmodule/xmodule/modulestore/xml_exporter.py index 7927b2c68c..5930d40cc8 100644 --- a/common/lib/xmodule/xmodule/modulestore/xml_exporter.py +++ b/common/lib/xmodule/xmodule/modulestore/xml_exporter.py @@ -54,13 +54,14 @@ def export_to_xml(modulestore, contentstore, course_location, root_dir, course_d # NOTE: this code assumes that verticals are the top most draftable container # should we change the application, then this assumption will no longer # be valid - draft_items = draft_modulestore.get_items([None, course_location.org, course_location.course, - 'vertical', None, 'draft']) + if draft_modulestore is not None: + draft_items = draft_modulestore.get_items([None, course_location.org, course_location.course, + 'vertical', None, 'draft']) - if len(draft_items)>0: - draft_course_dir = export_fs.makeopendir('drafts') - for draft_item in draft_items: - draft_item.export_to_xml(draft_course_dir) + if len(draft_items)>0: + draft_course_dir = export_fs.makeopendir('drafts') + for draft_item in draft_items: + draft_item.export_to_xml(draft_course_dir) def export_extra_content(export_fs, modulestore, course_location, category_type, dirname, file_suffix=''): diff --git a/common/lib/xmodule/xmodule/modulestore/xml_importer.py b/common/lib/xmodule/xmodule/modulestore/xml_importer.py index e6c4bc8bc9..204e229fdb 100644 --- a/common/lib/xmodule/xmodule/modulestore/xml_importer.py +++ b/common/lib/xmodule/xmodule/modulestore/xml_importer.py @@ -276,6 +276,12 @@ def import_from_xml(store, data_dir, course_dirs=None, import_module(module, store, course_data_path, static_content_store) + # now import any 'draft' items + if draft_store is not None: + import_course_draft(xml_module_store, draft_store, course_data_path, + static_content_store, target_location_namespace if target_location_namespace is not None + else course_location) + finally: # turn back on all write signalling if pseudo_course_id in store.ignore_write_events_on_courses: @@ -283,10 +289,6 @@ def import_from_xml(store, data_dir, course_dirs=None, store.refresh_cached_metadata_inheritance_tree(target_location_namespace if target_location_namespace is not None else course_location) - # now import any 'draft' items - if draft_store is not None: - import_course_draft(xml_module_store, draft_store, course_data_path, static_content_store, target_location_namespace) - return xml_module_store, course_items def import_module(module, store, course_data_path, static_content_store, allow_not_found=False):