diff --git a/cms/lib/xblock/tagging/test.py b/cms/lib/xblock/tagging/test.py index be8bb62a6454..6e0607dfd266 100644 --- a/cms/lib/xblock/tagging/test.py +++ b/cms/lib/xblock/tagging/test.py @@ -11,7 +11,7 @@ from django.test.client import RequestFactory from lxml import etree from opaque_keys.edx.asides import AsideUsageKeyV1, AsideUsageKeyV2 -from xblock.fields import ScopeIds +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,10 +123,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 = make_test_scope_ids( + "bobs-type", + "usage-id", + course_key=CourseKey.from_string("course-v1:myOrg+myCourse+myRun"), + user_id="bob", + ) key_store = DictKeyValueStore() field_data = KvsFieldData(key_store) runtime = TestRuntime(services={'field-data': field_data}) diff --git a/lms/djangoapps/courseware/tests/test_discussion_xblock.py b/lms/djangoapps/courseware/tests/test_discussion_xblock.py index a94286156d73..5bdd012483ac 100644 --- a/lms/djangoapps/courseware/tests/test_discussion_xblock.py +++ b/lms/djangoapps/courseware/tests/test_discussion_xblock.py @@ -28,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 @@ -52,8 +53,7 @@ def setUp(self): self.data = DictFieldData({ 'discussion_id': self.discussion_id }) - scope_ids = mock.Mock() - scope_ids.usage_id.context_key = self.course_id + 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/lms/djangoapps/courseware/tests/test_video_mongo.py b/lms/djangoapps/courseware/tests/test_video_mongo.py index 3af319e778d9..7f431be31f76 100644 --- a/lms/djangoapps/courseware/tests/test_video_mongo.py +++ b/lms/djangoapps/courseware/tests/test_video_mongo.py @@ -101,7 +101,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), ) diff --git a/openedx/core/djangoapps/video_config/tests/test_services.py b/openedx/core/djangoapps/video_config/tests/test_services.py index ea446717d4e4..20612b070b5d 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/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 eed96a41e853..dac3ac1404b0 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 6edeba971177..080262828d17 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 155d72ea6692..ff0723b9a8ba 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 53f025cee7fc..fbd439e127d5 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 = [ diff --git a/xmodule/modulestore/tests/test_api.py b/xmodule/modulestore/tests/test_api.py index a7e15ee2c209..85e1018e65c4 100644 --- a/xmodule/modulestore/tests/test_api.py +++ b/xmodule/modulestore/tests/test_api.py @@ -18,6 +18,7 @@ get_root_module_name, get_xblock_root_module_name, ) +from xmodule.tests import make_test_scope_ids def test_get_root_module_name(): @@ -43,7 +44,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 ded0dfc43db7..b9e32cbd0205 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( @@ -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..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, 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 e83c0a6ead62..d32445d9179f 100644 --- a/xmodule/tests/test_html_block.py +++ b/xmodule/tests/test_html_block.py @@ -7,7 +7,7 @@ from xmodule.html_block import CourseInfoBlock -from . import get_test_system +from . import get_test_system, make_test_scope_ids class CourseInfoBlockTestCase(unittest.TestCase): @@ -37,7 +37,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 @@ -73,7 +73,7 @@ def test_updates_order(self): info_block = CourseInfoBlock( Mock(), DictFieldData({'items': sample_update_data, 'data': ""}), - Mock() + make_test_scope_ids('course_info') ) expected_context = { 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..f63b248ef5f0 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,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), 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 +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), 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 +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), 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 +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), 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`"""