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.assertTrue(not schema.validate(tree_invalid))
40
43
45 schema = self.parse('''\
46 <schema xmlns="http://purl.oclc.org/dsdl/schematron" >
47 <pattern name="Open model">
48 </pattern>
49 </schema>
50 ''')
51 self.assertRaises(etree.SchematronParseError,
52 etree.Schematron, schema)
53
60
68
69
71 suite = unittest.TestSuite()
72 suite.addTests([unittest.makeSuite(ETreeSchematronTestCase)])
73 suite.addTests(
74 [make_doctest('../../../doc/validation.txt')])
75 return suite
76
77 if __name__ == '__main__':
78 print('to test use test.py %s' % __file__)
79