Beispiel #1
0
    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)
Beispiel #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('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)
Beispiel #3
0
 def test_list_selector(self):
     sel = xmlutil.Selector('test', 'values', 0)
     self.assertEqual(3, len(sel.chain))
     self.assertEqual('test', sel.chain[0])
     self.assertEqual('values', sel.chain[1])
     self.assertEqual(0, sel.chain[2])
     self.assertEqual(1, sel(self.obj_for_test))
Beispiel #4
0
    def test_element_text(self):
        # Create an element
        elem = xmlutil.TemplateElement('test')

        # Ensure that it has no text
        self.assertIsNone(elem.text)

        # Try setting it to a string and ensure it becomes a selector
        elem.text = 'test'
        self.assertTrue(hasattr(elem.text, 'chain'))
        self.assertEqual(1, len(elem.text.chain))
        self.assertEqual('test', elem.text.chain[0])

        # Try resetting the text to None
        elem.text = None
        self.assertIsNone(elem.text)

        # Now make up a selector and try setting the text to that
        sel = xmlutil.Selector()
        elem.text = sel
        self.assertEqual(sel, elem.text)

        # Finally, try deleting the text and see what happens
        del elem.text
        self.assertIsNone(elem.text)
Beispiel #5
0
    def test_render(self):
        # Create a template element
        tmpl_elem = xmlutil.TemplateElement('test')
        tmpl_elem.text = xmlutil.Selector()

        # Create the object we're going to render
        obj = ['elem1', 'elem2', 'elem3', 'elem4']

        # Try a render with no object
        elems = tmpl_elem.render(None, None)
        self.assertEqual(0, len(elems))

        # Try a render with one object
        elems = tmpl_elem.render(None, 'foo')
        self.assertEqual(1, len(elems))
        self.assertEqual('foo', elems[0][0].text)
        self.assertEqual('foo', elems[0][1])

        # Now, try rendering an object with multiple entries
        parent = etree.Element('parent')
        elems = tmpl_elem.render(parent, obj)
        self.assertEqual(4, len(elems))

        # Check the results
        for idx in range(len(obj)):
            self.assertEqual(obj[idx], elems[idx][0].text)
            self.assertEqual(obj[idx], elems[idx][1])
Beispiel #6
0
    def test_element_subselector(self):
        sel = xmlutil.Selector('a', 'b')

        # Create a template element with an explicit subselector
        elem = xmlutil.TemplateElement('test', subselector=sel)

        self.assertEqual(sel, elem.subselector)
Beispiel #7
0
    def test_element_attribute_items(self):
        expected = dict(a=xmlutil.Selector(1),
                        b=xmlutil.Selector(2),
                        c=xmlutil.Selector(3))
        keys = set(expected.keys())

        # Create a template element with some attributes
        elem = xmlutil.TemplateElement('test', attrib=expected)

        # Now verify items
        for k, v in elem.items():
            self.assertEqual(expected[k], v)
            keys.remove(k)

        # Did we visit all keys?
        self.assertEqual(0, len(keys))
Beispiel #8
0
    def test_dyntag(self):
        obj = ['a', 'b', 'c']

        # Create a template element with a dynamic tag
        tmpl_elem = xmlutil.TemplateElement(xmlutil.Selector())

        # Try the render
        parent = etree.Element('parent')
        elems = tmpl_elem.render(parent, obj)

        # Verify the particulars of the render
        self.assertEqual(len(obj), len(elems))
        for idx in range(len(obj)):
            self.assertEqual(obj[idx], elems[idx][0].tag)
Beispiel #9
0
    def test_element_set_attributes(self):
        attrs = dict(a=None, b='foo', c=xmlutil.Selector('foo', 'bar'))

        # Create a bare template element with no attributes
        elem = xmlutil.TemplateElement('test')

        # Set the attribute values
        for k, v in attrs.items():
            elem.set(k, v)

        # Now verify what got set
        self.assertEqual(1, len(elem.attrib['a'].chain))
        self.assertEqual('a', elem.attrib['a'].chain[0])
        self.assertEqual(1, len(elem.attrib['b'].chain))
        self.assertEqual('foo', elem.attrib['b'].chain[0])
        self.assertEqual(attrs['c'], elem.attrib['c'])
Beispiel #10
0
 def test_missing_key_selector(self):
     sel = xmlutil.Selector('test2', 'attrs')
     self.assertIsNone(sel(self.obj_for_test))
     self.assertRaises(KeyError, sel, self.obj_for_test, True)
Beispiel #11
0
 def test_items_selector(self):
     sel = xmlutil.Selector('test', 'attrs', xmlutil.get_items)
     self.assertEqual(3, len(sel.chain))
     self.assertEqual(xmlutil.get_items, sel.chain[2])
     for key, val in sel(self.obj_for_test):
         self.assertEqual(self.obj_for_test['test']['attrs'][key], val)
Beispiel #12
0
 def test_datum_selector(self):
     sel = xmlutil.Selector('test', 'name')
     self.assertEqual(2, len(sel.chain))
     self.assertEqual('test', sel.chain[0])
     self.assertEqual('name', sel.chain[1])
     self.assertEqual('test', sel(self.obj_for_test))
Beispiel #13
0
 def test_dict_selector(self):
     sel = xmlutil.Selector('test')
     self.assertEqual(1, len(sel.chain))
     self.assertEqual('test', sel.chain[0])
     self.assertEqual(self.obj_for_test['test'],
                      sel(self.obj_for_test))
Beispiel #14
0
 def test_empty_selector(self):
     sel = xmlutil.Selector()
     self.assertEqual(0, len(sel.chain))
     self.assertEqual(self.obj_for_test, sel(self.obj_for_test))
Beispiel #15
0
 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)