From d5e272ab896fad7cab50fa09668650151ad73981 Mon Sep 17 00:00:00 2001 From: yanyongyu <42488585+yanyongyu@users.noreply.github.com> Date: Sun, 20 Mar 2022 15:09:53 +0800 Subject: [PATCH] :white_check_mark: add success tests --- tests/test_param.py | 13 ++++++++++++- tests/utils.py | 3 ++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/tests/test_param.py b/tests/test_param.py index 4589c1670a9e..52521a41cd9b 100644 --- a/tests/test_param.py +++ b/tests/test_param.py @@ -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) diff --git a/tests/utils.py b/tests/utils.py index 31939be7eb2e..d0715e0ebf59 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -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: