diff --git a/flow360/component/flow360_solver_params.py b/flow360/component/flow360_solver_params.py index fefa5cb93..b6f278d2f 100644 --- a/flow360/component/flow360_solver_params.py +++ b/flow360/component/flow360_solver_params.py @@ -250,10 +250,6 @@ class Flow360MeshParams(BaseModel): boundaries: MeshBoundary = Field() sliding_interfaces: Optional[List[SlidingInterface]] = Field(alias="slidingInterfaces") - # pylint: disable=C0115,R0903 - class Config: - allow_population_by_field_name = True - def json(self, *args, **kwargs): """ to json @@ -266,3 +262,7 @@ def from_file(cls, file: str) -> Flow360MeshParams: read model from json file """ return cls.parse_file(file) + + # pylint: disable=C0115,R0903 + class Config: + allow_population_by_field_name = True diff --git a/flow360/component/volume_mesh.py b/flow360/component/volume_mesh.py index 717d4139c..932de1de6 100644 --- a/flow360/component/volume_mesh.py +++ b/flow360/component/volume_mesh.py @@ -8,7 +8,7 @@ from typing import Optional, Union, List import numpy as np -from pydantic import Extra, Field, validator +from pydantic import Extra, Field, validator, ValidationError from ..cloud.s3_utils import S3TransferType from ..cloud.rest_api import RestApi @@ -306,9 +306,20 @@ def init_mesh_params(cls, value): """ validator for mesh_params """ + params = value if isinstance(value, str): - return json.loads(value) - return value + try: + params = json.loads(value) + except json.decoder.JSONDecodeError: + return None + try: + Flow360MeshParams(**params) + except ValidationError: + return None + except TypeError: + return None + + return params @validator("endianness", pre=True) def init_endianness(cls, value): diff --git a/tests/test_volume_mesh.py b/tests/test_volume_mesh.py index 921e5e3a5..86d73747d 100644 --- a/tests/test_volume_mesh.py +++ b/tests/test_volume_mesh.py @@ -18,8 +18,11 @@ VolumeMeshFileFormat, CompressionFormat, UGRIDEndianness, + VolumeMeshMeta, ) +from tests.data.volume_mesh_list import volume_mesh_list_raw + @pytest.fixture(autouse=True) def change_test_dir(request, monkeypatch): @@ -121,3 +124,33 @@ def test_mesh_filename_detection(): mesh_format = VolumeMeshFileFormat.detect(filename) with pytest.raises(RuntimeError): endianess = UGRIDEndianness.detect(filename) + + +def test_volume_mesh_list_with_incorrect_data(): + v = VolumeMeshMeta(**volume_mesh_list_raw[0]) + assert v.status == "uploaded" + assert type(v.mesh_params) is Flow360MeshParams + assert type(v.mesh_params.boundaries) is MeshBoundary + assert v.mesh_params.boundaries.no_slip_walls == ["1", "wall"] + + v = VolumeMeshMeta(**volume_mesh_list_raw[1]) + assert v.status == "uploaded" + assert type(v.mesh_params) is Flow360MeshParams + assert type(v.mesh_params.boundaries) is MeshBoundary + assert v.mesh_params.boundaries.no_slip_walls == ["4"] + + v = VolumeMeshMeta(**volume_mesh_list_raw[2]) + assert v.status == "uploaded" + assert type(v.mesh_params) is Flow360MeshParams + assert type(v.mesh_params.boundaries) is MeshBoundary + assert v.mesh_params.boundaries.no_slip_walls == ["1"] + + item_incorrect1 = volume_mesh_list_raw[3] + v = VolumeMeshMeta(**item_incorrect1) + assert v.status == "error" + assert v.mesh_params is None + + item_incorrect2 = volume_mesh_list_raw[4] + v = VolumeMeshMeta(**item_incorrect2) + assert v.status == "error" + assert v.mesh_params is None