Skip to content

Commit

Permalink
FIX: pylint after pytest introduction
Browse files Browse the repository at this point in the history
  • Loading branch information
amilcarlucas committed Jan 7, 2025
1 parent 3db53a8 commit 50b92a8
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 43 deletions.
30 changes: 15 additions & 15 deletions unittests/annotate_params_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import tempfile
import unittest
from unittest import mock
from unittest.mock import Mock, mock_open, patch
from unittest.mock import patch
from xml.etree import ElementTree as ET # no parsing, just data-structure manipulation

import pytest
Expand All @@ -40,20 +40,20 @@

@pytest.fixture
def mock_update() -> mock.Mock:
with patch("ardupilot_methodic_configurator.annotate_params.update_parameter_documentation") as mock:
yield mock
with patch("ardupilot_methodic_configurator.annotate_params.update_parameter_documentation") as mock_fun:
yield mock_fun


@pytest.fixture
def mock_get_xml_dir() -> mock.Mock:
with patch("ardupilot_methodic_configurator.annotate_params.get_xml_dir") as mock:
yield mock
with patch("ardupilot_methodic_configurator.annotate_params.get_xml_dir") as mock_fun:
yield mock_fun


@pytest.fixture
def mock_get_xml_url() -> mock.Mock:
with patch("ardupilot_methodic_configurator.annotate_params.get_xml_url") as mock:
yield mock
with patch("ardupilot_methodic_configurator.annotate_params.get_xml_url") as mock_fun:
yield mock_fun


class TestParamDocsUpdate(unittest.TestCase): # pylint: disable=missing-class-docstring, too-many-public-methods
Expand Down Expand Up @@ -88,7 +88,7 @@ def setUp(self) -> None:
},
}

@patch("builtins.open", new_callable=mock_open, read_data="<root></root>")
@patch("builtins.open", new_callable=mock.mock_open, read_data="<root></root>")
@patch("os.path.isfile")
@patch("ardupilot_methodic_configurator.annotate_params.Par.load_param_file_into_dict")
def test_get_xml_data_local_file(self, mock_load_param, mock_isfile, mock_open_) -> None:
Expand Down Expand Up @@ -139,7 +139,7 @@ def side_effect(_filename) -> bool:
mock_load_param.side_effect = FileNotFoundError

# Mock the open function to return a dummy XML string
mock_open = mock.mock_open(read_data="<root></root>") # pylint: disable=redefined-outer-name
mock_open = mock.mock_open(read_data="<root></root>")
with patch("builtins.open", mock_open):
# Call the function with a filename that exists in the script directory
result = get_xml_data(BASE_URL, ".", PARAM_DEFINITION_XML_FILE, "ArduCopter")
Expand Down Expand Up @@ -602,10 +602,10 @@ class TestAnnotateParamsExceptionHandling(unittest.TestCase):
"""Test parameter exception handling."""

