Skip to content

Commit afa0b3c

Browse files
authored
⬆️ upgrades iter_model_examples_in_module to enable all example tests (#6632)
1 parent 4fd252f commit afa0b3c

File tree

1 file changed

+14
-18
lines changed

1 file changed

+14
-18
lines changed

packages/pytest-simcore/src/pytest_simcore/pydantic_models.py

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -82,21 +82,20 @@ def _is_model_cls(obj) -> bool:
8282
for model_name, model_cls in inspect.getmembers(module, _is_model_cls):
8383
assert model_name # nosec
8484
if (
85-
(config_cls := model_cls.model_config)
86-
and inspect.isclass(config_cls)
87-
and is_strict_inner(model_cls, config_cls)
88-
and (schema_extra := getattr(config_cls, "schema_extra", {}))
89-
and isinstance(schema_extra, dict)
85+
(model_config := model_cls.model_config)
86+
and isinstance(model_config, dict)
87+
and (json_schema_extra := model_config.get("json_schema_extra", {}))
88+
and isinstance(json_schema_extra, dict)
9089
):
91-
if "example" in schema_extra:
90+
if "example" in json_schema_extra:
9291
yield ModelExample(
9392
model_cls=model_cls,
9493
example_name="example",
95-
example_data=schema_extra["example"],
94+
example_data=json_schema_extra["example"],
9695
)
9796

98-
elif "examples" in schema_extra:
99-
for index, example in enumerate(schema_extra["examples"]):
97+
elif "examples" in json_schema_extra:
98+
for index, example in enumerate(json_schema_extra["examples"]):
10099
yield ModelExample(
101100
model_cls=model_cls,
102101
example_name=f"examples_{index}",
@@ -120,10 +119,10 @@ def model_cls_examples(model_cls: type[BaseModel]) -> dict[str, dict[str, Any]]:
120119
"SEE https://pydantic-docs.helpmanual.io/usage/schema/#schema-customization"
121120
)
122121

122+
json_schema_extra: dict = model_cls.model_config.get("json_schema_extra", {})
123+
123124
# checks exampleS setup in schema_extra
124-
examples_list = copy.deepcopy(
125-
model_cls.model_config["json_schema_extra"].get("examples", [])
126-
)
125+
examples_list = copy.deepcopy(json_schema_extra.get("examples", []))
127126
assert isinstance(examples_list, list), (
128127
"OpenAPI and json-schema differ regarding the format for exampleS."
129128
"The former is a dict and the latter an array. "
@@ -132,15 +131,12 @@ def model_cls_examples(model_cls: type[BaseModel]) -> dict[str, dict[str, Any]]:
132131
"SEE https://swagger.io/docs/specification/adding-examples/"
133132
)
134133

135-
# check example in schema_extra
136-
example = copy.deepcopy(model_cls.model_config["json_schema_extra"].get("example"))
137-
138134
# collect all examples and creates fixture -> {example-name: example, ...}
139135
examples = {
140-
f"{model_cls.__name__}.example[{index}]": example
141-
for index, example in enumerate(examples_list)
136+
f"{model_cls.__name__}.example[{index}]": example_
137+
for index, example_ in enumerate(examples_list)
142138
}
143-
if example:
139+
if example := copy.deepcopy(json_schema_extra.get("example")):
144140
examples[f"{model_cls.__name__}.example"] = example
145141

146142
return examples

0 commit comments

Comments
 (0)