Package lxml :: Package tests :: Module test_xmlschema |
[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, StringIO, 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
     29 -    def test_xmlschema_parse(self):
     30          schema = self.parse('''
     31  <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
     32    <xsd:element name="a" type="AType"/>
     33    <xsd:complexType name="AType">
     34      <xsd:sequence>
     35        <xsd:element name="b" type="xsd:string" />
     36      </xsd:sequence>
     37    </xsd:complexType>
     38  </xsd:schema>
     39  ''')
     40          schema = etree.XMLSchema(schema)
     41          parser = etree.XMLParser(schema=schema)
     42
     43          tree_valid = self.parse('<a><b></b></a>', parser=parser)
     44          self.assertEquals('a', tree_valid.getroot().tag)
     45
     46          self.assertRaises(etree.XMLSyntaxError,
     47                            self.parse, '<a><c></c></a>', parser=parser)
     48
     49 -    def test_xmlschema_stringio(self):
     50          schema_file = StringIO('''
     51  <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
     52    <xsd:element name="a" type="AType"/>
     53    <xsd:complexType name="AType">
     54      <xsd:sequence>
     55        <xsd:element name="b" type="xsd:string" />
     56      </xsd:sequence>
     57    </xsd:complexType>
     58  </xsd:schema>
     59  ''')
     60          schema = etree.XMLSchema(file=schema_file)
     61          parser = etree.XMLParser(schema=schema)
     62
     63          tree_valid = self.parse('<a><b></b></a>', parser=parser)
     64          self.assertEquals('a', tree_valid.getroot().tag)
     65
     66          self.assertRaises(etree.XMLSyntaxError,
     67                            self.parse, '<a><c></c></a>', parser=parser)
     68
     69 -    def test_xmlschema_iterparse(self):
     70          schema = self.parse('''
     71  <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
     72    <xsd:element name="a" type="AType"/>
     73    <xsd:complexType name="AType">
     74      <xsd:sequence>
     75        <xsd:element name="b" type="xsd:string" />
     76      </xsd:sequence>
     77    </xsd:complexType>
     78  </xsd:schema>
     79  ''')
     80          schema = etree.XMLSchema(schema)
     81          xml = StringIO('<a><b></b></a>')
     82          events = [ (event, el.tag)
     83                     for (event, el) in etree.iterparse(xml, schema=schema) ]
     84
     85          self.assertEquals([('end', 'b'), ('end', 'a')],
     86                            events)
     87
     88 -    def test_xmlschema_iterparse_fail(self):
     89          schema = self.parse('''
     90  <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
     91    <xsd:element name="a" type="AType"/>
     92    <xsd:complexType name="AType">
     93      <xsd:sequence>
     94        <xsd:element name="b" type="xsd:string" />
     95      </xsd:sequence>
     96    </xsd:complexType>
     97  </xsd:schema>
     98  ''')
     99          schema = etree.XMLSchema(schema)
    100          self.assertRaises(
    101              etree.XMLSyntaxError,
    102              list, etree.iterparse(StringIO('<a><c></c></a>'), schema=schema))
    103
    104 -    def test_xmlschema_elementtree_error(self):
    105          self.assertRaises(ValueError, etree.XMLSchema, etree.ElementTree())
    106
    107 -    def test_xmlschema_invalid_schema1(self):
    108          schema = self.parse('''\
    109  <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    110    <element name="a" type="AType"/>
    111    <xsd:complexType name="AType">
    112      <xsd:sequence>
    113        <xsd:element name="b" type="xsd:string" />
    114      </xsd:sequence>
    115    </xsd:complexType>
    116  </xsd:schema>
    117  ''')
    118          self.assertRaises(etree.XMLSchemaParseError,
    119                            etree.XMLSchema, schema)
    120
    121 -    def test_xmlschema_invalid_schema2(self):
    122          schema = self.parse('<test/>')
    123          self.assertRaises(etree.XMLSchemaParseError,
    124                            etree.XMLSchema, schema)
    125
    126 -    def test_xmlschema_file(self):
    127          # this will only work if we access the file through path or
    128          # file object..
    129          f = open(fileInTestDir('test.xsd'), 'r')
    130          schema = etree.XMLSchema(file=f)
    131          tree_valid = self.parse('<a><b></b></a>')
    132          self.assert_(schema.validate(tree_valid))
    133
    134 -    def test_xmlschema_shortcut(self):
    135          tree_valid = self.parse('<a><b></b></a>')
    136          tree_invalid = self.parse('<a><c></c></a>')
    137          schema = self.parse('''\
    138  <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    139    <xsd:element name="a" type="AType"/>
    140    <xsd:complexType name="AType">
    141      <xsd:sequence>
    142        <xsd:element name="b" type="xsd:string" />
    143      </xsd:sequence>
    144    </xsd:complexType>
    145  </xsd:schema>
    146  ''')
    147          self.assert_(tree_valid.xmlschema(schema))
    148          self.assert_(not tree_invalid.xmlschema(schema))
    149
    150
    151 -def test_suite():
    152      suite = unittest.TestSuite()
    153      suite.addTests([unittest.makeSuite(ETreeXMLSchemaTestCase)])
    154      suite.addTests(
    155          [doctest.DocFileSuite('../../../doc/validation.txt')])
    156      return suite
    157
    158  if __name__ == '__main__':
    159      print 'to test use test.py %s' % __file__
    160