From 7cebb873df947a96a02cb38f7cc465f8c5d040b5 Mon Sep 17 00:00:00 2001 From: Chris Dodge Date: Fri, 14 Jun 2013 16:35:21 -0400 Subject: [PATCH] allow for an optional throw_on_not_found on contentstore().find() so we can clean up caller logic in the tests --- .../contentstore/tests/test_contentstore.py | 52 +++++-------------- .../lib/xmodule/xmodule/contentstore/mongo.py | 7 ++- 2 files changed, 19 insertions(+), 40 deletions(-) diff --git a/cms/djangoapps/contentstore/tests/test_contentstore.py b/cms/djangoapps/contentstore/tests/test_contentstore.py index 5b829781c4..a503f22c26 100644 --- a/cms/djangoapps/contentstore/tests/test_contentstore.py +++ b/cms/djangoapps/contentstore/tests/test_contentstore.py @@ -435,14 +435,12 @@ class ContentStoreToyCourseTest(ModuleStoreTestCase): import_from_xml(module_store, 'common/test/data/', ['full'], static_content_store=content_store) - content = None - try: - location = StaticContent.get_location_from_path('/c4x/edX/full/asset/circuits_duality.gif') - content = content_store.find(location) - except NotFoundError: - pass - + # look up original (and thumbnail) in content store, should be there after import + location = StaticContent.get_location_from_path('/c4x/edX/full/asset/circuits_duality.gif') + content = content_store.find(location, throw_on_not_found=False) + thumbnail_location = content.thumbnail_location self.assertIsNotNone(content) + self.assertIsNotNone(thumbnail_location) # go through the website to do the delete, since the soft-delete logic is in the view @@ -453,26 +451,14 @@ class ContentStoreToyCourseTest(ModuleStoreTestCase): asset_location = StaticContent.get_location_from_path('/c4x/edX/full/asset/circuits_duality.gif') # now try to find it in store, but they should not be there any longer - content = None - thumbnail = None - try: - content = content_store.find(asset_location) - thumbnail = trash_store.find(content.thumbnail_location) - except NotFoundError: - pass + content = content_store.find(asset_location, throw_on_not_found=False) + thumbnail = content_store.find(thumbnail_location, throw_on_not_found=False) self.assertIsNone(content) self.assertIsNone(thumbnail) - # now try to find it and the thumbnail in trashcan - content = None - thumbnail = None - try: - content = trash_store.find(asset_location) - thumbnail = trash_store.find(content.thumbnail_location) - except NotFoundError: - pass - - # should be in trashcan + # now try to find it and the thumbnail in trashcan - should be in there + content = trash_store.find(asset_location, throw_on_not_found=False) + thumbnail = trash_store.find(thumbnail_location, throw_on_not_found=False) self.assertIsNotNone(content) self.assertIsNotNone(thumbnail) @@ -480,13 +466,8 @@ class ContentStoreToyCourseTest(ModuleStoreTestCase): restore_asset_from_trashcan('/c4x/edX/full/asset/circuits_duality.gif') # now try to find it in courseware store, and they should be back after restore - content = None - thumbnail = None - try: - content = content_store.find(asset_location) - thumbnail = trash_store.find(content.thumbnail_location) - except NotFoundError: - pass + content = content_store.find(asset_location, throw_on_not_found=False) + thumbnail = content_store.find(thumbnail_location, throw_on_not_found=False) self.assertIsNotNone(content) self.assertIsNotNone(thumbnail) @@ -502,13 +483,8 @@ class ContentStoreToyCourseTest(ModuleStoreTestCase): course_location = CourseDescriptor.id_to_location('edX/full/6.002_Spring_2012') - content = None - try: - location = StaticContent.get_location_from_path('/c4x/edX/full/asset/circuits_duality.gif') - content = content_store.find(location) - except NotFoundError: - pass - + location = StaticContent.get_location_from_path('/c4x/edX/full/asset/circuits_duality.gif') + content = content_store.find(location, throw_on_not_found=False) self.assertIsNotNone(content) # go through the website to do the delete, since the soft-delete logic is in the view diff --git a/common/lib/xmodule/xmodule/contentstore/mongo.py b/common/lib/xmodule/xmodule/contentstore/mongo.py index 7d96e132ee..fa0fc95181 100644 --- a/common/lib/xmodule/xmodule/contentstore/mongo.py +++ b/common/lib/xmodule/xmodule/contentstore/mongo.py @@ -43,7 +43,7 @@ class MongoContentStore(ContentStore): if self.fs.exists({"_id": id}): self.fs.delete(id) - def find(self, location): + def find(self, location, throw_on_not_found=True): id = StaticContent.get_id_from_location(location) try: with self.fs.get(id) as fp: @@ -52,7 +52,10 @@ class MongoContentStore(ContentStore): thumbnail_location=fp.thumbnail_location if hasattr(fp, 'thumbnail_location') else None, import_path=fp.import_path if hasattr(fp, 'import_path') else None) except NoFile: - raise NotFoundError() + if throw_on_not_found: + raise NotFoundError() + else: + return None def export(self, location, output_directory): content = self.find(location)