fix: fix credit serialization for Learner Home (#31870)

* fix: fix credit serialization

Was accidentally indexing based on course ID string instead of course ID

* docs: update a docstring

Programs are weird in that they index on string of course ID instead of
course ID. Highlighting this info through the docstring

* test: add more checks in serialization tests
This commit is contained in:
Nathan Sprenkle
2023-03-02 11:53:00 -05:00
committed by GitHub
parent b1507c6cec
commit d305ab8a61
3 changed files with 14 additions and 4 deletions

View File

@@ -460,7 +460,7 @@ class LearnerEnrollmentSerializer(serializers.Serializer):
def get_credit(self, instance):
"""Pull credit statuses from context"""
credit_status = self.context["credit_statuses"].get(str(instance.course_id))
credit_status = self.context["credit_statuses"].get(instance.course_id)
# If user or course is ineligible for credit, return empty
if not credit_status:

View File

@@ -820,7 +820,7 @@ class TestCreditSerializer(LearnerDashboardBaseTest):
"""Mock data following the shape of credit_statuses"""
return {
"course_key": enrollment.course.id,
"course_key": str(enrollment.course.id),
"eligible": True,
"deadline": random_date(),
"purchased": False,
@@ -835,7 +835,7 @@ class TestCreditSerializer(LearnerDashboardBaseTest):
def create_test_context(cls, enrollment):
"""Credit data, packaged as it would be for serialization context"""
return {enrollment.course.id: {**cls.create_test_data(enrollment)}}
return {enrollment.course_id: {**cls.create_test_data(enrollment)}}
def test_serialize_credit(self):
# Given an enrollment and a course with ability to purchase credit
@@ -900,8 +900,18 @@ class TestLearnerEnrollmentsSerializer(LearnerDashboardBaseTest):
"programs",
"credit",
]
# Verify we have all the expected keys in our output
self.assertEqual(output.keys(), set(expected_keys))
# Entitlements should be the only empty field for an enrollment
entitlement = output.pop("entitlement")
self.assertDictEqual(entitlement, {})
# All other keys should have some basic info, unless we broke something
for key in output.keys():
self.assertNotEqual(output[key], {})
def test_credit_no_credit_option(self):
# Given an enrollment
enrollment = self.create_test_enrollment()

View File

@@ -343,7 +343,7 @@ def get_course_programs(user, course_enrollments, site):
Get programs related to the courses the user is enrolled in.
Returns: {
<course_id>: {
str(<course_id>): {
"programs": [list of programs]
}
}