def test_etree_to_dict_comments_handling(self): u"""Tests lxml.etree tree conversion to Python dict with comment handling.""" comment_node = etree.Comment('a_comment_node') self.root.insert(0, comment_node) assert helpers.etree_to_dict(self.root) == { 'root': { '#comments': 'a_comment_node' } } assert helpers.etree_to_dict(self.root, without_comments=True) == { 'root': {} }
def test_etree_to_dict(self): u"""Tests lxml.etree tree conversion to Python dict.""" # A single node. assert helpers.etree_to_dict(self.root) == {'root': None} # Children without text. child1 = etree.SubElement(self.root, 'child1') child2 = etree.SubElement(self.root, 'child2') assert helpers.etree_to_dict(self.root) == { 'root': { 'child1': None, 'child2': None } } # Attributes should be represented with the '@' symbol. child2.set('is_orphan', 'Not yet') assert helpers.etree_to_dict(self.root) == { 'root': { 'child1': None, 'child2': { '@is_orphan': 'Not yet' } } } # Node values should be carried over to the dict. child1.text = 'hello' assert helpers.etree_to_dict(self.root) == { 'root': { 'child1': 'hello', 'child2': { '@is_orphan': 'Not yet' } } } # A node with both attributes and text should have text assigned to the # '#text' key in the dict. tag = etree.Element('root') tag.set('attr1', 'value1') tag.text = 'some_text' assert helpers.etree_to_dict(tag) == { 'root': { '@attr1': 'value1', '#text': 'some_text', } }
def test__dict_to_etree__given_node_with_trim(self): tag = etree.Element('root') tag.text = ' ' assert helpers.etree_to_dict(tag) == { 'root': None, } tag = etree.Element('root') tag.set('attr1', 'value1') tag.text = ' ' assert helpers.etree_to_dict(tag, ) == { 'root': { '@attr1': 'value1', } }
def test__dict_to_etree__given_node_with_trim(self): tag = etree.Element('root') tag.text = ' ' assert helpers.etree_to_dict(tag) == { 'root': None, } tag = etree.Element('root') tag.set('attr1', 'value1') tag.text = ' ' assert helpers.etree_to_dict(tag,) == { 'root': { '@attr1': 'value1', } }
def test__dict_to_etree__given_node_with_whitespace__should_preserve_it(self): tag = etree.Element('root') tag.text = ' ' assert helpers.etree_to_dict(tag) == { 'root': ' ', } tag = etree.Element('root') tag.set('attr1', 'value1') tag.text = ' ' assert helpers.etree_to_dict(tag) == { 'root': { '@attr1': 'value1', '#text': ' ', } }
def test__dict_to_etree__given_node_with_whitespace__should_preserve_it( self): tag = etree.Element('root') tag.text = ' ' assert helpers.etree_to_dict(tag, trim=False) == { 'root': ' ', } tag = etree.Element('root') tag.set('attr1', 'value1') tag.text = ' ' assert helpers.etree_to_dict(tag, trim=False) == { 'root': { '@attr1': 'value1', '#text': ' ', } }
def test_etree_to_dict(self): u"""Tests lxml.etree tree conversion to Python dict.""" # A single node. assert helpers.etree_to_dict(self.root) == {'root': None} # Children without text. child1 = etree.SubElement(self.root, 'child1') child2 = etree.SubElement(self.root, 'child2') assert helpers.etree_to_dict(self.root) == {'root': {'child1': None, 'child2': None}} # Attributes should be represented with the '@' symbol. child2.set('is_orphan', 'Not yet') assert helpers.etree_to_dict(self.root) == { 'root': { 'child1': None, 'child2': {'@is_orphan': 'Not yet'} } } # Node values should be carried over to the dict. child1.text = 'hello' assert helpers.etree_to_dict(self.root) == { 'root': { 'child1': 'hello', 'child2': {'@is_orphan': 'Not yet'} } } # A node with both attributes and text should have text assigned to the # '#text' key in the dict. tag = etree.Element('root') tag.set('attr1', 'value1') tag.text = 'some_text' assert helpers.etree_to_dict(tag) == { 'root': { '@attr1': 'value1', '#text': 'some_text', } }
def test_etree_to_dict(self): u"""Tests lxml.etree tree conversion to Python dict.""" # A single node. assert helpers.etree_to_dict(self.root) == {"root": None} # Children without text. child1 = etree.SubElement(self.root, "child1") child2 = etree.SubElement(self.root, "child2") assert helpers.etree_to_dict(self.root) == {"root": {"child1": None, "child2": None}} # Attributes should be represented with the '@' symbol. child2.set("is_orphan", "Not yet") assert helpers.etree_to_dict(self.root) == {"root": {"child1": None, "child2": {"@is_orphan": "Not yet"}}} # Node values should be carried over to the dict. child1.text = "hello" assert helpers.etree_to_dict(self.root) == {"root": {"child1": "hello", "child2": {"@is_orphan": "Not yet"}}} # A node with both attributes and text should have text assigned to the # '#text' key in the dict. tag = etree.Element("root") tag.set("attr1", "value1") tag.text = "some_text" assert helpers.etree_to_dict(tag) == {"root": {"@attr1": "value1", "#text": "some_text"}}