예제 #1
0
    def test_node_kind_property(self):
        document = DocumentNode(ElementTree.parse(io.StringIO(u'<A/>')))
        element = ElementNode(ElementTree.Element('schema'))
        attribute = AttributeNode('id', '0212349350')
        namespace = NamespaceNode('xs', 'http://www.w3.org/2001/XMLSchema')
        comment = CommentNode(ElementTree.Comment('nothing important'))
        pi = ProcessingInstructionNode(
            self.context,
            ElementTree.ProcessingInstruction('action', 'nothing to do'))
        text = TextNode('betelgeuse')

        self.assertEqual(document.kind, 'document')
        self.assertEqual(element.kind, 'element')
        self.assertEqual(attribute.kind, 'attribute')
        self.assertEqual(namespace.kind, 'namespace')
        self.assertEqual(comment.kind, 'comment')
        self.assertEqual(pi.kind, 'processing-instruction')
        self.assertEqual(text.kind, 'text')

        with patch.multiple(DummyXsdType, is_simple=lambda x: True):
            xsd_type = DummyXsdType()

            attribute = AttributeNode('id', '0212349350', xsd_type=xsd_type)
            self.assertEqual(attribute.kind, 'attribute')

            typed_element = ElementNode(element.elem, xsd_type=xsd_type)
            self.assertEqual(typed_element.kind, 'element')
예제 #2
0
    def test_select_results(self):
        token = self.parser.parse('.')
        elem = ElementTree.Element('A', attrib={'max': '30'})
        elem.text = '10'

        context = XPathContext(elem)
        self.assertListEqual(list(token.select_results(context)), [elem])

        context = XPathContext(elem, item=TypedElement(elem, 10))
        self.assertListEqual(list(token.select_results(context)), [elem])

        context = XPathContext(elem, item=AttributeNode('max', '30'))
        self.assertListEqual(list(token.select_results(context)), ['30'])

        context = XPathContext(elem,
                               item=TypedAttribute(AttributeNode('max', '30'),
                                                   30))
        self.assertListEqual(list(token.select_results(context)), [30])

        attribute = namedtuple('XsdAttribute', 'name type')('max', 'xs:string')
        context = XPathContext(elem,
                               item=TypedAttribute(
                                   AttributeNode('max', attribute), 30))
        self.assertListEqual(list(token.select_results(context)), [attribute])

        context = XPathContext(elem, item=10)
        self.assertListEqual(list(token.select_results(context)), [10])

        context = XPathContext(elem, item='10')
        self.assertListEqual(list(token.select_results(context)), ['10'])
예제 #3
0
 def test_node_is_idref_function(self):
     self.assertTrue(node_is_idrefs(ElementTree.XML('<A>xyz</A>')))
     self.assertTrue(node_is_idrefs(ElementTree.XML('<A>xyz abc</A>')))
     self.assertFalse(node_is_idrefs(ElementTree.XML('<A>12345</A>')))
     self.assertTrue(node_is_idrefs(AttributeNode('id', 'alpha')))
     self.assertTrue(node_is_idrefs(AttributeNode('id', 'alpha beta')))
     self.assertFalse(node_is_idrefs(AttributeNode('id', '12345')))
     self.assertIsNone(node_is_idrefs('a text node'))
예제 #4
0
 def test_is_attribute_node_function(self):
     attr = AttributeNode('a1', '10')
     self.assertTrue(is_attribute_node(attr, '*'))
     with self.assertRaises(ValueError):
         is_attribute_node(attr, '**')
     with self.assertRaises(ValueError):
         is_attribute_node(attr, '*:*:*')
     with self.assertRaises(ValueError):
         is_attribute_node(attr, 'foo:*')
     self.assertTrue(is_attribute_node(attr, '*:a1'))
     self.assertFalse(is_attribute_node(attr, '{foo}*'))
     self.assertTrue(is_attribute_node(AttributeNode('{foo}a1', '10'), '{foo}*'))
