From 755b3275525953a2c4145b8a95913d0fbb9982a4 Mon Sep 17 00:00:00 2001 From: Hannes Ljungberg Date: Thu, 26 Nov 2020 09:30:50 +0100 Subject: [PATCH] Added test for ArrayField's __contains lookup with subqueries. --- tests/postgres_tests/test_array.py | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/tests/postgres_tests/test_array.py b/tests/postgres_tests/test_array.py index 822e61f4e4..4d6f302b91 100644 --- a/tests/postgres_tests/test_array.py +++ b/tests/postgres_tests/test_array.py @@ -9,7 +9,7 @@ from django.core import checks, exceptions, serializers, validators from django.core.exceptions import FieldError from django.core.management import call_command from django.db import IntegrityError, connection, models -from django.db.models.expressions import RawSQL +from django.db.models.expressions import Exists, OuterRef, RawSQL from django.db.models.functions import Cast from django.test import TransactionTestCase, modify_settings, override_settings from django.test.utils import isolate_apps @@ -313,6 +313,19 @@ class TestQuerying(PostgreSQLTestCase): self.objs[1:3] ) + def test_contains_subquery(self): + IntegerArrayModel.objects.create(field=[2, 3]) + inner_qs = IntegerArrayModel.objects.values_list('field', flat=True) + self.assertSequenceEqual( + NullableIntegerArrayModel.objects.filter(field__contains=inner_qs[:1]), + self.objs[2:3], + ) + inner_qs = IntegerArrayModel.objects.filter(field__contains=OuterRef('field')) + self.assertSequenceEqual( + NullableIntegerArrayModel.objects.filter(Exists(inner_qs)), + self.objs[1:3], + ) + def test_icontains(self): # Using the __icontains lookup with ArrayField is inefficient. instance = CharArrayModel.objects.create(field=['FoO'])