|
@@ -1,6 +1,7 @@
|
|
|
from django.test import TestCase
|
|
|
from django.contrib.contenttypes.models import ContentType
|
|
|
-from models import Link, Place, Restaurant, Person, Address, CharLink, TextLink, OddRelation1, OddRelation2
|
|
|
+from django.db.models import Q
|
|
|
+from models import *
|
|
|
|
|
|
class GenericRelationTests(TestCase):
|
|
|
|
|
@@ -40,3 +41,34 @@ class GenericRelationTests(TestCase):
|
|
|
oddrel = OddRelation2.objects.create(name='tlink')
|
|
|
tl = TextLink.objects.create(content_object=oddrel)
|
|
|
oddrel.delete()
|
|
|
+
|
|
|
+ def test_q_object_or(self):
|
|
|
+ """
|
|
|
+ Tests that SQL query parameters for generic relations are properly
|
|
|
+ grouped when OR is used.
|
|
|
+
|
|
|
+ Test for bug http://code.djangoproject.com/ticket/11535
|
|
|
+
|
|
|
+ In this bug the first query (below) works while the second, with the
|
|
|
+ query parameters the same but in reverse order, does not.
|
|
|
+
|
|
|
+ The issue is that the generic relation conditions do not get properly
|
|
|
+ grouped in parentheses.
|
|
|
+ """
|
|
|
+ note_contact = Contact.objects.create()
|
|
|
+ org_contact = Contact.objects.create()
|
|
|
+ note = Note.objects.create(note='note', content_object=note_contact)
|
|
|
+ org = Organization.objects.create(name='org name')
|
|
|
+ org.contacts.add(org_contact)
|
|
|
+ # search with a non-matching note and a matching org name
|
|
|
+ qs = Contact.objects.filter(Q(notes__note__icontains=r'other note') |
|
|
|
+ Q(organizations__name__icontains=r'org name'))
|
|
|
+ self.assertTrue(org_contact in qs)
|
|
|
+ # search again, with the same query parameters, in reverse order
|
|
|
+ qs = Contact.objects.filter(
|
|
|
+ Q(organizations__name__icontains=r'org name') |
|
|
|
+ Q(notes__note__icontains=r'other note'))
|
|
|
+ self.assertTrue(org_contact in qs)
|
|
|
+
|
|
|
+
|
|
|
+
|