Skip to content

Commit 556427e

Browse files
committed
Merge branch 'master' into mai/refactor-products
2 parents 2a0ca68 + cdabc90 commit 556427e

File tree

18 files changed

+194
-133
lines changed

18 files changed

+194
-133
lines changed

api/specs/web-server/_storage.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
FileUploadCompletionBody,
1818
FileUploadSchema,
1919
LinkType,
20+
PathMetaDataGet,
2021
PresignedLink,
2122
)
2223
from models_library.api_schemas_webserver.storage import (
@@ -58,7 +59,7 @@ async def list_storage_locations():
5859

5960
@router.get(
6061
"/storage/locations/{location_id}/paths",
61-
response_model=CursorPage[FileMetaDataGet],
62+
response_model=CursorPage[PathMetaDataGet],
6263
)
6364
async def list_storage_paths(
6465
_path: Annotated[StorageLocationPathParams, Depends()],

packages/models-library/tests/test_service_settings_labels.py

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
import json
77
from copy import deepcopy
8-
from pprint import pformat
98
from typing import Any, Final, NamedTuple
109

1110
import pytest
@@ -33,6 +32,10 @@
3332
from models_library.services_resources import DEFAULT_SINGLE_SERVICE_NAME
3433
from models_library.utils.string_substitution import TextTemplate
3534
from pydantic import BaseModel, TypeAdapter, ValidationError
35+
from pytest_simcore.pydantic_models import (
36+
assert_validation_model,
37+
iter_model_examples_in_class,
38+
)
3639

3740

3841
class _Parametrization(NamedTuple):
@@ -89,17 +92,23 @@ def test_service_settings():
8992
service_setting.set_destination_containers(["random_value1", "random_value2"])
9093

9194

92-
@pytest.mark.parametrize("model_cls", [SimcoreServiceLabels])
95+
@pytest.mark.parametrize(
96+
"model_cls, example_name, example_data",
97+
iter_model_examples_in_class(SimcoreServiceLabels),
98+
)
9399
def test_correctly_detect_dynamic_sidecar_boot(
94-
model_cls: type[BaseModel], model_cls_examples: dict[str, dict[str, Any]]
100+
model_cls: type[BaseModel], example_name: str, example_data: Any
95101
):
96-
for name, example in model_cls_examples.items():
97-
print(name, ":", pformat(example))
98-
model_instance = TypeAdapter(model_cls).validate_python(example)
99-
assert model_instance.callbacks_mapping is not None
100-
assert model_instance.needs_dynamic_sidecar == (
101-
"simcore.service.paths-mapping" in example
102-
)
102+
103+
model_instance = assert_validation_model(
104+
model_cls, example_name=example_name, example_data=example_data
105+
)
106+
107+
assert isinstance(model_instance, SimcoreServiceLabels)
108+
assert model_instance.callbacks_mapping is not None
109+
assert model_instance.needs_dynamic_sidecar == (
110+
"simcore.service.paths-mapping" in example_data
111+
)
103112

104113

105114
def test_raises_error_if_http_entrypoint_is_missing():

packages/pytest-simcore/src/pytest_simcore/helpers/faker_factories.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ def random_product(
290290

291291

292292
def random_product_price(
293-
product_name: str, fake: Faker = DEFAULT_FAKER, **overrides
293+
*, product_name: str, fake: Faker = DEFAULT_FAKER, **overrides
294294
) -> dict[str, Any]:
295295
from simcore_postgres_database.models.products_prices import products_prices
296296

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from collections.abc import Iterator
88
from contextlib import suppress
99
from types import ModuleType
10-
from typing import Any, NamedTuple
10+
from typing import Any, NamedTuple, TypeVar
1111

1212
import pytest
1313
from common_library.json_serialization import json_dumps
@@ -143,9 +143,12 @@ def test_model_examples(
143143
)
144144

145145

146+
TBaseModel = TypeVar("TBaseModel", bound=BaseModel)
147+
148+
146149
def assert_validation_model(
147-
model_cls: type[BaseModel], example_name: str, example_data: Any
148-
) -> BaseModel:
150+
model_cls: type[TBaseModel], example_name: str, example_data: Any
151+
) -> TBaseModel:
149152
try:
150153
model_instance = model_cls.model_validate(example_data)
151154
except ValidationError as err:
@@ -155,7 +158,7 @@ def assert_validation_model(
155158
f"\nError: {err}"
156159
)
157160

158-
assert isinstance(model_instance, BaseModel)
161+
assert isinstance(model_instance, model_cls)
159162
return model_instance
160163

161164

@@ -171,6 +174,7 @@ def model_cls_examples(model_cls: type[BaseModel]) -> dict[str, dict[str, Any]]:
171174
"The 'model_cls_examples' fixture is deprecated and will be removed in a future version. "
172175
"Please use 'iter_model_example_in_class' or 'iter_model_examples_in_module' as an alternative.",
173176
DeprecationWarning,
177+
stacklevel=2,
174178
)
175179
# Use by defining model_cls as test parametrization
176180
assert model_cls, (

packages/service-integration/tests/test_versioning.py

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,17 @@
22
# pylint: disable=unused-argument
33
# pylint: disable=unused-variable
44

5-
import json
5+
6+
import itertools
7+
from typing import Any
68

79
import pytest
810
from packaging.version import Version
11+
from pydantic import BaseModel
12+
from pytest_simcore.pydantic_models import (
13+
assert_validation_model,
14+
iter_model_examples_in_class,
15+
)
916
from service_integration.versioning import (
1017
ExecutableVersionInfo,
1118
ServiceVersionInfo,
@@ -45,11 +52,15 @@ def test_bump_version_string(
4552

4653

4754
@pytest.mark.parametrize(
48-
"model_cls",
49-
[ExecutableVersionInfo, ServiceVersionInfo],
55+
"model_cls, example_name, example_data",
56+
itertools.chain(
57+
iter_model_examples_in_class(ExecutableVersionInfo),
58+
iter_model_examples_in_class(ServiceVersionInfo),
59+
),
5060
)
51-
def test_version_info_model_examples(model_cls, model_cls_examples):
52-
for name, example in model_cls_examples.items():
53-
print(name, ":", json.dumps(example, indent=1))
54-
model_instance = model_cls(**example)
55-
assert model_instance, f"Failed with {name}"
61+
def test_version_info_model_examples(
62+
model_cls: type[BaseModel], example_name: str, example_data: Any
63+
):
64+
assert_validation_model(
65+
model_cls, example_name=example_name, example_data=example_data
66+
)
Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,22 @@
1-
import json
21
from typing import Any
32

43
import pytest
54
import settings_library
65
from pydantic import BaseModel
7-
from pytest_simcore.pydantic_models import walk_model_examples_in_package
6+
from pytest_simcore.pydantic_models import (
7+
assert_validation_model,
8+
walk_model_examples_in_package,
9+
)
810

911

1012
@pytest.mark.parametrize(
1113
"model_cls, example_name, example_data",
1214
walk_model_examples_in_package(settings_library),
1315
)
1416
def test_all_settings_library_models_config_examples(
15-
model_cls: type[BaseModel], example_name: int, example_data: Any
17+
model_cls: type[BaseModel], example_name: str, example_data: Any
1618
):
17-
assert model_cls.model_validate(
18-
example_data
19-
), f"Failed {example_name} : {json.dumps(example_data)}"
19+
20+
assert_validation_model(
21+
model_cls, example_name=example_name, example_data=example_data
22+
)
Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,22 @@
1-
import json
21
from itertools import chain
32
from typing import Any
43

54
import pytest
65
import simcore_service_api_server.models.schemas
76
from pydantic import BaseModel
8-
from pytest_simcore.pydantic_models import walk_model_examples_in_package
7+
from pytest_simcore.pydantic_models import (
8+
assert_validation_model,
9+
walk_model_examples_in_package,
10+
)
911

1012

1113
@pytest.mark.parametrize(
1214
"model_cls, example_name, example_data",
1315
chain(walk_model_examples_in_package(simcore_service_api_server.models)),
1416
)
1517
def test_all_models_library_models_config_examples(
16-
model_cls: type[BaseModel], example_name: int, example_data: Any
18+
model_cls: type[BaseModel], example_name: str, example_data: Any
1719
):
18-
assert model_cls.model_validate(
19-
example_data
20-
), f"Failed {example_name} : {json.dumps(example_data)}"
20+
assert_validation_model(
21+
model_cls, example_name=example_name, example_data=example_data
22+
)

services/api-server/tests/unit/test_models.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@
33
# pylint: disable=too-many-arguments
44
# pylint: disable=unused-argument
55
# pylint: disable=unused-variable
6-
import json
76
from typing import Any
87

98
import pytest
109
import simcore_service_api_server.models
1110
from pydantic import BaseModel
12-
from pytest_simcore.pydantic_models import walk_model_examples_in_package
11+
from pytest_simcore.pydantic_models import (
12+
assert_validation_model,
13+
walk_model_examples_in_package,
14+
)
1315
from simcore_postgres_database.models.users import UserRole
1416
from simcore_service_api_server.models.schemas.profiles import UserRoleEnum
1517

@@ -21,9 +23,9 @@
2123
def test_api_server_model_examples(
2224
model_cls: type[BaseModel], example_name: int, example_data: Any
2325
):
24-
assert model_cls.model_validate(
25-
example_data
26-
), f"Failed {example_name} : {json.dumps(example_data)}"
26+
assert_validation_model(
27+
model_cls, example_name=example_name, example_data=example_data
28+
)
2729

2830

2931
def test_enums_in_sync():

services/catalog/tests/unit/test__model_examples.py

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,24 @@
44
# pylint: disable=unused-argument
55
# pylint: disable=unused-variable
66

7-
import json
87
from typing import Any
98

109
import pytest
1110
import simcore_service_catalog.models
12-
from pydantic import BaseModel, ValidationError
13-
from pytest_simcore.pydantic_models import walk_model_examples_in_package
11+
from pydantic import BaseModel
12+
from pytest_simcore.pydantic_models import (
13+
assert_validation_model,
14+
walk_model_examples_in_package,
15+
)
1416

1517

1618
@pytest.mark.parametrize(
1719
"model_cls, example_name, example_data",
1820
walk_model_examples_in_package(simcore_service_catalog.models),
1921
)
2022
def test_catalog_service_model_examples(
21-
model_cls: type[BaseModel], example_name: int, example_data: Any
23+
model_cls: type[BaseModel], example_name: str, example_data: Any
2224
):
23-
try:
24-
assert model_cls.model_validate(example_data) is not None
25-
except ValidationError as err:
26-
pytest.fail(
27-
f"\n{example_name}: {json.dumps(example_data, indent=1)}\nError: {err}"
28-
)
25+
assert_validation_model(
26+
model_cls, example_name=example_name, example_data=example_data
27+
)

services/director/tests/unit/test__model_examples.py

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,24 @@
44
# pylint: disable=unused-argument
55
# pylint: disable=unused-variable
66

7-
import json
87
from typing import Any
98

109
import pytest
1110
import simcore_service_director.models
12-
from pydantic import BaseModel, ValidationError
13-
from pytest_simcore.pydantic_models import walk_model_examples_in_package
11+
from pydantic import BaseModel
12+
from pytest_simcore.pydantic_models import (
13+
assert_validation_model,
14+
walk_model_examples_in_package,
15+
)
1416

1517

1618
@pytest.mark.parametrize(
1719
"model_cls, example_name, example_data",
1820
walk_model_examples_in_package(simcore_service_director.models),
1921
)
2022
def test_director_service_model_examples(
21-
model_cls: type[BaseModel], example_name: int, example_data: Any
23+
model_cls: type[BaseModel], example_name: str, example_data: Any
2224
):
23-
try:
24-
assert model_cls.model_validate(example_data) is not None
25-
except ValidationError as err:
26-
pytest.fail(
27-
f"\n{example_name}: {json.dumps(example_data, indent=1)}\nError: {err}"
28-
)
25+
assert_validation_model(
26+
model_cls, example_name=example_name, example_data=example_data
27+
)

0 commit comments

Comments
 (0)