Fix tests after refactor

This commit is contained in:
Pim van Pelt
2025-08-03 11:43:53 +02:00
parent f1d10458c6
commit 64e1ca124f
5 changed files with 60 additions and 48 deletions

View File

@@ -21,7 +21,7 @@ class TestCLIIntegration:
# Verify parser is created
assert monitor_parser is not None
assert hasattr(setup_monitor_parser, "_parser")
assert monitor_parser.prog.endswith("monitor")
def test_maintenance_parser_setup(self):
"""Test maintenance parser setup"""
@@ -32,7 +32,7 @@ class TestCLIIntegration:
# Verify parser is created
assert maintenance_parser is not None
assert hasattr(setup_maintenance_parser, "_parser")
assert maintenance_parser.prog.endswith("maintenance")
def test_info_parser_setup(self):
"""Test info parser setup"""
@@ -50,16 +50,13 @@ class TestCLIIntegration:
mock_args = Mock()
mock_args.monitor_action = None
# Setup parser reference
mock_parser = Mock()
setup_monitor_parser._parser = mock_parser
# Execute
result = handle_monitor_command(mock_args, mock_client)
# Verify
assert result is False
mock_parser.print_help.assert_called_once()
captured = capsys.readouterr()
assert "Error: No monitor action specified" in captured.out
def test_maintenance_help_message(self, mock_client, capsys):
"""Test maintenance command shows help when no action specified"""
@@ -67,16 +64,13 @@ class TestCLIIntegration:
mock_args = Mock()
mock_args.maintenance_action = None
# Setup parser reference
mock_parser = Mock()
setup_maintenance_parser._parser = mock_parser
# Execute
result = handle_maintenance_command(mock_args, mock_client)
# Verify
assert result is False
mock_parser.print_help.assert_called_once()
captured = capsys.readouterr()
assert "Error: No maintenance action specified" in captured.out
def test_monitor_command_with_full_args(self, mock_client):
"""Test monitor command with complete argument structure"""
@@ -87,11 +81,9 @@ class TestCLIIntegration:
mock_args.group = ["web-services"]
# Mock client methods
mock_client.find_monitors_by_pattern.return_value = [
{"id": 1, "name": "test-monitor"}
]
mock_client.get_monitors_in_groups.return_value = [
{"id": 2, "name": "web-service-monitor"}
mock_client.find_and_get_monitors.return_value = [
{"id": 1, "name": "test-monitor"},
{"id": 2, "name": "web-service-monitor"},
]
mock_client.api.pause_monitor.return_value = {"msg": "Paused Successfully."}
@@ -100,8 +92,9 @@ class TestCLIIntegration:
# Verify
assert result is True
mock_client.find_monitors_by_pattern.assert_called_once_with(["test*"])
mock_client.get_monitors_in_groups.assert_called_once_with(["web-services"])
mock_client.find_and_get_monitors.assert_called_once_with(
["test*"], ["web-services"]
)
# Should pause both monitors (deduplicated)
assert mock_client.api.pause_monitor.call_count == 2
@@ -235,7 +228,11 @@ class TestErrorHandling:
mock_args.group = None
# Mock no matches found
mock_client.find_monitors_by_pattern.return_value = []
def mock_find_and_get_monitors(*args, **kwargs):
print("Error: No monitors found matching the specified patterns or groups")
return []
mock_client.find_and_get_monitors.side_effect = mock_find_and_get_monitors
# Execute
result = handle_monitor_command(mock_args, mock_client)
@@ -255,7 +252,11 @@ class TestErrorHandling:
mock_args.maintenance_action = "list"
# Mock API error
mock_client.api.get_maintenances.side_effect = Exception("Connection timeout")
from uptime_kuma_api import UptimeKumaException
mock_client.api.get_maintenances.side_effect = UptimeKumaException(
"Connection timeout"
)
# Execute
result = handle_maintenance_command(mock_args, mock_client)

View File

@@ -171,10 +171,12 @@ class TestKumaClient:
def test_find_monitors_by_pattern_api_error(self, capsys):
"""Test finding monitors handles API errors"""
from uptime_kuma_api import UptimeKumaException
client = KumaClient("http://test.com")
client.api = Mock()
client.api.get_monitors.side_effect = Exception("API Error")
client.api.get_monitors.side_effect = UptimeKumaException("API Error")
result = client.find_monitors_by_pattern(["Web*"])
assert len(result) == 0
@@ -203,7 +205,9 @@ class TestKumaClient:
@patch("kumacli.client.UptimeKumaApi")
def test_connect_failure(self, mock_api_class, capsys):
"""Test connection failure"""
mock_api_class.side_effect = Exception("Connection failed")
from uptime_kuma_api import UptimeKumaException
mock_api_class.side_effect = UptimeKumaException("Connection failed")
client = KumaClient("http://test.com", "user", "pass")
result = client.connect()

View File

@@ -49,8 +49,10 @@ class TestInfoCommands:
def test_get_info_api_error(self, mock_client, capsys):
"""Test info command with API error"""
from uptime_kuma_api import UptimeKumaException
# Setup
mock_client.api.info.side_effect = Exception("Connection failed")
mock_client.api.info.side_effect = UptimeKumaException("Connection failed")
info_commands = InfoCommands(mock_client)
@@ -80,9 +82,11 @@ class TestInfoCommandHandler:
def test_handle_info_command_with_error(self, mock_client):
"""Test info command handler with error"""
from uptime_kuma_api import UptimeKumaException
# Setup
mock_args = Mock()
mock_client.api.info.side_effect = Exception("API Error")
mock_client.api.info.side_effect = UptimeKumaException("API Error")
# Execute
result = handle_info_command(mock_args, mock_client)

View File

@@ -43,8 +43,10 @@ class TestMaintenanceCommands:
def test_list_maintenances_api_error(self, mock_client, capsys):
"""Test maintenance listing with API error"""
from uptime_kuma_api import UptimeKumaException
# Setup
mock_client.api.get_maintenances.side_effect = Exception("API Error")
mock_client.api.get_maintenances.side_effect = UptimeKumaException("API Error")
maintenance_commands = MaintenanceCommands(mock_client)

View File

@@ -16,8 +16,7 @@ class TestMonitorCommands:
def test_pause_monitors_by_pattern(self, mock_client, mock_monitors, capsys):
"""Test pausing monitors by pattern"""
# Setup
mock_client.api.get_monitors.return_value = mock_monitors
mock_client.find_monitors_by_pattern.return_value = [
mock_client.find_and_get_monitors.return_value = [
{"id": 1, "name": "Test Monitor 1"},
{"id": 2, "name": "Test Monitor 2"},
]
@@ -29,7 +28,7 @@ class TestMonitorCommands:
monitor_commands.pause_monitors(monitor_patterns=["Test*"])
# Verify
mock_client.find_monitors_by_pattern.assert_called_once_with(["Test*"])
mock_client.find_and_get_monitors.assert_called_once_with(["Test*"], None)
assert mock_client.api.pause_monitor.call_count == 2
mock_client.api.pause_monitor.assert_any_call(1)
mock_client.api.pause_monitor.assert_any_call(2)
@@ -43,7 +42,7 @@ class TestMonitorCommands:
def test_pause_monitors_by_group(self, mock_client, mock_monitors, capsys):
"""Test pausing monitors by group"""
# Setup
mock_client.get_monitors_in_groups.return_value = [
mock_client.find_and_get_monitors.return_value = [
{"id": 4, "name": "Child Monitor"}
]
mock_client.api.pause_monitor.return_value = {"msg": "Paused Successfully."}
@@ -54,7 +53,7 @@ class TestMonitorCommands:
monitor_commands.pause_monitors(group_patterns=["Group*"])
# Verify
mock_client.get_monitors_in_groups.assert_called_once_with(["Group*"])
mock_client.find_and_get_monitors.assert_called_once_with(None, ["Group*"])
mock_client.api.pause_monitor.assert_called_once_with(4)
captured = capsys.readouterr()
@@ -63,19 +62,21 @@ class TestMonitorCommands:
def test_pause_monitors_no_patterns(self, mock_client, capsys):
"""Test pausing monitors without patterns"""
# Setup
mock_client.find_and_get_monitors.return_value = []
monitor_commands = MonitorCommands(mock_client)
# Execute
monitor_commands.pause_monitors()
# Verify
captured = capsys.readouterr()
assert "Error: Either --monitor or --group flag is required." in captured.out
mock_client.find_and_get_monitors.assert_called_once_with(None, None)
def test_pause_monitors_no_matches(self, mock_client, capsys):
"""Test pausing monitors with no matches"""
# Setup
mock_client.find_monitors_by_pattern.return_value = []
mock_client.find_and_get_monitors.return_value = []
monitor_commands = MonitorCommands(mock_client)
@@ -83,19 +84,19 @@ class TestMonitorCommands:
monitor_commands.pause_monitors(monitor_patterns=["NonExistent*"])
# Verify
captured = capsys.readouterr()
assert (
"Error: No monitors found matching the specified patterns or groups"
in captured.out
mock_client.find_and_get_monitors.assert_called_once_with(
["NonExistent*"], None
)
def test_pause_monitors_api_error(self, mock_client, capsys):
"""Test pausing monitors with API error"""
# Setup
mock_client.find_monitors_by_pattern.return_value = [
mock_client.find_and_get_monitors.return_value = [
{"id": 1, "name": "Test Monitor 1"}
]
mock_client.api.pause_monitor.side_effect = Exception("API Error")
from uptime_kuma_api import UptimeKumaException
mock_client.api.pause_monitor.side_effect = UptimeKumaException("API Error")
monitor_commands = MonitorCommands(mock_client)
@@ -110,7 +111,7 @@ class TestMonitorCommands:
def test_resume_monitors_by_pattern(self, mock_client, mock_monitors, capsys):
"""Test resuming monitors by pattern"""
# Setup
mock_client.find_monitors_by_pattern.return_value = [
mock_client.find_and_get_monitors.return_value = [
{"id": 2, "name": "Test Monitor 2"}
]
mock_client.api.resume_monitor.return_value = {"msg": "Resumed Successfully."}
@@ -121,6 +122,7 @@ class TestMonitorCommands:
monitor_commands.resume_monitors(monitor_patterns=["Test*"])
# Verify
mock_client.find_and_get_monitors.assert_called_once_with(["Test*"], None)
mock_client.api.resume_monitor.assert_called_once_with(2)
captured = capsys.readouterr()
@@ -165,17 +167,16 @@ class TestMonitorCommands:
def test_resume_monitors_no_args(self, mock_client, capsys):
"""Test resuming monitors without any arguments"""
# Setup
mock_client.find_and_get_monitors.return_value = []
monitor_commands = MonitorCommands(mock_client)
# Execute
monitor_commands.resume_monitors()
# Verify
captured = capsys.readouterr()
assert (
"Error: Either --monitor, --group, or --all flag is required."
in captured.out
)
mock_client.find_and_get_monitors.assert_called_once_with(None, None)
class TestMonitorCommandHandler:
@@ -204,7 +205,7 @@ class TestMonitorCommandHandler:
mock_args.monitor = ["test*"]
mock_args.group = None
mock_client.find_monitors_by_pattern.return_value = [
mock_client.find_and_get_monitors.return_value = [
{"id": 1, "name": "test monitor"}
]
mock_client.api.pause_monitor.return_value = {"msg": "Paused Successfully."}
@@ -225,7 +226,7 @@ class TestMonitorCommandHandler:
mock_args.group = None
mock_args.all = False
mock_client.find_monitors_by_pattern.return_value = [
mock_client.find_and_get_monitors.return_value = [
{"id": 1, "name": "test monitor"}
]
mock_client.api.resume_monitor.return_value = {"msg": "Resumed Successfully."}