def test_basic_namespace(self): """Test an ultra basic XML.""" basic_xml = ET.fromstring("""<?xml version="1.0"?> <Data xmlns:fictional="http://characters.example.com"> <fictional:Age>37</fictional:Age> </Data>""") class XmlModel(Model): _attribute_map = { 'age': { 'key': 'age', 'type': 'int', 'xml': { 'name': 'Age', 'prefix': 'fictional', 'ns': 'http://characters.example.com' } }, } _xml_map = {'name': 'Data'} mymodel = XmlModel(age=37, ) s = Serializer({"XmlModel": XmlModel}) rawxml = s.body(mymodel, 'XmlModel') assert_xml_equals(rawxml, basic_xml)
def test_direct_array(self): """Test an ultra basic XML.""" basic_xml = ET.fromstring("""<?xml version="1.0"?> <bananas> <Data country="france"/> </bananas> """) class XmlModel(Model): _attribute_map = { 'country': { 'key': 'country', 'type': 'str', 'xml': { 'name': 'country', 'attr': True } }, } _xml_map = {'name': 'Data'} mymodel = XmlModel(country="france") s = Serializer({"XmlModel": XmlModel}) rawxml = s.body( [mymodel], '[XmlModel]', serialization_ctxt={'xml': { 'name': 'bananas', 'wrapped': True }}) assert_xml_equals(rawxml, basic_xml)
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 = ET.fromstring("""<?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'} mymodel = AppleBarrel(good_apples=['granny', 'fuji']) s = Serializer({"AppleBarrel": AppleBarrel}) rawxml = s.body(mymodel, 'AppleBarrel') assert_xml_equals(rawxml, basic_xml)
def test_basic(self): """Test an ultra basic XML.""" basic_xml = ET.fromstring("""<?xml version="1.0"?> <Data country="france"> <Age>37</Age> </Data>""") class XmlModel(Model): _attribute_map = { 'age': { 'key': 'age', 'type': 'int', 'xml': { 'name': 'Age' } }, 'country': { 'key': 'country', 'type': 'str', 'xml': { 'name': 'country', 'attr': True } }, } _xml_map = {'name': 'Data'} mymodel = XmlModel(age=37, country="france") s = Serializer({"XmlModel": XmlModel}) rawxml = s.body(mymodel, 'XmlModel') assert_xml_equals(rawxml, basic_xml)
def test_type_basic(self): """Test some types.""" basic_xml = ET.fromstring("""<?xml version="1.0"?> <Data> <Age>37</Age> <Enabled>true</Enabled> </Data>""") class XmlModel(Model): _attribute_map = { 'age': { 'key': 'age', 'type': 'int', 'xml': { 'name': 'Age' } }, 'enabled': { 'key': 'enabled', 'type': 'bool', 'xml': { 'name': 'Enabled' } }, } _xml_map = {'name': 'Data'} mymodel = XmlModel(age=37, enabled=True) s = Serializer({"XmlModel": XmlModel}) rawxml = s.body(mymodel, 'XmlModel') assert_xml_equals(rawxml, basic_xml)
def test_add_prop(self): """Test addProp as a dict. """ basic_xml = ET.fromstring("""<?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'} mymodel = XmlModel(metadata={ 'Key1': 'value1', 'Key2': 'value2', }) s = Serializer({"XmlModel": XmlModel}) rawxml = s.body(mymodel, 'XmlModel') assert_xml_equals(rawxml, basic_xml)
def serializer_helper(object_to_serialize: Model) -> dict: if object_to_serialize is None: return None serializer = Serializer(DEPENDICIES_DICT) # pylint: disable=protected-access return serializer._serialize(object_to_serialize)
def test_two_complex_same_type(self): """Two different attribute are same type """ basic_xml = ET.fromstring("""<?xml version="1.0"?> <AppleBarrel> <EuropeanApple name="granny"/> <USAApple name="fuji"/> </AppleBarrel>""") class AppleBarrel(Model): _attribute_map = { 'eu_apple': {'key': 'EuropeanApple', 'type': 'Apple', 'xml': {'name': 'EuropeanApple'}}, 'us_apple': {'key': 'USAApple', 'type': 'Apple', 'xml': {'name': 'USAApple'}}, } _xml_map = { 'name': 'AppleBarrel' } class Apple(Model): _attribute_map = { 'name': {'key': 'name', 'type': 'str', 'xml':{'name': 'name', 'attr': True}}, } _xml_map = { } mymodel = AppleBarrel( eu_apple=Apple(name='granny'), us_apple=Apple(name='fuji'), ) s = Serializer({"AppleBarrel": AppleBarrel, "Apple": Apple}) rawxml = s.body(mymodel, 'AppleBarrel') assert_xml_equals(rawxml, basic_xml)
def serialize_obj(self, obj, class_name): ''' Return a JSON representation of an Azure object. :param obj: Azure object :param class_name: Name of the object's class :return: serialized result ''' serializer = Serializer() return serializer.body(obj, class_name)
def __init__(self): self.config = KeyVaultSampleConfig() self.credentials = None self.keyvault_data_client = None self.keyvault_mgmt_client = None self.resource_mgmt_client = None self._setup_complete = False self.samples = {(name, m) for name, m in inspect.getmembers(self) if getattr(m, 'kv_sample', False)} models = {} models.update({k: v for k, v in azure.keyvault.models.__dict__.items() if isinstance(v, type)}) models.update({k: v for k, v in azure.mgmt.keyvault.models.__dict__.items() if isinstance(v, type)}) self._serializer = Serializer(models)
def test_object(self): """Test serialize object as is. """ basic_xml = ET.fromstring("""<?xml version="1.0"?> <Data country="france"> <Age>37</Age> </Data>""") s = Serializer() rawxml = s.body(basic_xml, 'object') # It should actually be the same object, should not even try to touch it assert rawxml is basic_xml
def test_list_wrapped_items_name_complex_types(self): """Test XML list and wrap, items is ref and there is itemsName. """ basic_xml = ET.fromstring("""<?xml version="1.0"?> <AppleBarrel> <GoodApples> <Apple name="granny"/> <Apple name="fuji"/> </GoodApples> </AppleBarrel>""") class AppleBarrel(Model): _attribute_map = { # Pomme should be ignored, since it's invalid to define itemsName for a $ref type 'good_apples': { 'key': 'GoodApples', 'type': '[Apple]', 'xml': { 'name': 'GoodApples', 'wrapped': True, 'itemsName': 'Pomme' } }, } _xml_map = {'name': 'AppleBarrel'} class Apple(Model): _attribute_map = { 'name': { 'key': 'name', 'type': 'str', 'xml': { 'name': 'name', 'attr': True } }, } _xml_map = {'name': 'Apple'} mymodel = AppleBarrel( good_apples=[Apple( name='granny'), Apple(name='fuji')]) s = Serializer({"AppleBarrel": AppleBarrel, "Apple": Apple}) rawxml = s.body(mymodel, 'AppleBarrel') assert_xml_equals(rawxml, basic_xml)
def serializer_helper(object_to_serialize: Model) -> dict: if object_to_serialize is None: return None dependencies = [ schema_cls for key, schema_cls in getmembers(schema) if isinstance(schema_cls, type) and issubclass(schema_cls, (Model, Enum)) ] dependencies += [ schema_cls for key, schema_cls in getmembers(teams_schema) if isinstance(schema_cls, type) and issubclass(schema_cls, (Model, Enum)) ] dependencies_dict = {dependency.__name__: dependency for dependency in dependencies} serializer = Serializer(dependencies_dict) # pylint: disable=protected-access return serializer._serialize(object_to_serialize)
def test_list_not_wrapped_complex_types(self): """Test XML list and wrap, items is ref and there is no itemsName. """ basic_xml = ET.fromstring("""<?xml version="1.0"?> <AppleBarrel> <Apple name="granny"/> <Apple name="fuji"/> </AppleBarrel>""") class AppleBarrel(Model): _attribute_map = { # Name is ignored if "wrapped" is False 'good_apples': { 'key': 'GoodApples', 'type': '[Apple]', 'xml': { 'name': 'GoodApples' } }, } _xml_map = {'name': 'AppleBarrel'} class Apple(Model): _attribute_map = { 'name': { 'key': 'name', 'type': 'str', 'xml': { 'name': 'name', 'attr': True } }, } _xml_map = {'name': 'Apple'} mymodel = AppleBarrel( good_apples=[Apple( name='granny'), Apple(name='fuji')]) s = Serializer({"AppleBarrel": AppleBarrel, "Apple": Apple}) rawxml = s.body(mymodel, 'AppleBarrel') assert_xml_equals(rawxml, basic_xml)
def serialize_obj(self, obj, class_name, enum_modules=[]): ''' Return a JSON representation of an Azure object. :param obj: Azure object :param class_name: Name of the object's class :param enum_modules: List of module names to build enum dependencies from. :return: serialized result ''' dependencies = dict() if enum_modules: for module_name in enum_modules: mod = importlib.import_module(module_name) for mod_class_name, mod_class_obj in inspect.getmembers(mod, predicate=inspect.isclass): dependencies[mod_class_name] = mod_class_obj self.log("dependencies: ") self.log(str(dependencies)) serializer = Serializer(classes=dependencies) return serializer.body(obj, class_name)
def test_basic_unicode(self): """Test a XML with unicode.""" basic_xml = ET.fromstring(u"""<?xml version="1.0" encoding="utf-8"?> <Data language="français"/>""".encode("utf-8")) class XmlModel(Model): _attribute_map = { 'language': { 'key': 'language', 'type': 'str', 'xml': { 'name': 'language', 'attr': True } }, } _xml_map = {'name': 'Data'} mymodel = XmlModel(language=u"français") s = Serializer({"XmlModel": XmlModel}) rawxml = s.body(mymodel, 'XmlModel') assert_xml_equals(rawxml, basic_xml)