PageRenderTime 29ms CodeModel.GetById 19ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/modeltests/m2m_and_m2o/tests.py

https://code.google.com/p/mango-py/
Python | 75 lines | 65 code | 8 blank | 2 comment | 0 complexity | f19dccf8c57c4e153f4f3a0d4a234e9e MD5 | raw file
 1from django.db.models import Q
 2from django.test import TestCase
 3
 4from models import Issue, User
 5
 6
 7class RelatedObjectTests(TestCase):
 8    def test_m2m_and_m2o(self):
 9        r = User.objects.create(username="russell")
10        g = User.objects.create(username="gustav")
11
12        i1 = Issue(num=1)
13        i1.client = r
14        i1.save()
15
16        i2 = Issue(num=2)
17        i2.client = r
18        i2.save()
19        i2.cc.add(r)
20
21        i3 = Issue(num=3)
22        i3.client = g
23        i3.save()
24        i3.cc.add(r)
25
26        self.assertQuerysetEqual(
27            Issue.objects.filter(client=r.id), [
28                1,
29                2,
30            ],
31            lambda i: i.num
32        )
33        self.assertQuerysetEqual(
34            Issue.objects.filter(client=g.id), [
35                3,
36            ],
37            lambda i: i.num
38        )
39        self.assertQuerysetEqual(
40            Issue.objects.filter(cc__id__exact=g.id), []
41        )
42        self.assertQuerysetEqual(
43            Issue.objects.filter(cc__id__exact=r.id), [
44                2,
45                3,
46            ],
47            lambda i: i.num
48        )
49
50        # These queries combine results from the m2m and the m2o relationships.
51        # They're three ways of saying the same thing.
52        self.assertQuerysetEqual(
53            Issue.objects.filter(Q(cc__id__exact = r.id) | Q(client=r.id)), [
54                1,
55                2,
56                3,
57            ],
58            lambda i: i.num
59        )
60        self.assertQuerysetEqual(
61            Issue.objects.filter(cc__id__exact=r.id) | Issue.objects.filter(client=r.id), [
62                1,
63                2,
64                3,
65            ],
66            lambda i: i.num
67        )
68        self.assertQuerysetEqual(
69            Issue.objects.filter(Q(client=r.id) | Q(cc__id__exact=r.id)), [
70                1,
71                2,
72                3,
73            ],
74            lambda i: i.num
75        )