Skip to content

Commit 5825968

Browse files
authored
Normalize strings before converting to protobuf enum values (#848)
Allow normalizing strings before converting them to a protobuf enum value. By default, the string is converted to lowercase. This means that all capitalizations of the string will be accepted, for example "SI", "Si", "si" will all be accepted in the unit system parameter. Closes #793.
1 parent 7f32cb6 commit 5825968

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

src/ansys/acp/core/_tree_objects/_grpc_helpers/enum_wrapper.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ def wrap_to_string_enum(
4343
doc: str,
4444
explicit_value_list: tuple[int, ...] | None = None,
4545
extra_aliases: Mapping[str, tuple[str, str]] = types.MappingProxyType({}),
46+
normalization_function: Callable[[str], str] = lambda val: val.lower(),
4647
) -> tuple[_StrEnumT, Callable[[_StrEnumT], int], Callable[[int], _StrEnumT]]:
4748
"""Create a string Enum with the same keys as the given protobuf Enum.
4849
@@ -64,6 +65,10 @@ def wrap_to_string_enum(
6465
and the alias field value.
6566
Note that the alias will not be used when converting from the protobuf value to the string
6667
enum: the primary field name will be used instead.
68+
normalization_function :
69+
A callable which is applied to strings before converting them to protobuf. This means that
70+
any string which will result in an enum value will be accepted, for example if it has the
71+
wrong capitalization.
6772
6873
Returns
6974
-------
@@ -93,6 +98,7 @@ def wrap_to_string_enum(
9398
res_enum.__doc__ = doc
9499

95100
def to_pb_conversion_func(val: _StrEnumT) -> int:
101+
val = normalization_function(val)
96102
val = res_enum(val) # generates a nicer error if 'val' is not a valid enum value
97103
return to_pb_conversion_dict[val]
98104

tests/unittests/test_acp_instance.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929

3030
import pytest
3131

32+
import ansys.acp.core as pyacp
33+
3234

3335
def test_server_version(acp_instance):
3436
version = acp_instance.server_version
@@ -88,6 +90,35 @@ def test_import_inexistent(acp_instance):
8890
assert filename in str(exc.value)
8991

9092

93+
@pytest.mark.parametrize(
94+
"input_unit_system,expected_unit_system",
95+
[
96+
("SI", pyacp.UnitSystemType.SI),
97+
("Si", pyacp.UnitSystemType.SI),
98+
("si", pyacp.UnitSystemType.SI),
99+
(pyacp.UnitSystemType.SI, pyacp.UnitSystemType.SI),
100+
("uMKS", pyacp.UnitSystemType.uMKS),
101+
],
102+
)
103+
def test_import_unit_system(acp_instance, model_data_dir, input_unit_system, expected_unit_system):
104+
model = acp_instance.import_model(
105+
model_data_dir / "class40.cdb",
106+
unit_system=input_unit_system,
107+
format="ansys:cdb",
108+
)
109+
assert model.unit_system == expected_unit_system
110+
111+
112+
@pytest.mark.parametrize("format", ["ansys:cdb", "AnSYS:cDB", pyacp.FeFormat.ANSYS_CDB])
113+
def test_format_capitalization(acp_instance, model_data_dir, format):
114+
"""Test that the format is case insensitive."""
115+
acp_instance.import_model(
116+
model_data_dir / "class40.cdb",
117+
unit_system="SI",
118+
format=format,
119+
)
120+
121+
91122
def test_restart_wait(acp_instance, load_model_from_tempfile):
92123
"""Test that the server is accessible after restart.
93124

0 commit comments

Comments
 (0)