From bfbd3bc5f43a92d266fa1ab26949557e43602fa4 Mon Sep 17 00:00:00 2001 From: Mainak Kundu Date: Mon, 16 Sep 2024 09:11:14 -0400 Subject: [PATCH] test: Add test for _combine_set_states --- src/ansys/fluent/core/solver/flobject.py | 13 ++++++--- tests/test_flobject.py | 37 ++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 4 deletions(-) diff --git a/src/ansys/fluent/core/solver/flobject.py b/src/ansys/fluent/core/solver/flobject.py index 837f0d0e7b7..55ec3a9421a 100644 --- a/src/ansys/fluent/core/solver/flobject.py +++ b/src/ansys/fluent/core/solver/flobject.py @@ -665,10 +665,15 @@ def _combine_set_states(states: List[Tuple[str, StateT]]) -> Tuple[str, StateT]: for path, state in states: comps = path.split("/") comps = comps[len(common_path) :] - obj = combined_state - for comp in comps[:-1]: - obj = obj.setdefault(comp, {}) - obj[comps[-1]] = state + if comps: + if not isinstance(combined_state, dict): + combined_state = {} + obj = combined_state + for comp in comps[:-1]: + obj = obj.setdefault(comp, {}) + obj[comps[-1]] = state + else: + combined_state = state return "/".join(common_path), combined_state diff --git a/tests/test_flobject.py b/tests/test_flobject.py index a9fb11dc7f1..9afbb97e25e 100644 --- a/tests/test_flobject.py +++ b/tests/test_flobject.py @@ -11,6 +11,7 @@ from ansys.fluent.core.solver import flobject from ansys.fluent.core.solver.flobject import ( InactiveObjectError, + _combine_set_states, _gethash, find_children, ) @@ -1238,3 +1239,39 @@ def test_default_argument_names_for_commands(static_mixer_settings_session): assert solver.results.graphics.contour.delete.argument_names == ["name_list"] # The following is the default behavior when no arguments are associated with the command. assert solver.results.graphics.contour.list.argument_names == [] + + +def test_combine_set_states(): + assert _combine_set_states( + [ + ("A/B/C", 1), + ] + ) == ("A/B/C", 1) + + assert _combine_set_states( + [ + ("A/B/C", 1), + ("A/B/C", 2), + ] + ) == ("A/B/C", 2) + + assert _combine_set_states( + [ + ("A/B/C", 1), + ("A/B/C", {"X": 2}), + ] + ) == ("A/B/C", {"X": 2}) + + assert _combine_set_states( + [ + ("A/B/C", 1), + ("A/B/D", 2), + ] + ) == ("A/B", {"C": 1, "D": 2}) + + assert _combine_set_states( + [ + ("A/B/C", {"X": 1}), + ("A/B/D/E", 2), + ] + ) == ("A/B", {"C": {"X": 1}, "D": {"E": 2}})