예제 #5
0
    def test_add_xsd_type_alternatives(self):
        schema = xmlschema.XMLSchema("""
            <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
              <xs:element name="root" type="xs:int"/>
              <xs:attribute name="a" type="xs:string"/>
            </xs:schema>""")

        root_token = self.parser.parse('root')
        self.assertIsNone(
            root_token.add_xsd_type('xs:string'))  # ignore non-schema items
        self.assertIsNone(root_token.xsd_types)

        xsd_type = root_token.add_xsd_type(schema.elements['root'])
        self.assertEqual(root_token.xsd_types,
                         {'root': schema.meta_schema.types['int']})
        self.assertIs(xsd_type, schema.meta_schema.types['int'])

        root_token.xsd_types = None
        typed_element = TypedElement(schema.elements['root'], xsd_type, 1)
        xsd_type = root_token.add_xsd_type(typed_element)
        self.assertEqual(root_token.xsd_types,
                         {'root': schema.meta_schema.types['int']})
        self.assertIs(xsd_type, schema.meta_schema.types['int'])

        attribute = AttributeNode('a', schema.attributes['a'])
        typed_attribute = TypedAttribute(attribute,
                                         schema.meta_schema.types['string'],
                                         'alpha')
        xsd_type = root_token.add_xsd_type(typed_attribute)
        self.assertEqual(
            root_token.xsd_types, {
                'a': schema.meta_schema.types['string'],
                'root': schema.meta_schema.types['int']
            })
        self.assertIs(xsd_type, schema.meta_schema.types['string'])
예제 #6
0
 def test_node_name_function(self):
     elem = ElementTree.Element('root')
     attr = AttributeNode('a1', '20')
     namespace = NamespaceNode('xs', 'http://www.w3.org/2001/XMLSchema')
     self.assertEqual(node_name(elem), 'root')
     self.assertEqual(node_name(attr), 'a1')
     self.assertEqual(node_name(namespace), 'xs')
예제 #7
0
    def test_match_attribute_node_function(self):
        attr = AttributeNode('a1', '10', parent=None)
        self.assertTrue(match_attribute_node(attr, '*'))
        self.assertTrue(match_attribute_node(TypedAttribute(attr, None, 10), 'a1'))
        with self.assertRaises(ValueError):
            match_attribute_node(attr, '**')
        with self.assertRaises(ValueError):
            match_attribute_node(attr, '*:*:*')
        with self.assertRaises(ValueError):
            match_attribute_node(attr, 'foo:*')
        self.assertTrue(match_attribute_node(attr, '*:a1'))
        self.assertFalse(match_attribute_node(attr, '{foo}*'))
        self.assertTrue(match_attribute_node(AttributeNode('{foo}a1', '10'), '{foo}*'))

        attr = AttributeNode('{http://xpath.test/ns}a1', '10', parent=None)
        self.assertTrue(match_attribute_node(attr, '*:a1'))
예제 #8
0
    def test_node_kind_function(self):
        document = ElementTree.parse(io.StringIO(u'<A/>'))
        element = ElementTree.Element('schema')
        attribute = AttributeNode('id', '0212349350')
        namespace = NamespaceNode('xs', 'http://www.w3.org/2001/XMLSchema')
        comment = ElementTree.Comment('nothing important')
        pi = ElementTree.ProcessingInstruction('action', 'nothing to do')
        text = TextNode('betelgeuse')
        self.assertEqual(node_kind(document), 'document-node')
        self.assertEqual(node_kind(element), 'element')
        self.assertEqual(node_kind(attribute), 'attribute')
        self.assertEqual(node_kind(namespace), 'namespace')
        self.assertEqual(node_kind(comment), 'comment')
        self.assertEqual(node_kind(pi), 'processing-instruction')
        self.assertEqual(node_kind(text), 'text')
        self.assertIsNone(node_kind(()))
        self.assertIsNone(node_kind(None))
        self.assertIsNone(node_kind(10))

        with patch.multiple(DummyXsdType, is_simple=lambda x: True):
            xsd_type = DummyXsdType()

            typed_attribute = TypedAttribute(attribute, xsd_type, '0212349350')
            self.assertEqual(node_kind(typed_attribute), 'attribute')

            typed_element = TypedElement(element, xsd_type, None)
            self.assertEqual(node_kind(typed_element), 'element')
