Coverage for tests/test_pydantic/test_field.py: 99%

73 statements  

« prev     ^ index     » next       coverage.py v7.17.2, created at 2026-11-16 00:02 +0000

1from decimal import Decimal 1ABCDEFGHIJKLM

2from typing import Annotated, Any, Literal 1ABCDEFGHIJKLM

3 

4import pytest 1ABCDEFGHIJKLM

5from pydantic import ValidationError 1ABCDEFGHIJKLM

6from sqlmodel import Discriminator, Field, SQLModel, Tag 1ABCDEFGHIJKLM

7 

8 

9def test_decimal(): 1ABCDEFGHIJKLM

10 class Model(SQLModel): 1NOPQRSTUVWXYZ

11 dec: Decimal = Field(max_digits=4, decimal_places=2) 1NOPQRSTUVWXYZ

12 

13 Model(dec=Decimal("3.14")) 1NOPQRSTUVWXYZ

14 Model(dec=Decimal("69.42")) 1NOPQRSTUVWXYZ

15 

16 with pytest.raises(ValidationError): 1NOPQRSTUVWXYZ

17 Model(dec=Decimal("3.142")) 1NOPQRSTUVWXYZ

18 with pytest.raises(ValidationError): 1NOPQRSTUVWXYZ

19 Model(dec=Decimal("0.069")) 1NOPQRSTUVWXYZ

20 with pytest.raises(ValidationError): 1NOPQRSTUVWXYZ

21 Model(dec=Decimal("420")) 1NOPQRSTUVWXYZ

22 

23 

24def test_discriminator(): 1ABCDEFGHIJKLM

25 # Example adapted from 

26 # [Pydantic docs](https://pydantic-docs.helpmanual.io/usage/types/#discriminated-unions-aka-tagged-unions): 

27 

28 class Cat(SQLModel): 1abcdefghijklm

29 pet_type: Literal["cat"] 1abcdefghijklm

30 meows: int 1abcdefghijklm

31 

32 class Dog(SQLModel): 1abcdefghijklm

33 pet_type: Literal["dog"] 1abcdefghijklm

34 barks: float 1abcdefghijklm

35 

36 class Lizard(SQLModel): 1abcdefghijklm

37 pet_type: Literal["reptile", "lizard"] 1abcdefghijklm

38 scales: bool 1abcdefghijklm

39 

40 class Model(SQLModel): 1abcdefghijklm

41 pet: Cat | Dog | Lizard = Field(..., discriminator="pet_type") 1abcdefghijklm

42 n: int 1abcdefghijklm

43 

44 Model(pet={"pet_type": "dog", "barks": 3.14}, n=1) # type: ignore[arg-type] 1abcdefghijklm

45 

46 with pytest.raises(ValidationError): 1abcdefghijklm

47 Model(pet={"pet_type": "dog"}, n=1) # type: ignore[arg-type] 1abcdefghijklm

48 

49 

50def test_discriminator_callable(): 1ABCDEFGHIJKLM

51 # Example adapted from 

52 # [Pydantic docs](https://docs.pydantic.dev/latest/concepts/unions/#discriminated-unions-with-callable-discriminator): 

53 

54 class Pie(SQLModel): 1nopqrstuvwxyz

55 pass 1nopqrstuvwxyz

56 

57 class ApplePie(Pie): 1nopqrstuvwxyz

58 fruit: Literal["apple"] = "apple" 1nopqrstuvwxyz

59 

60 class PumpkinPie(Pie): 1nopqrstuvwxyz

61 filling: Literal["pumpkin"] = "pumpkin" 1nopqrstuvwxyz

62 

63 def get_discriminator_value(v: Any) -> str: 1nopqrstuvwxyz

64 if isinstance(v, dict): 1nopqrstuvwxyz

65 return v.get("fruit", v.get("filling")) 1nopqrstuvwxyz

66 return getattr(v, "fruit", getattr(v, "filling", None)) 

67 

68 class ThanksgivingDinner(SQLModel): 1nopqrstuvwxyz

69 dessert: ( 1nopqrstuvwxyz

70 Annotated[ApplePie, Tag("apple")] | Annotated[PumpkinPie, Tag("pumpkin")] 

71 ) = Field( 

72 discriminator=Discriminator(get_discriminator_value), 

73 ) 

74 

75 apple_pie = ThanksgivingDinner.model_validate({"dessert": {"fruit": "apple"}}) 1nopqrstuvwxyz

76 assert isinstance(apple_pie.dessert, ApplePie) 1nopqrstuvwxyz

77 

78 pumpkin_pie = ThanksgivingDinner.model_validate({"dessert": {"filling": "pumpkin"}}) 1nopqrstuvwxyz

79 assert isinstance(pumpkin_pie.dessert, PumpkinPie) 1nopqrstuvwxyz

80 

81 

82def test_repr(): 1ABCDEFGHIJKLM

83 class Model(SQLModel): 2? @ [ ] ^ _ ` { | } ~ abbb

84 id: int | None = Field(primary_key=True) 2? @ [ ] ^ _ ` { | } ~ abbb

85 foo: str = Field(repr=False) 2? @ [ ] ^ _ ` { | } ~ abbb

86 

87 instance = Model(id=123, foo="bar") 2? @ [ ] ^ _ ` { | } ~ abbb

88 assert "foo=" not in repr(instance) 2? @ [ ] ^ _ ` { | } ~ abbb

89 

90 

91def test_min_items(): 1ABCDEFGHIJKLM

92 with pytest.warns( 10123456789!#$

93 DeprecationWarning, 

94 match="`min_items` is deprecated and will be removed, use `min_length` instead", 

95 ): 

96 

97 class Model(SQLModel): 10123456789!#$

98 items: list[int] = Field(min_items=2) 10123456789!#$

99 

100 Model(items=[1, 2]) 10123456789!#$

101 

102 with pytest.raises(ValidationError) as exc_info: 10123456789!#$

103 Model(items=[1]) 10123456789!#$

104 assert len(exc_info.value.errors()) == 1 10123456789!#$

105 assert exc_info.value.errors()[0]["type"] == "too_short" 10123456789!#$

106 

107 

108def test_max_items(): 1ABCDEFGHIJKLM

109 with pytest.warns( 1%'()*+,-./:;=

110 DeprecationWarning, 

111 match="`max_items` is deprecated and will be removed, use `max_length` instead", 

112 ): 

113 

114 class Model(SQLModel): 1%'()*+,-./:;=

115 items: list[int] = Field(max_items=2) 1%'()*+,-./:;=

116 

117 Model(items=[1, 2]) 1%'()*+,-./:;=

118 

119 with pytest.raises(ValidationError) as exc_info: 1%'()*+,-./:;=

120 Model(items=[1, 2, 3]) 1%'()*+,-./:;=

121 assert len(exc_info.value.errors()) == 1 1%'()*+,-./:;=

122 assert exc_info.value.errors()[0]["type"] == "too_long" 1%'()*+,-./:;=