forked from he0119/CoolQBot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
conftest.py
86 lines (65 loc) · 2.74 KB
/
conftest.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
from pathlib import Path
import nonebot
import pytest
from loguru import logger
from nonebug import NONEBOT_INIT_KWARGS
from nonebug.app import App
from pytest_mock import MockerFixture
from sqlalchemy import StaticPool, delete
def pytest_configure(config: pytest.Config) -> None:
config.stash[NONEBOT_INIT_KWARGS] = {
"sqlalchemy_database_url": "sqlite+aiosqlite:///:memory:",
"sqlalchemy_engine_options": {"poolclass": StaticPool},
"alembic_startup_check": False,
"superusers": ["nickname"],
}
@pytest.fixture(scope="session")
def _load_plugin(nonebug_init: None):
from nonebot.adapters.onebot.v11 import Adapter
driver = nonebot.get_driver()
driver.register_adapter(Adapter)
nonebot.require("nonebot_plugin_localstore")
nonebot.require("nonebot_plugin_datastore")
nonebot.require("nonebot_plugin_orm")
nonebot.require("nonebot_plugin_apscheduler")
nonebot.require("nonebot_plugin_saa")
nonebot.require("nonebot_plugin_alconna")
nonebot.require("nonebot_plugin_session")
nonebot.require("nonebot_plugin_user")
nonebot.load_plugins(str(Path(__file__).parent.parent / "src" / "plugins"))
@pytest.fixture()
async def app(tmp_path: Path, _load_plugin, mocker: MockerFixture):
from nonebot_plugin_datastore.config import plugin_config
from nonebot_plugin_orm import init_orm
driver = nonebot.get_driver()
# 清除连接钩子,现在 NoneBug 会自动触发 on_bot_connect
driver._bot_connection_hook.clear()
# 插件数据目录
mocker.patch("nonebot_plugin_localstore.BASE_DATA_DIR", tmp_path / "data")
mocker.patch("nonebot_plugin_localstore.BASE_CACHE_DIR", tmp_path / "cache")
mocker.patch("nonebot_plugin_localstore.BASE_CONFIG_DIR", tmp_path / "config")
plugin_config.datastore_cache_dir = tmp_path / "cache"
plugin_config.datastore_config_dir = tmp_path / "config"
plugin_config.datastore_data_dir = tmp_path / "data"
mocker.patch("nonebot_plugin_orm._data_dir", tmp_path / "orm")
await init_orm()
return App()
@pytest.fixture()
def caplog(caplog):
handler_id = logger.add(caplog.handler, format="{message}")
yield caplog
logger.remove(handler_id)
@pytest.fixture()
async def _default_user(app: App):
from nonebot_plugin_orm import get_session
from nonebot_plugin_user.models import Bind, User
from nonebot_plugin_user.utils import get_user, set_user_name
await get_user("qq", "10")
await set_user_name("qq", "10", "nickname")
await get_user("qq", "10000")
await set_user_name("qq", "10000", "nickname10000")
yield
# 清除数据库
async with get_session() as session, session.begin():
await session.execute(delete(User))
await session.execute(delete(Bind))