예제 #9
0
    def test_name_property(self):
        root = self.context.root
        attr = AttributeNode('a1', '20')
        namespace = NamespaceNode('xs', 'http://www.w3.org/2001/XMLSchema')

        self.assertEqual(root.name, 'root')
        self.assertEqual(attr.name, 'a1')
        self.assertEqual(namespace.name, 'xs')

        with patch.multiple(DummyXsdType, is_simple=lambda x: True):
            xsd_type = DummyXsdType()

            typed_elem = ElementNode(elem=root.elem, xsd_type=xsd_type)
            self.assertEqual(typed_elem.name, 'root')

            typed_attr = AttributeNode('a1', value='20', xsd_type=xsd_type)
            self.assertEqual(typed_attr.name, 'a1')
예제 #10
0
    def test_get_typed_node(self):
        schema = xmlschema.XMLSchema("""
            <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
              <xs:element name="root" type="xs:int"/>
              <xs:attribute name="a" type="xs:int"/>
            </xs:schema>""")

        self.parser.schema = xmlschema.xpath.XMLSchemaProxy(schema)

        try:
            root_token = self.parser.parse('root')
            elem = ElementTree.Element('root')
            elem.text = '49'
            node = root_token.get_typed_node(elem)
            self.assertIsInstance(node, TypedElement)
            self.assertIsInstance(node.xsd_type, xmlschema.XsdType)
            self.assertEqual(node.value, 49)
            self.assertIs(root_token.get_typed_node(node), node)

            elem.text = 'beta'
            with self.assertRaises(TypeError) as err:
                root_token.get_typed_node(elem)
            self.assertIn('XPDY0050', str(err.exception))
            self.assertIn('does not match sequence type', str(err.exception))

            root_token.xsd_types['root'] = schema.meta_schema.types[
                'anySimpleType']
            elem.text = '36'
            node = root_token.get_typed_node(elem)
            self.assertIsInstance(node, TypedElement)
            self.assertIsInstance(node.xsd_type, xmlschema.XsdType)
            self.assertIsInstance(node.value, UntypedAtomic)
            self.assertEqual(node.value, 36)

            root_token.xsd_types['root'] = schema.meta_schema.types['anyType']
            node = root_token.get_typed_node(elem)
            self.assertIs(node.elem, elem)

            root_token = self.parser.parse('@a')
            self.assertEqual(root_token[0].xsd_types,
                             {'a': schema.meta_schema.types['int']})

            attribute = AttributeNode('a', '10')
            node = root_token[0].get_typed_node(attribute)
            self.assertIsInstance(node, TypedAttribute)
            self.assertIsInstance(node.xsd_type, xmlschema.XsdType)
            self.assertEqual(node.value, 10)

            root_token[0].xsd_types['a'] = schema.meta_schema.types['anyType']
            node = root_token[0].get_typed_node(attribute)
            self.assertIsInstance(node, TypedAttribute)
            self.assertIsInstance(node.xsd_type, xmlschema.XsdType)
            self.assertIsInstance(node.value, UntypedAtomic)
            self.assertEqual(node.value, 10)

        finally:
            self.parser.schema = None
