Add tests
This commit is contained in:
92
tests/test_info.py
Normal file
92
tests/test_info.py
Normal file
@@ -0,0 +1,92 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
import pytest
|
||||
from unittest.mock import Mock, patch
|
||||
from io import StringIO
|
||||
import sys
|
||||
|
||||
from kumacli.cmd.info import InfoCommands, handle_info_command
|
||||
|
||||
|
||||
class TestInfoCommands:
|
||||
def test_get_info_success(self, mock_client, capsys):
|
||||
"""Test successful info retrieval"""
|
||||
# Setup
|
||||
mock_info_data = {
|
||||
"version": "1.23.0",
|
||||
"hostname": "kuma-server",
|
||||
"primaryBaseURL": "https://status.example.com"
|
||||
}
|
||||
mock_client.api.info.return_value = mock_info_data
|
||||
|
||||
info_commands = InfoCommands(mock_client)
|
||||
|
||||
# Execute
|
||||
info_commands.get_info()
|
||||
|
||||
# Verify
|
||||
mock_client.api.info.assert_called_once()
|
||||
captured = capsys.readouterr()
|
||||
assert "Server Information:" in captured.out
|
||||
assert "version: 1.23.0" in captured.out
|
||||
assert "hostname: kuma-server" in captured.out
|
||||
assert "primaryBaseURL: https://status.example.com" in captured.out
|
||||
|
||||
def test_get_info_empty_response(self, mock_client, capsys):
|
||||
"""Test info command with empty response"""
|
||||
# Setup
|
||||
mock_client.api.info.return_value = None
|
||||
|
||||
info_commands = InfoCommands(mock_client)
|
||||
|
||||
# Execute
|
||||
info_commands.get_info()
|
||||
|
||||
# Verify
|
||||
mock_client.api.info.assert_called_once()
|
||||
captured = capsys.readouterr()
|
||||
assert "No server info available" in captured.out
|
||||
|
||||
def test_get_info_api_error(self, mock_client, capsys):
|
||||
"""Test info command with API error"""
|
||||
# Setup
|
||||
mock_client.api.info.side_effect = Exception("Connection failed")
|
||||
|
||||
info_commands = InfoCommands(mock_client)
|
||||
|
||||
# Execute
|
||||
info_commands.get_info()
|
||||
|
||||
# Verify
|
||||
mock_client.api.info.assert_called_once()
|
||||
captured = capsys.readouterr()
|
||||
assert "Error getting server info: Connection failed" in captured.out
|
||||
|
||||
|
||||
class TestInfoCommandHandler:
|
||||
def test_handle_info_command(self, mock_client):
|
||||
"""Test info command handler"""
|
||||
# Setup
|
||||
mock_args = Mock()
|
||||
mock_info_data = {"version": "1.23.0"}
|
||||
mock_client.api.info.return_value = mock_info_data
|
||||
|
||||
# Execute
|
||||
result = handle_info_command(mock_args, mock_client)
|
||||
|
||||
# Verify
|
||||
assert result is True
|
||||
mock_client.api.info.assert_called_once()
|
||||
|
||||
def test_handle_info_command_with_error(self, mock_client):
|
||||
"""Test info command handler with error"""
|
||||
# Setup
|
||||
mock_args = Mock()
|
||||
mock_client.api.info.side_effect = Exception("API Error")
|
||||
|
||||
# Execute
|
||||
result = handle_info_command(mock_args, mock_client)
|
||||
|
||||
# Verify
|
||||
assert result is True # Handler always returns True
|
||||
mock_client.api.info.assert_called_once()
|
Reference in New Issue
Block a user