Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions flow360/component/flow360_solver_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
17 changes: 14 additions & 3 deletions flow360/component/volume_mesh.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand Down
33 changes: 33 additions & 0 deletions tests/test_volume_mesh.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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