示例#1
0
    def test_annotation(self):
        schema = XMLSchema10(
            """<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
                     <xs:element name="root">
                         <xs:annotation/>
                     </xs:element>
                 </xs:schema>""")
        self.assertTrue(schema.elements['root'].annotation.built)

        schema = XMLSchema10(
            """<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
                     <xs:element name="root">
                         <xs:annotation>
                             <xs:appinfo/>
                             <xs:documentation/>
                         </xs:annotation>
                     </xs:element>
                 </xs:schema>""")
        self.assertEqual(len(schema.all_errors), 0)

        schema = XMLSchema10(
            """<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
                     <xs:element name="root">
                         <xs:annotation>
                             <xs:documentation wrong="abc" source=""/>
                             <xs:appinfo wrong="10" source=""/>
                             <xs:element name="wrong"/>
                         </xs:annotation>
                     </xs:element>
                 </xs:schema>""",
            validation='lax')
        self.assertEqual(len(schema.all_errors), 3)
示例#2
0
    def test_invalid_schema(self):
        xsd_text = """<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
            <xs:element name="root" minOccurs="0"/>
        </xs:schema>"""

        with self.assertRaises(XMLSchemaParseError):
            XMLSchema10(xsd_text)

        schema = XMLSchema10(xsd_text, validation='lax')
        self.assertEqual(schema.validity, 'invalid')
        self.assertEqual(len(schema.all_errors), 2)  # One by meta-schema check

        schema = XMLSchema10(xsd_text, validation='skip')
        self.assertEqual(schema.validity, 'notKnown')
        self.assertEqual(len(schema.all_errors), 0)
示例#3
0
    def test_parse_error(self):
        xsd_file = os.path.join(CASES_DIR, 'examples/vehicles/vehicles.xsd')
        schema = XMLSchema10(xsd_file)

        with self.assertRaises(TypeError):
            schema.parse_error("test error", elem=(1, 2))
        with self.assertRaises(TypeError):
            schema.parse_error(b"test error")

        with self.assertRaises(XMLSchemaParseError):
            schema.parse_error("test error")

        self.assertEqual(len(schema.errors), 0)
        schema.parse_error("test error", validation='skip')
        self.assertEqual(len(schema.errors), 0)
        schema.parse_error("test error", validation='lax')
        self.assertEqual(len(schema.errors), 1)
        schema.parse_error(XMLSchemaParseError(schema, "test error"),
                           validation='lax')
        self.assertEqual(len(schema.errors), 2)
        schema.parse_error(ValueError("wrong value"), validation='lax')
        self.assertEqual(len(schema.errors), 3)
        schema.parse_error(ValueError("'invalid value'"), validation='lax')
        self.assertEqual(len(schema.errors), 4)
        self.assertEqual(schema.errors[-1].message, "invalid value")
示例#4
0
    def test_validation_attempted(self):
        schema = XMLSchema10("""<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
                     <xs:notation name="content" public="text/html"/>
                     <xs:element name="root"/>
                 </xs:schema>""")

        self.assertEqual(schema.notations['content'].validation_attempted, 'full')
示例#5
0
    def test_schema_set(self):
        other_schema = XMLSchema10("""<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
            <xs:element name="root"/>
        </xs:schema>""")

        with self.assertRaises(ValueError):
            other_schema.elements['root'].schema = self.schema
示例#6
0
    def test_valid_schema(self):
        xsd_file = os.path.join(CASES_DIR, 'examples/vehicles/vehicles.xsd')

        schema = XMLSchema10(xsd_file, build=False)
        self.assertEqual(schema.validity, 'notKnown')
        self.assertEqual(len(schema.all_errors), 0)

        schema.build()
        self.assertEqual(schema.validity, 'valid')
        self.assertEqual(len(schema.all_errors), 0)
示例#7
0
    def setUpClass(cls):
        cls.schema = XMLSchema10(
            """<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

                     <xs:simpleType name="emptyType">
                         <xs:restriction base="xs:string">
                             <xs:length value="0"/>
                         </xs:restriction>
                     </xs:simpleType>

                     <xs:complexType name="emptyType2">
                         <xs:attribute name="foo" type="xs:string"/>
                     </xs:complexType>        

                     <xs:simpleType name="idType">
                         <xs:restriction base="xs:ID"/>
                     </xs:simpleType>
                     
                     <xs:simpleType name="fooType">
                         <xs:restriction base="xs:string"/>
                     </xs:simpleType>

                     <xs:simpleType name="fooListType">
                         <xs:list itemType="xs:string"/>
                     </xs:simpleType>
                     
                     <xs:complexType name="barType">
                         <xs:sequence>
                             <xs:element name="node"/>
                         </xs:sequence>
                     </xs:complexType>        

                     <xs:complexType name="barExtType">
                         <xs:complexContent>
                             <xs:extension base="barType">
                                 <xs:sequence>
                                     <xs:element name="node"/>
                                 </xs:sequence>
                             </xs:extension>
                         </xs:complexContent>
                     </xs:complexType>        

                     <xs:complexType name="mixedType" mixed="true">
                         <xs:sequence>
                             <xs:element name="node" type="xs:string"/>
                         </xs:sequence>
                     </xs:complexType>        

                 </xs:schema>""")
