def test_master_attach(self): # Set up a master template elem = xmlutil.TemplateElement('test') tmpl = xmlutil.MasterTemplate(elem, 1) # Make sure it has a root but no slaves self.assertEqual(elem, tmpl.root) self.assertEqual(0, len(tmpl.slaves)) # Try to attach an invalid slave bad_elem = xmlutil.TemplateElement('test2') self.assertRaises(ValueError, tmpl.attach, bad_elem) self.assertEqual(0, len(tmpl.slaves)) # Try to attach an invalid and a valid slave good_elem = xmlutil.TemplateElement('test') self.assertRaises(ValueError, tmpl.attach, good_elem, bad_elem) self.assertEqual(0, len(tmpl.slaves)) # Try to attach an inapplicable template class InapplicableTemplate(xmlutil.Template): def apply(self, master): return False inapp_tmpl = InapplicableTemplate(good_elem) tmpl.attach(inapp_tmpl) self.assertEqual(0, len(tmpl.slaves)) # Now try attaching an applicable template tmpl.attach(good_elem) self.assertEqual(1, len(tmpl.slaves)) self.assertEqual(good_elem, tmpl.slaves[0].root)
def test_make_flat_dict(self): expected_xml = ("<?xml version='1.0' encoding='UTF-8'?>\n" '<wrapper><a>foo</a><b>bar</b></wrapper>') root = xmlutil.make_flat_dict('wrapper') tmpl = xmlutil.MasterTemplate(root, 1) result = tmpl.serialize(dict(wrapper=dict(a='foo', b='bar'))) self.assertEqual(expected_xml, result)
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_slave_apply(self): # Construct a master template elem = xmlutil.TemplateElement('test') master = xmlutil.MasterTemplate(elem, 3) # Construct a slave template with applicable minimum version slave = xmlutil.SlaveTemplate(elem, 2) self.assertTrue(slave.apply(master)) # Construct a slave template with equal minimum version slave = xmlutil.SlaveTemplate(elem, 3) self.assertTrue(slave.apply(master)) # Construct a slave template with inapplicable minimum version slave = xmlutil.SlaveTemplate(elem, 4) self.assertFalse(slave.apply(master)) # Construct a slave template with applicable version range slave = xmlutil.SlaveTemplate(elem, 2, 4) self.assertTrue(slave.apply(master)) # Construct a slave template with low version range slave = xmlutil.SlaveTemplate(elem, 1, 2) self.assertFalse(slave.apply(master)) # Construct a slave template with high version range slave = xmlutil.SlaveTemplate(elem, 4, 5) self.assertFalse(slave.apply(master)) # Construct a slave template with matching version range slave = xmlutil.SlaveTemplate(elem, 3, 3) self.assertTrue(slave.apply(master))
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 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_master_copy(self): # Construct a master template elem = xmlutil.TemplateElement('test') tmpl = xmlutil.MasterTemplate(elem, 1, nsmap=dict(a='foo')) # Give it a slave slave = xmlutil.TemplateElement('test') tmpl.attach(slave) # Construct a copy copy = tmpl.copy() # Check to see if we actually managed a copy self.assertNotEqual(tmpl, copy) self.assertEqual(tmpl.root, copy.root) self.assertEqual(tmpl.version, copy.version) self.assertEqual(id(tmpl.nsmap), id(copy.nsmap)) self.assertNotEqual(id(tmpl.slaves), id(copy.slaves)) self.assertEqual(len(tmpl.slaves), len(copy.slaves)) self.assertEqual(tmpl.slaves[0], copy.slaves[0])
def construct(self): elem = xmlutil.TemplateElement('test') return xmlutil.MasterTemplate(elem, 1)
def construct(self): sel = xmlutil.Selector('meta', xmlutil.get_items, 0) root = xmlutil.TemplateElement('meta', selector=sel) root.set('key', 0) root.text = 1 return xmlutil.MasterTemplate(root, 1, nsmap=metadata_nsmap)
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)