Skip to content

Commit 80a13b0

Browse files
committed
refactor: dedicated method for filling multi valued complex attributes
1 parent 388a220 commit 80a13b0

3 files changed

Lines changed: 41 additions & 1 deletion

File tree

doc/changelog.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ Changelog
77
Fixed
88
^^^^^
99
- Sort results in get_all_available_tags, so it can be used with pytest-xdist.
10+
- `generate_random_value` generate coherent `ref` and `value` values for complex attributes. :pr:`30` :pr:`37`
1011

1112
[0.2.0] - 2025-08-12
1213
--------------------

scim2_tester/filling.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from scim2_models import ComplexAttribute
1212
from scim2_models import Extension
1313
from scim2_models import ExternalReference
14+
from scim2_models import MultiValuedComplexAttribute
1415
from scim2_models import Mutability
1516
from scim2_models import Reference
1617
from scim2_models import Resource
@@ -123,6 +124,9 @@ def generate_random_value(
123124
elif isclass(field_type) and issubclass(field_type, Enum):
124125
value = random.choice(list(field_type))
125126

127+
elif isclass(field_type) and issubclass(field_type, MultiValuedComplexAttribute):
128+
value = fill_mvca_with_random_values(context, field_type()) # type: ignore[arg-type]
129+
126130
elif isclass(field_type) and issubclass(field_type, ComplexAttribute):
127131
value = fill_with_random_values(context, field_type()) # type: ignore[arg-type]
128132

@@ -172,3 +176,24 @@ def fill_with_random_values(
172176
set_value_by_urn(obj, urn, value)
173177

174178
return obj
179+
180+
181+
def fill_mvca_with_random_values(
182+
context: "CheckContext",
183+
obj: MultiValuedComplexAttribute,
184+
) -> Resource[Any] | None:
185+
"""Fill a MultiValuedComplexAttribute with random values.
186+
187+
For SCIM reference fields, correctly sets the value field to match
188+
the ID extracted from the reference URL.
189+
"""
190+
fill_with_random_values(context, obj)
191+
ref_type = type(obj).get_field_root_type("ref")
192+
if (
193+
get_origin(ref_type) is Reference
194+
and get_args(ref_type)
195+
and get_args(ref_type)[0] not in (URIReference, ExternalReference, Any)
196+
and (ref := getattr(obj, "ref", None))
197+
):
198+
obj.value = ref.rsplit("/", 1)[-1]
199+
return obj

tests/test_filling.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,24 @@ def test_model_resolution_from_reference_type(testing_context):
3333
assert result != Group
3434

3535

36-
def test_generate_random_value_for_reference(testing_context):
36+
def test_generate_random_value_for_reference(testing_context, httpserver):
3737
"""Validates random value generation for reference fields."""
3838
group = Group()
3939

40+
# Mock HTTP response for user creation
41+
user_data = {
42+
"schemas": ["urn:ietf:params:scim:schemas:core:2.0:User"],
43+
"id": "test-user-id",
44+
"userName": "test-user",
45+
"meta": {
46+
"resourceType": "User",
47+
"location": f"http://localhost:{httpserver.port}/Users/test-user-id",
48+
},
49+
}
50+
httpserver.expect_request("/Users", method="POST").respond_with_json(
51+
user_data, status=201
52+
)
53+
4054
result = fill_with_random_values(testing_context, group)
4155

4256
assert len(result.members) == 1

0 commit comments

Comments
 (0)