|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +""" |
| 4 | +Tests for the backend_filesystem_configuration_steps.py file. |
| 5 | +
|
| 6 | +This file is part of Ardupilot methodic configurator. https://github.com/ArduPilot/MethodicConfigurator |
| 7 | +
|
| 8 | +SPDX-FileCopyrightText: 2024-2025 Amilcar do Carmo Lucas <[email protected]> |
| 9 | +
|
| 10 | +SPDX-License-Identifier: GPL-3.0-or-later |
| 11 | +""" |
| 12 | + |
| 13 | +import unittest |
| 14 | +from unittest.mock import mock_open, patch |
| 15 | + |
| 16 | +from ardupilot_methodic_configurator.backend_filesystem_configuration_steps import ConfigurationSteps |
| 17 | + |
| 18 | + |
| 19 | +class TestConfigurationSteps(unittest.TestCase): |
| 20 | + """ConfigurationSteps test class.""" |
| 21 | + |
| 22 | + def setUp(self) -> None: |
| 23 | + self.config_steps = ConfigurationSteps("vehicle_dir", "vehicle_type") |
| 24 | + |
| 25 | + @patch("builtins.open", new_callable=mock_open, read_data='{"test_file": {"forced_parameters": {}}}') |
| 26 | + @patch("os.path.join") |
| 27 | + @patch("os.path.dirname") |
| 28 | + @patch("os.path.abspath") |
| 29 | + def test_re_init( |
| 30 | + self, |
| 31 | + mock_abspath: unittest.mock.Mock, |
| 32 | + mock_dirname: unittest.mock.Mock, |
| 33 | + mock_join: unittest.mock.Mock, |
| 34 | + mock_open2: unittest.mock.Mock, |
| 35 | + ) -> None: |
| 36 | + mock_abspath.return_value = "abs_path" |
| 37 | + mock_dirname.return_value = "dir_name" |
| 38 | + mock_join.side_effect = lambda *args: "/".join(args) |
| 39 | + self.config_steps.re_init("vehicle_dir", "vehicle_type") |
| 40 | + assert self.config_steps.configuration_steps |
| 41 | + mock_open2.assert_called_once_with("vehicle_dir/configuration_steps_vehicle_type.json", encoding="utf-8") |
| 42 | + assert "test_file" in self.config_steps.configuration_steps |
| 43 | + |
| 44 | + def test_compute_parameters(self) -> None: |
| 45 | + file_info = {"forced_parameters": {"PARAM1": {"New Value": "10", "Change Reason": "Test reason"}}} |
| 46 | + variables: dict[str, dict] = {"doc_dict": {"PARAM1": {"values": {10: "value"}}}} |
| 47 | + result = self.config_steps.compute_parameters("test_file", file_info, "forced", variables) |
| 48 | + assert result == "" |
| 49 | + assert "test_file" in self.config_steps.forced_parameters |
| 50 | + assert "PARAM1" in self.config_steps.forced_parameters["test_file"] |
| 51 | + assert self.config_steps.forced_parameters["test_file"]["PARAM1"].value == 10 |
| 52 | + assert self.config_steps.forced_parameters["test_file"]["PARAM1"].comment == "Test reason" |
| 53 | + |
| 54 | + def test_auto_changed_by(self) -> None: |
| 55 | + self.config_steps.configuration_steps = {"test_file": {"auto_changed_by": "auto_change"}} |
| 56 | + result = self.config_steps.auto_changed_by("test_file") |
| 57 | + assert result == "auto_change" |
| 58 | + |
| 59 | + def test_jump_possible(self) -> None: |
| 60 | + self.config_steps.configuration_steps = {"test_file": {"jump_possible": {"step1": "step2"}}} |
| 61 | + result = self.config_steps.jump_possible("test_file") |
| 62 | + assert result == {"step1": "step2"} |
| 63 | + |
| 64 | + def test_get_documentation_text_and_url(self) -> None: |
| 65 | + self.config_steps.configuration_steps = { |
| 66 | + "test_file": {"prefix_text": "Documentation text", "prefix_url": "http://example.com"} |
| 67 | + } |
| 68 | + text, url = self.config_steps.get_documentation_text_and_url("test_file", "prefix") |
| 69 | + assert text == "Documentation text" |
| 70 | + assert url == "http://example.com" |
| 71 | + |
| 72 | + def test_get_seq_tooltip_text(self) -> None: |
| 73 | + self.config_steps.configuration_steps = {"test_file": {"tooltip_key": "Tooltip text"}} |
| 74 | + result = self.config_steps.get_seq_tooltip_text("test_file", "tooltip_key") |
| 75 | + assert result == "Tooltip text" |
| 76 | + |
| 77 | + def test_missing_new_value(self) -> None: |
| 78 | + file_info = {"forced_parameters": {"PARAM1": {"Change Reason": "Test reason"}}} |
| 79 | + with self.assertLogs(level="ERROR") as log: |
| 80 | + self.config_steps._ConfigurationSteps__validate_parameters_in_configuration_steps("test_file", file_info, "forced") # noqa: SLF001 pylint: disable=protected-access |
| 81 | + assert any("New Value" in message for message in log.output) |
| 82 | + |
| 83 | + def test_missing_change_reason(self) -> None: |
| 84 | + file_info = {"forced_parameters": {"PARAM1": {"New Value": "10"}}} |
| 85 | + with self.assertLogs(level="ERROR") as log: |
| 86 | + self.config_steps._ConfigurationSteps__validate_parameters_in_configuration_steps("test_file", file_info, "forced") # noqa: SLF001 pylint: disable=protected-access |
| 87 | + assert any("Change Reason" in message for message in log.output) |
| 88 | + |
| 89 | + def test_compute_parameters_with_invalid_expression(self) -> None: |
| 90 | + file_info = {"forced_parameters": {"PARAM1": {"New Value": "invalid_expression", "Change Reason": "Test reason"}}} |
| 91 | + variables: dict[str, dict] = {"doc_dict": {"PARAM1": {"values": {10: "value"}}}} |
| 92 | + result = self.config_steps.compute_parameters("test_file", file_info, "forced", variables) |
| 93 | + assert "could not be computed" in result |
| 94 | + |
| 95 | + def test_compute_parameters_with_missing_doc_dict(self) -> None: |
| 96 | + file_info = {"forced_parameters": {"PARAM1": {"New Value": "10", "Change Reason": "Test reason"}}} |
| 97 | + variables: dict[str, dict] = {} |
| 98 | + result = self.config_steps.compute_parameters("test_file", file_info, "forced", variables) |
| 99 | + assert result == "" |
| 100 | + |
| 101 | + def test_compute_parameters_with_string_result(self) -> None: |
| 102 | + file_info = {"forced_parameters": {"PARAM1": {"New Value": "'value'", "Change Reason": "Test reason"}}} |
| 103 | + variables: dict[str, dict] = {"doc_dict": {"PARAM1": {"values": {10: "value"}}}} |
| 104 | + result = self.config_steps.compute_parameters("test_file", file_info, "forced", variables) |
| 105 | + assert result == "" |
| 106 | + assert self.config_steps.forced_parameters["test_file"]["PARAM1"].value == 10 |
| 107 | + |
| 108 | + |
| 109 | +if __name__ == "__main__": |
| 110 | + unittest.main() |
0 commit comments