Coverage for tests/test_validation.py: 100%
33 statements
« prev ^ index » next coverage.py v7.7.1, created at 2025-03-24 00:02 +0000
« prev ^ index » next coverage.py v7.7.1, created at 2025-03-24 00:02 +0000
1from typing import Optional 1mnopqr
3import pytest 1mnopqr
4from pydantic.error_wrappers import ValidationError 1mnopqr
5from sqlmodel import SQLModel 1mnopqr
7from .conftest import needs_pydanticv1, needs_pydanticv2 1mnopqr
10@needs_pydanticv1 1mnopqr
11def test_validation_pydantic_v1(clear_sqlmodel): 1mnopqr
12 """Test validation of implicit and explicit None values.
14 # For consistency with pydantic, validators are not to be called on
15 # arguments that are not explicitly provided.
17 https://github.com/tiangolo/sqlmodel/issues/230
18 https://github.com/samuelcolvin/pydantic/issues/1223
20 """
21 from pydantic import validator 1abcdef
23 class Hero(SQLModel): 1abcdef
24 name: Optional[str] = None 1abcdef
25 secret_name: Optional[str] = None 1abcdef
26 age: Optional[int] = None 1abcdef
28 @validator("name", "secret_name", "age") 1abcdef
29 def reject_none(cls, v): 1abcdef
30 assert v is not None 1abcdef
31 return v 1abcdef
33 Hero.validate({"age": 25}) 1abcdef
35 with pytest.raises(ValidationError): 1abcdef
36 Hero.validate({"name": None, "age": 25}) 1abcdef
39@needs_pydanticv2 1mnopqr
40def test_validation_pydantic_v2(clear_sqlmodel): 1mnopqr
41 """Test validation of implicit and explicit None values.
43 # For consistency with pydantic, validators are not to be called on
44 # arguments that are not explicitly provided.
46 https://github.com/tiangolo/sqlmodel/issues/230
47 https://github.com/samuelcolvin/pydantic/issues/1223
49 """
50 from pydantic import field_validator 1ghijkl
52 class Hero(SQLModel): 1ghijkl
53 name: Optional[str] = None 1ghijkl
54 secret_name: Optional[str] = None 1ghijkl
55 age: Optional[int] = None 1ghijkl
57 @field_validator("name", "secret_name", "age") 1ghijkl
58 def reject_none(cls, v): 1ghijkl
59 assert v is not None 1ghijkl
60 return v 1ghijkl
62 Hero.model_validate({"age": 25}) 1ghijkl
64 with pytest.raises(ValidationError): 1ghijkl
65 Hero.model_validate({"name": None, "age": 25}) 1ghijkl