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
     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_elementtree_error(self):
     50          self.assertRaises(ValueError, etree.XMLSchema, etree.ElementTree())
     51
     52 -    def test_xmlschema_invalid_schema1(self):
     53          schema = self.parse('''\
     54  <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
     55    <element name="a" type="AType"/>
     56    <xsd:complexType name="AType">
     57      <xsd:sequence>
     58        <xsd:element name="b" type="xsd:string" />
     59      </xsd:sequence>
     60    </xsd:complexType>
     61  </xsd:schema>
     62  ''')
     63          self.assertRaises(etree.XMLSchemaParseError,
     64                            etree.XMLSchema, schema)
     65
     66 -    def test_xmlschema_invalid_schema2(self):
     67          schema = self.parse('<test/>')
     68          self.assertRaises(etree.XMLSchemaParseError,
     69                            etree.XMLSchema, schema)
     70
     71 -    def test_xmlschema_file(self):
     72          # this will only work if we access the file through path or
     73          # file object..
     74          f = open(fileInTestDir('test.xsd'), 'r')
     75          schema = etree.XMLSchema(file=f)
     76          tree_valid = self.parse('<a><b></b></a>')
     77          self.assert_(schema.validate(tree_valid))
     78
     79 -    def test_xmlschema_shortcut(self):
     80          tree_valid = self.parse('<a><b></b></a>')
     81          tree_invalid = self.parse('<a><c></c></a>')
     82          schema = self.parse('''\
     83  <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
     84    <xsd:element name="a" type="AType"/>
     85    <xsd:complexType name="AType">
     86      <xsd:sequence>
     87        <xsd:element name="b" type="xsd:string" />
     88      </xsd:sequence>
     89    </xsd:complexType>
     90  </xsd:schema>
     91  ''')
     92          self.assert_(tree_valid.xmlschema(schema))
     93          self.assert_(not tree_invalid.xmlschema(schema))
     94
     95
     96 -def test_suite():
     97      suite = unittest.TestSuite()
     98      suite.addTests([unittest.makeSuite(ETreeXMLSchemaTestCase)])
     99      suite.addTests(
    100          [doctest.DocFileSuite('../../../doc/validation.txt')])
    101      return suite
    102
    103  if __name__ == '__main__':
    104      print 'to test use test.py %s' % __file__
    105