|
2 | 2 | # pylint:disable=unused-argument |
3 | 3 | # pylint:disable=redefined-outer-name |
4 | 4 |
|
5 | | -from pprint import pformat |
6 | 5 | from typing import Any |
7 | 6 |
|
8 | 7 | import pytest |
9 | 8 | from models_library.projects_state import RunningState |
10 | 9 | from pydantic.main import BaseModel |
| 10 | +from pytest_simcore.pydantic_models import ( |
| 11 | + assert_validation_model, |
| 12 | + iter_model_examples_in_class, |
| 13 | +) |
11 | 14 | from simcore_postgres_database.models.comp_pipeline import StateType |
12 | 15 | from simcore_service_director_v2.models.comp_tasks import CompTaskAtDB |
13 | 16 |
|
14 | 17 |
|
15 | 18 | @pytest.mark.parametrize( |
16 | | - "model_cls", |
17 | | - (CompTaskAtDB,), |
| 19 | + "model_cls, example_name, example_data", |
| 20 | + iter_model_examples_in_class(CompTaskAtDB), |
18 | 21 | ) |
19 | 22 | def test_computation_task_model_examples( |
20 | | - model_cls: type[BaseModel], model_cls_examples: dict[str, dict[str, Any]] |
21 | | -): |
22 | | - for name, example in model_cls_examples.items(): |
23 | | - print(name, ":", pformat(example)) |
24 | | - model_instance = model_cls(**example) |
25 | | - assert model_instance, f"Failed with {name}" |
26 | | - |
27 | | - |
28 | | -@pytest.mark.parametrize( |
29 | | - "model_cls", |
30 | | - [CompTaskAtDB], |
31 | | -) |
32 | | -def test_computation_task_model_export_to_db_model( |
33 | | - model_cls: type[BaseModel], model_cls_examples: dict[str, dict[str, Any]] |
| 23 | + model_cls: type[BaseModel], example_name: str, example_data: dict[str, Any] |
34 | 24 | ): |
35 | | - for name, example in model_cls_examples.items(): |
36 | | - print(name, ":", pformat(example)) |
37 | | - model_instance = model_cls(**example) |
38 | | - assert model_instance, f"Failed with {name}" |
| 25 | + model_instance = assert_validation_model( |
| 26 | + model_cls, example_name=example_name, example_data=example_data |
| 27 | + ) |
39 | 28 |
|
40 | | - assert isinstance(model_instance, CompTaskAtDB) |
41 | | - db_model = model_instance.to_db_model() |
| 29 | + assert isinstance(model_instance, CompTaskAtDB) |
| 30 | + db_model = model_instance.to_db_model() |
42 | 31 |
|
43 | | - assert isinstance(db_model, dict) |
44 | | - assert StateType(db_model["state"]) |
| 32 | + assert isinstance(db_model, dict) |
| 33 | + assert StateType(db_model["state"]) |
45 | 34 |
|
46 | 35 |
|
47 | 36 | @pytest.mark.parametrize( |
48 | | - "model_cls", |
49 | | - [CompTaskAtDB], |
| 37 | + "model_cls, example_name, example_data", |
| 38 | + iter_model_examples_in_class(CompTaskAtDB), |
50 | 39 | ) |
51 | 40 | def test_computation_task_model_with_running_state_value_field( |
52 | | - model_cls: type[BaseModel], model_cls_examples: dict[str, dict[str, Any]] |
| 41 | + model_cls: type[BaseModel], example_name: str, example_data: dict[str, Any] |
53 | 42 | ): |
54 | | - for name, example in model_cls_examples.items(): |
55 | | - example["state"] = RunningState.WAITING_FOR_RESOURCES.value |
56 | | - print(name, ":", pformat(example)) |
57 | | - model_instance = model_cls(**example) |
58 | | - assert model_instance, f"Failed with {name}" |
| 43 | + example_data["state"] = RunningState.WAITING_FOR_RESOURCES.value |
| 44 | + model_instance = model_cls(**example_data) |
| 45 | + assert model_instance, f"Failed with {example_name}" |
59 | 46 |
|
60 | 47 |
|
61 | 48 | @pytest.mark.parametrize( |
62 | | - "model_cls", |
63 | | - [CompTaskAtDB], |
| 49 | + "model_cls, example_name, example_data", |
| 50 | + iter_model_examples_in_class(CompTaskAtDB), |
64 | 51 | ) |
65 | 52 | def test_computation_task_model_with_wrong_default_value_field( |
66 | | - model_cls: type[BaseModel], model_cls_examples: dict[str, dict[str, Any]] |
| 53 | + model_cls: type[BaseModel], example_name: str, example_data: dict[str, Any] |
67 | 54 | ): |
68 | | - for name, example in model_cls_examples.items(): |
69 | | - for output_schema in example.get("schema", {}).get("outputs", {}).values(): |
70 | | - output_schema["defaultValue"] = None |
| 55 | + for output_schema in example_data.get("schema", {}).get("outputs", {}).values(): |
| 56 | + output_schema["defaultValue"] = None |
71 | 57 |
|
72 | | - print(name, ":", pformat(example)) |
73 | | - model_instance = model_cls(**example) |
74 | | - assert model_instance, f"Failed with {name}" |
| 58 | + model_instance = model_cls(**example_data) |
| 59 | + assert model_instance, f"Failed with {example_name}" |
0 commit comments