Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,3 @@ repos:
- pyyaml
- tomli
always_run: true
- repo: https://github.com/mashi/codecov-validator
rev: 1.0.1
hooks:
- id: ccv
4 changes: 1 addition & 3 deletions space_packet_parser/xtce/parameter_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,7 @@ def get_units(parameter_type_element: ElementTree.Element) -> str | tuple[str, .
"""
# Assume we are not parsing a Time Parameter Type, which stores units differently

units = parameter_type_element.findall("UnitSet/Unit")
units = [u.text for u in units if u.text]
units = parameter_type_element.xpath('.//*[local-name()="UnitSet"]/*[local-name()="Unit"]/text()')

if not units:
# Units are optional so return None if they aren't specified
Expand Down Expand Up @@ -293,7 +292,6 @@ def to_xml(self, *, elmaker: ElementMaker) -> ElementTree.Element:
"""

param_type_element = getattr(elmaker, self.__class__.__name__)(name=self.name)

if self.unit:
if isinstance(self.unit, tuple):
unit_elements = [elmaker.Unit(u) for u in self.unit]
Expand Down
46 changes: 46 additions & 0 deletions tests/unit/test_xtce/test_parameter_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,21 @@ def test_string_parameter_type(elmaker, xtce_parser, xml_string: str, expectatio
),
(
f"""
<xtce:IntegerParameterType xmlns:xtce="{XTCE_1_2_XMLNS}" name="TEST_INT_Type">
<xtce:UnitSet>
<xtce:Unit>km</xtce:Unit>
</xtce:UnitSet>
<xtce:IntegerDataEncoding sizeInBits="16" encoding="unsigned"/>
</xtce:IntegerParameterType>
""",
parameter_types.IntegerParameterType(
name="TEST_INT_Type",
unit="km",
encoding=encodings.IntegerDataEncoding(size_in_bits=16, encoding="unsigned"),
),
),
(
f"""
<xtce:IntegerParameterType xmlns:xtce="{XTCE_1_2_XMLNS}" name="TEST_INT_Type">
<xtce:UnitSet>
<xtce:Unit>m</xtce:Unit>
Expand Down Expand Up @@ -843,3 +858,34 @@ def test_absolute_time_parameter_type(elmaker, xtce_parser, xml_string, expectat
ElementTree.fromstring(result_string, parser=xtce_parser)
)
assert full_circle == expectation


def test_parameter_type_to_xml_unit_serialization(elmaker):
"""Test serialization of UnitSet in ParameterType.to_xml"""

encoding = encodings.IntegerDataEncoding(size_in_bits=16, encoding="unsigned")
ns = {"xtce": XTCE_1_2_XMLNS}

# Single unit
param = parameter_types.IntegerParameterType(
name="TEST_PARAM_Type",
unit="km",
encoding=encoding,
)
element = param.to_xml(elmaker=elmaker)

units = element.findall(".//xtce:Unit", namespaces=ns)
assert len(units) == 1
assert units[0].text == "km"

# Multiple units
param = parameter_types.IntegerParameterType(
name="TEST_PARAM_Type",
unit=("m", "s"),
encoding=encoding,
)
element = param.to_xml(elmaker=elmaker)

units = element.findall(".//xtce:Unit", namespaces=ns)
assert len(units) == 2
assert [u.text for u in units] == ["m", "s"]
Loading