diff --git a/lms/djangoapps/courseware/grades.py b/lms/djangoapps/courseware/grades.py index e1b508d574..d9a76768c1 100644 --- a/lms/djangoapps/courseware/grades.py +++ b/lms/djangoapps/courseware/grades.py @@ -842,7 +842,9 @@ def _calculate_score_for_modules(user_id, course, modules): if module.category not in ignore_categories and (module.graded or module.has_score): module_score = scores_client.get(locations[index]) if module_score: - module_percentages.append(module_score.correct / module_score.total) + correct = module_score.correct or 0 + total = module_score.total or 1 + module_percentages.append(correct / total) return sum(module_percentages) / float(len(module_percentages)) if module_percentages else 0 diff --git a/lms/djangoapps/courseware/tests/test_grades.py b/lms/djangoapps/courseware/tests/test_grades.py index cb464534fd..4b43ec2a8d 100644 --- a/lms/djangoapps/courseware/tests/test_grades.py +++ b/lms/djangoapps/courseware/tests/test_grades.py @@ -19,7 +19,7 @@ from courseware.grades import ( get_module_score ) from courseware.module_render import get_module -from courseware.model_data import FieldDataCache +from courseware.model_data import FieldDataCache, set_score from courseware.tests.helpers import ( LoginEnrollmentTestCase, get_request_for_user @@ -432,6 +432,29 @@ class TestGetModuleScore(LoginEnrollmentTestCase, ModuleStoreTestCase): score = get_module_score(self.request.user, self.course, self.seq1) self.assertEqual(score, .5) + def test_get_module_score_with_empty_score(self): + """ + Test test_get_module_score_with_empty_score + """ + set_score(self.request.user.id, self.problem1.location, None, None) # pylint: disable=no-member + set_score(self.request.user.id, self.problem2.location, None, None) # pylint: disable=no-member + + with self.assertNumQueries(1): + score = get_module_score(self.request.user, self.course, self.seq1) + self.assertEqual(score, 0) + + answer_problem(self.course, self.request, self.problem1) + + with self.assertNumQueries(1): + score = get_module_score(self.request.user, self.course, self.seq1) + self.assertEqual(score, 0.5) + + answer_problem(self.course, self.request, self.problem2) + + with self.assertNumQueries(1): + score = get_module_score(self.request.user, self.course, self.seq1) + self.assertEqual(score, 1.0) + def test_get_module_score_with_randomize(self): """ Test test_get_module_score_with_randomize