Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: avoid errors when retrieving invalid routine #3606

Merged
merged 8 commits into from
Dec 13, 2024
1 change: 1 addition & 0 deletions doc/changelog.d/3606.miscellaneous.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
feat: avoid errors when retrieving invalid routine
9 changes: 8 additions & 1 deletion src/ansys/mapdl/core/parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,14 @@ def routine(self) -> str:
>>> mapdl.parameters.routine
'PREP7'
"""
value = self._mapdl.get_value("ACTIVE", item1="ROUT")
value = int(self._mapdl.get_value("ACTIVE", item1="ROUT"))
if value not in ROUTINE_MAP:
self._mapdl.logger.info(
f"Getting a valid routine number failed. Routine obtained is {value}. Executing 'FINISH'."
)
self._mapdl.finish()
value = 0

return ROUTINE_MAP[int(value)]

@property
Expand Down
24 changes: 24 additions & 0 deletions tests/test_parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

import logging
import re
from unittest.mock import patch

import numpy as np
import pytest
Expand Down Expand Up @@ -470,3 +472,25 @@ def test_non_interactive(mapdl, cleared):
mapdl.parameters["qwer"] = 3

assert mapdl.parameters["qwer"] == 3


@pytest.mark.parametrize("value", [121, 299])
def test_failing_get_routine(mapdl, caplog, value):
from ansys.mapdl.core.parameters import ROUTINE_MAP

prev_level = mapdl.logger.logger.level
mapdl.logger.setLevel(logging.INFO)

with patch("ansys.mapdl.core.mapdl_extended._MapdlExtended.get_value") as mck:
mck.return_value = value
with caplog.at_level(logging.INFO):
routine = mapdl.parameters.routine

mck.assert_called_once()

txt = str(caplog.text)
assert f"Getting a valid routine number failed." in txt
assert f"Routine obtained is {value}. Executing 'FINISH'." in txt
assert routine == ROUTINE_MAP[0]

mapdl.logger.setLevel(prev_level)
Loading