示例#1
0
 def test_bind_parser_method(self):
     schema_proxy1 = XMLSchemaProxy(self.xs1)
     schema_proxy2 = XMLSchemaProxy(self.xs2)
     parser = XPath2Parser(strict=False, schema=schema_proxy1)
     self.assertIs(parser.schema, schema_proxy1)
     schema_proxy1.bind_parser(parser)
     self.assertIs(parser.schema, schema_proxy1)
     schema_proxy2.bind_parser(parser)
     self.assertIs(parser.schema, schema_proxy2)
示例#2
0
    def test_initialization(self):
        schema_proxy = XMLSchemaProxy()
        self.assertIs(schema_proxy._schema, self.schema_class.meta_schema)

        schema_proxy = XMLSchemaProxy(self.xs1, base_element=self.xs1.elements['vehicles'])
        self.assertIs(schema_proxy._schema, self.xs1)

        with self.assertRaises(ValueError):
            XMLSchemaProxy(self.xs1, base_element=self.xs2.elements['collection'])

        with self.assertRaises(TypeError):
            XMLSchemaProxy(self.xs1, base_element=ElementTree.Element('vehicles'))
    def test_attributes_type(self):
        parser = XPath2Parser(namespaces=self.namespaces)
        token = parser.parse("@min le @max")
        self.assertTrue(
            token.evaluate(context=XPathContext(
                self.etree.XML('<root min="10" max="20" />'))))
        self.assertTrue(
            token.evaluate(context=XPathContext(
                self.etree.XML('<root min="10" max="2" />'))))

        schema = xmlschema.XMLSchema('''
            <xs:schema xmlns="http://xpath.test/ns" xmlns:xs="http://www.w3.org/2001/XMLSchema"
                targetNamespace="http://xpath.test/ns">
              <xs:element name="range" type="intRange"/>
              <xs:complexType name="intRange">
                <xs:attribute name="min" type="xs:int"/>
                <xs:attribute name="max" type="xs:int"/>
              </xs:complexType>
            </xs:schema>''')
        parser = XPath2Parser(namespaces=self.namespaces,
                              schema=XMLSchemaProxy(schema,
                                                    schema.elements['range']))
        token = parser.parse("@min le @max")
        self.assertTrue(
            token.evaluate(context=XPathContext(
                self.etree.XML('<root min="10" max="20" />'))))
        self.assertFalse(
            token.evaluate(context=XPathContext(
                self.etree.XML('<root min="10" max="2" />'))))

        schema = xmlschema.XMLSchema('''
            <xs:schema xmlns="http://xpath.test/ns" xmlns:xs="http://www.w3.org/2001/XMLSchema"
                targetNamespace="http://xpath.test/ns">
              <xs:element name="range" type="intRange"/>
              <xs:complexType name="intRange">
                <xs:attribute name="min" type="xs:int"/>
                <xs:attribute name="max" type="xs:string"/>
              </xs:complexType>
            </xs:schema>''')
        parser = XPath2Parser(namespaces=self.namespaces,
                              schema=XMLSchemaProxy(schema,
                                                    schema.elements['range']))
        if PY3:
            self.assertRaises(TypeError, parser.parse, '@min le @max')
        else:
            # In Python 2 strings and numbers are comparable and strings are 'greater than' numbers.
            token = parser.parse("@min le @max")
            self.assertTrue(
                token.evaluate(context=XPathContext(
                    self.etree.XML('<root min="10" max="20" />'))))
            self.assertTrue(
                token.evaluate(context=XPathContext(
                    self.etree.XML('<root min="10" max="2" />'))))
示例#4
0
 def test_iter_atomic_types_method(self):
     schema_proxy = XMLSchemaProxy(self.xs3)
     k = 0
     for k, xsd_type in enumerate(schema_proxy.iter_atomic_types(), start=1):
         self.assertNotIn(XSD_NAMESPACE, xsd_type.name)
         self.assertIsInstance(xsd_type, (XsdAtomic, XsdAtomicRestriction))
     self.assertGreater(k, 10)
示例#5
0
 def test_get_element_method(self):
     schema_proxy = XMLSchemaProxy(self.xs1)
     qname = '{%s}cars' % self.xs1.target_namespace
     self.assertIs(schema_proxy.get_element(qname),
                   self.xs1.elements['cars'])
     qname = '{%s}unknown' % self.xs1.target_namespace
     self.assertIsNone(schema_proxy.get_element(qname))
