Package lxml :: Package tests :: Module test_xmlschema
[hide private]
[frames] | no frames]

Source Code for Module lxml.tests.test_xmlschema

 1  # -*- coding: utf-8 -*- 
 2   
 3  """ 
 4  Test cases related to XML Schema parsing and validation 
 5  """ 
 6   
 7  import unittest 
 8   
 9  from common_imports import etree, doctest, HelperTestCase, fileInTestDir 
10   
11 -class ETreeXMLSchemaTestCase(HelperTestCase):
12 - def test_xmlschema(self):
13 tree_valid = self.parse('<a><b></b></a>') 14 tree_invalid = self.parse('<a><c></c></a>') 15 schema = self.parse(''' 16 <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 17 <xsd:element name="a" type="AType"/> 18 <xsd:complexType name="AType"> 19 <xsd:sequence> 20 <xsd:element name="b" type="xsd:string" /> 21 </xsd:sequence> 22 </xsd:complexType> 23 </xsd:schema> 24 ''') 25 schema = etree.XMLSchema(schema) 26 self.assert_(schema.validate(tree_valid)) 27 self.assert_(not schema.validate(tree_invalid))
28
30 self.assertRaises(ValueError, etree.XMLSchema, etree.ElementTree())
31
33 schema = self.parse('''\ 34 <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 35 <element name="a" type="AType"/> 36 <xsd:complexType name="AType"> 37 <xsd:sequence> 38 <xsd:element name="b" type="xsd:string" /> 39 </xsd:sequence> 40 </xsd:complexType> 41 </xsd:schema> 42 ''') 43 self.assertRaises(etree.XMLSchemaParseError, 44 etree.XMLSchema, schema)
45
47 schema = self.parse('<test/>') 48 self.assertRaises(etree.XMLSchemaParseError, 49 etree.XMLSchema, schema)
50
51 - def test_xmlschema_file(self):
52 # this will only work if we access the file through path or 53 # file object.. 54 f = open(fileInTestDir('test.xsd'), 'r') 55 schema = etree.XMLSchema(file=f) 56 tree_valid = self.parse('<a><b></b></a>') 57 self.assert_(schema.validate(tree_valid))
58
59 - def test_xmlschema_shortcut(self):
60 tree_valid = self.parse('<a><b></b></a>') 61 tree_invalid = self.parse('<a><c></c></a>') 62 schema = self.parse('''\ 63 <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 64 <xsd:element name="a" type="AType"/> 65 <xsd:complexType name="AType"> 66 <xsd:sequence> 67 <xsd:element name="b" type="xsd:string" /> 68 </xsd:sequence> 69 </xsd:complexType> 70 </xsd:schema> 71 ''') 72 self.assert_(tree_valid.xmlschema(schema)) 73 self.assert_(not tree_invalid.xmlschema(schema))
74 75
76 -def test_suite():
77 suite = unittest.TestSuite() 78 suite.addTests([unittest.makeSuite(ETreeXMLSchemaTestCase)]) 79 suite.addTests( 80 [doctest.DocFileSuite('../../../doc/validation.txt')]) 81 return suite
82 83 if __name__ == '__main__': 84 unittest.main() 85