111 lines
3.7 KiB
Python
111 lines
3.7 KiB
Python
from wtforms.validators import ValidationError, StopValidation
|
|
import pytest, copy
|
|
|
|
from atst.forms.validators import (
|
|
Name,
|
|
IsNumber,
|
|
PhoneNumber,
|
|
ListItemsUnique,
|
|
RequiredIfNot,
|
|
)
|
|
|
|
|
|
class TestIsNumber:
|
|
@pytest.mark.parametrize("valid", ["0", "12", "-12"])
|
|
def test_IsNumber_accepts_integers(self, valid, dummy_form, dummy_field):
|
|
validator = IsNumber()
|
|
dummy_field.data = valid
|
|
validator(dummy_form, dummy_field)
|
|
|
|
@pytest.mark.parametrize("invalid", ["12.1", "two", ""])
|
|
def test_IsNumber_rejects_anything_else(self, invalid, dummy_form, dummy_field):
|
|
validator = IsNumber()
|
|
dummy_field.data = invalid
|
|
with pytest.raises(ValidationError):
|
|
validator(dummy_form, dummy_field)
|
|
|
|
|
|
class TestPhoneNumber:
|
|
@pytest.mark.parametrize("valid", ["12345", "1234567890", "(123) 456-7890"])
|
|
def test_PhoneNumber_accepts_valid_numbers(self, valid, dummy_form, dummy_field):
|
|
validator = PhoneNumber()
|
|
dummy_field.data = valid
|
|
validator(dummy_form, dummy_field)
|
|
|
|
@pytest.mark.parametrize(
|
|
"invalid", ["1234", "123456", "1234567abc", "(123) 456-789012"]
|
|
)
|
|
def test_PhoneNumber_rejects_invalid_numbers(
|
|
self, invalid, dummy_form, dummy_field
|
|
):
|
|
validator = PhoneNumber()
|
|
dummy_field.data = invalid
|
|
with pytest.raises(ValidationError):
|
|
validator(dummy_form, dummy_field)
|
|
|
|
|
|
class TestName:
|
|
@pytest.mark.parametrize("valid", ["a", "abcde", "hi mark", "cloud9", "niña"])
|
|
def test_Name_accepts_letters(self, valid, dummy_form, dummy_field):
|
|
validator = Name()
|
|
dummy_field.data = valid
|
|
validator(dummy_form, dummy_field)
|
|
|
|
@pytest.mark.parametrize(
|
|
"invalid",
|
|
["", "/my name", ":-)", "Name&Name", "Ke$ha", "A^Name", "#yourvalidname"],
|
|
)
|
|
def test_Name_rejects_invalid_characters(self, invalid, dummy_form, dummy_field):
|
|
validator = Name()
|
|
dummy_field.data = invalid
|
|
with pytest.raises(ValidationError):
|
|
validator(dummy_form, dummy_field)
|
|
|
|
|
|
class TestListItemsUnique:
|
|
@pytest.mark.parametrize("valid", [["a", "aa", "aaa"], ["one", "two", "three"]])
|
|
def test_ListItemsUnique_allows_unique_items(self, valid, dummy_form, dummy_field):
|
|
validator = ListItemsUnique()
|
|
dummy_field.data = valid
|
|
validator(dummy_form, dummy_field)
|
|
|
|
@pytest.mark.parametrize(
|
|
"invalid", [["a", "a", "a"], ["one", "two", "two", "three"]]
|
|
)
|
|
def test_ListItemsUnique_rejects_duplicate_names(
|
|
self, invalid, dummy_form, dummy_field
|
|
):
|
|
validator = ListItemsUnique()
|
|
dummy_field.data = invalid
|
|
with pytest.raises(ValidationError):
|
|
validator(dummy_form, dummy_field)
|
|
|
|
|
|
class TestRequiredIfNot:
|
|
def test_RequiredIfNot_requires_field_if_arg_is_falsy(
|
|
self, dummy_form_with_field, dummy_field
|
|
):
|
|
form = dummy_form_with_field("arg", False)
|
|
validator = RequiredIfNot("arg")
|
|
dummy_field.data = None
|
|
|
|
with pytest.raises(ValidationError):
|
|
validator(form, dummy_field)
|
|
|
|
def test_RequiredIfNot_does_not_require_field_if_arg_is_truthy(
|
|
self, dummy_form_with_field, dummy_field
|
|
):
|
|
form = dummy_form_with_field("arg", True)
|
|
validator = RequiredIfNot("arg")
|
|
dummy_field.data = None
|
|
|
|
with pytest.raises(StopValidation):
|
|
validator(form, dummy_field)
|
|
|
|
def test_RequiredIfNot_arg_is_None_raises_error(self, dummy_form, dummy_field):
|
|
validator = RequiredIfNot("arg")
|
|
dummy_field.data = "some data"
|
|
|
|
with pytest.raises(Exception):
|
|
validator(dummy_form, dummy_field)
|