|
1 from datetime import datetime |
|
2 |
|
3 from django.test import TestCase |
|
4 |
|
5 from models import Reporter, Article, Writer |
|
6 |
|
7 |
|
8 class M2MIntermediaryTests(TestCase): |
|
9 def test_intermeiary(self): |
|
10 r1 = Reporter.objects.create(first_name="John", last_name="Smith") |
|
11 r2 = Reporter.objects.create(first_name="Jane", last_name="Doe") |
|
12 |
|
13 a = Article.objects.create( |
|
14 headline="This is a test", pub_date=datetime(2005, 7, 27) |
|
15 ) |
|
16 |
|
17 w1 = Writer.objects.create(reporter=r1, article=a, position="Main writer") |
|
18 w2 = Writer.objects.create(reporter=r2, article=a, position="Contributor") |
|
19 |
|
20 self.assertQuerysetEqual( |
|
21 a.writer_set.select_related().order_by("-position"), [ |
|
22 ("John Smith", "Main writer"), |
|
23 ("Jane Doe", "Contributor"), |
|
24 ], |
|
25 lambda w: (unicode(w.reporter), w.position) |
|
26 ) |
|
27 self.assertEqual(w1.reporter, r1) |
|
28 self.assertEqual(w2.reporter, r2) |
|
29 |
|
30 self.assertEqual(w1.article, a) |
|
31 self.assertEqual(w2.article, a) |
|
32 |
|
33 self.assertQuerysetEqual( |
|
34 r1.writer_set.all(), [ |
|
35 ("John Smith", "Main writer") |
|
36 ], |
|
37 lambda w: (unicode(w.reporter), w.position) |
|
38 ) |