示例#8
0
    def test_parse_particle(self):
        schema = XMLSchema10(
            """<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
                     <xs:element name="root"/>
                 </xs:schema>""")
        xsd_element = schema.elements['root']

        elem = ElementTree.Element('root', minOccurs='1', maxOccurs='1')
        xsd_element._parse_particle(elem)

        elem = ElementTree.Element('root', minOccurs='2', maxOccurs='1')
        with self.assertRaises(XMLSchemaParseError) as ctx:
            xsd_element._parse_particle(elem)
        self.assertIn(
            "maxOccurs must be 'unbounded' or greater than minOccurs",
            str(ctx.exception))

        elem = ElementTree.Element('root', minOccurs='-1', maxOccurs='1')
        with self.assertRaises(XMLSchemaParseError) as ctx:
            xsd_element._parse_particle(elem)
        self.assertIn("minOccurs value must be a non negative integer",
                      str(ctx.exception))

        elem = ElementTree.Element('root', minOccurs='1', maxOccurs='-1')
        with self.assertRaises(XMLSchemaParseError) as ctx:
            xsd_element._parse_particle(elem)
        self.assertIn(
            "maxOccurs must be 'unbounded' or greater than minOccurs",
            str(ctx.exception))

        elem = ElementTree.Element('root', minOccurs='1', maxOccurs='none')
        with self.assertRaises(XMLSchemaParseError) as ctx:
            xsd_element._parse_particle(elem)
        self.assertIn(
            "maxOccurs value must be a non negative integer or 'unbounded'",
            str(ctx.exception))

        elem = ElementTree.Element('root', minOccurs='2')
        with self.assertRaises(XMLSchemaParseError) as ctx:
            xsd_element._parse_particle(elem)
        self.assertIn("minOccurs must be lesser or equal than maxOccurs",
                      str(ctx.exception))

        elem = ElementTree.Element('root', minOccurs='none')
        with self.assertRaises(XMLSchemaParseError) as ctx:
            xsd_element._parse_particle(elem)
        self.assertIn("minOccurs value is not an integer value",
                      str(ctx.exception))
示例#9
0
    def test_representation(self):
        schema = XMLSchema10("""<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
            <xs:element name="node">
                <xs:complexType>
                    <xs:simpleContent>
                        <xs:extension base="xs:string">
                            <xs:attribute ref="slot"/>
                        </xs:extension> 
                    </xs:simpleContent>
                </xs:complexType>
            </xs:element>
            <xs:attribute name="slot" type="xs:string"/>
        </xs:schema>""")

        self.assertEqual(repr(schema.elements['node']), "XsdElement(name='node', occurs=[1, 1])")
        self.assertEqual(repr(schema.attributes['slot']), "XsdAttribute(name='slot')")
        self.assertEqual(repr(schema.elements['node'].type.attributes['slot']),
                         "XsdAttribute(ref='slot')")
示例#10
0
    def test_has_occurs_restriction(self):
        schema = XMLSchema10(
            """<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
                     <xs:complexType name="barType">
                         <xs:sequence>
                             <xs:element name="node0" />
                             <xs:element name="node1" minOccurs="0"/>
                             <xs:element name="node2" minOccurs="0" maxOccurs="unbounded"/>
                             <xs:element name="node3" minOccurs="2" maxOccurs="unbounded"/>
                             <xs:element name="node4" minOccurs="2" maxOccurs="10"/>
                             <xs:element name="node5" minOccurs="4" maxOccurs="10"/>
                             <xs:element name="node6" minOccurs="4" maxOccurs="9"/>
                             <xs:element name="node7" minOccurs="1" maxOccurs="9"/>
                             <xs:element name="node8" minOccurs="3" maxOccurs="11"/>
                             <xs:element name="node9" minOccurs="0" maxOccurs="0"/>
                         </xs:sequence>
                     </xs:complexType>                             
                 </xs:schema>""")

        xsd_group = schema.types['barType'].content

        for k in range(9):
            self.assertTrue(xsd_group[k].has_occurs_restriction(xsd_group[k]),
                            msg="Fail for node%d" % k)

        self.assertTrue(xsd_group[0].has_occurs_restriction(xsd_group[1]))
        self.assertFalse(xsd_group[1].has_occurs_restriction(xsd_group[0]))
        self.assertTrue(xsd_group[3].has_occurs_restriction(xsd_group[2]))
        self.assertFalse(xsd_group[2].has_occurs_restriction(xsd_group[1]))
        self.assertFalse(xsd_group[2].has_occurs_restriction(xsd_group[3]))
        self.assertTrue(xsd_group[4].has_occurs_restriction(xsd_group[3]))
        self.assertTrue(xsd_group[4].has_occurs_restriction(xsd_group[2]))
        self.assertFalse(xsd_group[4].has_occurs_restriction(xsd_group[5]))
        self.assertTrue(xsd_group[5].has_occurs_restriction(xsd_group[4]))
        self.assertTrue(xsd_group[6].has_occurs_restriction(xsd_group[5]))
        self.assertFalse(xsd_group[5].has_occurs_restriction(xsd_group[6]))
        self.assertFalse(xsd_group[7].has_occurs_restriction(xsd_group[6]))
        self.assertFalse(xsd_group[5].has_occurs_restriction(xsd_group[7]))
        self.assertTrue(xsd_group[6].has_occurs_restriction(xsd_group[7]))
        self.assertFalse(xsd_group[7].has_occurs_restriction(xsd_group[8]))
        self.assertFalse(xsd_group[8].has_occurs_restriction(xsd_group[7]))
        self.assertTrue(xsd_group[9].has_occurs_restriction(xsd_group[1]))
        self.assertTrue(xsd_group[9].has_occurs_restriction(xsd_group[2]))
