add more depend test case

This commit is contained in:
yanyongyu 2021-12-20 14:50:11 +08:00
parent 0d24a79840
commit c49059f9d3
2 changed files with 30 additions and 2 deletions

View File

@ -1,5 +1,6 @@
from dataclasses import dataclass
from nonebot import on_message from nonebot import on_message
from nonebot.adapters import Event
from nonebot.params import Depends from nonebot.params import Depends
test_depends = on_message() test_depends = on_message()
@ -17,6 +18,20 @@ def parameterless():
runned.append(1) runned.append(1)
def gen_sync():
yield 1
async def gen_async():
yield 2
@dataclass
class ClassDependency:
x: int = Depends(gen_sync)
y: int = Depends(gen_async)
# test parameterless # test parameterless
@test_depends.handle(parameterless=[Depends(parameterless)]) @test_depends.handle(parameterless=[Depends(parameterless)])
async def depends(x: int = Depends(dependency)): async def depends(x: int = Depends(dependency)):
@ -28,3 +43,7 @@ async def depends(x: int = Depends(dependency)):
async def depends_cache(y: int = Depends(dependency, use_cache=True)): async def depends_cache(y: int = Depends(dependency, use_cache=True)):
# test cache # test cache
return y return y
async def class_depend(c: ClassDependency = Depends()):
return c

View File

@ -8,7 +8,13 @@ from utils import load_plugin, make_fake_event, make_fake_message
async def test_depend(app: App, load_plugin): async def test_depend(app: App, load_plugin):
from nonebot.params import DependParam from nonebot.params import DependParam
from plugins.param.param_depend import runned, depends, test_depends from plugins.param.param_depend import (
ClassDependency,
runned,
depends,
class_depend,
test_depends,
)
async with app.test_dependent(depends, allow_types=[DependParam]) as ctx: async with app.test_dependent(depends, allow_types=[DependParam]) as ctx:
ctx.should_return(1) ctx.should_return(1)
@ -24,6 +30,9 @@ async def test_depend(app: App, load_plugin):
assert len(runned) == 2 and runned[0] == runned[1] == 1 assert len(runned) == 2 and runned[0] == runned[1] == 1
async with app.test_dependent(class_depend, allow_types=[DependParam]) as ctx:
ctx.should_return(ClassDependency(x=1, y=2))
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_bot(app: App, load_plugin): async def test_bot(app: App, load_plugin):