From 9c29a2e1895ebc83734da45557a050db40b402d2 Mon Sep 17 00:00:00 2001 From: salmannawaz Date: Thu, 3 Dec 2026 15:28:09 +0500 Subject: [PATCH 1/5] refactor: upgrade to XBlock with mandatory OpaqueKey scope IDs Test edx-platform against the unreleased XBlock change that requires usage_id and def_id to be OpaqueKeys (openedx/XBlock#809), and adjust the places that relied on non-key scope IDs: - Stop passing a MemoryIdManager to the new XBlockRuntime; it was only a placeholder, as that runtime does not support asides yet. - Use a real UsageKey in StructuredTagsAsideTestCase. XBlock is temporarily installed from kdmccormick/XBlock@scope-id-types via [tool.uv.sources]. That fork has no git tags, so setuptools-scm would build it as version 0.0; extra-build-variables gives the build an explicit version. Both are marked TEMP and should be removed once openedx/XBlock#809 is released. Takes over #37935. Co-authored-by: Kyle McCormick Co-Authored-By: Claude Opus 5.5 (1M context) --- cms/lib/xblock/tagging/test.py | 12 +++++++----- openedx/core/djangoapps/xblock/runtime/runtime.py | 4 ++-- pyproject.toml | 10 ++++++++++ requirements/edx/base.txt | 2 +- requirements/edx/development.txt | 2 +- uv.lock | 8 ++------ 6 files changed, 23 insertions(+), 15 deletions(-) diff --git a/cms/lib/xblock/tagging/test.py b/cms/lib/xblock/tagging/test.py index be8bb62a6454..9f5040a05183 100644 --- a/cms/lib/xblock/tagging/test.py +++ b/cms/lib/xblock/tagging/test.py @@ -10,7 +10,7 @@ import ddt from django.test.client import RequestFactory from lxml import etree -from opaque_keys.edx.asides import AsideUsageKeyV1, AsideUsageKeyV2 +from opaque_keys.edx.asides import AsideUsageKeyV1, AsideUsageKeyV2, UsageKey from xblock.fields import ScopeIds from xblock.runtime import DictKeyValueStore, KvsFieldData from xblock.test.tools import TestRuntime @@ -122,10 +122,12 @@ def test_aside_contains_tags(self): """ Checks that available_tags list is not empty """ - sids = ScopeIds(user_id="bob", - block_type="bobs-type", - def_id="definition-id", - usage_id="usage-id") + sids = ScopeIds( + user_id="bob", + block_type="bobs-type", + def_id="definition-id", + usage_id=UsageKey.from_string("block-v1:myOrg+myCourse+myRun+type@bobs-type+block@usage-id"), + ) key_store = DictKeyValueStore() field_data = KvsFieldData(key_store) runtime = TestRuntime(services={'field-data': field_data}) diff --git a/openedx/core/djangoapps/xblock/runtime/runtime.py b/openedx/core/djangoapps/xblock/runtime/runtime.py index dc5d0696ee62..2a437a2e1360 100644 --- a/openedx/core/djangoapps/xblock/runtime/runtime.py +++ b/openedx/core/djangoapps/xblock/runtime/runtime.py @@ -19,7 +19,7 @@ from xblock.exceptions import NoSuchServiceError from xblock.field_data import DictFieldData, FieldData, SplitFieldData from xblock.fields import Scope, ScopeIds -from xblock.runtime import IdReader, KvsFieldData, MemoryIdManager, Runtime +from xblock.runtime import IdReader, KvsFieldData, Runtime from common.djangoapps.edxmako.services import MakoService from common.djangoapps.static_replace.services import ReplaceURLService @@ -130,7 +130,7 @@ def __init__( ), default_class=None, select=None, - id_generator=MemoryIdManager(), # We don't really use id_generator until we need to support asides + id_generator=None, # We won't need an id_generator until we need to support asides ) assert student_data_mode in (StudentDataMode.Ephemeral, StudentDataMode.Persisted) self.authored_data_mode = authored_data_mode diff --git a/pyproject.toml b/pyproject.toml index 12978604e9cb..19e35bb7fe18 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -258,6 +258,10 @@ default-groups = [] # explicit, not omitted -- uv's own default-groups fallback # tested at once, list them here as one set, e.g. [[{ group = "django52" }, { group = "django60" }]], # otherwise uv refuses to resolve both into one lock. conflicts = [] +# TEMP: Kyle's XBlock fork (see [tool.uv.sources] below) has no git tags, so setuptools-scm +# would build it as version 0.0. Give the build a version explicitly. Remove together +# with the xblock source once https://github.com/openedx/XBlock/pull/809 is released. +extra-build-variables = { xblock = { SETUPTOOLS_SCM_PRETEND_VERSION = "6.3.5" } } # DO NOT EDIT constraint-dependencies DIRECTLY. # This list is managed by `edx_lint write_uv_constraints` and will be @@ -290,6 +294,12 @@ constraint-dependencies = [ "pytest<9.2.0", "lti-consumer-xblock==11.5.1", ] + +[tool.uv.sources] +# TEMP: test against the unreleased XBlock change that mandates OpaqueKey scope IDs +# (https://github.com/openedx/XBlock/pull/809). Remove once it is released to PyPI. +xblock = { git = "https://github.com/kdmccormick/XBlock.git", branch = "kdmccormick/scope-id-types" } + [tool.edx_lint] uv_constraints = [ # Date: 2025-10-07 diff --git a/requirements/edx/base.txt b/requirements/edx/base.txt index 1c558914f6cd..aa4d8794f770 100644 --- a/requirements/edx/base.txt +++ b/requirements/edx/base.txt @@ -1288,7 +1288,7 @@ wrapt==2.5.2 # via # openedx-platform # xblocks-core -xblock==6.3.5 +xblock @ git+https://github.com/kdmccormick/XBlock.git@ad65b7255ab68b3f274c048ea8a79a91a129aea3 # via # acid-xblock # crowdsourcehinter-xblock diff --git a/requirements/edx/development.txt b/requirements/edx/development.txt index 62fd6f0a790b..056af5c5cac0 100644 --- a/requirements/edx/development.txt +++ b/requirements/edx/development.txt @@ -1572,7 +1572,7 @@ wrapt==2.5.2 # via # openedx-platform # xblocks-core -xblock==6.3.5 +xblock @ git+https://github.com/kdmccormick/XBlock.git@ad65b7255ab68b3f274c048ea8a79a91a129aea3 # via # acid-xblock # crowdsourcehinter-xblock diff --git a/uv.lock b/uv.lock index 0a53ff806aaf..75c888e7bf9f 100644 --- a/uv.lock +++ b/uv.lock @@ -4972,7 +4972,7 @@ requires-dist = [ { name = "web-fragments" }, { name = "webob" }, { name = "wrapt" }, - { name = "xblock", extras = ["django"] }, + { name = "xblock", extras = ["django"], git = "https://github.com/kdmccormick/XBlock.git?branch=kdmccormick%2Fscope-id-types" }, { name = "xss-utils" }, ] @@ -8120,7 +8120,7 @@ wheels = [ [[package]] name = "xblock" version = "6.3.5" -source = { registry = "https://pypi.org/simple" } +source = { git = "https://github.com/kdmccormick/XBlock.git?branch=kdmccormick%2Fscope-id-types#ad65b7255ab68b3f274c048ea8a79a91a129aea3" } dependencies = [ { name = "edx-opaque-keys" }, { name = "fs" }, @@ -8133,10 +8133,6 @@ dependencies = [ { name = "simplejson" }, { name = "webob" }, ] -sdist = { url = "https://files.pythonhosted.org/packages/5b/c2/26b295d703beeb34e1b051d5842699ef80814163faa96d0f1875464092e5/xblock-6.3.5.tar.gz", hash = "sha256:e2ca8389b4d7ca414bfb1bceb3888e7d78d7223acb684d369c1e76455c64874f", size = 1228312, upload-time = "2026-11-02T19:02:15.24Z" } -wheels = [ - { url = "https://files.pythonhosted.org/packages/e4/ff/76a4984d5632f0e270caca666d4868dff88c4d5e1dd96fc471540afe1f61/xblock-6.3.5-py3-none-any.whl", hash = "sha256:d57d6b162645fa5574deb3d3111914d01aaeddea78aface97e621183349d3f2f", size = 160955, upload-time = "2026-11-02T19:02:13.712Z" }, -] [package.optional-dependencies] django = [ From 890c64dd79455e81f3125d48b7fa3d26c132a5c7 Mon Sep 17 00:00:00 2001 From: salmannawaz Date: Thu, 3 Dec 2026 16:11:05 +0500 Subject: [PATCH 2/5] test: use a real key for def_id in StructuredTagsAsideTestCase The XBlock scope-id-types branch now also rejects raw string definition IDs, so pass the UsageKey as def_id as well. Co-Authored-By: Claude Opus 5.5 (1M context) --- cms/lib/xblock/tagging/test.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/cms/lib/xblock/tagging/test.py b/cms/lib/xblock/tagging/test.py index 9f5040a05183..6610ccbeb176 100644 --- a/cms/lib/xblock/tagging/test.py +++ b/cms/lib/xblock/tagging/test.py @@ -122,11 +122,12 @@ def test_aside_contains_tags(self): """ Checks that available_tags list is not empty """ + usage_key = UsageKey.from_string("block-v1:myOrg+myCourse+myRun+type@bobs-type+block@usage-id") sids = ScopeIds( user_id="bob", block_type="bobs-type", - def_id="definition-id", - usage_id=UsageKey.from_string("block-v1:myOrg+myCourse+myRun+type@bobs-type+block@usage-id"), + def_id=usage_key, + usage_id=usage_key, ) key_store = DictKeyValueStore() field_data = KvsFieldData(key_store) From e73071b8666afee04d5f0db5222e01c45b1f2780 Mon Sep 17 00:00:00 2001 From: salmannawaz Date: Thu, 3 Dec 2026 16:26:23 +0500 Subject: [PATCH 3/5] test: pass valid ScopeIds in video and discussion test setup The XBlock scope-id-types branch validates ScopeIds, so: - give the mock user from get_test_system/prepare_block_runtime a real integer id, since it becomes the user_id of the asides it creates; - pass 'video' as the block_type in test_video.instantiate_block; - build real ScopeIds in TestDiscussionXBlock instead of a Mock. Co-Authored-By: Claude Opus 5.5 (1M context) --- lms/djangoapps/courseware/tests/test_discussion_xblock.py | 5 +++-- xmodule/tests/__init__.py | 4 ++-- xmodule/tests/test_video.py | 2 +- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/lms/djangoapps/courseware/tests/test_discussion_xblock.py b/lms/djangoapps/courseware/tests/test_discussion_xblock.py index a94286156d73..99e79450c34d 100644 --- a/lms/djangoapps/courseware/tests/test_discussion_xblock.py +++ b/lms/djangoapps/courseware/tests/test_discussion_xblock.py @@ -18,6 +18,7 @@ from opaque_keys.edx.keys import CourseKey from web_fragments.fragment import Fragment from xblock.field_data import DictFieldData +from xblock.fields import ScopeIds from xblock_discussion import DiscussionXBlock from common.djangoapps.student.tests.factories import CourseEnrollmentFactory, UserFactory @@ -52,8 +53,8 @@ def setUp(self): self.data = DictFieldData({ 'discussion_id': self.discussion_id }) - scope_ids = mock.Mock() - scope_ids.usage_id.context_key = self.course_id + usage_key = self.course_id.make_usage_key('discussion', 'test_discussion') + scope_ids = ScopeIds(None, 'discussion', usage_key, usage_key) self.block = DiscussionXBlock( self.runtime, field_data=self.data, diff --git a/xmodule/tests/__init__.py b/xmodule/tests/__init__.py index ded0dfc43db7..419974f06c66 100644 --- a/xmodule/tests/__init__.py +++ b/xmodule/tests/__init__.py @@ -126,7 +126,7 @@ def get_test_system( descriptor_system = get_test_descriptor_system(id_reader=id_manager, id_generator=id_manager) if not user: - user = Mock(name='get_test_system.user', is_staff=False) + user = Mock(name='get_test_system.user', is_staff=False, id=1) if not user_location: user_location = Mock(name='get_test_system.user_location') user_service = StubUserService( @@ -185,7 +185,7 @@ def prepare_block_runtime( """ if not user: - user = Mock(name='get_test_system.user', is_staff=False) + user = Mock(name='get_test_system.user', is_staff=False, id=1) if not user_location: user_location = Mock(name='get_test_system.user_location') user_service = StubUserService( diff --git a/xmodule/tests/test_video.py b/xmodule/tests/test_video.py index 7829fc24ca28..59ce4cad8800 100644 --- a/xmodule/tests/test_video.py +++ b/xmodule/tests/test_video.py @@ -95,7 +95,7 @@ def instantiate_block(**field_data): usage_key = course_key.make_usage_key('video', 'SampleProblem') return system.construct_xblock_from_class( VideoBlock, - scope_ids=ScopeIds(None, None, usage_key, usage_key), + scope_ids=ScopeIds(None, 'video', usage_key, usage_key), field_data=DictFieldData(field_data), ) From 99162e33d70136df452f0f970c6c8a181c943b44 Mon Sep 17 00:00:00 2001 From: salmannawaz Date: Fri, 4 Dec 2026 14:59:56 +0500 Subject: [PATCH 4/5] test: build valid ScopeIds across xmodule tests The XBlock scope-id-types branch validates ScopeIds when a block is created, so tests can no longer pass a Mock, a None block_type, or raw strings for the IDs. - Add make_test_scope_ids(), which builds ScopeIds through a CourseLocationManager id generator, as runtime code does, and use it in place of Mock() and string IDs. - Pass real block types where ScopeIds had block_type=None. - Use a real CourseLocationManager in place of Mock id generators. - Pass the course's ScopeIds, not its usage_id, to _prepare_asides. - Update test_unit_block for XBlock's new test runtime usage-id format. Co-Authored-By: Claude Opus 5.5 (1M context) --- cms/lib/xblock/tagging/test.py | 14 ++++----- .../tests/test_discussion_xblock.py | 5 ++-- .../video_config/tests/test_services.py | 2 +- xmodule/modulestore/tests/test_api.py | 3 +- xmodule/modulestore/tests/test_inheritance.py | 4 +-- xmodule/modulestore/tests/test_semantics.py | 4 +-- .../modulestore/tests/test_xml_importer.py | 6 ++-- xmodule/tests/__init__.py | 12 ++++++++ xmodule/tests/test_annotatable_block.py | 2 +- xmodule/tests/test_capa_block.py | 6 ++-- xmodule/tests/test_conditional.py | 6 ++-- xmodule/tests/test_delay_between_attempts.py | 2 +- xmodule/tests/test_html_block.py | 30 ++++++++----------- xmodule/tests/test_lti20_unit.py | 4 +-- xmodule/tests/test_lti_unit.py | 2 +- xmodule/tests/test_sequence.py | 4 +-- xmodule/tests/test_unit_block.py | 6 ++-- xmodule/tests/test_word_cloud.py | 15 +++++----- xmodule/tests/test_xml_block.py | 11 +++---- xmodule/tests/xml/__init__.py | 2 +- 20 files changed, 74 insertions(+), 66 deletions(-) diff --git a/cms/lib/xblock/tagging/test.py b/cms/lib/xblock/tagging/test.py index 6610ccbeb176..6e0607dfd266 100644 --- a/cms/lib/xblock/tagging/test.py +++ b/cms/lib/xblock/tagging/test.py @@ -10,8 +10,8 @@ import ddt from django.test.client import RequestFactory from lxml import etree -from opaque_keys.edx.asides import AsideUsageKeyV1, AsideUsageKeyV2, UsageKey -from xblock.fields import ScopeIds +from opaque_keys.edx.asides import AsideUsageKeyV1, AsideUsageKeyV2 +from opaque_keys.edx.keys import CourseKey from xblock.runtime import DictKeyValueStore, KvsFieldData from xblock.test.tools import TestRuntime @@ -31,6 +31,7 @@ BlockFactory, CourseFactory, ) +from xmodule.tests import make_test_scope_ids # pylint: disable=wrong-import-order @ddt.ddt @@ -122,12 +123,11 @@ def test_aside_contains_tags(self): """ Checks that available_tags list is not empty """ - usage_key = UsageKey.from_string("block-v1:myOrg+myCourse+myRun+type@bobs-type+block@usage-id") - sids = ScopeIds( + sids = make_test_scope_ids( + "bobs-type", + "usage-id", + course_key=CourseKey.from_string("course-v1:myOrg+myCourse+myRun"), user_id="bob", - block_type="bobs-type", - def_id=usage_key, - usage_id=usage_key, ) key_store = DictKeyValueStore() field_data = KvsFieldData(key_store) diff --git a/lms/djangoapps/courseware/tests/test_discussion_xblock.py b/lms/djangoapps/courseware/tests/test_discussion_xblock.py index 99e79450c34d..5bdd012483ac 100644 --- a/lms/djangoapps/courseware/tests/test_discussion_xblock.py +++ b/lms/djangoapps/courseware/tests/test_discussion_xblock.py @@ -18,7 +18,6 @@ from opaque_keys.edx.keys import CourseKey from web_fragments.fragment import Fragment from xblock.field_data import DictFieldData -from xblock.fields import ScopeIds from xblock_discussion import DiscussionXBlock from common.djangoapps.student.tests.factories import CourseEnrollmentFactory, UserFactory @@ -29,6 +28,7 @@ from openedx.core.djangoapps.discussions.services import DiscussionConfigService from xmodule.modulestore.tests.django_utils import TEST_DATA_SPLIT_MODULESTORE, SharedModuleStoreTestCase from xmodule.modulestore.tests.factories import BlockFactory, ToyCourseFactory +from xmodule.tests import make_test_scope_ids from xmodule.tests.helpers import mock_render_template @@ -53,8 +53,7 @@ def setUp(self): self.data = DictFieldData({ 'discussion_id': self.discussion_id }) - usage_key = self.course_id.make_usage_key('discussion', 'test_discussion') - scope_ids = ScopeIds(None, 'discussion', usage_key, usage_key) + scope_ids = make_test_scope_ids('discussion', 'test_discussion', course_key=self.course_id) self.block = DiscussionXBlock( self.runtime, field_data=self.data, diff --git a/openedx/core/djangoapps/video_config/tests/test_services.py b/openedx/core/djangoapps/video_config/tests/test_services.py index 5f589bfde749..e22182ec2c88 100644 --- a/openedx/core/djangoapps/video_config/tests/test_services.py +++ b/openedx/core/djangoapps/video_config/tests/test_services.py @@ -28,7 +28,7 @@ def instantiate_video_block(self, **field_data): usage_key = course_key.make_usage_key('video', 'SampleProblem') return system.construct_xblock_from_class( VideoBlock, - scope_ids=ScopeIds(None, None, usage_key, usage_key), + scope_ids=ScopeIds(None, 'video', usage_key, usage_key), field_data=DictFieldData(field_data), ) diff --git a/xmodule/modulestore/tests/test_api.py b/xmodule/modulestore/tests/test_api.py index 9b6e32fa45dd..d94b1bfcf699 100644 --- a/xmodule/modulestore/tests/test_api.py +++ b/xmodule/modulestore/tests/test_api.py @@ -17,6 +17,7 @@ get_root_module_name, get_xblock_root_module_name, ) +from xmodule.tests import make_test_scope_ids from xmodule.video_block import VideoBlock @@ -45,7 +46,7 @@ def test_get_xblock_root_module_name(): }) runtime = TestRuntime(Mock(), mixins=[TestSimpleMixin], services={'field-data': field_data}) - mixed_done_xblock = runtime.construct_xblock_from_class(DoneXBlock, Mock()) + mixed_done_xblock = runtime.construct_xblock_from_class(DoneXBlock, make_test_scope_ids('done')) assert mixed_done_xblock.__module__ == 'xblock.core' assert mixed_done_xblock.unmixed_class == DoneXBlock, 'The unmixed_class property retains the original property.' diff --git a/xmodule/modulestore/tests/test_inheritance.py b/xmodule/modulestore/tests/test_inheritance.py index 11fe96210f58..bf866d3bd272 100644 --- a/xmodule/modulestore/tests/test_inheritance.py +++ b/xmodule/modulestore/tests/test_inheritance.py @@ -9,10 +9,10 @@ import ddt from django.utils.timezone import now, timedelta from xblock.core import XBlock -from xblock.fields import ScopeIds from xblock.test.tools import TestRuntime from xmodule.modulestore.inheritance import InheritanceMixin +from xmodule.tests import make_test_scope_ids class TestXBlock(XBlock): @@ -36,7 +36,7 @@ def setUp(self): Mock(entry_point=XBlock.entry_point), mixins=[InheritanceMixin], services={'field-data': {}} ) self.xblock = runtime.construct_xblock_from_class( - TestXBlock, ScopeIds('user', 'TestXBlock', 'def_id', 'usage_id') + TestXBlock, make_test_scope_ids('TestXBlock', user_id='user') ) super().setUp() diff --git a/xmodule/modulestore/tests/test_semantics.py b/xmodule/modulestore/tests/test_semantics.py index 9b5899ac00d5..799d3baef42f 100644 --- a/xmodule/modulestore/tests/test_semantics.py +++ b/xmodule/modulestore/tests/test_semantics.py @@ -276,12 +276,12 @@ def _do_create(self, block_type, with_asides=False): block.fields[test_data.field_name].write_to(block, initial_field_value) asides = [] if with_asides: - asides = self._prepare_asides(self.course.scope_ids.usage_id) + asides = self._prepare_asides(self.course.scope_ids) self.store.update_item(block, ModuleStoreEnum.UserID.test, asides=asides, allow_not_found=True) else: asides = [] if with_asides: - asides = self._prepare_asides(self.course.scope_ids.usage_id) + asides = self._prepare_asides(self.course.scope_ids) self.store.create_child( user_id=ModuleStoreEnum.UserID.test, parent_usage_key=self.course.scope_ids.usage_id, diff --git a/xmodule/modulestore/tests/test_xml_importer.py b/xmodule/modulestore/tests/test_xml_importer.py index 8f18907f2c2f..228b6594c361 100644 --- a/xmodule/modulestore/tests/test_xml_importer.py +++ b/xmodule/modulestore/tests/test_xml_importer.py @@ -13,14 +13,14 @@ from opaque_keys.edx.keys import CourseKey from opaque_keys.edx.locator import BlockUsageLocator, CourseLocator from path import Path as path -from xblock.fields import List, Scope, ScopeIds, String +from xblock.fields import List, Scope, String from xblock.runtime import DictKeyValueStore, KvsFieldData, Runtime from xmodule.modulestore import ModuleStoreEnum from xmodule.modulestore.inheritance import InheritanceMixin from xmodule.modulestore.tests.mongo_connection import MONGO_HOST, MONGO_PORT_NUM from xmodule.modulestore.xml_importer import StaticContentImporter, _update_and_import_block, _update_block_location -from xmodule.tests import DATA_DIR +from xmodule.tests import DATA_DIR, make_test_scope_ids from xmodule.x_module import XModuleMixin OPEN_BUILTIN = 'builtins.open' @@ -156,7 +156,7 @@ def setUp(self): """ self.runtime = mock.MagicMock(Runtime) self.field_data = KvsFieldData(kvs=DictKeyValueStore()) - self.scope_ids = ScopeIds('Bob', 'mutablestubxblock', '123', 'import') + self.scope_ids = make_test_scope_ids('mutablestubxblock', 'import', user_id='Bob') self.xblock = StubXBlockWithMutableFields(self.runtime, self.field_data, self.scope_ids) self.fake_children_locations = [ diff --git a/xmodule/tests/__init__.py b/xmodule/tests/__init__.py index 419974f06c66..b9e32cbd0205 100644 --- a/xmodule/tests/__init__.py +++ b/xmodule/tests/__init__.py @@ -241,6 +241,18 @@ def get_block(block): return runtime +def make_test_scope_ids(block_type, block_id='test_block', course_key=None, user_id=None): + """ + Return valid ScopeIds for a test block of the given type. + + XBlock validates ScopeIds, so tests can no longer pass a Mock or None for them. + """ + id_generator = CourseLocationManager(course_key or CourseKey.from_string('org/course/run')) + def_id = id_generator.create_definition(block_type, block_id) + usage_id = id_generator.create_usage(def_id) + return ScopeIds(user_id, block_type, def_id, usage_id) + + def get_test_descriptor_system(render_template=None, **kwargs): """ Construct a test ModuleStoreRuntime instance. diff --git a/xmodule/tests/test_annotatable_block.py b/xmodule/tests/test_annotatable_block.py index d1ea0087b4b1..6177e16f6803 100644 --- a/xmodule/tests/test_annotatable_block.py +++ b/xmodule/tests/test_annotatable_block.py @@ -43,7 +43,7 @@ def setUp(self): self.annotatable = self.annotatable_class( get_test_system(), DictFieldData({'data': self.sample_xml}), - ScopeIds(None, None, None, BlockUsageLocator(CourseLocator('org', 'course', 'run'), 'category', 'name')) + ScopeIds(None, 'category', None, BlockUsageLocator(CourseLocator('org', 'course', 'run'), 'category', 'name')) ) def test_annotation_data_attr(self): diff --git a/xmodule/tests/test_capa_block.py b/xmodule/tests/test_capa_block.py index c2179e20a4f0..19424f0e2761 100644 --- a/xmodule/tests/test_capa_block.py +++ b/xmodule/tests/test_capa_block.py @@ -37,7 +37,7 @@ from xmodule.tests import DATA_DIR from ..capa_block import RANDOMIZATION, SHOWANSWER -from . import get_test_system +from . import get_test_system, make_test_scope_ids class CapaFactory: @@ -4046,7 +4046,7 @@ def _user_state(self, username="testuser", response_count=10, suffix=""): def _get_block(self): """Create and return a mock ProblemBlock with default test data.""" - scope_ids = Mock(block_type="problem") + scope_ids = make_test_scope_ids('problem') block = ProblemBlock(get_test_system(), scope_ids=scope_ids) block.runtime = Mock() block.data = "" @@ -4054,7 +4054,7 @@ def _get_block(self): def test_generate_report_data_not_implemented(self): """Verify report generation is not supported for non-problem blocks.""" - scope_ids = Mock(block_type="noproblem") + scope_ids = make_test_scope_ids('noproblem') block = ProblemBlock(get_test_system(), scope_ids=scope_ids) with pytest.raises(NotImplementedError): next(block.generate_report_data(iter([]))) diff --git a/xmodule/tests/test_conditional.py b/xmodule/tests/test_conditional.py index d5fb9b0c254b..71594b273cab 100644 --- a/xmodule/tests/test_conditional.py +++ b/xmodule/tests/test_conditional.py @@ -129,7 +129,7 @@ def load_item(usage_id, for_parent=None): # pylint: disable=unused-argument cond_block = ConditionalBlock( system, field_data, - ScopeIds(None, None, cond_location, cond_location) + ScopeIds(None, 'conditional', cond_location, cond_location) ) system.get_block_for_descriptor = lambda desc: desc if visible_to_nonstaff_users(desc) else None cond_block.get_required_blocks = [ @@ -281,7 +281,7 @@ def test_conditional_block_with_empty_sources_list(self): dummy_system = Mock() dummy_location = BlockUsageLocator(CourseLocator("edX", "conditional_test", "test_run"), "conditional", "SampleConditional") - dummy_scope_ids = ScopeIds(None, None, dummy_location, dummy_location) + dummy_scope_ids = ScopeIds(None, 'conditional', dummy_location, dummy_location) dummy_field_data = DictFieldData({ 'data': '', 'xml_attributes': {'sources': 'i4x://HarvardX/ER22x/poll_question/T15_poll'}, @@ -301,7 +301,7 @@ def test_conditional_block_parse_sources(self): dummy_system = Mock() dummy_location = BlockUsageLocator(CourseLocator("edX", "conditional_test", "test_run"), "conditional", "SampleConditional") - dummy_scope_ids = ScopeIds(None, None, dummy_location, dummy_location) + dummy_scope_ids = ScopeIds(None, 'conditional', dummy_location, dummy_location) dummy_field_data = DictFieldData({ 'data': '', 'xml_attributes': {'sources': 'i4x://HarvardX/ER22x/poll_question/T15_poll;i4x://HarvardX/ER22x/poll_question/T16_poll'}, # pylint: disable=line-too-long diff --git a/xmodule/tests/test_delay_between_attempts.py b/xmodule/tests/test_delay_between_attempts.py index bc2d074df91b..4998ae7c029b 100644 --- a/xmodule/tests/test_delay_between_attempts.py +++ b/xmodule/tests/test_delay_between_attempts.py @@ -107,7 +107,7 @@ def create( block = ProblemBlock( system, DictFieldData(field_data), - ScopeIds(None, None, location, location), + ScopeIds(None, 'problem', location, location), ) if correct: diff --git a/xmodule/tests/test_html_block.py b/xmodule/tests/test_html_block.py index 53ca238cbcf6..6297303abedd 100644 --- a/xmodule/tests/test_html_block.py +++ b/xmodule/tests/test_html_block.py @@ -7,7 +7,7 @@ from django.contrib.auth.models import AnonymousUser from django.test import TestCase from django.test.utils import override_settings -from opaque_keys.edx.locator import BlockUsageLocator, CourseLocator +from opaque_keys.edx.locator import CourseLocator from xblock.field_data import DictFieldData from xblock.fields import ScopeIds @@ -15,7 +15,7 @@ from xmodule.html_block import CourseInfoBlock from ..x_module import PUBLIC_VIEW, STUDENT_VIEW -from . import get_test_descriptor_system, get_test_system +from . import get_test_descriptor_system, get_test_system, make_test_scope_ids @ddt.ddt @@ -42,7 +42,7 @@ def test_disabled(self, settings): """ field_data = DictFieldData({'data': '

Some HTML

'}) module_system = get_test_system() - block = self.html_class(module_system, field_data, Mock()) + block = self.html_class(module_system, field_data, make_test_scope_ids('html')) with override_settings(**settings): assert block.student_view_data() ==\ @@ -70,7 +70,7 @@ def test_common_values(self, html): """ field_data = DictFieldData({'data': html}) module_system = get_test_system() - block = self.html_class(module_system, field_data, Mock()) + block = self.html_class(module_system, field_data, make_test_scope_ids('html')) assert block.student_view_data() == dict(enabled=True, html=html) @ddt.data( @@ -84,7 +84,7 @@ def test_student_preview_view(self, view): html = '

This is a test

' field_data = DictFieldData({'data': html}) module_system = get_test_system() - block = self.html_class(module_system, field_data, Mock()) + block = self.html_class(module_system, field_data, make_test_scope_ids('html')) rendered = module_system.render(block, view, {}).content assert html in rendered @@ -102,25 +102,19 @@ def test_substitution_user_id(self): sample_xml = '''%%USER_ID%%''' field_data = DictFieldData({'data': sample_xml}) module_system = get_test_system() - block = self.html_class(module_system, field_data, Mock()) + block = self.html_class(module_system, field_data, make_test_scope_ids('html')) assert block.get_html() == str(module_system.anonymous_student_id) def test_substitution_course_id(self): sample_xml = '''%%COURSE_ID%%''' field_data = DictFieldData({'data': sample_xml}) module_system = get_test_system() - block = self.html_class(module_system, field_data, Mock()) course_key = CourseLocator( org='some_org', course='some_course', run='some_run' ) - usage_key = BlockUsageLocator( - course_key=course_key, - block_type='problem', - block_id='block_id' - ) - block.scope_ids.usage_id = usage_key + block = self.html_class(module_system, field_data, make_test_scope_ids('html', course_key=course_key)) assert block.get_html() == str(course_key) def test_substitution_without_magic_string(self): @@ -131,14 +125,14 @@ def test_substitution_without_magic_string(self): ''' field_data = DictFieldData({'data': sample_xml}) module_system = get_test_system() - block = self.html_class(module_system, field_data, Mock()) + block = self.html_class(module_system, field_data, make_test_scope_ids('html')) assert block.get_html() == sample_xml def test_substitution_without_anonymous_student_id(self): sample_xml = '''%%USER_ID%%''' field_data = DictFieldData({'data': sample_xml}) module_system = get_test_system(user=AnonymousUser()) - block = self.html_class(module_system, field_data, Mock()) + block = self.html_class(module_system, field_data, make_test_scope_ids('html')) block.runtime.service(block, 'user')._deprecated_anonymous_user_id = '' # pylint: disable=protected-access assert block.get_html() == sample_xml @@ -164,7 +158,7 @@ def instantiate_block(self, **field_data): usage_key = course_key.make_usage_key('html', 'SampleHtml') return system.construct_xblock_from_class( self.html_class, - scope_ids=ScopeIds(None, None, usage_key, usage_key), + scope_ids=ScopeIds(None, 'html', usage_key, usage_key), field_data=DictFieldData(field_data), ) @@ -271,7 +265,7 @@ def test_updates_render(self): info_block = CourseInfoBlock( get_test_system(), DictFieldData({'items': sample_update_data, 'data': ""}), - Mock() + make_test_scope_ids('course_info') ) # Prior to TNL-4115, an exception would be raised when trying to parse invalid dates in this method @@ -307,7 +301,7 @@ def test_updates_order(self): info_block = CourseInfoBlock( Mock(), DictFieldData({'items': sample_update_data, 'data': ""}), - Mock() + make_test_scope_ids('course_info') ) # This is the expected context that should be used by the render function diff --git a/xmodule/tests/test_lti20_unit.py b/xmodule/tests/test_lti20_unit.py index c5657362dffe..cd202dd1c0ee 100644 --- a/xmodule/tests/test_lti20_unit.py +++ b/xmodule/tests/test_lti20_unit.py @@ -15,7 +15,7 @@ from xmodule.lti_2_util import LTIError as BuiltInLTIError from xmodule.tests.helpers import StubUserService -from . import get_test_system +from . import get_test_system, make_test_scope_ids class _LTI20RESTResultServiceTestBase(TestCase): @@ -41,7 +41,7 @@ def setUp(self): self.runtime.publish = Mock() self.runtime._services['rebind_user'] = Mock() # pylint: disable=protected-access - self.xblock = self.lti_class(self.runtime, DictFieldData({}), Mock()) + self.xblock = self.lti_class(self.runtime, DictFieldData({}), make_test_scope_ids('lti')) self.lti_id = self.xblock.lti_id self.xblock.due = None self.xblock.graceperiod = None diff --git a/xmodule/tests/test_lti_unit.py b/xmodule/tests/test_lti_unit.py index 479f5df46817..46444e62df4a 100644 --- a/xmodule/tests/test_lti_unit.py +++ b/xmodule/tests/test_lti_unit.py @@ -79,7 +79,7 @@ def setUp(self): self.xblock = self.lti_class( self.runtime, DictFieldData({}), - ScopeIds(None, None, None, BlockUsageLocator(self.course_id, 'lti', 'name')) + ScopeIds(None, 'lti', None, BlockUsageLocator(self.course_id, 'lti', 'name')) ) current_user = self.runtime.service(self.xblock, 'user').get_current_user() self.user_id = current_user.opt_attrs.get(ATTR_KEY_ANONYMOUS_USER_ID) diff --git a/xmodule/tests/test_sequence.py b/xmodule/tests/test_sequence.py index 3901fc7a09e5..f33534a67b88 100644 --- a/xmodule/tests/test_sequence.py +++ b/xmodule/tests/test_sequence.py @@ -19,7 +19,7 @@ from openedx.features.content_type_gating.models import ContentTypeGatingConfig from xmodule.seq_block import TIMED_EXAM_GATING_WAFFLE_FLAG, SequenceBlock -from xmodule.tests import get_test_system, prepare_block_runtime +from xmodule.tests import get_test_system, make_test_scope_ids, prepare_block_runtime from xmodule.tests.helpers import StubUserService from xmodule.tests.xml import XModuleXmlImportTest from xmodule.tests.xml import factories as xml @@ -136,7 +136,7 @@ def _assert_view_at_position(self, rendered_html, expected_position): def test_student_view_init(self): runtime = get_test_system() runtime.position = 2 - seq_block = SequenceBlock(runtime=runtime, scope_ids=Mock()) + seq_block = SequenceBlock(runtime=runtime, scope_ids=make_test_scope_ids('sequential')) seq_block.bind_for_student(34) assert seq_block.position == 2 # matches position set in the runtime diff --git a/xmodule/tests/test_unit_block.py b/xmodule/tests/test_unit_block.py index 33ce9e9bef5d..24032010048d 100644 --- a/xmodule/tests/test_unit_block.py +++ b/xmodule/tests/test_unit_block.py @@ -53,12 +53,12 @@ def test_unit_html(self): html = block.runtime.render(block, 'student_view').content self.assertXmlEqual(html, ( - '
' + '
' '
' - '
' + '
' 'This is some HTML.' '
' - '
' + '
' '' '
' diff --git a/xmodule/tests/test_word_cloud.py b/xmodule/tests/test_word_cloud.py index c76ed541a433..36a76ba25e94 100644 --- a/xmodule/tests/test_word_cloud.py +++ b/xmodule/tests/test_word_cloud.py @@ -1,6 +1,5 @@ """Test for Word Cloud Block functional logic.""" import json -from unittest.mock import Mock from django.test import TestCase from fs.memoryfs import MemoryFS @@ -11,7 +10,9 @@ from xblock.fields import ScopeIds from xblock_word_cloud import WordCloudBlock -from . import get_test_descriptor_system, get_test_system +from xmodule.modulestore.xml import CourseLocationManager + +from . import get_test_descriptor_system, get_test_system, make_test_scope_ids class _TestWordCloudBase(TestCase): @@ -48,7 +49,7 @@ def test_xml_import_export_cycle(self): ) olx_element = etree.fromstring(original_xml) - runtime.id_generator = Mock() + runtime.id_generator = CourseLocationManager(CourseLocator('org', 'course', 'run')) def_id = runtime.id_generator.create_definition(olx_element.tag, olx_element.get('url_name')) keys = ScopeIds(None, olx_element.tag, def_id, runtime.id_generator.create_usage(def_id)) @@ -78,7 +79,7 @@ def test_bad_ajax_request(self): Make sure that answer for incorrect request is error json. """ module_system = get_test_system() - block = self.word_cloud_class(module_system, DictFieldData(self.raw_field_data), Mock()) + block = self.word_cloud_class(module_system, DictFieldData(self.raw_field_data), make_test_scope_ids('word_cloud')) # The extracted Word Cloud XBlock uses @XBlock.json_handler for handling AJAX requests, # which requires a different way of method invocation. @@ -91,7 +92,7 @@ def test_good_ajax_request(self): Make sure that ajax request works correctly. """ module_system = get_test_system() - block = self.word_cloud_class(module_system, DictFieldData(self.raw_field_data), Mock()) + block = self.word_cloud_class(module_system, DictFieldData(self.raw_field_data), make_test_scope_ids('word_cloud')) # The extracted Word Cloud XBlock uses @XBlock.json_handler for handling AJAX requests. # It expects a standard Python dictionary as POST data and returns a JSON object in response. @@ -121,7 +122,7 @@ def test_indexibility(self): Test indexibility of Word Cloud """ module_system = get_test_system() - block = self.word_cloud_class(module_system, DictFieldData(self.raw_field_data), Mock()) + block = self.word_cloud_class(module_system, DictFieldData(self.raw_field_data), make_test_scope_ids('word_cloud')) assert block.index_dictionary() ==\ {'content_type': 'Word Cloud', 'content': {'display_name': 'Word Cloud Block', @@ -145,7 +146,7 @@ def test_studio_submit_handler(self): 'values': TEST_SUBMIT_DATA, } module_system = get_test_system() - block = self.word_cloud_class(module_system, DictFieldData(self.raw_field_data), Mock()) + block = self.word_cloud_class(module_system, DictFieldData(self.raw_field_data), make_test_scope_ids('word_cloud')) body = json.dumps(TEST_REQUEST_JSON) request = Request.blank('/') request.method = 'POST' diff --git a/xmodule/tests/test_xml_block.py b/xmodule/tests/test_xml_block.py index 1aba731f65d8..3a7f5e442639 100644 --- a/xmodule/tests/test_xml_block.py +++ b/xmodule/tests/test_xml_block.py @@ -14,7 +14,7 @@ from xmodule.modulestore.inheritance import InheritanceKeyValueStore, InheritanceMixin, InheritingFieldData from xmodule.modulestore.split_mongo.split_mongo_kvs import SplitMongoKVS from xmodule.seq_block import SequenceBlock -from xmodule.tests import get_test_descriptor_system +from xmodule.tests import get_test_descriptor_system, make_test_scope_ids from xmodule.tests.xml import XModuleXmlImportTest from xmodule.tests.xml.factories import CourseFactory, ProblemFactory, SequenceFactory from xmodule.x_module import XModuleMixin @@ -107,11 +107,10 @@ def get_a_block(self, usage_id=None): """ Construct an XBlock for testing with. """ - scope_ids = Mock() if usage_id is None: block_id = f"_auto{len(self.all_blocks)}" usage_id = self.get_usage_id("course", block_id) - scope_ids.usage_id = usage_id + scope_ids = make_test_scope_ids(usage_id.block_type, usage_id.block_id, course_key=usage_id.course_key) block = self.system.construct_xblock_from_class( self.TestableInheritingXBlock, field_data=self.field_data, @@ -409,7 +408,7 @@ def get_xml_editable_fields(self, field_data): runtime = get_test_descriptor_system() return runtime.construct_xblock_from_class( self.TestableXmlXBlock, - scope_ids=Mock(), + scope_ids=make_test_scope_ids('test'), field_data=field_data, ).editable_metadata_fields @@ -426,7 +425,9 @@ def non_editable_metadata_fields(self): return non_editable_fields system = get_test_descriptor_system(render_template=Mock()) - return system.construct_xblock_from_class(TestModuleBlock, field_data=field_data, scope_ids=Mock()) + return system.construct_xblock_from_class( + TestModuleBlock, field_data=field_data, scope_ids=make_test_scope_ids('test') + ) def assert_field_values( # pylint: disable=dangerous-default-value,too-many-arguments,too-many-positional-arguments self, diff --git a/xmodule/tests/xml/__init__.py b/xmodule/tests/xml/__init__.py index 0276f7e2c361..8d4c3e585388 100644 --- a/xmodule/tests/xml/__init__.py +++ b/xmodule/tests/xml/__init__.py @@ -39,7 +39,7 @@ def get_policy(usage_id): render_template=lambda template, context: pprint.pformat((template, context)), services={'field-data': KvsFieldData(DictKeyValueStore())}, ) - self.id_generator = Mock() + self.id_generator = CourseLocationManager(self.course_id) def process_xml(self, xml): # pylint: disable=method-hidden """Parse `xml` as an XBlock, and add it to `self._blocks`""" From f21a6a354e2e560f40d2f2d1b87fa1f562cbf046 Mon Sep 17 00:00:00 2001 From: salmannawaz Date: Fri, 4 Dec 2026 15:38:10 +0500 Subject: [PATCH 5/5] style: wrap long lines in word cloud and annotatable block tests Co-Authored-By: Claude Opus 5.5 (1M context) --- xmodule/tests/test_annotatable_block.py | 4 +++- xmodule/tests/test_word_cloud.py | 16 ++++++++++++---- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/xmodule/tests/test_annotatable_block.py b/xmodule/tests/test_annotatable_block.py index 6177e16f6803..bd4b2969a5fd 100644 --- a/xmodule/tests/test_annotatable_block.py +++ b/xmodule/tests/test_annotatable_block.py @@ -43,7 +43,9 @@ def setUp(self): self.annotatable = self.annotatable_class( get_test_system(), DictFieldData({'data': self.sample_xml}), - ScopeIds(None, 'category', None, BlockUsageLocator(CourseLocator('org', 'course', 'run'), 'category', 'name')) + ScopeIds( + None, 'category', None, BlockUsageLocator(CourseLocator('org', 'course', 'run'), 'category', 'name') + ) ) def test_annotation_data_attr(self): diff --git a/xmodule/tests/test_word_cloud.py b/xmodule/tests/test_word_cloud.py index 36a76ba25e94..f63b248ef5f0 100644 --- a/xmodule/tests/test_word_cloud.py +++ b/xmodule/tests/test_word_cloud.py @@ -79,7 +79,9 @@ def test_bad_ajax_request(self): Make sure that answer for incorrect request is error json. """ module_system = get_test_system() - block = self.word_cloud_class(module_system, DictFieldData(self.raw_field_data), make_test_scope_ids('word_cloud')) + block = self.word_cloud_class( + module_system, DictFieldData(self.raw_field_data), make_test_scope_ids('word_cloud') + ) # The extracted Word Cloud XBlock uses @XBlock.json_handler for handling AJAX requests, # which requires a different way of method invocation. @@ -92,7 +94,9 @@ def test_good_ajax_request(self): Make sure that ajax request works correctly. """ module_system = get_test_system() - block = self.word_cloud_class(module_system, DictFieldData(self.raw_field_data), make_test_scope_ids('word_cloud')) + block = self.word_cloud_class( + module_system, DictFieldData(self.raw_field_data), make_test_scope_ids('word_cloud') + ) # The extracted Word Cloud XBlock uses @XBlock.json_handler for handling AJAX requests. # It expects a standard Python dictionary as POST data and returns a JSON object in response. @@ -122,7 +126,9 @@ def test_indexibility(self): Test indexibility of Word Cloud """ module_system = get_test_system() - block = self.word_cloud_class(module_system, DictFieldData(self.raw_field_data), make_test_scope_ids('word_cloud')) + block = self.word_cloud_class( + module_system, DictFieldData(self.raw_field_data), make_test_scope_ids('word_cloud') + ) assert block.index_dictionary() ==\ {'content_type': 'Word Cloud', 'content': {'display_name': 'Word Cloud Block', @@ -146,7 +152,9 @@ def test_studio_submit_handler(self): 'values': TEST_SUBMIT_DATA, } module_system = get_test_system() - block = self.word_cloud_class(module_system, DictFieldData(self.raw_field_data), make_test_scope_ids('word_cloud')) + block = self.word_cloud_class( + module_system, DictFieldData(self.raw_field_data), make_test_scope_ids('word_cloud') + ) body = json.dumps(TEST_REQUEST_JSON) request = Request.blank('/') request.method = 'POST'