|
| 1 | +""" |
| 2 | +Tests for enterprise.filters.grades pipeline step. |
| 3 | +""" |
| 4 | +import uuid |
| 5 | +from unittest.mock import patch |
| 6 | + |
| 7 | +from django.test import TestCase |
| 8 | + |
| 9 | +from enterprise.filters.grades import GradeEventContextEnricher |
| 10 | + |
| 11 | + |
| 12 | +class TestGradeEventContextEnricher(TestCase): |
| 13 | + """ |
| 14 | + Tests for GradeEventContextEnricher pipeline step. |
| 15 | + """ |
| 16 | + |
| 17 | + def _make_step(self): |
| 18 | + return GradeEventContextEnricher( |
| 19 | + filter_type="org.openedx.learning.grade.context.requested.v1", |
| 20 | + running_pipeline=[], |
| 21 | + ) |
| 22 | + |
| 23 | + @patch("enterprise.models.EnterpriseCourseEnrollment.get_enterprise_uuids_with_user_and_course") |
| 24 | + def test_enriches_context_when_enterprise_enrollment_found(self, mock_get_uuids): |
| 25 | + """ |
| 26 | + When an enterprise course enrollment exists, enterprise_uuid is added to context. |
| 27 | + """ |
| 28 | + enterprise_uuid = uuid.uuid4() |
| 29 | + mock_get_uuids.return_value = [enterprise_uuid] |
| 30 | + |
| 31 | + step = self._make_step() |
| 32 | + context = {"org": "TestOrg", "course_id": "course-v1:org+course+run"} |
| 33 | + result = step.run_filter(context=context, user_id=7, course_id="course-v1:org+course+run") |
| 34 | + |
| 35 | + assert result == { |
| 36 | + "context": {**context, "enterprise_uuid": str(enterprise_uuid)}, |
| 37 | + "user_id": 7, |
| 38 | + "course_id": "course-v1:org+course+run", |
| 39 | + } |
| 40 | + mock_get_uuids.assert_called_once_with(7, "course-v1:org+course+run") |
| 41 | + |
| 42 | + @patch("enterprise.models.EnterpriseCourseEnrollment.get_enterprise_uuids_with_user_and_course") |
| 43 | + def test_returns_unchanged_context_when_no_enterprise_enrollment(self, mock_get_uuids): |
| 44 | + """ |
| 45 | + When no enterprise course enrollment exists, context is returned unchanged. |
| 46 | + """ |
| 47 | + mock_get_uuids.return_value = [] |
| 48 | + |
| 49 | + step = self._make_step() |
| 50 | + context = {"org": "TestOrg"} |
| 51 | + result = step.run_filter(context=context, user_id=99, course_id="course-v1:org+course+run") |
| 52 | + |
| 53 | + assert result == { |
| 54 | + "context": context, |
| 55 | + "user_id": 99, |
| 56 | + "course_id": "course-v1:org+course+run", |
| 57 | + } |
| 58 | + assert "enterprise_uuid" not in result["context"] |
| 59 | + |
| 60 | + @patch("enterprise.models.EnterpriseCourseEnrollment.get_enterprise_uuids_with_user_and_course") |
| 61 | + def test_uses_first_uuid_when_multiple_enrollments(self, mock_get_uuids): |
| 62 | + """ |
| 63 | + When multiple enterprise enrollments exist, only the first UUID is used. |
| 64 | + """ |
| 65 | + first_uuid = uuid.uuid4() |
| 66 | + second_uuid = uuid.uuid4() |
| 67 | + mock_get_uuids.return_value = [first_uuid, second_uuid] |
| 68 | + |
| 69 | + step = self._make_step() |
| 70 | + context = {} |
| 71 | + result = step.run_filter(context=context, user_id=1, course_id="course-v1:x+y+z") |
| 72 | + |
| 73 | + assert result["context"]["enterprise_uuid"] == str(first_uuid) |
0 commit comments