Update validators and filter to remove strings that contain only
whitespace The validator ListItemRequired() was only checking for None and an empty string, not for strings that were multiple whitespace characters. This fixes this issue by checking each item with regex to make sure it contains non whitespace characters The filter remove_empty_string() also was not checking for strings that were multiple whitespace characters. This was also fixed by using regex tomake sure that the string contains non whitespace characters, and also clips any trailing whitespace.
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import pytest
|
||||
from wtforms.fields import RadioField
|
||||
from wtforms.fields import RadioField, FieldList, StringField
|
||||
from werkzeug.datastructures import ImmutableMultiDict
|
||||
|
||||
from atst.forms.forms import BaseForm
|
||||
@@ -16,6 +16,12 @@ class FormWithChoices(BaseForm):
|
||||
)
|
||||
|
||||
|
||||
class FormWithList(BaseForm):
|
||||
list = FieldList(
|
||||
StringField("a very fancy list", filters=[BaseForm.remove_empty_string])
|
||||
)
|
||||
|
||||
|
||||
class TestBaseForm:
|
||||
class Foo:
|
||||
person = {"force_side": None}
|
||||
@@ -34,3 +40,17 @@ class TestBaseForm:
|
||||
form_data_3 = ImmutableMultiDict({"force_side": "dark"})
|
||||
form_3 = FormWithChoices(form_data_3, obj=self.obj)
|
||||
assert form_3.data["force_side"] is "dark"
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"form_data",
|
||||
[["testing", "", "QA"], ["testing", " ", "QA"], ["testing", None, "QA"]],
|
||||
)
|
||||
def test_blank_list_items_removed(self, form_data):
|
||||
form = FormWithList(list=form_data)
|
||||
assert form.validate(flash_invalid=False)
|
||||
assert not form.data == ["testing", "QA"]
|
||||
|
||||
def test_remove_empty_string_clips_whitespace(self):
|
||||
form = FormWithList(list=[" QA", " testing "])
|
||||
assert form.validate(flash_invalid=False)
|
||||
assert form.list.data == ["QA", "testing"]
|
||||
|
@@ -85,3 +85,20 @@ class TestFileLength:
|
||||
|
||||
dummy_field.data = "random string"
|
||||
assert validator(dummy_form, dummy_field)
|
||||
|
||||
|
||||
class TestListItemRequired:
|
||||
@pytest.mark.parametrize("valid", [[" a", ""], ["a ", ""], ["a", ""]])
|
||||
def test_ListItemRequired(self, valid, dummy_form, dummy_field):
|
||||
validator = ListItemRequired()
|
||||
dummy_field.data = valid
|
||||
validator(dummy_form, dummy_field)
|
||||
|
||||
@pytest.mark.parametrize("invalid", [[""], [" "], [None], []])
|
||||
def test_ListItemRequired_rejects_blank_names(
|
||||
self, invalid, dummy_form, dummy_field
|
||||
):
|
||||
validator = ListItemRequired()
|
||||
dummy_field.data = invalid
|
||||
with pytest.raises(ValidationError):
|
||||
validator(dummy_form, dummy_field)
|
||||
|
Reference in New Issue
Block a user