PageRenderTime 22ms CodeModel.GetById 11ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/modeltests/m2m_multiple/tests.py

https://code.google.com/p/mango-py/
Python | 84 lines | 77 code | 7 blank | 0 comment | 2 complexity | a4098dc2e8549afba80a9a04f91cfd8c MD5 | raw file
 1from datetime import datetime
 2
 3from django.test import TestCase
 4
 5from models import Article, Category
 6
 7
 8class M2MMultipleTests(TestCase):
 9    def test_multiple(self):
10        c1, c2, c3, c4 = [
11            Category.objects.create(name=name)
12            for name in ["Sports", "News", "Crime", "Life"]
13        ]
14
15        a1 = Article.objects.create(
16            headline="Area man steals", pub_date=datetime(2005, 11, 27)
17        )
18        a1.primary_categories.add(c2, c3)
19        a1.secondary_categories.add(c4)
20
21        a2 = Article.objects.create(
22            headline="Area man runs", pub_date=datetime(2005, 11, 28)
23        )
24        a2.primary_categories.add(c1, c2)
25        a2.secondary_categories.add(c4)
26
27        self.assertQuerysetEqual(
28            a1.primary_categories.all(), [
29                "Crime",
30                "News",
31            ],
32            lambda c: c.name
33        )
34        self.assertQuerysetEqual(
35            a2.primary_categories.all(), [
36                "News",
37                "Sports",
38            ],
39            lambda c: c.name
40        )
41        self.assertQuerysetEqual(
42            a1.secondary_categories.all(), [
43                "Life",
44            ],
45            lambda c: c.name
46        )
47        self.assertQuerysetEqual(
48            c1.primary_article_set.all(), [
49                "Area man runs",
50            ],
51            lambda a: a.headline
52        )
53        self.assertQuerysetEqual(
54            c1.secondary_article_set.all(), []
55        )
56        self.assertQuerysetEqual(
57            c2.primary_article_set.all(), [
58                "Area man steals",
59                "Area man runs",
60            ],
61            lambda a: a.headline
62        )
63        self.assertQuerysetEqual(
64            c2.secondary_article_set.all(), []
65        )
66        self.assertQuerysetEqual(
67            c3.primary_article_set.all(), [
68                "Area man steals",
69            ],
70            lambda a: a.headline
71        )
72        self.assertQuerysetEqual(
73            c3.secondary_article_set.all(), []
74        )
75        self.assertQuerysetEqual(
76            c4.primary_article_set.all(), []
77        )
78        self.assertQuerysetEqual(
79            c4.secondary_article_set.all(), [
80                "Area man steals",
81                "Area man runs",
82            ],
83            lambda a: a.headline
84        )