summaryrefslogtreecommitdiff
path: root/tests/generic_relations_regress
diff options
context:
space:
mode:
authorAnssi Kääriäinen <akaariai@gmail.com>2013-11-05 20:02:24 +0200
committerAnssi Kääriäinen <akaariai@gmail.com>2013-11-05 20:02:24 +0200
commit76da053641e52db540801e18b362497c01e9bb1d (patch)
treec961e9a5a75e30a3a9ad89bb863bf19f625d07ae /tests/generic_relations_regress
parent8a0489221eb20f9687125cabda64bb16ea3bad0b (diff)
Fixed #10461 -- bug in generic relation + annotate() case
This issue was fixed when the contenttype restriction was moved from where clause to the join clause. So, this is tests only addition.
Diffstat (limited to 'tests/generic_relations_regress')
-rw-r--r--tests/generic_relations_regress/tests.py25
1 files changed, 24 insertions, 1 deletions
diff --git a/tests/generic_relations_regress/tests.py b/tests/generic_relations_regress/tests.py
index d7a43846bb..285bd43c47 100644
--- a/tests/generic_relations_regress/tests.py
+++ b/tests/generic_relations_regress/tests.py
@@ -1,4 +1,4 @@
-from django.db.models import Q
+from django.db.models import Q, Sum
from django.db.utils import IntegrityError
from django.test import TestCase, skipIfDBFeature
@@ -213,3 +213,26 @@ class GenericRelationTests(TestCase):
# B would then fail).
self.assertNotIn(" join ", str(B.objects.exclude(a__flag=True).query).lower())
self.assertIn("content_type_id", str(B.objects.exclude(a__flag=True).query).lower())
+
+ def test_annotate(self):
+ hs1 = HasLinkThing.objects.create()
+ b = Board.objects.create(name=str(hs1.pk))
+ l = Link.objects.create(content_object=hs1)
+ Link.objects.create(content_object=b)
+ qs = HasLinkThing.objects.annotate(Sum('links'))
+ # If content_type restriction isn't in the query's join condition,
+ # then wrong results are produced here as b will also match (it has
+ # same pk).
+ self.assertEqual(qs.count(), 1)
+ self.assertEqual(qs[0].links__sum, 1)
+ l.delete()
+ # Now if we don't have proper left join, we will not produce any
+ # results at all here.
+ # clear cached results
+ qs = qs.all()
+ self.assertEqual(qs.count(), 1)
+ # Note - 0 here would be a nicer result...
+ self.assertIs(qs[0].links__sum, None)
+ # Finally test that filtering works.
+ self.assertEqual(qs.filter(links__sum__isnull=True).count(), 1)
+ self.assertEqual(qs.filter(links__sum__isnull=False).count(), 0)