Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FEATURE: Add tests #88

Merged
merged 1 commit into from
Jan 1, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .github/workflows/codecov.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,16 @@ jobs:
python -m pip install pymavlink pytest pytest-cov coverage mock
pip install -U .
- name: Install apt packages
run: |
sudo apt-get update
sudo apt-get install -y xvfb
- name: Run tests and collect coverage
# run: pytest --cov ardupilot_methodic_configurator
run: |
export DISPLAY=:99
Xvfb :99 -screen 0 1024x768x16 &
coverage run -m pytest
coverage xml -o unittests/coverage.xml
Expand Down
7 changes: 7 additions & 0 deletions .github/workflows/coverage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,15 @@ jobs:
pip install build
pip install -U .
- name: Install apt packages
run: |
sudo apt-get update
sudo apt-get install -y xvfb
- name: Test with unittest
run: |
export DISPLAY=:99
Xvfb :99 -screen 0 1024x768x16 &
coverage run -m pytest
coverage xml -o unittests/coverage.xml
Expand Down
9 changes: 7 additions & 2 deletions .github/workflows/unit-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,19 @@ jobs:
pip install build
pip install -U .
- name: Install apt packages
run: |
sudo apt-get update
sudo apt-get install -y xvfb
- name: Set PYTHONPATH
run: |
echo "$PYTHONPATH"
echo "PYTHONPATH=/home/runner/work/MethodicConfigurator/ardupilot_methodic_configurator/ardupilot_methodic_configurator" >> $GITHUB_ENV
echo "$PYTHONPATH"
- name: Test with unittest
run: |
export DISPLAY=:99
Xvfb :99 -screen 0 1024x768x16 &
python -m unittest discover -s ./unittests -p '*_test.py'
- name: Unittest coverage report
Expand Down
7 changes: 7 additions & 0 deletions .github/workflows/unittests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,15 @@ jobs:
pip install build
pip install -U .
- name: Install apt packages
run: |
sudo apt-get update
sudo apt-get install -y xvfb
- name: Test with pytest
run: |
export DISPLAY=:99
Xvfb :99 -screen 0 1024x768x16 &
pytest --junitxml=unittests/test-results-${{ matrix.python-version }}.xml
- name: Upload pytest test results
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
#!/usr/bin/env python3

"""
Unittests for the frontend_tkinter_parameter_editor_documentation_frame.py file.

This file is part of Ardupilot methodic configurator. https://github.com/ArduPilot/MethodicConfigurator

SPDX-FileCopyrightText: 2024 Amilcar do Carmo Lucas <[email protected]>

SPDX-License-Identifier: GPL-3.0-or-later
"""

import tkinter as tk
import unittest
from tkinter import ttk
from unittest.mock import Mock, patch

from ardupilot_methodic_configurator.frontend_tkinter_parameter_editor_documentation_frame import DocumentationFrame


class TestDocumentationFrame(unittest.TestCase):
"""Test the DocumentationFrame class."""

def setUp(self) -> None:
self.root = tk.Tk()
self.local_filesystem = Mock()
self.local_filesystem.get_documentation_text_and_url.return_value = ("Sample blog text", "http://example.com/blog")
self.current_file = "test_file"
self.local_filesystem.get_seq_tooltip_text.side_effect = ["Why text", "Why now text"]
self.doc_frame = DocumentationFrame(self.root, self.local_filesystem, self.current_file)

def test_create_documentation_frame(self) -> None:
"""Test the creation of the documentation frame."""
self.assertIsInstance(self.doc_frame.documentation_frame, ttk.LabelFrame)
self.assertEqual(self.doc_frame.documentation_frame.cget("text"), "test_file Documentation")

expected_labels = ["Forum Blog:", "Wiki:", "External tool:", "Mandatory:"]
for label in expected_labels:
self.assertIn(label, 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:
"""Test the automatic opening of documentation links."""
self.local_filesystem.get_documentation_text_and_url.side_effect = [
("Blog text", "http://blog.url"),
("Wiki text", "http://wiki.url"),
("External tool text", "http://external_tool.url"),
("Mandatory text", None),
]
self.doc_frame.auto_open_var.set(True)

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)

