2016-11-06 07:16:56 +01:00
|
|
|
from django.forms import ChoiceField, Field, Form, Select
|
2016-04-09 19:17:34 +02:00
|
|
|
from django.test import SimpleTestCase
|
|
|
|
|
|
|
|
|
|
|
|
class BasicFieldsTests(SimpleTestCase):
|
|
|
|
def test_field_sets_widget_is_required(self):
|
|
|
|
self.assertTrue(Field(required=True).widget.is_required)
|
|
|
|
self.assertFalse(Field(required=False).widget.is_required)
|
|
|
|
|
|
|
|
def test_cooperative_multiple_inheritance(self):
|
2017-01-19 08:39:46 +01:00
|
|
|
class A:
|
2016-04-09 19:17:34 +02:00
|
|
|
def __init__(self):
|
|
|
|
self.class_a_var = True
|
2017-01-21 14:13:44 +01:00
|
|
|
super().__init__()
|
2016-04-09 19:17:34 +02:00
|
|
|
|
|
|
|
class ComplexField(Field, A):
|
|
|
|
def __init__(self):
|
2017-01-21 14:13:44 +01:00
|
|
|
super().__init__()
|
2016-04-09 19:17:34 +02:00
|
|
|
|
|
|
|
f = ComplexField()
|
|
|
|
self.assertTrue(f.class_a_var)
|
2016-11-06 07:16:56 +01:00
|
|
|
|
|
|
|
def test_field_deepcopies_widget_instance(self):
|
|
|
|
class CustomChoiceField(ChoiceField):
|
|
|
|
widget = Select(attrs={"class": "my-custom-class"})
|
|
|
|
|
|
|
|
class TestForm(Form):
|
|
|
|
field1 = CustomChoiceField(choices=[])
|
|
|
|
field2 = CustomChoiceField(choices=[])
|
|
|
|
|
|
|
|
f = TestForm()
|
|
|
|
f.fields["field1"].choices = [("1", "1")]
|
|
|
|
f.fields["field2"].choices = [("2", "2")]
|
|
|
|
self.assertEqual(f.fields["field1"].widget.choices, [("1", "1")])
|
|
|
|
self.assertEqual(f.fields["field2"].widget.choices, [("2", "2")])
|
2016-11-06 10:30:48 +01:00
|
|
|
|
|
|
|
|
|
|
|
class DisabledFieldTests(SimpleTestCase):
|
|
|
|
def test_disabled_field_has_changed_always_false(self):
|
|
|
|
disabled_field = Field(disabled=True)
|
|
|
|
self.assertFalse(disabled_field.has_changed("x", "y"))
|