Ticket #17988: test_model_formset_checks_commit_before_delete.diff

File test_model_formset_checks_commit_before_delete.diff, 1.7 KB (added by Remoun Metyas, 13 years ago)

Regression test.

  • tests/regressiontests/inline_formsets/tests.py

     
    108108            obj.save()
    109109        self.assertEqual(school.child_set.count(), 1)
    110110
     111    def test_deletion_without_commit(self):
     112        """
     113        Make sure inlineformsets respect commit=False when deleting (#17988).
     114        """
     115        PoemFormSet = inlineformset_factory(Poet, Poem, can_delete=True)
     116        poet = Poet.objects.create(name='test')
     117        poem = poet.poem_set.create(name='test poem')
     118        data = {
     119            'poem_set-TOTAL_FORMS': u'1',
     120            'poem_set-INITIAL_FORMS': u'1',
     121            'poem_set-MAX_NUM_FORMS': u'0',
     122            'poem_set-0-id': unicode(poem.pk),
     123            'poem_set-0-poet': unicode(poet.pk),
     124            'poem_set-0-name': u'test',
     125        }
     126        formset = PoemFormSet(data, instance=poet)
     127        self.assertTrue(formset.is_valid())
     128        formset.save()
     129        self.assertEqual(Poem.objects.count(), 1)
    111130
     131        data['poem_set-0-DELETE'] = 'on'
     132        formset = PoemFormSet(data, instance=poet)
     133        self.assertTrue(formset.is_valid())
     134
     135        poems = formset.save(commit=False)
     136        self.assertEqual(poems, [])
     137        self.assertEqual(len(formset.deleted_objects), 1)
     138        self.assertEqual(Poem.objects.count(), 1)
     139
     140        poems = formset.save()
     141        self.assertEqual(poems, [])
     142        self.assertEqual(len(formset.deleted_objects), 1)
     143        self.assertEqual(Poem.objects.count(), 0)
     144
     145
    112146class InlineFormsetFactoryTest(TestCase):
    113147    def test_inline_formset_factory(self):
    114148        """
Back to Top