atst/tests/forms/test_base_form.py
leigh-mil ab9b62f54b 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.
2019-11-12 13:07:50 -05:00

57 lines
1.8 KiB
Python

import pytest
from wtforms.fields import RadioField, FieldList, StringField
from werkzeug.datastructures import ImmutableMultiDict
from atst.forms.forms import BaseForm
class FormWithChoices(BaseForm):
force_side = RadioField(
"Choose your side",
choices=[
("light", "Light Side"),
("dark", "Dark Side"),
("neutral", "Chaotic Neutral"),
],
)
class FormWithList(BaseForm):
list = FieldList(
StringField("a very fancy list", filters=[BaseForm.remove_empty_string])
)
class TestBaseForm:
class Foo:
person = {"force_side": None}
obj = Foo()
def test_radio_field_saves_only_as_choice(self):
form_data_1 = ImmutableMultiDict({"force_side": "None"})
form_1 = FormWithChoices(form_data_1, obj=self.obj)
assert form_1.data["force_side"] is None
form_data_2 = ImmutableMultiDict({"force_side": "a fake choice"})
form_2 = FormWithChoices(form_data_2, obj=self.obj)
assert form_2.data["force_side"] is None
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"]