File: test_frozen_set_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 (84 lines) | stat: -rw-r--r-- 2,661 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
from contextlib import suppress
from typing import Any

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

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_set_with_min_items_and_max_items(min_items: int, max_items: int) -> None:
    if max_items >= min_items:
        result = handle_constrained_collection(
            collection_type=frozenset,
            factory=ModelFactory,
            field_meta=PydanticFieldMeta(name="test", annotation=frozenset),
            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=frozenset,
                factory=ModelFactory,
                field_meta=PydanticFieldMeta(name="test", annotation=frozenset),
                item_type=str,
                max_items=max_items,
                min_items=min_items,
            )


@given(
    integers(min_value=0, max_value=10),
)
def test_handle_constrained_set_with_max_items(
    max_items: int,
) -> None:
    result = handle_constrained_collection(
        collection_type=frozenset,
        factory=ModelFactory,
        field_meta=PydanticFieldMeta(name="test", annotation=frozenset),
        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_set_with_min_items(
    min_items: int,
) -> None:
    result = handle_constrained_collection(
        collection_type=frozenset,
        factory=ModelFactory,
        field_meta=PydanticFieldMeta(name="test", annotation=frozenset),
        item_type=str,
        min_items=min_items,
    )
    assert len(result) >= min_items


@pytest.mark.parametrize("t_type", tuple(ModelFactory.get_provider_map()))
def test_handle_constrained_set_with_different_types(t_type: Any) -> None:
    with suppress(ParameterException):
        result = handle_constrained_collection(
            collection_type=frozenset,
            factory=ModelFactory,
            field_meta=PydanticFieldMeta(name="test", annotation=frozenset),
            item_type=t_type,
        )
        assert len(result) >= 0