Reformat with black

This commit is contained in:
Pim van Pelt
2025-08-03 11:03:38 +02:00
parent 81c7f9c9bd
commit c1d70cd9b6
8 changed files with 239 additions and 209 deletions

View File

@@ -21,11 +21,7 @@ def run_tests(args=None):
if args: if args:
cmd.extend(args) cmd.extend(args)
else: else:
cmd.extend([ cmd.extend(["tests/", "-v", "--tb=short"])
"tests/",
"-v",
"--tb=short"
])
try: try:
result = subprocess.run(cmd, check=True) result = subprocess.run(cmd, check=True)

View File

@@ -2,8 +2,9 @@
import sys import sys
import os import os
# Add the src directory to Python path # Add the src directory to Python path
sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..', 'src')) sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..", "src"))
import pytest import pytest
from unittest.mock import Mock, MagicMock from unittest.mock import Mock, MagicMock
@@ -28,7 +29,7 @@ def mock_monitors():
"type": "http", "type": "http",
"url": "https://example.com", "url": "https://example.com",
"active": True, "active": True,
"parent": None "parent": None,
}, },
{ {
"id": 2, "id": 2,
@@ -36,14 +37,14 @@ def mock_monitors():
"type": "http", "type": "http",
"url": "https://test.com", "url": "https://test.com",
"active": False, "active": False,
"parent": None "parent": None,
}, },
{ {
"id": 3, "id": 3,
"name": "Group Monitor", "name": "Group Monitor",
"type": "group", "type": "group",
"active": True, "active": True,
"parent": None "parent": None,
}, },
{ {
"id": 4, "id": 4,
@@ -51,8 +52,8 @@ def mock_monitors():
"type": "http", "type": "http",
"url": "https://child.com", "url": "https://child.com",
"active": False, "active": False,
"parent": 3 "parent": 3,
} },
] ]
@@ -65,13 +66,13 @@ def mock_maintenances():
"title": "Test Maintenance", "title": "Test Maintenance",
"description": "Test maintenance description", "description": "Test maintenance description",
"strategy": "single", "strategy": "single",
"active": True "active": True,
}, },
{ {
"id": 2, "id": 2,
"title": "Inactive Maintenance", "title": "Inactive Maintenance",
"description": "Inactive maintenance description", "description": "Inactive maintenance description",
"strategy": "single", "strategy": "single",
"active": False "active": False,
} },
] ]

View File

@@ -21,7 +21,7 @@ class TestCLIIntegration:
# Verify parser is created # Verify parser is created
assert monitor_parser is not None assert monitor_parser is not None
assert hasattr(setup_monitor_parser, '_parser') assert hasattr(setup_monitor_parser, "_parser")
def test_maintenance_parser_setup(self): def test_maintenance_parser_setup(self):
"""Test maintenance parser setup""" """Test maintenance parser setup"""
@@ -32,7 +32,7 @@ class TestCLIIntegration:
# Verify parser is created # Verify parser is created
assert maintenance_parser is not None assert maintenance_parser is not None
assert hasattr(setup_maintenance_parser, '_parser') assert hasattr(setup_maintenance_parser, "_parser")
def test_info_parser_setup(self): def test_info_parser_setup(self):
"""Test info parser setup""" """Test info parser setup"""
@@ -137,7 +137,9 @@ class TestArgumentParsing:
setup_monitor_parser(subparsers) setup_monitor_parser(subparsers)
# Test with monitor patterns # Test with monitor patterns
args = parser.parse_args(["monitor", "list", "--monitor", "web*", "--monitor", "api*"]) args = parser.parse_args(
["monitor", "list", "--monitor", "web*", "--monitor", "api*"]
)
assert args.resource == "monitor" assert args.resource == "monitor"
assert args.monitor_action == "list" assert args.monitor_action == "list"
assert args.monitor == ["web*", "api*"] assert args.monitor == ["web*", "api*"]
@@ -173,13 +175,20 @@ class TestArgumentParsing:
setup_maintenance_parser(subparsers) setup_maintenance_parser(subparsers)
# Test maintenance add # Test maintenance add
args = parser.parse_args([ args = parser.parse_args(
"maintenance", "add", [
"--title", "Server Update", "maintenance",
"--description", "Updating server software", "add",
"--duration", "2h", "--title",
"--monitor", "server*" "Server Update",
]) "--description",
"Updating server software",
"--duration",
"2h",
"--monitor",
"server*",
]
)
assert args.resource == "maintenance" assert args.resource == "maintenance"
assert args.maintenance_action == "add" assert args.maintenance_action == "add"
assert args.title == "Server Update" assert args.title == "Server Update"
@@ -234,7 +243,10 @@ class TestErrorHandling:
# Verify error handling # Verify error handling
assert result is True # Command completes even with no matches assert result is True # Command completes even with no matches
captured = capsys.readouterr() captured = capsys.readouterr()
assert "Error: No monitors found matching the specified patterns or groups" in captured.out assert (
"Error: No monitors found matching the specified patterns or groups"
in captured.out
)
def test_maintenance_command_resilience(self, mock_client, capsys): def test_maintenance_command_resilience(self, mock_client, capsys):
"""Test maintenance command handles API errors""" """Test maintenance command handles API errors"""

