Refactor RequiredIfNot custom validator, add tests
This commit is contained in:
@@ -1,7 +1,13 @@
|
||||
from wtforms.validators import ValidationError
|
||||
import pytest
|
||||
from wtforms.validators import ValidationError, StopValidation
|
||||
import pytest, copy
|
||||
|
||||
from atst.forms.validators import Name, IsNumber, PhoneNumber, ListItemsUnique
|
||||
from atst.forms.validators import (
|
||||
Name,
|
||||
IsNumber,
|
||||
PhoneNumber,
|
||||
ListItemsUnique,
|
||||
RequiredIfNot,
|
||||
)
|
||||
|
||||
|
||||
class TestIsNumber:
|
||||
@@ -73,3 +79,32 @@ class TestListItemsUnique:
|
||||
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)
|
||||
|
Reference in New Issue
Block a user