nonebot-bison/tests/conftest.py
uy/sun 8da8f66fcf
💥 适配最新的 DataStore 插件,并切换模型为 SQLModel (#178)
* 使用 SQLModel

* 处理数据库迁移

* 与之前的模型相匹配

* sqlmodel 和 sqlalchemy 的导入移入测试函数内

并且使用 init_db 且测试前加载插件

* 重命名 alembic_version 表之前先检查是否存在且 version_num 属于插件

* 降级应该是把名称重新命名回去而不是删掉

* 不再设置 arbitrary_types_allowed 为 True
2023-01-30 22:52:11 +08:00

67 lines
1.8 KiB
Python

import asyncio
import typing
from pathlib import Path
import nonebot
import pytest
from nonebug.app import App
from sqlalchemy.ext.asyncio.session import AsyncSession
from sqlalchemy.sql.expression import delete
@pytest.fixture
async def app(nonebug_init: None, tmp_path: Path, monkeypatch: pytest.MonkeyPatch):
import nonebot
config = nonebot.get_driver().config
config.bison_config_path = str(tmp_path / "legacy_config")
config.datastore_config_dir = str(tmp_path / "config")
config.datastore_cache_dir = str(tmp_path / "cache")
config.datastore_data_dir = str(tmp_path / "data")
config.command_start = {""}
config.superusers = {"10001"}
config.log_level = "TRACE"
config.bison_filter_log = False
nonebot.require("nonebot_bison")
return App(monkeypatch)
@pytest.fixture
def dummy_user_subinfo(app: App):
from nonebot_bison.types import User, UserSubInfo
user = User(123, "group")
return UserSubInfo(user=user, categories=[], tags=[])
@pytest.fixture
async def db_migration(app: App):
from nonebot_bison.config.db_model import Subscribe, Target, User
from nonebot_plugin_datastore.db import get_engine, init_db
await init_db()
async with AsyncSession(get_engine()) as sess:
await sess.execute(delete(User))
await sess.execute(delete(Subscribe))
await sess.execute(delete(Target))
await sess.commit()
await sess.close()
@pytest.fixture
async def init_scheduler(db_migration):
from nonebot_bison.scheduler.manager import init_scheduler
await init_scheduler()
@pytest.fixture
async def use_legacy_config(app: App):
import aiofiles
from nonebot_bison.config.config_legacy import config, get_config_path
async with aiofiles.open(get_config_path()[0], "w") as f:
await f.write("{}")
config._do_init()