33import uuid
44from enum import Enum
55from inspect import isclass
6+ from typing import TYPE_CHECKING
67from typing import Annotated
78from typing import Any
89from typing import get_args
1314from scim2_models import ExternalReference
1415from scim2_models import Meta
1516from scim2_models import Reference
16- from scim2_models import Required
1717from scim2_models import Resource
1818from scim2_models import URIReference
1919from scim2_models .utils import UNION_TYPES
2020
2121from scim2_tester .utils import CheckConfig
2222
23-
24- def create_minimal_object (
25- conf : CheckConfig , model : type [Resource ]
26- ) -> tuple [Resource , list [Resource ]]:
27- """Create an object filling with the minimum required field set."""
28- field_names = [
29- field_name
30- for field_name in model .model_fields
31- if model .get_field_annotation (field_name , Required ) == Required .true
32- ]
33- obj , garbages = fill_with_random_values (conf , model (), field_names )
34- obj = conf .client .create (obj )
35- return obj , garbages
23+ if TYPE_CHECKING :
24+ from scim2_tester .utils import ResourceManager
3625
3726
3827def model_from_ref_type (
@@ -58,10 +47,19 @@ def model_from_ref_type_(ref_type):
5847
5948
6049def fill_with_random_values (
61- conf : CheckConfig , obj : Resource , field_names : list [str ] | None = None
62- ) -> Resource :
63- """Fill an object with random values generated according the attribute types."""
64- garbages = []
50+ conf : CheckConfig ,
51+ obj : Resource ,
52+ resource_manager : "ResourceManager" ,
53+ field_names : list [str ] | None = None ,
54+ ) -> Resource | None :
55+ """Fill an object with random values generated according the attribute types.
56+
57+ :param conf: The check configuration containing the SCIM client
58+ :param obj: The Resource object to fill with random values
59+ :param resource_manager: Resource manager for automatic cleanup
60+ :param field_names: Optional list of field names to fill (defaults to all)
61+ :returns: The filled object or None if the object ends up empty
62+ """
6563 for field_name in field_names or obj .__class__ .model_fields .keys ():
6664 field = obj .__class__ .model_fields [field_name ]
6765 if field .default :
@@ -110,9 +108,8 @@ def fill_with_random_values(
110108 model = model_from_ref_type (
111109 conf , ref_type , different_than = obj .__class__
112110 )
113- ref_obj , sub_garbages = create_minimal_object ( conf , model )
111+ ref_obj = resource_manager . create_and_register ( model )
114112 value = ref_obj .meta .location
115- garbages += sub_garbages
116113
117114 else :
118115 value = f"https://{ str (uuid .uuid4 ())} .test"
@@ -121,21 +118,18 @@ def fill_with_random_values(
121118 value = random .choice (list (field_type ))
122119
123120 elif isclass (field_type ) and issubclass (field_type , ComplexAttribute ):
124- value , sub_garbages = fill_with_random_values (conf , field_type ())
125- garbages += sub_garbages
121+ value = fill_with_random_values (conf , field_type (), resource_manager )
126122
127123 elif isclass (field_type ) and issubclass (field_type , Extension ):
128- value , sub_garbages = fill_with_random_values (conf , field_type ())
129- garbages += sub_garbages
124+ value = fill_with_random_values (conf , field_type (), resource_manager )
130125
131126 else :
132127 # Put emails so this will be accepted by EmailStr too
133128 value = str (uuid .uuid4 ())
134129
135130 if is_multiple :
136131 setattr (obj , field_name , [value ])
137-
138132 else :
139133 setattr (obj , field_name , value )
140134
141- return obj , garbages
135+ return obj
0 commit comments