Skip to content

Commit

Permalink
test: Update the course in the cache after it's got new content.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
feanil committed Aug 9, 2024
1 parent 29ebada commit a36e22e
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)
)
Expand Down
4 changes: 4 additions & 0 deletions lms/djangoapps/grades/tests/integration/test_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand Down
2 changes: 2 additions & 0 deletions lms/djangoapps/grades/tests/test_transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)

0 comments on commit a36e22e

Please sign in to comment.