mirror of
https://github.com/django/django.git
synced 2025-10-24 06:06:09 +00:00
Fixed #24766 -- Added join promotion for Case expressions
This commit is contained in:
committed by
Tim Graham
parent
8e86d9d3df
commit
be9d645346
@@ -1016,6 +1016,38 @@ class CaseExpressionTests(TestCase):
|
||||
transform=attrgetter('integer', 'test')
|
||||
)
|
||||
|
||||
def test_join_promotion(self):
|
||||
o = CaseTestModel.objects.create(integer=1, integer2=1, string='1')
|
||||
# Testing that:
|
||||
# 1. There isn't any object on the remote side of the fk_rel
|
||||
# relation. If the query used inner joins, then the join to fk_rel
|
||||
# would remove o from the results. So, in effect we are testing that
|
||||
# we are promoting the fk_rel join to a left outer join here.
|
||||
# 2. The default value of 3 is generated for the case expression.
|
||||
self.assertQuerysetEqual(
|
||||
CaseTestModel.objects.filter(pk=o.pk).annotate(
|
||||
foo=Case(
|
||||
When(fk_rel__pk=1, then=2),
|
||||
default=3,
|
||||
output_field=models.IntegerField()
|
||||
),
|
||||
),
|
||||
[(o, 3)],
|
||||
lambda x: (x, x.foo)
|
||||
)
|
||||
# Now 2 should be generated, as the fk_rel is null.
|
||||
self.assertQuerysetEqual(
|
||||
CaseTestModel.objects.filter(pk=o.pk).annotate(
|
||||
foo=Case(
|
||||
When(fk_rel__isnull=True, then=2),
|
||||
default=3,
|
||||
output_field=models.IntegerField()
|
||||
),
|
||||
),
|
||||
[(o, 2)],
|
||||
lambda x: (x, x.foo)
|
||||
)
|
||||
|
||||
|
||||
class CaseDocumentationExamples(TestCase):
|
||||
@classmethod
|
||||
|
||||
Reference in New Issue
Block a user