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 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
|
import pytest
from tests.common import DummyPostData
from wtforms import validators
from wtforms import widgets
from wtforms.fields import SelectField
from wtforms.form import Form
def make_form(name="F", **fields):
return type(str(name), (Form,), fields)
def test_select_field_copies_choices():
class F(Form):
items = SelectField(choices=[])
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
def add_choice(self, choice):
self.items.choices.append((choice, choice))
f1 = F()
f2 = F()
f1.add_choice("a")
f2.add_choice("b")
assert f1.items.choices == [("a", "a")]
assert f2.items.choices == [("b", "b")]
assert f1.items.choices is not f2.items.choices
class F(Form):
a = SelectField(choices=[("a", "hello"), ("btest", "bye")], default="a")
b = SelectField(
choices=[(1, "Item 1"), (2, "Item 2")],
coerce=int,
option_widget=widgets.TextInput(),
)
def test_defaults():
form = F()
assert form.a.data == "a"
assert form.b.data is None
assert form.validate() is False
assert form.a() == (
'<select id="a" name="a"><option selected value="a">hello</option>'
'<option value="btest">bye</option></select>'
)
assert form.b() == (
'<select id="b" name="b"><option value="1">Item 1</option>'
'<option value="2">Item 2</option></select>'
)
def test_with_data():
form = F(DummyPostData(a=["btest"]))
assert form.a.data == "btest"
assert form.a() == (
'<select id="a" name="a"><option value="a">hello</option>'
'<option selected value="btest">bye</option></select>'
)
def test_value_coercion():
form = F(DummyPostData(b=["2"]))
assert form.b.data == 2
assert form.b.validate(form)
form = F(DummyPostData(b=["b"]))
assert form.b.data is None
assert not form.b.validate(form)
def test_iterable_options():
form = F()
first_option = list(form.a)[0]
assert isinstance(first_option, form.a._Option)
assert list(str(x) for x in form.a) == [
'<option selected value="a">hello</option>',
'<option value="btest">bye</option>',
]
assert isinstance(first_option.widget, widgets.Option)
assert isinstance(list(form.b)[0].widget, widgets.TextInput)
assert (
first_option(disabled=True)
== '<option disabled selected value="a">hello</option>'
)
def test_default_coerce():
F = make_form(a=SelectField(choices=[("a", "Foo")]))
form = F(DummyPostData(a=[]))
assert not form.validate()
assert form.a.data is None
assert len(form.a.errors) == 1
assert form.a.errors[0] == "Not a valid choice."
def test_validate_choices():
F = make_form(a=SelectField(choices=[("a", "Foo")]))
form = F(DummyPostData(a=["b"]))
assert not form.validate()
assert form.a.data == "b"
assert len(form.a.errors) == 1
assert form.a.errors[0] == "Not a valid choice."
def test_validate_choices_when_empty():
F = make_form(a=SelectField(choices=[]))
form = F(DummyPostData(a=["b"]))
assert not form.validate()
assert form.a.data == "b"
assert len(form.a.errors) == 1
assert form.a.errors[0] == "Not a valid choice."
def test_validate_choices_when_none():
F = make_form(a=SelectField())
form = F(DummyPostData(a="b"))
with pytest.raises(TypeError, match="Choices cannot be None"):
form.validate()
def test_dont_validate_choices():
F = make_form(a=SelectField(choices=[("a", "Foo")], validate_choice=False))
form = F(DummyPostData(a=["b"]))
assert form.validate()
assert form.a.data == "b"
assert len(form.a.errors) == 0
def test_choices_can_be_none_when_choice_validation_is_disabled():
F = make_form(a=SelectField(validate_choice=False))
form = F(DummyPostData(a="b"))
assert form.validate()
def test_choice_shortcut():
F = make_form(a=SelectField(choices=["foo", "bar"], validate_choice=False))
form = F(a="bar")
assert '<option value="foo">foo</option>' in form.a()
def test_choice_shortcut_post():
F = make_form(a=SelectField(choices=["foo", "bar"]))
form = F(DummyPostData(a=["foo"]))
assert form.validate()
assert form.a.data == "foo"
assert len(form.a.errors) == 0
@pytest.mark.parametrize("choices", [[], None, {}])
def test_empty_choice(choices):
F = make_form(a=SelectField(choices=choices, validate_choice=False))
form = F(a="bar")
assert form.a() == '<select id="a" name="a"></select>'
def test_callable_choices():
def choices():
return ["foo", "bar"]
F = make_form(a=SelectField(choices=choices))
form = F(a="bar")
assert list(str(x) for x in form.a) == [
'<option value="foo">foo</option>',
'<option selected value="bar">bar</option>',
]
def test_requried_flag():
F = make_form(
c=SelectField(
choices=[("a", "hello"), ("b", "bye")],
validators=[validators.InputRequired()],
)
)
form = F(DummyPostData(c="a"))
assert form.c() == (
'<select id="c" name="c" required>'
'<option selected value="a">hello</option>'
'<option value="b">bye</option>'
"</select>"
)
def test_required_validator():
F = make_form(
c=SelectField(
choices=[("a", "hello"), ("b", "bye")],
validators=[validators.InputRequired()],
)
)
form = F(DummyPostData(c="b"))
assert form.validate()
assert form.c.errors == []
form = F()
assert form.validate() is False
assert form.c.errors == ["This field is required."]
def test_render_kw_preserved():
F = make_form(
a=SelectField(choices=[("foo"), ("bar")], render_kw=dict(disabled=True))
)
form = F()
assert form.a() == (
'<select disabled id="a" name="a">'
'<option value="foo">foo</option>'
'<option value="bar">bar</option>'
"</select>"
)
def test_optgroup():
F = make_form(a=SelectField(choices={"hello": [("a", "Foo")]}))
form = F(a="a")
assert (
'<optgroup label="hello">'
'<option selected value="a">Foo</option>'
"</optgroup>" in form.a()
)
assert list(form.a.iter_choices()) == [("a", "Foo", True, {})]
def test_optgroup_shortcut():
F = make_form(a=SelectField(choices={"hello": ["foo", "bar"]}))
form = F(a="bar")
assert (
'<optgroup label="hello">'
'<option value="foo">foo</option>'
'<option selected value="bar">bar</option>'
"</optgroup>" in form.a()
)
assert list(form.a.iter_choices()) == [
("foo", "foo", False, {}),
("bar", "bar", True, {}),
]
@pytest.mark.parametrize("choices", [[], ()])
def test_empty_optgroup(choices):
F = make_form(a=SelectField(choices={"hello": choices}))
form = F(a="bar")
assert '<optgroup label="hello"></optgroup>' in form.a()
assert list(form.a.iter_choices()) == []
def test_option_render_kw():
F = make_form(
a=SelectField(choices=[("a", "Foo", {"title": "foobar", "data-foo": "bar"})])
)
form = F(a="a")
assert (
'<option data-foo="bar" selected title="foobar" value="a">Foo</option>'
in form.a()
)
assert list(form.a.iter_choices()) == [
("a", "Foo", True, {"title": "foobar", "data-foo": "bar"})
]
def test_optgroup_option_render_kw():
F = make_form(
a=SelectField(
choices={"hello": [("a", "Foo", {"title": "foobar", "data-foo": "bar"})]}
)
)
form = F(a="a")
assert (
'<optgroup label="hello">'
'<option data-foo="bar" selected title="foobar" value="a">Foo</option>'
"</optgroup>" in form.a()
)
assert list(form.a.iter_choices()) == [
("a", "Foo", True, {"title": "foobar", "data-foo": "bar"})
]
|