File: test_xpath.py

package info (click to toggle)
python-xmlschema 4.1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 5,208 kB
  • sloc: python: 39,174; xml: 1,282; makefile: 36
file content (442 lines) | stat: -rw-r--r-- 19,887 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
#!/usr/bin/env python
#
# Copyright (c), 2016-2020, SISSA (International School for Advanced Studies).
# All rights reserved.
# This file is distributed under the terms of the MIT License.
# See the file 'LICENSE' in the root directory of the present
# distribution, or http://opensource.org/licenses/MIT.
#
# @author Davide Brunato <brunato@sissa.it>
#
"""Tests for XPath parsing and selectors"""

import unittest
import os
import pathlib
from xml.etree import ElementTree

from elementpath import XPath1Parser, XPath2Parser, Selector, LazyElementNode

from xmlschema import XMLSchema10, XMLSchema11
from xmlschema.names import XSD_NAMESPACE
from xmlschema.xpath import XMLSchemaProxy, XPathElement, split_path, ElementSelector
from xmlschema.validators import XsdAtomic, XsdAtomicRestriction

CASES_DIR = os.path.join(os.path.dirname(__file__), 'test_cases/')


class XMLSchemaProxyTest(unittest.TestCase):

    schema_class = XMLSchema10

    @classmethod
    def setUpClass(cls):
        cls.xs1 = cls.schema_class(os.path.join(CASES_DIR, "examples/vehicles/vehicles.xsd"))
        cls.xs2 = cls.schema_class(os.path.join(CASES_DIR, "examples/collection/collection.xsd"))
        cls.xs3 = cls.schema_class(os.path.join(CASES_DIR, "features/decoder/simple-types.xsd"))

    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'))  # noqa

    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)

    @unittest.skip(
        "Requires network access, not granted during the Debian build")
    def test_get_context_method(self):
        schema_proxy = XMLSchemaProxy(self.xs1)
        context = schema_proxy.get_context()
        self.assertIs(context.root.value, self.xs1)

    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_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))

    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))

    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))

    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'])

    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_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_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)


class XPathElementTest(unittest.TestCase):

    schema_class = XMLSchema10
    col_xsd_path = None

    @classmethod
    def setUpClass(cls):
        cls.col_xsd_path = pathlib.Path(CASES_DIR).joinpath("examples/collection/collection.xsd")
        cls.col_schema = cls.schema_class(cls.col_xsd_path)

    def test_is_matching(self):
        # The mixin method is used by schema class but overridden for XSD components.
        # A schema has no formal name, so it takes the source's filename, if any.
        # This does not have effect on validation because schema is the root.
        self.assertEqual(self.col_schema.default_namespace, 'http://example.com/ns/collection')
        self.assertEqual(self.col_schema.name, 'collection.xsd')
        self.assertTrue(self.col_schema.is_matching('collection.xsd'))
        self.assertFalse(
            self.col_schema.is_matching('collection.xsd', 'http://example.com/ns/collection')
        )

    def test_iteration(self):
        elem = XPathElement('foo', self.col_schema.types['objType'])
        self.assertListEqual(
            [child.name for child in elem],
            ['position', 'title', 'year', 'author', 'estimation', 'characters']
        )

        elem = XPathElement('foo', self.col_schema.builtin_types()['string'])
        self.assertListEqual(list(elem), [])

    def test_xpath_proxy(self):
        elem = XPathElement('foo', self.col_schema.types['objType'])
        xpath_proxy = elem.xpath_proxy
        self.assertIsInstance(xpath_proxy, XMLSchemaProxy)
        self.assertIs(xpath_proxy._schema, self.col_schema)

    def test_xpath_node(self):
        elem = XPathElement('foo', self.col_schema.types['objType'])
        xpath_node = elem.xpath_node
        self.assertIsInstance(xpath_node, LazyElementNode)
        self.assertIs(xpath_node, elem._xpath_node)
        self.assertIs(xpath_node, elem.xpath_node)

    def test_schema(self):
        elem = XPathElement('foo', self.col_schema.types['objType'])
        self.assertIs(elem.schema, self.col_schema)
        self.assertIs(elem.namespaces, self.col_schema.namespaces)

    def test_target_namespace(self):
        elem = XPathElement('foo', self.col_schema.types['objType'])
        self.assertEqual(elem.target_namespace, 'http://example.com/ns/collection')

    def test_xsd_version(self):
        elem = XPathElement('foo', self.col_schema.types['objType'])
        self.assertEqual(elem.xsd_version, self.col_schema.xsd_version)

    def test_maps(self):
        elem = XPathElement('foo', self.col_schema.types['objType'])
        self.assertIs(elem.maps, self.col_schema.maps)

    def test_elem_name(self):
        elem = XPathElement('foo', self.col_schema.types['objType'])
        try:
            elem.namespaces['col'] = 'http://example.com/ns/collection'

            self.assertEqual(elem.local_name, 'foo')
            self.assertEqual(elem.qualified_name, '{http://example.com/ns/collection}foo')
            self.assertEqual(elem.prefixed_name, 'foo')

            elem = XPathElement('{http://example.com/ns/collection}foo',
                                self.col_schema.types['objType'])
            self.assertEqual(elem.local_name, 'foo')
            self.assertEqual(elem.qualified_name, '{http://example.com/ns/collection}foo')
            self.assertEqual(elem.prefixed_name, 'col:foo')
        finally:
            elem.namespaces.pop('col')


