|
11 | 11 | """
|
12 | 12 |
|
13 | 13 | import unittest
|
| 14 | +from os import path as os_path |
14 | 15 | from unittest.mock import MagicMock, patch
|
15 | 16 |
|
16 | 17 | from ardupilot_methodic_configurator.backend_filesystem import LocalFilesystem
|
@@ -250,6 +251,215 @@ def test_get_upload_local_and_remote_filenames(self) -> None:
|
250 | 251 | assert result == ("vehicle_dir/source_local", "dest_on_fc")
|
251 | 252 | mock_join.assert_called_once_with("vehicle_dir", "source_local")
|
252 | 253 |
|
| 254 | + def test_copy_fc_values_to_file(self) -> None: |
| 255 | + lfs = LocalFilesystem("vehicle_dir", "vehicle_type", None, allow_editing_template_files=False) |
| 256 | + test_params = {"PARAM1": 1.0, "PARAM2": 2.0} |
| 257 | + test_file = "test.param" |
| 258 | + |
| 259 | + # Test with non-existent file |
| 260 | + result = lfs.copy_fc_values_to_file(test_file, test_params) |
| 261 | + assert result == 0 |
| 262 | + |
| 263 | + # Test with existing file and matching parameters |
| 264 | + lfs.file_parameters = {test_file: {"PARAM1": MagicMock(value=0.0), "PARAM2": MagicMock(value=0.0)}} |
| 265 | + result = lfs.copy_fc_values_to_file(test_file, test_params) |
| 266 | + assert result == 2 |
| 267 | + assert lfs.file_parameters[test_file]["PARAM1"].value == 1.0 |
| 268 | + assert lfs.file_parameters[test_file]["PARAM2"].value == 2.0 |
| 269 | + |
| 270 | + def test_write_and_read_last_uploaded_filename(self) -> None: |
| 271 | + lfs = LocalFilesystem("vehicle_dir", "vehicle_type", None, allow_editing_template_files=False) |
| 272 | + test_filename = "test.param" |
| 273 | + |
| 274 | + # Test writing |
| 275 | + expected_path = os_path.join("vehicle_dir", "last_uploaded_filename.txt") |
| 276 | + with patch("builtins.open", unittest.mock.mock_open()) as mock_file: |
| 277 | + lfs.write_last_uploaded_filename(test_filename) |
| 278 | + mock_file.assert_called_once_with(expected_path, "w", encoding="utf-8") |
| 279 | + mock_file().write.assert_called_once_with(test_filename) |
| 280 | + |
| 281 | + # Test reading |
| 282 | + with patch("builtins.open", unittest.mock.mock_open(read_data=test_filename)) as mock_file: |
| 283 | + result = lfs._LocalFilesystem__read_last_uploaded_filename() |
| 284 | + assert result == test_filename |
| 285 | + mock_file.assert_called_once_with(expected_path, encoding="utf-8") |
| 286 | + |
| 287 | + def test_write_param_default_values(self) -> None: |
| 288 | + lfs = LocalFilesystem("vehicle_dir", "vehicle_type", None, allow_editing_template_files=False) |
| 289 | + |
| 290 | + # Test with new values |
| 291 | + new_values = {"PARAM1": MagicMock(value=1.0)} |
| 292 | + result = lfs.write_param_default_values(new_values) |
| 293 | + assert result is True |
| 294 | + assert lfs.param_default_dict == new_values |
| 295 | + |
| 296 | + # Test with same values (no change) |
| 297 | + result = lfs.write_param_default_values(new_values) |
| 298 | + assert result is False |
| 299 | + |
| 300 | + def test_get_start_file(self) -> None: |
| 301 | + lfs = LocalFilesystem("vehicle_dir", "vehicle_type", None, allow_editing_template_files=False) |
| 302 | + lfs.file_parameters = {"01_file.param": {}, "02_file.param": {}, "03_file.param": {}} |
| 303 | + |
| 304 | + # Test with explicit index |
| 305 | + result = lfs.get_start_file(1, True) # noqa: FBT003 |
| 306 | + assert result == "02_file.param" |
| 307 | + |
| 308 | + # Test with out of range index |
| 309 | + result = lfs.get_start_file(5, True) # noqa: FBT003 |
| 310 | + assert result == "03_file.param" |
| 311 | + |
| 312 | + # Test with tcal available |
| 313 | + result = lfs.get_start_file(-1, True) # noqa: FBT003 |
| 314 | + assert result == "01_file.param" |
| 315 | + |
| 316 | + # Test with tcal not available |
| 317 | + result = lfs.get_start_file(-1, False) # noqa: FBT003 |
| 318 | + assert result == "03_file.param" |
| 319 | + |
| 320 | + def test_get_eval_variables(self) -> None: |
| 321 | + lfs = LocalFilesystem("vehicle_dir", "vehicle_type", None, allow_editing_template_files=False) |
| 322 | + |
| 323 | + # Test with empty components and doc_dict |
| 324 | + result = lfs.get_eval_variables() |
| 325 | + assert result == {} |
| 326 | + |
| 327 | + # Test with components and doc_dict |
| 328 | + lfs.vehicle_components = {"Components": {"test": "value"}} |
| 329 | + lfs.doc_dict = {"param": "doc"} |
| 330 | + result = lfs.get_eval_variables() |
| 331 | + assert "vehicle_components" in result |
| 332 | + assert "doc_dict" in result |
| 333 | + assert result["vehicle_components"] == {"test": "value"} |
| 334 | + assert result["doc_dict"] == {"param": "doc"} |
| 335 | + |
| 336 | + def test_tolerance_check(self) -> None: |
| 337 | + """Test numerical value comparison with tolerances.""" |
| 338 | + LocalFilesystem("vehicle_dir", "vehicle_type", None, allow_editing_template_files=False) |
| 339 | + |
| 340 | + # Test exact match |
| 341 | + x, y = 1.0, 1.0 |
| 342 | + assert abs(x - y) <= 1e-08 + (1e-03 * abs(y)) |
| 343 | + |
| 344 | + # Test within absolute tolerance |
| 345 | + x, y = 1.0, 1.00000001 |
| 346 | + assert abs(x - y) <= 1e-08 + (1e-03 * abs(y)) |
| 347 | + |
| 348 | + # Test within relative tolerance |
| 349 | + x, y = 1.0, 1.001 |
| 350 | + assert abs(x - y) <= 1e-08 + (1e-03 * abs(y)) |
| 351 | + |
| 352 | + # Test outside both tolerances |
| 353 | + x, y = 1.0, 1.1 |
| 354 | + assert not abs(x - y) <= 1e-08 + (1e-03 * abs(y)) |
| 355 | + |
| 356 | + # Test with custom tolerances |
| 357 | + x, y = 1.0, 1.5 |
| 358 | + assert abs(x - y) <= 1.0 + (1e-03 * abs(y)) # atol=1.0 |
| 359 | + x, y = 1.0, 2.0 |
| 360 | + assert abs(x - y) <= 1e-08 + (1.0 * abs(y)) # rtol=1.0 |
| 361 | + |
| 362 | + def test_categorize_parameters(self) -> None: |
| 363 | + lfs = LocalFilesystem("vehicle_dir", "vehicle_type", None, allow_editing_template_files=False) |
| 364 | + lfs.param_default_dict = {"PARAM1": MagicMock(value=1.0)} |
| 365 | + lfs.doc_dict = {"PARAM1": {"ReadOnly": True}, "PARAM2": {"Calibration": True}, "PARAM3": {}} |
| 366 | + |
| 367 | + test_params = {"PARAM1": MagicMock(value=2.0), "PARAM2": MagicMock(value=2.0), "PARAM3": MagicMock(value=2.0)} |
| 368 | + |
| 369 | + readonly, calibration, other = lfs.categorize_parameters(test_params) |
| 370 | + |
| 371 | + assert "PARAM1" in readonly |
| 372 | + assert "PARAM2" in calibration |
| 373 | + assert "PARAM3" in other |
| 374 | + |
| 375 | + def test_copy_fc_params_values_to_template_created_vehicle_files(self) -> None: |
| 376 | + lfs = LocalFilesystem("vehicle_dir", "vehicle_type", None, allow_editing_template_files=False) |
| 377 | + fc_parameters = {"PARAM1": 1.0, "PARAM2": 2.0} |
| 378 | + |
| 379 | + # Test with empty file_parameters |
| 380 | + result = lfs.copy_fc_params_values_to_template_created_vehicle_files(fc_parameters) |
| 381 | + assert result == "" |
| 382 | + |
| 383 | + # Test with file_parameters and configuration_steps |
| 384 | + param1_mock = MagicMock() |
| 385 | + param1_mock.value = 0.0 |
| 386 | + param2_mock = MagicMock() |
| 387 | + param2_mock.value = 0.0 |
| 388 | + |
| 389 | + lfs.file_parameters = {"test.param": {"PARAM1": param1_mock, "PARAM2": param2_mock}} |
| 390 | + lfs.configuration_steps = {"test.param": {"forced": {}, "derived": {}}} |
| 391 | + |
| 392 | + with ( |
| 393 | + patch("ardupilot_methodic_configurator.backend_filesystem.Par.export_to_param") as mock_export, |
| 394 | + patch("ardupilot_methodic_configurator.backend_filesystem.Par.format_params") as mock_format, |
| 395 | + ): |
| 396 | + mock_format.return_value = "formatted_params" |
| 397 | + |
| 398 | + result = lfs.copy_fc_params_values_to_template_created_vehicle_files(fc_parameters) |
| 399 | + assert result == "" |
| 400 | + assert param1_mock.value == 1.0 |
| 401 | + assert param2_mock.value == 2.0 |
| 402 | + mock_export.assert_called_once_with("formatted_params", os_path.join("vehicle_dir", "test.param")) |
| 403 | + |
| 404 | + def test_write_param_default_values_to_file(self) -> None: |
| 405 | + lfs = LocalFilesystem("vehicle_dir", "vehicle_type", None, allow_editing_template_files=False) |
| 406 | + param_mock = MagicMock() |
| 407 | + param_mock.value = 1.0 |
| 408 | + param_default_values = {"PARAM1": param_mock} |
| 409 | + |
| 410 | + with ( |
| 411 | + patch("ardupilot_methodic_configurator.backend_filesystem.Par.export_to_param") as mock_export, |
| 412 | + patch("ardupilot_methodic_configurator.backend_filesystem.Par.format_params") as mock_format, |
| 413 | + ): |
| 414 | + mock_format.return_value = "formatted_params" |
| 415 | + |
| 416 | + # Test with new values |
| 417 | + lfs.write_param_default_values_to_file(param_default_values) |
| 418 | + assert lfs.param_default_dict == param_default_values |
| 419 | + mock_format.assert_called_with(param_default_values) |
| 420 | + mock_export.assert_called_with("formatted_params", os_path.join("vehicle_dir", "00_default.param")) |
| 421 | + |
| 422 | + # Test with same values (no change) |
| 423 | + mock_export.reset_mock() |
| 424 | + lfs.write_param_default_values_to_file(param_default_values) |
| 425 | + mock_export.assert_not_called() |
| 426 | + |
| 427 | + def test_export_to_param(self) -> None: |
| 428 | + lfs = LocalFilesystem("vehicle_dir", "vehicle_type", None, allow_editing_template_files=False) |
| 429 | + param_mock = MagicMock() |
| 430 | + param_mock.value = 1.0 |
| 431 | + test_params = {"PARAM1": param_mock} |
| 432 | + |
| 433 | + with ( |
| 434 | + patch("ardupilot_methodic_configurator.backend_filesystem.Par.export_to_param") as mock_export, |
| 435 | + patch("ardupilot_methodic_configurator.backend_filesystem.update_parameter_documentation") as mock_update, |
| 436 | + patch("ardupilot_methodic_configurator.backend_filesystem.Par.format_params") as mock_format, |
| 437 | + ): |
| 438 | + mock_format.return_value = "formatted_params" |
| 439 | + |
| 440 | + # Test with documentation annotation |
| 441 | + lfs.export_to_param(test_params, "test.param", True) # noqa: FBT003 |
| 442 | + mock_format.assert_called_with(test_params) |
| 443 | + mock_export.assert_called_with("formatted_params", os_path.join("vehicle_dir", "test.param")) |
| 444 | + mock_update.assert_called_once() |
| 445 | + |
| 446 | + # Test without documentation annotation |
| 447 | + mock_export.reset_mock() |
| 448 | + mock_update.reset_mock() |
| 449 | + lfs.export_to_param(test_params, "test.param", False) # noqa: FBT003 |
| 450 | + mock_export.assert_called_once() |
| 451 | + mock_update.assert_not_called() |
| 452 | + |
| 453 | + def test_all_intermediate_parameter_file_comments(self) -> None: |
| 454 | + lfs = LocalFilesystem("vehicle_dir", "vehicle_type", None, allow_editing_template_files=False) |
| 455 | + lfs.file_parameters = { |
| 456 | + "file1.param": {"PARAM1": MagicMock(comment="Comment 1"), "PARAM2": MagicMock(comment="Comment 2")}, |
| 457 | + "file2.param": {"PARAM2": MagicMock(comment="Override comment 2"), "PARAM3": MagicMock(comment="Comment 3")}, |
| 458 | + } |
| 459 | + |
| 460 | + result = lfs._LocalFilesystem__all_intermediate_parameter_file_comments() |
| 461 | + assert result == {"PARAM1": "Comment 1", "PARAM2": "Override comment 2", "PARAM3": "Comment 3"} |
| 462 | + |
253 | 463 |
|
254 | 464 | class TestCopyTemplateFilesToNewVehicleDir(unittest.TestCase):
|
255 | 465 | """Copy Template Files To New Vehicle Directory testclass."""
|
|
0 commit comments