summaryrefslogtreecommitdiff
path: root/tests/postgres_tests
diff options
context:
space:
mode:
authorClaude Paroz <claude@2xlibre.net>2016-03-26 20:11:57 +0100
committerClaude Paroz <claude@2xlibre.net>2016-04-01 09:10:35 +0200
commit6a8ba2eef45ccc3022c88510bffd29734d71c39e (patch)
tree446f3986b40a06586b17771597d8fc81a35e3115 /tests/postgres_tests
parenta304e7ddff23b14b8e6546a92eb5250d9cf6eaff (diff)
[1.9.x] 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. Partial backport of db19619545 from master.
Diffstat (limited to 'tests/postgres_tests')
-rw-r--r--tests/postgres_tests/test_json.py29
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('[&quot;foo&quot;]</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('[&quot;foo&quot;]</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)