From d2c33c4b3e375fcaf43d4ea9a2c734b150a54b6e Mon Sep 17 00:00:00 2001 From: Feanil Patel Date: Fri, 2 Aug 2024 13:18:18 -0400 Subject: [PATCH] test: Update the course in the cache after it's got new content. Because signals are disabled by default for performance reasons, this doesn't happen automatically. So we manually refresh the course in the cache after all the changes have been made so that the course in the cache matches the latest version in the modulestore. --- .../transformers/tests/test_library_content.py | 5 +++++ .../grades/rest_api/v1/tests/test_gradebook_views.py | 7 +++++++ lms/djangoapps/grades/tests/integration/test_events.py | 4 ++++ lms/djangoapps/grades/tests/test_transformer.py | 2 ++ 4 files changed, 18 insertions(+) diff --git a/lms/djangoapps/course_blocks/transformers/tests/test_library_content.py b/lms/djangoapps/course_blocks/transformers/tests/test_library_content.py index 7fdb58f7f612..5a4d7a0de11a 100644 --- a/lms/djangoapps/course_blocks/transformers/tests/test_library_content.py +++ b/lms/djangoapps/course_blocks/transformers/tests/test_library_content.py @@ -8,6 +8,7 @@ from openedx.core.djangoapps.content.block_structure.api import clear_course_from_cache from openedx.core.djangoapps.content.block_structure.transformers import BlockStructureTransformers +import openedx.core.djangoapps.content.block_structure.api as bs_api from ...api import get_course_blocks from ..library_content import ContentLibraryOrderTransformer, ContentLibraryTransformer from .helpers import CourseStructureTestCase @@ -41,6 +42,8 @@ def setUp(self): self.course_hierarchy = self.get_course_hierarchy() self.blocks = self.build_course(self.course_hierarchy) self.course = self.blocks['course'] + # Do this manually because publish signals are not fired by default in tests. + bs_api.update_course_in_cache(self.course.id) clear_course_from_cache(self.course.id) # Enroll user in course. @@ -122,6 +125,7 @@ def test_content_library(self): ) assert len(list(raw_block_structure.get_block_keys())) == len(self.blocks) + bs_api.update_course_in_cache(self.course.id) clear_course_from_cache(self.course.id) trans_block_structure = get_course_blocks( self.user, @@ -175,6 +179,7 @@ def setUp(self): self.course_hierarchy = self.get_course_hierarchy() self.blocks = self.build_course(self.course_hierarchy) self.course = self.blocks['course'] + bs_api.update_course_in_cache(self.course.id) clear_course_from_cache(self.course.id) # Enroll user in course. diff --git a/lms/djangoapps/grades/rest_api/v1/tests/test_gradebook_views.py b/lms/djangoapps/grades/rest_api/v1/tests/test_gradebook_views.py index 5d7c428c2221..ab11daf36576 100644 --- a/lms/djangoapps/grades/rest_api/v1/tests/test_gradebook_views.py +++ b/lms/djangoapps/grades/rest_api/v1/tests/test_gradebook_views.py @@ -22,6 +22,7 @@ from xmodule.modulestore.tests.django_utils import SharedModuleStoreTestCase from xmodule.modulestore.tests.factories import CourseFactory, BlockFactory +import openedx.core.djangoapps.content.block_structure.api as bs_api from common.djangoapps.course_modes.models import CourseMode from common.djangoapps.student.roles import ( CourseBetaTesterRole, @@ -2228,6 +2229,12 @@ def test_get_override_for_unreleased_block(self): display_name='Unreleased Section', ) + # We need to update the course in the cache after we create the new block. + # Review Question: Should we be doing this here? Does it make sense to do + # this in the xmodule/modulestore/tests/factories.py BlockFactory class + # as a part of the publish there? + bs_api.update_course_in_cache(self.course_data.course_key) + resp = self.client.get( self.get_url(subsection_id=unreleased_subsection.location) ) diff --git a/lms/djangoapps/grades/tests/integration/test_events.py b/lms/djangoapps/grades/tests/integration/test_events.py index f058cc81799e..52c1f14bacd8 100644 --- a/lms/djangoapps/grades/tests/integration/test_events.py +++ b/lms/djangoapps/grades/tests/integration/test_events.py @@ -7,6 +7,7 @@ from crum import set_current_request +import openedx.core.djangoapps.content.block_structure.api as bs_api from xmodule.capa.tests.response_xml_factory import MultipleChoiceResponseXMLFactory from common.djangoapps.student.models import CourseEnrollment from common.djangoapps.student.tests.factories import UserFactory @@ -76,6 +77,9 @@ def setUp(self): CourseEnrollment.enroll(self.student, self.course.id) self.instructor = UserFactory.create(is_staff=True, username='test_instructor', password=self.TEST_PASSWORD) self.refresh_course() + # Since this doesn't happen automatically and we don't want to run all the publish signal handlers + # Just make sure we have the latest version of the course in cache before we test the problem. + bs_api.update_course_in_cache(self.course.id) @patch('lms.djangoapps.grades.events.tracker') def test_submit_answer(self, events_tracker): diff --git a/lms/djangoapps/grades/tests/test_transformer.py b/lms/djangoapps/grades/tests/test_transformer.py index b0cb6c0d49fb..7ef13e5b5b7c 100644 --- a/lms/djangoapps/grades/tests/test_transformer.py +++ b/lms/djangoapps/grades/tests/test_transformer.py @@ -13,6 +13,7 @@ from xmodule.modulestore.tests.django_utils import SharedModuleStoreTestCase from xmodule.modulestore.tests.factories import check_mongo_calls_range +import openedx.core.djangoapps.content.block_structure.api as bs_api from common.djangoapps.student.tests.factories import UserFactory from lms.djangoapps.course_blocks.api import get_course_blocks from lms.djangoapps.course_blocks.transformers.tests.helpers import CourseStructureTestCase @@ -462,6 +463,7 @@ def test_modulestore_performance(self, store_type, max_mongo_calls, min_mongo_ca ) with self.store.default_store(store_type): blocks = self.build_course(course) + bs_api.update_course_in_cache(blocks['course'].id) clear_course_from_cache(blocks['course'].id) with check_mongo_calls_range(max_mongo_calls, min_mongo_calls): get_course_blocks(self.student, blocks['course'].location, self.transformers)