summaryrefslogtreecommitdiff
path: root/tests/forms_tests/field_tests/test_jsonfield.py
diff options
context:
space:
mode:
Diffstat (limited to 'tests/forms_tests/field_tests/test_jsonfield.py')
-rw-r--r--tests/forms_tests/field_tests/test_jsonfield.py71
1 files changed, 39 insertions, 32 deletions
diff --git a/tests/forms_tests/field_tests/test_jsonfield.py b/tests/forms_tests/field_tests/test_jsonfield.py
index b4f3e74f6a..be2b077e64 100644
--- a/tests/forms_tests/field_tests/test_jsonfield.py
+++ b/tests/forms_tests/field_tests/test_jsonfield.py
@@ -3,7 +3,12 @@ import uuid
from django.core.serializers.json import DjangoJSONEncoder
from django.forms import (
- CharField, Form, JSONField, Textarea, TextInput, ValidationError,
+ CharField,
+ Form,
+ JSONField,
+ Textarea,
+ TextInput,
+ ValidationError,
)
from django.test import SimpleTestCase
@@ -12,25 +17,25 @@ class JSONFieldTest(SimpleTestCase):
def test_valid(self):
field = JSONField()
value = field.clean('{"a": "b"}')
- self.assertEqual(value, {'a': 'b'})
+ self.assertEqual(value, {"a": "b"})
def test_valid_empty(self):
field = JSONField(required=False)
- self.assertIsNone(field.clean(''))
+ self.assertIsNone(field.clean(""))
self.assertIsNone(field.clean(None))
def test_invalid(self):
field = JSONField()
- with self.assertRaisesMessage(ValidationError, 'Enter a valid JSON.'):
- field.clean('{some badly formed: json}')
+ with self.assertRaisesMessage(ValidationError, "Enter a valid JSON."):
+ field.clean("{some badly formed: json}")
def test_prepare_value(self):
field = JSONField()
- self.assertEqual(field.prepare_value({'a': 'b'}), '{"a": "b"}')
- self.assertEqual(field.prepare_value(None), 'null')
- self.assertEqual(field.prepare_value('foo'), '"foo"')
- self.assertEqual(field.prepare_value('你好,世界'), '"你好,世界"')
- self.assertEqual(field.prepare_value({'a': '😀🐱'}), '{"a": "😀🐱"}')
+ self.assertEqual(field.prepare_value({"a": "b"}), '{"a": "b"}')
+ self.assertEqual(field.prepare_value(None), "null")
+ self.assertEqual(field.prepare_value("foo"), '"foo"')
+ self.assertEqual(field.prepare_value("你好,世界"), '"你好,世界"')
+ self.assertEqual(field.prepare_value({"a": "😀🐱"}), '{"a": "😀🐱"}')
self.assertEqual(
field.prepare_value(["你好,世界", "jaźń"]),
'["你好,世界", "jaźń"]',
@@ -46,6 +51,7 @@ class JSONFieldTest(SimpleTestCase):
def test_custom_widget_attribute(self):
"""The widget can be overridden with an attribute."""
+
class CustomJSONField(JSONField):
widget = TextInput
@@ -57,12 +63,12 @@ class JSONFieldTest(SimpleTestCase):
tests = [
'["a", "b", "c"]',
'{"a": 1, "b": 2}',
- '1',
- '1.5',
+ "1",
+ "1.5",
'"foo"',
- 'true',
- 'false',
- 'null',
+ "true",
+ "false",
+ "null",
]
for json_string in tests:
with self.subTest(json_string=json_string):
@@ -71,8 +77,8 @@ class JSONFieldTest(SimpleTestCase):
def test_has_changed(self):
field = JSONField()
- self.assertIs(field.has_changed({'a': True}, '{"a": 1}'), True)
- self.assertIs(field.has_changed({'a': 1, 'b': 2}, '{"b": 2, "a": 1}'), False)
+ self.assertIs(field.has_changed({"a": True}, '{"a": 1}'), True)
+ self.assertIs(field.has_changed({"a": 1, "b": 2}, '{"b": 2, "a": 1}'), False)
def test_custom_encoder_decoder(self):
class CustomDecoder(json.JSONDecoder):
@@ -80,11 +86,11 @@ class JSONFieldTest(SimpleTestCase):
return super().__init__(object_hook=self.as_uuid, *args, **kwargs)
def as_uuid(self, dct):
- if 'uuid' in dct:
- dct['uuid'] = uuid.UUID(dct['uuid'])
+ if "uuid" in dct:
+ dct["uuid"] = uuid.UUID(dct["uuid"])
return dct
- value = {'uuid': uuid.UUID('{c141e152-6550-4172-a784-05448d98204b}')}
+ value = {"uuid": uuid.UUID("{c141e152-6550-4172-a784-05448d98204b}")}
encoded_value = '{"uuid": "c141e152-6550-4172-a784-05448d98204b"}'
field = JSONField(encoder=DjangoJSONEncoder, decoder=CustomDecoder)
self.assertEqual(field.prepare_value(value), encoded_value)
@@ -94,8 +100,8 @@ class JSONFieldTest(SimpleTestCase):
class JSONForm(Form):
json_field = JSONField(disabled=True)
- form = JSONForm({'json_field': '["bar"]'}, initial={'json_field': ['foo']})
- self.assertIn('[&quot;foo&quot;]</textarea>', form.as_p())
+ form = JSONForm({"json_field": '["bar"]'}, initial={"json_field": ["foo"]})
+ self.assertIn("[&quot;foo&quot;]</textarea>", form.as_p())
def test_redisplay_none_input(self):
class JSONForm(Form):
@@ -103,29 +109,30 @@ class JSONFieldTest(SimpleTestCase):
tests = [
{},
- {'json_field': None},
+ {"json_field": None},
]
for data in tests:
with self.subTest(data=data):
form = JSONForm(data)
- self.assertEqual(form['json_field'].value(), 'null')
- self.assertIn('null</textarea>', form.as_p())
- self.assertEqual(form.errors['json_field'], ['This field is required.'])
+ self.assertEqual(form["json_field"].value(), "null")
+ self.assertIn("null</textarea>", form.as_p())
+ self.assertEqual(form.errors["json_field"], ["This field is required."])
def test_redisplay_wrong_input(self):
"""
Displaying a bound form (typically due to invalid input). The form
should not overquote JSONField inputs.
"""
+
class JSONForm(Form):
name = CharField(max_length=2)
json_field = JSONField()
# JSONField input is valid, name is too long.
- form = JSONForm({'name': 'xyz', 'json_field': '["foo"]'})
- self.assertNotIn('json_field', form.errors)
- self.assertIn('[&quot;foo&quot;]</textarea>', form.as_p())
+ form = JSONForm({"name": "xyz", "json_field": '["foo"]'})
+ self.assertNotIn("json_field", form.errors)
+ self.assertIn("[&quot;foo&quot;]</textarea>", form.as_p())
# Invalid JSONField.
- form = JSONForm({'name': 'xy', 'json_field': '{"foo"}'})
- self.assertEqual(form.errors['json_field'], ['Enter a valid JSON.'])
- self.assertIn('{&quot;foo&quot;}</textarea>', form.as_p())
+ form = JSONForm({"name": "xy", "json_field": '{"foo"}'})
+ self.assertEqual(form.errors["json_field"], ["Enter a valid JSON."])
+ self.assertIn("{&quot;foo&quot;}</textarea>", form.as_p())