Coverage for tests / test_pydantic / test_field.py: 100%
38 statements
« prev ^ index » next coverage.py v7.13.0, created at 2026-06-10 09:40 +0000
« prev ^ index » next coverage.py v7.13.0, created at 2026-06-10 09:40 +0000
1from decimal import Decimal 1qrstuvwx
2from typing import Literal 1qrstuvwx
4import pytest 1qrstuvwx
5from pydantic import ValidationError 1qrstuvwx
6from sqlmodel import Field, SQLModel 1qrstuvwx
9def test_decimal(): 1qrstuvwx
10 class Model(SQLModel): 1ijklmnop
11 dec: Decimal = Field(max_digits=4, decimal_places=2) 1ijklmnop
13 Model(dec=Decimal("3.14")) 1ijklmnop
14 Model(dec=Decimal("69.42")) 1ijklmnop
16 with pytest.raises(ValidationError): 1ijklmnop
17 Model(dec=Decimal("3.142")) 1ijklmnop
18 with pytest.raises(ValidationError): 1ijklmnop
19 Model(dec=Decimal("0.069")) 1ijklmnop
20 with pytest.raises(ValidationError): 1ijklmnop
21 Model(dec=Decimal("420")) 1ijklmnop
24def test_discriminator(): 1qrstuvwx
25 # Example adapted from
26 # [Pydantic docs](https://pydantic-docs.helpmanual.io/usage/types/#discriminated-unions-aka-tagged-unions):
28 class Cat(SQLModel): 1abcdefgh
29 pet_type: Literal["cat"] 1abcdefgh
30 meows: int 1abcdefgh
32 class Dog(SQLModel): 1abcdefgh
33 pet_type: Literal["dog"] 1abcdefgh
34 barks: float 1abcdefgh
36 class Lizard(SQLModel): 1abcdefgh
37 pet_type: Literal["reptile", "lizard"] 1abcdefgh
38 scales: bool 1abcdefgh
40 class Model(SQLModel): 1abcdefgh
41 pet: Cat | Dog | Lizard = Field(..., discriminator="pet_type") 1abcdefgh
42 n: int 1abcdefgh
44 Model(pet={"pet_type": "dog", "barks": 3.14}, n=1) # type: ignore[arg-type] 1abcdefgh
46 with pytest.raises(ValidationError): 1abcdefgh
47 Model(pet={"pet_type": "dog"}, n=1) # type: ignore[arg-type] 1abcdefgh
50def test_repr(): 1qrstuvwx
51 class Model(SQLModel): 1yzABCDEF
52 id: int | None = Field(primary_key=True) 1yzABCDEF
53 foo: str = Field(repr=False) 1yzABCDEF
55 instance = Model(id=123, foo="bar") 1yzABCDEF
56 assert "foo=" not in repr(instance) 1yzABCDEF