@pytest.mark.usefixtures("mock_update", "mock_get_xml_dir", "mock_get_xml_url")
@patch("builtins.open", new_callable=mock_open)
@patch("builtins.open", new_callable=mock.mock_open)
def test_main_ioerror(self, mock_file) -> None:
with patch("ardupilot_methodic_configurator.annotate_params.arg_parser") as mock_arg_parser:
mock_arg_parser.return_value = Mock(
mock_arg_parser.return_value = mock.Mock(
vehicle_type="ArduCopter",
firmware_version="4.0",
target=".",
Expand All @@ -621,10 +621,10 @@ def test_main_ioerror(self, mock_file) -> None:
assert cm.value.code in [1, 2]

@pytest.mark.usefixtures("mock_update", "mock_get_xml_dir", "mock_get_xml_url")
@patch("builtins.open", new_callable=mock_open)
@patch("builtins.open", new_callable=mock.mock_open)
def test_main_oserror(self, mock_file) -> None:
with patch("ardupilot_methodic_configurator.annotate_params.arg_parser") as mock_arg_parser:
mock_arg_parser.return_value = Mock(
mock_arg_parser.return_value = mock.Mock(
vehicle_type="ArduCopter",
firmware_version="4.0",
target=".",
Expand All @@ -640,8 +640,8 @@ def test_main_oserror(self, mock_file) -> None:
assert cm.value.code in [1, 2]

@patch("ardupilot_methodic_configurator.annotate_params.get_xml_url")
def test_get_xml_url_exception(self, mock_get_xml_url) -> None:
mock_get_xml_url.side_effect = ValueError("Mocked Value Error")
def test_get_xml_url_exception(self, mock_get_xml_url_) -> None:
mock_get_xml_url_.side_effect = ValueError("Mocked Value Error")
with pytest.raises(ValueError, match="Vehicle type 'NonExistingVehicle' is not supported."):
get_xml_url("NonExistingVehicle", "4.0")

Expand Down
28 changes: 14 additions & 14 deletions unittests/extract_param_defaults_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ def test_mavproxy_sort(self) -> None:
@pytest.mark.usefixtures("mock_print")
class TestOutputParams(unittest.TestCase): # pylint: disable=missing-class-docstring
@patch("builtins.print")
def test_output_params(self, mock_print) -> None:
def test_output_params(self, mock_print_) -> None:
# Prepare a dummy defaults dictionary
defaults = {"PARAM2": 1.0, "PARAM1": 2.0}

Expand All @@ -200,10 +200,10 @@ def test_output_params(self, mock_print) -> None:

# Check if the print function was called with the correct parameters
expected_calls = [unittest.mock.call("PARAM2,1"), unittest.mock.call("PARAM1,2")]
mock_print.assert_has_calls(expected_calls, any_order=False)
mock_print_.assert_has_calls(expected_calls, any_order=False)

@patch("builtins.print")
def test_output_params_missionplanner_non_numeric(self, mock_print) -> None:
def test_output_params_missionplanner_non_numeric(self, mock_print_) -> None:
# Prepare a dummy defaults dictionary
defaults = {"PARAM1": "non-numeric"}

Expand All @@ -212,10 +212,10 @@ def test_output_params_missionplanner_non_numeric(self, mock_print) -> None:

# Check if the print function was called with the correct parameters
expected_calls = [unittest.mock.call("PARAM1,non-numeric")]
mock_print.assert_has_calls(expected_calls, any_order=False)
mock_print_.assert_has_calls(expected_calls, any_order=False)

@patch("builtins.print")
def test_output_params_mavproxy(self, mock_print) -> None:
def test_output_params_mavproxy(self, mock_print_) -> None:
# Prepare a dummy defaults dictionary
defaults = {"PARAM2": 2.0, "PARAM1": 1.0}

Expand All @@ -228,10 +228,10 @@ def test_output_params_mavproxy(self, mock_print) -> None:
unittest.mock.call("%-15s %.6f" % ("PARAM1", 1.0)), # pylint: disable=consider-using-f-string
unittest.mock.call("%-15s %.6f" % ("PARAM2", 2.0)), # pylint: disable=consider-using-f-string
]
mock_print.assert_has_calls(expected_calls, any_order=False)
mock_print_.assert_has_calls(expected_calls, any_order=False)

@patch("builtins.print")
def test_output_params_qgcs(self, mock_print) -> None:
def test_output_params_qgcs(self, mock_print_) -> None:
# Prepare a dummy defaults dictionary
defaults = {"PARAM2": 2.0, "PARAM1": 1.0}

Expand All @@ -245,10 +245,10 @@ def test_output_params_qgcs(self, mock_print) -> None:
unittest.mock.call("%u %u %-15s %.6f %u" % (1, 1, "PARAM1", 1.0, 9)), # pylint: disable=consider-using-f-string
unittest.mock.call("%u %u %-15s %.6f %u" % (1, 1, "PARAM2", 2.0, 9)), # pylint: disable=consider-using-f-string
]
mock_print.assert_has_calls(expected_calls, any_order=False)
mock_print_.assert_has_calls(expected_calls, any_order=False)

@patch("builtins.print")
def test_output_params_qgcs_2_4(self, mock_print) -> None:
def test_output_params_qgcs_2_4(self, mock_print_) -> None:
# Prepare a dummy defaults dictionary
defaults = {"PARAM2": 2.0, "PARAM1": 1.0}

Expand All @@ -262,10 +262,10 @@ def test_output_params_qgcs_2_4(self, mock_print) -> None:
unittest.mock.call("%u %u %-15s %.6f %u" % (2, 4, "PARAM1", 1.0, 9)), # pylint: disable=consider-using-f-string
unittest.mock.call("%u %u %-15s %.6f %u" % (2, 4, "PARAM2", 2.0, 9)), # pylint: disable=consider-using-f-string
]
mock_print.assert_has_calls(expected_calls, any_order=False)
mock_print_.assert_has_calls(expected_calls, any_order=False)

@patch("builtins.print")
def test_output_params_qgcs_SYSID_THISMAV(self, mock_print) -> None: # noqa: N802, pylint: disable=invalid-name
def test_output_params_qgcs_SYSID_THISMAV(self, mock_print_) -> None: # noqa: N802, pylint: disable=invalid-name
# Prepare a dummy defaults dictionary
defaults = {"PARAM2": 2.0, "PARAM1": 1.0, "SYSID_THISMAV": 3.0}

Expand All @@ -280,7 +280,7 @@ def test_output_params_qgcs_SYSID_THISMAV(self, mock_print) -> None: # noqa: N8
unittest.mock.call("%u %u %-15s %.6f %u" % (3, 7, "PARAM2", 2.0, 9)), # pylint: disable=consider-using-f-string
unittest.mock.call("%u %u %-15s %.6f %u" % (3, 7, "SYSID_THISMAV", 3.0, 9)), # pylint: disable=consider-using-f-string
]
mock_print.assert_has_calls(expected_calls, any_order=False)
mock_print_.assert_has_calls(expected_calls, any_order=False)

