2022-01-14 07:08:38 +08:00
|
|
|
from django.forms import FileField, FileInput, Form
|
2015-08-31 10:13:42 +08:00
|
|
|
|
|
|
|
from .base import WidgetTest
|
|
|
|
|
|
|
|
|
|
|
|
class FileInputTest(WidgetTest):
|
|
|
|
widget = FileInput()
|
|
|
|
|
|
|
|
def test_render(self):
|
|
|
|
"""
|
|
|
|
FileInput widgets never render the value attribute. The old value
|
|
|
|
isn't useful if a form is updated or an error occurred.
|
|
|
|
"""
|
2018-01-21 15:09:10 +08:00
|
|
|
self.check_html(
|
|
|
|
self.widget,
|
|
|
|
"email",
|
|
|
|
"test@example.com",
|
|
|
|
html='<input type="file" name="email">',
|
|
|
|
)
|
|
|
|
self.check_html(
|
|
|
|
self.widget, "email", "", html='<input type="file" name="email">'
|
|
|
|
)
|
|
|
|
self.check_html(
|
|
|
|
self.widget, "email", None, html='<input type="file" name="email">'
|
2022-02-04 03:24:19 +08:00
|
|
|
)
|
2016-09-07 05:41:54 +08:00
|
|
|
|
|
|
|
def test_value_omitted_from_data(self):
|
|
|
|
self.assertIs(self.widget.value_omitted_from_data({}, {}, "field"), True)
|
|
|
|
self.assertIs(
|
|
|
|
self.widget.value_omitted_from_data({}, {"field": "value"}, "field"), False
|
|
|
|
)
|
2020-01-05 15:51:33 +08:00
|
|
|
|
|
|
|
def test_use_required_attribute(self):
|
|
|
|
# False when initial data exists. The file input is left blank by the
|
|
|
|
# user to keep the existing, initial value.
|
|
|
|
self.assertIs(self.widget.use_required_attribute(None), True)
|
|
|
|
self.assertIs(self.widget.use_required_attribute("resume.txt"), False)
|
2022-01-14 07:08:38 +08:00
|
|
|
|
|
|
|
def test_fieldset(self):
|
|
|
|
class TestForm(Form):
|
|
|
|
template_name = "forms_tests/use_fieldset.html"
|
|
|
|
field = FileField(widget=self.widget)
|
|
|
|
|
|
|
|
form = TestForm()
|
|
|
|
self.assertIs(self.widget.use_fieldset, False)
|
|
|
|
self.assertHTMLEqual(
|
|
|
|
'<div><label for="id_field">Field:</label><input id="id_field" '
|
|
|
|
'name="field" required type="file"></div>',
|
|
|
|
form.render(),
|
|
|
|
)
|