From a843a9ba8da40ffae49c9ec13e7b5c41d5e40ca5 Mon Sep 17 00:00:00 2001 From: Louise Grandjonc Date: Tue, 1 Oct 2019 16:25:40 -0700 Subject: [1.11.x] Fixed #30826 -- Fixed crash of many JSONField lookups when one hand side is key transform. Regression in 6c3dfba89215fc56fc27ef61829a6fff88be4abb. Backport of 7d1bf29977bb368d7c28e7c6eb146db3b3009ae7 from master. --- tests/postgres_tests/test_json.py | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) (limited to 'tests/postgres_tests/test_json.py') diff --git a/tests/postgres_tests/test_json.py b/tests/postgres_tests/test_json.py index b8b5dd481f..faefcd18a3 100644 --- a/tests/postgres_tests/test_json.py +++ b/tests/postgres_tests/test_json.py @@ -21,7 +21,9 @@ 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 + from django.contrib.postgres.fields.jsonb import ( + KeyTextTransform, KeyTransform + ) except ImportError: pass @@ -130,7 +132,12 @@ class TestQuerying(PostgreSQLTestCase): 'k': True, 'l': False, }), - JSONModel.objects.create(field={'foo': 'bar'}), + JSONModel.objects.create(field={ + 'foo': 'bar', + 'baz': {'a': 'b', 'c': 'd'}, + 'bar': ['foo', 'bar'], + 'bax': {'foo': 'bar'}, + }), ] def test_exact(self): @@ -305,6 +312,25 @@ class TestQuerying(PostgreSQLTestCase): queries[0]['sql'], ) + def test_lookups_with_key_transform(self): + tests = ( + ('field__d__contains', 'e'), + ('field__baz__contained_by', {'a': 'b', 'c': 'd', 'e': 'f'}), + ('field__baz__has_key', 'c'), + ('field__baz__has_keys', ['a', 'c']), + ('field__baz__has_any_keys', ['a', 'x']), + ('field__contains', KeyTransform('bax', 'field')), + ( + 'field__contained_by', + KeyTransform('x', RawSQL('%s::jsonb', ['{"x": {"a": "b", "c": 1, "d": "e"}}'])), + ), + ('field__has_key', KeyTextTransform('foo', 'field')), + ) + for lookup, value in tests: + self.assertTrue(JSONModel.objects.filter( + **{lookup: value} + ).exists()) + @skipUnlessDBFeature('has_jsonb_datatype') class TestSerialization(PostgreSQLTestCase): -- cgit v1.3