~ubuntu-branches/ubuntu/quantal/python-django/quantal-security

« back to all changes in this revision

Viewing changes to tests/regressiontests/generic_relations_regress/tests.py

  • Committer: Bazaar Package Importer
  • Author(s): Chris Lamb
  • Date: 2010-05-21 07:52:55 UTC
  • mfrom: (1.3.6 upstream)
  • mto: This revision was merged to the branch mainline in revision 28.
  • Revision ID: james.westby@ubuntu.com-20100521075255-ii78v1dyfmyu3uzx
Tags: upstream-1.2
ImportĀ upstreamĀ versionĀ 1.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
from django.test import TestCase
2
2
from django.contrib.contenttypes.models import ContentType
3
 
from models import Link, Place, Restaurant
 
3
from django.db.models import Q
 
4
from models import *
4
5
 
5
6
class GenericRelationTests(TestCase):
6
 
    
 
7
 
7
8
    def test_inherited_models_content_type(self):
8
9
        """
9
10
        Test that GenericRelations on inherited classes use the correct content
10
11
        type.
11
12
        """
12
 
        
 
13
 
13
14
        p = Place.objects.create(name="South Park")
14
 
        r = Restaurant.objects.create(name="Chubby's")        
 
15
        r = Restaurant.objects.create(name="Chubby's")
15
16
        l1 = Link.objects.create(content_object=p)
16
17
        l2 = Link.objects.create(content_object=r)
17
18
        self.assertEqual(list(p.links.all()), [l1])
18
19
        self.assertEqual(list(r.links.all()), [l2])
19
 
        
 
 
b'\\ No newline at end of file'
 
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