forked from p15670423/monkey
Island, UT: Handle tuples in representations.py
This commit is contained in:
parent
da9f3c97ed
commit
fe6e3d75b5
|
@ -17,24 +17,27 @@ def _normalize_obj(obj):
|
|||
|
||||
|
||||
def _normalize_value(value):
|
||||
if isinstance(value, list):
|
||||
for i in range(0, len(value)):
|
||||
value[i] = _normalize_value(value[i])
|
||||
if type(value) == dict:
|
||||
return _normalize_obj(value)
|
||||
# ObjectId is serializible by default, but returns a dict
|
||||
# So serialize it first into a plain string
|
||||
if isinstance(value, bson.objectid.ObjectId):
|
||||
return str(value)
|
||||
|
||||
if isinstance(value, list):
|
||||
return [_normalize_value(_value) for _value in value]
|
||||
if isinstance(value, tuple):
|
||||
return tuple((_normalize_value(_value) for _value in value))
|
||||
if type(value) == dict:
|
||||
return _normalize_obj(value)
|
||||
if isinstance(value, datetime):
|
||||
return str(value)
|
||||
if issubclass(type(value), Enum):
|
||||
return value.name
|
||||
|
||||
try:
|
||||
dumps(value)
|
||||
return value
|
||||
except TypeError:
|
||||
return value.__dict__
|
||||
except AttributeError:
|
||||
pass
|
||||
|
||||
return value
|
||||
|
||||
|
||||
def output_json(value, code, headers=None):
|
||||
|
|
|
@ -13,7 +13,8 @@ class MockClass:
|
|||
|
||||
|
||||
obj_id_str = "123456789012345678901234"
|
||||
mock_object = MockClass(1)
|
||||
mock_object1 = MockClass(1)
|
||||
mock_object2 = MockClass(2)
|
||||
|
||||
|
||||
def test_normalize_dicts():
|
||||
|
@ -21,9 +22,9 @@ def test_normalize_dicts():
|
|||
|
||||
assert {"a": "a"} == _normalize_value({"a": "a"})
|
||||
|
||||
assert {"id": 12345} == _normalize_value({"_id": 12345})
|
||||
assert {"id": 12345} == _normalize_value({"id": 12345})
|
||||
|
||||
assert {"id": obj_id_str} == _normalize_value({"_id": bson.objectid.ObjectId(obj_id_str)})
|
||||
assert {"id": obj_id_str} == _normalize_value({"id": bson.objectid.ObjectId(obj_id_str)})
|
||||
|
||||
dt = datetime.now()
|
||||
expected = {"a": str(dt)}
|
||||
|
@ -39,7 +40,7 @@ def test_normalize_complex():
|
|||
"bb": bson.objectid.ObjectId(obj_id_str),
|
||||
}
|
||||
],
|
||||
"b": {"_id": bson.objectid.ObjectId(obj_id_str)},
|
||||
"b": {"id": bson.objectid.ObjectId(obj_id_str)},
|
||||
}
|
||||
|
||||
expected_dict = {"a": [{"ba": obj_id_str, "bb": obj_id_str}], "b": {"id": obj_id_str}}
|
||||
|
@ -47,16 +48,22 @@ def test_normalize_complex():
|
|||
|
||||
|
||||
def test_normalize_list():
|
||||
mock_list = [bson.objectid.ObjectId(obj_id_str), {"a": "b"}, {"object": [mock_object]}]
|
||||
mock_list = [bson.objectid.ObjectId(obj_id_str), {"a": "b"}, {"object": [mock_object1]}]
|
||||
|
||||
expected_list = [obj_id_str, {"a": "b"}, {"object": [{"a": 1}]}]
|
||||
assert expected_list == _normalize_value(mock_list)
|
||||
|
||||
|
||||
def test_normalize__enum():
|
||||
def test_normalize_enum():
|
||||
class BogusEnum(Enum):
|
||||
bogus_val = "Bogus"
|
||||
|
||||
my_obj = {"something": "something", "my_enum": BogusEnum.bogus_val}
|
||||
|
||||
assert {"something": "something", "my_enum": "bogus_val"} == _normalize_value(my_obj)
|
||||
|
||||
|
||||
def test_normalize_tuple():
|
||||
mock_tuple = [{"my_tuple": (mock_object1, mock_object2, b"one_two")}]
|
||||
expected_tuple = [{"my_tuple": ({"a": 1}, {"a": 2}, b"one_two")}]
|
||||
assert expected_tuple == _normalize_value(mock_tuple)
|
||||
|
|
Loading…
Reference in New Issue