✨ Add support for all Field parameters from Pydantic 1.9.0 and above, make Pydantic 1.9.0 the minimum required version (#440)
Co-authored-by: Sebastián Ramírez <tiangolo@gmail.com>
This commit is contained in:
0
tests/test_pydantic/__init__.py
Normal file
0
tests/test_pydantic/__init__.py
Normal file
57
tests/test_pydantic/test_field.py
Normal file
57
tests/test_pydantic/test_field.py
Normal file
@@ -0,0 +1,57 @@
|
||||
from decimal import Decimal
|
||||
from typing import Optional, Union
|
||||
|
||||
import pytest
|
||||
from pydantic import ValidationError
|
||||
from sqlmodel import Field, SQLModel
|
||||
from typing_extensions import Literal
|
||||
|
||||
|
||||
def test_decimal():
|
||||
class Model(SQLModel):
|
||||
dec: Decimal = Field(max_digits=4, decimal_places=2)
|
||||
|
||||
Model(dec=Decimal("3.14"))
|
||||
Model(dec=Decimal("69.42"))
|
||||
|
||||
with pytest.raises(ValidationError):
|
||||
Model(dec=Decimal("3.142"))
|
||||
with pytest.raises(ValidationError):
|
||||
Model(dec=Decimal("0.069"))
|
||||
with pytest.raises(ValidationError):
|
||||
Model(dec=Decimal("420"))
|
||||
|
||||
|
||||
def test_discriminator():
|
||||
# Example adapted from
|
||||
# [Pydantic docs](https://pydantic-docs.helpmanual.io/usage/types/#discriminated-unions-aka-tagged-unions):
|
||||
|
||||
class Cat(SQLModel):
|
||||
pet_type: Literal["cat"]
|
||||
meows: int
|
||||
|
||||
class Dog(SQLModel):
|
||||
pet_type: Literal["dog"]
|
||||
barks: float
|
||||
|
||||
class Lizard(SQLModel):
|
||||
pet_type: Literal["reptile", "lizard"]
|
||||
scales: bool
|
||||
|
||||
class Model(SQLModel):
|
||||
pet: Union[Cat, Dog, Lizard] = Field(..., discriminator="pet_type")
|
||||
n: int
|
||||
|
||||
Model(pet={"pet_type": "dog", "barks": 3.14}, n=1) # type: ignore[arg-type]
|
||||
|
||||
with pytest.raises(ValidationError):
|
||||
Model(pet={"pet_type": "dog"}, n=1) # type: ignore[arg-type]
|
||||
|
||||
|
||||
def test_repr():
|
||||
class Model(SQLModel):
|
||||
id: Optional[int] = Field(primary_key=True)
|
||||
foo: str = Field(repr=False)
|
||||
|
||||
instance = Model(id=123, foo="bar")
|
||||
assert "foo=" not in repr(instance)
|
||||
Reference in New Issue
Block a user