예제 #11
0
    def test_string_value_function(self):
        token = self.parser.parse('true()')

        document = ElementTree.parse(
            io.StringIO(u'<A>123<B1>456</B1><B2>789</B2></A>'))
        element = ElementTree.Element('schema')
        comment = ElementTree.Comment('nothing important')
        pi = ElementTree.ProcessingInstruction('action', 'nothing to do')

        document_node = XPathContext(document).root

        context = XPathContext(element)
        element_node = context.root
        attribute_node = AttributeNode('id', '0212349350')
        namespace_node = NamespaceNode('xs',
                                       'http://www.w3.org/2001/XMLSchema')
        comment_node = CommentNode(comment)
        pi_node = ProcessingInstructionNode(pi)
        text_node = TextNode('betelgeuse')

        self.assertEqual(token.string_value(document_node), '123456789')
        self.assertEqual(token.string_value(element_node), '')
        self.assertEqual(token.string_value(attribute_node), '0212349350')
        self.assertEqual(token.string_value(namespace_node),
                         'http://www.w3.org/2001/XMLSchema')
        self.assertEqual(token.string_value(comment_node), 'nothing important')
        self.assertEqual(token.string_value(pi_node), 'action nothing to do')
        self.assertEqual(token.string_value(text_node), 'betelgeuse')
        self.assertEqual(token.string_value(None), '')

        self.assertEqual(token.string_value(Decimal(+1999)), '1999')
        self.assertEqual(token.string_value(Decimal('+1999')), '1999')
        self.assertEqual(token.string_value(Decimal('+19.0010')), '19.001')

        self.assertEqual(token.string_value(10), '10')
        self.assertEqual(token.string_value(1e99), '1E99')
        self.assertEqual(token.string_value(1e-05), '1E-05')
        self.assertEqual(token.string_value(1.00), '1')
        self.assertEqual(token.string_value(+19.0010), '19.001')

        self.assertEqual(token.string_value(float('nan')), 'NaN')
        self.assertEqual(token.string_value(float('inf')), 'INF')
        self.assertEqual(token.string_value(float('-inf')), '-INF')

        self.assertEqual(token.string_value(()), '()')

        tagged_object = Tagged()
        self.assertEqual(token.string_value(tagged_object),
                         "Tagged(tag='root')")

        with patch.multiple(DummyXsdType, is_simple=lambda x: True):
            xsd_type = DummyXsdType()
            element.text = '10'
            typed_elem = ElementNode(elem=element, xsd_type=xsd_type)
            self.assertEqual(token.string_value(typed_elem), '10')
            self.assertEqual(token.data_value(typed_elem), 10)
예제 #12
0
    def test_data_value_function(self):
        token = self.parser.parse('true()')

        if self.parser.version != '1.0':
            xsd_type = DummyXsdType()
            context = XPathContext(ElementTree.XML('<age>19</age>'))
            context.root.xsd_type = xsd_type
            self.assertEqual(token.data_value(context.root), 19)

        context = XPathContext(ElementTree.XML('<dummy/>'))
        obj = AttributeNode('age', '19')
        self.assertEqual(token.data_value(obj), UntypedAtomic('19'))

        obj = NamespaceNode('tns', 'http://xpath.test/ns')
        self.assertEqual(token.data_value(obj), 'http://xpath.test/ns')

        obj = TextNode('19')
        self.assertEqual(token.data_value(obj), UntypedAtomic('19'))

        obj = ElementTree.XML('<root>a<e1>b</e1>c<e2>d</e2>e</root>')
        element_node = ElementNode(obj)
        self.assertEqual(token.data_value(element_node),
                         UntypedAtomic('abcde'))

        obj = ElementTree.parse(
            io.StringIO('<root>a<e1>b</e1>c<e2>d</e2>e</root>'))
        document_node = DocumentNode(obj)
        self.assertEqual(token.data_value(document_node),
                         UntypedAtomic('abcde'))

        obj = ElementTree.Comment("foo bar")
        comment_node = CommentNode(obj)
        self.assertEqual(token.data_value(comment_node), 'foo bar')

        obj = ElementTree.ProcessingInstruction('action', 'nothing to do')
        pi_node = ProcessingInstructionNode(obj)
        self.assertEqual(token.data_value(pi_node), 'action nothing to do')

        self.assertIsNone(token.data_value(None))
        self.assertEqual(token.data_value(19), 19)
        self.assertEqual(token.data_value('19'), '19')
        self.assertFalse(token.data_value(False))

        # Does not check type of non nodes, simply returns the object.
        tagged_object = Tagged()
        self.assertIs(token.data_value(tagged_object), tagged_object)
