Feature: 添加插件 Pydantic 相关使用方法 (#2563)

This commit is contained in:
Ju4tCode
2024-02-05 14:00:49 +08:00
committed by GitHub
parent 2ebf956599
commit dace63d9d2
10 changed files with 93 additions and 19 deletions

View File

@ -13,3 +13,4 @@ NESTED_MISSING_DICT__A=1
NESTED_MISSING_DICT__B__C=2
NOT_NESTED=some string
NOT_NESTED__A=1
PLUGIN_CONFIG=1

View File

@ -2,12 +2,14 @@ from typing import Any
from dataclasses import dataclass
import pytest
from pydantic import BaseModel
from nonebot.compat import (
DEFAULT_CONFIG,
Required,
FieldInfo,
PydanticUndefined,
model_dump,
custom_validation,
type_validate_python,
)
@ -28,6 +30,16 @@ async def test_field_info():
assert FieldInfo(test="test").extra["test"] == "test"
@pytest.mark.asyncio
async def test_model_dump():
class TestModel(BaseModel):
test1: int
test2: int
assert model_dump(TestModel(test1=1, test2=2), include={"test1"}) == {"test1": 1}
assert model_dump(TestModel(test1=1, test2=2), exclude={"test1"}) == {"test2": 2}
@pytest.mark.asyncio
async def test_custom_validation():
called = []

View File

@ -1,4 +1,5 @@
import pytest
from pydantic import BaseModel
import nonebot
from nonebot.plugin import PluginManager, _managers
@ -35,3 +36,14 @@ async def test_get_available_plugin():
finally:
_managers.clear()
_managers.extend(old_managers)
@pytest.mark.asyncio
async def test_get_plugin_config():
class Config(BaseModel):
plugin_config: int
# check get plugin config
config = nonebot.get_plugin_config(Config)
assert isinstance(config, Config)
assert config.plugin_config == 1