本文整理汇总了Python中msrest.Serializer._serialize方法的典型用法代码示例。如果您正苦于以下问题:Python Serializer._serialize方法的具体用法?Python Serializer._serialize怎么用?Python Serializer._serialize使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类msrest.Serializer
的用法示例。
在下文中一共展示了Serializer._serialize方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: TestRuntimeSerialized
# 需要导入模块: from msrest import Serializer [as 别名]
# 或者: from msrest.Serializer import _serialize [as 别名]
class TestRuntimeSerialized(unittest.TestCase):
class TestObj(Model):
_validation = {}
_attribute_map = {
'attr_a': {'key':'id', 'type':'str'},
'attr_b': {'key':'AttrB', 'type':'int'},
'attr_c': {'key':'Key_C', 'type': 'bool'},
'attr_d': {'key':'AttrD', 'type':'[int]'},
'attr_e': {'key':'AttrE', 'type': '{float}'}
}
def __init__(self):
self.attr_a = None
self.attr_b = None
self.attr_c = None
self.attr_d = None
self.attr_e = None
def __str__(self):
return "Test_Object"
def setUp(self):
self.s = Serializer()
return super(TestRuntimeSerialized, self).setUp()
def test_obj_without_attr_map(self):
"""
Test serializing an object with no attribute_map.
"""
test_obj = type("BadTestObj", (), {})
with self.assertRaises(SerializationError):
self.s._serialize(test_obj)
def test_obj_with_malformed_map(self):
"""
Test serializing an object with a malformed attribute_map.
"""
test_obj = type("BadTestObj", (Model,), {"_attribute_map":None})
with self.assertRaises(SerializationError):
self.s._serialize(test_obj)
test_obj._attribute_map = {"attr":"val"}
with self.assertRaises(SerializationError):
self.s._serialize(test_obj)
test_obj._attribute_map = {"attr":{"val":1}}
with self.assertRaises(SerializationError):
self.s._serialize(test_obj)
def test_obj_with_mismatched_map(self):
"""
Test serializing an object with mismatching attributes and map.
"""
test_obj = type("BadTestObj", (Model,), {"_attribute_map":None})
test_obj._attribute_map = {"abc":{"key":"ABC", "type":"str"}}
with self.assertRaises(SerializationError):
self.s._serialize(test_obj)
def test_attr_none(self):
"""
Test serializing an object with None attributes.
"""
test_obj = self.TestObj()
message = self.s._serialize(test_obj)
self.assertIsInstance(message, dict)
self.assertFalse('id' in message)
def test_attr_int(self):
"""
Test serializing an object with Int attributes.
"""
test_obj = self.TestObj()
self.TestObj._validation = {
'attr_b': {'required': True},
}
test_obj.attr_b = None
with self.assertRaises(ValidationError):
self.s._serialize(test_obj)
test_obj.attr_b = 25
message = self.s._serialize(test_obj)
self.assertEqual(message['AttrB'], int(test_obj.attr_b))
test_obj.attr_b = "34534"
message = self.s._serialize(test_obj)
self.assertEqual(message['AttrB'], int(test_obj.attr_b))
test_obj.attr_b = "NotANumber"
#.........这里部分代码省略.........