@patch("ardupilot_methodic_configurator.frontend_tkinter_parameter_editor_documentation_frame.show_tooltip")
def test_update_why_why_now_tooltip(self, _mock_show_tooltip) -> None:
"""Test the update_why_why_now_tooltip method."""
self.local_filesystem.get_seq_tooltip_text.side_effect = ["Why text", "Why now text"]

self.doc_frame.update_why_why_now_tooltip(self.current_file)

@patch("ardupilot_methodic_configurator.frontend_tkinter_parameter_editor_documentation_frame.webbrowser_open")
@patch("ardupilot_methodic_configurator.frontend_tkinter_parameter_editor_documentation_frame.show_tooltip")
def test_update_documentation_labels(self, mock_show_tooltip, _mock_webbrowser_open) -> None:
"""Test the update_documentation_labels method."""
self.local_filesystem.get_documentation_text_and_url.side_effect = lambda _file, key: {
"blog": ("Blog text", "http://blog.url"),
"wiki": ("Wiki text", "http://wiki.url"),
"external_tool": ("External tool text", "http://external_tool.url"),
"mandatory": ("Mandatory text", None),
}[key]

self.doc_frame.update_documentation_labels(self.current_file)

self.assertEqual(self.doc_frame.documentation_labels["Forum Blog:"].cget("text"), "Blog text")
self.assertEqual(self.doc_frame.documentation_labels["Wiki:"].cget("text"), "Wiki text")
self.assertEqual(self.doc_frame.documentation_labels["External tool:"].cget("text"), "External tool text")
self.assertEqual(self.doc_frame.documentation_labels["Mandatory:"].cget("text"), "Mandatory text")

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")

@patch("ardupilot_methodic_configurator.frontend_tkinter_parameter_editor_documentation_frame.webbrowser_open")
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"),
("Wiki text", "http://wiki.url"),
("External tool text", "http://external_tool.url"),
("Mandatory text", None),
]

self.doc_frame.update_documentation_labels(self.current_file)

# Simulate clicking on the labels
self.doc_frame.documentation_labels["Forum Blog:"].event_generate("<Button-1>")
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")

def test_update_documentation_labels_no_urls(self) -> None:
"""Test updating documentation labels when no URLs are provided."""
self.local_filesystem.get_documentation_text_and_url.side_effect = [
("Blog text", None),
("Wiki text", None),
("External tool text", None),
("Mandatory text", None),
]

self.doc_frame.update_documentation_labels(self.current_file)

self.assertEqual(self.doc_frame.documentation_labels["Forum Blog:"].cget("text"), "Blog text")
self.assertEqual(self.doc_frame.documentation_labels["Wiki:"].cget("text"), "Wiki text")
self.assertEqual(self.doc_frame.documentation_labels["External tool:"].cget("text"), "External tool text")
self.assertEqual(self.doc_frame.documentation_labels["Mandatory:"].cget("text"), "Mandatory text")

self.assertEqual(str(self.doc_frame.documentation_labels["Forum Blog:"].cget("foreground")), "black")
self.assertEqual(str(self.doc_frame.documentation_labels["Wiki:"].cget("foreground")), "black")
self.assertEqual(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:
"""Test the tooltip texts for the documentation labels."""
self.local_filesystem.get_documentation_text_and_url.side_effect = [
("Blog text", "http://blog.url"),
("Wiki text", "http://wiki.url"),
("External tool text", "http://external_tool.url"),
("Mandatory text", None),
]

self.doc_frame.update_documentation_labels(self.current_file)

self.assertEqual(self.doc_frame.documentation_labels["Forum Blog:"].cget("text"), "Blog text")
self.assertEqual(self.doc_frame.documentation_labels["Wiki:"].cget("text"), "Wiki text")
self.assertEqual(self.doc_frame.documentation_labels["External tool:"].cget("text"), "External tool text")
self.assertEqual(self.doc_frame.documentation_labels["Mandatory:"].cget("text"), "Mandatory text")

self.assertEqual(str(self.doc_frame.documentation_labels["Forum Blog:"].cget("foreground")), "blue")
self.assertEqual(str(self.doc_frame.documentation_labels["Wiki:"].cget("foreground")), "blue")
self.assertEqual(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")


if __name__ == "__main__":
unittest.main()
Loading