本文整理汇总了Python中msrest.serialization.Model方法的典型用法代码示例。如果您正苦于以下问题:Python serialization.Model方法的具体用法?Python serialization.Model怎么用?Python serialization.Model使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类msrest.serialization
的用法示例。
在下文中一共展示了serialization.Model方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_model_kwargs
# 需要导入模块: from msrest import serialization [as 别名]
# 或者: from msrest.serialization import Model [as 别名]
def test_model_kwargs(self):
class MyModel(Model):
_validation = {
'id': {'readonly': True},
'name': {'required': True},
}
_attribute_map = {
'id': {'key': 'id', 'type': 'str'},
'name': {'key': 'name', 'type': 'str'},
'location': {'key': 'location', 'type': 'str'},
}
def __init__(self, **kwargs):
super(MyModel, self).__init__(**kwargs)
self.id = None
self.name = kwargs.get('name', None)
self.location = kwargs.get('location', None)
validation = MyModel().validate()
self.assertEqual(str(validation[0]), "Parameter 'MyModel.name' can not be None.")
示例2: test_validation_type
# 需要导入模块: from msrest import serialization [as 别名]
# 或者: from msrest.serialization import Model [as 别名]
def test_validation_type(self):
# https://github.com/Azure/msrest-for-python/issues/85
s = Serializer()
s.query("filter", 186, "int", maximum=666)
s.query("filter", "186", "int", maximum=666)
class TestValidationObj(Model):
_attribute_map = {
'attr_a': {'key':'id', 'type':'int'},
}
_validation = {
'attr_a': {'maximum': 4294967295, 'minimum': 1},
}
test_obj = TestValidationObj()
test_obj.attr_a = 186
errors_found = test_obj.validate()
assert not errors_found
test_obj.attr_a = '186'
errors_found = test_obj.validate()
assert not errors_found
示例3: test_serialize_from_dict_datetime
# 需要导入模块: from msrest import serialization [as 别名]
# 或者: from msrest.serialization import Model [as 别名]
def test_serialize_from_dict_datetime(self):
class DateTimeTest(Model):
_attribute_map = {
'birthday':{'key':'birthday','type':'iso-8601'},
}
def __init__(self, birthday):
self.birthday = birthday
serializer = Serializer({
'DateTimeTest': DateTimeTest
})
mydate = serializer.body(
{'birthday': datetime(1980, 12, 27)},
'DateTimeTest'
)
assert mydate["birthday"] == "1980-12-27T00:00:00.000Z"
示例4: test_additional_properties_with_auto_model
# 需要导入模块: from msrest import serialization [as 别名]
# 或者: from msrest.serialization import Model [as 别名]
def test_additional_properties_with_auto_model(self):
class AdditionalTest(Model):
_attribute_map = {
"name": {"key":"Name", "type":"str"},
"display_name": {"key":"DisplayName", "type":"str"},
'additional_properties': {'key': '', 'type': '{object}'}
}
o = {
'name': 'test',
'display_name': "display_name"
}
expected_message = {
"Name": "test",
"DisplayName": "display_name",
}
s = Serializer({'AdditionalTest': AdditionalTest})
serialized = s.body(o, 'AdditionalTest')
self.assertEqual(serialized, expected_message)
示例5: test_long_as_type_object
# 需要导入模块: from msrest import serialization [as 别名]
# 或者: from msrest.serialization import Model [as 别名]
def test_long_as_type_object(self):
"""Test irrelevant on Python 3. But still doing it to test regresssion.
https://github.com/Azure/msrest-for-python/pull/121
"""
try:
long_type = long
except NameError:
long_type = int
class TestModel(Model):
_attribute_map = {'data': {'key': 'data', 'type': 'object'}}
m = TestModel(data = {'id': long_type(1)})
serialized = m.serialize()
assert serialized == {
'data': {'id': long_type(1)}
}
示例6: test_personalize_deserialization
# 需要导入模块: from msrest import serialization [as 别名]
# 或者: from msrest.serialization import Model [as 别名]
def test_personalize_deserialization(self):
class TestDurationObj(Model):
_attribute_map = {
'attr_a': {'key':'attr_a', 'type':'duration'},
}
with self.assertRaises(DeserializationError):
obj = TestDurationObj.from_dict({
"attr_a": "00:00:10"
})
def duration_rest_key_extractor(attr, attr_desc, data):
value = rest_key_extractor(attr, attr_desc, data)
if attr == "attr_a":
# Stupid parsing, this is just a test
return "PT"+value[-2:]+"S"
obj = TestDurationObj.from_dict(
{"attr_a": "00:00:10"},
key_extractors=[duration_rest_key_extractor]
)
self.assertEqual(timedelta(seconds=10), obj.attr_a)
示例7: test_attr_list_complex
# 需要导入模块: from msrest import serialization [as 别名]
# 或者: from msrest.serialization import Model [as 别名]
def test_attr_list_complex(self):
"""
Test deserializing an object with a list of complex objects as an attribute.
"""
class ListObj(Model):
_attribute_map = {"abc":{"key":"ABC", "type":"int"}}
class CmplxTestObj(Model):
_response_map = {}
_attribute_map = {'attr_a': {'key':'id', 'type':'[ListObj]'}}
d = Deserializer({'ListObj':ListObj})
response = d(CmplxTestObj, json.dumps({"id":[{"ABC": "123"}]}), 'application/json')
deserialized_list = list(response.attr_a)
self.assertIsInstance(deserialized_list[0], ListObj)
self.assertEqual(deserialized_list[0].abc, 123)
示例8: test_model_instance_equality
# 需要导入模块: from msrest import serialization [as 别名]
# 或者: from msrest.serialization import Model [as 别名]
def test_model_instance_equality(self):
class Animal(Model):
_attribute_map = {
"name":{"key":"Name", "type":"str"},
}
def __init__(self, name=None):
self.name = name
animal1 = Animal('a1')
animal2 = Animal('a2')
animal3 = Animal('a1')
self.assertTrue(animal1!=animal2)
self.assertTrue(animal1==animal3)
示例9: test_basic_unicode
# 需要导入模块: from msrest import serialization [as 别名]
# 或者: from msrest.serialization import Model [as 别名]
def test_basic_unicode(self):
"""Test a XML with unicode."""
basic_xml = u"""<?xml version="1.0" encoding="utf-8"?>
<Data language="français"/>"""
class XmlModel(Model):
_attribute_map = {
'language': {'key': 'language', 'type': 'str', 'xml':{'name': 'language', 'attr': True}},
}
_xml_map = {
'name': 'Data'
}
s = Deserializer({"XmlModel": XmlModel})
result = s(XmlModel, basic_xml, "application/xml")
assert result.language == u"français"
示例10: test_add_prop
# 需要导入模块: from msrest import serialization [as 别名]
# 或者: from msrest.serialization import Model [as 别名]
def test_add_prop(self):
"""Test addProp as a dict.
"""
basic_xml = """<?xml version="1.0"?>
<Data>
<Metadata>
<Key1>value1</Key1>
<Key2>value2</Key2>
</Metadata>
</Data>"""
class XmlModel(Model):
_attribute_map = {
'metadata': {'key': 'Metadata', 'type': '{str}', 'xml': {'name': 'Metadata'}},
}
_xml_map = {
'name': 'Data'
}
s = Deserializer({"XmlModel": XmlModel})
result = s(XmlModel, basic_xml, "application/xml")
assert len(result.metadata) == 2
assert result.metadata['Key1'] == "value1"
assert result.metadata['Key2'] == "value2"
示例11: test_basic_empty
# 需要导入模块: from msrest import serialization [as 别名]
# 或者: from msrest.serialization import Model [as 别名]
def test_basic_empty(self):
"""Test an basic XML with an empty node."""
basic_xml = """<?xml version="1.0"?>
<Data>
<Age/>
</Data>"""
class XmlModel(Model):
_attribute_map = {
'age': {'key': 'age', 'type': 'str', 'xml':{'name': 'Age'}},
}
_xml_map = {
'name': 'Data'
}
s = Deserializer({"XmlModel": XmlModel})
result = s(XmlModel, basic_xml, "application/xml")
assert result.age == ""
示例12: test_basic_empty_list
# 需要导入模块: from msrest import serialization [as 别名]
# 或者: from msrest.serialization import Model [as 别名]
def test_basic_empty_list(self):
"""Test an basic XML with an empty node."""
basic_xml = """<?xml version="1.0"?>
<Data/>"""
class XmlModel(Model):
_attribute_map = {
'age': {'key': 'age', 'type': 'str', 'xml':{'name': 'Age'}},
}
_xml_map = {
'name': 'Data'
}
s = Deserializer({"XmlModel": XmlModel})
result = s('[XmlModel]', basic_xml, "application/xml")
assert result == []
示例13: test_list_not_wrapped_items_name_basic_types
# 需要导入模块: from msrest import serialization [as 别名]
# 或者: from msrest.serialization import Model [as 别名]
def test_list_not_wrapped_items_name_basic_types(self):
"""Test XML list and no wrap, items is basic type and there is itemsName.
"""
basic_xml = """<?xml version="1.0"?>
<AppleBarrel>
<Apple>granny</Apple>
<Apple>fuji</Apple>
</AppleBarrel>"""
class AppleBarrel(Model):
_attribute_map = {
'good_apples': {'key': 'GoodApples', 'type': '[str]', 'xml': {'name': 'GoodApples', 'itemsName': 'Apple'}},
}
_xml_map = {
'name': 'AppleBarrel'
}
s = Deserializer({"AppleBarrel": AppleBarrel})
result = s(AppleBarrel, basic_xml, "application/xml")
assert result.good_apples == ["granny", "fuji"]
示例14: test_list_wrapped_basic_types
# 需要导入模块: from msrest import serialization [as 别名]
# 或者: from msrest.serialization import Model [as 别名]
def test_list_wrapped_basic_types(self):
"""Test XML list and wrap, items is basic type and there is no itemsName.
"""
basic_xml = """<?xml version="1.0"?>
<AppleBarrel>
<GoodApples>
<GoodApples>granny</GoodApples>
<GoodApples>fuji</GoodApples>
</GoodApples>
</AppleBarrel>"""
class AppleBarrel(Model):
_attribute_map = {
'good_apples': {'key': 'GoodApples', 'type': '[str]', 'xml': {'name': 'GoodApples', 'wrapped': True}},
}
_xml_map = {
'name': 'AppleBarrel'
}
s = Deserializer({"AppleBarrel": AppleBarrel})
result = s(AppleBarrel, basic_xml, "application/xml")
assert result.good_apples == ["granny", "fuji"]
示例15: test_list_not_wrapped_basic_types
# 需要导入模块: from msrest import serialization [as 别名]
# 或者: from msrest.serialization import Model [as 别名]
def test_list_not_wrapped_basic_types(self):
"""Test XML list and no wrap, items is basic type and there is no itemsName.
"""
basic_xml = """<?xml version="1.0"?>
<AppleBarrel>
<GoodApples>granny</GoodApples>
<GoodApples>fuji</GoodApples>
</AppleBarrel>"""
class AppleBarrel(Model):
_attribute_map = {
'good_apples': {'key': 'GoodApples', 'type': '[str]', 'xml': {'name': 'GoodApples'}},
}
_xml_map = {
'name': 'AppleBarrel'
}
s = Deserializer({"AppleBarrel": AppleBarrel})
result = s(AppleBarrel, basic_xml, "application/xml")
assert result.good_apples == ["granny", "fuji"]