Commit 05712da6 by Christina Roberts

Merge pull request #744 from edx/dhm/pep_pylint_cleanups

Add doc strings and fix spaces
parents 4129c1cf 20641bab
......@@ -10,6 +10,7 @@ from xmodule.modulestore.locator import BlockUsageLocator
from xmodule.modulestore.mongo import draft
from xmodule.modulestore import Location
class LocMapperStore(object):
'''
This store persists mappings among the addressing schemes. At this time, it's between the old i4x Location
......@@ -25,26 +26,30 @@ class LocMapperStore(object):
or dominant store, but that's not a requirement. This store creates its own connection.
'''
# C0103: varnames and attrs must be >= 3 chars, but db defined by long time usage
# pylint: disable = C0103
def __init__(self, host, db, collection, port=27017, user=None, password=None, **kwargs):
'''
Constructor
'''
self.db = pymongo.database.Database(pymongo.MongoClient(
host=host,
port=port,
tz_aware=True,
**kwargs
), db)
self.db = pymongo.database.Database(
pymongo.MongoClient(
host=host,
port=port,
tz_aware=True,
**kwargs
),
db
)
if user is not None and password is not None:
self.db.authenticate(user, password)
self.location_map = self.db[collection + '.location_map']
self.location_map.write_concern = {'w': 1}
# location_map functions
def create_map_entry(self, course_location, course_id=None, draft_branch='draft', prod_branch='published',
block_map=None):
block_map=None):
"""
Add a new entry to map this course_location to the new style CourseLocator.course_id. If course_id is not
provided, it creates the default map of using org.course.name from the location (just like course_id) if
......@@ -89,13 +94,12 @@ class LocMapperStore(object):
location_id['name'] = course_location.name
self.location_map.insert({
'_id': location_id,
'course_id': course_id,
'draft_branch': draft_branch,
'prod_branch': prod_branch,
'block_map': block_map or {},
})
'_id': location_id,
'course_id': course_id,
'draft_branch': draft_branch,
'prod_branch': prod_branch,
'block_map': block_map or {},
})
def translate_location(self, old_style_course_id, location, published=True, add_entry_if_missing=True):
"""
......@@ -202,7 +206,6 @@ class LocMapperStore(object):
revision)
return None
def add_block_location_translator(self, location, old_course_id=None, usage_id=None):
"""
Similar to translate_location which adds an entry if none is found, but this cannot create a new
......@@ -250,7 +253,7 @@ class LocMapperStore(object):
if computed_usage_id is None:
computed_usage_id = self._add_to_block_map(location, location_id, map_entry['block_map'])
elif (location.name not in map_entry['block_map'] or
location.category not in map_entry['block_map'][location.name]):
location.category not in map_entry['block_map'][location.name]):
alt_usage_id = self._verify_uniqueness(computed_usage_id, map_entry['block_map'])
if alt_usage_id != computed_usage_id:
if usage_id is not None:
......@@ -269,7 +272,6 @@ class LocMapperStore(object):
return computed_usage_id
def update_block_location_translator(self, location, usage_id, old_course_id=None, autogenerated_usage_id=False):
"""
Update all existing maps from location's block to the new usage_id. Used for changing the usage_id,
......@@ -307,7 +309,6 @@ class LocMapperStore(object):
return usage_id
def delete_block_location_translator(self, location, old_course_id=None):
"""
Remove all existing maps from location's block.
......@@ -349,8 +350,8 @@ class LocMapperStore(object):
"""
if course_id:
# re doesn't allow ?P<_id.org> and ilk
m = re.match(r'([^/]+)/([^/]+)/([^/]+)', course_id)
return dict(zip(['_id.org', '_id.course', '_id.name'], m.groups()))
matched = re.match(r'([^/]+)/([^/]+)/([^/]+)', course_id)
return dict(zip(['_id.org', '_id.course', '_id.name'], matched.groups()))
location_id = {'_id.org': location.org, '_id.course': location.course}
if location.category == 'course':
......@@ -358,6 +359,9 @@ class LocMapperStore(object):
return location_id
def _block_id_is_guid(self, name):
"""
Does the given name look like it's a guid?
"""
return len(name) == 32 and re.search(r'[^0-9A-Fa-f]', name) is None
def _verify_uniqueness(self, name, block_map):
......
......@@ -12,6 +12,9 @@ from xmodule.modulestore.loc_mapper_store import LocMapperStore
class TestLocationMapper(unittest.TestCase):
"""
Test the location to locator mapper
"""
def setUp(self):
modulestore_options = {
......@@ -23,11 +26,10 @@ class TestLocationMapper(unittest.TestCase):
# pylint: disable=W0142
TestLocationMapper.loc_store = LocMapperStore(**modulestore_options)
def tearDown(self):
db = TestLocationMapper.loc_store.db
db.drop_collection(TestLocationMapper.loc_store.location_map)
db.connection.close()
dbref = TestLocationMapper.loc_store.db
dbref.drop_collection(TestLocationMapper.loc_store.location_map)
dbref.connection.close()
TestLocationMapper.loc_store = None
def test_create_map(self):
......@@ -438,7 +440,6 @@ class TestLocationMapper(unittest.TestCase):
)
self.assertEqual(trans_loc.name, '1')
def test_delete_block(self):
"""
test delete_block_location_translator(location, old_course_id=None)
......@@ -490,10 +491,17 @@ class TestLocationMapper(unittest.TestCase):
)
self.assertEqual(locator.usage_id, 'problem3')
#==================================
# functions to mock existing services
def loc_mapper():
"""
Mocks the global location mapper.
"""
return TestLocationMapper.loc_store
def render_to_template_mock(*_args):
pass
"""
Mocks the mako render_to_template w/ a noop
"""
......@@ -252,12 +252,18 @@ class LocatorTest(TestCase):
def check_course_locn_fields(self, testobj, msg, version_guid=None,
course_id=None, branch=None):
"""
Checks the version, course_id, and branch in testobj
"""
self.assertEqual(testobj.version_guid, version_guid, msg)
self.assertEqual(testobj.course_id, course_id, msg)
self.assertEqual(testobj.branch, branch, msg)
def check_block_locn_fields(self, testobj, msg, version_guid=None,
course_id=None, branch=None, block=None):
"""
Does adds a block id check over and above the check_course_locn_fields tests
"""
self.check_course_locn_fields(testobj, msg, version_guid, course_id,
branch)
self.assertEqual(testobj.usage_id, block)
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