From 05c02f0d5f8efce60d23baa20a06099b5f59dfca Mon Sep 17 00:00:00 2001 From: Calen Pennington Date: Fri, 5 Oct 2012 10:28:18 -0400 Subject: [PATCH] Fixing tests from changes around draft mode --- cms/djangoapps/contentstore/tests/tests.py | 16 +++++----- cms/djangoapps/contentstore/views.py | 1 + cms/envs/test.py | 22 ++++++++----- .../xmodule/xmodule/modulestore/__init__.py | 8 +++-- .../lib/xmodule/xmodule/modulestore/mongo.py | 31 ++++++++++++------- .../lib/xmodule/xmodule/modulestore/search.py | 9 +++--- common/lib/xmodule/xmodule/modulestore/xml.py | 15 +++++---- 7 files changed, 59 insertions(+), 43 deletions(-) diff --git a/cms/djangoapps/contentstore/tests/tests.py b/cms/djangoapps/contentstore/tests/tests.py index 429774c91e..a149ce65b2 100644 --- a/cms/djangoapps/contentstore/tests/tests.py +++ b/cms/djangoapps/contentstore/tests/tests.py @@ -141,8 +141,6 @@ class AuthTestCase(ContentStoreTestCase): """Make sure pages that do require login work.""" auth_pages = ( reverse('index'), - reverse('edit_item'), - reverse('save_item'), ) # These are pages that should just load when the user is logged in @@ -195,17 +193,17 @@ class EditTestCase(ContentStoreTestCase): xmodule.modulestore.django._MODULESTORES = {} xmodule.modulestore.django.modulestore().collection.drop() - def check_edit_item(self, test_course_name): + def check_edit_unit(self, test_course_name): import_from_xml(modulestore(), 'common/test/data/', [test_course_name]) - for descriptor in modulestore().get_items(Location(None, None, None, None, None)): + for descriptor in modulestore().get_items(Location(None, None, 'vertical', None, None)): print "Checking ", descriptor.location.url() print descriptor.__class__, descriptor.location - resp = self.client.get(reverse('edit_item'), {'id': descriptor.location.url()}) + resp = self.client.get(reverse('edit_unit', kwargs={'location': descriptor.location.url()})) self.assertEqual(resp.status_code, 200) - def test_edit_item_toy(self): - self.check_edit_item('toy') + def test_edit_unit_toy(self): + self.check_edit_unit('toy') - def test_edit_item_full(self): - self.check_edit_item('full') + def test_edit_unit_full(self): + self.check_edit_unit('full') diff --git a/cms/djangoapps/contentstore/views.py b/cms/djangoapps/contentstore/views.py index 43ec799a66..94dcc58b3c 100644 --- a/cms/djangoapps/contentstore/views.py +++ b/cms/djangoapps/contentstore/views.py @@ -169,6 +169,7 @@ def edit_subsection(request, location): 'lms_link': lms_link }) + @login_required def edit_unit(request, location): """ diff --git a/cms/envs/test.py b/cms/envs/test.py index 7dcd32caab..217ed0e573 100644 --- a/cms/envs/test.py +++ b/cms/envs/test.py @@ -38,17 +38,23 @@ STATICFILES_DIRS += [ if os.path.isdir(COMMON_TEST_DATA_ROOT / course_dir) ] +modulestore_options = { + 'default_class': 'xmodule.raw_module.RawDescriptor', + 'host': 'localhost', + 'db': 'test_xmodule', + 'collection': 'modulestore', + 'fs_root': GITHUB_REPO_ROOT, + 'render_template': 'mitxmako.shortcuts.render_to_string', +} + MODULESTORE = { 'default': { 'ENGINE': 'xmodule.modulestore.mongo.MongoModuleStore', - 'OPTIONS': { - 'default_class': 'xmodule.raw_module.RawDescriptor', - 'host': 'localhost', - 'db': 'test_xmodule', - 'collection': 'modulestore', - 'fs_root': GITHUB_REPO_ROOT, - 'render_template': 'mitxmako.shortcuts.render_to_string', - } + 'OPTIONS': modulestore_options + }, + 'direct': { + 'ENGINE': 'xmodule.modulestore.mongo.MongoModuleStore', + 'OPTIONS': modulestore_options } } diff --git a/common/lib/xmodule/xmodule/modulestore/__init__.py b/common/lib/xmodule/xmodule/modulestore/__init__.py index 3ee83449f9..3e22f2f506 100644 --- a/common/lib/xmodule/xmodule/modulestore/__init__.py +++ b/common/lib/xmodule/xmodule/modulestore/__init__.py @@ -240,11 +240,15 @@ class ModuleStore(object): An abstract interface for a database backend that stores XModuleDescriptor instances """ + def has_item(self, location): + """ + Returns True if location exists in this ModuleStore. + """ + raise NotImplementedError + def get_item(self, location, depth=0): """ Returns an XModuleDescriptor instance for the item at location. - If location.revision is None, returns the item with the most - recent revision If any segment of the location is None except revision, raises xmodule.modulestore.exceptions.InsufficientSpecificationError diff --git a/common/lib/xmodule/xmodule/modulestore/mongo.py b/common/lib/xmodule/xmodule/modulestore/mongo.py index 1e203c6a78..30ae5c7539 100644 --- a/common/lib/xmodule/xmodule/modulestore/mongo.py +++ b/common/lib/xmodule/xmodule/modulestore/mongo.py @@ -70,17 +70,21 @@ class CachingDescriptorSystem(MakoDescriptorSystem): ) -def location_to_query(location): +def location_to_query(location, wildcard=True): """ Takes a Location and returns a SON object that will query for that location. Fields in location that are None are ignored in the query + + If `wildcard` is True, then a None in a location is treated as a wildcard + query. Otherwise, it is searched for literally """ query = SON() # Location dict is ordered by specificity, and SON # will preserve that order for queries for key, val in Location(location).dict().iteritems(): - if val is not None: - query['_id.{key}'.format(key=key)] = val + if wildcard and val is None: + continue + query['_id.{key}'.format(key=key)] = val return query @@ -203,18 +207,27 @@ class MongoModuleStore(ModuleStoreBase): ItemNotFoundError. ''' item = self.collection.find_one( - location_to_query(location), + location_to_query(location, wildcard=False), sort=[('revision', pymongo.ASCENDING)], ) if item is None: raise ItemNotFoundError(location) return item + def has_item(self, location): + """ + Returns True if location exists in this ModuleStore. + """ + location = Location.ensure_fully_specified(location) + try: + self._find_one(location) + return True + except ItemNotFoundError: + return False + def get_item(self, location, depth=0): """ Returns an XModuleDescriptor instance for the item at location. - If location.revision is None, returns the item with the most - recent revision. If any segment of the location is None except revision, raises xmodule.modulestore.exceptions.InsufficientSpecificationError @@ -322,16 +335,10 @@ class MongoModuleStore(ModuleStoreBase): '''Find all locations that are the parents of this location. Needed for path_to_location(). - If there is no data at location in this modulestore, raise - ItemNotFoundError. - returns an iterable of things that can be passed to Location. This may be empty if there are no parents. ''' location = Location.ensure_fully_specified(location) - # Check that it's actually in this modulestore. - self._find_one(location) - # now get the parents items = self.collection.find({'definition.children': location.url()}, {'_id': True}) return [i['_id'] for i in items] diff --git a/common/lib/xmodule/xmodule/modulestore/search.py b/common/lib/xmodule/xmodule/modulestore/search.py index f9901e8bfe..e6d71d271e 100644 --- a/common/lib/xmodule/xmodule/modulestore/search.py +++ b/common/lib/xmodule/xmodule/modulestore/search.py @@ -60,10 +60,8 @@ def path_to_location(modulestore, course_id, location): (loc, path) = queue.pop() # Takes from the end loc = Location(loc) - # get_parent_locations should raise ItemNotFoundError if location - # isn't found so we don't have to do it explicitly. Call this - # first to make sure the location is there (even if it's a course, and - # we would otherwise immediately exit). + # Call get_parent_locations first to make sure the location is there + # (even if it's a course, and we would otherwise immediately exit). parents = modulestore.get_parent_locations(loc) # print 'Processing loc={0}, path={1}'.format(loc, path) @@ -81,6 +79,9 @@ def path_to_location(modulestore, course_id, location): # If we're here, there is no path return None + if not modulestore.has_item(location): + raise ItemNotFoundError + path = find_path_to_course() if path is None: raise NoPathToItem(location) diff --git a/common/lib/xmodule/xmodule/modulestore/xml.py b/common/lib/xmodule/xmodule/modulestore/xml.py index 6f8430917d..baef017665 100644 --- a/common/lib/xmodule/xmodule/modulestore/xml.py +++ b/common/lib/xmodule/xmodule/modulestore/xml.py @@ -477,11 +477,16 @@ class XMLModuleStore(ModuleStoreBase): except KeyError: raise ItemNotFoundError(location) + def has_item(self, location): + """ + Returns True if location exists in this ModuleStore. + """ + location = Location(location) + return any(location in course_modules for course_modules in self.modules.values()) + def get_item(self, location, depth=0): """ Returns an XModuleDescriptor instance for the item at location. - If location.revision is None, returns the most item with the most - recent revision If any segment of the location is None except revision, raises xmodule.modulestore.exceptions.InsufficientSpecificationError @@ -545,14 +550,8 @@ class XMLModuleStore(ModuleStoreBase): '''Find all locations that are the parents of this location. Needed for path_to_location(). - If there is no data at location in this modulestore, raise - ItemNotFoundError. - returns an iterable of things that can be passed to Location. This may be empty if there are no parents. ''' location = Location.ensure_fully_specified(location) - if not self.parent_tracker.is_known(location): - raise ItemNotFoundError(location) - return self.parent_tracker.parents(location)