-
-
Notifications
You must be signed in to change notification settings - Fork 712
/
Copy pathtest_validation.py
96 lines (69 loc) · 2.77 KB
/
test_validation.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
from typing import Optional
import pytest
from pydantic.error_wrappers import ValidationError
from sqlmodel import Field, SQLModel
from .conftest import needs_pydanticv1, needs_pydanticv2
@needs_pydanticv1
def test_validation_pydantic_v1(clear_sqlmodel):
"""Test validation of implicit and explicit None values.
# For consistency with pydantic, validators are not to be called on
# arguments that are not explicitly provided.
https://github.com/tiangolo/sqlmodel/issues/230
https://github.com/samuelcolvin/pydantic/issues/1223
"""
from pydantic import validator
class Hero(SQLModel):
name: Optional[str] = None
secret_name: Optional[str] = None
age: Optional[int] = None
@validator("name", "secret_name", "age")
def reject_none(cls, v):
assert v is not None
return v
Hero.validate({"age": 25})
with pytest.raises(ValidationError):
Hero.validate({"name": None, "age": 25})
@needs_pydanticv2
def test_validation_pydantic_v2(clear_sqlmodel):
"""Test validation of implicit and explicit None values.
# For consistency with pydantic, validators are not to be called on
# arguments that are not explicitly provided.
https://github.com/tiangolo/sqlmodel/issues/230
https://github.com/samuelcolvin/pydantic/issues/1223
"""
from pydantic import field_validator
class Hero(SQLModel):
name: Optional[str] = None
secret_name: Optional[str] = None
age: Optional[int] = None
@field_validator("name", "secret_name", "age")
def reject_none(cls, v):
assert v is not None
return v
Hero.model_validate({"age": 25})
with pytest.raises(ValidationError):
Hero.model_validate({"name": None, "age": 25})
@needs_pydanticv2
def test_validation_with_table_true():
"""Test validation with table=True."""
from pydantic import field_validator
class Hero(SQLModel, table=True):
name: Optional[str] = Field(default=None, primary_key=True)
secret_name: Optional[str] = None
age: Optional[int] = None
@field_validator("age", mode="after")
@classmethod
def double_age(cls, v):
if v is not None:
return v * 2
return v
Hero(name="Deadpond", age=25)
Hero.model_validate({"name": "Deadpond", "age": 25})
with pytest.raises(ValidationError):
Hero(name="Deadpond", secret_name="Dive Wilson", age="test")
with pytest.raises(ValidationError):
Hero.model_validate({"name": "Deadpond", "age": "test"})
double_age_hero = Hero(name="Deadpond", age=25)
assert double_age_hero.age == 50
double_age_hero = Hero.model_validate({"name": "Deadpond", "age": 25})
assert double_age_hero.age == 50