Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: throw an error if the scope if None on scope_mappings [FC-0076] #815

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ Change history for XBlock
Unreleased
----------

* throws InvalidScopeError if the scope is not defined

5.1.0 - 2024-08-07
------------------

Expand Down
6 changes: 4 additions & 2 deletions xblock/field_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,10 +145,12 @@ def _field_data(self, block, name):
"""Return the field data for the field `name` on the :class:`~xblock.core.XBlock` `block`"""
scope = block.fields[name].scope

if scope not in self._scope_mappings:
scope_mapping = self._scope_mappings.get(scope)

if scope_mapping is None:
raise InvalidScopeError(scope)

return self._scope_mappings[scope]
return scope_mapping

def get(self, block, name):
return self._field_data(block, name).get(block, name)
Expand Down
9 changes: 9 additions & 0 deletions xblock/test/test_field_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ def setup_method(self):
Scope.content: self.content,
Scope.settings: self.settings
})
self.split_empty = SplitFieldData({
Scope.content: self.content,
Scope.settings: self.settings,
Scope.user_state: None,
})
self.runtime = TestRuntime(services={'field-data': self.split})
self.block = TestingBlock(
runtime=self.runtime,
Expand Down Expand Up @@ -76,6 +81,10 @@ def test_invalid_scope(self):
with pytest.raises(InvalidScopeError):
self.split.get(self.block, 'user_state')

def test_empty_scope(self):
with pytest.raises(InvalidScopeError):
self.split_empty.get(self.block, 'user_state')

def test_default(self):
self.split.default(self.block, 'content')
self.content.default.assert_called_once_with(self.block, 'content')
Expand Down