mirror of
https://github.com/suyiiyii/nonebot-bison.git
synced 2026-05-09 10:17:56 +08:00
Merge branch 'dev' into AzideCupric/main
This commit is contained in:
+24
-1
@@ -1,3 +1,4 @@
|
||||
import asyncio
|
||||
import typing
|
||||
from pathlib import Path
|
||||
|
||||
@@ -13,6 +14,9 @@ async def app(nonebug_init: None, tmp_path: Path, monkeypatch: pytest.MonkeyPatc
|
||||
config = nonebot.get_driver().config
|
||||
config.bison_config_path = str(tmp_path)
|
||||
config.command_start = {""}
|
||||
config.superusers = {"10001"}
|
||||
config.log_level = "TRACE"
|
||||
config.bison_filter_log = False
|
||||
return App(monkeypatch)
|
||||
|
||||
|
||||
@@ -20,5 +24,24 @@ async def app(nonebug_init: None, tmp_path: Path, monkeypatch: pytest.MonkeyPatc
|
||||
def dummy_user_subinfo(app: App):
|
||||
from nonebot_bison.types import User, UserSubInfo
|
||||
|
||||
user = User("123", "group")
|
||||
user = User(123, "group")
|
||||
return UserSubInfo(user=user, category_getter=lambda _: [], tag_getter=lambda _: [])
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def task_watchdog(request):
|
||||
def cancel_test_on_exception(task: asyncio.Task):
|
||||
def maybe_cancel_clbk(t: asyncio.Task):
|
||||
exception = t.exception()
|
||||
if exception is None:
|
||||
return
|
||||
|
||||
for task in asyncio.all_tasks():
|
||||
coro = task.get_coro()
|
||||
if coro.__qualname__ == request.function.__qualname__:
|
||||
task.cancel()
|
||||
return
|
||||
|
||||
task.add_done_callback(maybe_cancel_clbk)
|
||||
|
||||
return cancel_test_on_exception
|
||||
|
||||
@@ -453,3 +453,17 @@ async def test_del_sub(app: App):
|
||||
ctx.should_finished()
|
||||
subs = config.list_subscribe(10000, "group")
|
||||
assert len(subs) == 0
|
||||
|
||||
|
||||
async def test_test(app: App):
|
||||
from nonebot.adapters.onebot.v11.bot import Bot
|
||||
from nonebot.adapters.onebot.v11.message import Message
|
||||
from nonebot_bison.config_manager import test_matcher
|
||||
|
||||
async with app.test_matcher(test_matcher) as ctx:
|
||||
bot = ctx.create_bot(base=Bot)
|
||||
event = fake_group_message_event(message=Message("testtt"))
|
||||
ctx.receive_event(bot, event)
|
||||
ctx.should_pass_permission()
|
||||
ctx.should_pass_rule()
|
||||
ctx.should_call_send(event, "666", True)
|
||||
|
||||
@@ -0,0 +1,45 @@
|
||||
from nonebug import App
|
||||
|
||||
from .utils import fake_admin_user, fake_private_message_event, fake_superuser
|
||||
|
||||
|
||||
async def test_query(app: App):
|
||||
from nonebot.adapters.onebot.v11.bot import Bot
|
||||
from nonebot.adapters.onebot.v11.message import Message
|
||||
from nonebot_bison.config_manager import group_manage_matcher
|
||||
|
||||
async with app.test_matcher(group_manage_matcher) as ctx:
|
||||
bot = ctx.create_bot(base=Bot)
|
||||
event = fake_private_message_event(
|
||||
message=Message("群管理"), sender=fake_superuser
|
||||
)
|
||||
ctx.receive_event(bot, event)
|
||||
ctx.should_pass_rule()
|
||||
ctx.should_pass_permission()
|
||||
ctx.should_call_api(
|
||||
"get_group_list", {}, [{"group_id": 101, "group_name": "test group"}]
|
||||
)
|
||||
ctx.should_call_send(
|
||||
event, Message("请选择需要管理的群:\n1. 101 - test group\n请输入左侧序号"), True
|
||||
)
|
||||
event_1_err = fake_private_message_event(
|
||||
message=Message("0"), sender=fake_superuser
|
||||
)
|
||||
ctx.receive_event(bot, event_1_err)
|
||||
ctx.should_rejected()
|
||||
ctx.should_call_send(event_1_err, "请输入正确序号", True)
|
||||
event_1_ok = fake_private_message_event(
|
||||
message=Message("1"), sender=fake_superuser
|
||||
)
|
||||
ctx.receive_event(bot, event_1_ok)
|
||||
ctx.should_call_send(event_1_ok, "请输入需要使用的命令:添加订阅,查询订阅,删除订阅", True)
|
||||
event_2_err = fake_private_message_event(
|
||||
message=Message("222"), sender=fake_superuser
|
||||
)
|
||||
ctx.receive_event(bot, event_2_err)
|
||||
ctx.should_rejected()
|
||||
ctx.should_call_send(event_2_err, "请输入正确的命令", True)
|
||||
event_2_ok = fake_private_message_event(
|
||||
message=Message("查询订阅"), sender=fake_superuser
|
||||
)
|
||||
ctx.receive_event(bot, event_2_ok)
|
||||
@@ -1,6 +1,7 @@
|
||||
import typing
|
||||
|
||||
import pytest
|
||||
from flaky import flaky
|
||||
from nonebug.app import App
|
||||
|
||||
if typing.TYPE_CHECKING:
|
||||
@@ -40,7 +41,7 @@ merge_source_9_2 = [
|
||||
]
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@flaky
|
||||
async def test_9_merge(app: App):
|
||||
from nonebot_bison.post import Post
|
||||
|
||||
@@ -50,7 +51,7 @@ async def test_9_merge(app: App):
|
||||
await post.generate_messages()
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@flaky
|
||||
async def test_9_merge_2(app: App):
|
||||
from nonebot_bison.post import Post
|
||||
|
||||
@@ -60,7 +61,7 @@ async def test_9_merge_2(app: App):
|
||||
await post.generate_messages()
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@flaky
|
||||
async def test_6_merge(app: App):
|
||||
from nonebot_bison.post import Post
|
||||
|
||||
@@ -69,7 +70,7 @@ async def test_6_merge(app: App):
|
||||
assert len(post.pics) == 5
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@flaky
|
||||
async def test_3_merge(app: App):
|
||||
from nonebot_bison.post import Post
|
||||
|
||||
@@ -78,7 +79,7 @@ async def test_3_merge(app: App):
|
||||
assert len(post.pics) == 5
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@flaky
|
||||
async def test_6_merge_only(app: App):
|
||||
from nonebot_bison.post import Post
|
||||
|
||||
@@ -87,7 +88,7 @@ async def test_6_merge_only(app: App):
|
||||
assert len(post.pics) == 1
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@flaky
|
||||
async def test_3_merge_only(app: App):
|
||||
from nonebot_bison.post import Post
|
||||
|
||||
|
||||
@@ -68,3 +68,4 @@ def fake_private_message_event(**field) -> "PrivateMessageEvent":
|
||||
from nonebot.adapters.onebot.v11.event import Sender
|
||||
|
||||
fake_admin_user = Sender(nickname="test", role="admin")
|
||||
fake_superuser = Sender(user_id=10001, nickname="superuser")
|
||||
|
||||
Reference in New Issue
Block a user