View File

@@ -113,7 +113,7 @@ class TestKumaClient:
{"id": 1, "name": "Web Server"}, {"id": 1, "name": "Web Server"},
{"id": 2, "name": "API Server"}, {"id": 2, "name": "API Server"},
{"id": 3, "name": "Database"}, {"id": 3, "name": "Database"},
{"id": 4, "name": "Web Frontend"} {"id": 4, "name": "Web Frontend"},
] ]
client.api.get_monitors.return_value = mock_monitors client.api.get_monitors.return_value = mock_monitors
@@ -136,7 +136,7 @@ class TestKumaClient:
mock_monitors = [ mock_monitors = [
{"id": 1, "name": "Web Server"}, {"id": 1, "name": "Web Server"},
{"id": 2, "name": "API Server"} {"id": 2, "name": "API Server"},
] ]
client.api.get_monitors.return_value = mock_monitors client.api.get_monitors.return_value = mock_monitors
@@ -150,9 +150,7 @@ class TestKumaClient:
client = KumaClient("http://test.com") client = KumaClient("http://test.com")
client.api = Mock() client.api = Mock()
mock_monitors = [ mock_monitors = [{"id": 1, "name": "Web Server"}]
{"id": 1, "name": "Web Server"}
]
client.api.get_monitors.return_value = mock_monitors client.api.get_monitors.return_value = mock_monitors
result = client.find_monitors_by_pattern(["Database*"]) result = client.find_monitors_by_pattern(["Database*"])
@@ -163,9 +161,7 @@ class TestKumaClient:
client = KumaClient("http://test.com") client = KumaClient("http://test.com")
client.api = Mock() client.api = Mock()
mock_monitors = [ mock_monitors = [{"id": 1, "name": "Web Server"}]
{"id": 1, "name": "Web Server"}
]
client.api.get_monitors.return_value = mock_monitors client.api.get_monitors.return_value = mock_monitors
# Same monitor should match both patterns # Same monitor should match both patterns
@@ -186,7 +182,7 @@ class TestKumaClient:
captured = capsys.readouterr() captured = capsys.readouterr()
assert "Error finding monitors: API Error" in captured.out assert "Error finding monitors: API Error" in captured.out
@patch('kumacli.client.UptimeKumaApi') @patch("kumacli.client.UptimeKumaApi")
def test_connect_success(self, mock_api_class, capsys): def test_connect_success(self, mock_api_class, capsys):
"""Test successful connection""" """Test successful connection"""
mock_api = Mock() mock_api = Mock()
@@ -204,7 +200,7 @@ class TestKumaClient:
captured = capsys.readouterr() captured = capsys.readouterr()
assert "Connected to http://test.com" in captured.out assert "Connected to http://test.com" in captured.out
@patch('kumacli.client.UptimeKumaApi') @patch("kumacli.client.UptimeKumaApi")
def test_connect_failure(self, mock_api_class, capsys): def test_connect_failure(self, mock_api_class, capsys):
"""Test connection failure""" """Test connection failure"""
mock_api_class.side_effect = Exception("Connection failed") mock_api_class.side_effect = Exception("Connection failed")

