Package lxml :: Package tests :: Module test_schematron
[hide private]
[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, 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) # needed for Py3 
12   
13  from common_imports import etree, HelperTestCase, fileInTestDir 
14  from common_imports import doctest, make_doctest 
15   
16 -class ETreeSchematronTestCase(HelperTestCase):
17 - def test_schematron(self):
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
42 self.assertRaises(ValueError, etree.Schematron, etree.ElementTree())
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
55 schema = self.parse('''\ 56 <schema xmlns="http://purl.oclc.org/dsdl/schematron" /> 57 ''') 58 self.assertRaises(etree.SchematronParseError, 59 etree.Schematron, schema)
60
62 # segfault 63 schema = self.parse('''\ 64 <schema xmlns="mynamespace" /> 65 ''') 66 self.assertRaises(etree.SchematronParseError, 67 etree.Schematron, schema)
68 69
70 -def test_suite():
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