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):
@property
def structure(self):
if not self._structure:
if self._structure is None:
self._structure = get_course_blocks(
self.user,
self.location,
......@@ -59,7 +59,7 @@ class CourseData(object):
@property
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()
return self._collected_block_structure
......@@ -90,7 +90,8 @@ class CourseData(object):
@property
def edited_on(self):
# 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)
def __unicode__(self):
......
......@@ -4,6 +4,7 @@ Tests for CourseData utility class.
from mock import patch
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 xmodule.modulestore import ModuleStoreEnum
from xmodule.modulestore.tests.django_utils import ModuleStoreTestCase
......@@ -23,10 +24,13 @@ class CourseDataTest(ModuleStoreTestCase):
# 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.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 = {
'course': self.course,
'collected_block_structure': self.one_true_structure,
'collected_block_structure': self.collected_structure,
'structure': self.one_true_structure,
'course_key': self.course.id,
'location': self.course.location,
......@@ -75,3 +79,18 @@ class CourseDataTest(ModuleStoreTestCase):
def test_no_data(self):
with self.assertRaises(ValueError):
_ = 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