"""健康检查完整测试."""
import pytest
from unittest.mock import Mock, MagicMock, patch
from src.health_check import health_check
from src.config_manager import ConfigManager
class TestHealthCheckComplete:
"""健康检查完整测试类."""
@pytest.fixture
def config_manager(self, temp_config_dir):
"""创建配置管理器."""
return ConfigManager(config_path=temp_config_dir / ".graphitiace" / "config.json")
def test_health_check_no_neo4j_config(self, config_manager):
"""测试没有 Neo4j 配置时的健康检查."""
with patch('src.health_check.ConfigManager', return_value=config_manager):
with patch('src.health_check.GraphitiClient') as mock_client_class:
mock_client = Mock()
mock_client.connect.return_value = False
mock_client_class.return_value = mock_client
result = health_check()
assert result is not None
assert isinstance(result, dict)
assert 'status' in result
assert 'checks' in result
def test_health_check_with_neo4j_config(self, config_manager):
"""测试有 Neo4j 配置时的健康检查."""
# 配置 Neo4j
config_manager.configure_neo4j(
uri="bolt://localhost:7687",
username="neo4j",
password="test_password"
)
with patch('src.health_check.ConfigManager', return_value=config_manager):
with patch('src.health_check.GraphitiClient') as mock_client_class:
mock_client = Mock()
mock_client.connect.return_value = True
mock_client.query_knowledge_graph.return_value = {"success": True}
mock_client_class.return_value = mock_client
result = health_check()
assert result is not None
assert isinstance(result, dict)
assert 'status' in result
assert 'checks' in result
assert 'database' in result['checks']
def test_health_check_database_connected(self, config_manager):
"""测试数据库已连接时的健康检查."""
config_manager.configure_neo4j(
uri="bolt://localhost:7687",
username="neo4j",
password="test_password"
)
with patch('src.health_check.ConfigManager', return_value=config_manager):
with patch('src.health_check.GraphitiClient') as mock_client_class:
mock_client = Mock()
mock_client.connect.return_value = True
mock_client.query_knowledge_graph.return_value = {"success": True}
mock_client_class.return_value = mock_client
result = health_check()
assert result is not None
assert isinstance(result, dict)
assert 'status' in result
assert 'checks' in result
def test_health_check_database_not_connected(self, config_manager):
"""测试数据库未连接时的健康检查."""
config_manager.configure_neo4j(
uri="bolt://localhost:7687",
username="neo4j",
password="test_password"
)
with patch('src.health_check.ConfigManager', return_value=config_manager):
with patch('src.health_check.GraphitiClient') as mock_client_class:
mock_client = Mock()
mock_client.connect.return_value = False
mock_client_class.return_value = mock_client
result = health_check()
assert result is not None
assert isinstance(result, dict)
assert 'status' in result
assert 'checks' in result
def test_health_check_with_api_config(self, config_manager):
"""测试有 API 配置时的健康检查."""
config_manager.configure_neo4j(
uri="bolt://localhost:7687",
username="neo4j",
password="test_password"
)
config_manager.configure_api(
provider="openai",
api_key="test_key"
)
with patch('src.health_check.ConfigManager', return_value=config_manager):
with patch('src.health_check.GraphitiClient') as mock_client_class:
mock_client = Mock()
mock_client.connect.return_value = True
mock_client.query_knowledge_graph.return_value = {"success": True}
mock_client_class.return_value = mock_client
result = health_check()
assert result is not None
assert isinstance(result, dict)
assert 'status' in result
assert 'checks' in result
assert 'graphiti' in result['checks']
def test_health_check_exception_handling(self, config_manager):
"""测试健康检查异常处理."""
with patch('src.health_check.ConfigManager', return_value=config_manager):
with patch('src.health_check.GraphitiClient') as mock_client_class:
mock_client = Mock()
mock_client.connect.return_value = True
mock_client.query_knowledge_graph.side_effect = Exception("Connection error")
mock_client_class.return_value = mock_client
result = health_check()
assert result is not None
assert isinstance(result, dict)
assert 'status' in result
# 应该包含错误信息或状态为 error
assert result['checks']['database']['status'] == 'error'