Refs #29147 --- Added JSONField serialization tests.

This commit is contained in:
Tim Graham 2018-03-21 19:18:00 -04:00
parent 0c100f8b57
commit 623139b5d1
1 changed files with 16 additions and 6 deletions

View File

@ -323,18 +323,28 @@ class TestChecks(PostgreSQLTestCase):
class TestSerialization(PostgreSQLTestCase):
test_data = (
'[{"fields": {"field": {"a": "b", "c": null}, "field_custom": null}, '
'[{"fields": {"field": %s, "field_custom": 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):
instance = JSONModel(field={'a': 'b', 'c': None})
for value, serialized in self.test_values:
with self.subTest(value=value):
instance = JSONModel(field=value)
data = serializers.serialize('json', [instance])
self.assertJSONEqual(data, self.test_data)
self.assertJSONEqual(data, self.test_data % serialized)
def test_loading(self):
instance = list(serializers.deserialize('json', self.test_data))[0].object
self.assertEqual(instance.field, {'a': 'b', 'c': None})
for value, serialized in self.test_values:
with self.subTest(value=value):
instance = list(serializers.deserialize('json', self.test_data % serialized))[0].object
self.assertEqual(instance.field, value)
class TestValidation(PostgreSQLTestCase):