Package lxml :: Package tests :: Module test_relaxng
[frames] | no frames]

Source Code for Module lxml.tests.test_relaxng

  1  # -*- coding: utf-8 -*- 
  2   
  3  """ 
  4  Test cases related to RelaxNG parsing and validation 
  5  """ 
  6   
  7  import unittest 
  8   
  9  from common_imports import etree, doctest, StringIO, HelperTestCase, fileInTestDir 
 10   
11 -class ETreeRelaxNGTestCase(HelperTestCase):
12 - def test_relaxng(self):
13 tree_valid = self.parse('<a><b></b></a>') 14 tree_invalid = self.parse('<a><c></c></a>') 15 schema = self.parse('''\ 16 <element name="a" xmlns="http://relaxng.org/ns/structure/1.0"> 17 <zeroOrMore> 18 <element name="b"> 19 <text /> 20 </element> 21 </zeroOrMore> 22 </element> 23 ''') 24 schema = etree.RelaxNG(schema) 25 self.assert_(schema.validate(tree_valid)) 26 self.assert_(not schema.validate(tree_invalid))
27
28 - def test_relaxng_stringio(self):
29 tree_valid = self.parse('<a><b></b></a>') 30 tree_invalid = self.parse('<a><c></c></a>') 31 schema_file = StringIO('''\ 32 <element name="a" xmlns="http://relaxng.org/ns/structure/1.0"> 33 <zeroOrMore> 34 <element name="b"> 35 <text /> 36 </element> 37 </zeroOrMore> 38 </element> 39 ''') 40 schema = etree.RelaxNG(file=schema_file) 41 self.assert_(schema.validate(tree_valid)) 42 self.assert_(not schema.validate(tree_invalid))
43
45 self.assertRaises(ValueError, etree.RelaxNG, etree.ElementTree())
46
47 - def test_relaxng_error(self):
48 tree_invalid = self.parse('<a><c></c></a>') 49 schema = self.parse('''\ 50 <element name="a" xmlns="http://relaxng.org/ns/structure/1.0"> 51 <zeroOrMore> 52 <element name="b"> 53 <text /> 54 </element> 55 </zeroOrMore> 56 </element> 57 ''') 58 schema = etree.RelaxNG(schema) 59 self.assert_(not schema.validate(tree_invalid)) 60 errors = schema.error_log 61 self.assert_([ log for log in errors 62 if log.level_name == "ERROR" ]) 63 self.assert_([ log for log in errors 64 if "not expect" in log.message ])
65
67 schema = self.parse('''\ 68 <element name="a" xmlns="http://relaxng.org/ns/structure/1.0"> 69 <zeroOrMore> 70 <element name="b" /> 71 </zeroOrMore> 72 </element> 73 ''') 74 self.assertRaises(etree.RelaxNGParseError, 75 etree.RelaxNG, schema)
76
78 schema = self.parse('''\ 79 <grammar xmlns="http://relaxng.org/ns/structure/1.0" /> 80 ''') 81 self.assertRaises(etree.RelaxNGParseError, 82 etree.RelaxNG, schema)
83
85 schema = self.parse('''\ 86 <grammar xmlns="http://relaxng.org/ns/structure/1.0"> 87 <define name="test"> 88 <element name="test"/> 89 </define> 90 </grammar> 91 ''') 92 self.assertRaises(etree.RelaxNGParseError, 93 etree.RelaxNG, schema)
94
96 # segfault 97 schema = self.parse('''\ 98 <element name="a" xmlns="mynamespace" /> 99 ''') 100 self.assertRaises(etree.RelaxNGParseError, 101 etree.RelaxNG, schema)
102
103 - def test_relaxng_include(self):
104 # this will only work if we access the file through path or 105 # file object.. 106 f = open(fileInTestDir('test1.rng'), 'r') 107 schema = etree.RelaxNG(file=f)
108
109 - def test_relaxng_shortcut(self):
110 tree_valid = self.parse('<a><b></b></a>') 111 tree_invalid = self.parse('<a><c></c></a>') 112 schema = self.parse('''\ 113 <element name="a" xmlns="http://relaxng.org/ns/structure/1.0"> 114 <zeroOrMore> 115 <element name="b"> 116 <text /> 117 </element> 118 </zeroOrMore> 119 </element> 120 ''') 121 self.assert_(tree_valid.relaxng(schema)) 122 self.assert_(not tree_invalid.relaxng(schema))
123
125 tree = self.parse('<a><b>B</b><c>C</c></a>') 126 schema = etree.RelaxNG( self.parse('''\ 127 <element name="a" xmlns="http://relaxng.org/ns/structure/1.0"> 128 <element name="b"> 129 <text /> 130 </element> 131 <element name="c"> 132 <text /> 133 </element> 134 </element> 135 ''') ) 136 self.assert_(schema.validate(tree)) 137 self.assert_(schema.validate(tree)) 138 139 schema = etree.RelaxNG( self.parse('''\ 140 <element name="b" xmlns="http://relaxng.org/ns/structure/1.0"> 141 <text /> 142 </element> 143 ''') ) 144 c_tree = etree.ElementTree(tree.getroot()[1]) 145 self.assertEqual(self._rootstring(c_tree), '<c>C</c>') 146 self.assert_(not schema.validate(c_tree)) 147 148 b_tree = etree.ElementTree(tree.getroot()[0]) 149 self.assertEqual(self._rootstring(b_tree), '<b>B</b>') 150 self.assert_(schema.validate(b_tree))
151 152
153 -def test_suite():
154 suite = unittest.TestSuite() 155 suite.addTests([unittest.makeSuite(ETreeRelaxNGTestCase)]) 156 suite.addTests( 157 [doctest.DocFileSuite('../../../doc/validation.txt')]) 158 return suite
159 160 if __name__ == '__main__': 161 print 'to test use test.py %s' % __file__ 162