lxml.tests.test_schematron

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

# Source Code for Module lxml.tests.test_schematron

     1  # -*- coding: utf-8 -*-
     2
     3  """
     4  Test cases related to Schematron 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, HelperTestCase, fileInTestDir
    14  from common_imports import doctest, make_doctest
    15
    16 -class ETreeSchematronTestCase(HelperTestCase):
    17 -    def test_schematron(self):
    18          tree_valid = self.parse('<AAA><BBB/><CCC/></AAA>')
    19          tree_invalid = self.parse('<AAA><BBB/><CCC/><DDD/></AAA>')
    20          schema = self.parse('''\
    21  <schema xmlns="http://purl.oclc.org/dsdl/schematron" >
    22       <pattern name="Open model">
    23            <rule context="AAA">
    24                 <assert test="BBB"> BBB element is not present</assert>
    25                 <assert test="CCC"> CCC element is not present</assert>
    26            </rule>
    27       </pattern>
    28       <pattern name="Closed model">
    29            <rule context="AAA">
    30                 <assert test="BBB"> BBB element is not present</assert>
    31                 <assert test="CCC"> CCC element is not present</assert>
    32                 <assert test="count(BBB|CCC) = count (*)">There is an extra element</assert>
    33            </rule>
    34       </pattern>
    35  </schema>
    36  ''')
    37          schema = etree.Schematron(schema)
    38          self.assertTrue(schema.validate(tree_valid))
    39          self.assertFalse(schema.error_log.filter_from_errors())
    40
    41          self.assertFalse(schema.validate(tree_invalid))
    42          self.assertTrue(schema.error_log.filter_from_errors())
    43
    44          self.assertTrue(schema.validate(tree_valid))             # repeat valid
    45          self.assertFalse(schema.error_log.filter_from_errors())  # repeat valid
    46
    47 -    def test_schematron_elementtree_error(self):
    48          self.assertRaises(ValueError, etree.Schematron, etree.ElementTree())
    49
    50 -    def test_schematron_invalid_schema(self):
    51          schema = self.parse('''\
    52  <schema xmlns="http://purl.oclc.org/dsdl/schematron" >
    53       <pattern name="Open model">
    54       </pattern>
    55  </schema>
    56  ''')
    57          self.assertRaises(etree.SchematronParseError,
    58                            etree.Schematron, schema)
    59
    60 -    def test_schematron_invalid_schema_empty(self):
    61          schema = self.parse('''\
    62  <schema xmlns="http://purl.oclc.org/dsdl/schematron" />
    63  ''')
    64          self.assertRaises(etree.SchematronParseError,
    65                            etree.Schematron, schema)
    66
    67 -    def test_schematron_invalid_schema_namespace(self):
    68          # segfault
    69          schema = self.parse('''\
    70  <schema xmlns="mynamespace" />
    71  ''')
    72          self.assertRaises(etree.SchematronParseError,
    73                            etree.Schematron, schema)
    74
    75
    76 -def test_suite():
    77      suite = unittest.TestSuite()
    78      suite.addTests([unittest.makeSuite(ETreeSchematronTestCase)])
    79      suite.addTests(
    80          [make_doctest('../../../doc/validation.txt')])
    81      return suite
    82
    83  if __name__ == '__main__':
    84      print('to test use test.py %s' % __file__)
    85