"""测试 server.py 中环境变量配置路径的功能."""
import pytest
import os
from pathlib import Path
from unittest.mock import patch, Mock
from src.server import create_server
from src.config_manager import ConfigManager
class TestServerConfigPathEnv:
"""测试通过环境变量指定配置文件路径的功能."""
@pytest.fixture
def temp_config_file(self, tmp_path):
"""创建临时配置文件."""
config_dir = tmp_path / ".graphitiace"
config_dir.mkdir()
config_file = config_dir / "config.json"
config_file.write_text('{"neo4j": {"uri": "bolt://localhost:7687"}}')
return config_file
def test_create_server_with_env_config_path(self, temp_config_file):
"""测试通过环境变量指定配置文件路径(覆盖第51-53行)."""
# 设置环境变量
env_value = str(temp_config_file)
with patch.dict(os.environ, {'GRAPHITIACE_CONFIG_PATH': env_value}):
# 创建服务器,应该使用环境变量指定的配置文件路径
server = create_server()
# 验证服务器已创建
assert server is not None
# 验证配置管理器使用了正确的路径
# 由于create_server内部创建ConfigManager,我们通过检查服务器功能来验证
if hasattr(server, 'request_handlers'):
assert server.request_handlers is not None
def test_create_server_without_env_config_path(self, temp_config_dir):
"""测试没有环境变量时使用默认配置路径."""
# 确保环境变量不存在
if 'GRAPHITIACE_CONFIG_PATH' in os.environ:
del os.environ['GRAPHITIACE_CONFIG_PATH']
# 创建服务器,应该使用默认配置路径
server = create_server()
# 验证服务器已创建
assert server is not None