Skip to content

Commit d8b8a42

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

1 file changed

Lines changed: 35 additions & 11 deletions

File tree

scim2_tester/filling.py

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,25 @@ 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+
garbages.append(ref_obj)
80+
garbages += sub_garbages
81+
82+
ref_objs[obj.__class__.__name__.lower()] = ref_obj
83+
6584
for field_name in field_names or obj.__class__.model_fields.keys():
6685
field = obj.__class__.model_fields[field_name]
6786
if field.default:
@@ -76,9 +95,6 @@ def fill_with_random_values(
7695
if field_type is Meta:
7796
value = None
7897

79-
elif field.examples:
80-
value = random.choice(field.examples)
81-
8298
# RFC7643 §4.1.2 provides the following indications, however
8399
# there is no way to guess the existence of such requirements
84100
# just by looking at the object schema.
@@ -95,6 +111,20 @@ def fill_with_random_values(
95111
elif field_name == "value" and "phone" in obj.__class__.__name__.lower():
96112
value = "".join(str(random.choice(range(10))) for _ in range(10))
97113

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

@@ -107,14 +137,8 @@ def fill_with_random_values(
107137
elif get_origin(field_type) is Reference:
108138
ref_type = get_args(field_type)[0]
109139
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-
140+
if obj.__class__.__name__.lower() in ref_objs:
141+
value = ref_objs[obj.__class__.__name__.lower()].meta.location
118142
else:
119143
value = f"https://{str(uuid.uuid4())}.test"
120144

0 commit comments

Comments
 (0)