def test_output_params_qgcs_SYSID_INVALID(self) -> None: # noqa: N802, pylint: disable=invalid-name
# Prepare a dummy defaults dictionary
Expand Down Expand Up @@ -313,7 +313,7 @@ def test_output_params_qgcs_COMPID_INVALID(self) -> None: # noqa: N802, pylint:
assert str(cm.value) == f"Invalid component ID parameter 259 must be smaller than {MAVLINK_COMPID_MAX}"

@patch("builtins.print")
def test_output_params_integer(self, mock_print) -> None:
def test_output_params_integer(self, mock_print_) -> None:
# Prepare a dummy defaults dictionary with an integer value
defaults = {"PARAM1": 1.01, "PARAM2": 2.00}

Expand All @@ -323,7 +323,7 @@ def test_output_params_integer(self, mock_print) -> None:

# Check if the print function was called with the correct parameters
expected_calls = [unittest.mock.call("PARAM1,1.01"), unittest.mock.call("PARAM2,2")]
mock_print.assert_has_calls(expected_calls, any_order=False)
mock_print_.assert_has_calls(expected_calls, any_order=False)


if __name__ == "__main__":
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def test_create_documentation_frame(self) -> None:
assert label in self.doc_frame.documentation_labels

@patch("ardupilot_methodic_configurator.frontend_tkinter_parameter_editor_documentation_frame.webbrowser_open")
def test_auto_open_documentation_links(self, mock_webbrowser_open) -> None:
def test_auto_open_documentation_links(self, mock_webbrowser_open_) -> None:
"""Test the automatic opening of documentation links."""
self.local_filesystem.get_documentation_text_and_url.side_effect = [
("Blog text", "http://blog.url"),
Expand All @@ -65,19 +65,19 @@ def test_auto_open_documentation_links(self, mock_webbrowser_open) -> None:

self.doc_frame.update_documentation_labels(self.current_file)

mock_webbrowser_open.assert_any_call(url="http://wiki.url", new=0, autoraise=False)
mock_webbrowser_open.assert_any_call(url="http://external_tool.url", new=0, autoraise=False)
mock_webbrowser_open.assert_any_call(url="http://blog.url", new=0, autoraise=True)
mock_webbrowser_open_.assert_any_call(url="http://wiki.url", new=0, autoraise=False)
mock_webbrowser_open_.assert_any_call(url="http://external_tool.url", new=0, autoraise=False)
mock_webbrowser_open_.assert_any_call(url="http://blog.url", new=0, autoraise=True)

@pytest.mark.usefixtures("mock_show_tooltip")
def test_update_why_why_now_tooltip(self) -> None:
"""Test the update_why_why_now_tooltip method."""
self.local_filesystem.get_seq_tooltip_text.side_effect = ["Why text", "Why now text"]
with patch(
"ardupilot_methodic_configurator.frontend_tkinter_parameter_editor_documentation_frame.show_tooltip"
) as mock_show_tooltip:
) as mock_show_tooltip_:
self.doc_frame.update_why_why_now_tooltip(self.current_file)
mock_show_tooltip.assert_called_once_with(
mock_show_tooltip_.assert_called_once_with(
self.doc_frame.documentation_frame, "Why: Why text\nWhy now: Why now text"
)

Expand All @@ -103,7 +103,7 @@ def test_update_documentation_labels(self) -> None:
assert str(self.doc_frame.documentation_labels["External tool:"].cget("foreground")) == "blue"

@patch("ardupilot_methodic_configurator.frontend_tkinter_parameter_editor_documentation_frame.webbrowser_open")
def test_manual_open_documentation_links(self, mock_webbrowser_open) -> None:
def test_manual_open_documentation_links(self, mock_webbrowser_open_) -> None:
"""Test manually opening documentation links by clicking on labels."""
self.local_filesystem.get_documentation_text_and_url.side_effect = [
("Blog text", "http://blog.url"),
Expand All @@ -119,9 +119,9 @@ def test_manual_open_documentation_links(self, mock_webbrowser_open) -> None:
self.doc_frame.documentation_labels["Wiki:"].event_generate("<Button-1>")
self.doc_frame.documentation_labels["External tool:"].event_generate("<Button-1>")

mock_webbrowser_open.assert_any_call("http://blog.url")
mock_webbrowser_open.assert_any_call("http://wiki.url")
mock_webbrowser_open.assert_any_call("http://external_tool.url")
mock_webbrowser_open_.assert_any_call("http://blog.url")
mock_webbrowser_open_.assert_any_call("http://wiki.url")
mock_webbrowser_open_.assert_any_call("http://external_tool.url")

def test_update_documentation_labels_no_urls(self) -> None:
"""Test updating documentation labels when no URLs are provided."""
Expand All @@ -144,7 +144,7 @@ def test_update_documentation_labels_no_urls(self) -> None:
assert str(self.doc_frame.documentation_labels["External tool:"].cget("foreground")) == "black"

@patch("ardupilot_methodic_configurator.frontend_tkinter_parameter_editor_documentation_frame.show_tooltip")
def test_tooltip_texts(self, mock_show_tooltip) -> None:
def test_tooltip_texts(self, mock_show_tooltip_) -> None:
"""Test the tooltip texts for the documentation labels."""
self.local_filesystem.get_documentation_text_and_url.side_effect = [
("Blog text", "http://blog.url"),
Expand All @@ -164,9 +164,9 @@ def test_tooltip_texts(self, mock_show_tooltip) -> None:
assert str(self.doc_frame.documentation_labels["Wiki:"].cget("foreground")) == "blue"
assert str(self.doc_frame.documentation_labels["External tool:"].cget("foreground")) == "blue"

mock_show_tooltip.assert_any_call(self.doc_frame.documentation_labels["Forum Blog:"], "http://blog.url")
mock_show_tooltip.assert_any_call(self.doc_frame.documentation_labels["Wiki:"], "http://wiki.url")
mock_show_tooltip.assert_any_call(self.doc_frame.documentation_labels["External tool:"], "http://external_tool.url")
mock_show_tooltip_.assert_any_call(self.doc_frame.documentation_labels["Forum Blog:"], "http://blog.url")
mock_show_tooltip_.assert_any_call(self.doc_frame.documentation_labels["Wiki:"], "http://wiki.url")
mock_show_tooltip_.assert_any_call(self.doc_frame.documentation_labels["External tool:"], "http://external_tool.url")


if __name__ == "__main__":
Expand Down

0 comments on commit 50b92a8

Please sign in to comment.