Ticket #14456: inline_formsets.diff

File inline_formsets.diff, 4.8 KB (added by Preston Timmons, 14 years ago)
  • tests/regressiontests/inline_formsets/models.py

    diff --git a/tests/regressiontests/inline_formsets/models.py b/tests/regressiontests/inline_formsets/models.py
    index 9b1f8b4..8d6c2b2 100644
    a b class Poem(models.Model):  
    2525
    2626    def __unicode__(self):
    2727        return self.name
    28 
    29 __test__ = {'API_TESTS': """
    30 
    31 >>> from django.forms.models import inlineformset_factory
    32 
    33 
    34 Child has two ForeignKeys to Parent, so if we don't specify which one to use
    35 for the inline formset, we should get an exception.
    36 
    37 >>> ifs = inlineformset_factory(Parent, Child)
    38 Traceback (most recent call last):
    39     ...
    40 Exception: <class 'regressiontests.inline_formsets.models.Child'> has more than 1 ForeignKey to <class 'regressiontests.inline_formsets.models.Parent'>
    41 
    42 
    43 These two should both work without a problem.
    44 
    45 >>> ifs = inlineformset_factory(Parent, Child, fk_name='mother')
    46 >>> ifs = inlineformset_factory(Parent, Child, fk_name='father')
    47 
    48 
    49 If we specify fk_name, but it isn't a ForeignKey from the child model to the
    50 parent model, we should get an exception.
    51 
    52 >>> ifs = inlineformset_factory(Parent, Child, fk_name='school')
    53 Traceback (most recent call last):
    54     ...
    55 Exception: fk_name 'school' is not a ForeignKey to <class 'regressiontests.inline_formsets.models.Parent'>
    56 
    57 
    58 If the field specified in fk_name is not a ForeignKey, we should get an
    59 exception.
    60 
    61 >>> ifs = inlineformset_factory(Parent, Child, fk_name='test')
    62 Traceback (most recent call last):
    63     ...
    64 Exception: <class 'regressiontests.inline_formsets.models.Child'> has no field named 'test'
    65 
    66 
    67 # Regression test for #9171.
    68 >>> ifs = inlineformset_factory(Parent, Child, exclude=('school',), fk_name='mother')
    69 """
    70 }
  • tests/regressiontests/inline_formsets/tests.py

    diff --git a/tests/regressiontests/inline_formsets/tests.py b/tests/regressiontests/inline_formsets/tests.py
    index 83d2fba..41100b9 100644
    a b from django.test import TestCase  
    22from django.forms.models import inlineformset_factory
    33from regressiontests.inline_formsets.models import Poet, Poem, School, Parent, Child
    44
     5
    56class DeletionTests(TestCase):
     7
    68    def test_deletion(self):
    79        PoemFormSet = inlineformset_factory(Poet, Poem, can_delete=True)
    810        poet = Poet.objects.create(name='test')
    class DeletionTests(TestCase):  
    103105            obj.save()
    104106        self.assertEqual(school.child_set.count(), 1)
    105107
     108
     109class InlineFormsetFactoryTest(TestCase):
     110
     111    def test_inline_formset_factory(self):
     112        """ These should both work without a problem. """
     113
     114        inlineformset_factory(Parent, Child, fk_name='mother')
     115        inlineformset_factory(Parent, Child, fk_name='father')
     116
     117    def test_exception_on_unspecified_foreign_key(self):
     118        """
     119        Child has two ForeignKeys to Parent, so if we don't specify which
     120        one to use for the inline formset, we should get an exception.
     121        """
     122
     123        self.assertRaises(Exception, inlineformset_factory, [Parent, Child])
     124        try:
     125            inlineformset_factory(Parent, Child)
     126        except Exception, e:
     127            self.assertEqual(str(e),
     128                "<class 'regressiontests.inline_formsets.models.Child'> has more than 1 ForeignKey to <class 'regressiontests.inline_formsets.models.Parent'>")
     129
     130
     131    def test_fk_name_not_foreign_key_field_from_child(self):
     132        """
     133        If we specify fk_name, but it isn't a ForeignKey from the child
     134        model to the parent model, we should get an exception.
     135        """
     136
     137        self.assertRaises(Exception, inlineformset_factory,
     138            [Parent, Child], {'fk_name': 'school'})
     139
     140        try:
     141            inlineformset_factory(Parent, Child, fk_name='school')
     142        except Exception, e:
     143            self.assertEqual(str(e),
     144                "fk_name 'school' is not a ForeignKey to <class 'regressiontests.inline_formsets.models.Parent'>")
     145
     146    def test_non_foreign_key_field(self):
     147        """
     148        If the field specified in fk_name is not a ForeignKey, we should
     149        get an exception.
     150        """
     151
     152        self.assertRaises(Exception, inlineformset_factory,
     153            [Parent, Child], {'fk_name': 'test'})
     154
     155        try:
     156            inlineformset_factory(Parent, Child, fk_name='test')
     157        except Exception, e:
     158            self.assertEqual(str(e),
     159                "<class 'regressiontests.inline_formsets.models.Child'> has no field named 'test'")
     160
     161    def test_any_iterable_allowed_as_argument_to_exclude(self):
     162        # Regression test for #9171.
     163        inlineformset_factory(Parent, Child, exclude=['school',],
     164            fk_name='mother')
     165
     166        inlineformset_factory(Parent, Child, exclude=('school',),
     167            fk_name='mother')
Back to Top