mirror of
https://github.com/zhenxun-org/zhenxun_bot.git
synced 2026-09-28 16:20:56 +08:00
✅ 添加测试:更新与添加插件 (#1594)
* ✅ 测试更新与添加插件 * ✅ Sourcery建议 * 👷 添加pytest * 🎨 优化代码
This commit is contained in:
+73
-11
@@ -1,37 +1,99 @@
|
||||
import json
|
||||
from pathlib import Path
|
||||
from collections.abc import Callable
|
||||
|
||||
import nonebot
|
||||
import pytest
|
||||
from nonebot.plugin import Plugin
|
||||
from nonebug import NONEBOT_INIT_KWARGS
|
||||
import nonebot
|
||||
from nonebug.app import App
|
||||
from pytest_mock import MockerFixture
|
||||
from respx import MockRouter
|
||||
from pytest_mock import MockerFixture
|
||||
from nonebug import NONEBOT_INIT_KWARGS
|
||||
from nonebug.mixin.process import MatcherContext
|
||||
|
||||
from tests.config import BotId, UserId
|
||||
|
||||
nonebot.load_plugin("nonebot_plugin_session")
|
||||
|
||||
|
||||
def get_response_json(path: str) -> dict:
|
||||
return json.loads(
|
||||
(Path(__file__).parent / "response" / path).read_text(encoding="utf8")
|
||||
)
|
||||
|
||||
|
||||
def pytest_configure(config: pytest.Config) -> None:
|
||||
config.stash[NONEBOT_INIT_KWARGS] = {
|
||||
"driver": "~fastapi+~httpx+~websockets",
|
||||
"superusers": ["AkashiCoin"],
|
||||
"command_start": "",
|
||||
"superusers": [UserId.SUPERUSER.__str__()],
|
||||
"command_start": [""],
|
||||
"session_running_expression": "别急呀,小真寻要宕机了!QAQ",
|
||||
"image_to_bytes": False,
|
||||
"nickname": ["真寻", "小真寻", "绪山真寻", "小寻子"],
|
||||
"session_expire_timeout": 30,
|
||||
"self_nickname": "小真寻",
|
||||
"db_url": "sqlite://:memory:",
|
||||
"platform_superusers": {"qq": ["qq_su"], "dodo": ["dodo_su"]},
|
||||
"platform_superusers": {
|
||||
"qq": [UserId.SUPERUSER_QQ.__str__()],
|
||||
"dodo": [UserId.SUPERUSER_DODO.__str__()],
|
||||
},
|
||||
"host": "127.0.0.1",
|
||||
"port": 8080,
|
||||
"log_level": "DEBUG",
|
||||
}
|
||||
|
||||
|
||||
@pytest.fixture(scope="session", autouse=True)
|
||||
def load_plugin(nonebug_init: None) -> set[Plugin]:
|
||||
return nonebot.load_plugins("zhenxun.plugins")
|
||||
def _init_bot(nonebug_init: None):
|
||||
from nonebot.adapters.onebot.v11 import Adapter as OneBotV11Adapter
|
||||
|
||||
driver = nonebot.get_driver()
|
||||
driver.register_adapter(OneBotV11Adapter)
|
||||
|
||||
nonebot.load_plugin("nonebot_plugin_alconna")
|
||||
nonebot.load_plugin("nonebot_plugin_apscheduler")
|
||||
nonebot.load_plugin("nonebot_plugin_userinfo")
|
||||
nonebot.load_plugin("nonebot_plugin_htmlrender")
|
||||
|
||||
nonebot.load_plugins("zhenxun/builtin_plugins")
|
||||
nonebot.load_plugins("zhenxun/plugins")
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
async def app(app: App, tmp_path: Path, mocker: MockerFixture):
|
||||
mocker.patch("nonebot.drivers.websockets.connect", return_value=MockRouter())
|
||||
return app
|
||||
from zhenxun.services.db_context import init, disconnect
|
||||
|
||||
driver = nonebot.get_driver()
|
||||
# 清除连接钩子,现在 NoneBug 会自动触发 on_bot_connect
|
||||
driver._bot_connection_hook.clear()
|
||||
mock_config_path = mocker.MagicMock()
|
||||
mock_config_path.LOG_PATH = tmp_path / "log"
|
||||
# mock_config_path.LOG_PATH.mkdir(parents=True, exist_ok=True)
|
||||
mock_config_path.DATA_PATH = tmp_path / "data"
|
||||
# mock_config_path.DATA_PATH.mkdir(parents=True, exist_ok=True)
|
||||
mock_config_path.TEMP_PATH = tmp_path / "resources" / "temp"
|
||||
# mock_config_path.TEMP_PATH.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
mocker.patch("zhenxun.configs.path_config", return_value=mock_config_path)
|
||||
|
||||
await init()
|
||||
yield app
|
||||
await disconnect()
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def create_bot() -> Callable:
|
||||
from nonebot.adapters.onebot.v11 import Bot, Adapter
|
||||
|
||||
def _create_bot(context: MatcherContext):
|
||||
return context.create_bot(
|
||||
base=Bot,
|
||||
adapter=nonebot.get_adapter(Adapter),
|
||||
self_id=BotId.QQ_BOT.__str__(),
|
||||
)
|
||||
|
||||
return _create_bot
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mocked_api(respx_mock: MockRouter):
|
||||
return respx_mock
|
||||
|
||||
Reference in New Issue
Block a user