# -*- coding: utf-8 -*-
"""
Test cases related to Schematron parsing and validation
"""
import unittest, sys, os.path
this_dir = os.path.dirname(__file__)
if this_dir not in sys.path:
sys.path.insert(0, this_dir) # needed for Py3
from common_imports import etree, HelperTestCase, fileInTestDir
from common_imports import doctest, make_doctest
class ETreeSchematronTestCase(HelperTestCase):
def test_schematron(self):
tree_valid = self.parse('')
tree_invalid = self.parse('')
schema = self.parse('''\
BBB element is not present
CCC element is not present
BBB element is not present
CCC element is not present
There is an extra element
''')
schema = etree.Schematron(schema)
self.assertTrue(schema.validate(tree_valid))
self.assertFalse(schema.error_log.filter_from_errors())
self.assertFalse(schema.validate(tree_invalid))
self.assertTrue(schema.error_log.filter_from_errors())
self.assertTrue(schema.validate(tree_valid)) # repeat valid
self.assertFalse(schema.error_log.filter_from_errors()) # repeat valid
def test_schematron_elementtree_error(self):
self.assertRaises(ValueError, etree.Schematron, etree.ElementTree())
def test_schematron_invalid_schema(self):
schema = self.parse('''\
''')
self.assertRaises(etree.SchematronParseError,
etree.Schematron, schema)
def test_schematron_invalid_schema_empty(self):
schema = self.parse('''\
''')
self.assertRaises(etree.SchematronParseError,
etree.Schematron, schema)
def test_schematron_invalid_schema_namespace(self):
# segfault
schema = self.parse('''\
''')
self.assertRaises(etree.SchematronParseError,
etree.Schematron, schema)
def test_suite():
suite = unittest.TestSuite()
suite.addTests([unittest.makeSuite(ETreeSchematronTestCase)])
suite.addTests(
[make_doctest('../../../doc/validation.txt')])
return suite
if __name__ == '__main__':
print('to test use test.py %s' % __file__)