Ticket #18573: 0001-test-default-formfield_callback.patch

File 0001-test-default-formfield_callback.patch, 1.7 KB (added by tuxcanfly, 12 years ago)
  • tests/regressiontests/model_forms_regress/tests.py

    From bfe18e6a0a45a73a92cce994f06206fc2839cada Mon Sep 17 00:00:00 2001
    From: Javed Khan <javed@agiliq.com>
    Date: Sat, 29 Dec 2012 21:14:50 +0530
    Subject: [PATCH] refs #18573: added test for default formfield_callback
    
    ---
     tests/regressiontests/model_forms_regress/tests.py |   25 ++++++++++++++++++++
     1 file changed, 25 insertions(+)
    
    diff --git a/tests/regressiontests/model_forms_regress/tests.py b/tests/regressiontests/model_forms_regress/tests.py
    index 90c907f..1545c59 100644
    a b class FormFieldCallbackTests(TestCase):  
    315315        self.assertEqual(callback_args,
    316316                         [(id_field, {}), (name_field, {'widget': widget})])
    317317
     318    def test_default_modelform_callback(self):
     319        """Test that formfield_callback defaults to the modelform formfield_callback"""
     320
     321        callback_args = []
     322
     323        def callback(db_field, **kwargs):
     324            callback_args.append((db_field, kwargs))
     325            return db_field.formfield(**kwargs)
     326
     327        widget = forms.Textarea()
     328
     329        class BaseForm(forms.ModelForm):
     330
     331            formfield_callback = callback
     332
     333            class Meta:
     334                model = Person
     335                widgets = {'name': widget}
     336
     337        _ = modelform_factory(Person, form=BaseForm)
     338        id_field, name_field = Person._meta.fields
     339
     340        self.assertEqual(callback_args,
     341                         [(id_field, {}), (name_field, {'widget': widget})])
     342
    318343    def test_bad_callback(self):
    319344        # A bad callback provided by user still gives an error
    320345        self.assertRaises(TypeError, modelform_factory, Person,
Back to Top