Skip to content

Commit 02aaa94

Browse files
jmfedericovdboor
authored andcommitted
Test FilteredRelation usage
1 parent 15e2f34 commit 02aaa94

File tree

1 file changed

+34
-1
lines changed

1 file changed

+34
-1
lines changed

polymorphic/tests/test_orm.py

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
from django.contrib.contenttypes.models import ContentType
55
from django.db import models
6-
from django.db.models import Case, Count, Q, When
6+
from django.db.models import Case, Count, FilteredRelation, Q, When
77
from django.db.utils import IntegrityError
88
from django.test import TransactionTestCase
99

@@ -1064,6 +1064,39 @@ def ComplexAgg(expression):
10641064
):
10651065
Model2A.objects.aggregate(ComplexAgg("Model2B___field2"))
10661066

1067+
def test_polymorphic__filtered_relation(self):
1068+
""" test annotation using FilteredRelation """
1069+
1070+
blog = BlogA.objects.create(name="Ba1", info="i1 joined")
1071+
blog.blogentry_set.create(text="bla1 joined")
1072+
blog.blogentry_set.create(text="bla2 joined")
1073+
blog.blogentry_set.create(text="bla3 joined")
1074+
blog.blogentry_set.create(text="bla4")
1075+
blog.blogentry_set.create(text="bla5")
1076+
BlogA.objects.create(name="Ba2", info="i2 joined")
1077+
BlogA.objects.create(name="Ba3", info="i3")
1078+
BlogB.objects.create(name="Bb3")
1079+
1080+
result = BlogA.objects.annotate(
1081+
text_joined=FilteredRelation("blogentry", condition=Q(blogentry__text__contains="joined")),
1082+
).aggregate(Count("text_joined"))
1083+
self.assertEqual(result, {"text_joined__count": 3})
1084+
1085+
result = BlogA.objects.annotate(
1086+
text_joined=FilteredRelation("blogentry", condition=Q(blogentry__text__contains="joined")),
1087+
).aggregate(count=Count("text_joined"))
1088+
self.assertEqual(result, {"count": 3})
1089+
1090+
result = BlogBase.objects.annotate(
1091+
info_joined=FilteredRelation("bloga", condition=Q(BlogA___info__contains="joined")),
1092+
).aggregate(Count("info_joined"))
1093+
self.assertEqual(result, {"info_joined__count": 2})
1094+
1095+
result = BlogBase.objects.annotate(
1096+
info_joined=FilteredRelation("bloga", condition=Q(BlogA___info__contains="joined")),
1097+
).aggregate(count=Count("info_joined"))
1098+
self.assertEqual(result, {"count": 2})
1099+
10671100
def test_polymorphic__expressions(self):
10681101

10691102
from django.db.models.functions import Concat

0 commit comments

Comments
 (0)