Skip to content

Commit e76f141

Browse files
authored
Add test case and namespace handling for Issues/47 support xtce unitset (#261)
* Add unit tests for EnumeratedParameterType UnitSet serialization, fix namespace handling, removed broken pre-commit hook" * Removed temporary print statements * Replace Codecov validator with local validation hook * Fixed invalid key
1 parent 7d5110e commit e76f141

3 files changed

Lines changed: 48 additions & 8 deletions

File tree

.pre-commit-config.yaml

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
ci:
22
autofix_prs: false
33
autoupdate_schedule: "quarterly"
4-
skip: [no-commit-to-branch, ccv]
4+
skip: [no-commit-to-branch]
55
repos:
66
- repo: https://github.com/pre-commit/pre-commit-hooks
77
rev: v6.0.0
@@ -41,7 +41,3 @@ repos:
4141
- pyyaml
4242
- tomli
4343
always_run: true
44-
- repo: https://github.com/mashi/codecov-validator
45-
rev: 1.0.1
46-
hooks:
47-
- id: ccv

space_packet_parser/xtce/parameter_types.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,7 @@ def get_units(parameter_type_element: ElementTree.Element) -> str | tuple[str, .
126126
"""
127127
# Assume we are not parsing a Time Parameter Type, which stores units differently
128128

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

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

295294
param_type_element = getattr(elmaker, self.__class__.__name__)(name=self.name)
296-
297295
if self.unit:
298296
if isinstance(self.unit, tuple):
299297
unit_elements = [elmaker.Unit(u) for u in self.unit]

tests/unit/test_xtce/test_parameter_types.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,21 @@ def test_string_parameter_type(elmaker, xtce_parser, xml_string: str, expectatio
170170
),
171171
(
172172
f"""
173+
<xtce:IntegerParameterType xmlns:xtce="{XTCE_1_2_XMLNS}" name="TEST_INT_Type">
174+
<xtce:UnitSet>
175+
<xtce:Unit>km</xtce:Unit>
176+
</xtce:UnitSet>
177+
<xtce:IntegerDataEncoding sizeInBits="16" encoding="unsigned"/>
178+
</xtce:IntegerParameterType>
179+
""",
180+
parameter_types.IntegerParameterType(
181+
name="TEST_INT_Type",
182+
unit="km",
183+
encoding=encodings.IntegerDataEncoding(size_in_bits=16, encoding="unsigned"),
184+
),
185+
),
186+
(
187+
f"""
173188
<xtce:IntegerParameterType xmlns:xtce="{XTCE_1_2_XMLNS}" name="TEST_INT_Type">
174189
<xtce:UnitSet>
175190
<xtce:Unit>m</xtce:Unit>
@@ -843,3 +858,34 @@ def test_absolute_time_parameter_type(elmaker, xtce_parser, xml_string, expectat
843858
ElementTree.fromstring(result_string, parser=xtce_parser)
844859
)
845860
assert full_circle == expectation
861+
862+
863+
def test_parameter_type_to_xml_unit_serialization(elmaker):
864+
"""Test serialization of UnitSet in ParameterType.to_xml"""
865+
866+
encoding = encodings.IntegerDataEncoding(size_in_bits=16, encoding="unsigned")
867+
ns = {"xtce": XTCE_1_2_XMLNS}
868+
869+
# Single unit
870+
param = parameter_types.IntegerParameterType(
871+
name="TEST_PARAM_Type",
872+
unit="km",
873+
encoding=encoding,
874+
)
875+
element = param.to_xml(elmaker=elmaker)
876+
877+
units = element.findall(".//xtce:Unit", namespaces=ns)
878+
assert len(units) == 1
879+
assert units[0].text == "km"
880+
881+
# Multiple units
882+
param = parameter_types.IntegerParameterType(
883+
name="TEST_PARAM_Type",
884+
unit=("m", "s"),
885+
encoding=encoding,
886+
)
887+
element = param.to_xml(elmaker=elmaker)
888+
889+
units = element.findall(".//xtce:Unit", namespaces=ns)
890+
assert len(units) == 2
891+
assert [u.text for u in units] == ["m", "s"]

0 commit comments

Comments
 (0)