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
|
from wtforms import Form
from wtforms.fields import SelectField
from wtforms_test import FormTestCase
from wtforms_components import SelectWidget
class Dummy:
fruits = None
class TestSelectWidgetWithNativeSelect(FormTestCase):
choices = (
("apple", "Apple"),
("peach", "Peach"),
("pear", "Pear"),
("cucumber", "Cucumber"),
("potato", "Potato"),
("tomato", "Tomato"),
)
def init_form(self, **kwargs):
class TestForm(Form):
fruit = SelectField(widget=SelectWidget(), **kwargs)
self.form_class = TestForm
return self.form_class
def test_option_selected(self):
form_class = self.init_form(choices=self.choices)
obj = Dummy()
obj.fruit = "peach"
form = form_class(obj=obj)
assert '<option selected value="peach">Peach</option>' in str(form.fruit)
def test_default_value(self):
form_class = self.init_form(choices=self.choices, default="pear")
form = form_class()
assert '<option selected value="pear">Pear</option>' in str(form.fruit)
|