Commit 8f530d12 by Don Mitchell

Raise ItemNotFoundError rather than return None if no map found

parent bd8a7906
...@@ -107,10 +107,13 @@ class LocMapperStore(object): ...@@ -107,10 +107,13 @@ class LocMapperStore(object):
if the code just trips into this w/o creating translations. The downfall is that ambiguous course if the code just trips into this w/o creating translations. The downfall is that ambiguous course
locations may generate conflicting block_ids. locations may generate conflicting block_ids.
Will raise ItemNotFoundError if there's no mapping and add_entry_if_missing is False.
:param old_style_course_id: the course_id used in old mongo not the new one (optional, will use location) :param old_style_course_id: the course_id used in old mongo not the new one (optional, will use location)
:param location: a Location pointing to a module :param location: a Location pointing to a module
:param published: a boolean to indicate whether the caller wants the draft or published branch. :param published: a boolean to indicate whether the caller wants the draft or published branch.
:param add_entry_if_missing: a boolean as to whether to return None or to create an entry if the course :param add_entry_if_missing: a boolean as to whether to raise ItemNotFoundError or to create an entry if
the course
or block is not found in the map. or block is not found in the map.
NOTE: unlike old mongo, draft branches contain the whole course; so, it applies to all category NOTE: unlike old mongo, draft branches contain the whole course; so, it applies to all category
...@@ -126,7 +129,7 @@ class LocMapperStore(object): ...@@ -126,7 +129,7 @@ class LocMapperStore(object):
self.create_map_entry(course_location) self.create_map_entry(course_location)
entry = self.location_map.find_one(location_id) entry = self.location_map.find_one(location_id)
else: else:
return None raise ItemNotFoundError()
elif maps.count() > 1: elif maps.count() > 1:
# if more than one, prefer the one w/o a name if that exists. Otherwise, choose the first (arbitrary) # if more than one, prefer the one w/o a name if that exists. Otherwise, choose the first (arbitrary)
# a bit odd b/c maps is a cursor and doesn't allow multitraversal w/o requerying db # a bit odd b/c maps is a cursor and doesn't allow multitraversal w/o requerying db
...@@ -150,7 +153,7 @@ class LocMapperStore(object): ...@@ -150,7 +153,7 @@ class LocMapperStore(object):
if add_entry_if_missing: if add_entry_if_missing:
usage_id = self._add_to_block_map(location, location_id, entry['block_map']) usage_id = self._add_to_block_map(location, location_id, entry['block_map'])
else: else:
return None raise ItemNotFoundError()
elif isinstance(usage_id, dict): elif isinstance(usage_id, dict):
# name is not unique, look through for the right category # name is not unique, look through for the right category
if location.category in usage_id: if location.category in usage_id:
...@@ -158,7 +161,7 @@ class LocMapperStore(object): ...@@ -158,7 +161,7 @@ class LocMapperStore(object):
elif add_entry_if_missing: elif add_entry_if_missing:
usage_id = self._add_to_block_map(location, location_id, entry['block_map']) usage_id = self._add_to_block_map(location, location_id, entry['block_map'])
else: else:
return None raise ItemNotFoundError()
else: else:
raise InvalidLocationError() raise InvalidLocationError()
...@@ -179,7 +182,7 @@ class LocMapperStore(object): ...@@ -179,7 +182,7 @@ class LocMapperStore(object):
:param locator: a BlockUsageLocator :param locator: a BlockUsageLocator
""" """
# Does not use _lookup_course b/c it doesn't actually require that the course exist in the active_version # This does not require that the course exist in any modulestore
# only that it has a mapping entry. # only that it has a mapping entry.
maps = self.location_map.find({'course_id': locator.course_id}) maps = self.location_map.find({'course_id': locator.course_id})
# look for one which maps to this block usage_id # look for one which maps to this block usage_id
......
...@@ -78,12 +78,12 @@ class TestLocationMapper(unittest.TestCase): ...@@ -78,12 +78,12 @@ class TestLocationMapper(unittest.TestCase):
org = 'foo_org' org = 'foo_org'
course = 'bar_course' course = 'bar_course'
old_style_course_id = '{}/{}/{}'.format(org, course, 'baz_run') old_style_course_id = '{}/{}/{}'.format(org, course, 'baz_run')
prob_locator = loc_mapper().translate_location( with self.assertRaises(ItemNotFoundError):
old_style_course_id, _ = loc_mapper().translate_location(
Location('i4x', org, course, 'problem', 'abc123'), old_style_course_id,
add_entry_if_missing=False Location('i4x', org, course, 'problem', 'abc123'),
) add_entry_if_missing=False
self.assertIsNone(prob_locator, 'found entry in empty map table') )
new_style_course_id = '{}.geek_dept.{}.baz_run'.format(org, course) new_style_course_id = '{}.geek_dept.{}.baz_run'.format(org, course)
block_map = {'abc123': {'problem': 'problem2'}} block_map = {'abc123': {'problem': 'problem2'}}
...@@ -109,12 +109,12 @@ class TestLocationMapper(unittest.TestCase): ...@@ -109,12 +109,12 @@ class TestLocationMapper(unittest.TestCase):
) )
self.assertEqual(prob_locator.course_id, new_style_course_id) self.assertEqual(prob_locator.course_id, new_style_course_id)
# look for non-existent problem # look for non-existent problem
prob_locator = loc_mapper().translate_location( with self.assertRaises(ItemNotFoundError):
None, prob_locator = loc_mapper().translate_location(
Location('i4x', org, course, 'problem', '1def23'), None,
add_entry_if_missing=False Location('i4x', org, course, 'problem', '1def23'),
) add_entry_if_missing=False
self.assertIsNone(prob_locator, "Found non-existent problem") )
# add a distractor course # add a distractor course
block_map = {'abc123': {'problem': 'problem3'}} block_map = {'abc123': {'problem': 'problem3'}}
...@@ -477,11 +477,12 @@ class TestLocationMapper(unittest.TestCase): ...@@ -477,11 +477,12 @@ class TestLocationMapper(unittest.TestCase):
# delete from one course # delete from one course
location = location.replace(name='abc123') location = location.replace(name='abc123')
loc_mapper().delete_block_location_translator(location, '{}/{}/{}'.format(org, course, 'baz_run')) loc_mapper().delete_block_location_translator(location, '{}/{}/{}'.format(org, course, 'baz_run'))
self.assertIsNone(loc_mapper().translate_location( with self.assertRaises(ItemNotFoundError):
'{}/{}/{}'.format(org, course, 'baz_run'), loc_mapper().translate_location(
location, '{}/{}/{}'.format(org, course, 'baz_run'),
add_entry_if_missing=False location,
)) add_entry_if_missing=False
)
locator = loc_mapper().translate_location( locator = loc_mapper().translate_location(
'{}/{}/{}'.format(org, course, 'delta_run'), '{}/{}/{}'.format(org, course, 'delta_run'),
location, location,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment