Feature: 支持子依赖定义 Pydantic 类型校验 (#2310)

This commit is contained in:
Ju4tCode
2023-08-29 18:45:12 +08:00
committed by GitHub
parent 79f833b946
commit f59271bd47
6 changed files with 244 additions and 25 deletions

View File

@ -42,9 +42,14 @@ async def test_depend(app: App):
ClassDependency,
runned,
depends,
validate,
class_depend,
test_depends,
validate_fail,
validate_field,
annotated_depend,
sub_type_mismatch,
validate_field_fail,
annotated_class_depend,
annotated_prior_depend,
)
@ -62,8 +67,7 @@ async def test_depend(app: App):
event_next = make_fake_event()()
ctx.receive_event(bot, event_next)
assert len(runned) == 2
assert runned[0] == runned[1] == 1
assert runned == [1, 1]
runned.clear()
@ -84,6 +88,29 @@ async def test_depend(app: App):
) as ctx:
ctx.should_return(ClassDependency(x=1, y=2))
with pytest.raises(TypeMisMatch): # noqa: PT012
async with app.test_dependent(
sub_type_mismatch, allow_types=[DependParam, BotParam]
) as ctx:
bot = ctx.create_bot()
ctx.pass_params(bot=bot)
async with app.test_dependent(validate, allow_types=[DependParam]) as ctx:
ctx.should_return(1)
with pytest.raises(TypeMisMatch):
async with app.test_dependent(validate_fail, allow_types=[DependParam]) as ctx:
...
async with app.test_dependent(validate_field, allow_types=[DependParam]) as ctx:
ctx.should_return(1)
with pytest.raises(TypeMisMatch):
async with app.test_dependent(
validate_field_fail, allow_types=[DependParam]
) as ctx:
...
@pytest.mark.asyncio
async def test_bot(app: App):