add got receive tests

This commit is contained in:
yanyongyu
2021-12-21 00:39:12 +08:00
parent d549cdf26a
commit e9b8515cf1
7 changed files with 111 additions and 52 deletions

View File

@ -1,2 +1,3 @@
DEBUG=true
NICKNAME=["test"]
CONFIG_FROM_ENV=

35
tests/plugins/matcher.py Normal file
View File

@ -0,0 +1,35 @@
from nonebot import on_message
from nonebot.adapters import Event
from nonebot.params import ArgStr, Received, LastReceived
test_handle = on_message()
@test_handle.handle()
async def handle():
await test_handle.finish("send", at_sender=True)
test_got = on_message()
@test_got.got("key1", "prompt key1")
@test_got.got("key2", "prompt key2")
async def got(key1: str = ArgStr(), key2: str = ArgStr()):
assert key1 == "text"
assert key2 == "text"
await test_got.reject("reject", at_sender=True)
test_receive = on_message()
@test_receive.receive()
@test_receive.receive("receive")
async def receive(
x: Event = Received("receive"), y: Event = LastReceived(), z: Event = Received()
):
assert str(x.get_message()) == "text"
assert str(z.get_message()) == "text"
assert x is y
await test_receive.pause("pause", at_sender=True)

41
tests/test_matcher.py Normal file
View File

@ -0,0 +1,41 @@
from typing import TYPE_CHECKING, Set
import pytest
from nonebug import App
from utils import load_plugin, make_fake_event, make_fake_message
@pytest.mark.asyncio
async def test_matcher(app: App, load_plugin):
from plugins.matcher import test_got, test_handle, test_receive
message = make_fake_message()("text")
event = make_fake_event(_message=message)()
assert len(test_handle.handlers) == 1
async with app.test_matcher(test_handle) as ctx:
bot = ctx.create_bot()
ctx.receive_event(bot, event)
ctx.should_call_send(event, "send", "result", at_sender=True)
ctx.should_finished()
assert len(test_got.handlers) == 1
async with app.test_matcher(test_got) as ctx:
bot = ctx.create_bot()
ctx.receive_event(bot, event)
ctx.should_call_send(event, "prompt key1", "result1")
ctx.receive_event(bot, event)
ctx.should_call_send(event, "prompt key2", "result2")
ctx.receive_event(bot, event)
ctx.should_call_send(event, "reject", "result3", at_sender=True)
ctx.should_rejected()
assert len(test_receive.handlers) == 1
async with app.test_matcher(test_receive) as ctx:
bot = ctx.create_bot()
ctx.receive_event(bot, event)
ctx.receive_event(bot, event)
ctx.receive_event(bot, event)
ctx.should_call_send(event, "pause", "result", at_sender=True)
ctx.should_paused()

View File

@ -196,7 +196,7 @@ async def test_matcher(app: App, load_plugin):
event = make_fake_event()()
fake_matcher.set_receive("test", event)
event_next = make_fake_event()()
fake_matcher.set_receive(None, event_next)
fake_matcher.set_receive("", event_next)
async with app.test_dependent(
receive, allow_types=[MatcherParam, DependParam]