View File

@@ -15,7 +15,7 @@ class TestInfoCommands:
mock_info_data = { mock_info_data = {
"version": "1.23.0", "version": "1.23.0",
"hostname": "kuma-server", "hostname": "kuma-server",
"primaryBaseURL": "https://status.example.com" "primaryBaseURL": "https://status.example.com",
} }
mock_client.api.info.return_value = mock_info_data mock_client.api.info.return_value = mock_info_data

View File

@@ -60,7 +60,9 @@ class TestMaintenanceCommands:
# Setup # Setup
mock_maintenance = {"id": 1, "title": "Test Maintenance"} mock_maintenance = {"id": 1, "title": "Test Maintenance"}
mock_client.api.get_maintenance.return_value = mock_maintenance mock_client.api.get_maintenance.return_value = mock_maintenance
mock_client.api.delete_maintenance.return_value = {"msg": "Deleted Successfully"} mock_client.api.delete_maintenance.return_value = {
"msg": "Deleted Successfully"
}
maintenance_commands = MaintenanceCommands(mock_client) maintenance_commands = MaintenanceCommands(mock_client)
@@ -71,13 +73,18 @@ class TestMaintenanceCommands:
mock_client.api.get_maintenance.assert_called_once_with(1) mock_client.api.get_maintenance.assert_called_once_with(1)
mock_client.api.delete_maintenance.assert_called_once_with(1) mock_client.api.delete_maintenance.assert_called_once_with(1)
captured = capsys.readouterr() captured = capsys.readouterr()
assert "Successfully deleted maintenance 'Test Maintenance' (ID: 1)" in captured.out assert (
"Successfully deleted maintenance 'Test Maintenance' (ID: 1)"
in captured.out
)
def test_delete_all_maintenances(self, mock_client, mock_maintenances, capsys): def test_delete_all_maintenances(self, mock_client, mock_maintenances, capsys):
"""Test deleting all maintenances""" """Test deleting all maintenances"""
# Setup # Setup
mock_client.api.get_maintenances.return_value = mock_maintenances mock_client.api.get_maintenances.return_value = mock_maintenances
mock_client.api.delete_maintenance.return_value = {"msg": "Deleted Successfully"} mock_client.api.delete_maintenance.return_value = {
"msg": "Deleted Successfully"
}
maintenance_commands = MaintenanceCommands(mock_client) maintenance_commands = MaintenanceCommands(mock_client)
@@ -99,7 +106,10 @@ class TestMaintenanceCommands:
# Verify # Verify
captured = capsys.readouterr() captured = capsys.readouterr()
assert "Error: Either --id or --all flag is required for delete operation" in captured.out assert (
"Error: Either --id or --all flag is required for delete operation"
in captured.out
)
class TestMaintenanceCommandHandler: class TestMaintenanceCommandHandler:
@@ -110,7 +120,7 @@ class TestMaintenanceCommandHandler:
mock_args.maintenance_action = None mock_args.maintenance_action = None
# Mock the parser setup # Mock the parser setup
with patch('kumacli.cmd.maintenance.setup_maintenance_parser') as mock_setup: with patch("kumacli.cmd.maintenance.setup_maintenance_parser") as mock_setup:
mock_parser = Mock() mock_parser = Mock()
mock_setup._parser = mock_parser mock_setup._parser = mock_parser
@@ -144,7 +154,9 @@ class TestMaintenanceCommandHandler:
mock_maintenance = {"id": 1, "title": "Test Maintenance"} mock_maintenance = {"id": 1, "title": "Test Maintenance"}
mock_client.api.get_maintenance.return_value = mock_maintenance mock_client.api.get_maintenance.return_value = mock_maintenance
mock_client.api.delete_maintenance.return_value = {"msg": "Deleted Successfully"} mock_client.api.delete_maintenance.return_value = {
"msg": "Deleted Successfully"
}
# Execute # Execute
result = handle_maintenance_command(mock_args, mock_client) result = handle_maintenance_command(mock_args, mock_client)
@@ -165,4 +177,7 @@ class TestMaintenanceCommandHandler:
# Verify # Verify
assert result is False assert result is False
captured = capsys.readouterr() captured = capsys.readouterr()
assert "Unknown maintenance action. Use --help for usage information." in captured.out assert (
"Unknown maintenance action. Use --help for usage information."
in captured.out
)

