Skip to content

Commit

Permalink
✅ add success tests
Browse files Browse the repository at this point in the history
  • Loading branch information
yanyongyu committed Mar 20, 2022
1 parent 0f0f485 commit d5e272a
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
13 changes: 12 additions & 1 deletion tests/test_param.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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,
Expand All @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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:
Expand Down

0 comments on commit d5e272a

Please sign in to comment.