1
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)
12
13 from common_imports import etree, HelperTestCase, fileInTestDir
14 from common_imports import doctest, make_doctest
15
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))
45 self.assertFalse(schema.error_log.filter_from_errors())
46
49
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
66
74
75
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