class XMLSchemaXPathTest(unittest.TestCase):

    schema_class = XMLSchema10
    xs1: XMLSchema10

    @classmethod
    def setUpClass(cls):
        cls.xs1 = cls.schema_class(os.path.join(CASES_DIR, "examples/vehicles/vehicles.xsd"))
        cls.xs2 = cls.schema_class(os.path.join(CASES_DIR, "examples/collection/collection.xsd"))
        cls.cars = cls.xs1.elements['vehicles'].type.content[0]
        cls.bikes = cls.xs1.elements['vehicles'].type.content[1]

    def test_xpath_wrong_syntax(self):
        self.assertRaises(SyntaxError, self.xs1.find, './*[')
        self.assertRaises(SyntaxError, self.xs1.find, './*)')
        self.assertRaises(SyntaxError, self.xs1.find, './*3')
        self.assertRaises(SyntaxError, self.xs1.find, './@3')

    def test_xpath_extra_spaces(self):
        self.assertTrue(self.xs1.find('./ *') is not None)
        self.assertTrue(self.xs1.find("\t\n vh:vehicles / vh:cars / .. /  vh:cars") == self.cars)

    def test_xpath_location_path(self):
        elements = sorted(self.xs1.elements.values(), key=lambda x: x.name)
        self.assertTrue(self.xs1.findall('.'))
        self.assertTrue(isinstance(self.xs1.find('.'), self.schema_class))
        self.assertTrue(sorted(self.xs1.findall("*"), key=lambda x: x.name) == elements)
        self.assertListEqual(self.xs1.findall("*"), self.xs1.findall("./*"))
        self.assertEqual(self.xs1.find("./vh:bikes"), self.xs1.elements['bikes'])
        self.assertEqual(self.xs1.find("./vh:vehicles/vh:cars").name,
                         self.xs1.elements['cars'].name)
        self.assertNotEqual(self.xs1.find("./vh:vehicles/vh:cars"), self.xs1.elements['cars'])
        self.assertNotEqual(self.xs1.find("/vh:vehicles/vh:cars"), self.xs1.elements['cars'])
        self.assertEqual(self.xs1.find("vh:vehicles/vh:cars/.."), self.xs1.elements['vehicles'])
        self.assertEqual(self.xs1.find("vh:vehicles/*/.."), self.xs1.elements['vehicles'])
        self.assertEqual(self.xs1.find("vh:vehicles/vh:cars/../vh:cars"),
                         self.xs1.find("vh:vehicles/vh:cars"))

    def test_xpath_axis(self):
        self.assertEqual(self.xs1.find("vh:vehicles/child::vh:cars/.."),
                         self.xs1.elements['vehicles'])

    def test_xpath_subscription(self):
        self.assertEqual(len(self.xs1.findall("./vh:vehicles/*")), 2)
        self.assertListEqual(self.xs1.findall("./vh:vehicles/*[2]"), [self.bikes])
        self.assertListEqual(self.xs1.findall("./vh:vehicles/*[3]"), [])
        self.assertListEqual(self.xs1.findall("./vh:vehicles/*[last()-1]"), [self.cars])
        self.assertListEqual(self.xs1.findall("./vh:vehicles/*[position()=last()]"), [self.bikes])

    def test_xpath_group(self):
        self.assertEqual(self.xs1.findall("/(vh:vehicles/*/*)"),
                         self.xs1.findall("/vh:vehicles/*/*"))
        self.assertEqual(self.xs1.findall("/(vh:vehicles/*/*)[1]"),
                         self.xs1.findall("/vh:vehicles/*/*[1]")[:1])

    def test_xpath_predicate(self):
        car = self.xs1.elements['cars'].type.content[0]

        self.assertListEqual(self.xs1.findall("./vh:vehicles/vh:cars/vh:car[@make]"), [car])
        self.assertListEqual(self.xs1.findall("./vh:vehicles/vh:cars/vh:car[@make]"), [car])
        self.assertListEqual(self.xs1.findall("./vh:vehicles/vh:cars['ciao']"), [self.cars])
        self.assertListEqual(self.xs1.findall("./vh:vehicles/*['']"), [])

    def test_xpath_descendants(self):
        selector = Selector('.//xs:element', self.xs2.namespaces, parser=XPath1Parser)
        elements = list(selector.iter_select(self.xs2.root))
        self.assertEqual(len(elements), 14)
        selector = Selector('.//xs:element|.//xs:attribute|.//xs:keyref',
                            self.xs2.namespaces, parser=XPath1Parser)
        elements = list(selector.iter_select(self.xs2.root))
        self.assertEqual(len(elements), 17)

    def test_xpath_issues(self):
        namespaces = {'ps': "http://schemas.microsoft.com/powershell/2004/04"}
        selector = Selector("./ps:Props/*|./ps:MS/*", namespaces=namespaces, parser=XPath1Parser)
        self.assertTrue(selector.root_token.tree,
                        '(| (/ (/ (.) (: (ps) (Props))) (*)) (/ (/ (.) (: (ps) (MS))) (*)))')

    def test_get(self):
        xsd_element = self.xs1.elements['vehicles']
        self.assertIsNone(xsd_element.get('unknown'))
        self.assertEqual(xsd_element[0][0].get('model'), xsd_element[0][0].attributes['model'])

    def test_getitem(self):
        xsd_element = self.xs1.elements['vehicles']
        self.assertEqual(xsd_element[0], xsd_element.type.content[0])
        self.assertEqual(xsd_element[1], xsd_element.type.content[1])
        with self.assertRaises(IndexError):
            _ = xsd_element[2]

    def test_reversed(self):
        xsd_element = self.xs1.elements['vehicles']
        self.assertListEqual(
            list(reversed(xsd_element)),
            [xsd_element.type.content[1], xsd_element.type.content[0]]
        )

    def test_iterfind(self):
        car = self.xs1.find('//vh:car')
        bike = self.xs1.find('//vh:bike')
        self.assertIsNotNone(car)
        self.assertIsNotNone(bike)
        self.assertListEqual(list(self.xs1.iterfind("/(vh:vehicles/*/*)")), [car, bike])

    def test_iter(self):
        xsd_element = self.xs1.elements['vehicles']
        descendants = list(xsd_element.iter())
        self.assertListEqual(descendants, [xsd_element] + xsd_element.type.content[:])

        descendants = list(xsd_element.iter('*'))
        self.assertListEqual(descendants, [xsd_element] + xsd_element.type.content[:])

        descendants = list(xsd_element.iter(self.xs1.elements['cars'].name))
        self.assertListEqual(descendants, [xsd_element.type.content[0]])

    def test_iterchildren(self):
        children = list(self.xs1.elements['vehicles'].iterchildren())
        self.assertListEqual(children, self.xs1.elements['vehicles'].type.content[:])
        children = list(self.xs1.elements['vehicles'].iterchildren('*'))
        self.assertListEqual(children, self.xs1.elements['vehicles'].type.content[:])
        children = list(self.xs1.elements['vehicles'].iterchildren(self.xs1.elements['bikes'].name))
        self.assertListEqual(children, self.xs1.elements['vehicles'].type.content[1:])


