def test_subordinate_apply(self): # Construct a main template elem = xmlutil.TemplateElement('test') main = xmlutil.MainTemplate(elem, 3) # Construct a subordinate template with applicable minimum version subordinate = xmlutil.SubordinateTemplate(elem, 2) self.assertEqual(subordinate.apply(main), True) # Construct a subordinate template with equal minimum version subordinate = xmlutil.SubordinateTemplate(elem, 3) self.assertEqual(subordinate.apply(main), True) # Construct a subordinate template with inapplicable minimum version subordinate = xmlutil.SubordinateTemplate(elem, 4) self.assertEqual(subordinate.apply(main), False) # Construct a subordinate template with applicable version range subordinate = xmlutil.SubordinateTemplate(elem, 2, 4) self.assertEqual(subordinate.apply(main), True) # Construct a subordinate template with low version range subordinate = xmlutil.SubordinateTemplate(elem, 1, 2) self.assertEqual(subordinate.apply(main), False) # Construct a subordinate template with high version range subordinate = xmlutil.SubordinateTemplate(elem, 4, 5) self.assertEqual(subordinate.apply(main), False) # Construct a subordinate template with matching version range subordinate = xmlutil.SubordinateTemplate(elem, 3, 3) self.assertEqual(subordinate.apply(main), True)
def construct(self): root = xmlutil.TemplateElement('snapshots') elem = xmlutil.SubTemplateElement(root, 'snapshot', selector='snapshots') make_snapshot(elem) alias = Extended_snapshot_attributes.alias namespace = Extended_snapshot_attributes.namespace return xmlutil.SubordinateTemplate(root, 1, nsmap={alias: namespace})
def construct(self): elem = xmlutil.TemplateElement('test') return xmlutil.SubordinateTemplate(elem, 1)
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 main 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) main = xmlutil.MainTemplate(root, 1, nsmap=dict(f='foo')) # Set up our subordinate template root_subordinate = xmlutil.TemplateElement('test', selector='test') image = xmlutil.SubTemplateElement(root_subordinate, 'image', selector='image', id='id') image.text = xmlutil.Selector('name') subordinate = xmlutil.SubordinateTemplate(root_subordinate, 1, nsmap=dict(b='bar')) # Attach the subordinate to the main... main.attach(subordinate) # Try serializing our object siblings = main._siblings() nsmap = main._nsmap() result = main._serialize(None, obj, siblings, nsmap) # Now we get to manually walk the element tree... self.assertEqual(result.tag, 'test') self.assertEqual(len(result.nsmap), 2) self.assertEqual(result.nsmap['f'], 'foo') self.assertEqual(result.nsmap['b'], 'bar') self.assertEqual(result.get('name'), obj['test']['name']) for idx, val in enumerate(obj['test']['values']): self.assertEqual(result[idx].tag, 'value') self.assertEqual(result[idx].text, str(val)) idx += 1 self.assertEqual(result[idx].tag, 'attrs') for attr in result[idx]: self.assertEqual(attr.tag, 'attr') self.assertEqual(attr.get('value'), str(obj['test']['attrs'][attr.get('key')])) idx += 1 self.assertEqual(result[idx].tag, 'image') self.assertEqual(result[idx].get('id'), str(obj['test']['image']['id'])) self.assertEqual(result[idx].text, obj['test']['image']['name'])