Skip to content

Commit

Permalink
fix: value->variant conversion for empty dict (#2739)
Browse files Browse the repository at this point in the history
* fix: value->variant conversion for empty dict

* fix: test

* fix: test

* fix: add test
  • Loading branch information
mkundu1 committed Apr 29, 2024
1 parent 1e1416a commit 66921e1
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/ansys/fluent/core/services/datamodel_se.py
Expand Up @@ -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])

Expand Down
3 changes: 2 additions & 1 deletion tests/test_data_model_cache.py
Expand Up @@ -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",
Expand Down
24 changes: 24 additions & 0 deletions tests/test_datamodel_service.py
Expand Up @@ -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 (
Expand All @@ -16,13 +17,36 @@
PyNamedObjectContainer,
PyTextual,
ReadOnlyObjectError,
_convert_value_to_variant,
_convert_variant_to_value,
convert_path_to_se_path,
)
from ansys.fluent.core.streaming_services.datamodel_streaming import DatamodelStream
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):
Expand Down

0 comments on commit 66921e1

Please sign in to comment.