예제 #13
0
 def test_node_kind_function(self):
     document = ElementTree.parse(io.StringIO(u'<A/>'))
     element = ElementTree.Element('schema')
     attribute = AttributeNode('id', '0212349350')
     namespace = NamespaceNode('xs', 'http://www.w3.org/2001/XMLSchema')
     comment = ElementTree.Comment('nothing important')
     pi = ElementTree.ProcessingInstruction('action', 'nothing to do')
     text = u'betelgeuse'
     self.assertEqual(node_kind(document), 'document')
     self.assertEqual(node_kind(element), 'element')
     self.assertEqual(node_kind(attribute), 'attribute')
     self.assertEqual(node_kind(namespace), 'namespace')
     self.assertEqual(node_kind(comment), 'comment')
     self.assertEqual(node_kind(pi), 'processing-instruction')
     self.assertEqual(node_kind(text), 'text')
     self.assertIsNone(node_kind(None))
     self.assertIsNone(node_kind(10))
예제 #14
0
    def test_node_name_function(self):
        elem = ElementTree.Element('root')
        attr = AttributeNode('a1', '20')
        namespace = NamespaceNode('xs', 'http://www.w3.org/2001/XMLSchema')
        self.assertEqual(node_name(elem), 'root')
        self.assertEqual(node_name(attr), 'a1')
        self.assertEqual(node_name(namespace), 'xs')
        self.assertIsNone(node_name(()))
        self.assertIsNone(node_name(None))

        with patch.multiple(DummyXsdType, is_simple=lambda x: True):
            xsd_type = DummyXsdType()

            typed_elem = TypedElement(elem=elem, xsd_type=xsd_type, value=10)
            self.assertEqual(node_name(typed_elem), 'root')

            typed_attr = TypedAttribute(attribute=attr, xsd_type=xsd_type, value=20)
            self.assertEqual(node_name(typed_attr), 'a1')
예제 #15
0
    def test_string_value_function(self):
        token = self.parser.parse('true()')

        document = ElementTree.parse(io.StringIO(u'<A>123<B1>456</B1><B2>789</B2></A>'))
        element = ElementTree.Element('schema')
        attribute = AttributeNode('id', '0212349350')
        namespace = NamespaceNode('xs', 'http://www.w3.org/2001/XMLSchema')
        comment = ElementTree.Comment('nothing important')
        pi = ElementTree.ProcessingInstruction('action', 'nothing to do')
        text = u'betelgeuse'
        self.assertEqual(token.string_value(document), '123456789')
        self.assertEqual(token.string_value(element), '')
        self.assertEqual(token.string_value(attribute), '0212349350')
        self.assertEqual(token.string_value(namespace), 'http://www.w3.org/2001/XMLSchema')
        self.assertEqual(token.string_value(comment), 'nothing important')
        self.assertEqual(token.string_value(pi), 'action nothing to do')
        self.assertEqual(token.string_value(text), 'betelgeuse')
        self.assertEqual(token.string_value(None), '')
        self.assertEqual(token.string_value(10), '10')
예제 #16
0
    def test_elem_iter_nodes_function(self):
        root = ElementTree.XML('<A>text1\n<B1 a="10">text2</B1><B2/><B3><C1>text3</C1></B3></A>')

        result = [root, TextNode('text1\n', root),
                  root[0], TextNode('text2', root[0]), root[1],
                  root[2], root[2][0], TextNode('text3', root[2][0])]

        self.assertListEqual(list(etree_iter_nodes(root)), result)
        self.assertListEqual(list(etree_iter_nodes(root, with_root=False)), result[1:])

        with patch.multiple(DummyXsdType, has_mixed_content=lambda x: True):
            xsd_type = DummyXsdType()
            typed_root = TypedElement(root, xsd_type, 'text1')
            self.assertListEqual(list(etree_iter_nodes(typed_root)), result)

        result = result[:4] + [AttributeNode('a', '10', root[0])] + result[4:]
        self.assertListEqual(list(etree_iter_nodes(root, with_attributes=True)), result)

        comment = ElementTree.Comment('foo')
        root[1].append(comment)
        self.assertListEqual(list(etree_iter_nodes(root, with_attributes=True)), result)
