summaryrefslogtreecommitdiff
path: root/tests/expressions_case
diff options
context:
space:
mode:
authorRyan Heard <ryanwheard@gmail.com>2020-05-19 00:47:56 -0500
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2020-05-27 07:32:23 +0200
commit587b179d4115f2e4dbc425d1ae035be9eb22197b (patch)
treeddfedcbf809a54c22314274b6d86103a0c280e93 /tests/expressions_case
parent2aac176e86204785f0f2ec4838049d8fed70870e (diff)
downloaddjango-587b179d4115f2e4dbc425d1ae035be9eb22197b.tar.gz
Fixed #31606 -- Allowed using condition with lookups in When() expression.
Diffstat (limited to 'tests/expressions_case')
-rw-r--r--tests/expressions_case/tests.py15
1 files changed, 14 insertions, 1 deletions
diff --git a/tests/expressions_case/tests.py b/tests/expressions_case/tests.py
index f85def932a..efb4ba227e 100644
--- a/tests/expressions_case/tests.py
+++ b/tests/expressions_case/tests.py
@@ -6,7 +6,7 @@ from uuid import UUID
from django.core.exceptions import FieldError
from django.db.models import (
- BinaryField, Case, CharField, Count, DurationField, F,
+ BinaryField, BooleanField, Case, CharField, Count, DurationField, F,
GenericIPAddressField, IntegerField, Max, Min, Q, Sum, TextField,
TimeField, UUIDField, Value, When,
)
@@ -312,6 +312,17 @@ class CaseExpressionTests(TestCase):
transform=attrgetter('integer', 'integer2')
)
+ def test_condition_with_lookups(self):
+ qs = CaseTestModel.objects.annotate(
+ test=Case(
+ When(Q(integer2=1), string='2', then=Value(False)),
+ When(Q(integer2=1), string='1', then=Value(True)),
+ default=Value(False),
+ output_field=BooleanField(),
+ ),
+ )
+ self.assertIs(qs.get(integer=1).test, True)
+
def test_case_reuse(self):
SOME_CASE = Case(
When(pk=0, then=Value('0')),
@@ -1351,6 +1362,8 @@ class CaseWhenTests(SimpleTestCase):
with self.assertRaisesMessage(TypeError, msg):
When(condition=Value(1, output_field=IntegerField()))
with self.assertRaisesMessage(TypeError, msg):
+ When(Value(1, output_field=IntegerField()), string='1')
+ with self.assertRaisesMessage(TypeError, msg):
When()
def test_empty_q_object(self):