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          schema = etree.RelaxNG(file=f)
    113
    114 -    def test_relaxng_shortcut(self):
    115          tree_valid = self.parse('<a><b></b></a>')
    116          tree_invalid = self.parse('<a><c></c></a>')
    117          schema = self.parse('''\
    118  <element name="a" xmlns="http://relaxng.org/ns/structure/1.0">
    119    <zeroOrMore>
    120       <element name="b">
    121         <text />
    122       </element>
    123    </zeroOrMore>
    124  </element>
    125  ''')
    126          self.assert_(tree_valid.relaxng(schema))
    127          self.assert_(not tree_invalid.relaxng(schema))
    128
    129 -    def test_multiple_elementrees(self):
    130          tree = self.parse('<a><b>B</b><c>C</c></a>')
    131          schema = etree.RelaxNG( self.parse('''\
    132  <element name="a" xmlns="http://relaxng.org/ns/structure/1.0">
    133    <element name="b">
    134      <text />
    135    </element>
    136    <element name="c">
    137      <text />
    138    </element>
    139  </element>
    140  ''') )
    141          self.assert_(schema.validate(tree))
    142          self.assert_(schema.validate(tree))
    143
    144          schema = etree.RelaxNG( self.parse('''\
    145  <element name="b" xmlns="http://relaxng.org/ns/structure/1.0">
    146    <text />
    147  </element>
    148  ''') )
    149          c_tree = etree.ElementTree(tree.getroot()[1])
    150          self.assertEqual(self._rootstring(c_tree), _bytes('<c>C</c>'))
    151          self.assert_(not schema.validate(c_tree))
    152
    153          b_tree = etree.ElementTree(tree.getroot()[0])
    154          self.assertEqual(self._rootstring(b_tree), _bytes('<b>B</b>'))
    155          self.assert_(schema.validate(b_tree))
    156
    157
    158 -def test_suite():
    159      suite = unittest.TestSuite()
    160      suite.addTests([unittest.makeSuite(ETreeRelaxNGTestCase)])
    161      suite.addTests(
    162          [make_doctest('../../../doc/validation.txt')])
    163      return suite
    164
    165  if __name__ == '__main__':
    166      print('to test use test.py %s' % __file__)
    167