File: test_number_range.py

package info (click to toggle)
wtforms 3.2.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,064 kB
  • sloc: python: 5,264; makefile: 27; sh: 17
file content (47 lines) | stat: -rw-r--r-- 1,298 bytes parent folder | download | duplicates (2)
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
import decimal

import pytest

from wtforms.validators import NumberRange
from wtforms.validators import ValidationError


@pytest.mark.parametrize(
    "min_v, max_v, test_v", [(5, 10, 7), (5, None, 7), (None, 100, 70)]
)
def test_number_range_passes(min_v, max_v, test_v, dummy_form, dummy_field):
    """
    It should pass if the test_v is between min_v and max_v
    """
    dummy_field.data = test_v
    validator = NumberRange(min_v, max_v)
    validator(dummy_form, dummy_field)


@pytest.mark.parametrize(
    "min_v, max_v, test_v",
    [
        (5, 10, None),
        (5, 10, 0),
        (5, 10, 12),
        (5, 10, -5),
        (5, None, 4),
        (None, 100, 500),
    ],
)
def test_number_range_raises(min_v, max_v, test_v, dummy_form, dummy_field):
    """
    It should raise ValidationError if the test_v is not between min_v and max_v
    """
    dummy_field.data = test_v
    validator = NumberRange(min_v, max_v)
    with pytest.raises(ValidationError):
        validator(dummy_form, dummy_field)


@pytest.mark.parametrize("nan", [float("NaN"), decimal.Decimal("NaN")])
def test_number_range_nan(nan, dummy_form, dummy_field):
    validator = NumberRange(0, 10)
    dummy_field.data = nan
    with pytest.raises(ValidationError):
        validator(dummy_form, dummy_field)