Skip to content

Commit 5f4fb80

Browse files
committed
check compressed payloads for correct type
1 parent 2f18fd6 commit 5f4fb80

File tree

3 files changed

+35
-3
lines changed

3 files changed

+35
-3
lines changed

src/pynxtools/dataconverter/helpers.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -742,7 +742,11 @@ def is_valid_data_field(
742742
"""
743743

744744
accepted_types = NEXUS_TO_PYTHON_DATA_TYPES[nxdl_type]
745-
# Do not count the dict as it represents a link value
745+
746+
if isinstance(value, dict) and set(value.keys()) == {"compress", "strength"}:
747+
value = value["compress"]
748+
749+
# Do not count other dicts as they represent a link value
746750
if not isinstance(value, dict) and not is_valid_data_type(value, accepted_types):
747751
# try to convert string to bool
748752
if accepted_types[0] is bool and isinstance(value, str):

src/pynxtools/dataconverter/validation.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -821,7 +821,7 @@ def is_documented(key: str, tree: NexusNode) -> bool:
821821
# Collection found, mark as documented
822822
return True
823823

824-
if isinstance(mapping[key], dict) and "link" in mapping[key]:
824+
if isinstance(mapping[key], Mapping) and "link" in mapping[key]:
825825
resolved_link = _follow_link({key: mapping[key]}, "")
826826

827827
if key not in resolved_link:

tests/dataconverter/test_validation.py

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1702,15 +1702,43 @@ def listify_template(data_dict: Template):
17021702
[],
17031703
id="appdef-compressed-payload",
17041704
),
1705+
pytest.param(
1706+
alter_dict(
1707+
TEMPLATE,
1708+
"/ENTRY[my_entry]/NXODD_name[nxodd_name]/float_value",
1709+
{"compress": np.int64(2.0), "strength": 1},
1710+
),
1711+
[
1712+
"The value at /ENTRY[my_entry]/NXODD_name[nxodd_name]/float_value "
1713+
"should be one of the following Python types: "
1714+
"(<class 'float'>, <class 'numpy.floating'>), as defined in the "
1715+
"NXDL as NX_FLOAT."
1716+
],
1717+
id="appdef-compressed-payload-wrong-type",
1718+
),
17051719
pytest.param(
17061720
alter_dict(
17071721
TEMPLATE,
17081722
"/ENTRY[my_entry]/SAMPLE[sample1]]/changer_position",
1709-
{"compress": np.float32(2.0), "strength": 1},
1723+
{"compress": np.int64(2), "strength": 1},
17101724
),
17111725
[],
17121726
id="baseclass-compressed-payload",
17131727
),
1728+
pytest.param(
1729+
alter_dict(
1730+
TEMPLATE,
1731+
"/ENTRY[my_entry]/SAMPLE[sample1]]/changer_position",
1732+
{"compress": np.float32(2.0), "strength": 3},
1733+
),
1734+
[
1735+
"The value at /ENTRY[my_entry]/SAMPLE[sample1]]/changer_position "
1736+
"should be one of the following Python types: "
1737+
"(<class 'int'>, <class 'numpy.integer'>), as defined in the "
1738+
"NXDL as NX_INT."
1739+
],
1740+
id="baseclass-compressed-payload-wrong-type",
1741+
),
17141742
],
17151743
)
17161744
def test_validate_data_dict(caplog, data_dict, error_messages, request):

0 commit comments

Comments
 (0)