2009-04-01 03:55:20 +08:00
|
|
|
from django.test import TestCase
|
|
|
|
from django.forms.models import inlineformset_factory
|
2009-05-08 17:59:46 +08:00
|
|
|
from regressiontests.inline_formsets.models import Poet, Poem, School, Parent, Child
|
2009-04-01 03:55:20 +08:00
|
|
|
|
|
|
|
class DeletionTests(TestCase):
|
|
|
|
def test_deletion(self):
|
|
|
|
PoemFormSet = inlineformset_factory(Poet, Poem, can_delete=True)
|
|
|
|
poet = Poet.objects.create(name='test')
|
2009-04-02 06:16:34 +08:00
|
|
|
poem = poet.poem_set.create(name='test poem')
|
2009-04-01 03:55:20 +08:00
|
|
|
data = {
|
|
|
|
'poem_set-TOTAL_FORMS': u'1',
|
|
|
|
'poem_set-INITIAL_FORMS': u'1',
|
2009-04-02 06:16:34 +08:00
|
|
|
'poem_set-0-id': str(poem.pk),
|
|
|
|
'poem_set-0-poet': str(poet.pk),
|
2009-04-01 03:55:20 +08:00
|
|
|
'poem_set-0-name': u'test',
|
|
|
|
'poem_set-0-DELETE': u'on',
|
|
|
|
}
|
|
|
|
formset = PoemFormSet(data, instance=poet)
|
|
|
|
formset.save()
|
2009-04-05 01:51:35 +08:00
|
|
|
self.failUnless(formset.is_valid())
|
2009-04-01 03:55:20 +08:00
|
|
|
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)
|
2009-05-08 17:59:46 +08:00
|
|
|
|
|
|
|
def test_save_new(self):
|
|
|
|
"""
|
|
|
|
Make sure inlineformsets respect commit=False
|
|
|
|
regression for #10750
|
|
|
|
"""
|
|
|
|
# exclude some required field from the forms
|
2010-01-12 10:29:45 +08:00
|
|
|
ChildFormSet = inlineformset_factory(School, Child, exclude=['father', 'mother'])
|
2009-05-08 17:59:46 +08:00
|
|
|
school = School.objects.create(name=u'test')
|
|
|
|
mother = Parent.objects.create(name=u'mother')
|
|
|
|
father = Parent.objects.create(name=u'father')
|
|
|
|
data = {
|
|
|
|
'child_set-TOTAL_FORMS': u'1',
|
|
|
|
'child_set-INITIAL_FORMS': u'0',
|
|
|
|
'child_set-0-name': u'child',
|
|
|
|
}
|
|
|
|
formset = ChildFormSet(data, instance=school)
|
|
|
|
self.assertEqual(formset.is_valid(), True)
|
|
|
|
objects = formset.save(commit=False)
|
2010-01-12 10:29:45 +08:00
|
|
|
for obj in objects:
|
|
|
|
obj.mother = mother
|
|
|
|
obj.father = father
|
|
|
|
obj.save()
|
2009-05-08 17:59:46 +08:00
|
|
|
self.assertEqual(school.child_set.count(), 1)
|
|
|
|
|