View File

@@ -5,7 +5,11 @@ from unittest.mock import Mock, patch
from io import StringIO from io import StringIO
import sys import sys
from kumacli.cmd.monitor import MonitorCommands, handle_monitor_command, setup_monitor_parser from kumacli.cmd.monitor import (
MonitorCommands,
handle_monitor_command,
setup_monitor_parser,
)
class TestMonitorCommands: class TestMonitorCommands:
@@ -15,7 +19,7 @@ class TestMonitorCommands:
mock_client.api.get_monitors.return_value = mock_monitors mock_client.api.get_monitors.return_value = mock_monitors
mock_client.find_monitors_by_pattern.return_value = [ mock_client.find_monitors_by_pattern.return_value = [
{"id": 1, "name": "Test Monitor 1"}, {"id": 1, "name": "Test Monitor 1"},
{"id": 2, "name": "Test Monitor 2"} {"id": 2, "name": "Test Monitor 2"},
] ]
mock_client.api.pause_monitor.return_value = {"msg": "Paused Successfully."} mock_client.api.pause_monitor.return_value = {"msg": "Paused Successfully."}
@@ -80,7 +84,10 @@ class TestMonitorCommands:
# Verify # Verify
captured = capsys.readouterr() captured = capsys.readouterr()
assert "Error: No monitors found matching the specified patterns or groups" in captured.out assert (
"Error: No monitors found matching the specified patterns or groups"
in captured.out
)
def test_pause_monitors_api_error(self, mock_client, capsys): def test_pause_monitors_api_error(self, mock_client, capsys):
"""Test pausing monitors with API error""" """Test pausing monitors with API error"""
@@ -144,9 +151,7 @@ class TestMonitorCommands:
def test_resume_monitors_all_no_paused(self, mock_client, capsys): def test_resume_monitors_all_no_paused(self, mock_client, capsys):
"""Test resuming all paused monitors when none are paused""" """Test resuming all paused monitors when none are paused"""
# Setup # Setup
active_monitors = [ active_monitors = [{"id": 1, "name": "Active Monitor", "active": True}]
{"id": 1, "name": "Active Monitor", "active": True}
]
mock_client.api.get_monitors.return_value = active_monitors mock_client.api.get_monitors.return_value = active_monitors
monitor_commands = MonitorCommands(mock_client) monitor_commands = MonitorCommands(mock_client)
@@ -167,7 +172,10 @@ class TestMonitorCommands:
# Verify # Verify
captured = capsys.readouterr() captured = capsys.readouterr()
assert "Error: Either --monitor, --group, or --all flag is required." in captured.out assert (
"Error: Either --monitor, --group, or --all flag is required."
in captured.out
)
class TestMonitorCommandHandler: class TestMonitorCommandHandler:
@@ -178,7 +186,7 @@ class TestMonitorCommandHandler:
mock_args.monitor_action = None mock_args.monitor_action = None
# Mock the parser setup to avoid importing issues # Mock the parser setup to avoid importing issues
with patch('kumacli.cmd.monitor.setup_monitor_parser') as mock_setup: with patch("kumacli.cmd.monitor.setup_monitor_parser") as mock_setup:
mock_parser = Mock() mock_parser = Mock()
mock_setup._parser = mock_parser mock_setup._parser = mock_parser
@@ -261,4 +269,6 @@ class TestMonitorCommandHandler:
# Verify # Verify
assert result is False assert result is False
captured = capsys.readouterr() captured = capsys.readouterr()
assert "Unknown monitor action. Use --help for usage information." in captured.out assert (
"Unknown monitor action. Use --help for usage information." in captured.out
)