Skip to content

Commit

Permalink
Fixed #18508 -- tests for repeated deletion bug in ModelFormSet
Browse files Browse the repository at this point in the history
The ticket's issue was already fixed by patch for #14877.
  • Loading branch information
OleLaursen authored and akaariai committed Oct 31, 2013
1 parent c64efe3 commit f4f01fb
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions tests/model_formsets/tests.py
Expand Up @@ -100,6 +100,36 @@ def test_change_form_deletion_when_invalid(self):
formset.save() formset.save()
self.assertEqual(Poet.objects.count(), 0) self.assertEqual(Poet.objects.count(), 0)


def test_outdated_deletion(self):
poet = Poet.objects.create(name='test')
poem = Poem.objects.create(name='Brevity is the soul of wit', poet=poet)

PoemFormSet = inlineformset_factory(Poet, Poem, fields="__all__", can_delete=True)

# Simulate deletion of an object that doesn't exist in the database
data = {
'form-TOTAL_FORMS': '2',
'form-INITIAL_FORMS': '2',
'form-0-id': str(poem.pk),
'form-0-name': 'foo',
'form-1-id': str(poem.pk + 1), # doesn't exist
'form-1-name': 'bar',
'form-1-DELETE': 'on',
}
formset = PoemFormSet(data, instance=poet, prefix="form")

# The formset is valid even though poem.pk + 1 doesn't exist,
# because it's marked for deletion anyway
self.assertTrue(formset.is_valid())

formset.save()

# Make sure the save went through correctly
self.assertEqual(Poem.objects.get(pk=poem.pk).name, "foo")
self.assertEqual(poet.poem_set.count(), 1)
self.assertFalse(Poem.objects.filter(pk=poem.pk + 1).exists())


class ModelFormsetTest(TestCase): class ModelFormsetTest(TestCase):
def test_simple_save(self): def test_simple_save(self):
qs = Author.objects.all() qs = Author.objects.all()
Expand Down

0 comments on commit f4f01fb

Please sign in to comment.