Skip to content

Commit

Permalink
fixes pydantic models search
Browse files Browse the repository at this point in the history
  • Loading branch information
pcrespov committed Oct 29, 2024
1 parent f8fa4d9 commit 6728e00
Showing 1 changed file with 14 additions and 18 deletions.
32 changes: 14 additions & 18 deletions packages/pytest-simcore/src/pytest_simcore/pydantic_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,21 +82,20 @@ def _is_model_cls(obj) -> bool:
for model_name, model_cls in inspect.getmembers(module, _is_model_cls):
assert model_name # nosec
if (
(config_cls := model_cls.model_config)
and inspect.isclass(config_cls)
and is_strict_inner(model_cls, config_cls)
and (schema_extra := getattr(config_cls, "schema_extra", {}))
and isinstance(schema_extra, dict)
(model_config := model_cls.model_config)
and isinstance(model_config, dict)
and (json_schema_extra := model_config.get("json_schema_extra", {}))
and isinstance(json_schema_extra, dict)
):
if "example" in schema_extra:
if "example" in json_schema_extra:
yield ModelExample(
model_cls=model_cls,
example_name="example",
example_data=schema_extra["example"],
example_data=json_schema_extra["example"],
)

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

json_schema_extra: dict = model_cls.model_config.get("json_schema_extra", {})

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

# check example in schema_extra
example = copy.deepcopy(model_cls.model_config["json_schema_extra"].get("example"))

# collect all examples and creates fixture -> {example-name: example, ...}
examples = {
f"{model_cls.__name__}.example[{index}]": example
for index, example in enumerate(examples_list)
f"{model_cls.__name__}.example[{index}]": example_
for index, example_ in enumerate(examples_list)
}
if example:
if example := copy.deepcopy(json_schema_extra.get("example")):
examples[f"{model_cls.__name__}.example"] = example

return examples

0 comments on commit 6728e00

Please sign in to comment.