🐛 fix event maybe converted when checking type (#876)

Fix: 修复 event 类型检查会对类型进行自动转换
This commit is contained in:
Ju4tCode
2022-03-20 19:40:43 +08:00
committed by GitHub
parent fcdb05a7e2
commit 45e2e6c280
6 changed files with 53 additions and 20 deletions

View File

@ -36,19 +36,33 @@ async def test_depend(app: App, load_plugin):
@pytest.mark.asyncio
async def test_bot(app: App, load_plugin):
from nonebot.params import BotParam
from plugins.param.param_bot import get_bot
from nonebot.exception import TypeMisMatch
from plugins.param.param_bot import SubBot, get_bot, sub_bot
async with app.test_dependent(get_bot, allow_types=[BotParam]) as ctx:
bot = ctx.create_bot()
ctx.pass_params(bot=bot)
ctx.should_return(bot)
async with app.test_dependent(sub_bot, allow_types=[BotParam]) as ctx:
bot = ctx.create_bot(base=SubBot)
ctx.pass_params(bot=bot)
ctx.should_return(bot)
with pytest.raises(TypeMisMatch):
async with app.test_dependent(sub_bot, allow_types=[BotParam]) as ctx:
bot = ctx.create_bot()
ctx.pass_params(bot=bot)
@pytest.mark.asyncio
async def test_event(app: App, load_plugin):
from nonebot.exception import TypeMisMatch
from nonebot.params import EventParam, DependParam
from plugins.param.param_event import (
SubEvent,
event,
sub_event,
event_type,
event_to_me,
event_message,
@ -57,11 +71,20 @@ async def test_event(app: App, load_plugin):
fake_message = make_fake_message()("text")
fake_event = make_fake_event(_message=fake_message)()
fake_subevent = make_fake_event(_base=SubEvent)()
async with app.test_dependent(event, allow_types=[EventParam]) as ctx:
ctx.pass_params(event=fake_event)
ctx.should_return(fake_event)
async with app.test_dependent(sub_event, allow_types=[EventParam]) as ctx:
ctx.pass_params(event=fake_subevent)
ctx.should_return(fake_subevent)
with pytest.raises(TypeMisMatch):
async with app.test_dependent(sub_event, allow_types=[EventParam]) as ctx:
ctx.pass_params(event=fake_event)
async with app.test_dependent(
event_type, allow_types=[EventParam, DependParam]
) as ctx: