diff options
| author | Claude Paroz <claude@2xlibre.net> | 2016-03-26 20:11:57 +0100 |
|---|---|---|
| committer | Claude Paroz <claude@2xlibre.net> | 2016-04-01 09:04:20 +0200 |
| commit | db19619545dd99a1d2502c72974d79eca33acff7 (patch) | |
| tree | 114ee5c154d19d49d06d4f6e9e1deb4e2d4e7039 /tests/postgres_tests | |
| parent | 64aba7a8aba06b8be52a1a099b44e1d3be4bdd26 (diff) | |
Fixed #25532 -- Properly redisplayed JSONField form input values
Thanks David Szotten for the report and Tommy Beadle for code inspiration.
Thanks Tim Graham for the review.
Diffstat (limited to 'tests/postgres_tests')
| -rw-r--r-- | tests/postgres_tests/test_json.py | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/tests/postgres_tests/test_json.py b/tests/postgres_tests/test_json.py index 59e5fe3fe7..11b6a349aa 100644 --- a/tests/postgres_tests/test_json.py +++ b/tests/postgres_tests/test_json.py @@ -3,7 +3,9 @@ import unittest from django.core import exceptions, serializers from django.db import connection +from django.forms import CharField, Form from django.test import TestCase +from django.utils.html import escape from . import PostgreSQLTestCase from .models import JSONModel @@ -258,7 +260,34 @@ class TestFormField(PostgreSQLTestCase): form_field = model_field.formfield() self.assertIsInstance(form_field, forms.JSONField) + def test_formfield_disabled(self): + class JsonForm(Form): + name = CharField() + jfield = forms.JSONField(disabled=True) + + form = JsonForm({'name': 'xyz', 'jfield': '["bar"]'}, initial={'jfield': ['foo']}) + self.assertIn('["foo"]</textarea>', form.as_p()) + def test_prepare_value(self): field = forms.JSONField() self.assertEqual(field.prepare_value({'a': 'b'}), '{"a": "b"}') self.assertEqual(field.prepare_value(None), 'null') + self.assertEqual(field.prepare_value('foo'), '"foo"') + + def test_redisplay_wrong_input(self): + """ + When displaying a bound form (typically due to invalid input), the form + should not overquote JSONField inputs. + """ + class JsonForm(Form): + name = CharField(max_length=2) + jfield = forms.JSONField() + + # JSONField input is fine, name is too long + form = JsonForm({'name': 'xyz', 'jfield': '["foo"]'}) + self.assertIn('["foo"]</textarea>', form.as_p()) + + # This time, the JSONField input is wrong + form = JsonForm({'name': 'xy', 'jfield': '{"foo"}'}) + # Appears once in the textarea and once in the error message + self.assertEqual(form.as_p().count(escape('{"foo"}')), 2) |
