File: test_xsd_globals.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 (240 lines) | stat: -rw-r--r-- 9,320 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
#!/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>
#
import unittest
import warnings
from typing import Any

from xmlschema import XMLSchema10, XMLSchema11
from xmlschema.namespaces import NamespaceView
import xmlschema.names as nm


class TestGlobalMapsViews(unittest.TestCase):

    @classmethod
    def setUpClass(cls):
        XMLSchema10.meta_schema.build()
        cls.comp0 = XMLSchema10.meta_schema.maps.types[nm.XSD_STRING]
        cls.comp1 = XMLSchema10.meta_schema.maps.types[nm.XSD_DOUBLE]
        cls.comp2 = XMLSchema10.meta_schema.maps.types[nm.XSD_INT]

    @classmethod
    def tearDownClass(cls):
        XMLSchema10.meta_schema.clear()

    def test_init(self):
        components: Any = {'{tns0}name0': 0, '{tns1}name1': 1, 'name2': 2}
        ns_view = NamespaceView(components, 'tns1')
        self.assertEqual(ns_view, {'name1': 1})

    def test_repr(self):
        qnames: Any = {'{tns0}name0': 0, '{tns1}name1': 1, 'name2': 2}
        ns_view = NamespaceView(qnames, 'tns0')
        self.assertEqual(repr(ns_view), "NamespaceView({'name0': 0})")

    def test_getitem(self):
        qnames: Any = {'{tns0}name0': 0, '{tns1}name1': 1, 'name2': 2}
        ns_view = NamespaceView(qnames, 'tns1')

        self.assertEqual(ns_view['name1'], 1)

        with self.assertRaises(KeyError):
            ns_view['name0']  # noqa

    def test_contains(self):
        qnames: Any = {'{tns0}name0': 0, '{tns1}name1': 1, 'name2': 2}
        ns_view = NamespaceView(qnames, 'tns1')

        self.assertIn('name1', ns_view)
        self.assertNotIn('{tns1}name1', ns_view)
        self.assertNotIn('{tns0}name0', ns_view)
        self.assertNotIn('name0', ns_view)
        self.assertNotIn('name2', ns_view)
        self.assertNotIn(1, ns_view)

    def test_as_dict(self):
        qnames: Any = {'{tns0}name0': 0, '{tns1}name1': 1, '{tns1}name2': 2, 'name3': 3}
        ns_view = NamespaceView(qnames, 'tns1')
        self.assertEqual(ns_view.as_dict(), {'name1': 1, 'name2': 2})

        ns_view = NamespaceView(qnames, '')
        self.assertEqual(ns_view.as_dict(), {'name3': 3})

    def test_iter(self):
        qnames: Any = {'{tns0}name0': 0, '{tns1}name1': 1, '{tns1}name2': 2, 'name3': 3}
        ns_view = NamespaceView(qnames, 'tns1')
        self.assertListEqual(list(ns_view), ['name1', 'name2'])
        ns_view = NamespaceView(qnames, '')
        self.assertListEqual(list(ns_view), ['name3'])


