Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: 修复 event 类型检查会对类型进行自动转换 #876

Merged
merged 2 commits into from
Mar 20, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
✅ add success tests
yanyongyu committed Mar 20, 2022

Unverified

This user has not yet uploaded their public signing key.
commit d5e272ab896fad7cab50fa09668650151ad73981
13 changes: 12 additions & 1 deletion tests/test_param.py
Original file line number Diff line number Diff line change
@@ -37,13 +37,18 @@ async def test_depend(app: App, load_plugin):
async def test_bot(app: App, load_plugin):
from nonebot.params import BotParam
from nonebot.exception import TypeMisMatch
from plugins.param.param_bot import get_bot, sub_bot
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()
@@ -55,6 +60,7 @@ 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,
@@ -65,11 +71,16 @@ 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)
3 changes: 2 additions & 1 deletion tests/utils.py
Original file line number Diff line number Diff line change
@@ -61,6 +61,7 @@ def __add__(self, other):


def make_fake_event(
_base: Optional[Type["Event"]] = None,
_type: str = "message",
_name: str = "test",
_description: str = "test",
@@ -72,7 +73,7 @@ def make_fake_event(
) -> Type["Event"]:
from nonebot.adapters import Event

_Fake = create_model("_Fake", __base__=Event, **fields)
_Fake = create_model("_Fake", __base__=_base or Event, **fields)

class FakeEvent(_Fake):
def get_type(self) -> str: