diff --git a/openedx_filters/learning/filters.py b/openedx_filters/learning/filters.py index 011d0149..2f10252a 100644 --- a/openedx_filters/learning/filters.py +++ b/openedx_filters/learning/filters.py @@ -4,6 +4,8 @@ from typing import Optional +from django.db.models.query import QuerySet + from openedx_filters.exceptions import OpenEdxFilterException from openedx_filters.tooling import OpenEdxPublicFilter from openedx_filters.utils import SensitiveDataManagementMixin @@ -818,3 +820,40 @@ def run_filter(cls, url, org): """ data = super().run_pipeline(url=url, org=org) return data.get("url"), data.get("org") + + +class ScheduleQuerySetRequested(OpenEdxPublicFilter): + """ + Custom class used to create schedule queryset filters filters and its custom methods. + """ + + filter_type = "org.openedx.learning.schedule.queryset.requested.v1" + + class PreventScheduleQuerySetRequest(OpenEdxFilterException): + """ + Custom class used to stop the schedule queryset request process. + """ + + def __init__(self, message: str, schedules: QuerySet): + """ + Override init that defines specific arguments used in the schedule queryset request process. + + Arguments: + message (str): error message for the exception. + schedules (QuerySet): Queryset of schedules to be sent + """ + super().__init__(message, schedules=schedules) + + @classmethod + def run_filter(cls, schedules: QuerySet) -> QuerySet: + """ + Execute a filter with the signature specified. + + Arguments: + schedules (QuerySet): Queryset of schedules to be sent. + + Returns: + QuerySet: Schedules to be sent. + """ + data = super().run_pipeline(schedules=schedules) + return data.get("schedules") diff --git a/openedx_filters/learning/tests/test_filters.py b/openedx_filters/learning/tests/test_filters.py index 41896ccb..dc76a82d 100644 --- a/openedx_filters/learning/tests/test_filters.py +++ b/openedx_filters/learning/tests/test_filters.py @@ -25,6 +25,7 @@ InstructorDashboardRenderStarted, ORASubmissionViewRenderStarted, RenderXBlockStarted, + ScheduleQuerySetRequested, StudentLoginRequested, StudentRegistrationRequested, VerticalBlockChildRenderStarted, @@ -776,3 +777,44 @@ def test_lms_page_url_requested(self): self.assertEqual(url, url_result) self.assertEqual(org, org_result) + + +@ddt +class TestScheduleFilters(TestCase): + """ + Test class to verify standard behavior of the schedule filters. + + You'll find test suites for: + - `ScheduleQuerySetRequested` + """ + + def test_schedule_requested(self): + """ + Test schedule requested filter. + + Expected behavior: + - The filter should return the filterd schedules. + """ + schedules = Mock() + + result = ScheduleQuerySetRequested.run_filter(schedules) + + self.assertEqual(schedules, result) + + @data( + ( + ScheduleQuerySetRequested.PreventScheduleQuerySetRequest, + {"schedules": Mock(), "message": "Can't request QuerySet Schedule."} + ) + ) + @unpack + def test_halt_queryset_request(self, request_exception, attributes): + """ + Test for queryset request exceptions attributes. + + Expected behavior: + - The exception must have the attributes specified. + """ + exception = request_exception(**attributes) + + self.assertDictContainsSubset(attributes, exception.__dict__)