Repository: django/django
File: tests/model_fields/test_jsonfield.py (Python)

Code before:
        constraint.validate(
            NullableJSONModel, NullableJSONModel(value={"name": "Django"})
        )
        msg = f"Constraint “{constraint.name}” is violated."
        with self.assertRaisesMessage(ValidationError, msg):
            constraint.validate(
                NullableJSONModel, NullableJSONModel(value={"name": None})
            )

    def test_default(self):
        obj = JSONNullDefaultModel.objects.create()
        self.assertIsNone(obj.value)

    def test_custom_jsonnull_encoder(self):
        obj = JSONNullDefaultModel.objects.create(
            value={"name": JSONNull(), "array": [1, JSONNull()]}
        )
        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):
    def setUp(self):
        self.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)."
        )
        self.obj = NullableJSONModel.objects.create(value=JSONNull())
        return super().setUp()

    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)

Review comment (suggestion):
We usually use `setUpTestData` to avoid recreating an object every time.

Code after:
            )

    def test_default(self):
        obj = JSONNullDefaultModel.objects.create()
        self.assertIsNone(obj.value)

    def test_custom_jsonnull_encoder(self):
        obj = JSONNullDefaultModel.objects.create(
            value={"name": JSONNull(), "array": [1, JSONNull()]}
        )
        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])