From 54f60bc85df7fe38ba6ef6779996d8544d340d3e Mon Sep 17 00:00:00 2001 From: Mariusz Felisiak Date: Wed, 17 Mar 2021 11:44:09 +0100 Subject: [PATCH] Refs #32548 -- Added tests for passing conditional expressions to Q(). --- tests/expressions/tests.py | 6 ++++++ tests/queryset_pickle/tests.py | 11 +++++++++++ 2 files changed, 17 insertions(+) diff --git a/tests/expressions/tests.py b/tests/expressions/tests.py index 9ecc033b6b..82d8a9f351 100644 --- a/tests/expressions/tests.py +++ b/tests/expressions/tests.py @@ -838,6 +838,12 @@ class BasicExpressionsTests(TestCase): with self.subTest(conditions): self.assertCountEqual(Employee.objects.filter(conditions), [self.max]) + def test_boolean_expression_in_Q(self): + is_poc = Company.objects.filter(point_of_contact=OuterRef('pk')) + self.gmbh.point_of_contact = self.max + self.gmbh.save() + self.assertCountEqual(Employee.objects.filter(Q(Exists(is_poc))), [self.max]) + class IterableLookupInnerExpressionsTests(TestCase): @classmethod diff --git a/tests/queryset_pickle/tests.py b/tests/queryset_pickle/tests.py index d0ae963cd9..bf61960419 100644 --- a/tests/queryset_pickle/tests.py +++ b/tests/queryset_pickle/tests.py @@ -172,6 +172,17 @@ class PickleabilityTestCase(TestCase): m2ms = pickle.loads(pickle.dumps(m2ms)) self.assertSequenceEqual(m2ms, [m2m]) + def test_pickle_boolean_expression_in_Q__queryset(self): + group = Group.objects.create(name='group') + Event.objects.create(title='event', group=group) + groups = Group.objects.filter( + models.Q(models.Exists( + Event.objects.filter(group_id=models.OuterRef('id')), + )), + ) + groups2 = pickle.loads(pickle.dumps(groups)) + self.assertSequenceEqual(groups2, [group]) + def test_pickle_exists_queryset_still_usable(self): group = Group.objects.create(name='group') Event.objects.create(title='event', group=group)