예제 #17
0
    def test_data_value_function(self):
        token = self.parser.parse('true()')

        if self.parser.version != '1.0':
            with patch.multiple(DummyXsdType(),
                                is_simple=lambda x: False,
                                has_simple_content=lambda x: True) as xsd_type:
                obj = TypedElement(ElementTree.XML('<age>19</age>'), xsd_type,
                                   19)
                self.assertEqual(token.data_value(obj), 19)

        obj = AttributeNode('age', '19')
        self.assertEqual(token.data_value(obj), UntypedAtomic('19'))

        obj = NamespaceNode('tns', 'http://xpath.test/ns')
        self.assertEqual(token.data_value(obj), 'http://xpath.test/ns')

        obj = TextNode('19')
        self.assertEqual(token.data_value(obj), UntypedAtomic('19'))

        obj = ElementTree.XML('<root>a<e1>b</e1>c<e2>d</e2>e</root>')
        self.assertEqual(token.data_value(obj), UntypedAtomic('abcde'))

        obj = ElementTree.parse(
            io.StringIO('<root>a<e1>b</e1>c<e2>d</e2>e</root>'))
        self.assertEqual(token.data_value(obj), UntypedAtomic('abcde'))

        obj = ElementTree.Comment("foo bar")
        self.assertEqual(token.data_value(obj), 'foo bar')

        obj = ElementTree.ProcessingInstruction('action', 'nothing to do')
        self.assertEqual(token.data_value(obj), 'action nothing to do')

        self.assertIsNone(token.data_value(None))
        self.assertEqual(token.data_value(19), 19)
        self.assertEqual(token.data_value('19'), '19')
        self.assertFalse(token.data_value(False))

        tagged_object = Tagged()
        self.assertIsNone(token.data_value(tagged_object))
예제 #18
0
    def test_attribute_nodes(self):
        parent = ElementTree.Element('element')
        attribute = AttributeNode('id', '0212349350')

        self.assertEqual(repr(attribute),
                         "AttributeNode(name='id', value='0212349350')")
        self.assertEqual(attribute, AttributeNode('id', '0212349350'))
        self.assertEqual(attribute.as_item(), ('id', '0212349350'))
        self.assertNotEqual(attribute.as_item(), AttributeNode('id', '0212349350'))
        self.assertNotEqual(attribute, AttributeNode('id', '0212349350', parent))

        attribute = AttributeNode('id', '0212349350', parent)
        self.assertEqual(attribute, AttributeNode('id', '0212349350', parent))
        self.assertEqual(attribute.as_item(), ('id', '0212349350'))
        self.assertNotEqual(attribute, AttributeNode('id', '0212349350'))
        self.assertNotEqual(attribute, AttributeNode('id', '0212349350',
                                                     parent=ElementTree.Element('element')))

        attribute = AttributeNode('value', '10', parent)
        self.assertEqual(repr(attribute)[:65],
                         "AttributeNode(name='value', value='10', parent=<Element 'element'")

        with patch.multiple(DummyXsdType, is_simple=lambda x: True):
            xsd_type = DummyXsdType()

            typed_attribute = TypedAttribute(attribute, xsd_type, 10)
            self.assertEqual(repr(typed_attribute), "TypedAttribute(name='value')")
            self.assertEqual(typed_attribute.as_item(), ('value', 10))

            self.assertEqual(typed_attribute, TypedAttribute(attribute, DummyXsdType(), 10))
            self.assertEqual(typed_attribute, TypedAttribute(attribute, None, 10))
            self.assertEqual(typed_attribute,
                             TypedAttribute(AttributeNode('value', '10', parent), xsd_type, 10))
            self.assertNotEqual(typed_attribute, TypedAttribute(attribute, xsd_type, '10'))
            self.assertNotEqual(typed_attribute,
                                TypedAttribute(AttributeNode('value', '10'), xsd_type, 10))