示例#6
0
 def test_get_attribute_method(self):
     schema_proxy = XMLSchemaProxy(self.xs1)
     qname = '{%s}step' % self.xs1.target_namespace
     self.assertIs(schema_proxy.get_attribute(qname),
                   self.xs1.attributes['step'])
     qname = '{%s}unknown' % self.xs1.target_namespace
     self.assertIsNone(schema_proxy.get_attribute(qname))
示例#7
0
 def test_get_type_method(self):
     schema_proxy = XMLSchemaProxy(self.xs1)
     qname = '{%s}vehicleType' % self.xs1.target_namespace
     self.assertIs(schema_proxy.get_type(qname),
                   self.xs1.types['vehicleType'])
     qname = '{%s}unknown' % self.xs1.target_namespace
     self.assertIsNone(schema_proxy.get_type(qname))
 def test_find_api(self):
     schema_src = """<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
                         <xs:element name="test_element" type="xs:string"/>
                     </xs:schema>"""
     schema = xmlschema.XMLSchema(schema_src)
     schema_proxy = XMLSchemaProxy(schema=schema)
     self.assertEqual(schema_proxy.find('/test_element'),
                      schema.elements['test_element'])
示例#9
0
 def test_get_substitution_group_method(self):
     schema = XMLSchema11.meta_schema
     schema.build()
     schema_proxy = XMLSchemaProxy(schema)
     qname = '{%s}facet' % schema.target_namespace
     self.assertIs(schema_proxy.get_substitution_group(qname),
                   schema.substitution_groups['facet'])
     qname = '{%s}unknown' % schema.target_namespace
     self.assertIsNone(schema_proxy.get_substitution_group(qname))
