Package lxml :: Package tests :: Module test_sax

[hide private]
[frames] | no frames]

# Source Code for Module lxml.tests.test_sax

      1  # -*- coding: utf-8 -*-
      2
      3  """
      4  Test cases related to SAX I/O
      5  """
      6
      7  import unittest, sys, os.path
      8
      9  this_dir = os.path.dirname(__file__)
     10  if this_dir not in sys.path:
     11      sys.path.insert(0, this_dir) # needed for Py3
     12
     13  from common_imports import HelperTestCase, make_doctest, BytesIO, _bytes
     14  from lxml import sax
     15  from xml.dom import pulldom
     16
     17
     18 -class ETreeSaxTestCase(HelperTestCase):
     19
     20 -    def test_etree_sax_simple(self):
     21          tree = self.parse('<a>ab<b/>ba</a>')
     22          xml_out = self._saxify_serialize(tree)
     23          self.assertEqual(_bytes('<a>ab<b/>ba</a>'),
     24                            xml_out)
     25
     26 -    def test_etree_sax_double(self):
     27          tree = self.parse('<a>ab<b>bb</b>ba</a>')
     28          xml_out = self._saxify_serialize(tree)
     29          self.assertEqual(_bytes('<a>ab<b>bb</b>ba</a>'),
     30                            xml_out)
     31
     32 -    def test_etree_sax_comment(self):
     33          tree = self.parse('<a>ab<!-- TEST -->ba</a>')
     34          xml_out = self._saxify_serialize(tree)
     35          self.assertEqual(_bytes('<a>abba</a>'),
     36                            xml_out)
     37
     38 -    def test_etree_sax_pi(self):
     39          tree = self.parse('<a>ab<?this and that?>ba</a>')
     40          xml_out = self._saxify_serialize(tree)
     41          self.assertEqual(_bytes('<a>ab<?this and that?>ba</a>'),
     42                            xml_out)
     43
     44 -    def test_etree_sax_comment_root(self):
     45          tree = self.parse('<!-- TEST --><a>ab</a>')
     46          xml_out = self._saxify_serialize(tree)
     47          self.assertEqual(_bytes('<a>ab</a>'),
     48                            xml_out)
     49
     50 -    def test_etree_sax_pi_root(self):
     51          tree = self.parse('<?this and that?><a>ab</a>')
     52          xml_out = self._saxify_serialize(tree)
     53          self.assertEqual(_bytes('<?this and that?><a>ab</a>'),
     54                            xml_out)
     55
     56 -    def test_etree_sax_attributes(self):
     57          tree = self.parse('<a aa="5">ab<b b="5"/>ba</a>')
     58          xml_out = self._saxify_serialize(tree)
     59          self.assertEqual(_bytes('<a aa="5">ab<b b="5"/>ba</a>'),
     60                            xml_out)
     61
     62 -    def test_etree_sax_ns1(self):
     63          tree = self.parse('<a xmlns="bla">ab<b>bb</b>ba</a>')
     64          new_tree = self._saxify_unsaxify(tree)
     65          root = new_tree.getroot()
     66          self.assertEqual('{bla}a',
     67                           root.tag)
     68          self.assertEqual('{bla}b',
     69                           root[0].tag)
     70
     71 -    def test_etree_sax_ns2(self):
     72          tree = self.parse('<a xmlns="blaA">ab<b:b xmlns:b="blaB">bb</b:b>ba</a>')
     73          new_tree = self._saxify_unsaxify(tree)
     74          root = new_tree.getroot()
     75          self.assertEqual('{blaA}a',
     76                           root.tag)
     77          self.assertEqual('{blaB}b',
     78                           root[0].tag)
     79
     80 -    def test_sax_to_pulldom(self):
     81          tree = self.parse('<a xmlns="blaA">ab<b:b xmlns:b="blaB">bb</b:b>ba</a>')
     82          handler = pulldom.SAX2DOM()
     83          sax.saxify(tree, handler)
     84          dom = handler.document
     85
     86          self.assertEqual('a',
     87                           dom.firstChild.localName)
     88          self.assertEqual('blaA',
     89                           dom.firstChild.namespaceURI)
     90
     91          children = dom.firstChild.childNodes
     92          self.assertEqual('ab',
     93                           children[0].nodeValue)
     94          self.assertEqual('blaB',
     95                           children[1].namespaceURI)
     96          self.assertEqual('ba',
     97                           children[2].nodeValue)
     98
     99 -    def test_element_sax(self):
    100          tree = self.parse('<a><b/></a>')
    101          a = tree.getroot()
    102          b = a[0]
    103
    104          xml_out = self._saxify_serialize(a)
    105          self.assertEqual(_bytes('<a><b/></a>'),
    106                            xml_out)
    107
    108          xml_out = self._saxify_serialize(b)
    109          self.assertEqual(_bytes('<b/>'),
    110                            xml_out)
    111
    112 -    def test_element_sax_ns(self):
    113          tree = self.parse('<a:a xmlns:a="blaA"><b/></a:a>')
    114          a = tree.getroot()
    115          b = a[0]
    116
    117          new_tree = self._saxify_unsaxify(a)
    118          root = new_tree.getroot()
    119          self.assertEqual('{blaA}a',
    120                           root.tag)
    121          self.assertEqual('b',
    122                           root[0].tag)
    123
    124          new_tree = self._saxify_unsaxify(b)
    125          root = new_tree.getroot()
    126          self.assertEqual('b',
    127                           root.tag)
    128          self.assertEqual(0,
    129                           len(root))
    130
    131 -    def test_etree_sax_handler_default_ns(self):
    132          handler = sax.ElementTreeContentHandler()
    133          handler.startDocument()
    134          handler.startPrefixMapping(None, 'blaA')
    135          handler.startElementNS(('blaA', 'a'), 'a', {})
    136          handler.startPrefixMapping(None, 'blaB')
    137          handler.startElementNS(('blaB', 'b'), 'b', {})
    138          handler.endElementNS(  ('blaB', 'b'), 'b')
    139          handler.endPrefixMapping(None)
    140          handler.startElementNS(('blaA', 'c'), 'c', {})
    141          handler.endElementNS(  ('blaA', 'c'), 'c')
    142          handler.endElementNS(  ('blaA', 'a'), 'a')
    143          handler.endPrefixMapping(None)
    144          handler.endDocument()
    145
    146          new_tree = handler.etree
    147          root = new_tree.getroot()
    148          self.assertEqual('{blaA}a',
    149                           root.tag)
    150          self.assertEqual('{blaB}b',
    151                           root[0].tag)
    152          self.assertEqual('{blaA}c',
    153                           root[1].tag)
    154
    155 -    def test_etree_sax_handler_default_ns_None(self):
    156          handler = sax.ElementTreeContentHandler()
    157          handler.startDocument()
    158          handler.startPrefixMapping(None, 'blaA')
    159          handler.startElementNS((None, 'a'), 'a', {})
    160          handler.startPrefixMapping(None, 'blaB')
    161          handler.startElementNS((None, 'b'), 'b', {})
    162          handler.endElementNS(  (None, 'b'), 'b')
    163          handler.endPrefixMapping(None)
    164          handler.startElementNS((None, 'c'), 'c', {})
    165          handler.endElementNS(  (None, 'c'), 'c')
    166          handler.endElementNS(  (None, 'a'), 'a')
    167          handler.endPrefixMapping(None)
    168          handler.endDocument()
    169
    170          new_tree = handler.etree
    171          root = new_tree.getroot()
    172          self.assertEqual('{blaA}a',
    173                           root.tag)
    174          self.assertEqual('{blaB}b',
    175                           root[0].tag)
    176          self.assertEqual('{blaA}c',
    177                           root[1].tag)
    178
    179 -    def test_etree_sax_redefine_ns(self):
    180          handler = sax.ElementTreeContentHandler()
    181          handler.startDocument()
    182          handler.startPrefixMapping('ns', 'blaA')
    183          handler.startElementNS(('blaA', 'a'), 'ns:a', {})
    184          handler.startPrefixMapping('ns', 'blaB')
    185          handler.startElementNS(('blaB', 'b'), 'ns:b', {})
    186          handler.endElementNS(  ('blaB', 'b'), 'ns:b')
    187          handler.endPrefixMapping('ns')
    188          handler.startElementNS(('blaA', 'c'), 'ns:c', {})
    189          handler.endElementNS(  ('blaA', 'c'), 'ns:c')
    190          handler.endElementNS(  ('blaA', 'a'), 'ns:a')
    191          handler.endPrefixMapping('ns')
    192          handler.endDocument()
    193
    194          new_tree = handler.etree
    195          root = new_tree.getroot()
    196          self.assertEqual('{blaA}a',
    197                           root.tag)
    198          self.assertEqual('{blaB}b',
    199                           root[0].tag)
    200          self.assertEqual('{blaA}c',
    201                           root[1].tag)
    202
    203 -    def test_etree_sax_no_ns(self):
    204          handler = sax.ElementTreeContentHandler()
    205          handler.startDocument()
    206          handler.startElement('a', {})
    207          handler.startElement('b', {})
    208          handler.endElement('b')
    209          handler.startElement('c') # with empty attributes
    210          handler.endElement('c')
    211          handler.endElement('a')
    212          handler.endDocument()
    213
    214          new_tree = handler.etree
    215          root = new_tree.getroot()
    216          self.assertEqual('a', root.tag)
    217          self.assertEqual('b', root[0].tag)
    218          self.assertEqual('c', root[1].tag)
    219
    220 -    def test_etree_sax_no_ns_attributes(self):
    221          handler = sax.ElementTreeContentHandler()
    222          handler.startDocument()
    223          handler.startElement('a', {"attr_a1": "a1"})
    224          handler.startElement('b', {"attr_b1": "b1"})
    225          handler.endElement('b')
    226          handler.endElement('a')
    227          handler.endDocument()
    228
    229          new_tree = handler.etree
    230          root = new_tree.getroot()
    231          self.assertEqual('a', root.tag)
    232          self.assertEqual('b', root[0].tag)
    233          self.assertEqual('a1', root.attrib["attr_a1"])
    234          self.assertEqual('b1', root[0].attrib["attr_b1"])
    235
    236 -    def test_etree_sax_ns_attributes(self):
    237          handler = sax.ElementTreeContentHandler()
    238          handler.startDocument()
    239
    240          self.assertRaises(ValueError,
    241              handler.startElement,
    242              'a', {"blaA:attr_a1": "a1"}
    243          )
    244
    245 -    def test_etree_sax_error(self):
    246          handler = sax.ElementTreeContentHandler()
    247          handler.startDocument()
    248          handler.startElement('a')
    249          self.assertRaises(sax.SaxError, handler.endElement, 'b')
    250
    251 -    def test_etree_sax_error2(self):
    252          handler = sax.ElementTreeContentHandler()
    253          handler.startDocument()
    254          handler.startElement('a')
    255          handler.startElement('b')
    256          self.assertRaises(sax.SaxError, handler.endElement, 'a')
    257
    258 -    def _saxify_unsaxify(self, saxifiable):
    259          handler = sax.ElementTreeContentHandler()
    260          sax.ElementTreeProducer(saxifiable, handler).saxify()
    261          return handler.etree
    262
    263 -    def _saxify_serialize(self, tree):
    264          new_tree = self._saxify_unsaxify(tree)
    265          f = BytesIO()
    266          new_tree.write(f)
    267          return f.getvalue().replace(_bytes('\n'), _bytes(''))
    268
    269
    270 -def test_suite():
    271      suite = unittest.TestSuite()
    272      suite.addTests([unittest.makeSuite(ETreeSaxTestCase)])
    273      suite.addTests(
    274          [make_doctest('../../../doc/sax.txt')])
    275      return suite
    276
    277
    278  if __name__ == '__main__':
    279      print('to test use test.py %s' % __file__)
    280