예제 #19
0
    def test_match_xsd_type(self):
        schema = xmlschema.XMLSchema("""
            <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
              <xs:element name="root" type="xs:int"/>
              <xs:attribute name="a" type="xs:string"/>
            </xs:schema>""")
        self.parser.schema = xmlschema.xpath.XMLSchemaProxy(schema)

        try:
            root_token = self.parser.parse('root')
            self.assertEqual(root_token.xsd_types,
                             {'root': schema.meta_schema.types['int']})

            context = XPathSchemaContext(root=schema)
            obj = list(root_token.select_xsd_nodes(context, 'root'))
            self.assertIsInstance(obj[0], ElementNode)
            self.assertEqual(root_token.xsd_types,
                             {'root': schema.meta_schema.types['int']})

            context.axis = 'self'
            root_token.xsd_types = None
            list(root_token.select_xsd_nodes(context, 'root'))
            self.assertIsNone(root_token.xsd_types)

            context.axis = None
            obj = list(root_token.select_xsd_nodes(context, 'root'))
            self.assertIsInstance(obj[0], ElementNode)

            context = XPathSchemaContext(root=schema.meta_schema)
            obj = list(root_token.select_xsd_nodes(context, 'root'))
            self.assertListEqual(obj, [])

            root_token = self.parser.parse('@a')
            self.assertEqual(root_token[0].xsd_types,
                             {'a': schema.meta_schema.types['string']})

            context = XPathSchemaContext(root=schema.meta_schema, axis='self')
            xsd_attribute = schema.attributes['a']
            context.item = AttributeNode('a',
                                         xsd_attribute,
                                         xsd_type=xsd_attribute.type)

            obj = list(root_token.select_xsd_nodes(context, 'a'))
            self.assertIsInstance(obj[0], AttributeNode)
            self.assertIsNotNone(obj[0].xsd_type)
            self.assertEqual(root_token[0].xsd_types,
                             {'a': schema.meta_schema.types['string']})

            root_token.xsd_types = None
            context = XPathSchemaContext(root=schema)
            list(root_token.select_xsd_nodes(context, 'a'))
            self.assertIsNone(root_token.xsd_types)

            context = XPathSchemaContext(root=schema.meta_schema, axis='self')
            attribute = context.item = AttributeNode('a',
                                                     schema.attributes['a'])

            obj = list(root_token.select_xsd_nodes(context, 'a'))
            self.assertIsInstance(obj[0], AttributeNode)
            self.assertEqual(obj[0], attribute)
            self.assertIsInstance(obj[0].value, xmlschema.XsdAttribute)
            self.assertIsInstance(obj[0].typed_value, str)
            self.assertEqual(root_token[0].xsd_types,
                             {'a': schema.meta_schema.types['string']})

        finally:
            self.parser.schema = None
