From 41f5159b37b7e8ee5100c795ae4079a5a3b789f7 Mon Sep 17 00:00:00 2001 From: Don Mitchell Date: Tue, 4 Mar 2014 12:55:19 -0500 Subject: [PATCH] Test special code paths in create_item --- .../lib/xmodule/xmodule/modulestore/mixed.py | 6 +-- .../tests/test_mixed_modulestore.py | 45 +++++++++++++++---- 2 files changed, 38 insertions(+), 13 deletions(-) diff --git a/common/lib/xmodule/xmodule/modulestore/mixed.py b/common/lib/xmodule/xmodule/modulestore/mixed.py index 4b675fc146..5152ea9231 100644 --- a/common/lib/xmodule/xmodule/modulestore/mixed.py +++ b/common/lib/xmodule/xmodule/modulestore/mixed.py @@ -353,7 +353,8 @@ class MixedModuleStore(ModuleStoreWriteBase): elif isinstance(store, SplitMongoModuleStore): if isinstance(course_or_parent_loc, basestring): # course_id course_or_parent_loc = loc_mapper().translate_location_to_course_locator( - course_or_parent_loc, None + # hardcode draft version until we figure out how we're handling branches from app + course_or_parent_loc, None, published=False ) elif not isinstance(course_or_parent_loc, CourseLocator): raise ValueError(u"Cannot create a child of {} in split. Wrong repr.".format(course_or_parent_loc)) @@ -364,9 +365,6 @@ class MixedModuleStore(ModuleStoreWriteBase): fields.update(kwargs.pop('definition_data', {})) kwargs['fields'] = fields - if not kwargs.get('block_id', False) and getattr(location, 'name', False): - kwargs['block_id'] = getattr(location, 'name') - xblock = store.create_item(course_or_parent_loc, category, user_id, **kwargs) else: raise NotImplementedError(u"Cannot create an item on store %s" % store) diff --git a/common/lib/xmodule/xmodule/modulestore/tests/test_mixed_modulestore.py b/common/lib/xmodule/xmodule/modulestore/tests/test_mixed_modulestore.py index dac4870f51..d2e31cb831 100644 --- a/common/lib/xmodule/xmodule/modulestore/tests/test_mixed_modulestore.py +++ b/common/lib/xmodule/xmodule/modulestore/tests/test_mixed_modulestore.py @@ -141,15 +141,6 @@ class TestMixedModuleStore(LocMapperSetupSansDjango): self.store = MixedModuleStore(**self.options) self.addCleanup(self.store.close_all_connections) - def generate_location(course_id): - """ - Generate the locations for the given ids - """ - course_dict = Location.parse_course_id(course_id) - course_dict['tag'] = 'i4x' - course_dict['category'] = 'course' - return Location(course_dict) - self.course_locations = { course_id: generate_location(course_id) for course_id in [self.MONGO_COURSEID, self.XML_COURSEID1, self.XML_COURSEID2] @@ -351,6 +342,32 @@ class TestMixedModuleStore(LocMapperSetupSansDjango): else: self.assertEqual(found_orphans, [unicode(orphan.location)]) + @ddt.data('split') + def test_create_item_from_course_id(self, default_ms): + """ + Test code paths missed by the above: + * passing an old-style course_id which has a loc map to split's create_item + """ + self.initdb(default_ms) + # create loc_map entry + loc_mapper().translate_location(self.MONGO_COURSEID, generate_location(self.MONGO_COURSEID)) + orphan = self.store.create_item(self.MONGO_COURSEID, 'problem', block_id='orphan') + self.assertEqual( + orphan.location.version_agnostic().as_course_locator(), + self.course_locations[self.MONGO_COURSEID].as_course_locator() + ) + + @ddt.data('direct') + def test_create_item_from_parent_location(self, default_ms): + """ + Test a code path missed by the above: passing an old-style location as parent but no + new location for the child + """ + self.initdb(default_ms) + self.store.create_item(self.course_locations[self.MONGO_COURSEID], 'problem', block_id='orphan') + orphans = self.store.get_orphans(self.course_locations[self.MONGO_COURSEID], None) + self.assertEqual(len(orphans), 0, "unexpected orphans: {}".format(orphans)) + #============================================================================================================= # General utils for not using django settings #============================================================================================================= @@ -378,3 +395,13 @@ def create_modulestore_instance(engine, doc_store_config, options, i18n_service= doc_store_config=doc_store_config, **options ) + + +def generate_location(course_id): + """ + Generate the locations for the given ids + """ + course_dict = Location.parse_course_id(course_id) + course_dict['tag'] = 'i4x' + course_dict['category'] = 'course' + return Location(course_dict)