Skip to content

Commit 5874b11

Browse files
committed
Migrate m2m_and_m2o doctests. Thanks to Alex Gaynor.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@13786 bcc190cf-cafb-0310-a4f2-bffc1f526a37
1 parent 4d77fee commit 5874b11

File tree

2 files changed

+75
-44
lines changed

2 files changed

+75
-44
lines changed

tests/modeltests/m2m_and_m2o/models.py

Lines changed: 0 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -19,47 +19,3 @@ def __unicode__(self):
1919

2020
class Meta:
2121
ordering = ('num',)
22-
23-
24-
__test__ = {'API_TESTS':"""
25-
>>> Issue.objects.all()
26-
[]
27-
>>> r = User(username='russell')
28-
>>> r.save()
29-
>>> g = User(username='gustav')
30-
>>> g.save()
31-
32-
>>> i = Issue(num=1)
33-
>>> i.client = r
34-
>>> i.save()
35-
36-
>>> i2 = Issue(num=2)
37-
>>> i2.client = r
38-
>>> i2.save()
39-
>>> i2.cc.add(r)
40-
41-
>>> i3 = Issue(num=3)
42-
>>> i3.client = g
43-
>>> i3.save()
44-
>>> i3.cc.add(r)
45-
46-
>>> from django.db.models.query import Q
47-
48-
>>> Issue.objects.filter(client=r.id)
49-
[<Issue: 1>, <Issue: 2>]
50-
>>> Issue.objects.filter(client=g.id)
51-
[<Issue: 3>]
52-
>>> Issue.objects.filter(cc__id__exact=g.id)
53-
[]
54-
>>> Issue.objects.filter(cc__id__exact=r.id)
55-
[<Issue: 2>, <Issue: 3>]
56-
57-
# These queries combine results from the m2m and the m2o relationships.
58-
# They're three ways of saying the same thing.
59-
>>> Issue.objects.filter(Q(cc__id__exact=r.id) | Q(client=r.id))
60-
[<Issue: 1>, <Issue: 2>, <Issue: 3>]
61-
>>> Issue.objects.filter(cc__id__exact=r.id) | Issue.objects.filter(client=r.id)
62-
[<Issue: 1>, <Issue: 2>, <Issue: 3>]
63-
>>> Issue.objects.filter(Q(client=r.id) | Q(cc__id__exact=r.id))
64-
[<Issue: 1>, <Issue: 2>, <Issue: 3>]
65-
"""}

tests/modeltests/m2m_and_m2o/tests.py

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
from django.db.models import Q
2+
from django.test import TestCase
3+
4+
from models import Issue, User
5+
6+
7+
class 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+
)

0 commit comments

Comments
 (0)