Fix tests after refactor
This commit is contained in:
@@ -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 monitor_parser.prog.endswith("monitor")
|
||||||
|
|
||||||
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 maintenance_parser.prog.endswith("maintenance")
|
||||||
|
|
||||||
def test_info_parser_setup(self):
|
def test_info_parser_setup(self):
|
||||||
"""Test info parser setup"""
|
"""Test info parser setup"""
|
||||||
@@ -50,16 +50,13 @@ class TestCLIIntegration:
|
|||||||
mock_args = Mock()
|
mock_args = Mock()
|
||||||
mock_args.monitor_action = None
|
mock_args.monitor_action = None
|
||||||
|
|
||||||
# Setup parser reference
|
|
||||||
mock_parser = Mock()
|
|
||||||
setup_monitor_parser._parser = mock_parser
|
|
||||||
|
|
||||||
# Execute
|
# Execute
|
||||||
result = handle_monitor_command(mock_args, mock_client)
|
result = handle_monitor_command(mock_args, mock_client)
|
||||||
|
|
||||||
# Verify
|
# Verify
|
||||||
assert result is False
|
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):
|
def test_maintenance_help_message(self, mock_client, capsys):
|
||||||
"""Test maintenance command shows help when no action specified"""
|
"""Test maintenance command shows help when no action specified"""
|
||||||
@@ -67,16 +64,13 @@ class TestCLIIntegration:
|
|||||||
mock_args = Mock()
|
mock_args = Mock()
|
||||||
mock_args.maintenance_action = None
|
mock_args.maintenance_action = None
|
||||||
|
|
||||||
# Setup parser reference
|
|
||||||
mock_parser = Mock()
|
|
||||||
setup_maintenance_parser._parser = mock_parser
|
|
||||||
|
|
||||||
# Execute
|
# Execute
|
||||||
result = handle_maintenance_command(mock_args, mock_client)
|
result = handle_maintenance_command(mock_args, mock_client)
|
||||||
|
|
||||||
# Verify
|
# Verify
|
||||||
assert result is False
|
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):
|
def test_monitor_command_with_full_args(self, mock_client):
|
||||||
"""Test monitor command with complete argument structure"""
|
"""Test monitor command with complete argument structure"""
|
||||||
@@ -87,11 +81,9 @@ class TestCLIIntegration:
|
|||||||
mock_args.group = ["web-services"]
|
mock_args.group = ["web-services"]
|
||||||
|
|
||||||
# Mock client methods
|
# Mock client methods
|
||||||
mock_client.find_monitors_by_pattern.return_value = [
|
mock_client.find_and_get_monitors.return_value = [
|
||||||
{"id": 1, "name": "test-monitor"}
|
{"id": 1, "name": "test-monitor"},
|
||||||
]
|
{"id": 2, "name": "web-service-monitor"},
|
||||||
mock_client.get_monitors_in_groups.return_value = [
|
|
||||||
{"id": 2, "name": "web-service-monitor"}
|
|
||||||
]
|
]
|
||||||
mock_client.api.pause_monitor.return_value = {"msg": "Paused Successfully."}
|
mock_client.api.pause_monitor.return_value = {"msg": "Paused Successfully."}
|
||||||
|
|
||||||
@@ -100,8 +92,9 @@ class TestCLIIntegration:
|
|||||||
|
|
||||||
# Verify
|
# Verify
|
||||||
assert result is True
|
assert result is True
|
||||||
mock_client.find_monitors_by_pattern.assert_called_once_with(["test*"])
|
mock_client.find_and_get_monitors.assert_called_once_with(
|
||||||
mock_client.get_monitors_in_groups.assert_called_once_with(["web-services"])
|
["test*"], ["web-services"]
|
||||||
|
)
|
||||||
# Should pause both monitors (deduplicated)
|
# Should pause both monitors (deduplicated)
|
||||||
assert mock_client.api.pause_monitor.call_count == 2
|
assert mock_client.api.pause_monitor.call_count == 2
|
||||||
|
|
||||||
@@ -235,7 +228,11 @@ class TestErrorHandling:
|
|||||||
mock_args.group = None
|
mock_args.group = None
|
||||||
|
|
||||||
# Mock no matches found
|
# 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
|
# Execute
|
||||||
result = handle_monitor_command(mock_args, mock_client)
|
result = handle_monitor_command(mock_args, mock_client)
|
||||||
@@ -255,7 +252,11 @@ class TestErrorHandling:
|
|||||||
mock_args.maintenance_action = "list"
|
mock_args.maintenance_action = "list"
|
||||||
|
|
||||||
# Mock API error
|
# 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
|
# Execute
|
||||||
result = handle_maintenance_command(mock_args, mock_client)
|
result = handle_maintenance_command(mock_args, mock_client)
|
||||||
|
@@ -171,10 +171,12 @@ class TestKumaClient:
|
|||||||
|
|
||||||
def test_find_monitors_by_pattern_api_error(self, capsys):
|
def test_find_monitors_by_pattern_api_error(self, capsys):
|
||||||
"""Test finding monitors handles API errors"""
|
"""Test finding monitors handles API errors"""
|
||||||
|
from uptime_kuma_api import UptimeKumaException
|
||||||
|
|
||||||
client = KumaClient("http://test.com")
|
client = KumaClient("http://test.com")
|
||||||
client.api = Mock()
|
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*"])
|
result = client.find_monitors_by_pattern(["Web*"])
|
||||||
assert len(result) == 0
|
assert len(result) == 0
|
||||||
@@ -203,7 +205,9 @@ class TestKumaClient:
|
|||||||
@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")
|
from uptime_kuma_api import UptimeKumaException
|
||||||
|
|
||||||
|
mock_api_class.side_effect = UptimeKumaException("Connection failed")
|
||||||
|
|
||||||
client = KumaClient("http://test.com", "user", "pass")
|
client = KumaClient("http://test.com", "user", "pass")
|
||||||
result = client.connect()
|
result = client.connect()
|
||||||
|
@@ -49,8 +49,10 @@ class TestInfoCommands:
|
|||||||
|
|
||||||
def test_get_info_api_error(self, mock_client, capsys):
|
def test_get_info_api_error(self, mock_client, capsys):
|
||||||
"""Test info command with API error"""
|
"""Test info command with API error"""
|
||||||
|
from uptime_kuma_api import UptimeKumaException
|
||||||
|
|
||||||
# Setup
|
# Setup
|
||||||
mock_client.api.info.side_effect = Exception("Connection failed")
|
mock_client.api.info.side_effect = UptimeKumaException("Connection failed")
|
||||||
|
|
||||||
info_commands = InfoCommands(mock_client)
|
info_commands = InfoCommands(mock_client)
|
||||||
|
|
||||||
@@ -80,9 +82,11 @@ class TestInfoCommandHandler:
|
|||||||
|
|
||||||
def test_handle_info_command_with_error(self, mock_client):
|
def test_handle_info_command_with_error(self, mock_client):
|
||||||
"""Test info command handler with error"""
|
"""Test info command handler with error"""
|
||||||
|
from uptime_kuma_api import UptimeKumaException
|
||||||
|
|
||||||
# Setup
|
# Setup
|
||||||
mock_args = Mock()
|
mock_args = Mock()
|
||||||
mock_client.api.info.side_effect = Exception("API Error")
|
mock_client.api.info.side_effect = UptimeKumaException("API Error")
|
||||||
|
|
||||||
# Execute
|
# Execute
|
||||||
result = handle_info_command(mock_args, mock_client)
|
result = handle_info_command(mock_args, mock_client)
|
||||||
|
@@ -43,8 +43,10 @@ class TestMaintenanceCommands:
|
|||||||
|
|
||||||
def test_list_maintenances_api_error(self, mock_client, capsys):
|
def test_list_maintenances_api_error(self, mock_client, capsys):
|
||||||
"""Test maintenance listing with API error"""
|
"""Test maintenance listing with API error"""
|
||||||
|
from uptime_kuma_api import UptimeKumaException
|
||||||
|
|
||||||
# Setup
|
# 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)
|
maintenance_commands = MaintenanceCommands(mock_client)
|
||||||
|
|
||||||
|
@@ -16,8 +16,7 @@ class TestMonitorCommands:
|
|||||||
def test_pause_monitors_by_pattern(self, mock_client, mock_monitors, capsys):
|
def test_pause_monitors_by_pattern(self, mock_client, mock_monitors, capsys):
|
||||||
"""Test pausing monitors by pattern"""
|
"""Test pausing monitors by pattern"""
|
||||||
# Setup
|
# Setup
|
||||||
mock_client.api.get_monitors.return_value = mock_monitors
|
mock_client.find_and_get_monitors.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"},
|
||||||
]
|
]
|
||||||
@@ -29,7 +28,7 @@ class TestMonitorCommands:
|
|||||||
monitor_commands.pause_monitors(monitor_patterns=["Test*"])
|
monitor_commands.pause_monitors(monitor_patterns=["Test*"])
|
||||||
|
|
||||||
# Verify
|
# 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
|
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(1)
|
||||||
mock_client.api.pause_monitor.assert_any_call(2)
|
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):
|
def test_pause_monitors_by_group(self, mock_client, mock_monitors, capsys):
|
||||||
"""Test pausing monitors by group"""
|
"""Test pausing monitors by group"""
|
||||||
# Setup
|
# Setup
|
||||||
mock_client.get_monitors_in_groups.return_value = [
|
mock_client.find_and_get_monitors.return_value = [
|
||||||
{"id": 4, "name": "Child Monitor"}
|
{"id": 4, "name": "Child Monitor"}
|
||||||
]
|
]
|
||||||
mock_client.api.pause_monitor.return_value = {"msg": "Paused Successfully."}
|
mock_client.api.pause_monitor.return_value = {"msg": "Paused Successfully."}
|
||||||
@@ -54,7 +53,7 @@ class TestMonitorCommands:
|
|||||||
monitor_commands.pause_monitors(group_patterns=["Group*"])
|
monitor_commands.pause_monitors(group_patterns=["Group*"])
|
||||||
|
|
||||||
# Verify
|
# 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)
|
mock_client.api.pause_monitor.assert_called_once_with(4)
|
||||||
|
|
||||||
captured = capsys.readouterr()
|
captured = capsys.readouterr()
|
||||||
@@ -63,19 +62,21 @@ class TestMonitorCommands:
|
|||||||
|
|
||||||
def test_pause_monitors_no_patterns(self, mock_client, capsys):
|
def test_pause_monitors_no_patterns(self, mock_client, capsys):
|
||||||
"""Test pausing monitors without patterns"""
|
"""Test pausing monitors without patterns"""
|
||||||
|
# Setup
|
||||||
|
mock_client.find_and_get_monitors.return_value = []
|
||||||
|
|
||||||
monitor_commands = MonitorCommands(mock_client)
|
monitor_commands = MonitorCommands(mock_client)
|
||||||
|
|
||||||
# Execute
|
# Execute
|
||||||
monitor_commands.pause_monitors()
|
monitor_commands.pause_monitors()
|
||||||
|
|
||||||
# Verify
|
# Verify
|
||||||
captured = capsys.readouterr()
|
mock_client.find_and_get_monitors.assert_called_once_with(None, None)
|
||||||
assert "Error: Either --monitor or --group flag is required." in captured.out
|
|
||||||
|
|
||||||
def test_pause_monitors_no_matches(self, mock_client, capsys):
|
def test_pause_monitors_no_matches(self, mock_client, capsys):
|
||||||
"""Test pausing monitors with no matches"""
|
"""Test pausing monitors with no matches"""
|
||||||
# Setup
|
# Setup
|
||||||
mock_client.find_monitors_by_pattern.return_value = []
|
mock_client.find_and_get_monitors.return_value = []
|
||||||
|
|
||||||
monitor_commands = MonitorCommands(mock_client)
|
monitor_commands = MonitorCommands(mock_client)
|
||||||
|
|
||||||
@@ -83,19 +84,19 @@ class TestMonitorCommands:
|
|||||||
monitor_commands.pause_monitors(monitor_patterns=["NonExistent*"])
|
monitor_commands.pause_monitors(monitor_patterns=["NonExistent*"])
|
||||||
|
|
||||||
# Verify
|
# Verify
|
||||||
captured = capsys.readouterr()
|
mock_client.find_and_get_monitors.assert_called_once_with(
|
||||||
assert (
|
["NonExistent*"], None
|
||||||
"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"""
|
||||||
# Setup
|
# Setup
|
||||||
mock_client.find_monitors_by_pattern.return_value = [
|
mock_client.find_and_get_monitors.return_value = [
|
||||||
{"id": 1, "name": "Test Monitor 1"}
|
{"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)
|
monitor_commands = MonitorCommands(mock_client)
|
||||||
|
|
||||||
@@ -110,7 +111,7 @@ class TestMonitorCommands:
|
|||||||
def test_resume_monitors_by_pattern(self, mock_client, mock_monitors, capsys):
|
def test_resume_monitors_by_pattern(self, mock_client, mock_monitors, capsys):
|
||||||
"""Test resuming monitors by pattern"""
|
"""Test resuming monitors by pattern"""
|
||||||
# Setup
|
# Setup
|
||||||
mock_client.find_monitors_by_pattern.return_value = [
|
mock_client.find_and_get_monitors.return_value = [
|
||||||
{"id": 2, "name": "Test Monitor 2"}
|
{"id": 2, "name": "Test Monitor 2"}
|
||||||
]
|
]
|
||||||
mock_client.api.resume_monitor.return_value = {"msg": "Resumed Successfully."}
|
mock_client.api.resume_monitor.return_value = {"msg": "Resumed Successfully."}
|
||||||
@@ -121,6 +122,7 @@ class TestMonitorCommands:
|
|||||||
monitor_commands.resume_monitors(monitor_patterns=["Test*"])
|
monitor_commands.resume_monitors(monitor_patterns=["Test*"])
|
||||||
|
|
||||||
# Verify
|
# Verify
|
||||||
|
mock_client.find_and_get_monitors.assert_called_once_with(["Test*"], None)
|
||||||
mock_client.api.resume_monitor.assert_called_once_with(2)
|
mock_client.api.resume_monitor.assert_called_once_with(2)
|
||||||
|
|
||||||
captured = capsys.readouterr()
|
captured = capsys.readouterr()
|
||||||
@@ -165,17 +167,16 @@ class TestMonitorCommands:
|
|||||||
|
|
||||||
def test_resume_monitors_no_args(self, mock_client, capsys):
|
def test_resume_monitors_no_args(self, mock_client, capsys):
|
||||||
"""Test resuming monitors without any arguments"""
|
"""Test resuming monitors without any arguments"""
|
||||||
|
# Setup
|
||||||
|
mock_client.find_and_get_monitors.return_value = []
|
||||||
|
|
||||||
monitor_commands = MonitorCommands(mock_client)
|
monitor_commands = MonitorCommands(mock_client)
|
||||||
|
|
||||||
# Execute
|
# Execute
|
||||||
monitor_commands.resume_monitors()
|
monitor_commands.resume_monitors()
|
||||||
|
|
||||||
# Verify
|
# Verify
|
||||||
captured = capsys.readouterr()
|
mock_client.find_and_get_monitors.assert_called_once_with(None, None)
|
||||||
assert (
|
|
||||||
"Error: Either --monitor, --group, or --all flag is required."
|
|
||||||
in captured.out
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
class TestMonitorCommandHandler:
|
class TestMonitorCommandHandler:
|
||||||
@@ -204,7 +205,7 @@ class TestMonitorCommandHandler:
|
|||||||
mock_args.monitor = ["test*"]
|
mock_args.monitor = ["test*"]
|
||||||
mock_args.group = None
|
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"}
|
{"id": 1, "name": "test monitor"}
|
||||||
]
|
]
|
||||||
mock_client.api.pause_monitor.return_value = {"msg": "Paused Successfully."}
|
mock_client.api.pause_monitor.return_value = {"msg": "Paused Successfully."}
|
||||||
@@ -225,7 +226,7 @@ class TestMonitorCommandHandler:
|
|||||||
mock_args.group = None
|
mock_args.group = None
|
||||||
mock_args.all = False
|
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"}
|
{"id": 1, "name": "test monitor"}
|
||||||
]
|
]
|
||||||
mock_client.api.resume_monitor.return_value = {"msg": "Resumed Successfully."}
|
mock_client.api.resume_monitor.return_value = {"msg": "Resumed Successfully."}
|
||||||
|
Reference in New Issue
Block a user