Files
NeoBot/tests/test_config_loader.py
K2cr2O1 8508fc95f5 feat: 添加测试覆盖率并修复相关问题
refactor(redis_manager): 移除冗余的ConnectionError处理
refactor(event_handler): 优化Bot类型注解
refactor(factory): 移除未使用的GroupCardNoticeEvent

test: 添加全面的单元测试覆盖
- 添加test_import.py测试模块导入
- 添加test_debug.py测试插件加载调试
- 添加test_plugin_error.py测试错误处理
- 添加test_config_loader.py测试配置加载
- 添加test_redis_manager.py测试Redis管理
- 添加test_bot.py测试Bot功能
- 扩展test_models.py测试消息模型
- 添加test_plugin_manager_coverage.py测试插件管理
- 添加test_executor.py测试代码执行器
- 添加test_ws.py测试WebSocket
- 添加test_api.py测试API接口
- 添加test_core_managers.py测试核心管理模块

fix(plugin_manager): 修复插件加载日志变量问题

覆盖率已到达86%(忽略插件)
2026-01-09 23:18:58 +08:00

126 lines
3.8 KiB
Python

import pytest
import tomllib
from pathlib import Path
from core.config_loader import Config
from core.config_models import ConfigModel, NapCatWSModel, BotModel, RedisModel, DockerModel
class TestConfigLoader:
def test_config_initialization(self, tmp_path):
"""测试配置加载器初始化。"""
config_file = tmp_path / "config.toml"
config_file.write_text("""
[napcat_ws]
uri = "ws://localhost:3560"
token = "test_token"
[bot]
command = ["/"]
ignore_self_message = true
permission_denied_message = "权限不足,需要 {permission_name} 权限"
[redis]
host = "localhost"
port = 6379
db = 0
password = ""
[docker]
base_url = "unix:///var/run/docker.sock"
sandbox_image = "python-sandbox:latest"
timeout = 10
concurrency_limit = 5
tls_verify = false
""", encoding='utf-8')
config = Config(str(config_file))
assert config.path == config_file
assert isinstance(config._model, ConfigModel)
def test_config_properties(self, tmp_path):
"""测试配置属性访问。"""
config_file = tmp_path / "config.toml"
config_file.write_text("""
[napcat_ws]
uri = "ws://localhost:3560"
token = "test_token"
reconnect_interval = 5
[bot]
command = ["/"]
ignore_self_message = true
permission_denied_message = "权限不足,需要 {permission_name} 权限"
[redis]
host = "localhost"
port = 6379
db = 0
password = ""
[docker]
base_url = "unix:///var/run/docker.sock"
sandbox_image = "python-sandbox:latest"
timeout = 10
concurrency_limit = 5
tls_verify = false
""", encoding='utf-8')
config = Config(str(config_file))
assert isinstance(config.napcat_ws, NapCatWSModel)
assert config.napcat_ws.uri == "ws://localhost:3560"
assert config.napcat_ws.token == "test_token"
assert config.napcat_ws.reconnect_interval == 5
assert isinstance(config.bot, BotModel)
assert config.bot.command == ["/"]
assert config.bot.ignore_self_message is True
assert config.bot.permission_denied_message == "权限不足,需要 {permission_name} 权限"
assert isinstance(config.redis, RedisModel)
assert config.redis.host == "localhost"
assert config.redis.port == 6379
assert config.redis.db == 0
assert config.redis.password == ""
assert isinstance(config.docker, DockerModel)
assert config.docker.base_url == "unix:///var/run/docker.sock"
assert config.docker.sandbox_image == "python-sandbox:latest"
assert config.docker.timeout == 10
assert config.docker.concurrency_limit == 5
assert config.docker.tls_verify is False
def test_config_file_not_found(self, tmp_path):
"""测试配置文件不存在时的错误处理。"""
config_file = tmp_path / "non_existent_config.toml"
with pytest.raises(FileNotFoundError):
Config(str(config_file))
def test_config_invalid_format(self, tmp_path):
"""测试配置文件格式错误时的错误处理。"""
config_file = tmp_path / "invalid_config.toml"
config_file.write_text("invalid toml format", encoding='utf-8')
with pytest.raises(Exception):
Config(str(config_file))
def test_config_validation_error(self, tmp_path):
"""测试配置验证失败时的错误处理。"""
config_file = tmp_path / "invalid_config.toml"
config_file.write_text("""
[napcat_ws]
uri = "ws://localhost:3560"
[bot]
command = ["/"]
ignore_self_message = true
permission_denied_message = "权限不足,需要 {permission_name} 权限"
[redis]
host = "localhost"
port = 6379
db = 0
password = ""
[docker]
base_url = "unix:///var/run/docker.sock"
sandbox_image = "python-sandbox:latest"
timeout = 10
concurrency_limit = 5
tls_verify = false
""", encoding='utf-8')
with pytest.raises(Exception):
Config(str(config_file))