Fixed #14456 -- converted inline_formsets tests from doctests to unittests. We have always been at war with doctests. Thanks to prestontimmons for the patch.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@14212 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
1ac4c101ae
commit
8d364763ed
|
@ -1,6 +1,7 @@
|
||||||
# coding: utf-8
|
# coding: utf-8
|
||||||
from django.db import models
|
from django.db import models
|
||||||
|
|
||||||
|
|
||||||
class School(models.Model):
|
class School(models.Model):
|
||||||
name = models.CharField(max_length=100)
|
name = models.CharField(max_length=100)
|
||||||
|
|
||||||
|
@ -25,46 +26,3 @@ class Poem(models.Model):
|
||||||
|
|
||||||
def __unicode__(self):
|
def __unicode__(self):
|
||||||
return self.name
|
return self.name
|
||||||
|
|
||||||
__test__ = {'API_TESTS': """
|
|
||||||
|
|
||||||
>>> from django.forms.models import inlineformset_factory
|
|
||||||
|
|
||||||
|
|
||||||
Child has two ForeignKeys to Parent, so if we don't specify which one to use
|
|
||||||
for the inline formset, we should get an exception.
|
|
||||||
|
|
||||||
>>> ifs = inlineformset_factory(Parent, Child)
|
|
||||||
Traceback (most recent call last):
|
|
||||||
...
|
|
||||||
Exception: <class 'regressiontests.inline_formsets.models.Child'> has more than 1 ForeignKey to <class 'regressiontests.inline_formsets.models.Parent'>
|
|
||||||
|
|
||||||
|
|
||||||
These two should both work without a problem.
|
|
||||||
|
|
||||||
>>> ifs = inlineformset_factory(Parent, Child, fk_name='mother')
|
|
||||||
>>> ifs = inlineformset_factory(Parent, Child, fk_name='father')
|
|
||||||
|
|
||||||
|
|
||||||
If we specify fk_name, but it isn't a ForeignKey from the child model to the
|
|
||||||
parent model, we should get an exception.
|
|
||||||
|
|
||||||
>>> ifs = inlineformset_factory(Parent, Child, fk_name='school')
|
|
||||||
Traceback (most recent call last):
|
|
||||||
...
|
|
||||||
Exception: fk_name 'school' is not a ForeignKey to <class 'regressiontests.inline_formsets.models.Parent'>
|
|
||||||
|
|
||||||
|
|
||||||
If the field specified in fk_name is not a ForeignKey, we should get an
|
|
||||||
exception.
|
|
||||||
|
|
||||||
>>> ifs = inlineformset_factory(Parent, Child, fk_name='test')
|
|
||||||
Traceback (most recent call last):
|
|
||||||
...
|
|
||||||
Exception: <class 'regressiontests.inline_formsets.models.Child'> has no field named 'test'
|
|
||||||
|
|
||||||
|
|
||||||
# Regression test for #9171.
|
|
||||||
>>> ifs = inlineformset_factory(Parent, Child, exclude=('school',), fk_name='mother')
|
|
||||||
"""
|
|
||||||
}
|
|
||||||
|
|
|
@ -2,7 +2,9 @@ from django.test import TestCase
|
||||||
from django.forms.models import inlineformset_factory
|
from django.forms.models import inlineformset_factory
|
||||||
from regressiontests.inline_formsets.models import Poet, Poem, School, Parent, Child
|
from regressiontests.inline_formsets.models import Poet, Poem, School, Parent, Child
|
||||||
|
|
||||||
|
|
||||||
class DeletionTests(TestCase):
|
class DeletionTests(TestCase):
|
||||||
|
|
||||||
def test_deletion(self):
|
def test_deletion(self):
|
||||||
PoemFormSet = inlineformset_factory(Poet, Poem, can_delete=True)
|
PoemFormSet = inlineformset_factory(Poet, Poem, can_delete=True)
|
||||||
poet = Poet.objects.create(name='test')
|
poet = Poet.objects.create(name='test')
|
||||||
|
@ -103,3 +105,51 @@ class DeletionTests(TestCase):
|
||||||
obj.save()
|
obj.save()
|
||||||
self.assertEqual(school.child_set.count(), 1)
|
self.assertEqual(school.child_set.count(), 1)
|
||||||
|
|
||||||
|
|
||||||
|
class InlineFormsetFactoryTest(TestCase):
|
||||||
|
def test_inline_formset_factory(self):
|
||||||
|
"""
|
||||||
|
These should both work without a problem.
|
||||||
|
"""
|
||||||
|
inlineformset_factory(Parent, Child, fk_name='mother')
|
||||||
|
inlineformset_factory(Parent, Child, fk_name='father')
|
||||||
|
|
||||||
|
def test_exception_on_unspecified_foreign_key(self):
|
||||||
|
"""
|
||||||
|
Child has two ForeignKeys to Parent, so if we don't specify which one
|
||||||
|
to use for the inline formset, we should get an exception.
|
||||||
|
"""
|
||||||
|
self.assertRaisesRegexp(Exception,
|
||||||
|
"<class 'regressiontests.inline_formsets.models.Child'> has more than 1 ForeignKey to <class 'regressiontests.inline_formsets.models.Parent'>",
|
||||||
|
inlineformset_factory, Parent, Child
|
||||||
|
)
|
||||||
|
|
||||||
|
def test_fk_name_not_foreign_key_field_from_child(self):
|
||||||
|
"""
|
||||||
|
If we specify fk_name, but it isn't a ForeignKey from the child model
|
||||||
|
to the parent model, we should get an exception.
|
||||||
|
"""
|
||||||
|
self.assertRaises(Exception,
|
||||||
|
"fk_name 'school' is not a ForeignKey to <class 'regressiontests.inline_formsets.models.Parent'>",
|
||||||
|
inlineformset_factory, Parent, Child, fk_name='school'
|
||||||
|
)
|
||||||
|
|
||||||
|
def test_non_foreign_key_field(self):
|
||||||
|
"""
|
||||||
|
If the field specified in fk_name is not a ForeignKey, we should get an
|
||||||
|
exception.
|
||||||
|
"""
|
||||||
|
self.assertRaisesRegexp(Exception,
|
||||||
|
"<class 'regressiontests.inline_formsets.models.Child'> has no field named 'test'",
|
||||||
|
inlineformset_factory, Parent, Child, fk_name='test'
|
||||||
|
)
|
||||||
|
|
||||||
|
def test_any_iterable_allowed_as_argument_to_exclude(self):
|
||||||
|
# Regression test for #9171.
|
||||||
|
inlineformset_factory(
|
||||||
|
Parent, Child, exclude=['school'], fk_name='mother'
|
||||||
|
)
|
||||||
|
|
||||||
|
inlineformset_factory(
|
||||||
|
Parent, Child, exclude=('school',), fk_name='mother'
|
||||||
|
)
|
||||||
|
|
Loading…
Reference in New Issue