From 348ca845385beaddc7c862ff8ec369f041a5088d Mon Sep 17 00:00:00 2001 From: Clifford Gama Date: Fri, 24 Oct 2025 23:38:52 +0200 Subject: Refs #35381 -- Deprecated using None in JSONExact rhs to mean JSON null. Key and index lookups are exempt from the deprecation. Co-authored-by: Jacob Walls --- tests/model_fields/test_jsonfield.py | 47 +++++++++++++++++++++++++++++++++++- tests/postgres_tests/test_array.py | 12 +++++++++ 2 files changed, 58 insertions(+), 1 deletion(-) (limited to 'tests') diff --git a/tests/model_fields/test_jsonfield.py b/tests/model_fields/test_jsonfield.py index fd2a880f99..937b557794 100644 --- a/tests/model_fields/test_jsonfield.py +++ b/tests/model_fields/test_jsonfield.py @@ -41,8 +41,15 @@ from django.db.models.fields.json import ( KeyTransformTextLookupMixin, ) from django.db.models.functions import Cast -from django.test import SimpleTestCase, TestCase, skipIfDBFeature, skipUnlessDBFeature +from django.test import ( + SimpleTestCase, + TestCase, + ignore_warnings, + skipIfDBFeature, + skipUnlessDBFeature, +) from django.test.utils import CaptureQueriesContext +from django.utils.deprecation import RemovedInDjango70Warning from .models import ( CustomJSONDecoder, @@ -229,6 +236,8 @@ class TestSaveLoad(TestCase): self.assertIsNone(obj.value) @skipUnlessDBFeature("supports_primitives_in_json_field") + # RemovedInDjango70Warning. + @ignore_warnings(category=RemovedInDjango70Warning) def test_json_null_different_from_sql_null(self): json_null = NullableJSONModel.objects.create(value=Value(None, JSONField())) NullableJSONModel.objects.update(value=Value(None, JSONField())) @@ -242,6 +251,9 @@ class TestSaveLoad(TestCase): ) self.assertSequenceEqual( NullableJSONModel.objects.filter(value=None), + # RemovedInDjango70Warning: When the deprecation ends, replace + # with: + # [sql_null], [json_null], ) self.assertSequenceEqual( @@ -1365,3 +1377,36 @@ class JSONNullTests(TestCase): obj.refresh_from_db() self.assertIsNone(obj.value["name"]) self.assertEqual(obj.value["array"], [1, None]) + + +# RemovedInDjango70Warning. +@skipUnlessDBFeature("supports_primitives_in_json_field") +class JSONExactNoneDeprecationTests(TestCase): + @classmethod + def setUpTestData(cls): + cls.msg = ( + "Using None as the right-hand side of an exact lookup on JSONField to mean " + "JSON scalar 'null' is deprecated. Use JSONNull() instead (or use the " + "__isnull lookup if you meant SQL NULL)." + ) + cls.obj = NullableJSONModel.objects.create(value=JSONNull()) + + def test_filter(self): + with self.assertWarnsMessage(RemovedInDjango70Warning, self.msg): + self.assertSequenceEqual( + NullableJSONModel.objects.filter(value=None), [self.obj] + ) + + def test_annotation_q_filter(self): + qs = NullableJSONModel.objects.annotate( + has_empty_data=Q(value__isnull=True) | Q(value=None) + ).filter(has_empty_data=True) + with self.assertWarnsMessage(RemovedInDjango70Warning, self.msg): + self.assertSequenceEqual(qs, [self.obj]) + + def test_case_when(self): + qs = NullableJSONModel.objects.annotate( + has_json_null=Case(When(value=None, then=Value(True)), default=Value(False)) + ).filter(has_json_null=True) + with self.assertWarnsMessage(RemovedInDjango70Warning, self.msg): + self.assertSequenceEqual(qs, [self.obj]) diff --git a/tests/postgres_tests/test_array.py b/tests/postgres_tests/test_array.py index e65009ad83..f35211e8ed 100644 --- a/tests/postgres_tests/test_array.py +++ b/tests/postgres_tests/test_array.py @@ -16,6 +16,7 @@ from django.db.models.functions import Cast, JSONObject, Upper from django.test import TransactionTestCase, override_settings, skipUnlessDBFeature from django.test.utils import isolate_apps from django.utils import timezone +from django.utils.deprecation import RemovedInDjango70Warning from . import PostgreSQLSimpleTestCase, PostgreSQLTestCase, PostgreSQLWidgetTestCase from .models import ( @@ -1586,6 +1587,17 @@ class TestJSONFieldQuerying(PostgreSQLTestCase): self.assertSequenceEqual( OtherTypesArrayModel.objects.filter(json__1__isnull=True), [obj] ) + # RemovedInDjango70Warning. + msg = ( + "Using None as the right-hand side of an exact lookup on JSONField to mean " + "JSON scalar 'null' is deprecated. Use JSONNull() instead (or use the " + "__isnull lookup if you meant SQL NULL)." + ) + with self.assertWarnsMessage(RemovedInDjango70Warning, msg): + # RemovedInDjango70Warning: deindent, and replace [] with [obj]. + self.assertSequenceEqual( + OtherTypesArrayModel.objects.filter(json__1=None), [] + ) def test_saving_and_querying_for_json_null(self): obj = OtherTypesArrayModel.objects.create(json=[JSONNull(), JSONNull()]) -- cgit v1.3