Refs --- Added JSONField serialization tests.
@@ -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):
