lxml.tests.test_relaxng

Package lxml :: Package tests :: Module test_relaxng |
[hide private]
[frames] | no frames]

# Source Code for Module lxml.tests.test_relaxng

      1  # -*- coding: utf-8 -*-
      2
      3  """
      4  Test cases related to RelaxNG parsing and validation
      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 etree, BytesIO, _bytes, HelperTestCase, fileInTestDir
     14  from common_imports import doctest, make_doctest
     15
     16 -class ETreeRelaxNGTestCase(HelperTestCase):
     17 -    def test_relaxng(self):
     18          tree_valid = self.parse('<a><b></b></a>')
     19          tree_invalid = self.parse('<a><c></c></a>')
     20          schema = self.parse('''\
     21  <element name="a" xmlns="http://relaxng.org/ns/structure/1.0">
     22    <zeroOrMore>
     23       <element name="b">
     24         <text />
     25       </element>
     26    </zeroOrMore>
     27  </element>
     28  ''')
     29          schema = etree.RelaxNG(schema)
     30          self.assert_(schema.validate(tree_valid))
     31          self.assert_(not schema.validate(tree_invalid))
     32
     33 -    def test_relaxng_stringio(self):
     34          tree_valid = self.parse('<a><b></b></a>')
     35          tree_invalid = self.parse('<a><c></c></a>')
     36          schema_file = BytesIO('''\
     37  <element name="a" xmlns="http://relaxng.org/ns/structure/1.0">
     38    <zeroOrMore>
     39       <element name="b">
     40         <text />
     41       </element>
     42    </zeroOrMore>
     43  </element>
     44  ''')
     45          schema = etree.RelaxNG(file=schema_file)
     46          self.assert_(schema.validate(tree_valid))
     47          self.assert_(not schema.validate(tree_invalid))
     48
     49 -    def test_relaxng_elementtree_error(self):
     50          self.assertRaises(ValueError, etree.RelaxNG, etree.ElementTree())
     51
     52 -    def test_relaxng_error(self):
     53          tree_invalid = self.parse('<a><c></c></a>')
     54          schema = self.parse('''\
     55  <element name="a" xmlns="http://relaxng.org/ns/structure/1.0">
     56    <zeroOrMore>
     57       <element name="b">
     58         <text />
     59       </element>
     60    </zeroOrMore>
     61  </element>
     62  ''')
     63          schema = etree.RelaxNG(schema)
     64          self.assert_(not schema.validate(tree_invalid))
     65          errors = schema.error_log
     66          self.assert_([ log for log in errors
     67                         if log.level_name == "ERROR" ])
     68          self.assert_([ log for log in errors
     69                         if "not expect" in log.message ])
     70
     71 -    def test_relaxng_invalid_schema(self):
     72          schema = self.parse('''\
     73  <element name="a" xmlns="http://relaxng.org/ns/structure/1.0">
     74    <zeroOrMore>
     75       <element name="b" />
     76    </zeroOrMore>
     77  </element>
     78  ''')
     79          self.assertRaises(etree.RelaxNGParseError,
     80                            etree.RelaxNG, schema)
     81
     82 -    def test_relaxng_invalid_schema2(self):
     83          schema = self.parse('''\
     84  <grammar xmlns="http://relaxng.org/ns/structure/1.0" />
     85  ''')
     86          self.assertRaises(etree.RelaxNGParseError,
     87                            etree.RelaxNG, schema)
     88
     89 -    def test_relaxng_invalid_schema3(self):
     90          schema = self.parse('''\
     91  <grammar xmlns="http://relaxng.org/ns/structure/1.0">
     92    <define name="test">
     93      <element name="test"/>
     94    </define>
     95  </grammar>
     96  ''')
     97          self.assertRaises(etree.RelaxNGParseError,
     98                            etree.RelaxNG, schema)
     99
    100 -    def test_relaxng_invalid_schema4(self):
    101          # segfault
    102          schema = self.parse('''\
    103  <element name="a" xmlns="mynamespace" />
    104  ''')
    105          self.assertRaises(etree.RelaxNGParseError,
    106                            etree.RelaxNG, schema)
    107
    108 -    def test_relaxng_include(self):
    109          # this will only work if we access the file through path or
    110          # file object..
    111          f = open(fileInTestDir('test1.rng'), 'rb')
    112          try:
    113              schema = etree.RelaxNG(file=f)
    114          finally:
    115              f.close()
    116
    117 -    def test_relaxng_shortcut(self):
    118          tree_valid = self.parse('<a><b></b></a>')
    119          tree_invalid = self.parse('<a><c></c></a>')
    120          schema = self.parse('''\
    121  <element name="a" xmlns="http://relaxng.org/ns/structure/1.0">
    122    <zeroOrMore>
    123       <element name="b">
    124         <text />
    125       </element>
    126    </zeroOrMore>
    127  </element>
    128  ''')
    129          self.assert_(tree_valid.relaxng(schema))
    130          self.assert_(not tree_invalid.relaxng(schema))
    131
    132 -    def test_multiple_elementrees(self):
    133          tree = self.parse('<a><b>B</b><c>C</c></a>')
    134          schema = etree.RelaxNG( self.parse('''\
    135  <element name="a" xmlns="http://relaxng.org/ns/structure/1.0">
    136    <element name="b">
    137      <text />
    138    </element>
    139    <element name="c">
    140      <text />
    141    </element>
    142  </element>
    143  ''') )
    144          self.assert_(schema.validate(tree))
    145          self.assert_(schema.validate(tree))
    146
    147          schema = etree.RelaxNG( self.parse('''\
    148  <element name="b" xmlns="http://relaxng.org/ns/structure/1.0">
    149    <text />
    150  </element>
    151  ''') )
    152          c_tree = etree.ElementTree(tree.getroot()[1])
    153          self.assertEqual(self._rootstring(c_tree), _bytes('<c>C</c>'))
    154          self.assert_(not schema.validate(c_tree))
    155
    156          b_tree = etree.ElementTree(tree.getroot()[0])
    157          self.assertEqual(self._rootstring(b_tree), _bytes('<b>B</b>'))
    158          self.assert_(schema.validate(b_tree))
    159
    160
    161 -def test_suite():
    162      suite = unittest.TestSuite()
    163      suite.addTests([unittest.makeSuite(ETreeRelaxNGTestCase)])
    164      suite.addTests(
    165          [make_doctest('../../../doc/validation.txt')])
    166      return suite
    167
    168  if __name__ == '__main__':
    169      print('to test use test.py %s' % __file__)
    170