示例#10
0
    def test_get_primitive_type_method(self):
        schema_proxy = XMLSchemaProxy(self.xs3)

        string_type = self.xs3.meta_schema.types['string']
        xsd_type = self.xs3.types['list_of_strings']
        self.assertIs(schema_proxy.get_primitive_type(xsd_type), string_type)

        xsd_type = self.xs3.types['integer_or_float']
        self.assertIs(schema_proxy.get_primitive_type(xsd_type), xsd_type)
 def test_find_api(self):
     schema_src = """<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
                         <xs:element name="test_element" type="xs:string"/>
                     </xs:schema>"""
     schema = xmlschema.XMLSchema(schema_src)
     schema_proxy = XMLSchemaProxy(schema=schema)
     if xmlschema.__version__ == '1.0.14':
         self.assertIsNone(
             schema_proxy.find('/test_element'))  # Not implemented!
     else:
         self.assertEqual(schema_proxy.find('/test_element'),
                          schema.elements['test_element'])
    def test_schema_constructors(self):
        schema_src = dedent("""
            <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
                <xs:simpleType name="stringType">
                    <xs:restriction base="xs:string"/>
                </xs:simpleType>
                <xs:simpleType name="intType">
                    <xs:restriction base="xs:int"/>
                </xs:simpleType>
            </xs:schema>""")
        schema = xmlschema.XMLSchema(schema_src)
        schema_proxy = XMLSchemaProxy(schema=schema)
        parser = XPath2Parser(namespaces=self.namespaces, schema=schema_proxy)

        with self.assertRaises(NameError) as ctx:
            parser.schema_constructor(XSD_ANY_ATOMIC_TYPE)
        self.assertIn('XPST0080', str(ctx.exception))

        with self.assertRaises(NameError) as ctx:
            parser.schema_constructor(XSD_NOTATION)
        self.assertIn('XPST0080', str(ctx.exception))

        token = parser.parse('stringType("apple")')
        self.assertEqual(token.symbol, 'stringType')
        self.assertEqual(token.label, 'constructor function')
        self.assertEqual(token.evaluate(), 'apple')

        token = parser.parse('stringType(())')
        self.assertEqual(token.symbol, 'stringType')
        self.assertEqual(token.label, 'constructor function')
        self.assertEqual(token.evaluate(), [])

        token = parser.parse('stringType(10)')
        self.assertEqual(token.symbol, 'stringType')
        self.assertEqual(token.label, 'constructor function')
        self.assertEqual(token.evaluate(), '10')

        token = parser.parse('stringType(.)')
        self.assertEqual(token.symbol, 'stringType')
        self.assertEqual(token.label, 'constructor function')

        token = parser.parse('intType(10)')
        self.assertEqual(token.symbol, 'intType')
        self.assertEqual(token.label, 'constructor function')
        self.assertEqual(token.evaluate(), 10)

        with self.assertRaises(ValueError) as ctx:
            parser.parse('intType(true())')
        self.assertIn('FORG0001', str(ctx.exception))
    def test_elements_type(self):
        schema = xmlschema.XMLSchema('''
            <xs:schema xmlns="http://xpath.test/ns" xmlns:xs="http://www.w3.org/2001/XMLSchema"
                    targetNamespace="http://xpath.test/ns">
                <xs:element name="values">
                    <xs:complexType>
                        <xs:sequence>
                            <xs:element name="a" type="xs:string"/>
                            <xs:element name="b" type="xs:integer"/>
                            <xs:element name="c" type="xs:boolean"/>
                            <xs:element name="d" type="xs:float"/>
                        </xs:sequence>
                    </xs:complexType>
                </xs:element>
            </xs:schema>''')
        parser = XPath2Parser(namespaces={
            '': "http://xpath.test/ns",
            'xs': XSD_NAMESPACE
        },
                              schema=XMLSchemaProxy(schema))

        token = parser.parse("//a")
        self.assertEqual(token[0].xsd_types['a'],
                         schema.maps.types['{%s}string' % XSD_NAMESPACE])
        token = parser.parse("//b")
        self.assertEqual(token[0].xsd_types['b'],
                         schema.maps.types['{%s}integer' % XSD_NAMESPACE])
        token = parser.parse("//values/c")

        self.assertEqual(token[0][0].xsd_types["{http://xpath.test/ns}values"],
                         schema.elements['values'].type)
        self.assertEqual(token[1].xsd_types['c'],
                         schema.maps.types['{%s}boolean' % XSD_NAMESPACE])

        token = parser.parse("values/c")
        self.assertEqual(token[0].xsd_types['{http://xpath.test/ns}values'],
                         schema.elements['values'].type)
        self.assertEqual(token[1].xsd_types['c'],
                         schema.maps.types['{%s}boolean' % XSD_NAMESPACE])

        token = parser.parse("values/*")
        self.assertEqual(
            token[1].xsd_types, {
                'a': schema.maps.types['{%s}string' % XSD_NAMESPACE],
                'b': schema.maps.types['{%s}integer' % XSD_NAMESPACE],
                'c': schema.maps.types['{%s}boolean' % XSD_NAMESPACE],
                'd': schema.maps.types['{%s}float' % XSD_NAMESPACE],
            })
    def test_bind_parser_method(self):
        schema_src = """<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
                            <xs:simpleType name="test_type">
                              <xs:restriction base="xs:string"/>
                            </xs:simpleType>
                        </xs:schema>"""
        schema = xmlschema.XMLSchema(schema_src)

        schema_proxy = XMLSchemaProxy(schema=schema)
        parser = XPath2Parser(namespaces=self.namespaces)
        schema_proxy.bind_parser(parser)
        self.assertIs(schema_proxy, parser.schema)
        parser = XPath2Parser(namespaces=self.namespaces)
        super(XMLSchemaProxy, schema_proxy).bind_parser(parser)
        self.assertIs(schema_proxy, parser.schema)
        super(XMLSchemaProxy, schema_proxy).bind_parser(parser)
        self.assertIs(schema_proxy, parser.schema)
    def test_get_primitive_type_api(self):
        schema_proxy = XMLSchemaProxy()
        short_type = schema_proxy.get_type('{%s}short' % XSD_NAMESPACE)
        decimal_type = schema_proxy.get_type('{%s}decimal' % XSD_NAMESPACE)
        self.assertEqual(schema_proxy.get_primitive_type(short_type),
                         decimal_type)

        ntokens_type = schema_proxy.get_type('{%s}NMTOKENS' % XSD_NAMESPACE)
        string_type = schema_proxy.get_type('{%s}string' % XSD_NAMESPACE)
        self.assertEqual(schema_proxy.get_primitive_type(ntokens_type),
                         string_type)

        facet_type = schema_proxy.get_type('{%s}facet' % XSD_NAMESPACE)
        any_type = schema_proxy.get_type('{%s}anyType' % XSD_NAMESPACE)
        self.assertEqual(schema_proxy.get_primitive_type(facet_type), any_type)
        self.assertEqual(schema_proxy.get_primitive_type(any_type), any_type)

        any_simple_type = schema_proxy.get_type('{%s}anySimpleType' %
                                                XSD_NAMESPACE)
        self.assertEqual(schema_proxy.get_primitive_type(any_simple_type),
                         any_simple_type)
    def test_bind_parser_method(self):
        schema_src = dedent("""
            <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
                <xs:simpleType name="stringType">
                    <xs:restriction base="xs:string"/>
                </xs:simpleType>
            </xs:schema>""")
        schema = xmlschema.XMLSchema(schema_src)

        schema_proxy = XMLSchemaProxy(schema=schema)
        parser = XPath2Parser(namespaces=self.namespaces)
        self.assertFalse(parser.is_schema_bound())

        schema_proxy.bind_parser(parser)
        self.assertTrue(parser.is_schema_bound())
        self.assertIs(schema_proxy, parser.schema)

        # To test AbstractSchemaProxy.bind_parser()
        parser = XPath2Parser(namespaces=self.namespaces)
        super(XMLSchemaProxy, schema_proxy).bind_parser(parser)
        self.assertIs(schema_proxy, parser.schema)
        super(XMLSchemaProxy, schema_proxy).bind_parser(parser)
        self.assertIs(schema_proxy, parser.schema)
    def test_schema_proxy_init(self):
        schema_src = """<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
                            <xs:element name="test_element" type="xs:string"/>
                        </xs:schema>"""
        schema_tree = ElementTree.parse(io.StringIO(schema_src))

        self.assertIsInstance(XMLSchemaProxy(), XMLSchemaProxy)
        self.assertIsInstance(XMLSchemaProxy(xmlschema.XMLSchema(schema_src)),
                              XMLSchemaProxy)
        with self.assertRaises(TypeError):
            XMLSchemaProxy(schema=schema_tree)
        with self.assertRaises(TypeError):
            XMLSchemaProxy(schema=xmlschema.XMLSchema(schema_src),
                           base_element=schema_tree)
        with self.assertRaises(TypeError):
            XMLSchemaProxy(schema=xmlschema.XMLSchema(schema_src),
                           base_element=schema_tree.getroot())

        schema = xmlschema.XMLSchema(schema_src)
        with self.assertRaises(ValueError):
            XMLSchemaProxy(base_element=schema.elements['test_element'])
 def setUp(self):
     self.schema_proxy = XMLSchemaProxy(self.schema)
     self.parser = XPath2Parser(namespaces=self.namespaces,
                                schema=self.schema_proxy,
                                variables=self.variables)
    def test_elements_type(self):
        schema = xmlschema.XMLSchema('''
            <xs:schema xmlns="http://xpath.test/ns" xmlns:xs="http://www.w3.org/2001/XMLSchema"
                    targetNamespace="http://xpath.test/ns">
                <xs:element name="values">
                    <xs:complexType>
                        <xs:sequence>
                            <xs:element name="a" type="xs:string"/>
                            <xs:element name="b" type="xs:integer"/>
                            <xs:element name="c" type="xs:boolean"/>
                            <xs:element name="d" type="xs:float"/>
                        </xs:sequence>
                    </xs:complexType>
                </xs:element>
            </xs:schema>''')
        parser = XPath2Parser(namespaces={
            '': "http://xpath.test/ns",
            'xs': XSD_NAMESPACE
        },
                              schema=XMLSchemaProxy(schema))
        token = parser.parse("//a")
        self.assertEqual(token[0].xsd_type,
                         schema.maps.types['{%s}string' % XSD_NAMESPACE])
        token = parser.parse("//b")
        self.assertEqual(token[0].xsd_type,
                         schema.maps.types['{%s}integer' % XSD_NAMESPACE])
        token = parser.parse("//values/c")
        self.assertEqual(token[0][0].xsd_type, schema.elements['values'].type)
        self.assertEqual(token[1].xsd_type,
                         schema.maps.types['{%s}boolean' % XSD_NAMESPACE])
        token = parser.parse("values/c")
        self.assertEqual(token[0].xsd_type, schema.elements['values'].type)
        self.assertEqual(token[1].xsd_type,
                         schema.maps.types['{%s}boolean' % XSD_NAMESPACE])

        schema = xmlschema.XMLSchema('''
            <xs:schema xmlns="http://xpath.test/ns" xmlns:xs="http://www.w3.org/2001/XMLSchema"
                    targetNamespace="http://xpath.test/ns">
                <xs:element name="values">
                    <xs:complexType>
                        <xs:sequence>
                            <xs:element name="a" type="xs:string"/>
                            <xs:element name="b" type="rangeType"/>
                            <xs:element name="c" type="xs:boolean"/>
                            <xs:element name="d" type="xs:float"/>
                        </xs:sequence>
                    </xs:complexType>
                </xs:element>
                <xs:complexType name="rangeType">
                    <xs:simpleContent>
                        <xs:extension base="xs:integer">
                            <xs:attribute name="min" type="xs:integer"/>
                            <xs:attribute name="max" type="xs:integer"/>
                        </xs:extension>
                    </xs:simpleContent>
                </xs:complexType>
            </xs:schema>''')
        parser = XPath2Parser(namespaces={
            '': "http://xpath.test/ns",
            'xs': XSD_NAMESPACE
        },
                              schema=XMLSchemaProxy(schema))
        token = parser.parse("//a")
        self.assertEqual(token[0].xsd_type,
                         schema.maps.types['{%s}string' % XSD_NAMESPACE])
        token = parser.parse("//b")
        self.assertEqual(token[0].xsd_type, schema.types['rangeType'])
        token = parser.parse("values/c")
        self.assertEqual(token[0].xsd_type, schema.elements['values'].type)
        self.assertEqual(token[1].xsd_type,
                         schema.maps.types['{%s}boolean' % XSD_NAMESPACE])
        token = parser.parse("//b/@min")
        self.assertEqual(token[0][0].xsd_type, schema.types['rangeType'])
        self.assertEqual(token[1][0].xsd_type,
                         schema.maps.types['{%s}integer' % XSD_NAMESPACE])
        token = parser.parse("values/b/@min")
        self.assertEqual(token[0][0].xsd_type, schema.elements['values'].type)
        self.assertEqual(token[0][1].xsd_type, schema.types['rangeType'])
        self.assertEqual(token[1][0].xsd_type,
                         schema.maps.types['{%s}integer' % XSD_NAMESPACE])

        token = parser.parse("//b/@min lt //b/@max")
        self.assertEqual(token[0][0][0].xsd_type, schema.types['rangeType'])
        self.assertEqual(token[0][1][0].xsd_type,
                         schema.maps.types['{%s}integer' % XSD_NAMESPACE])
        self.assertEqual(token[1][0][0].xsd_type, schema.types['rangeType'])
        self.assertEqual(token[1][1][0].xsd_type,
                         schema.maps.types['{%s}integer' % XSD_NAMESPACE])

        root = self.etree.XML(
            '<values xmlns="http://xpath.test/ns"><b min="19"/></values>')
        with self.assertRaises(TypeError):
            token.evaluate(context=XPathContext(root))

        root = self.etree.XML(
            '<values xmlns="http://xpath.test/ns"><b min="19">30</b></values>')
        self.assertIsNone(token.evaluate(context=XPathContext(root)))

        root = self.etree.XML(
            '<values xmlns="http://xpath.test/ns"><b min="19" max="40">30</b></values>'
        )
        context = XPathContext(root)
        self.assertTrue(token.evaluate(context))

        root = self.etree.XML(
            '<values xmlns="http://xpath.test/ns"><b min="19" max="10">30</b></values>'
        )
        context = XPathContext(root)
        self.assertFalse(token.evaluate(context))