class XPathSelectorsTest(unittest.TestCase):

    def test_rel_xpath_boolean(self):
        root = ElementTree.XML('<A><B><C/></B></A>')
        el = root[0]
        self.assertTrue(Selector('boolean(C)').iter_select(el))
        self.assertFalse(next(Selector('boolean(D)').iter_select(el)))

    def test_split_path(self):
        path = '/md:EntitiesDescriptor/md:EntityDescriptor[@entityID="https://xmlschema.test"]'
        result = '/md:EntitiesDescriptor/md:EntityDescriptor[@entityID="https://xmlschema.test"]'
        self.assertEqual(''.join(split_path(path)), result)

        path = 'md:EntitiesDescriptor/md:EntityDescriptor[@entityID="https://xmlschema.test"]'
        result = 'md:EntitiesDescriptor/md:EntityDescriptor[@entityID="https://xmlschema.test"]'
        self.assertEqual(''.join(split_path(path)), result)

        namespaces = {'': 'foo'}
        self.assertEqual(''.join(split_path(path, namespaces)), result)

        path = '/A/B/C'
        self.assertEqual(''.join(split_path(path)), '/A/B/C')
        self.assertEqual(''.join(split_path(path, namespaces)), '/{foo}A/{foo}B/{foo}C')

        path = 'A/B/C'
        self.assertEqual(''.join(split_path(path)), 'A/B/C')
        self.assertEqual(''.join(split_path(path, namespaces)), '{foo}A/{foo}B/{foo}C')

        path = 'A/{}B/C[@D="1"]'
        self.assertEqual(''.join(split_path(path)), 'A/{}B/C[@D="1"]')
        self.assertEqual(''.join(split_path(path, namespaces)),
                         '{foo}A/{}B/{foo}C[@D="1"]')

        path = 'A/{bar}B/C[@D="1"]'
        self.assertEqual(''.join(split_path(path)), 'A/{bar}B/C[@D="1"]')
        self.assertEqual(''.join(split_path(path, namespaces)),
                         '{foo}A/{bar}B/{foo}C[@D="1"]')

        path = 'A/p:B/C[@D="1"]/E'
        self.assertEqual(''.join(split_path(path)), 'A/p:B/C[@D="1"]/E')
        self.assertEqual(''.join(split_path(path, namespaces)),
                         '{foo}A/p:B/{foo}C[@D="1"]/{foo}E')

    def test_split_path_parts(self):
        path = '/md:EntitiesDescriptor/md:EntityDescriptor[@entityID="https://xmlschema.test"]'
        result = ['/', 'md:EntitiesDescriptor', '/', 'md:EntityDescriptor', '[', '@',
                  'entityID', '=', '"https://xmlschema.test"', ']']
        self.assertListEqual(list(split_path(path)), result)

        path = '/A/B/C'
        result = ['/', '{foo}A', '/', '{foo}B', '/', '{foo}C']
        namespaces = {'': 'foo'}
        self.assertEqual(list(split_path(path, namespaces)), result)
        self.assertEqual(list(split_path(path)), ['/', 'A', '/', 'B', '/', 'C'])

        path = 'A/B/C'
        self.assertEqual(list(split_path(path)), ['A', '/', 'B', '/', 'C'])
        self.assertEqual(list(split_path(path, namespaces)), result[1:])

        path = 'A/{}B/C[@D="1"]'
        result = ['A', '/', '{}B', '/', 'C', '[', '@', 'D', '=', '"1"', ']']
        self.assertEqual(list(split_path(path)), result)
        self.assertEqual(list(split_path(path, namespaces)),
                         ['{foo}A', '/', '{}B', '/', '{foo}C', '[', '@', 'D', '=', '"1"', ']'])

        path = 'A/{bar}B/C[@D="1"]'
        self.assertEqual(
            list(split_path(path)),
            ['A', '/', '{bar}B', '/', 'C', '[', '@', 'D', '=', '"1"', ']']
        )
        self.assertEqual(
            list(split_path(path, namespaces)),
            ['{foo}A', '/', '{bar}B', '/', '{foo}C', '[', '@', 'D', '=', '"1"', ']']
        )

        path = 'A/p:B/C[@D="1"]/E'
        self.assertEqual(
            list(split_path(path)),
            ['A', '/', 'p:B', '/', 'C', '[', '@', 'D', '=', '"1"', ']', '/', 'E']
        )
        self.assertEqual(
            list(split_path(path, namespaces)),
            ['{foo}A', '/', 'p:B', '/', '{foo}C', '[', '@', 'D', '=', '"1"', ']', '/', '{foo}E']
        )

        self.assertEqual(
            list(split_path(path, namespaces, extended_names=True)),
            ['{foo}A', '/', 'p:B', '/', '{foo}C', '[', '@', 'D', '=', '"1"', ']', '/', '{foo}E']
        )

        namespaces = {'': 'foo', 'p': 'bar'}
        self.assertEqual(
            list(split_path(path, namespaces, extended_names=True)),
            ['{foo}A', '/', '{bar}B', '/', '{foo}C', '[', '@', 'D', '=', '"1"', ']', '/', '{foo}E']
        )

        path = 'A//p:B/C[2]/E'
        self.assertEqual(
            list(split_path(path)),
            ['A', '//', 'p:B', '/', 'C', '[', '2', ']', '/', 'E']
        )
        self.assertEqual(
            list(split_path(path, namespaces)),
            ['{foo}A', '//', 'p:B', '/', '{foo}C', '[', '2', ']', '/', '{foo}E']
        )
        self.assertEqual(list(split_path('.')), ['.'])
        self.assertEqual(list(split_path('*/b')), ['*', '/', 'b'])

    def test_element_selector(self):
        selector = ElementSelector('*')
        self.assertEqual(list(selector.parts), ['*'])


if __name__ == '__main__':
    from xmlschema.testing import run_xmlschema_tests
    run_xmlschema_tests('XPath processor')