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 38 39 40 41 42 43 44 45 46 47
|
from wtforms import Form
from wtforms_test import FormTestCase
from tests import MultiDict
from wtforms_components import SelectMultipleField
class Dummy:
fruits = []
class TestSelectMultipleField(FormTestCase):
choices = (
("Fruits", (("apple", "Apple"), ("peach", "Peach"), ("pear", "Pear"))),
(
"Vegetables",
(
("cucumber", "Cucumber"),
("potato", "Potato"),
("tomato", "Tomato"),
),
),
)
def init_form(self, **kwargs):
class TestForm(Form):
fruits = SelectMultipleField(**kwargs)
self.form_class = TestForm
return self.form_class
def test_understands_nested_choices(self):
form_class = self.init_form(choices=self.choices)
form = form_class(MultiDict([("fruits", "apple"), ("fruits", "invalid")]))
form.validate()
assert form.errors == {
"fruits": ["'invalid' is not a valid choice for this field"]
}
def test_option_selected(self):
form_class = self.init_form(choices=self.choices)
obj = Dummy()
obj.fruits = ["peach"]
form = form_class(obj=obj)
assert '<option selected value="peach">Peach</option>' in str(form.fruits)
|