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

Code before:
        obj = NullableJSONModel.objects.create(value={"key": None})
        self.assertSequenceEqual(
            NullableJSONModel.objects.filter(value__key=JSONNull()),
            [obj],
        )
        self.assertSequenceEqual(
            NullableJSONModel.objects.filter(value__key=None), [obj]
        )

    def test_index_lookup(self):
        obj = NullableJSONModel.objects.create(value=["a", "b", None, 3])
        self.assertSequenceEqual(
            NullableJSONModel.objects.filter(value__2=JSONNull()), [obj]
        )
        self.assertSequenceEqual(NullableJSONModel.objects.filter(value__2=None), [obj])

    @skipUnlessDBFeature("supports_table_check_constraints")
    def test_constraint_validation(self):
        constraint = CheckConstraint(
            condition=~Q(value=JSONNull()), name="check_not_json_null"
        )
        constraint.validate(NullableJSONModel, NullableJSONModel(value={"key": None}))
        msg = f"Constraint “{constraint.name}” is violated."
        with self.assertRaisesMessage(ValidationError, msg):
            constraint.validate(NullableJSONModel, NullableJSONModel(value=JSONNull()))

    def test_constraint_validation_key_transform(self):
        constraint = CheckConstraint(
            condition=Q(value__has_key="name") & ~Q(value__name=JSONNull()),
            name="check_value_name_not_json_null",
        )
        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)
        self.assertSequenceEqual(
            JSONNullDefaultModel.objects.filter(value__isnull=True), []
        )

    def test_filter_in(self):
        obj = NullableJSONModel.objects.create(value=JSONNull())
        self.assertSequenceEqual(
            NullableJSONModel.objects.filter(value__in=[JSONNull()]),

Review comment (suggestion):
```suggestion
    @skipUnlessDBFeature("supports_table_check_constraints")
    def test_constraint_validation_key_transform(self):
```

Code after:
            NullableJSONModel.objects.filter(value__key=JSONNull()),
            [obj],
        )
        self.assertSequenceEqual(
            NullableJSONModel.objects.filter(value__key=None), [obj]
        )

    def test_index_lookup(self):
        obj = NullableJSONModel.objects.create(value=["a", "b", None, 3])
        self.assertSequenceEqual(
            NullableJSONModel.objects.filter(value__2=JSONNull()), [obj]
        )
        self.assertSequenceEqual(NullableJSONModel.objects.filter(value__2=None), [obj])

    @skipUnlessDBFeature("supports_table_check_constraints")
    def test_constraint_validation(self):
        constraint = CheckConstraint(
            condition=~Q(value=JSONNull()), name="check_not_json_null"
        )
        constraint.validate(NullableJSONModel, NullableJSONModel(value={"key": None}))
        msg = f"Constraint “{constraint.name}” is violated."
        with self.assertRaisesMessage(ValidationError, msg):
            constraint.validate(NullableJSONModel, NullableJSONModel(value=JSONNull()))

    @skipUnlessDBFeature("supports_table_check_constraints")
    def test_constraint_validation_key_transform(self):
        constraint = CheckConstraint(
            condition=Q(value__has_key="name") & ~Q(value__name=JSONNull()),
            name="check_value_name_not_json_null",
        )
        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])