예제 #20
0
    def test_get_xsd_type(self):
        schema = xmlschema.XMLSchema("""
            <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
              <xs:element name="root" type="xs:int"/>
              <xs:attribute name="a" type="xs:string"/>
            </xs:schema>""")

        root_token = self.parser.parse('root')
        self.assertIsNone(root_token.xsd_types)
        self.assertIsNone(root_token.get_xsd_type('root'))

        self.parser.schema = xmlschema.xpath.XMLSchemaProxy(schema)

        try:
            root_token = self.parser.parse('root')
            self.assertEqual(root_token.xsd_types,
                             {'root': schema.meta_schema.types['int']})

            xsd_type = root_token.get_xsd_type('root')
            self.assertEqual(xsd_type, schema.meta_schema.types['int'])
            self.assertIsNone(root_token.get_xsd_type('node'))

            TestElement = namedtuple('XsdElement', 'name xsd_version type')
            root_token.add_xsd_type(
                TestElement('node', '1.0', schema.meta_schema.types['float']))
            root_token.add_xsd_type(
                TestElement('node', '1.0',
                            schema.meta_schema.types['boolean']))
            root_token.add_xsd_type(
                TestElement('node', '1.0',
                            schema.meta_schema.types['decimal']))

            xsd_type = root_token.get_xsd_type('node')
            self.assertEqual(xsd_type, schema.meta_schema.types['float'])

            xsd_type = root_token.get_xsd_type(AttributeNode('node', 'false'))
            self.assertEqual(xsd_type, schema.meta_schema.types['boolean'])
            xsd_type = root_token.get_xsd_type(AttributeNode('node', 'alpha'))
            self.assertEqual(xsd_type, schema.meta_schema.types['float'])

            elem = ElementTree.Element('node')
            elem.text = 'false'
            xsd_type = root_token.get_xsd_type(ElementNode(elem))
            self.assertEqual(xsd_type, schema.meta_schema.types['boolean'])

            typed_element = ElementNode(elem, xsd_type=xsd_type)
            self.assertIs(xsd_type, root_token.get_xsd_type(typed_element))

            elem.text = 'alpha'
            xsd_type = root_token.get_xsd_type(ElementNode(elem))
            self.assertEqual(xsd_type, schema.meta_schema.types['float'])

        finally:
            self.parser.schema = None

        schema = xmlschema.XMLSchema("""
            <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
              <xs:element name="a" type="aType"/>
              <xs:complexType name="aType">
                <xs:sequence>
                  <xs:element name="b1" type="xs:int"/>
                  <xs:element name="b2" type="xs:boolean"/>
                </xs:sequence>
              </xs:complexType>
            </xs:schema>""")
        self.parser.schema = xmlschema.xpath.XMLSchemaProxy(schema)

        try:
            root_token = self.parser.parse('a')
            elem = ElementTree.Element('a')
            elem.append(ElementTree.Element('b1'))
            elem.append(ElementTree.Element('b2'))
            elem[0].text = 14
            elem[1].text = 'true'

            self.assertEqual(root_token.get_xsd_type(ElementNode(elem)),
                             schema.types['aType'])

            TestElement = namedtuple('XsdElement', 'name xsd_version type')
            root_token.add_xsd_type(
                TestElement('a', '1.0', schema.meta_schema.types['float']))
            self.assertEqual(root_token.get_xsd_type(ElementNode(elem)),
                             schema.types['aType'])

            root_token.xsd_types['a'].insert(
                0, schema.meta_schema.types['boolean'])
            self.assertEqual(root_token.get_xsd_type(ElementNode(elem)),
                             schema.types['aType'])

            del elem[1]
            self.assertEqual(root_token.get_xsd_type(ElementNode(elem)),
                             schema.meta_schema.types['boolean'])
        finally:
            self.parser.schema = None
예제 #21
0
    def test_match_name_method(self):
        attr = AttributeNode('a1', '10', parent=None)
        self.assertTrue(attr.match_name('*'))
        self.assertTrue(attr.match_name('a1'))
        self.assertTrue(attr.match_name('*:a1'))
        self.assertFalse(attr.match_name('{foo}*'))
        self.assertFalse(attr.match_name('foo:*'))

        self.assertTrue(AttributeNode('{foo}a1', '10').match_name('{foo}*'))

        attr = AttributeNode('{http://xpath.test/ns}a1', '10', parent=None)
        self.assertTrue(attr.match_name('*:a1'))
예제 #22
0
    def test_attribute_nodes(self):
        parent = self.context.root
        attribute = AttributeNode('id', '0212349350')

        self.assertEqual(repr(attribute),
                         "AttributeNode(name='id', value='0212349350')")
        self.assertNotEqual(attribute, AttributeNode('id', '0212349350'))
        self.assertEqual(attribute.as_item(), ('id', '0212349350'))
        self.assertNotEqual(attribute.as_item(),
                            AttributeNode('id', '0212349350'))
        self.assertNotEqual(attribute, AttributeNode('id', '0212349350',
                                                     parent))

        attribute = AttributeNode('id', '0212349350', parent)
        self.assertNotEqual(attribute, AttributeNode('id', '0212349350',
                                                     parent))
        self.assertEqual(attribute.as_item(), ('id', '0212349350'))

        attribute = AttributeNode('value', '10', parent)
        self.assertEqual(repr(attribute),
                         "AttributeNode(name='value', value='10')")

        with patch.multiple(DummyXsdType, is_simple=lambda x: True):
            xsd_type = DummyXsdType()
            attribute.xsd_type = xsd_type
            self.assertEqual(attribute.as_item(), ('value', '10'))