From 623139b5d1bd006eac78b375bcaf5948e695c3c6 Mon Sep 17 00:00:00 2001 From: Tim Graham Date: Wed, 21 Mar 2018 19:18:00 -0400 Subject: [PATCH] Refs #29147 --- Added JSONField serialization tests. --- tests/postgres_tests/test_json.py | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/tests/postgres_tests/test_json.py b/tests/postgres_tests/test_json.py index b22cbfc571e..305278fc6a4 100644 --- a/tests/postgres_tests/test_json.py +++ b/tests/postgres_tests/test_json.py @@ -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}) - data = serializers.serialize('json', [instance]) - self.assertJSONEqual(data, self.test_data) + 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 % 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):