File: test_time_range_validator.py

package info (click to toggle)
wtforms-components 0.11.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 352 kB
  • sloc: python: 1,582; makefile: 135; sh: 11
file content (37 lines) | stat: -rw-r--r-- 1,271 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
from datetime import time

from wtforms import Form
from wtforms_test import FormTestCase

from tests import MultiDict
from wtforms_components import TimeField, TimeRange


class TestTimeRangeValidator(FormTestCase):
    def init_form(self, **kwargs):
        class ModelTestForm(Form):
            time = TimeField(validators=[TimeRange(**kwargs)])

        self.form_class = ModelTestForm
        return self.form_class

    def test_time_greater_than_validator(self):
        form_class = self.init_form(min=time(12))
        form = form_class(MultiDict(time="11:12"))
        form.validate()
        error_msg = "Time must be greater than 12:00."
        assert form.errors["time"] == [error_msg]

    def test_time_less_than_validator(self):
        form_class = self.init_form(max=time(13, 30))
        form = form_class(MultiDict(time="13:40"))
        form.validate()
        error_msg = "Time must be less than 13:30."
        assert form.errors["time"] == [error_msg]

    def test_time_between_validator(self):
        form_class = self.init_form(min=time(12), max=time(13))
        form = form_class(MultiDict(time="14:30"))
        form.validate()
        error_msg = "Time must be between 12:00 and 13:00."
        assert form.errors["time"] == [error_msg]