Migrated m2m_multiple doctests. Thanks to Alex Gaynor.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@13788 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
935aa4c6a1
commit
21e7987c4c
|
@ -28,52 +28,3 @@ class Article(models.Model):
|
|||
def __unicode__(self):
|
||||
return self.headline
|
||||
|
||||
__test__ = {'API_TESTS':"""
|
||||
>>> from datetime import datetime
|
||||
|
||||
>>> c1 = Category(name='Sports')
|
||||
>>> c1.save()
|
||||
>>> c2 = Category(name='News')
|
||||
>>> c2.save()
|
||||
>>> c3 = Category(name='Crime')
|
||||
>>> c3.save()
|
||||
>>> c4 = Category(name='Life')
|
||||
>>> c4.save()
|
||||
|
||||
>>> a1 = Article(headline='Area man steals', pub_date=datetime(2005, 11, 27))
|
||||
>>> a1.save()
|
||||
>>> a1.primary_categories.add(c2, c3)
|
||||
>>> a1.secondary_categories.add(c4)
|
||||
|
||||
>>> a2 = Article(headline='Area man runs', pub_date=datetime(2005, 11, 28))
|
||||
>>> a2.save()
|
||||
>>> a2.primary_categories.add(c1, c2)
|
||||
>>> a2.secondary_categories.add(c4)
|
||||
|
||||
>>> a1.primary_categories.all()
|
||||
[<Category: Crime>, <Category: News>]
|
||||
|
||||
>>> a2.primary_categories.all()
|
||||
[<Category: News>, <Category: Sports>]
|
||||
|
||||
>>> a1.secondary_categories.all()
|
||||
[<Category: Life>]
|
||||
|
||||
|
||||
>>> c1.primary_article_set.all()
|
||||
[<Article: Area man runs>]
|
||||
>>> c1.secondary_article_set.all()
|
||||
[]
|
||||
>>> c2.primary_article_set.all()
|
||||
[<Article: Area man steals>, <Article: Area man runs>]
|
||||
>>> c2.secondary_article_set.all()
|
||||
[]
|
||||
>>> c3.primary_article_set.all()
|
||||
[<Article: Area man steals>]
|
||||
>>> c3.secondary_article_set.all()
|
||||
[]
|
||||
>>> c4.primary_article_set.all()
|
||||
[]
|
||||
>>> c4.secondary_article_set.all()
|
||||
[<Article: Area man steals>, <Article: Area man runs>]
|
||||
"""}
|
||||
|
|
|
@ -0,0 +1,84 @@
|
|||
from datetime import datetime
|
||||
|
||||
from django.test import TestCase
|
||||
|
||||
from models import Article, Category
|
||||
|
||||
|
||||
class M2MMultipleTests(TestCase):
|
||||
def test_multiple(self):
|
||||
c1, c2, c3, c4 = [
|
||||
Category.objects.create(name=name)
|
||||
for name in ["Sports", "News", "Crime", "Life"]
|
||||
]
|
||||
|
||||
a1 = Article.objects.create(
|
||||
headline="Area man steals", pub_date=datetime(2005, 11, 27)
|
||||
)
|
||||
a1.primary_categories.add(c2, c3)
|
||||
a1.secondary_categories.add(c4)
|
||||
|
||||
a2 = Article.objects.create(
|
||||
headline="Area man runs", pub_date=datetime(2005, 11, 28)
|
||||
)
|
||||
a2.primary_categories.add(c1, c2)
|
||||
a2.secondary_categories.add(c4)
|
||||
|
||||
self.assertQuerysetEqual(
|
||||
a1.primary_categories.all(), [
|
||||
"Crime",
|
||||
"News",
|
||||
],
|
||||
lambda c: c.name
|
||||
)
|
||||
self.assertQuerysetEqual(
|
||||
a2.primary_categories.all(), [
|
||||
"News",
|
||||
"Sports",
|
||||
],
|
||||
lambda c: c.name
|
||||
)
|
||||
self.assertQuerysetEqual(
|
||||
a1.secondary_categories.all(), [
|
||||
"Life",
|
||||
],
|
||||
lambda c: c.name
|
||||
)
|
||||
self.assertQuerysetEqual(
|
||||
c1.primary_article_set.all(), [
|
||||
"Area man runs",
|
||||
],
|
||||
lambda a: a.headline
|
||||
)
|
||||
self.assertQuerysetEqual(
|
||||
c1.secondary_article_set.all(), []
|
||||
)
|
||||
self.assertQuerysetEqual(
|
||||
c2.primary_article_set.all(), [
|
||||
"Area man steals",
|
||||
"Area man runs",
|
||||
],
|
||||
lambda a: a.headline
|
||||
)
|
||||
self.assertQuerysetEqual(
|
||||
c2.secondary_article_set.all(), []
|
||||
)
|
||||
self.assertQuerysetEqual(
|
||||
c3.primary_article_set.all(), [
|
||||
"Area man steals",
|
||||
],
|
||||
lambda a: a.headline
|
||||
)
|
||||
self.assertQuerysetEqual(
|
||||
c3.secondary_article_set.all(), []
|
||||
)
|
||||
self.assertQuerysetEqual(
|
||||
c4.primary_article_set.all(), []
|
||||
)
|
||||
self.assertQuerysetEqual(
|
||||
c4.secondary_article_set.all(), [
|
||||
"Area man steals",
|
||||
"Area man runs",
|
||||
],
|
||||
lambda a: a.headline
|
||||
)
|
Loading…
Reference in New Issue