File: test_byte_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 (81 lines) | stat: -rw-r--r-- 2,608 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
from random import Random

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

from polyfactory.exceptions import ParameterException
from polyfactory.value_generators.constrained_strings import handle_constrained_string_or_bytes


@given(booleans(), integers(max_value=10000), integers(max_value=10000))
def test_handle_constrained_bytes_with_min_length_and_max_length(
    to_lower: bool,
    min_length: int,
    max_length: int,
) -> None:
    if min_length < 0 or max_length < 0 or min_length > max_length:
        with pytest.raises(ParameterException):
            handle_constrained_string_or_bytes(
                random=Random(),
                t_type=bytes,
                min_length=min_length,
                max_length=max_length,
                pattern=None,
            )
    else:
        result = handle_constrained_string_or_bytes(
            random=Random(),
            t_type=bytes,
            min_length=min_length,
            max_length=max_length,
            pattern=None,
        )
        if to_lower:
            assert result == result.lower()
        assert len(result) >= min_length
        assert len(result) <= max_length


@given(booleans(), integers(max_value=10000))
def test_handle_constrained_bytes_with_min_length(to_lower: bool, min_length: int) -> None:
    if min_length < 0:
        with pytest.raises(ParameterException):
            handle_constrained_string_or_bytes(
                random=Random(),
                t_type=bytes,
                min_length=min_length,
                pattern=None,
            )
    else:
        result = handle_constrained_string_or_bytes(
            random=Random(),
            t_type=bytes,
            min_length=min_length,
            pattern=None,
        )
        if to_lower:
            assert result == result.lower()
        assert len(result) >= min_length


@given(booleans(), integers(max_value=10000))
def test_handle_constrained_bytes_with_max_length(to_lower: bool, max_length: int) -> None:
    if max_length < 0:
        with pytest.raises(ParameterException):
            handle_constrained_string_or_bytes(
                random=Random(),
                t_type=bytes,
                max_length=max_length,
                pattern=None,
            )
    else:
        result = handle_constrained_string_or_bytes(
            random=Random(),
            t_type=bytes,
            max_length=max_length,
            pattern=None,
        )
        if to_lower:
            assert result == result.lower()
        assert len(result) <= max_length