mirror of
https://github.com/RYGhub/royalnet.git
synced 2024-11-26 21:14:19 +00:00
✨ Implement params tests
This commit is contained in:
parent
002b51fefe
commit
b8607edb56
2 changed files with 15 additions and 12 deletions
|
@ -31,7 +31,7 @@ def parameter_to_field(param: inspect.Parameter, **kwargs) -> Tuple[type, pydant
|
|||
return (
|
||||
param.annotation,
|
||||
pydantic.Field(
|
||||
default=param.default if param.default else None,
|
||||
default=param.default if param.default is not inspect.Parameter.empty else ...,
|
||||
title=param.name,
|
||||
**kwargs,
|
||||
),
|
||||
|
@ -52,12 +52,12 @@ def signature_to_model(f: Callable, __config__: pydantic.BaseConfig = ModelConfi
|
|||
name: str = f.__name__
|
||||
signature: inspect.Signature = inspect.signature(f)
|
||||
|
||||
params = {key: parameter_to_field(value) for key, value in signature.parameters if not key.startswith("_")}
|
||||
params = {key: parameter_to_field(value) for key, value in signature.parameters.items() if not key.startswith("_")}
|
||||
|
||||
model: Type[pydantic.BaseModel] = pydantic.create_model(name,
|
||||
__config__=ModelConfig,
|
||||
**params,
|
||||
**extra_params)
|
||||
model = pydantic.create_model(name,
|
||||
__config__=ModelConfig,
|
||||
**params,
|
||||
**extra_params)
|
||||
return model
|
||||
|
||||
|
||||
|
|
|
@ -7,7 +7,7 @@ import royalnet.engineer as re
|
|||
|
||||
@pytest.fixture
|
||||
def a_random_function():
|
||||
def f(big_f: str, _hidden: int) -> str:
|
||||
def f(*, big_f: str, _hidden: int) -> str:
|
||||
return big_f
|
||||
return f
|
||||
|
||||
|
@ -15,9 +15,9 @@ def a_random_function():
|
|||
def test_parameter_to_field(a_random_function):
|
||||
signature = inspect.signature(a_random_function)
|
||||
parameter = signature.parameters["big_f"]
|
||||
fieldinfo = re.parameter_to_field(parameter)
|
||||
t, fieldinfo = re.parameter_to_field(parameter)
|
||||
assert isinstance(fieldinfo, pydantic.fields.FieldInfo)
|
||||
assert fieldinfo.default == parameter.default == str
|
||||
assert fieldinfo.default is ...
|
||||
assert fieldinfo.title == parameter.name == "big_f"
|
||||
|
||||
|
||||
|
@ -33,8 +33,11 @@ def test_signature_to_model(a_random_function):
|
|||
with pytest.raises(pydantic.ValidationError):
|
||||
Model()
|
||||
|
||||
with pytest.raises(pydantic.ValidationError):
|
||||
Model(big_f="exists", _hidden="no")
|
||||
model = Model(big_f="exists", _hidden="no")
|
||||
assert isinstance(model, pydantic.BaseModel)
|
||||
assert model.big_f == "exists"
|
||||
with pytest.raises(AttributeError):
|
||||
model._hidden
|
||||
|
||||
with pytest.raises(pydantic.ValidationError):
|
||||
Model(big_f=1)
|
||||
Model(big_f=...)
|
||||
|
|
Loading…
Reference in a new issue