def test_serialize_with_delimiter(self): # Our test object to serialize obj = {'test': {'scope0:key1': 'Value1', 'scope0:scope1:key2': 'Value2', 'scope0:scope1:scope2:key3': 'Value3' }} # Set up our master template root = xmlutil.TemplateElement('test', selector='test') key1 = xmlutil.SubTemplateElement(root, 'scope0:key1', selector='scope0:key1') key1.text = xmlutil.Selector() key2 = xmlutil.SubTemplateElement(root, 'scope0:scope1:key2', selector='scope0:scope1:key2') key2.text = xmlutil.Selector() key3 = xmlutil.SubTemplateElement(root, 'scope0:scope1:scope2:key3', selector='scope0:scope1:scope2:key3') key3.text = xmlutil.Selector() serializer = xmlutil.MasterTemplate(root, 1) xml_list = [] xml_list.append("<?xmlversion='1.0'encoding='UTF-8'?><test>") xml_list.append("<scope0><key1>Value1</key1><scope1>") xml_list.append("<key2>Value2</key2><scope2><key3>Value3</key3>") xml_list.append("</scope2></scope1></scope0></test>") expected_xml = ''.join(xml_list) result = serializer.serialize(obj) result = result.replace('\n', '').replace(' ', '') self.assertEqual(expected_xml, result)
def test__serialize(self): # Our test object to serialize obj = {'test': {'name': 'foobar', 'values': [1, 2, 3, 4], 'attrs': {'a': 1, 'b': 2, 'c': 3, 'd': 4, }, 'image': {'name': 'image_foobar', 'id': 42, }, }, } # Set up our master template root = xmlutil.TemplateElement('test', selector='test', name='name') value = xmlutil.SubTemplateElement(root, 'value', selector='values') value.text = xmlutil.Selector() attrs = xmlutil.SubTemplateElement(root, 'attrs', selector='attrs') xmlutil.SubTemplateElement(attrs, 'attr', selector=xmlutil.get_items, key=0, value=1) master = xmlutil.MasterTemplate(root, 1, nsmap=dict(f='foo')) # Set up our slave template root_slave = xmlutil.TemplateElement('test', selector='test') image = xmlutil.SubTemplateElement(root_slave, 'image', selector='image', id='id') image.text = xmlutil.Selector('name') slave = xmlutil.SlaveTemplate(root_slave, 1, nsmap=dict(b='bar')) # Attach the slave to the master... master.attach(slave) # Try serializing our object siblings = master._siblings() nsmap = master._nsmap() result = master._serialize(None, obj, siblings, nsmap) # Now we get to manually walk the element tree... self.assertEqual('test', result.tag) self.assertEqual(2, len(result.nsmap)) self.assertEqual('foo', result.nsmap['f']) self.assertEqual('bar', result.nsmap['b']) self.assertEqual(result.get('name'), obj['test']['name']) for idx, val in enumerate(obj['test']['values']): self.assertEqual('value', result[idx].tag) self.assertEqual(str(val), result[idx].text) idx += 1 self.assertEqual('attrs', result[idx].tag) for attr in result[idx]: self.assertEqual('attr', attr.tag) self.assertEqual(str(obj['test']['attrs'][attr.get('key')]), attr.get('value')) idx += 1 self.assertEqual('image', result[idx].tag) self.assertEqual(str(obj['test']['image']['id']), result[idx].get('id')) self.assertEqual(obj['test']['image']['name'], result[idx].text)
def test_subelement(self): # Try the SubTemplateElement constructor parent = xmlutil.SubTemplateElement(None, 'parent') self.assertEqual('parent', parent.tag) self.assertEqual(0, len(parent)) # Now try it with a parent element child = xmlutil.SubTemplateElement(parent, 'child') self.assertEqual('child', child.tag) self.assertEqual(1, len(parent)) self.assertEqual(parent[0], child)
def construct(self): root = MetadataTemplateElement('metadata', selector='metadata') elem = xmlutil.SubTemplateElement(root, 'meta', selector=xmlutil.get_items) elem.set('key', 0) elem.text = 1 return xmlutil.MasterTemplate(root, 1, nsmap=metadata_nsmap)
def make_ext(elem): elem.set('name') elem.set('namespace') elem.set('alias') elem.set('updated') desc = xmlutil.SubTemplateElement(elem, 'description') desc.text = 'description' xmlutil.make_links(elem, 'links')
def make_version(elem): elem.set('id') elem.set('status') elem.set('updated') mts = MediaTypesTemplateElement('media-types') elem.append(mts) mt = xmlutil.SubTemplateElement(mts, 'media-type', selector='media-types') mt.set('base') mt.set('type') xmlutil.make_links(elem, 'links')
def construct(self): root = xmlutil.TemplateElement('extensions') elem = xmlutil.SubTemplateElement(root, 'extension', selector='extensions') make_ext(elem) return xmlutil.MasterTemplate(root, 1, nsmap=ext_nsmap)
def construct(self): root = xmlutil.TemplateElement('versions') elem = xmlutil.SubTemplateElement(root, 'version', selector='versions') make_version(elem) return xmlutil.MasterTemplate(root, 1, nsmap=version_nsmap)