Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

[1.0.x] Backport of r10348 from trunk, plus missing tests that should…

… have been in r10286.

git-svn-id: http://code.djangoproject.com/svn/django/branches/releases/1.0.X@10349 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit f58df036bddcf41ce50e0ea217fbef2f8470e98b 1 parent c7a16bc
@jkocherhans jkocherhans authored
View
70 tests/modeltests/model_formsets/tests.py
@@ -0,0 +1,70 @@
+from django.test import TestCase
+from django.forms.models import modelformset_factory
+from modeltests.model_formsets.models import Poet, Poem
+
+class DeletionTests(TestCase):
+ def test_deletion(self):
+ PoetFormSet = modelformset_factory(Poet, can_delete=True)
+ poet = Poet.objects.create(name='test')
+ data = {
+ 'form-TOTAL_FORMS': u'1',
+ 'form-INITIAL_FORMS': u'1',
+ 'form-0-id': str(poet.pk),
+ 'form-0-name': u'test',
+ 'form-0-DELETE': u'on',
+ }
+ formset = PoetFormSet(data, queryset=Poet.objects.all())
+ formset.save()
+ self.assertTrue(formset.is_valid())
+ self.assertEqual(Poet.objects.count(), 0)
+
+ def test_add_form_deletion_when_invalid(self):
+ """
+ Make sure that an add form that is filled out, but marked for deletion
+ doesn't cause validation errors.
+ """
+ PoetFormSet = modelformset_factory(Poet, can_delete=True)
+ data = {
+ 'form-TOTAL_FORMS': u'1',
+ 'form-INITIAL_FORMS': u'0',
+ 'form-0-id': u'',
+ 'form-0-name': u'x' * 1000,
+ }
+ formset = PoetFormSet(data, queryset=Poet.objects.all())
+ # Make sure this form doesn't pass validation.
+ self.assertEqual(formset.is_valid(), False)
+ self.assertEqual(Poet.objects.count(), 0)
+
+ # Then make sure that it *does* pass validation and delete the object,
+ # even though the data isn't actually valid.
+ data['form-0-DELETE'] = 'on'
+ formset = PoetFormSet(data, queryset=Poet.objects.all())
+ self.assertEqual(formset.is_valid(), True)
+ formset.save()
+ self.assertEqual(Poet.objects.count(), 0)
+
+ def test_change_form_deletion_when_invalid(self):
+ """
+ Make sure that an add form that is filled out, but marked for deletion
+ doesn't cause validation errors.
+ """
+ PoetFormSet = modelformset_factory(Poet, can_delete=True)
+ poet = Poet.objects.create(name='test')
+ data = {
+ 'form-TOTAL_FORMS': u'1',
+ 'form-INITIAL_FORMS': u'1',
+ 'form-0-id': u'1',
+ 'form-0-name': u'x' * 1000,
+ }
+ formset = PoetFormSet(data, queryset=Poet.objects.all())
+ # Make sure this form doesn't pass validation.
+ self.assertEqual(formset.is_valid(), False)
+ self.assertEqual(Poet.objects.count(), 1)
+
+ # Then make sure that it *does* pass validation and delete the object,
+ # even though the data isn't actually valid.
+ data['form-0-DELETE'] = 'on'
+ formset = PoetFormSet(data, queryset=Poet.objects.all())
+ self.assertEqual(formset.is_valid(), True)
+ formset.save()
+ self.assertEqual(Poet.objects.count(), 0)
View
76 tests/regressiontests/inline_formsets/tests.py
@@ -0,0 +1,76 @@
+from django.test import TestCase
+from django.forms.models import inlineformset_factory
+from regressiontests.inline_formsets.models import Poet, Poem
+
+class DeletionTests(TestCase):
+ def test_deletion(self):
+ PoemFormSet = inlineformset_factory(Poet, Poem, can_delete=True)
+ poet = Poet.objects.create(name='test')
+ poem = poet.poem_set.create(name='test poem')
+ data = {
+ 'poem_set-TOTAL_FORMS': u'1',
+ 'poem_set-INITIAL_FORMS': u'1',
+ 'poem_set-0-id': str(poem.pk),
+ 'poem_set-0-poet': str(poet.pk),
+ 'poem_set-0-name': u'test',
+ 'poem_set-0-DELETE': u'on',
+ }
+ formset = PoemFormSet(data, instance=poet)
+ formset.save()
+ self.assertTrue(formset.is_valid())
+ self.assertEqual(Poem.objects.count(), 0)
+
+ def test_add_form_deletion_when_invalid(self):
+ """
+ Make sure that an add form that is filled out, but marked for deletion
+ doesn't cause validation errors.
+ """
+ PoemFormSet = inlineformset_factory(Poet, Poem, can_delete=True)
+ poet = Poet.objects.create(name='test')
+ data = {
+ 'poem_set-TOTAL_FORMS': u'1',
+ 'poem_set-INITIAL_FORMS': u'0',
+ 'poem_set-0-id': u'',
+ 'poem_set-0-poem': u'1',
+ 'poem_set-0-name': u'x' * 1000,
+ }
+ formset = PoemFormSet(data, instance=poet)
+ # Make sure this form doesn't pass validation.
+ self.assertEqual(formset.is_valid(), False)
+ self.assertEqual(Poem.objects.count(), 0)
+
+ # Then make sure that it *does* pass validation and delete the object,
+ # even though the data isn't actually valid.
+ data['poem_set-0-DELETE'] = 'on'
+ formset = PoemFormSet(data, instance=poet)
+ self.assertEqual(formset.is_valid(), True)
+ formset.save()
+ self.assertEqual(Poem.objects.count(), 0)
+
+ def test_change_form_deletion_when_invalid(self):
+ """
+ Make sure that a change form that is filled out, but marked for deletion
+ doesn't cause validation errors.
+ """
+ PoemFormSet = inlineformset_factory(Poet, Poem, can_delete=True)
+ poet = Poet.objects.create(name='test')
+ poet.poem_set.create(name='test poem')
+ data = {
+ 'poem_set-TOTAL_FORMS': u'1',
+ 'poem_set-INITIAL_FORMS': u'1',
+ 'poem_set-0-id': u'1',
+ 'poem_set-0-poem': u'1',
+ 'poem_set-0-name': u'x' * 1000,
+ }
+ formset = PoemFormSet(data, instance=poet)
+ # Make sure this form doesn't pass validation.
+ self.assertEqual(formset.is_valid(), False)
+ self.assertEqual(Poem.objects.count(), 1)
+
+ # Then make sure that it *does* pass validation and delete the object,
+ # even though the data isn't actually valid.
+ data['poem_set-0-DELETE'] = 'on'
+ formset = PoemFormSet(data, instance=poet)
+ self.assertEqual(formset.is_valid(), True)
+ formset.save()
+ self.assertEqual(Poem.objects.count(), 0)
Please sign in to comment.
Something went wrong with that request. Please try again.