mirror of https://github.com/django/django.git
Refs #29147 --- Added JSONField serialization tests.
This commit is contained in:
parent
0c100f8b57
commit
623139b5d1
|
@ -323,18 +323,28 @@ class TestChecks(PostgreSQLTestCase):
|
||||||
|
|
||||||
class TestSerialization(PostgreSQLTestCase):
|
class TestSerialization(PostgreSQLTestCase):
|
||||||
test_data = (
|
test_data = (
|
||||||
'[{"fields": {"field": {"a": "b", "c": null}, "field_custom": null}, '
|
'[{"fields": {"field": %s, "field_custom": null}, '
|
||||||
'"model": "postgres_tests.jsonmodel", "pk": null}]'
|
'"model": "postgres_tests.jsonmodel", "pk": null}]'
|
||||||
)
|
)
|
||||||
|
test_values = (
|
||||||
|
# (Python value, serialized value),
|
||||||
|
({'a': 'b', 'c': None}, '{"a": "b", "c": null}'),
|
||||||
|
('abc', '"abc"'),
|
||||||
|
('{"a": "a"}', '"{\\"a\\": \\"a\\"}"'),
|
||||||
|
)
|
||||||
|
|
||||||
def test_dumping(self):
|
def test_dumping(self):
|
||||||
instance = JSONModel(field={'a': 'b', 'c': None})
|
for value, serialized in self.test_values:
|
||||||
data = serializers.serialize('json', [instance])
|
with self.subTest(value=value):
|
||||||
self.assertJSONEqual(data, self.test_data)
|
instance = JSONModel(field=value)
|
||||||
|
data = serializers.serialize('json', [instance])
|
||||||
|
self.assertJSONEqual(data, self.test_data % serialized)
|
||||||
|
|
||||||
def test_loading(self):
|
def test_loading(self):
|
||||||
instance = list(serializers.deserialize('json', self.test_data))[0].object
|
for value, serialized in self.test_values:
|
||||||
self.assertEqual(instance.field, {'a': 'b', 'c': None})
|
with self.subTest(value=value):
|
||||||
|
instance = list(serializers.deserialize('json', self.test_data % serialized))[0].object
|
||||||
|
self.assertEqual(instance.field, value)
|
||||||
|
|
||||||
|
|
||||||
class TestValidation(PostgreSQLTestCase):
|
class TestValidation(PostgreSQLTestCase):
|
||||||
|
|
Loading…
Reference in New Issue