1
0
mirror of https://github.com/django/django.git synced 2025-10-23 21:59:11 +00:00

Refs #28577 -- Added check for HStoreField to prevent mutable default.

This commit is contained in:
Tim Graham
2018-04-03 11:12:56 -04:00
committed by GitHub
parent 167d98528a
commit 4f7467b690
2 changed files with 35 additions and 4 deletions

View File

@@ -1,11 +1,11 @@
import json
from django.core import exceptions, serializers
from django.core import checks, exceptions, serializers
from django.forms import Form
from django.test.utils import modify_settings
from django.test.utils import isolate_apps, modify_settings
from . import PostgreSQLTestCase
from .models import HStoreModel
from .models import HStoreModel, PostgreSQLModel
try:
from django.contrib.postgres import forms
@@ -190,6 +190,34 @@ class TestQuerying(HStoreTestCase):
)
@isolate_apps('postgres_tests')
class TestChecks(PostgreSQLTestCase):
def test_invalid_default(self):
class MyModel(PostgreSQLModel):
field = HStoreField(default={})
model = MyModel()
self.assertEqual(model.check(), [
checks.Warning(
msg=(
"HStoreField 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 = HStoreField(default=dict)
self.assertEqual(MyModel().check(), [])
class TestSerialization(HStoreTestCase):
test_data = json.dumps([{
'model': 'postgres_tests.hstoremodel',