Skip to content

Commit

Permalink
CHORE: Add tests to frontend classes
Browse files Browse the repository at this point in the history
  • Loading branch information
amilcarlucas committed Jan 27, 2025
1 parent 9bc543b commit f3dee9c
Show file tree
Hide file tree
Showing 3 changed files with 153 additions and 0 deletions.
104 changes: 104 additions & 0 deletions tests/test_frontend_tkinter_software_update.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
#!/usr/bin/python3

"""
Tests for the frontend_tkinter_software_update.py file.
This file is part of Ardupilot methodic configurator. https://github.com/ArduPilot/MethodicConfigurator
SPDX-FileCopyrightText: 2024-2025 Amilcar Lucas
SPDX-License-Identifier: GPL-3.0-or-later
"""

import unittest
from unittest.mock import MagicMock, patch

from ardupilot_methodic_configurator.frontend_tkinter_software_update import UpdateDialog


class TestUpdateDialog(unittest.TestCase):
"""Test cases for the UpdateDialog class."""

def setUp(self) -> None:
"""Set up test fixtures."""
self.tk_mock = patch("tkinter.Tk").start()
self.ttk_mock = patch("tkinter.ttk").start()
self.version_info = "Test Version Info"
self.download_callback = MagicMock(return_value=True)

def tearDown(self) -> None:
"""Clean up after each test."""
patch.stopall()

def test_init(self) -> None:
"""Test dialog initialization."""
dialog = UpdateDialog(self.version_info, self.download_callback)

self.tk_mock.assert_called_once()
assert dialog.result is None
assert dialog.download_callback == self.download_callback

def test_update_progress(self) -> None:
"""Test progress bar updates."""

def test_on_yes_successful_update(self) -> None:
"""Test successful update process."""

def test_on_yes_failed_update(self) -> None:
"""Test failed update process."""

def test_on_no(self) -> None:
"""Test 'Not Now' button behavior."""
dialog = UpdateDialog(self.version_info)
dialog.on_no()

assert not dialog.result
dialog.root.destroy.assert_called_once()

def test_on_cancel(self) -> None:
"""Test cancel operation."""
dialog = UpdateDialog(self.version_info)
dialog.on_cancel()

assert not dialog.result
dialog.root.destroy.assert_called_once()

def test_grid_management(self) -> None:
"""Test grid management during update process."""

@patch("tkinter.ttk.Button")
def test_button_states(self, mock_button) -> None:
"""Test button state management."""

def test_status_messages(self) -> None:
"""Test status message updates."""

def test_window_protocol(self) -> None:
"""Test window protocol configuration."""
dialog = UpdateDialog(self.version_info)
dialog.root.protocol.assert_called_with("WM_DELETE_WINDOW", dialog.on_cancel)

@patch("tkinter.ttk.Frame")
def test_frame_configuration(self, mock_frame) -> None:
"""Test frame configuration."""
dialog = UpdateDialog(self.version_info)
mock_frame.assert_called_with(dialog.root, padding="20")
dialog.frame.grid.assert_called_with(sticky="nsew")

def test_progress_value_bounds(self) -> None:
"""Test progress bar value bounds."""

def test_multiple_updates(self) -> None:
"""Test multiple update attempts."""
dialog = UpdateDialog(self.version_info, self.download_callback)

# First update attempt
dialog.on_yes()
assert dialog.result

# Second update attempt should not change result
dialog.on_yes()
assert dialog.result

def test_version_info_display(self) -> None:
"""Test version info display in dialog."""
47 changes: 47 additions & 0 deletions tests/test_frontend_tkinter_template_overview.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#!/usr/bin/python3

"""
Tests for the frontend_tkinter_template_overview.py file.
This file is part of Ardupilot methodic configurator. https://github.com/ArduPilot/MethodicConfigurator
SPDX-FileCopyrightText: 2024-2025 Amilcar do Carmo Lucas <[email protected]>
SPDX-License-Identifier: GPL-3.0-or-later
"""

import argparse
import unittest
from unittest.mock import patch

import pytest

from ardupilot_methodic_configurator.frontend_tkinter_template_overview import argument_parser, main


class TestTemplateOverviewWindow(unittest.TestCase):
"""Test cases for the TemplateOverviewWindow class."""


class TestArgumentParser(unittest.TestCase):
"""Test cases for argument parsing functionality."""

def test_argument_parser_exit(self) -> None:
"""Test argument parser exits with no arguments."""
with pytest.raises(SystemExit):
argument_parser()

@patch("sys.argv", ["script.py", "--loglevel", "DEBUG"])
def test_main_function(self) -> None:
"""Test main function execution."""
with (
patch("ardupilot_methodic_configurator.frontend_tkinter_template_overview.TemplateOverviewWindow") as mock_window,
patch("ardupilot_methodic_configurator.frontend_tkinter_template_overview.argument_parser") as mock_parser,
):
mock_parser.return_value = argparse.Namespace(loglevel="DEBUG")
main()
mock_window.assert_called_once_with(None)


if __name__ == "__main__":
unittest.main()
2 changes: 2 additions & 0 deletions tests/test_middleware_software_updates.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ def update_manager() -> UpdateManager:


class TestUpdateManager:
"""Test cases for the UpdateManager class."""

def test_format_version_info(self) -> None:
result = format_version_info("1.0.0", "2.0.0", "Test changes")
assert "1.0.0" in result
Expand Down

0 comments on commit f3dee9c

Please sign in to comment.