Esempio n. 1
0
    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.assertEqual(slave.apply(master), True)

        # Construct a slave template with equal minimum version
        slave = xmlutil.SlaveTemplate(elem, 3)
        self.assertEqual(slave.apply(master), True)

        # Construct a slave template with inapplicable minimum version
        slave = xmlutil.SlaveTemplate(elem, 4)
        self.assertEqual(slave.apply(master), False)

        # Construct a slave template with applicable version range
        slave = xmlutil.SlaveTemplate(elem, 2, 4)
        self.assertEqual(slave.apply(master), True)

        # Construct a slave template with low version range
        slave = xmlutil.SlaveTemplate(elem, 1, 2)
        self.assertEqual(slave.apply(master), False)

        # Construct a slave template with high version range
        slave = xmlutil.SlaveTemplate(elem, 4, 5)
        self.assertEqual(slave.apply(master), False)

        # Construct a slave template with matching version range
        slave = xmlutil.SlaveTemplate(elem, 3, 3)
        self.assertEqual(slave.apply(master), True)
Esempio n. 2
0
    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(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'])
Esempio n. 3
0
 def construct(self):
     elem = xmlutil.TemplateElement('test')
     return xmlutil.SlaveTemplate(elem, 1)