diff options
| author | Nick Pope <nick@nickpope.me.uk> | 2023-12-30 07:24:30 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-12-30 08:24:30 +0100 |
| commit | 94b6f101f7dc363a8e71593570b17527dbb9f77f (patch) | |
| tree | 431ddcb41b024af3a4fba136b53a210ad390cf4c /tests/postgres_tests/test_array.py | |
| parent | 561e16d6a7f07d8cadbdb6c72c68934dcd087d18 (diff) | |
Fixed #29049 -- Added slicing notation to F expressions.
Co-authored-by: Priyansh Saxena <askpriyansh@gmail.com>
Co-authored-by: Niclas Olofsson <n@niclasolofsson.se>
Co-authored-by: David Smith <smithdc@gmail.com>
Co-authored-by: Mariusz Felisiak <felisiak.mariusz@gmail.com>
Co-authored-by: Abhinav Yadav <abhinav.sny.2002@gmail.com>
Diffstat (limited to 'tests/postgres_tests/test_array.py')
| -rw-r--r-- | tests/postgres_tests/test_array.py | 36 |
1 files changed, 35 insertions, 1 deletions
diff --git a/tests/postgres_tests/test_array.py b/tests/postgres_tests/test_array.py index 8aaa7be077..386a0afa3a 100644 --- a/tests/postgres_tests/test_array.py +++ b/tests/postgres_tests/test_array.py @@ -10,7 +10,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 Exists, OuterRef, RawSQL, Value +from django.db.models.expressions import Exists, F, OuterRef, RawSQL, Value from django.db.models.functions import Cast, JSONObject, Upper from django.test import TransactionTestCase, override_settings, skipUnlessDBFeature from django.test.utils import isolate_apps @@ -594,6 +594,40 @@ class TestQuerying(PostgreSQLTestCase): [None, [1], [2], [2, 3], [20, 30]], ) + def test_slicing_of_f_expressions(self): + tests = [ + (F("field")[:2], [1, 2]), + (F("field")[2:], [3, 4]), + (F("field")[1:3], [2, 3]), + (F("field")[3], [4]), + (F("field")[:3][1:], [2, 3]), # Nested slicing. + (F("field")[:3][1], [2]), # Slice then index. + ] + for expression, expected in tests: + with self.subTest(expression=expression, expected=expected): + instance = IntegerArrayModel.objects.create(field=[1, 2, 3, 4]) + instance.field = expression + instance.save() + instance.refresh_from_db() + self.assertEqual(instance.field, expected) + + def test_slicing_of_f_expressions_with_annotate(self): + IntegerArrayModel.objects.create(field=[1, 2, 3]) + annotated = IntegerArrayModel.objects.annotate( + first_two=F("field")[:2], + after_two=F("field")[2:], + random_two=F("field")[1:3], + ).get() + self.assertEqual(annotated.first_two, [1, 2]) + self.assertEqual(annotated.after_two, [3]) + self.assertEqual(annotated.random_two, [2, 3]) + + def test_slicing_of_f_expressions_with_len(self): + queryset = NullableIntegerArrayModel.objects.annotate( + subarray=F("field")[:1] + ).filter(field__len=F("subarray__len")) + self.assertSequenceEqual(queryset, self.objs[:2]) + def test_usage_in_subquery(self): self.assertSequenceEqual( NullableIntegerArrayModel.objects.filter( |
