From beb7fe659c59ed54879f397f33c0cc80a1769c88 Mon Sep 17 00:00:00 2001 From: Bryann Valderrama Date: Fri, 6 Dec 2024 13:12:02 -0500 Subject: [PATCH 1/5] feat: add schedule queryset request filter integration --- openedx/core/djangoapps/schedules/resolvers.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/openedx/core/djangoapps/schedules/resolvers.py b/openedx/core/djangoapps/schedules/resolvers.py index 5f769e6af299..7a966a575bec 100644 --- a/openedx/core/djangoapps/schedules/resolvers.py +++ b/openedx/core/djangoapps/schedules/resolvers.py @@ -14,6 +14,7 @@ from edx_ace.recipient import Recipient from edx_ace.recipient_resolver import RecipientResolver from edx_django_utils.monitoring import function_trace, set_custom_attribute +from openedx_filters.learning.filters import ScheduleQuerySetRequested from lms.djangoapps.courseware.utils import verified_upgrade_deadline_link, can_show_verified_upgrade from lms.djangoapps.discussion.notification_prefs.views import UsernameCipher @@ -154,6 +155,13 @@ def get_schedules_with_target_date_by_bin_and_orgs( schedules = self.filter_by_org(schedules) + try: + # .. filter_implemented_name: ScheduleQuerySetRequested + # .. filter_type: org.openedx.learning.schedule.queryset.requested.v1 + schedules = ScheduleQuerySetRequested.run_filter(schedules) + except ScheduleQuerySetRequested.PreventScheduleQuerysetRequest as exc: + schedules = exc.schedules + if "read_replica" in settings.DATABASES: schedules = schedules.using("read_replica") From 224fcafbce71db90102b6f5b57c14490bf68958e Mon Sep 17 00:00:00 2001 From: Bryann Valderrama Date: Thu, 12 Dec 2024 09:45:45 -0500 Subject: [PATCH 2/5] chore: remove try-except when running filter --- openedx/core/djangoapps/schedules/resolvers.py | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/openedx/core/djangoapps/schedules/resolvers.py b/openedx/core/djangoapps/schedules/resolvers.py index 7a966a575bec..a07f9c5de7f5 100644 --- a/openedx/core/djangoapps/schedules/resolvers.py +++ b/openedx/core/djangoapps/schedules/resolvers.py @@ -155,12 +155,9 @@ def get_schedules_with_target_date_by_bin_and_orgs( schedules = self.filter_by_org(schedules) - try: - # .. filter_implemented_name: ScheduleQuerySetRequested - # .. filter_type: org.openedx.learning.schedule.queryset.requested.v1 - schedules = ScheduleQuerySetRequested.run_filter(schedules) - except ScheduleQuerySetRequested.PreventScheduleQuerysetRequest as exc: - schedules = exc.schedules + # .. filter_implemented_name: ScheduleQuerySetRequested + # .. filter_type: org.openedx.learning.schedule.queryset.requested.v1 + schedules = ScheduleQuerySetRequested.run_filter(schedules) if "read_replica" in settings.DATABASES: schedules = schedules.using("read_replica") From 82ed56c1d1985928293f443f3357f9198fa6ae7f Mon Sep 17 00:00:00 2001 From: Bryann Valderrama Date: Thu, 12 Dec 2024 12:55:27 -0500 Subject: [PATCH 3/5] chore: upgrade openedx-filters to v1.12.0 --- requirements/edx/base.txt | 2 +- requirements/edx/development.txt | 2 +- requirements/edx/doc.txt | 2 +- requirements/edx/testing.txt | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/requirements/edx/base.txt b/requirements/edx/base.txt index f95ae8adb2c9..ccedfe5a0227 100644 --- a/requirements/edx/base.txt +++ b/requirements/edx/base.txt @@ -836,7 +836,7 @@ openedx-events==9.15.0 # edx-name-affirmation # event-tracking # ora2 -openedx-filters==1.11.0 +openedx-filters==1.12.0 # via # -r requirements/edx/kernel.in # lti-consumer-xblock diff --git a/requirements/edx/development.txt b/requirements/edx/development.txt index 75c533947407..0191c779d363 100644 --- a/requirements/edx/development.txt +++ b/requirements/edx/development.txt @@ -1390,7 +1390,7 @@ openedx-events==9.15.0 # edx-name-affirmation # event-tracking # ora2 -openedx-filters==1.11.0 +openedx-filters==1.12.0 # via # -r requirements/edx/doc.txt # -r requirements/edx/testing.txt diff --git a/requirements/edx/doc.txt b/requirements/edx/doc.txt index f7031d349784..dacd1efa6143 100644 --- a/requirements/edx/doc.txt +++ b/requirements/edx/doc.txt @@ -1005,7 +1005,7 @@ openedx-events==9.15.0 # edx-name-affirmation # event-tracking # ora2 -openedx-filters==1.11.0 +openedx-filters==1.12.0 # via # -r requirements/edx/base.txt # lti-consumer-xblock diff --git a/requirements/edx/testing.txt b/requirements/edx/testing.txt index 57a0dc6341ad..db1ed7889667 100644 --- a/requirements/edx/testing.txt +++ b/requirements/edx/testing.txt @@ -1050,7 +1050,7 @@ openedx-events==9.15.0 # edx-name-affirmation # event-tracking # ora2 -openedx-filters==1.11.0 +openedx-filters==1.12.0 # via # -r requirements/edx/base.txt # lti-consumer-xblock From dbf97ed8507d4905c40db30ed8de1a6495f0c201 Mon Sep 17 00:00:00 2001 From: Bryann Valderrama Date: Fri, 13 Dec 2024 12:33:02 -0500 Subject: [PATCH 4/5] test: add filter unit tests --- .../schedules/tests/test_filters.py | 61 +++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 openedx/core/djangoapps/schedules/tests/test_filters.py diff --git a/openedx/core/djangoapps/schedules/tests/test_filters.py b/openedx/core/djangoapps/schedules/tests/test_filters.py new file mode 100644 index 000000000000..8b80474c0f8d --- /dev/null +++ b/openedx/core/djangoapps/schedules/tests/test_filters.py @@ -0,0 +1,61 @@ +""" +Test cases for the Open edX Filters associated with the schedule app. +""" + +import datetime +from unittest.mock import Mock + +from django.test import TestCase, override_settings +from openedx_filters import PipelineStep + +from openedx.core.djangoapps.schedules.resolvers import BinnedSchedulesBaseResolver +from openedx.core.djangoapps.schedules.tests.test_resolvers import SchedulesResolverTestMixin +from openedx.core.djangolib.testing.utils import skip_unless_lms + + +class TestScheduleQuerySetRequestedPipelineStep(PipelineStep): + """Pipeline step class to test a configured pipeline step""" + + filtered_schedules = Mock() + + def run_filter(self, schedules): # pylint: disable=arguments-differ + """Pipeline step to filter the schedules""" + return { + "schedules": self.filtered_schedules, + } + + +@skip_unless_lms +class ScheduleQuerySetRequestedFiltersTest(SchedulesResolverTestMixin, TestCase): + """ + Tests for the Open edX Filters associated with the schedule queryset requested. + + The following filters are tested: + - ScheduleQuerySetRequested + """ + + def setUp(self): + super().setUp() + self.resolver = BinnedSchedulesBaseResolver( + async_send_task=Mock(name="async_send_task"), + site=self.site, + target_datetime=datetime.datetime.now(), + day_offset=3, + bin_num=2, + ) + + @override_settings( + OPEN_EDX_FILTERS_CONFIG={ + "org.openedx.learning.schedule.queryset.requested.v1": { + "pipeline": [ + "openedx.core.djangoapps.schedules.tests.test_filters.TestScheduleQuerySetRequestedPipelineStep", + ], + "fail_silently": False, + }, + }, + ) + def test_schedule_queryset_requested_filter(self) -> None: + """Test to verify the schedule queryset was modified by the pipeline step.""" + schedules = self.resolver.get_schedules_with_target_date_by_bin_and_orgs() + + self.assertEqual(TestScheduleQuerySetRequestedPipelineStep.filtered_schedules, schedules) From 1d46fa7b4242554247f09f8fb5773257fd2e186a Mon Sep 17 00:00:00 2001 From: Bryann Valderrama Date: Fri, 13 Dec 2024 15:47:48 -0500 Subject: [PATCH 5/5] test: inherit of ModuleStoreTestCase --- .../core/djangoapps/schedules/tests/test_filters.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/openedx/core/djangoapps/schedules/tests/test_filters.py b/openedx/core/djangoapps/schedules/tests/test_filters.py index 8b80474c0f8d..94235eb40012 100644 --- a/openedx/core/djangoapps/schedules/tests/test_filters.py +++ b/openedx/core/djangoapps/schedules/tests/test_filters.py @@ -5,18 +5,20 @@ import datetime from unittest.mock import Mock -from django.test import TestCase, override_settings +from django.db.models.query import QuerySet +from django.test import override_settings from openedx_filters import PipelineStep from openedx.core.djangoapps.schedules.resolvers import BinnedSchedulesBaseResolver from openedx.core.djangoapps.schedules.tests.test_resolvers import SchedulesResolverTestMixin from openedx.core.djangolib.testing.utils import skip_unless_lms +from xmodule.modulestore.tests.django_utils import ModuleStoreTestCase class TestScheduleQuerySetRequestedPipelineStep(PipelineStep): """Pipeline step class to test a configured pipeline step""" - filtered_schedules = Mock() + filtered_schedules = Mock(spec=QuerySet) def run_filter(self, schedules): # pylint: disable=arguments-differ """Pipeline step to filter the schedules""" @@ -26,7 +28,7 @@ def run_filter(self, schedules): # pylint: disable=arguments-differ @skip_unless_lms -class ScheduleQuerySetRequestedFiltersTest(SchedulesResolverTestMixin, TestCase): +class ScheduleQuerySetRequestedFiltersTest(SchedulesResolverTestMixin, ModuleStoreTestCase): """ Tests for the Open edX Filters associated with the schedule queryset requested. @@ -41,7 +43,7 @@ def setUp(self): site=self.site, target_datetime=datetime.datetime.now(), day_offset=3, - bin_num=2, + bin_num=1, ) @override_settings(