File: test_get_field_value_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 (66 lines) | stat: -rw-r--r-- 2,237 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
from datetime import date, datetime, timedelta
from decimal import Decimal
from typing import FrozenSet, List, Set, Tuple, Type, Union, cast

import pytest
from typing_extensions import Annotated

from polyfactory.factories.base import BaseFactory
from polyfactory.field_meta import Constraints, FieldMeta


@pytest.mark.parametrize("t", (int, float, Decimal))
def test_numbers(t: Type[Union[int, float, Decimal]]) -> None:
    constraints: Constraints = {"ge": 1, "le": 20}
    field_meta = FieldMeta.from_type(annotation=t, name="foo", constraints=constraints)
    value = BaseFactory.get_field_value(field_meta)

    assert value >= constraints["ge"]
    assert value <= constraints["le"]


@pytest.mark.parametrize("t", (str, bytes))
def test_str_and_bytes(t: Type[Union[str, bytes]]) -> None:
    constraints: Constraints = {"min_length": 20, "max_length": 45}
    field_meta = FieldMeta.from_type(annotation=t, name="foo", constraints=constraints)
    value = BaseFactory.get_field_value(field_meta)

    assert len(value) >= constraints["min_length"]
    assert len(value) <= constraints["max_length"]


@pytest.mark.parametrize("t", (List[int], Set[int], Tuple[int], FrozenSet[int]))
def test_collections(t: Type[Union[Tuple, List, Set, FrozenSet]]) -> None:
    constraints: Constraints = {
        "min_length": 2,
        "max_length": 10,
    }
    field_meta = FieldMeta.from_type(annotation=t, name="foo", constraints=constraints)
    value = BaseFactory.get_field_value(field_meta)

    assert len(value) >= constraints["min_length"]
    assert len(value) <= constraints["max_length"]


def test_date() -> None:
    ge_date = datetime.now().date()
    le_date = ge_date + timedelta(days=10)
    constraints = {"ge": ge_date, "le": le_date}

    field_meta = FieldMeta.from_type(
        annotation=date,
        name="foo",
        constraints=cast(Constraints, constraints),
    )
    value = BaseFactory.get_field_value(field_meta)

    assert value >= ge_date
    assert value <= le_date


def test_constraints_parsing() -> None:
    constraints = Constraints(min_length=10)
    annotation = Annotated[str, constraints]
    field_meta = FieldMeta.from_type(annotation)

    assert field_meta.constraints == constraints