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