Cannot set String field to a dict anymore!
This commit is contained in:
@@ -188,6 +188,18 @@ def assertDeserializeEqual(field, expected, arg):
|
||||
assert_equals(expected, deserialize_field(field, arg))
|
||||
|
||||
|
||||
def assertDeserializeNonString(field):
|
||||
"""
|
||||
Asserts input value is returned for None or something that is not a string.
|
||||
"""
|
||||
assertDeserializeEqual(field, None, None)
|
||||
assertDeserializeEqual(field, 3.14, 3.14)
|
||||
assertDeserializeEqual(field, True, True)
|
||||
assertDeserializeEqual(field, [10], [10])
|
||||
assertDeserializeEqual(field, {}, {})
|
||||
assertDeserializeEqual(field, [], [])
|
||||
|
||||
|
||||
class TestSerializeInteger(unittest.TestCase):
|
||||
""" Tests serialize/deserialize as related to Integer type. """
|
||||
|
||||
@@ -208,7 +220,7 @@ class TestSerializeInteger(unittest.TestCase):
|
||||
|
||||
def test_deserialize_unsupported_types(self):
|
||||
assertDeserializeEqual(Integer(), '[3]', '[3]')
|
||||
self.assertRaises(TypeError, deserialize_field, None)
|
||||
assertDeserializeNonString(Integer())
|
||||
|
||||
|
||||
class FloatTest(unittest.TestCase):
|
||||
@@ -235,7 +247,7 @@ class FloatTest(unittest.TestCase):
|
||||
|
||||
def test_deserialize_unsupported_types(self):
|
||||
assertDeserializeEqual(Float(), '[3]', '[3]')
|
||||
self.assertRaises(TypeError, deserialize_field, None)
|
||||
assertDeserializeNonString(Float())
|
||||
|
||||
|
||||
class BooleanTest(unittest.TestCase):
|
||||
@@ -264,8 +276,7 @@ class BooleanTest(unittest.TestCase):
|
||||
assertDeserializeEqual(Boolean(), 'fAlse', '"fAlse"')
|
||||
assertDeserializeEqual(Boolean(), "TruE", '"TruE"')
|
||||
|
||||
def test_deserialize_unsupported_types(self):
|
||||
self.assertRaises(TypeError, deserialize_field, None)
|
||||
assertDeserializeNonString(Boolean())
|
||||
|
||||
|
||||
class StringTest(unittest.TestCase):
|
||||
@@ -286,7 +297,7 @@ class StringTest(unittest.TestCase):
|
||||
assertDeserializeEqual(String(), 'false', 'false')
|
||||
assertDeserializeEqual(String(), '2', '2')
|
||||
assertDeserializeEqual(String(), '[3]', '[3]')
|
||||
self.assertRaises(TypeError, deserialize_field, None)
|
||||
assertDeserializeNonString(String())
|
||||
|
||||
|
||||
class AnyTest(unittest.TestCase):
|
||||
@@ -307,9 +318,7 @@ class AnyTest(unittest.TestCase):
|
||||
assertDeserializeEqual(Any(), '[', '[')
|
||||
assertDeserializeEqual(Any(), False, 'false')
|
||||
assertDeserializeEqual(Any(), 3.4, '3.4')
|
||||
|
||||
def test_deserialize_unsupported_types(self):
|
||||
self.assertRaises(TypeError, deserialize_field, None)
|
||||
assertDeserializeNonString(Any())
|
||||
|
||||
|
||||
class ListTest(unittest.TestCase):
|
||||
@@ -330,7 +339,7 @@ class ListTest(unittest.TestCase):
|
||||
assertDeserializeEqual(List(), '3.4', '3.4')
|
||||
assertDeserializeEqual(List(), 'false', 'false')
|
||||
assertDeserializeEqual(List(), '2', '2')
|
||||
self.assertRaises(TypeError, deserialize_field, None)
|
||||
assertDeserializeNonString(List())
|
||||
|
||||
|
||||
class DateTest(unittest.TestCase):
|
||||
@@ -342,9 +351,11 @@ class DateTest(unittest.TestCase):
|
||||
def test_deserialize(self):
|
||||
assertDeserializeEqual(Date(), '2012-12-31T23:59:59Z', "2012-12-31T23:59:59Z")
|
||||
assertDeserializeEqual(Date(), '2012-12-31T23:59:59Z', '"2012-12-31T23:59:59Z"')
|
||||
assertDeserializeNonString(Date())
|
||||
|
||||
|
||||
class TimedeltaTest(unittest.TestCase):
|
||||
""" Tests serialize/deserialize as related to Timedelta type. """
|
||||
|
||||
def test_serialize(self):
|
||||
assertSerializeEqual('"1 day 12 hours 59 minutes 59 seconds"',
|
||||
@@ -355,3 +366,4 @@ class TimedeltaTest(unittest.TestCase):
|
||||
'1 day 12 hours 59 minutes 59 seconds')
|
||||
assertDeserializeEqual(Timedelta(), '1 day 12 hours 59 minutes 59 seconds',
|
||||
'"1 day 12 hours 59 minutes 59 seconds"')
|
||||
assertDeserializeNonString(Timedelta())
|
||||
|
||||
@@ -95,23 +95,28 @@ def deserialize_field(field, value):
|
||||
Note that this is not the same as the value returned by from_json, as model types typically store
|
||||
their value internally as JSON. By default, this method will return the result of calling json.loads
|
||||
on the supplied value, unless json.loads throws a TypeError, or the type of the value returned by json.loads
|
||||
is not supported for this class (see 'is_type_supported'). In either of those cases, this method returns
|
||||
is not supported for this class (from_json throws an Error). In either of those cases, this method returns
|
||||
the input value.
|
||||
"""
|
||||
try:
|
||||
deserialized = json.loads(value)
|
||||
|
||||
if deserialized is None:
|
||||
return deserialized
|
||||
try:
|
||||
field.from_json(deserialized)
|
||||
return deserialized
|
||||
except (ValueError, TypeError):
|
||||
# Support older serialized forms by simply returning the String representation
|
||||
# Support older serialized version, which was just a string, not result of json.dumps.
|
||||
# If the deserialized version cannot be converted to the type (via from_json),
|
||||
# just return the original value. For example, if a string value of '3.4' was
|
||||
# stored for a String field (before we started storing the result of json.dumps),
|
||||
# then it would be deserialized as 3.4, but 3.4 is not supported for a String
|
||||
# field. Therefore field.from_json(3.4) will throw an Error, and we should
|
||||
# actually return the original value of '3.4'.
|
||||
return value
|
||||
|
||||
except (ValueError, TypeError):
|
||||
# Support older serialized version, which was just the String (not the result of json.dumps).
|
||||
# Support older serialized version.
|
||||
return value
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user