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  from __future__ import absolute_import 
 8   
 9  import unittest 
10   
11  from .common_imports import etree, HelperTestCase, make_doctest 
12   
13   
14 -class ETreeSchematronTestCase(HelperTestCase):
15 - def test_schematron(self):
16 tree_valid = self.parse('<AAA><BBB/><CCC/></AAA>') 17 tree_invalid = self.parse('<AAA><BBB/><CCC/><DDD/></AAA>') 18 schema = self.parse('''\ 19 <schema xmlns="http://purl.oclc.org/dsdl/schematron" > 20 <pattern name="Open model"> 21 <rule context="AAA"> 22 <assert test="BBB"> BBB element is not present</assert> 23 <assert test="CCC"> CCC element is not present</assert> 24 </rule> 25 </pattern> 26 <pattern name="Closed model"> 27 <rule context="AAA"> 28 <assert test="BBB"> BBB element is not present</assert> 29 <assert test="CCC"> CCC element is not present</assert> 30 <assert test="count(BBB|CCC) = count (*)">There is an extra element</assert> 31 </rule> 32 </pattern> 33 </schema> 34 ''') 35 schema = etree.Schematron(schema) 36 self.assertTrue(schema.validate(tree_valid)) 37 self.assertFalse(schema.error_log.filter_from_errors()) 38 39 self.assertFalse(schema.validate(tree_invalid)) 40 self.assertTrue(schema.error_log.filter_from_errors()) 41 42 self.assertTrue(schema.validate(tree_valid)) # repeat valid 43 self.assertFalse(schema.error_log.filter_from_errors()) # repeat valid
44
46 self.assertRaises(ValueError, etree.Schematron, etree.ElementTree())
47
49 schema = self.parse('''\ 50 <schema xmlns="http://purl.oclc.org/dsdl/schematron" > 51 <pattern name="Open model"> 52 </pattern> 53 </schema> 54 ''') 55 self.assertRaises(etree.SchematronParseError, 56 etree.Schematron, schema)
57
59 schema = self.parse('''\ 60 <schema xmlns="http://purl.oclc.org/dsdl/schematron" /> 61 ''') 62 self.assertRaises(etree.SchematronParseError, 63 etree.Schematron, schema)
64
66 # segfault 67 schema = self.parse('''\ 68 <schema xmlns="mynamespace" /> 69 ''') 70 self.assertRaises(etree.SchematronParseError, 71 etree.Schematron, schema)
72 73
74 -def test_suite():
75 suite = unittest.TestSuite() 76 suite.addTests([unittest.makeSuite(ETreeSchematronTestCase)]) 77 suite.addTests( 78 [make_doctest('../../../doc/validation.txt')]) 79 return suite
80 81 if __name__ == '__main__': 82 print('to test use test.py %s' % __file__) 83