Skip to content

Commit 2139509

Browse files
yashhzdamilcarlucas
authored andcommitted
fix(export): address review - widen mavproxy column, add round-trip test
- Update MAVProxy width specifier from 8 to 14 to accommodate .10f values - Add found-flags to ensure all small values are present in formatted output - Add true export→reimport round-trip test verifying numeric values survive Signed-off-by: Yash Goel <yashgoel249@gmail.com>
1 parent ac017e5 commit 2139509

2 files changed

Lines changed: 50 additions & 8 deletions

File tree

ardupilot_methodic_configurator/data_model_par_dict.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -265,9 +265,9 @@ def _format_params(self, file_format: str = "missionplanner") -> list[str]:
265265
sorted_dict = ParDict(dict(sorted(self.items())))
266266
formatted_params = [
267267
(
268-
f"{key:<16} {parameter.value:<8.10f} # {parameter.comment}"
268+
f"{key:<16} {parameter.value:<14.10f} # {parameter.comment}"
269269
if isinstance(parameter, Par) and parameter.comment
270-
else f"{key:<16} {parameter.value if isinstance(parameter, Par) else parameter:<8.10f}"
270+
else f"{key:<16} {parameter.value if isinstance(parameter, Par) else parameter:<14.10f}"
271271
)
272272
for key, parameter in sorted_dict.items()
273273
]

tests/test_data_model_par_dict.py

Lines changed: 48 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -563,8 +563,8 @@ def test_user_can_export_parameters_to_mavproxy_format(self, parameter_dict) ->
563563
content = f.read()
564564

565565
# MAVProxy format uses fixed-width columns
566-
assert "ACRO_YAW_P 4.5000000000 # Yaw P gain" in content
567-
assert "GPS_TYPE 1.0000000000" in content
566+
assert "ACRO_YAW_P 4.5000000000 # Yaw P gain" in content
567+
assert "GPS_TYPE 1.0000000000 " in content
568568
finally:
569569
os.unlink(output_file)
570570

@@ -943,7 +943,7 @@ def test_user_can_format_parameters_for_mavproxy(self, parameter_dict) -> None:
943943

944944
# Assert: Correct MAVProxy format (space-separated, fixed width)
945945
yaw_line = next((line for line in formatted if "ACRO_YAW_P" in line), "")
946-
assert yaw_line == "ACRO_YAW_P 4.5000000000 # Yaw P gain"
946+
assert yaw_line == "ACRO_YAW_P 4.5000000000 # Yaw P gain"
947947

948948
def test_user_receives_error_for_unsupported_format(self, parameter_dict) -> None:
949949
"""
@@ -964,8 +964,8 @@ def test_user_preserves_small_parameter_values_on_export(self) -> None:
964964
User preserves parameter values smaller than 1e-6 during export.
965965
966966
GIVEN: A user has parameters with very small but nonzero values
967-
WHEN: They export and reimport using MissionPlanner or MAVProxy format
968-
THEN: The values should survive the round-trip without silent data loss
967+
WHEN: They format using MissionPlanner or MAVProxy format
968+
THEN: The formatted strings should retain the small values
969969
"""
970970
# Arrange: Parameters with values that would be truncated by .6f formatting
971971
small_value_params = ParDict(
@@ -980,14 +980,56 @@ def test_user_preserves_small_parameter_values_on_export(self) -> None:
980980
for file_format in ("missionplanner", "mavproxy"):
981981
formatted = small_value_params._format_params(file_format) # pylint: disable=protected-access
982982

983-
# Extract the numeric value strings from the formatted output
983+
tiny_found = False
984+
micro_found = False
985+
boundary_found = False
984986
for line in formatted:
985987
if "TINY_VAL" in line:
986988
assert "0.0000001" in line, f"Value 1e-7 lost in {file_format} format: {line}"
989+
tiny_found = True
987990
if "MICRO_VAL" in line:
988991
assert "0.0000005" in line, f"Value 5e-7 lost in {file_format} format: {line}"
992+
micro_found = True
989993
if "BOUNDARY_VAL" in line:
990994
assert "0.000001" in line, f"Value 1e-6 lost in {file_format} format: {line}"
995+
boundary_found = True
996+
997+
assert tiny_found, f"TINY_VAL not found in {file_format} formatted output"
998+
assert micro_found, f"MICRO_VAL not found in {file_format} formatted output"
999+
assert boundary_found, f"BOUNDARY_VAL not found in {file_format} formatted output"
1000+
1001+
def test_user_preserves_small_values_on_export_reimport_round_trip(self) -> None:
1002+
"""
1003+
User preserves small parameter values through a full export→reimport round-trip.
1004+
1005+
GIVEN: A user has parameters with very small but nonzero values
1006+
WHEN: They export to a file and reimport it
1007+
THEN: The numeric values should match within float32 tolerance
1008+
"""
1009+
small_value_params = ParDict(
1010+
{
1011+
"TINY_VAL": Par(0.0000001, "Very small value"),
1012+
"BOUNDARY_VAL": Par(0.000001, "Boundary value"),
1013+
"NORMAL_VAL": Par(1.5, "Normal value"),
1014+
}
1015+
)
1016+
1017+
for file_format in ("missionplanner", "mavproxy"):
1018+
with tempfile.NamedTemporaryFile(mode="w", suffix=".param", delete=False) as f:
1019+
output_file = f.name
1020+
1021+
try:
1022+
small_value_params.export_to_param(output_file, file_format)
1023+
reloaded = ParDict.load_param_file_into_dict(output_file)
1024+
1025+
for key, original in small_value_params.items():
1026+
original_val = original.value if isinstance(original, Par) else original
1027+
reloaded_val = reloaded[key].value if isinstance(reloaded[key], Par) else reloaded[key]
1028+
assert abs(reloaded_val - original_val) < 1e-10, (
1029+
f"{key}: value {original_val} became {reloaded_val} after {file_format} round-trip"
1030+
)
1031+
finally:
1032+
os.unlink(output_file)
9911033

9921034
def test_user_can_annotate_parameters_with_comments(self, parameter_dict) -> None:
9931035
"""

0 commit comments

Comments
 (0)