Skip to content

Commit

Permalink
feat: avoid errors when retrieving invalid routine (#3606)
Browse files Browse the repository at this point in the history
* feat: avoiding getting a wrong routine

* tests: adding tests

* chore: adding changelog file 3606.miscellaneous.md [dependabot-skip]

* chore: adding changelog file 3606.miscellaneous.md [dependabot-skip]

* tests: renaming test

---------

Co-authored-by: pyansys-ci-bot <[email protected]>
  • Loading branch information
germa89 and pyansys-ci-bot authored Dec 13, 2024
1 parent 92f756c commit 3c81f2d
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
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)

0 comments on commit 3c81f2d

Please sign in to comment.