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, 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_elementtree_error(self):
     70          self.assertRaises(ValueError, etree.XMLSchema, etree.ElementTree())
     71
     72 -    def test_xmlschema_invalid_schema1(self):
     73          schema = self.parse('''\
     74  <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
     75    <element name="a" type="AType"/>
     76    <xsd:complexType name="AType">
     77      <xsd:sequence>
     78        <xsd:element name="b" type="xsd:string" />
     79      </xsd:sequence>
     80    </xsd:complexType>
     81  </xsd:schema>
     82  ''')
     83          self.assertRaises(etree.XMLSchemaParseError,
     84                            etree.XMLSchema, schema)
     85
     86 -    def test_xmlschema_invalid_schema2(self):
     87          schema = self.parse('<test/>')
     88          self.assertRaises(etree.XMLSchemaParseError,
     89                            etree.XMLSchema, schema)
     90
     91 -    def test_xmlschema_file(self):
     92          # this will only work if we access the file through path or
     93          # file object..
     94          f = open(fileInTestDir('test.xsd'), 'r')
     95          schema = etree.XMLSchema(file=f)
     96          tree_valid = self.parse('<a><b></b></a>')
     97          self.assert_(schema.validate(tree_valid))
     98
     99 -    def test_xmlschema_shortcut(self):
    100          tree_valid = self.parse('<a><b></b></a>')
    101          tree_invalid = self.parse('<a><c></c></a>')
    102          schema = self.parse('''\
    103  <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    104    <xsd:element name="a" type="AType"/>
    105    <xsd:complexType name="AType">
    106      <xsd:sequence>
    107        <xsd:element name="b" type="xsd:string" />
    108      </xsd:sequence>
    109    </xsd:complexType>
    110  </xsd:schema>
    111  ''')
    112          self.assert_(tree_valid.xmlschema(schema))
    113          self.assert_(not tree_invalid.xmlschema(schema))
    114
    115
    116 -def test_suite():
    117      suite = unittest.TestSuite()
    118      suite.addTests([unittest.makeSuite(ETreeXMLSchemaTestCase)])
    119      suite.addTests(
    120          [doctest.DocFileSuite('../../../doc/validation.txt')])
    121      return suite
    122
    123  if __name__ == '__main__':
    124      print 'to test use test.py %s' % __file__
    125