2010-09-12 20:04:57 +00:00
|
|
|
from datetime import datetime
|
|
|
|
|
|
|
|
from django.test import TestCase
|
|
|
|
|
2011-10-13 18:04:12 +00:00
|
|
|
from .models import Article, Category
|
2010-09-12 20:04:57 +00:00
|
|
|
|
|
|
|
|
|
|
|
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(
|
2015-08-22 18:05:54 +00:00
|
|
|
headline="Parrot steals", pub_date=datetime(2005, 11, 27)
|
2010-09-12 20:04:57 +00:00
|
|
|
)
|
|
|
|
a1.primary_categories.add(c2, c3)
|
|
|
|
a1.secondary_categories.add(c4)
|
|
|
|
|
|
|
|
a2 = Article.objects.create(
|
2015-08-22 18:05:54 +00:00
|
|
|
headline="Parrot runs", pub_date=datetime(2005, 11, 28)
|
2010-09-12 20:04:57 +00:00
|
|
|
)
|
|
|
|
a2.primary_categories.add(c1, c2)
|
|
|
|
a2.secondary_categories.add(c4)
|
|
|
|
|
2022-09-24 10:29:58 +00:00
|
|
|
self.assertQuerySetEqual(
|
2022-02-03 19:24:19 +00:00
|
|
|
a1.primary_categories.all(),
|
|
|
|
[
|
2010-09-12 20:04:57 +00:00
|
|
|
"Crime",
|
|
|
|
"News",
|
|
|
|
],
|
2022-02-03 19:24:19 +00:00
|
|
|
lambda c: c.name,
|
2010-09-12 20:04:57 +00:00
|
|
|
)
|
2022-09-24 10:29:58 +00:00
|
|
|
self.assertQuerySetEqual(
|
2022-02-03 19:24:19 +00:00
|
|
|
a2.primary_categories.all(),
|
|
|
|
[
|
2010-09-12 20:04:57 +00:00
|
|
|
"News",
|
|
|
|
"Sports",
|
|
|
|
],
|
2022-02-03 19:24:19 +00:00
|
|
|
lambda c: c.name,
|
2010-09-12 20:04:57 +00:00
|
|
|
)
|
2022-09-24 10:29:58 +00:00
|
|
|
self.assertQuerySetEqual(
|
2022-02-03 19:24:19 +00:00
|
|
|
a1.secondary_categories.all(),
|
|
|
|
[
|
2010-09-12 20:04:57 +00:00
|
|
|
"Life",
|
|
|
|
],
|
2022-02-03 19:24:19 +00:00
|
|
|
lambda c: c.name,
|
2010-09-12 20:04:57 +00:00
|
|
|
)
|
2022-09-24 10:29:58 +00:00
|
|
|
self.assertQuerySetEqual(
|
2022-02-03 19:24:19 +00:00
|
|
|
c1.primary_article_set.all(),
|
|
|
|
[
|
2015-08-22 18:05:54 +00:00
|
|
|
"Parrot runs",
|
2010-09-12 20:04:57 +00:00
|
|
|
],
|
2022-02-03 19:24:19 +00:00
|
|
|
lambda a: a.headline,
|
2010-09-12 20:04:57 +00:00
|
|
|
)
|
2022-09-24 10:29:58 +00:00
|
|
|
self.assertQuerySetEqual(c1.secondary_article_set.all(), [])
|
|
|
|
self.assertQuerySetEqual(
|
2022-02-03 19:24:19 +00:00
|
|
|
c2.primary_article_set.all(),
|
|
|
|
[
|
2015-08-22 18:05:54 +00:00
|
|
|
"Parrot steals",
|
|
|
|
"Parrot runs",
|
2010-09-12 20:04:57 +00:00
|
|
|
],
|
2022-02-03 19:24:19 +00:00
|
|
|
lambda a: a.headline,
|
2010-09-12 20:04:57 +00:00
|
|
|
)
|
2022-09-24 10:29:58 +00:00
|
|
|
self.assertQuerySetEqual(c2.secondary_article_set.all(), [])
|
|
|
|
self.assertQuerySetEqual(
|
2022-02-03 19:24:19 +00:00
|
|
|
c3.primary_article_set.all(),
|
|
|
|
[
|
2015-08-22 18:05:54 +00:00
|
|
|
"Parrot steals",
|
2010-09-12 20:04:57 +00:00
|
|
|
],
|
2022-02-03 19:24:19 +00:00
|
|
|
lambda a: a.headline,
|
2010-09-12 20:04:57 +00:00
|
|
|
)
|
2022-09-24 10:29:58 +00:00
|
|
|
self.assertQuerySetEqual(c3.secondary_article_set.all(), [])
|
|
|
|
self.assertQuerySetEqual(c4.primary_article_set.all(), [])
|
|
|
|
self.assertQuerySetEqual(
|
2022-02-03 19:24:19 +00:00
|
|
|
c4.secondary_article_set.all(),
|
|
|
|
[
|
2015-08-22 18:05:54 +00:00
|
|
|
"Parrot steals",
|
|
|
|
"Parrot runs",
|
2010-09-12 20:04:57 +00:00
|
|
|
],
|
2022-02-03 19:24:19 +00:00
|
|
|
lambda a: a.headline,
|
2010-09-12 20:04:57 +00:00
|
|
|
)
|