File: test_list_constraints.py

package info (click to toggle)
python-polyfactory 2.22.2-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,892 kB
  • sloc: python: 11,338; makefile: 103; sh: 37
file content (105 lines) | stat: -rw-r--r-- 3,246 bytes parent folder | download
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
97
98
99
100
101
102
103
104
105
import sys
from typing import Any, List

import pytest
from hypothesis import given
from hypothesis.strategies import integers

from pydantic import VERSION

from polyfactory.exceptions import ParameterException
from polyfactory.factories.pydantic_factory import ModelFactory, PydanticFieldMeta
from polyfactory.value_generators.constrained_collections import (
    handle_constrained_collection,
)


@given(
    integers(min_value=0, max_value=10),
    integers(min_value=0, max_value=10),
)
def test_handle_constrained_list_with_min_items_and_max_items(min_items: int, max_items: int) -> None:
    if max_items >= min_items:
        result = handle_constrained_collection(
            collection_type=list,
            factory=ModelFactory,
            field_meta=PydanticFieldMeta(name="test", annotation=list),
            item_type=str,
            max_items=max_items,
            min_items=min_items,
        )
        assert len(result) >= min_items
        assert len(result) <= max_items or 1
    else:
        with pytest.raises(ParameterException):
            handle_constrained_collection(
                collection_type=list,
                factory=ModelFactory,
                field_meta=PydanticFieldMeta(name="test", annotation=list),
                item_type=str,
                max_items=max_items,
                min_items=min_items,
            )


@given(
    integers(min_value=0, max_value=10),
)
def test_handle_constrained_list_with_max_items(
    max_items: int,
) -> None:
    result = handle_constrained_collection(
        collection_type=list,
        factory=ModelFactory,
        field_meta=PydanticFieldMeta(name="test", annotation=list),
        item_type=str,
        max_items=max_items,
    )
    assert len(result) <= max_items or 1


@given(
    integers(min_value=0, max_value=10),
)
def test_handle_constrained_list_with_min_items(
    min_items: int,
) -> None:
    result = handle_constrained_collection(
        collection_type=list,
        factory=ModelFactory,
        field_meta=PydanticFieldMeta.from_type(List[str], name="test"),
        item_type=str,
        min_items=min_items,
    )
    assert len(result) >= min_items


@pytest.mark.skipif(
    sys.version_info < (3, 9) and VERSION.startswith("2"),
    reason="fails on python 3.8 with pydantic v2",
)
@pytest.mark.parametrize("t_type", tuple(ModelFactory.get_provider_map()))
def test_handle_constrained_list_with_different_types(t_type: Any) -> None:
    field_meta = PydanticFieldMeta.from_type(List[t_type], name="test")
    result = handle_constrained_collection(
        collection_type=list,
        factory=ModelFactory,
        field_meta=field_meta.children[0],  # type: ignore[index]
        item_type=t_type,
    )
    assert len(result) >= 0


def test_handle_unique_items() -> None:
    field_meta = PydanticFieldMeta.from_type(List[str], name="test", constraints={"unique_items": True})
    result = handle_constrained_collection(
        collection_type=list,
        factory=ModelFactory,
        field_meta=field_meta.children[0],  # type: ignore[index]
        item_type=str,
        unique_items=True,
        min_items=2,
        max_items=2,
    )
    assert len(result) == 2
    assert len(set(result)) == 2