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