Skip to content

Commit e3bf535

Browse files
committed
Correctly set values for Reference objects in parent object
1 parent ed08973 commit e3bf535

1 file changed

Lines changed: 33 additions & 13 deletions

File tree

scim2_tester/filling.py

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,26 @@ def fill_with_random_values(
6262
) -> tuple[Resource, list[Resource]]:
6363
"""Fill an object with random values generated according the attribute types."""
6464
garbages = []
65+
ref_objs = {}
66+
67+
for field_name in field_names or obj.__class__.model_fields.keys():
68+
field_type = obj.get_field_root_type(field_name)
69+
if get_origin(field_type) == Annotated:
70+
field_type = get_args(field_type)[0]
71+
72+
if get_origin(field_type) is Reference:
73+
ref_type = get_args(field_type)[0]
74+
if ref_type not in (ExternalReference, URIReference):
75+
model = model_from_ref_type(
76+
conf, ref_type, different_than=obj.__class__
77+
)
78+
ref_obj, sub_garbages = create_minimal_object(conf, model)
79+
value = ref_obj.meta.location
80+
garbages.append(ref_obj)
81+
garbages += sub_garbages
82+
83+
ref_objs[obj.__class__.__name__.lower()] = ref_obj
84+
6585
for field_name in field_names or obj.__class__.model_fields.keys():
6686
field = obj.__class__.model_fields[field_name]
6787
if field.default:
@@ -76,9 +96,6 @@ def fill_with_random_values(
7696
if field_type is Meta:
7797
value = None
7898

79-
elif field.examples:
80-
value = random.choice(field.examples)
81-
8299
# RFC7643 §4.1.2 provides the following indications, however
83100
# there is no way to guess the existence of such requirements
84101
# just by looking at the object schema.
@@ -95,6 +112,18 @@ def fill_with_random_values(
95112
elif field_name == "value" and "phone" in obj.__class__.__name__.lower():
96113
value = "".join(str(random.choice(range(10))) for _ in range(10))
97114

115+
elif field_name == "value" and obj.__class__.__name__.lower() in ref_objs:
116+
value = ref_objs[obj.__class__.__name__.lower()].id
117+
118+
elif field_name == "type" and obj.__class__.__name__.lower() in ref_objs:
119+
value = ref_objs[obj.__class__.__name__.lower()].meta.resource_type
120+
121+
elif (field_name == "display" or field_name == "display_name") and obj.__class__.__name__.lower() in ref_objs:
122+
value = ref_objs[obj.__class__.__name__.lower()].display_name
123+
124+
elif field.examples:
125+
value = random.choice(field.examples)
126+
98127
elif field_type is int:
99128
value = uuid.uuid4().int
100129

@@ -106,16 +135,7 @@ def fill_with_random_values(
106135

107136
elif get_origin(field_type) is Reference:
108137
ref_type = get_args(field_type)[0]
109-
if ref_type not in (ExternalReference, URIReference):
110-
model = model_from_ref_type(
111-
conf, ref_type, different_than=obj.__class__
112-
)
113-
ref_obj, sub_garbages = create_minimal_object(conf, model)
114-
value = ref_obj.meta.location
115-
garbages.append(ref_obj)
116-
garbages += sub_garbages
117-
118-
else:
138+
if ref_type in (ExternalReference, URIReference):
119139
value = f"https://{str(uuid.uuid4())}.test"
120140

121141
elif isclass(field_type) and issubclass(field_type, Enum):

0 commit comments

Comments
 (0)