From f58fbd699ef72ec1470d971f17236c0e4d4774e9 Mon Sep 17 00:00:00 2001 From: piptouque Date: Tue, 3 Feb 2026 18:27:11 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B(models)=20fix=20groups'=20optional?= =?UTF-8?q?=20member=20field=20defaulting=20to=20null?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Without this fix, the mbox, mbox_sha1sum etc. kinds of groups still expect a member. --- CHANGELOG.md | 1 + src/ralph/models/xapi/base/groups.py | 2 +- tests/models/xapi/base/test_groups.py | 59 ++++++++++++++++++++++++++- 3 files changed, 60 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6f3b6ce9f..f05acb464 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,7 @@ and this project adheres to ### Fixed - Fix type of `statement.result.score.scaled` from `int` to `Decimal` +- Fix optional `member` field in Identified Groups not actually being optional ## [5.0.1] - 2024-07-11 diff --git a/src/ralph/models/xapi/base/groups.py b/src/ralph/models/xapi/base/groups.py index e47874b6a..1f88367f1 100644 --- a/src/ralph/models/xapi/base/groups.py +++ b/src/ralph/models/xapi/base/groups.py @@ -56,7 +56,7 @@ class BaseXapiIdentifiedGroup(BaseXapiGroupCommonProperties): member (list): Consist of a list of the members of this Group. """ - member: Optional[List[BaseXapiAgent]] + member: Optional[List[BaseXapiAgent]] = None class BaseXapiIdentifiedGroupWithMbox(BaseXapiIdentifiedGroup, BaseXapiMboxIFI): diff --git a/tests/models/xapi/base/test_groups.py b/tests/models/xapi/base/test_groups.py index 9bb41bbff..c6a11dbda 100644 --- a/tests/models/xapi/base/test_groups.py +++ b/tests/models/xapi/base/test_groups.py @@ -1,6 +1,13 @@ """Tests for the base xAPI `Group` definitions.""" -from ralph.models.xapi.base.groups import BaseXapiGroupCommonProperties +import pytest +from pydantic import ValidationError + +from ralph.models.xapi.base.groups import ( + BaseXapiGroupCommonProperties, + BaseXapiIdentifiedGroupWithMbox, + BaseXapiIdentifiedGroupWithMboxSha1Sum, +) from tests.factories import mock_xapi_instance @@ -12,3 +19,53 @@ def test_models_xapi_base_groups_group_common_properties_with_valid_field(): field = mock_xapi_instance(BaseXapiGroupCommonProperties) assert field.objectType == "Group" + + +@pytest.mark.parametrize( + "group", + [ + { + "name": "d75662f7c72c8d12267712aaf31b649cdab13cb8", + "mbox_sha1sum": "6395037c217b04eae94355900bcaf014772c7d7c", + "objectType": "Group", + }, + { + "name": "d75662f7c72c8d12267712aaf31b649cdab13cb8", + "mbox_sha1sum": "6395037c217b04eae94355900bcaf014772c7d7c", + "objectType": "Group", + "member": [], + }, + ], +) +def test_models_xapi_base_group_mbox_sha1sum_valid(group): + """Test an invalid group with `mbox_sha1sum` property""" + + try: + BaseXapiIdentifiedGroupWithMboxSha1Sum(**group) + except ValidationError as err: + pytest.fail(f"Valid statement should not raise exceptions: {err}") + + +@pytest.mark.parametrize( + "group", + [ + { + "name": "d75662f7c72c8d12267712aaf31b649cdab13cb8", + "mbox": "mailto:foob@r.com", + "objectType": "Group", + }, + { + "name": "d75662f7c72c8d12267712aaf31b649cdab13cb8", + "mbox": "mailto:foob@r.com", + "objectType": "Group", + "member": [], + }, + ], +) +def test_models_xapi_base_group_mbox_valid(group): + """Test an invalid group with `mbox` property""" + + try: + BaseXapiIdentifiedGroupWithMbox(**group) + except ValidationError as err: + pytest.fail(f"Valid statement should not raise exceptions: {err}")