Added tests for using ArrayField's IndexTransform/SliceTransform on expressions with params.

This commit is contained in:
Mariusz Felisiak 2019-08-02 12:39:15 +02:00
parent 25f21bd237
commit 0e02e496cd
1 changed files with 22 additions and 0 deletions

View File

@ -9,6 +9,8 @@ from django.core import checks, exceptions, serializers, validators
from django.core.exceptions import FieldError from django.core.exceptions import FieldError
from django.core.management import call_command from django.core.management import call_command
from django.db import IntegrityError, connection, models from django.db import IntegrityError, connection, models
from django.db.models.expressions import RawSQL
from django.db.models.functions import Cast
from django.test import TransactionTestCase, modify_settings, override_settings from django.test import TransactionTestCase, modify_settings, override_settings
from django.test.utils import isolate_apps from django.test.utils import isolate_apps
from django.utils import timezone from django.utils import timezone
@ -24,6 +26,7 @@ from .models import (
try: try:
from django.contrib.postgres.fields import ArrayField from django.contrib.postgres.fields import ArrayField
from django.contrib.postgres.fields.array import IndexTransform, SliceTransform
from django.contrib.postgres.forms import ( from django.contrib.postgres.forms import (
SimpleArrayField, SplitArrayField, SplitArrayWidget, SimpleArrayField, SplitArrayField, SplitArrayWidget,
) )
@ -304,6 +307,18 @@ class TestQuerying(PostgreSQLTestCase):
[instance] [instance]
) )
def test_index_transform_expression(self):
expr = RawSQL("string_to_array(%s, ';')", ['1;2'])
self.assertSequenceEqual(
NullableIntegerArrayModel.objects.filter(
field__0=Cast(
IndexTransform(1, models.IntegerField, expr),
output_field=models.IntegerField(),
),
),
self.objs[:1],
)
def test_overlap(self): def test_overlap(self):
self.assertSequenceEqual( self.assertSequenceEqual(
NullableIntegerArrayModel.objects.filter(field__overlap=[1, 2]), NullableIntegerArrayModel.objects.filter(field__overlap=[1, 2]),
@ -358,6 +373,13 @@ class TestQuerying(PostgreSQLTestCase):
[instance] [instance]
) )
def test_slice_transform_expression(self):
expr = RawSQL("string_to_array(%s, ';')", ['9;2;3'])
self.assertSequenceEqual(
NullableIntegerArrayModel.objects.filter(field__0_2=SliceTransform(2, 3, expr)),
self.objs[2:3],
)
def test_usage_in_subquery(self): def test_usage_in_subquery(self):
self.assertSequenceEqual( self.assertSequenceEqual(
NullableIntegerArrayModel.objects.filter( NullableIntegerArrayModel.objects.filter(