From 1d1b9f6574fd56d1c48a312e1f575fac5e8b189e Mon Sep 17 00:00:00 2001 From: suyiiyii Date: Sat, 28 Sep 2024 16:49:10 +0800 Subject: [PATCH] =?UTF-8?q?:bug:=20config=20=E4=B8=AD=E6=B7=BB=E5=8A=A0=20?= =?UTF-8?q?clear=5Fdb=20=E6=96=B9=E6=B3=95=EF=BC=8C=E7=94=A8=E4=BA=8E?= =?UTF-8?q?=E6=B8=85=E7=A9=BA=E6=95=B0=E6=8D=AE=E5=BA=93=E5=86=85=E5=AE=B9?= =?UTF-8?q?=EF=BC=9B=E6=B7=BB=E5=8A=A0clear=5Fdb=20fixture=EF=BC=8C?= =?UTF-8?q?=E7=94=A8=E4=BA=8E=E5=9C=A8=E5=8D=95=E6=B5=8B=E5=89=8D=E5=90=8E?= =?UTF-8?q?=E6=B8=85=E7=A9=BA=E6=95=B0=E6=8D=AE=E5=BA=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- nonebot_bison/config/db_config.py | 11 +++ tests/conftest.py | 10 ++ tests/sub_manager/test_add_cookie.py | 8 +- tests/sub_manager/test_delete_cookie.py | 121 ++++++++++++------------ tests/subs_io/test_subs_io.py | 1 + 5 files changed, 89 insertions(+), 62 deletions(-) diff --git a/nonebot_bison/config/db_config.py b/nonebot_bison/config/db_config.py index 996bf2e..b50f306 100644 --- a/nonebot_bison/config/db_config.py +++ b/nonebot_bison/config/db_config.py @@ -363,5 +363,16 @@ class DBConfig: res.sort(key=lambda x: (x.target.platform_name, x.cookie_id, x.target_id)) return res + async def clear_db(self): + """清空数据库""" + async with create_session() as sess: + await sess.execute(delete(User)) + await sess.execute(delete(Target)) + await sess.execute(delete(ScheduleTimeWeight)) + await sess.execute(delete(Subscribe)) + await sess.execute(delete(Cookie)) + await sess.execute(delete(CookieTarget)) + await sess.commit() + config = DBConfig() diff --git a/tests/conftest.py b/tests/conftest.py index 65a1066..2d3c0ab 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -123,3 +123,13 @@ async def _no_browser(app: App, mocker: MockerFixture): mocker.patch.object(plugin_config, "bison_use_browser", False) mocker.patch("nonebot_bison.platform.unavailable_paltforms", _get_unavailable_platforms()) + + +@pytest.fixture +async def _clear_db(): + from nonebot_bison.config import config + + await config.clear_db() + yield + await config.clear_db() + return diff --git a/tests/sub_manager/test_add_cookie.py b/tests/sub_manager/test_add_cookie.py index 28c2317..e6d4653 100644 --- a/tests/sub_manager/test_add_cookie.py +++ b/tests/sub_manager/test_add_cookie.py @@ -1,5 +1,6 @@ import json +import pytest from nonebug.app import App from pytest_mock import MockerFixture @@ -30,7 +31,8 @@ async def test_add_cookie_rule(app: App, mocker: MockerFixture): ctx.should_pass_permission() -async def test_add_cookie_target_no_cookie(app: App, mocker: MockerFixture): +@pytest.mark.usefixtures("_clear_db") +async def test_add_cookie_target_no_cookie(app: App): from nonebot.adapters.onebot.v11.bot import Bot from nonebot.adapters.onebot.v11.message import Message @@ -80,7 +82,8 @@ async def test_add_cookie_target_no_cookie(app: App, mocker: MockerFixture): ) -async def test_add_cookie(app: App, mocker: MockerFixture): +@pytest.mark.usefixtures("_clear_db") +async def test_add_cookie(app: App): from nonebot.adapters.onebot.v11.bot import Bot from nonebot.adapters.onebot.v11.message import Message @@ -192,7 +195,6 @@ async def test_add_cookie(app: App, mocker: MockerFixture): async def test_add_cookie_target_no_target(app: App, mocker: MockerFixture): - from nonebot.adapters.onebot.v11.bot import Bot from nonebot.adapters.onebot.v11.message import Message diff --git a/tests/sub_manager/test_delete_cookie.py b/tests/sub_manager/test_delete_cookie.py index b3541ea..44827de 100644 --- a/tests/sub_manager/test_delete_cookie.py +++ b/tests/sub_manager/test_delete_cookie.py @@ -1,10 +1,72 @@ import json +import pytest from nonebug.app import App from ..utils import fake_superuser, fake_private_message_event +@pytest.mark.usefixtures("_clear_db") +async def test_del_cookie(app: App): + from nonebug_saa import should_send_saa + from nonebot.adapters.onebot.v11.bot import Bot + from nonebot.adapters.onebot.v11.message import Message + from nonebot_plugin_saa import TargetQQGroup, MessageFactory + + from nonebot_bison.config import config + from nonebot_bison.config.db_model import Cookie + from nonebot_bison.types import Target as T_Target + from nonebot_bison.sub_manager import del_cookie_matcher + + async with app.test_matcher(del_cookie_matcher) as ctx: + bot = ctx.create_bot(base=Bot) + event = fake_private_message_event( + message=Message("删除cookie"), sender=fake_superuser, to_me=True, user_id=fake_superuser.user_id + ) + ctx.receive_event(bot, event) + ctx.should_pass_rule() + ctx.should_pass_permission() + ctx.should_call_send(event, "暂无已添加的 Cookie\n请使用“添加cookie”命令添加", True) + + async with app.test_matcher(del_cookie_matcher) as ctx: + bot = ctx.create_bot(base=Bot) + target = T_Target("weibo_id") + platform_name = "weibo" + await config.add_subscribe( + TargetQQGroup(group_id=123), + target=target, + target_name="weibo_name", + platform_name=platform_name, + cats=[], + tags=[], + ) + await config.add_cookie(Cookie(content=json.dumps({"cookie": "test"}), site_name="weibo.com")) + + event_1 = fake_private_message_event( + message=Message("删除cookie"), sender=fake_superuser, to_me=True, user_id=fake_superuser.user_id + ) + ctx.receive_event(bot, event_1) + ctx.should_pass_rule() + ctx.should_pass_permission() + should_send_saa( + ctx, + MessageFactory( + "已添加的 Cookie 为:\n1 weibo.com unnamed cookie" + " 0个关联\n请输入要删除的 Cookie 的序号\n输入'取消'中止" + ), + bot, + event=event_1, + ) + event_2 = fake_private_message_event( + message=Message("1"), sender=fake_superuser, to_me=True, user_id=fake_superuser.user_id + ) + ctx.receive_event(bot, event_2) + ctx.should_pass_rule() + ctx.should_pass_permission() + ctx.should_call_send(event_2, "删除成功", True) + + +@pytest.mark.usefixtures("_clear_db") async def test_del_cookie_err(app: App): from nonebug_saa import should_send_saa from nonebot.adapters.onebot.v11.bot import Bot @@ -71,62 +133,3 @@ async def test_del_cookie_err(app: App): ctx.should_call_send(event_2, "只能删除未关联的 Cookie,请使用“取消关联cookie”命令取消关联", True) ctx.should_call_send(event_2, "删除错误", True) ctx.should_rejected() - - -async def test_del_cookie(app: App): - from nonebug_saa import should_send_saa - from nonebot.adapters.onebot.v11.bot import Bot - from nonebot.adapters.onebot.v11.message import Message - from nonebot_plugin_saa import TargetQQGroup, MessageFactory - - from nonebot_bison.config import config - from nonebot_bison.config.db_model import Cookie - from nonebot_bison.types import Target as T_Target - from nonebot_bison.sub_manager import del_cookie_matcher - - async with app.test_matcher(del_cookie_matcher) as ctx: - bot = ctx.create_bot(base=Bot) - event = fake_private_message_event( - message=Message("删除cookie"), sender=fake_superuser, to_me=True, user_id=fake_superuser.user_id - ) - ctx.receive_event(bot, event) - ctx.should_pass_rule() - ctx.should_pass_permission() - ctx.should_call_send(event, "暂无已添加的 Cookie\n请使用“添加cookie”命令添加", True) - - async with app.test_matcher(del_cookie_matcher) as ctx: - bot = ctx.create_bot(base=Bot) - target = T_Target("weibo_id") - platform_name = "weibo" - await config.add_subscribe( - TargetQQGroup(group_id=123), - target=target, - target_name="weibo_name", - platform_name=platform_name, - cats=[], - tags=[], - ) - await config.add_cookie(Cookie(content=json.dumps({"cookie": "test"}), site_name="weibo.com")) - - event_1 = fake_private_message_event( - message=Message("删除cookie"), sender=fake_superuser, to_me=True, user_id=fake_superuser.user_id - ) - ctx.receive_event(bot, event_1) - ctx.should_pass_rule() - ctx.should_pass_permission() - should_send_saa( - ctx, - MessageFactory( - "已添加的 Cookie 为:\n1 weibo.com unnamed cookie" - " 0个关联\n请输入要删除的 Cookie 的序号\n输入'取消'中止" - ), - bot, - event=event_1, - ) - event_2 = fake_private_message_event( - message=Message("1"), sender=fake_superuser, to_me=True, user_id=fake_superuser.user_id - ) - ctx.receive_event(bot, event_2) - ctx.should_pass_rule() - ctx.should_pass_permission() - ctx.should_call_send(event_2, "删除成功", True) diff --git a/tests/subs_io/test_subs_io.py b/tests/subs_io/test_subs_io.py index 364f1fb..700becf 100644 --- a/tests/subs_io/test_subs_io.py +++ b/tests/subs_io/test_subs_io.py @@ -5,6 +5,7 @@ from nonebot.compat import model_dump from .utils import get_json +@pytest.mark.usefixtures("_clear_db") async def test_subs_export(app: App, init_scheduler): from nonebot_plugin_saa import TargetQQGroup