2010-09-12 22:04:48 +02:00
|
|
|
from datetime import datetime
|
|
|
|
|
|
|
|
from django.test import TestCase
|
|
|
|
|
2015-01-28 13:35:27 +01:00
|
|
|
from .models import Article, Reporter, Writer
|
2010-09-12 22:04:48 +02:00
|
|
|
|
|
|
|
|
|
|
|
class M2MIntermediaryTests(TestCase):
|
2019-08-05 17:47:50 +02:00
|
|
|
def test_intermediary(self):
|
2010-09-12 22:04:48 +02:00
|
|
|
r1 = Reporter.objects.create(first_name="John", last_name="Smith")
|
|
|
|
r2 = Reporter.objects.create(first_name="Jane", last_name="Doe")
|
|
|
|
|
|
|
|
a = Article.objects.create(
|
|
|
|
headline="This is a test", pub_date=datetime(2005, 7, 27)
|
|
|
|
)
|
|
|
|
|
|
|
|
w1 = Writer.objects.create(reporter=r1, article=a, position="Main writer")
|
|
|
|
w2 = Writer.objects.create(reporter=r2, article=a, position="Contributor")
|
|
|
|
|
2022-09-24 12:29:58 +02:00
|
|
|
self.assertQuerySetEqual(
|
2010-09-12 22:04:48 +02:00
|
|
|
a.writer_set.select_related().order_by("-position"),
|
|
|
|
[
|
|
|
|
("John Smith", "Main writer"),
|
|
|
|
("Jane Doe", "Contributor"),
|
|
|
|
],
|
2016-12-29 16:27:49 +01:00
|
|
|
lambda w: (str(w.reporter), w.position),
|
2010-09-12 22:04:48 +02:00
|
|
|
)
|
|
|
|
self.assertEqual(w1.reporter, r1)
|
|
|
|
self.assertEqual(w2.reporter, r2)
|
|
|
|
|
|
|
|
self.assertEqual(w1.article, a)
|
|
|
|
self.assertEqual(w2.article, a)
|
|
|
|
|
2022-09-24 12:29:58 +02:00
|
|
|
self.assertQuerySetEqual(
|
2010-09-12 22:04:48 +02:00
|
|
|
r1.writer_set.all(),
|
|
|
|
[("John Smith", "Main writer")],
|
2016-12-29 16:27:49 +01:00
|
|
|
lambda w: (str(w.reporter), w.position),
|
2010-09-12 22:04:48 +02:00
|
|
|
)
|