| 1 |
from django.forms import Form, NullBooleanField, NullBooleanSelect |
| 2 |
from django.utils import translation |
| 3 |
|
| 4 |
from .base import WidgetTest |
| 5 |
|
| 6 |
|
| 7 |
class NullBooleanSelectTest(WidgetTest): |
| 8 |
widget = NullBooleanSelect() |
| 9 |
|
| 10 |
def test_render_true(self): |
| 11 |
self.check_html( |
| 12 |
self.widget, |
| 13 |
"is_cool", |
| 14 |
True, |
| 15 |
html=("""<select name="is_cool"> |
| 16 |
<option value="unknown">Unknown</option> |
| 17 |
<option value="true" selected>Yes</option> |
| 18 |
<option value="false">No</option> |
| 19 |
</select>"""), |
| 20 |
) |
| 21 |
|
| 22 |
def test_render_false(self): |
| 23 |
self.check_html( |
| 24 |
self.widget, |
| 25 |
"is_cool", |
| 26 |
False, |
| 27 |
html=("""<select name="is_cool"> |
| 28 |
<option value="unknown">Unknown</option> |
| 29 |
<option value="true">Yes</option> |
| 30 |
<option value="false" selected>No</option> |
| 31 |
</select>"""), |
| 32 |
) |
| 33 |
|
| 34 |
def test_render_none(self): |
| 35 |
self.check_html( |
| 36 |
self.widget, |
| 37 |
"is_cool", |
| 38 |
None, |
| 39 |
html=("""<select name="is_cool"> |
| 40 |
<option value="unknown" selected>Unknown</option> |
| 41 |
<option value="true">Yes</option> |
| 42 |
<option value="false">No</option> |
| 43 |
</select>"""), |
| 44 |
) |
| 45 |
|
| 46 |
def test_render_value_unknown(self): |
| 47 |
self.check_html( |
| 48 |
self.widget, |
| 49 |
"is_cool", |
| 50 |
"unknown", |
| 51 |
html=("""<select name="is_cool"> |
| 52 |
<option value="unknown" selected>Unknown</option> |
| 53 |
<option value="true">Yes</option> |
| 54 |
<option value="false">No</option> |
| 55 |
</select>"""), |
| 56 |
) |
| 57 |
|
| 58 |
def test_render_value_true(self): |
| 59 |
self.check_html( |
| 60 |
self.widget, |
| 61 |
"is_cool", |
| 62 |
"true", |
| 63 |
html=("""<select name="is_cool"> |
| 64 |
<option value="unknown">Unknown</option> |
| 65 |
<option value="true" selected>Yes</option> |
| 66 |
<option value="false">No</option> |
| 67 |
</select>"""), |
| 68 |
) |
| 69 |
|
| 70 |
def test_render_value_false(self): |
| 71 |
self.check_html( |
| 72 |
self.widget, |
| 73 |
"is_cool", |
| 74 |
"false", |
| 75 |
html=("""<select name="is_cool"> |
| 76 |
<option value="unknown">Unknown</option> |
| 77 |
<option value="true">Yes</option> |
| 78 |
<option value="false" selected>No</option> |
| 79 |
</select>"""), |
| 80 |
) |
| 81 |
|
| 82 |
def test_render_value_1(self): |
| 83 |
self.check_html( |
| 84 |
self.widget, |
| 85 |
"is_cool", |
| 86 |
"1", |
| 87 |
html=("""<select name="is_cool"> |
| 88 |
<option value="unknown" selected>Unknown</option> |
| 89 |
<option value="true">Yes</option> |
| 90 |
<option value="false">No</option> |
| 91 |
</select>"""), |
| 92 |
) |
| 93 |
|
| 94 |
def test_render_value_2(self): |
| 95 |
self.check_html( |
| 96 |
self.widget, |
| 97 |
"is_cool", |
| 98 |
"2", |
| 99 |
html=("""<select name="is_cool"> |
| 100 |
<option value="unknown">Unknown</option> |
| 101 |
<option value="true" selected>Yes</option> |
| 102 |
<option value="false">No</option> |
| 103 |
</select>"""), |
| 104 |
) |
| 105 |
|
| 106 |
def test_render_value_3(self): |
| 107 |
self.check_html( |
| 108 |
self.widget, |
| 109 |
"is_cool", |
| 110 |
"3", |
| 111 |
html=("""<select name="is_cool"> |
| 112 |
<option value="unknown">Unknown</option> |
| 113 |
<option value="true">Yes</option> |
| 114 |
<option value="false" selected>No</option> |
| 115 |
</select>"""), |
| 116 |
) |
| 117 |
|
| 118 |
def test_l10n(self): |
| 119 |
""" |
| 120 |
The NullBooleanSelect widget's options are lazily localized (#17190). |
| 121 |
""" |
| 122 |
widget = NullBooleanSelect() |
| 123 |
|
| 124 |
with translation.override("de-at"): |
| 125 |
self.check_html( |
| 126 |
widget, |
| 127 |
"id_bool", |
| 128 |
True, |
| 129 |
html=(""" |
| 130 |
<select name="id_bool"> |
| 131 |
<option value="unknown">Unbekannt</option> |
| 132 |
<option value="true" selected>Ja</option> |
| 133 |
<option value="false">Nein</option> |
| 134 |
</select> |
| 135 |
"""), |
| 136 |
) |
| 137 |
|
| 138 |
def test_fieldset(self): |
| 139 |
class TestForm(Form): |
| 140 |
template_name = "forms_tests/use_fieldset.html" |
| 141 |
field = NullBooleanField(widget=self.widget) |
| 142 |
|
| 143 |
form = TestForm() |
| 144 |
self.assertIs(self.widget.use_fieldset, False) |
| 145 |
self.assertHTMLEqual( |
| 146 |
'<div><label for="id_field">Field:</label>' |
| 147 |
'<select name="field" id="id_field">' |
| 148 |
'<option value="unknown" selected>Unknown</option>' |
| 149 |
'<option value="true">Yes</option>' |
| 150 |
'<option value="false">No</option></select></div>', |
| 151 |
form.render(), |
| 152 |
) |