class TestXsd10GlobalsMaps(unittest.TestCase):

    schema_class = XMLSchema10

    @classmethod
    def setUpClass(cls):
        cls.schema_class.meta_schema.build()
        cls.total_globals = cls.schema_class.meta_schema.maps.global_maps.total
        cls.total_components = len(list(cls.schema_class.meta_schema.maps.iter_components()))

    @classmethod
    def tearDownClass(cls):
        cls.schema_class.meta_schema.clear()

    def test_maps_repr(self):
        self.assertEqual(
            repr(XMLSchema10.meta_schema.maps),
            "XsdGlobals(validator=MetaXMLSchema10(name='XMLSchema.xsd', "
            "namespace='http://www.w3.org/2001/XMLSchema'))"
        )

    def test_lookup(self):
        with self.assertRaises(KeyError):
            XMLSchema10.meta_schema.maps.lookup(nm.XSD_ELEMENT, 'wrong')

        xs_string = XMLSchema10.meta_schema.maps.lookup(nm.XSD_SIMPLE_TYPE, nm.XSD_STRING)
        self.assertEqual(xs_string.name, nm.XSD_STRING)

        with self.assertRaises(ValueError):
            XMLSchema10.meta_schema.maps.lookup('simpleType', nm.XSD_STRING)

    def test_deprecated_api(self):
        with warnings.catch_warnings(record=True) as ctx:
            warnings.simplefilter("always")

            maps_dir = dir(XMLSchema10.meta_schema.maps)
            for k, name in enumerate(filter(lambda x: x.startswith('lookup_'), maps_dir)):
                with self.assertRaises(KeyError):
                    getattr(XMLSchema10.meta_schema.maps, name)('wrong')

                self.assertEqual(len(ctx), k+1)
                self.assertEqual(ctx[k].category, DeprecationWarning)
                self.assertIn('will be removed in v5.0', ctx[k].message.args[0])

    def test_copy(self):
        maps = self.schema_class.meta_schema.maps.copy()
        orig = self.schema_class.meta_schema.maps

        self.assertIsNot(maps, orig)
        for name in ('types', 'attributes', 'elements', 'groups', 'attribute_groups',
                     'notations', 'identities', 'substitution_groups'):
            self.assertIsNot(getattr(maps, name), getattr(orig, name))

        self.assertEqual(maps.validation, orig.validation)
        self.assertIsNot(maps.validator, orig.validator)
        self.assertEqual(maps.global_maps.total, 0)

        self.assertEqual(len(maps.namespaces), len(orig.namespaces))
        for k, v in orig.namespaces.items():
            self.assertIn(k, maps.namespaces)
            self.assertEqual(len(maps.namespaces[k]), len(v))

        maps.build()
        self.assertEqual(maps.global_maps.total, self.total_globals)

    def test_clear(self):
        maps = self.schema_class.meta_schema.maps.copy()
        orig = self.schema_class.meta_schema.maps

        self.assertEqual(maps.global_maps.total, 0)
        self.assertEqual(orig.global_maps.total, self.total_globals)

        maps.build()
        self.assertEqual(maps.global_maps.total, self.total_globals)
        self.assertEqual(orig.global_maps.total, self.total_globals)
        maps.clear()
        self.assertEqual(maps.global_maps.total, 0)
        self.assertEqual(orig.global_maps.total, self.total_globals)
        maps.build()
        self.assertEqual(maps.global_maps.total, self.total_globals)
        self.assertEqual(orig.global_maps.total, self.total_globals)

        maps.clear(remove_schemas=True)
        self.assertEqual(maps.global_maps.total, 0)
        self.assertEqual(orig.global_maps.total, self.total_globals)

        # XSD meta-schema is still there but incomplete
        self.assertEqual(len(maps.schemas), 1)

        maps.build()
        self.assertEqual(maps.global_maps.total, self.total_globals)
        self.assertEqual(orig.global_maps.total, self.total_globals)

    def test_totals(self):
        self.assertEqual(len(XMLSchema10.meta_schema.maps.notations), 2)
        self.assertEqual(len(XMLSchema10.meta_schema.maps.types), 92)
        self.assertEqual(len(XMLSchema10.meta_schema.maps.attributes), 8)
        self.assertEqual(len(XMLSchema10.meta_schema.maps.attribute_groups), 3)
        self.assertEqual(len(XMLSchema10.meta_schema.maps.groups), 12)
        self.assertEqual(len(XMLSchema10.meta_schema.maps.elements), 41)
        self.assertEqual(
            len([e.is_global() for e in XMLSchema10.meta_schema.maps.iter_globals()]), 158
        )
        self.assertEqual(self.total_components, 808)
        self.assertEqual(len(XMLSchema10.meta_schema.maps.substitution_groups), 0)

    def test_build(self):
        self.assertEqual(len([
            e for e in self.schema_class.meta_schema.maps.iter_globals()
        ]), self.total_globals)
        self.assertTrue(self.schema_class.meta_schema.maps.built)
        self.schema_class.meta_schema.maps.clear()
        self.schema_class.meta_schema.maps.build()
        self.assertTrue(self.schema_class.meta_schema.maps.built)

    def test_components(self):
        total_counter = 0
        global_counter = 0
        for g in self.schema_class.meta_schema.maps.iter_globals():
            for c in g.iter_components():
                total_counter += 1
                if c.is_global():
                    global_counter += 1
        self.assertEqual(global_counter, self.total_globals)


class TestXsd11GlobalsMaps(TestXsd10GlobalsMaps):

    schema_class = XMLSchema11

    @classmethod
    def setUpClass(cls):
        super().setUpClass()

    @classmethod
    def tearDownClass(cls):
        super().tearDownClass()

    def test_totals(self):
        self.assertEqual(len(XMLSchema11.meta_schema.maps.notations), 2)
        self.assertEqual(len(XMLSchema11.meta_schema.maps.types), 103)
        self.assertEqual(len(XMLSchema11.meta_schema.maps.attributes), 10)
        self.assertEqual(len(XMLSchema11.meta_schema.maps.attribute_groups), 4)
        self.assertEqual(len(XMLSchema11.meta_schema.maps.groups), 13)
        self.assertEqual(len(XMLSchema11.meta_schema.maps.elements), 47)
        self.assertEqual(
            len([e.is_global() for e in XMLSchema11.meta_schema.maps.iter_globals()]), 179
        )
        self.assertEqual(self.total_globals, 179)
        self.assertEqual(self.total_components, 964)
        self.assertEqual(len(XMLSchema11.meta_schema.maps.substitution_groups), 1)

    def test_xsd_11_restrictions(self):
        all_model_type = self.schema_class.meta_schema.types['all']
        self.assertTrue(
            all_model_type.content.is_restriction(all_model_type.base_type.content)
        )


if __name__ == '__main__':
    from xmlschema.testing import run_xmlschema_tests
    run_xmlschema_tests('global maps')