|
1 from django.test import TestCase |
|
2 from django.contrib.contenttypes.models import ContentType |
|
3 from django.db.models import Q |
|
4 from models import * |
|
5 |
|
6 class GenericRelationTests(TestCase): |
|
7 |
|
8 def test_inherited_models_content_type(self): |
|
9 """ |
|
10 Test that GenericRelations on inherited classes use the correct content |
|
11 type. |
|
12 """ |
|
13 |
|
14 p = Place.objects.create(name="South Park") |
|
15 r = Restaurant.objects.create(name="Chubby's") |
|
16 l1 = Link.objects.create(content_object=p) |
|
17 l2 = Link.objects.create(content_object=r) |
|
18 self.assertEqual(list(p.links.all()), [l1]) |
|
19 self.assertEqual(list(r.links.all()), [l2]) |
|
20 |
|
21 def test_reverse_relation_pk(self): |
|
22 """ |
|
23 Test that the correct column name is used for the primary key on the |
|
24 originating model of a query. See #12664. |
|
25 """ |
|
26 p = Person.objects.create(account=23, name='Chef') |
|
27 a = Address.objects.create(street='123 Anywhere Place', |
|
28 city='Conifer', state='CO', |
|
29 zipcode='80433', content_object=p) |
|
30 |
|
31 qs = Person.objects.filter(addresses__zipcode='80433') |
|
32 self.assertEqual(1, qs.count()) |
|
33 self.assertEqual('Chef', qs[0].name) |
|
34 |
|
35 def test_charlink_delete(self): |
|
36 oddrel = OddRelation1.objects.create(name='clink') |
|
37 cl = CharLink.objects.create(content_object=oddrel) |
|
38 oddrel.delete() |
|
39 |
|
40 def test_textlink_delete(self): |
|
41 oddrel = OddRelation2.objects.create(name='tlink') |
|
42 tl = TextLink.objects.create(content_object=oddrel) |
|
43 oddrel.delete() |
|
44 |
|
45 def test_q_object_or(self): |
|
46 """ |
|
47 Tests that SQL query parameters for generic relations are properly |
|
48 grouped when OR is used. |
|
49 |
|
50 Test for bug http://code.djangoproject.com/ticket/11535 |
|
51 |
|
52 In this bug the first query (below) works while the second, with the |
|
53 query parameters the same but in reverse order, does not. |
|
54 |
|
55 The issue is that the generic relation conditions do not get properly |
|
56 grouped in parentheses. |
|
57 """ |
|
58 note_contact = Contact.objects.create() |
|
59 org_contact = Contact.objects.create() |
|
60 note = Note.objects.create(note='note', content_object=note_contact) |
|
61 org = Organization.objects.create(name='org name') |
|
62 org.contacts.add(org_contact) |
|
63 # search with a non-matching note and a matching org name |
|
64 qs = Contact.objects.filter(Q(notes__note__icontains=r'other note') | |
|
65 Q(organizations__name__icontains=r'org name')) |
|
66 self.assertTrue(org_contact in qs) |
|
67 # search again, with the same query parameters, in reverse order |
|
68 qs = Contact.objects.filter( |
|
69 Q(organizations__name__icontains=r'org name') | |
|
70 Q(notes__note__icontains=r'other note')) |
|
71 self.assertTrue(org_contact in qs) |
|
72 |
|
73 |
|
74 |