Commit 9df23dc2 by Nimisha Asthagiri Committed by GitHub

Merge pull request #15066 from edx/neem/fix-block-structure-key-error

Fix KeyError issue with empty course structures
parents cd56cc56 8c01ffb7
...@@ -49,7 +49,7 @@ class CourseData(object): ...@@ -49,7 +49,7 @@ class CourseData(object):
@property @property
def structure(self): def structure(self):
if not self._structure: if self._structure is None:
self._structure = get_course_blocks( self._structure = get_course_blocks(
self.user, self.user,
self.location, self.location,
...@@ -59,7 +59,7 @@ class CourseData(object): ...@@ -59,7 +59,7 @@ class CourseData(object):
@property @property
def collected_structure(self): def collected_structure(self):
if not self._collected_block_structure: if self._collected_block_structure is None:
self._collected_block_structure = get_block_structure_manager(self.course_key).get_collected() self._collected_block_structure = get_block_structure_manager(self.course_key).get_collected()
return self._collected_block_structure return self._collected_block_structure
...@@ -90,7 +90,8 @@ class CourseData(object): ...@@ -90,7 +90,8 @@ class CourseData(object):
@property @property
def edited_on(self): def edited_on(self):
# get course block from structure only; subtree_edited_on field on modulestore's course block isn't optimized. # get course block from structure only; subtree_edited_on field on modulestore's course block isn't optimized.
course_block = self.structure[self.location] structure = self._effective_structure
course_block = structure[self.location]
return getattr(course_block, 'subtree_edited_on', None) return getattr(course_block, 'subtree_edited_on', None)
def __unicode__(self): def __unicode__(self):
......
...@@ -4,6 +4,7 @@ Tests for CourseData utility class. ...@@ -4,6 +4,7 @@ Tests for CourseData utility class.
from mock import patch from mock import patch
from lms.djangoapps.course_blocks.api import get_course_blocks from lms.djangoapps.course_blocks.api import get_course_blocks
from openedx.core.djangoapps.content.block_structure.api import get_course_in_cache
from student.tests.factories import UserFactory from student.tests.factories import UserFactory
from xmodule.modulestore import ModuleStoreEnum from xmodule.modulestore import ModuleStoreEnum
from xmodule.modulestore.tests.django_utils import ModuleStoreTestCase from xmodule.modulestore.tests.django_utils import ModuleStoreTestCase
...@@ -23,10 +24,13 @@ class CourseDataTest(ModuleStoreTestCase): ...@@ -23,10 +24,13 @@ class CourseDataTest(ModuleStoreTestCase):
# need to re-retrieve the course since the version on the original course isn't accurate. # need to re-retrieve the course since the version on the original course isn't accurate.
self.course = self.store.get_course(self.course.id) self.course = self.store.get_course(self.course.id)
self.user = UserFactory.create() self.user = UserFactory.create()
self.one_true_structure = get_course_blocks(self.user, self.course.location) self.collected_structure = get_course_in_cache(self.course.id)
self.one_true_structure = get_course_blocks(
self.user, self.course.location, collected_block_structure=self.collected_structure,
)
self.expected_results = { self.expected_results = {
'course': self.course, 'course': self.course,
'collected_block_structure': self.one_true_structure, 'collected_block_structure': self.collected_structure,
'structure': self.one_true_structure, 'structure': self.one_true_structure,
'course_key': self.course.id, 'course_key': self.course.id,
'location': self.course.location, 'location': self.course.location,
...@@ -75,3 +79,18 @@ class CourseDataTest(ModuleStoreTestCase): ...@@ -75,3 +79,18 @@ class CourseDataTest(ModuleStoreTestCase):
def test_no_data(self): def test_no_data(self):
with self.assertRaises(ValueError): with self.assertRaises(ValueError):
_ = CourseData(self.user) _ = CourseData(self.user)
@patch.dict('django.conf.settings.FEATURES', {'DISABLE_START_DATES': False})
def test_full_string(self):
empty_structure = get_course_blocks(self.user, self.course.location)
self.assertFalse(empty_structure)
# full_string retrieves value from collected_structure when structure is empty.
course_data = CourseData(
self.user, structure=empty_structure, collected_block_structure=self.collected_structure,
)
self.assertIn(u'Course: course_key: {}, version:'.format(self.course.id), course_data.full_string())
# full_string returns minimal value when structures aren't readily available.
course_data = CourseData(self.user, course_key=self.course.id)
self.assertIn(u'empty course structure', course_data.full_string())
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