summaryrefslogtreecommitdiff
path: root/tests/postgres_tests/test_json.py
diff options
context:
space:
mode:
authorMariusz Felisiak <felisiak.mariusz@gmail.com>2019-08-14 15:25:35 +0200
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2019-08-14 15:58:10 +0200
commit473c526b1b014e73e139665db2ddbbcee23bb826 (patch)
treebd9122b6fafc0c2a410a6521dd8223e4c7d8d31d /tests/postgres_tests/test_json.py
parent3deda1f680ef34a753f6d872813737f363cb4886 (diff)
[1.11.x] Fixed #30672 -- Fixed crash of JSONField/HStoreField key transforms on expressions with params.
Regression in 4f5b58f5cd3c57fee9972ab074f8dc6895d8f387. Thanks Florian Apolloner for the report and helping with tests. Backport of 1f8382d34d54061eddc41df6994e20ee38c60907 from master.
Diffstat (limited to 'tests/postgres_tests/test_json.py')
-rw-r--r--tests/postgres_tests/test_json.py22
1 files changed, 22 insertions, 0 deletions
diff --git a/tests/postgres_tests/test_json.py b/tests/postgres_tests/test_json.py
index 925e800131..ead2c68df7 100644
--- a/tests/postgres_tests/test_json.py
+++ b/tests/postgres_tests/test_json.py
@@ -7,6 +7,9 @@ from decimal import Decimal
from django.core import exceptions, serializers
from django.core.serializers.json import DjangoJSONEncoder
from django.db import connection
+from django.db.models import F
+from django.db.models.expressions import RawSQL
+from django.db.models.functions import Cast
from django.forms import CharField, Form, widgets
from django.test import skipUnlessDBFeature
from django.test.utils import CaptureQueriesContext
@@ -18,6 +21,7 @@ from .models import JSONModel
try:
from django.contrib.postgres import forms
from django.contrib.postgres.fields import JSONField
+ from django.contrib.postgres.fields.jsonb import KeyTransform
except ImportError:
pass
@@ -147,6 +151,24 @@ class TestQuerying(PostgreSQLTestCase):
[self.objs[0]]
)
+ def test_key_transform_raw_expression(self):
+ expr = RawSQL('%s::jsonb', ['{"x": "bar"}'])
+ self.assertSequenceEqual(
+ JSONModel.objects.filter(field__foo=KeyTransform('x', expr)),
+ [self.objs[-1]],
+ )
+
+ def test_key_transform_expression(self):
+ self.assertSequenceEqual(
+ JSONModel.objects.filter(field__d__0__isnull=False).annotate(
+ key=KeyTransform('d', 'field'),
+ ).annotate(
+ chain=KeyTransform('0', 'key'),
+ expr=KeyTransform('0', Cast('key', JSONField())),
+ ).filter(chain=F('expr')),
+ [self.objs[8]],
+ )
+
def test_isnull_key(self):
# key__isnull works the same as has_key='key'.
self.assertSequenceEqual(