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):
|
|
|
|
class A(object):
|
|
|
|
def __init__(self):
|
|
|
|
self.class_a_var = True
|
|
|
|
super(A, self).__init__()
|
|
|
|
|
|
|
|
class ComplexField(Field, A):
|
|
|
|
def __init__(self):
|
|
|
|
super(ComplexField, self).__init__()
|
|
|
|
|
|
|
|
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')])
|