コード例 #1
0
 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': {}
     }
コード例 #2
0
ファイル: test_helpers.py プロジェクト: stxnext/mappet
 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': {}
     }
コード例 #3
0
    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',
            }
        }
コード例 #4
0
    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',
            }
        }
コード例 #5
0
ファイル: test_helpers.py プロジェクト: stxnext/mappet
    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',
            }
        }
コード例 #6
0
ファイル: test_helpers.py プロジェクト: pdyba/mappet
    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': ' ',
            }
        }
コード例 #7
0
    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': ' ',
            }
        }
コード例 #8
0
ファイル: test_helpers.py プロジェクト: pdyba/mappet
    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',
            }
        }
コード例 #9
0
ファイル: test_helpers.py プロジェクト: zefhemel/mappet
    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"}}