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