def test_init_with_dict(self): u"""Tests for initialization of mappet object with dict representing a XML tree.""" xml_dict = self.m.to_dict() m = mappet.Mappet(xml_dict) assert m.to_dict() == xml_dict assert mappet.Mappet({ 'a': { '#text': 'list_elem_1', '@attr1': 'val1' } }).to_str() == '<a attr1="val1">list_elem_1</a>' assert mappet.Mappet({ '#text': 'list_elem_1', '@attr1': 'val1' }).to_str() == '<root attr1="val1">list_elem_1</root>'
def test__len__(self): u"""Tests for counting the children.""" # 'root' node is ignored. assert len(self.m) == 4 assert len(self.m.node1) == 3 # Empty element should have no children. assert len(mappet.Mappet(etree.Element('root'))) == 0
def test_init_with_wrong_object(self): # Passing in a tuple should raise an exception. wrong_xml = ('node1', 'node2') with pytest.raises(AttributeError) as exc: mappet.Mappet(wrong_xml) assert 'Specified data cannot be used to construct a Mappet object' in str( exc.value)
def test_create__new_element_with_hyphens__will_be_created(self): u"""Tests for creating a node with hyphens in its name.""" m = mappet.Mappet(self.xml) m.node1.create('new-element', 'new_element_text') assert m.node1.to_dict() == { 'subnode1': [None, None], 'subnode2': 'subnode2_text', 'new-element': 'new_element_text', }
def test_xpath_regexp(self): result = self.m.xpath( "//*[re:test(., '^sub.*', 'subnode')]", regexp=True, ) assert len(result) == 5 x_node = etree.SubElement(self.xml, 'x_node') x_node.text = 'xxxxx' self.m = mappet.Mappet(self.xml) result = self.m.xpath("//*[re:test(., 'x{5}')]", regexp=True) assert result[-1] == x_node
def test_create__new_element_already_exists__will_raise(self): u"""Two identical elements cannot be created.""" m = mappet.Mappet(self.xml) m.node1.create('new-element', 'new_element_text') with pytest.raises(KeyError) as exc: # Creates an element that already exists. m.node1.create('new-element', 'new_element_text2') assert 'Node {} already exists in XML'.format('new-element') in str( exc.value)
def test__get_aliases(self): u"""Tests for generation of tagname aliases.""" assert self.m._get_aliases() == { 'node1': 'node1', 'node2': 'node2', 'node3': 'node3', 'node_list': 'node_list', } assert len(self.m._aliases) == 4 # A dict with aliases should return normalized names also. xml = etree.Element('root') etree.SubElement(xml, 'Normalize-Me') assert mappet.Mappet(xml)._get_aliases() == { 'normalize_me': 'Normalize-Me', }
def test_get__simple_node_and_node_with_attrs__should_return_same_tag_text( self): u"""Tests for value extraction from node with and without attributes. A node without attributes has a different structure than a one with. However, for text-only node, same text should be returned. """ xml = """<app> <data> <type>YEAR</type> </data> </app>""" xml_attr = """<app> <data> <type value="1">YEAR</type> </data> </app>""" m = mappet.Mappet(xml) m_attr = mappet.Mappet(xml_attr) # In both cases, the leaf's value is the same. assert m_attr.data.type.get() == m.data.type.get() # However, those nodes are not identical. assert m_attr.data.type != m.data.type
def test_xpath__given_existing_node__should_return_that_node(self): u"""A node that has children should be returned as Mapper object.""" self.xml.set('list', 'l1') self.xml.set('list', 'l1') self.xml.set('list', 'l2') self.xml.set('list', 'l3') etree.SubElement(self.xml, 'list') etree.SubElement(self.xml, 'list') m = mappet.Mappet(self.xml) xpath_evaluator_node = m.xpath('node1') xpath_node = m.xpath('node1', single_use=True) xpath_node_list = m.xpath('list/l1') assert xpath_evaluator_node.has_children() assert xpath_evaluator_node.to_dict() == m.node1.to_dict() assert xpath_evaluator_node == xpath_node assert isinstance(xpath_node_list, list)
def test__getattr__(self): u"""Tests for returning node's children.""" # node1 has two children called subnode1. n1 = self.m.node1.subnode1[0] assert n1 == self.m.node1.subnode1[0] assert set(self.m.node1.subnode1) == { self.m.node1.subnode1[0], self.m.node1.subnode1[1] } # If a node has only one child, it should be returned. one_child_node = etree.Element('root') subelement = etree.SubElement(one_child_node, 'subelement') assert mappet.Mappet(one_child_node).subelement == mappet.Literal( subelement) # Non-existing key should raise an exception. with pytest.raises(KeyError): self.m.node1.wrong_child
def test_set_literal_text_explicitly(self): u"""Tests for assignment and extraction of leaf's text using '#text' key.""" xml = '''<?xml version='1.0' encoding='iso-8859-2'?> <auth-request> <auth> <user type="admin" first-name="John" last-name="Johnny">123</user> <user type="admin" first-name="John" last-name="Johnny">123</user> <user type="edior" first-name="John" last-name="Johnny">123</user> </auth> </auth-request>''' m = mappet.Mappet(xml) # This is the only way to change leaf's text, that is returned by calling # 'user[1]'. m.auth.user[1]['#text'] = 'test' assert m.auth.user[1]['#text'] == 'test' assert m.auth.user[1].get() == 'test'
def setup(self): xml = etree.Element('root') xml.set('attr1', 'val1') xml.set('attr2', 'val2') node1 = etree.SubElement(xml, 'node1') etree.SubElement(xml, 'node2') etree.SubElement(xml, 'node3') etree.SubElement(node1, 'subnode1') etree.SubElement(node1, 'subnode1') subnode2 = etree.SubElement(node1, 'subnode2') subnode2.text = 'subnode2_text' node_list = etree.SubElement(xml, 'node_list') subnode = etree.SubElement(node_list, 'subnode') subnode.set('attr1', 'val1') etree.SubElement(node_list, 'subnode') etree.SubElement(node_list, 'subnode') subnode.text = 'subnode_text' self.xml = xml self.m = mappet.Mappet(xml)
def test_xpath__given_existing_node__should_return_that_node(self): u"""A node that has children should be returned as Mapper object.""" import time self.xml.set('list', 'l1') self.xml.set('list', 'l1') self.xml.set('list', 'l2') self.xml.set('list', 'l3') etree.SubElement(self.xml, 'list') etree.SubElement(self.xml, 'list') m = mappet.Mappet(self.xml) t0 = time.clock() node = m.xpath('node1') t1 = time.clock() - t0 t0 = time.clock() node_2 = m.xpath('node1', single_use=True) t2 = time.clock() - t0 node_3 = m.xpath('list/l1') assert node.has_children() assert node.to_dict() == m.node1.to_dict() assert node == node_2 assert isinstance(node_3, list) # time for single use should be faster in this explicit case. assert t2 < t1
def test__setstate__(self): u"""Tests for Mappet unpickling.""" import pickle picklestring = pickle.dumps(self.m) # Unplicked XML structure should be identical to the original one. assert mappet.Mappet(self.m._xml) == pickle.loads(picklestring)
def test_init_with_dict(self): u"""Tests for initialization of mappet object with dict representing a XML tree.""" xml_dict = self.m.to_dict() m = mappet.Mappet(xml_dict) assert m.to_dict() == xml_dict
def test_init_with_string(self): u"""Tests for initialization of mappet object with XML string.""" xml_str = '<root><node1 node_attr="val1"><leaf leaf_attr="val2">leaf_text</leaf></node1></root>' m = mappet.Mappet(xml_str) assert m.to_str() == xml_str