示例#11
0
    def test_get_matching_item(self):
        xsd_element = self.schema.elements['vehicles']

        self.assertIsNone(xsd_element.get_matching_item({}))
        self.assertTrue(
            xsd_element.get_matching_item({xsd_element.qualified_name: True}))
        self.assertTrue(
            xsd_element.get_matching_item({xsd_element.prefixed_name: True}))

        mapping = {xsd_element.local_name: True}
        self.assertIsNone(xsd_element.get_matching_item(mapping))
        self.assertTrue(
            xsd_element.get_matching_item(mapping, match_local_name=True))

        mapping = {xsd_element.type.local_name: True}  # type.name is None
        self.assertIsNone(
            xsd_element.type.get_matching_item(mapping, match_local_name=True))

        mapping = {'vhs:vehicles': True}
        self.assertIsNone(xsd_element.get_matching_item(mapping))

        self.schema.namespaces['vhs'] = self.schema.target_namespace
        try:
            self.assertTrue(xsd_element.get_matching_item(mapping))
        finally:
            self.schema.namespaces.pop('vhs')

        mapping = {'vhs:vehicles': {'xmlns:vhs': self.schema.target_namespace}}
        self.assertIs(xsd_element.get_matching_item(mapping),
                      mapping['vhs:vehicles'])

        mapping = {'vhs:vehicles': {'xmlns:vhs': 'http://xmlschema.test/ns'}}
        self.assertIsNone(xsd_element.get_matching_item(mapping))

        schema = XMLSchema10(
            dedent("""\
            <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
              <xs:element name="root"/>
            </xs:schema>"""))
        xsd_element = schema.elements['root']

        self.assertTrue(xsd_element.get_matching_item({'root': True}))
        self.assertIsNone(xsd_element.get_matching_item({'rook': True}))
示例#12
0
 def setUpClass(cls):
     xsd_file = os.path.join(CASES_DIR, 'examples/vehicles/vehicles.xsd')
     cls.schema = XMLSchema10(xsd_file)
示例#13
0
    def setUpClass(cls):
        cls.schema = XMLSchema10(
            """<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

                     <xs:simpleType name="emptyType">
                         <xs:restriction base="xs:string">
                             <xs:length value="0"/>
                         </xs:restriction>
                     </xs:simpleType>

                     <xs:complexType name="emptyType2">
                         <xs:attribute name="foo" type="xs:string"/>
                     </xs:complexType>        

                     <xs:simpleType name="idType">
                         <xs:restriction base="xs:ID"/>
                     </xs:simpleType>

                     <xs:simpleType name="decimalType">
                         <xs:restriction base="xs:decimal"/>
                     </xs:simpleType>

                     <xs:simpleType name="dateTimeType">
                         <xs:restriction base="xs:dateTime"/>
                     </xs:simpleType>
                     
                     <xs:simpleType name="fooType">
                         <xs:restriction base="xs:string"/>
                     </xs:simpleType>

                     <xs:simpleType name="fooListType">
                         <xs:list itemType="xs:string"/>
                     </xs:simpleType>

                     <xs:simpleType name="fooUnionType">
                         <xs:union memberTypes="xs:string xs:anyURI"/>
                     </xs:simpleType>
                     
                     <xs:complexType name="barType">
                         <xs:sequence>
                             <xs:element name="node"/>
                         </xs:sequence>
                     </xs:complexType>        

                     <xs:complexType name="barExtType">
                         <xs:complexContent>
                             <xs:extension base="barType">
                                 <xs:sequence>
                                     <xs:element name="node"/>
                                 </xs:sequence>
                             </xs:extension>
                         </xs:complexContent>
                     </xs:complexType>        

                     <xs:complexType name="barResType">
                         <xs:complexContent>
                             <xs:restriction base="barType">
                                 <xs:sequence>
                                     <xs:element name="node"/>
                                 </xs:sequence>
                             </xs:restriction>
                         </xs:complexContent>
                     </xs:complexType>        

                     <xs:complexType name="mixedType" mixed="true">
                         <xs:sequence>
                             <xs:element name="node" type="xs:string"/>
                         </xs:sequence>
                     </xs:complexType>        

                     <xs:element name="fooElem" type="fooType"/>
                     <xs:element name="barElem" type="barType" block="extension"/>
                     
                 </xs:schema>""")