From 66921e114fd1d2be058a50ee7a1b9ac36f6ac41c Mon Sep 17 00:00:00 2001 From: Mainak Kundu <94432368+mkundu1@users.noreply.github.com> Date: Mon, 29 Apr 2024 07:25:39 -0400 Subject: [PATCH] fix: value->variant conversion for empty dict (#2739) * fix: value->variant conversion for empty dict * fix: test * fix: test * fix: add test --- .../fluent/core/services/datamodel_se.py | 1 + tests/test_data_model_cache.py | 3 ++- tests/test_datamodel_service.py | 24 +++++++++++++++++++ 3 files changed, 27 insertions(+), 1 deletion(-) diff --git a/src/ansys/fluent/core/services/datamodel_se.py b/src/ansys/fluent/core/services/datamodel_se.py index e719a5c6110..bfb4d535b3c 100644 --- a/src/ansys/fluent/core/services/datamodel_se.py +++ b/src/ansys/fluent/core/services/datamodel_se.py @@ -295,6 +295,7 @@ def _convert_value_to_variant(val: _TValue, var: Variant) -> None: item_var = var.variant_vector_state.item.add() _convert_value_to_variant(item, item_var) elif isinstance(val, dict): + var.variant_map_state.SetInParent() for k, v in val.items(): _convert_value_to_variant(v, var.variant_map_state.item[k]) diff --git a/tests/test_data_model_cache.py b/tests/test_data_model_cache.py index 55889ca5f49..515488aa4b0 100644 --- a/tests/test_data_model_cache.py +++ b/tests/test_data_model_cache.py @@ -49,7 +49,8 @@ def test_data_model_cache(): ({"r1": {}}, "r1", {"A": [3.0, 6.0]}, [], {"r1": {"A": [3.0, 6.0]}}), ({"r1": {}}, "r1", {"A": ["ab", "cd"]}, [], {"r1": {"A": ["ab", "cd"]}}), ({"r1": {"A": {}}}, "r1", {"A": {"B": 5}}, [], {"r1": {"A": {"B": 5}}}), - ({"r1": {"A": 5}}, "r1", {"A": {}}, [], {"r1": {"A": None}}), + ({"r1": {"A": 5}}, "r1", {"A": {}}, [], {"r1": {"A": 5}}), + ({"r1": {"A": 5}}, "r1", {"A": None}, [], {"r1": {"A": None}}), ( {"r1": {"A": {}}}, "r1", diff --git a/tests/test_datamodel_service.py b/tests/test_datamodel_service.py index a16a09eb077..0f1dc9bdf50 100644 --- a/tests/test_datamodel_service.py +++ b/tests/test_datamodel_service.py @@ -7,6 +7,7 @@ from util.solver_workflow import new_solver_session # noqa: F401 from ansys.api.fluent.v0 import datamodel_se_pb2 +from ansys.api.fluent.v0.variant_pb2 import Variant import ansys.fluent.core as pyfluent from ansys.fluent.core import examples from ansys.fluent.core.services.datamodel_se import ( @@ -16,6 +17,7 @@ PyNamedObjectContainer, PyTextual, ReadOnlyObjectError, + _convert_value_to_variant, _convert_variant_to_value, convert_path_to_se_path, ) @@ -23,6 +25,28 @@ from ansys.fluent.core.utils.execution import timeout_loop +@pytest.mark.parametrize( + "value,expected", + [ + (None, None), + (False, False), + (True, True), + (1, 1), + (1.0, 1.0), + ("abc", "abc"), + ((1, 2, 3), [1, 2, 3]), + ([1, 2, 3], [1, 2, 3]), + ({"a": 5}, {"a": 5}), + ({"a": [1, 2, 3]}, {"a": [1, 2, 3]}), + ({"a": {"b": 5}}, {"a": {"b": 5}}), + ], +) +def test_convert_value_to_variant_to_value(value, expected): + variant = Variant() + _convert_value_to_variant(value, variant) + assert expected == _convert_variant_to_value(variant) + + @pytest.mark.fluent_version(">=23.2") @pytest.mark.codegen_required def test_event_subscription(new_mesh_session):