示例#20
0
 def test_is_instance_method(self):
     schema_proxy = XMLSchemaProxy(self.xs1)
     type_qname = '{%s}string' % self.xs1.meta_schema.target_namespace
     self.assertFalse(schema_proxy.is_instance(10, type_qname))
     self.assertTrue(schema_proxy.is_instance('10', type_qname))
 def test_cast_as_api(self):
     schema_proxy = XMLSchemaProxy()
     self.assertEqual(
         schema_proxy.cast_as('19', '{%s}short' % XSD_NAMESPACE), 19)
示例#22
0
 def test_cast_as_method(self):
     schema_proxy = XMLSchemaProxy(self.xs1)
     type_qname = '{%s}short' % self.xs1.meta_schema.target_namespace
     self.assertEqual(schema_proxy.cast_as('10', type_qname), 10)
 def test_get_type_api(self):
     schema_proxy = XMLSchemaProxy()
     self.assertIsNone(schema_proxy.get_type('unknown'))
     self.assertEqual(schema_proxy.get_type('{%s}string' % XSD_NAMESPACE),
                      xmlschema.XMLSchema.builtin_types()['string'])
 def test_get_context_method(self):
     schema_proxy = XMLSchemaProxy()
     self.assertIsInstance(schema_proxy.get_context(), XPathContext)
     self.assertIsInstance(
         super(XMLSchemaProxy, schema_proxy).get_context(), XPathContext)
示例#25
0
 def test_get_context_method(self):
     schema_proxy = XMLSchemaProxy(self.xs1)
     context = schema_proxy.get_context()
     self.assertIs(context.root, self.xs1)
示例#26
0
 def test_find_method(self):
     schema_proxy = XMLSchemaProxy(self.xs1)
     qname = '{%s}cars' % self.xs1.target_namespace
     self.assertIs(schema_proxy.find(qname), self.xs1.elements['cars'])