|
12 | 12 | from uuid import UUID |
13 | 13 |
|
14 | 14 | import pytest |
15 | | -from annotated_types import Ge, Gt, Le, LowerCase, MinLen, UpperCase |
| 15 | +from annotated_types import Ge, Gt, Le, LowerCase, MaxLen, MinLen, UpperCase |
16 | 16 | from typing_extensions import Annotated, TypeAlias |
17 | 17 |
|
18 | 18 | import pydantic |
@@ -717,18 +717,49 @@ class C(BaseModel): |
717 | 717 | assert CFactory.build() |
718 | 718 |
|
719 | 719 |
|
720 | | -def test_constrained_union_types() -> None: |
| 720 | +@pytest.mark.skipif(IS_PYDANTIC_V2, reason="pydantic 1 only test") |
| 721 | +def test_constrained_union_types_pydantic_v1() -> None: |
721 | 722 | class A(BaseModel): |
722 | 723 | a: Union[Annotated[List[str], MinLen(100)], Annotated[int, Ge(1000)]] |
723 | 724 | b: Union[List[Annotated[str, MinLen(100)]], int] |
724 | 725 | c: Union[Annotated[List[int], MinLen(100)], None] |
725 | | - d: Union[Annotated[List[int], MinLen(100)], Annotated[List[str], MinLen(100)]] |
726 | | - e: Optional[Union[Annotated[List[int], MinLen(10)], Annotated[List[str], MinLen(10)]]] |
| 726 | + d: Union[Annotated[List[int], MinLen(100)], Annotated[List[str], MaxLen(99)]] |
| 727 | + e: Optional[Union[Annotated[List[int], MinLen(10)], Annotated[List[str], MaxLen(9)]]] |
727 | 728 | f: Optional[Union[Annotated[List[int], MinLen(10)], List[str]]] |
| 729 | + g: Optional[ |
| 730 | + Union[ |
| 731 | + Annotated[List[int], MinLen(10)], |
| 732 | + Union[Annotated[List[str], MaxLen(9)], Annotated[Decimal, Field(max_digits=4, decimal_places=2)]], |
| 733 | + ] |
| 734 | + ] |
| 735 | + |
| 736 | + AFactory = ModelFactory.create_factory(A, __allow_none_optionals__=False) |
| 737 | + |
| 738 | + assert AFactory.build() |
| 739 | + |
| 740 | + |
| 741 | +@pytest.mark.skipif(IS_PYDANTIC_V1, reason="pydantic 2 only test") |
| 742 | +def test_constrained_union_types_pydantic_v2() -> None: |
| 743 | + class A(BaseModel): |
| 744 | + a: Union[Annotated[List[str], MinLen(100)], Annotated[int, Ge(1000)]] |
| 745 | + b: Union[List[Annotated[str, MinLen(100)]], int] |
| 746 | + c: Union[Annotated[List[int], MinLen(100)], None] |
| 747 | + d: Union[Annotated[List[int], MinLen(100)], Annotated[List[str], MaxLen(99)]] |
| 748 | + e: Optional[Union[Annotated[List[int], MinLen(10)], Annotated[List[str], MaxLen(9)]]] |
| 749 | + f: Optional[Union[Annotated[List[int], MinLen(10)], List[str]]] |
| 750 | + g: Optional[ |
| 751 | + Union[ |
| 752 | + Annotated[List[int], MinLen(10)], |
| 753 | + Union[Annotated[List[str], MaxLen(9)], Annotated[Decimal, Field(max_digits=4, decimal_places=2)]], |
| 754 | + ] |
| 755 | + ] |
| 756 | + # This annotation is not allowed in pydantic 1 |
| 757 | + h: Annotated[Union[List[int], List[str]], MinLen(10)] |
728 | 758 |
|
729 | 759 | AFactory = ModelFactory.create_factory(A, __allow_none_optionals__=False) |
730 | 760 |
|
731 | 761 | assert AFactory.build() |
| 762 | + assert list(AFactory.coverage()) |
732 | 763 |
|
733 | 764 |
|
734 | 765 | @pytest.mark.parametrize("allow_none", (True, False)) |
|
0 commit comments