From 4fdb9a3f96bcc40b8eac00925f0c06991443a32b Mon Sep 17 00:00:00 2001 From: Iason Batzianoulis Date: Mon, 16 Sep 2024 13:30:33 +0200 Subject: [PATCH] build preset to take only supported values (#84) --- .../simai/core/data/model_configuration.py | 40 +++++++++++----- tests/test_model_configuration.py | 47 +++++++++++++++++++ 2 files changed, 76 insertions(+), 11 deletions(-) create mode 100644 tests/test_model_configuration.py diff --git a/src/ansys/simai/core/data/model_configuration.py b/src/ansys/simai/core/data/model_configuration.py index fe0c79aa..69983a66 100644 --- a/src/ansys/simai/core/data/model_configuration.py +++ b/src/ansys/simai/core/data/model_configuration.py @@ -28,6 +28,13 @@ if TYPE_CHECKING: from ansys.simai.core.data.projects import Project +SupportedBuildPresets = { + "debug": "debug", + "1_day": "short", + "2_days": "default", + "7_days": "long", +} + @dataclass class DomainAxisDefinition: @@ -210,11 +217,11 @@ class ModelConfiguration: | *debug*: < 30 min, only 4 dat - | *short*: < 24 hours + | *1_day*: < 24 hours - | *default*: < 2 days, default value. + | *2_days*: < 2 days, default value. - | *long*: < 1 week + | *7_days*: < 1 week continuous: indicates if continuous learning is enabled. Default is False. input: the inputs of the model. output: the outputs of the model. @@ -280,12 +287,6 @@ class ModelConfiguration: """ project: "Optional[Project]" = None - build_preset: Literal[ - "debug", - "short", - "default", - "long", - ] = "default" continuous: bool = False input: ModelInput = field(default_factory=lambda: ModelInput()) output: ModelOutput = field(default_factory=lambda: ModelOutput()) @@ -313,11 +314,26 @@ def __get_gc(self): global_coefficients: list[GlobalCoefficientDefinition] = property(__get_gc, __set_gc) + def __validate_build_preset(self, val: str): + if val not in SupportedBuildPresets: + raise InvalidArguments( + f"Invalid value for build_preset, build_preset should be one of {list(SupportedBuildPresets.keys())}" + ) + + def __set_build_preset(self, val: str): + self.__validate_build_preset(val) + self.__dict__["build_preset"] = val + + def __get_build_preset(self): + return self.__dict__.get("build_preset") + + build_preset = property(__get_build_preset, __set_build_preset) + def __init__( self, project: "Project", boundary_conditions: Optional[dict[str, Any]] = None, - build_preset: Optional[str] = None, + build_preset: Optional[str] = "debug", continuous: bool = False, fields: Optional[dict[str, Any]] = None, global_coefficients: Optional[list[GlobalCoefficientDefinition]] = None, @@ -441,7 +457,7 @@ def _to_payload(self): return { "boundary_conditions": bcs, - "build_preset": self.build_preset, + "build_preset": SupportedBuildPresets[self.build_preset], "continuous": self.continuous, "fields": flds, "global_coefficients": gcs, @@ -462,3 +478,5 @@ def compute_global_coefficient(self): ) for gc in self.global_coefficients ] + + del __set_build_preset, __get_build_preset diff --git a/tests/test_model_configuration.py b/tests/test_model_configuration.py new file mode 100644 index 00000000..9c60ea73 --- /dev/null +++ b/tests/test_model_configuration.py @@ -0,0 +1,47 @@ +# Copyright (C) 2024 ANSYS, Inc. and/or its affiliates. +# SPDX-License-Identifier: MIT +# +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in all +# copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +# SOFTWARE. + + +import pytest + +from ansys.simai.core.data.model_configuration import ModelConfiguration, SupportedBuildPresets +from ansys.simai.core.errors import InvalidArguments + + +def test_build_preset_error(simai_client): + """WHEN build_preset gets a non-supported value + THEN an InvalidArgument is raised.""" + + raw_project = { + "id": "xX007Xx", + "name": "fifi", + } + + project = simai_client._project_directory._model_from(raw_project) + + with pytest.raises(InvalidArguments) as excinfo: + ModelConfiguration( + project=project, + build_preset="not_valid_value", + ) + + assert f"{list(SupportedBuildPresets)}" in excinfo.value