From f6e1789654e82bac08cead5a2d2a9132f6403f52 Mon Sep 17 00:00:00 2001 From: Flávio Juvenal Date: Thu, 17 Aug 2017 16:21:35 -0700 Subject: Fixed #28577 -- Added checks for ArrayField and JSONField to prevent mutable defaults. --- tests/postgres_tests/test_json.py | 41 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 39 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 cdabca04d6..acbd855f1a 100644 --- a/tests/postgres_tests/test_json.py +++ b/tests/postgres_tests/test_json.py @@ -2,13 +2,14 @@ import datetime import uuid from decimal import Decimal -from django.core import exceptions, serializers +from django.core import checks, exceptions, serializers from django.core.serializers.json import DjangoJSONEncoder from django.forms import CharField, Form, widgets +from django.test.utils import isolate_apps from django.utils.html import escape from . import PostgreSQLTestCase -from .models import JSONModel +from .models import JSONModel, PostgreSQLModel try: from django.contrib.postgres import forms @@ -259,6 +260,42 @@ class TestQuerying(PostgreSQLTestCase): self.assertTrue(JSONModel.objects.filter(field__foo__iregex=r'^bAr$').exists()) +@isolate_apps('postgres_tests') +class TestChecks(PostgreSQLTestCase): + + def test_invalid_default(self): + class MyModel(PostgreSQLModel): + field = JSONField(default={}) + + model = MyModel() + self.assertEqual(model.check(), [ + checks.Warning( + msg=( + "JSONField default should be a callable instead of an " + "instance so that it's not shared between all field " + "instances." + ), + hint='Use a callable instead, e.g., use `dict` instead of `{}`.', + obj=MyModel._meta.get_field('field'), + id='postgres.E003', + ) + ]) + + def test_valid_default(self): + class MyModel(PostgreSQLModel): + field = JSONField(default=dict) + + model = MyModel() + self.assertEqual(model.check(), []) + + def test_valid_default_none(self): + class MyModel(PostgreSQLModel): + field = JSONField(default=None) + + model = MyModel() + self.assertEqual(model.check(), []) + + class TestSerialization(PostgreSQLTestCase): test_data = ( '[{"fields": {"field": {"a": "b", "c": null}, "field_custom": null}, ' -- cgit v1.3