1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
|
# -*- 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('<AAA><BBB/><CCC/></AAA>')
tree_invalid = self.parse('<AAA><BBB/><CCC/><DDD/></AAA>')
schema = self.parse('''\
<schema xmlns="http://purl.oclc.org/dsdl/schematron" >
<pattern name="Open model">
<rule context="AAA">
<assert test="BBB"> BBB element is not present</assert>
<assert test="CCC"> CCC element is not present</assert>
</rule>
</pattern>
<pattern name="Closed model">
<rule context="AAA">
<assert test="BBB"> BBB element is not present</assert>
<assert test="CCC"> CCC element is not present</assert>
<assert test="count(BBB|CCC) = count (*)">There is an extra element</assert>
</rule>
</pattern>
</schema>
''')
schema = etree.Schematron(schema)
self.assert_(schema.validate(tree_valid))
self.assert_(not schema.validate(tree_invalid))
def test_schematron_elementtree_error(self):
self.assertRaises(ValueError, etree.Schematron, etree.ElementTree())
def test_schematron_invalid_schema(self):
schema = self.parse('''\
<schema xmlns="http://purl.oclc.org/dsdl/schematron" >
<pattern name="Open model">
</pattern>
</schema>
''')
self.assertRaises(etree.SchematronParseError,
etree.Schematron, schema)
def test_schematron_invalid_schema_empty(self):
schema = self.parse('''\
<schema xmlns="http://purl.oclc.org/dsdl/schematron" />
''')
self.assertRaises(etree.SchematronParseError,
etree.Schematron, schema)
def test_schematron_invalid_schema_namespace(self):
# segfault
schema = self.parse('''\
<schema xmlns="mynamespace" />
''')
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__)
|