File: test_namespaces.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 (506 lines) | stat: -rw-r--r-- 21,952 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
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
#!/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 io
import unittest
import copy
from textwrap import dedent

from xmlschema import XMLResource, XMLSchemaConverter
from xmlschema.locations import get_locations
from xmlschema.names import XSD_NAMESPACE, XSI_NAMESPACE
from xmlschema.namespaces import NamespaceMapper, NamespaceResourcesMap


class TestNamespaceMapper(unittest.TestCase):

    @classmethod
    def setUpClass(cls):
        cls.xml_data = dedent("""\
            <root xmlns="http://example.test/foo">
               <elem1 xmlns="http://example.test/bar"/>
               <bar:elem2 xmlns:bar="http://example.test/bar"/>
               <elem3 xmlns="http://example.test/foo"/>
               <foo:elem4 xmlns:foo="http://example.test/foo"/>
               <foo:elem5 xmlns:foo="http://example.test/bar"/>
               <foo:elem6 xmlns:foo="http://example.test/foo2"/>
               <elem7 xmlns=""/>
            </root>""")

    def test_init(self):
        namespaces = dict(xs=XSD_NAMESPACE, xsi=XSI_NAMESPACE)
        mapper = NamespaceMapper(namespaces)
        self.assertEqual(mapper, namespaces)
        self.assertIsNot(namespaces, mapper.namespaces)

    def test_dictionary_methods(self):
        namespaces = dict(xs=XSD_NAMESPACE)
        mapper = NamespaceMapper(namespaces)

        mapper['xsi'] = XSI_NAMESPACE
        self.assertEqual(mapper, dict(xs=XSD_NAMESPACE, xsi=XSI_NAMESPACE))

        del mapper['xs']
        self.assertEqual(len(mapper), 1)
        self.assertEqual(mapper, dict(xsi=XSI_NAMESPACE))

        mapper.clear()
        self.assertEqual(mapper, {})

    def test_strip_namespaces_and_process_namespaces_arguments(self):
        namespaces = dict(xs=XSD_NAMESPACE, xsi=XSI_NAMESPACE)

        mapper = NamespaceMapper(namespaces, strip_namespaces=False)
        self.assertFalse(mapper.strip_namespaces)
        self.assertTrue(mapper.process_namespaces)
        self.assertTrue(mapper._use_namespaces)
        self.assertEqual(mapper.map_qname('{%s}name' % XSD_NAMESPACE), 'xs:name')
        self.assertEqual(mapper.map_qname('{unknown}name'), '{unknown}name')

        mapper = NamespaceMapper(namespaces, strip_namespaces=True)
        self.assertTrue(mapper.strip_namespaces)
        self.assertTrue(mapper.process_namespaces)
        self.assertFalse(mapper._use_namespaces)
        self.assertEqual(mapper.map_qname('{%s}name' % XSD_NAMESPACE), 'name')
        self.assertEqual(mapper.map_qname('{unknown}name'), 'name')

        mapper = NamespaceMapper(namespaces, process_namespaces=True)
        self.assertEqual(mapper.map_qname('{%s}name' % XSD_NAMESPACE), 'xs:name')
        self.assertEqual(mapper.map_qname('{unknown}name'), '{unknown}name')

        mapper = NamespaceMapper(namespaces, process_namespaces=False)
        self.assertEqual(mapper.map_qname(f'{XSD_NAMESPACE}name'), f'{XSD_NAMESPACE}name')
        self.assertEqual(mapper.map_qname('{unknown}name'), '{unknown}name')

        mapper = NamespaceMapper(namespaces, process_namespaces=False, strip_namespaces=True)
        self.assertEqual(mapper.map_qname('{%s}name' % XSD_NAMESPACE), 'name')
        self.assertEqual(mapper.map_qname('{unknown}name'), 'name')

    def test_xmlns_processing_argument_with_resource(self):
        namespaces = dict(xs=XSD_NAMESPACE, xsi=XSI_NAMESPACE)
        resource = XMLResource('<root/>')

        kwargs = {
            'namespaces': namespaces,
            'source': resource,
        }
        mapper = NamespaceMapper(**kwargs)
        self.assertEqual(mapper.xmlns_processing, 'stacked')
        self.assertIsNotNone(mapper._xmlns_getter)

        mapper = NamespaceMapper(namespaces)
        self.assertEqual(mapper.xmlns_processing, 'none')
        self.assertIsNone(mapper._xmlns_getter)

        mapper = NamespaceMapper(xmlns_processing='collapsed', **kwargs)
        self.assertEqual(mapper.xmlns_processing, 'collapsed')
        self.assertIsNotNone(mapper._xmlns_getter)

        mapper = NamespaceMapper(xmlns_processing='root-only', **kwargs)
        self.assertEqual(mapper.xmlns_processing, 'root-only')
        self.assertIsNotNone(mapper._xmlns_getter)

        mapper = NamespaceMapper(xmlns_processing='none', **kwargs)
        self.assertEqual(mapper.xmlns_processing, 'none')
        self.assertIsNone(mapper._xmlns_getter)

    def test_xmlns_processing_argument_with_data_source(self):
        namespaces = dict(xs=XSD_NAMESPACE, xsi=XSI_NAMESPACE)

        mapper = NamespaceMapper(namespaces, source=dict())
        self.assertEqual(mapper.xmlns_processing, 'none')
        self.assertIsNone(mapper._xmlns_getter)

        mapper = NamespaceMapper(namespaces, xmlns_processing='collapsed', source=dict())
        self.assertEqual(mapper.xmlns_processing, 'collapsed')
        self.assertIsNotNone(mapper._xmlns_getter)

        # None can be a valid decoded value in certain cases
        mapper = NamespaceMapper(namespaces, xmlns_processing='collapsed', source=None)
        self.assertEqual(mapper.xmlns_processing, 'collapsed')
        self.assertIsNotNone(mapper._xmlns_getter)

    def test_invalid_xmlns_processing_argument(self):
        with self.assertRaises(ValueError):
            NamespaceMapper(xmlns_processing='nothing')

        with self.assertRaises(TypeError):
            NamespaceMapper(xmlns_processing=False)

    def test_source_argument(self):
        resource = XMLResource('<root/>')
        mapper = NamespaceMapper(source=resource)
        self.assertIs(mapper.source, resource)
        self.assertIsNotNone(mapper._xmlns_getter)

        mapper = NamespaceMapper()
        self.assertIsNone(mapper.source)
        self.assertIsNone(mapper._xmlns_getter)

    def test_get_xmlns_from_data(self):
        self.assertIsNone(NamespaceMapper().get_xmlns_from_data({}))

    def test_get_namespaces(self):
        namespaces = dict(xs=XSD_NAMESPACE, xsi=XSI_NAMESPACE)
        resource = XMLResource('<root xmlns="http://example.test/foo"/>')
        data = {'@xmlns:xs': "http://example.test/foo", 'value': [1, 2]}

        mapper = NamespaceMapper(namespaces)
        self.assertEqual(mapper.get_namespaces(), {})

        mapper = NamespaceMapper(namespaces)
        self.assertEqual(mapper.get_namespaces(namespaces), namespaces)

        mapper = NamespaceMapper(namespaces, xmlns_processing='stacked')
        self.assertEqual(mapper.get_namespaces(), {})

        mapper = NamespaceMapper(namespaces, xmlns_processing='stacked')
        self.assertEqual(mapper.get_namespaces(namespaces), namespaces)

        mapper = NamespaceMapper(namespaces, xmlns_processing='stacked', source=data)
        self.assertEqual(mapper.get_namespaces(), {})

        mapper = NamespaceMapper(namespaces, xmlns_processing='stacked', source=data)
        self.assertEqual(mapper.get_namespaces(namespaces), namespaces)

        mapper = NamespaceMapper(namespaces, xmlns_processing='stacked', source=resource)
        self.assertEqual(mapper.get_namespaces(), {'': 'http://example.test/foo'})

        mapper = NamespaceMapper(namespaces, xmlns_processing='stacked', source=resource)
        self.assertEqual(mapper.get_namespaces(namespaces),
                         {**namespaces, **{'': 'http://example.test/foo'}})

        mapper = XMLSchemaConverter(namespaces, xmlns_processing='stacked', source=data)
        self.assertEqual(mapper.get_namespaces(), {'xs': 'http://example.test/foo'})

        mapper = XMLSchemaConverter(namespaces, xmlns_processing='stacked', source=data)
        self.assertEqual(mapper.get_namespaces(namespaces),
                         {**namespaces, **{'xs0': 'http://example.test/foo'}})

    def test_copy(self):
        namespaces = dict(xs=XSD_NAMESPACE, xsi=XSI_NAMESPACE)

        mapper = NamespaceMapper(namespaces, strip_namespaces=True)
        other = copy.copy(mapper)

        self.assertIsNot(mapper.namespaces, other.namespaces)
        self.assertDictEqual(mapper.namespaces, other.namespaces)

    def test_default_namespace(self):
        namespaces = dict(xs=XSD_NAMESPACE, xsi=XSI_NAMESPACE)
        mapper = NamespaceMapper(namespaces)

        self.assertIsNone(mapper.default_namespace)
        mapper[''] = 'tns0'
        self.assertEqual(mapper.default_namespace, 'tns0')

    def test_map_qname(self):
        namespaces = dict(xs=XSD_NAMESPACE, xsi=XSI_NAMESPACE)
        mapper = NamespaceMapper(namespaces)

        mapper[''] = XSD_NAMESPACE
        self.assertEqual(mapper.map_qname(''), '')
        self.assertEqual(mapper.map_qname('foo'), 'foo')
        self.assertEqual(mapper.map_qname('{%s}element' % XSD_NAMESPACE), 'element')
        mapper.pop('')
        self.assertEqual(mapper.map_qname('{%s}element' % XSD_NAMESPACE), 'xs:element')

        with self.assertRaises(ValueError) as ctx:
            mapper.map_qname('{%selement' % XSD_NAMESPACE)
        self.assertIn("invalid value", str(ctx.exception))

        with self.assertRaises(ValueError) as ctx:
            mapper.map_qname('{%s}element}' % XSD_NAMESPACE)
        self.assertIn("invalid value", str(ctx.exception))

        with self.assertRaises(TypeError) as ctx:
            mapper.map_qname(None)
        self.assertIn("must be a string-like object", str(ctx.exception))

        with self.assertRaises(TypeError) as ctx:
            mapper.map_qname(99)
        self.assertIn("must be a string-like object", str(ctx.exception))

        mapper = NamespaceMapper(namespaces, process_namespaces=False)
        self.assertEqual(mapper.map_qname('bar'), 'bar')
        self.assertEqual(mapper.map_qname('xs:bar'), 'xs:bar')

        mapper = NamespaceMapper(namespaces, strip_namespaces=True)
        self.assertEqual(mapper.map_qname('bar'), 'bar')
        self.assertEqual(mapper.map_qname('xs:bar'), 'bar')

    def test_unmap_qname(self):
        namespaces = dict(xs=XSD_NAMESPACE, xsi=XSI_NAMESPACE)
        mapper = NamespaceMapper(namespaces)

        self.assertEqual(mapper.unmap_qname(''), '')
        self.assertEqual(mapper.unmap_qname('xs:element'), '{%s}element' % XSD_NAMESPACE)
        self.assertEqual(mapper.unmap_qname('{foo}bar'), '{foo}bar')
        self.assertEqual(mapper.unmap_qname('xsd:element'), 'xsd:element')

        with self.assertRaises(ValueError) as ctx:
            mapper.unmap_qname('xs::element')
        self.assertIn("invalid value", str(ctx.exception))

        with self.assertRaises(TypeError) as ctx:
            mapper.unmap_qname(None)
        self.assertIn("must be a string-like object", str(ctx.exception))

        with self.assertRaises(TypeError) as ctx:
            mapper.unmap_qname(99)
        self.assertIn("must be a string-like object", str(ctx.exception))

        self.assertEqual(mapper.unmap_qname('element'), 'element')
        mapper[''] = 'foo'
        self.assertEqual(mapper.unmap_qname('element'), '{foo}element')
        self.assertEqual(mapper.unmap_qname('element', name_table=['element']), 'element')

        mapper.strip_namespaces = True  # don't do tricks, create a new instance ...
        self.assertEqual(mapper.unmap_qname('element'), '{foo}element')

        mapper = NamespaceMapper(namespaces, process_namespaces=False)
        self.assertEqual(mapper.unmap_qname('bar'), 'bar')
        self.assertEqual(mapper.unmap_qname('xs:bar'), 'xs:bar')

        mapper = NamespaceMapper(namespaces, strip_namespaces=True)
        self.assertEqual(mapper.unmap_qname('bar'), 'bar')
        self.assertEqual(mapper.unmap_qname('xs:bar'), 'bar')

        mapper = NamespaceMapper(namespaces)
        self.assertEqual(mapper.unmap_qname('foo:bar'), 'foo:bar')
        xmlns = [('foo', 'http://example.com/foo')]
        self.assertEqual(
            mapper.unmap_qname('foo:bar', xmlns=xmlns),
            '{http://example.com/foo}bar'
        )

    def test_set_context_with_stacked_xmlns_processing(self):
        resource = XMLResource(io.StringIO(self.xml_data))

        mapper = NamespaceMapper(source=resource)
        self.assertEqual(mapper.xmlns_processing, 'stacked')
        self.assertEqual(len(mapper._contexts), 0)

        xmlns = mapper.set_context(resource.root, 0)
        self.assertEqual(len(mapper._contexts), 1)
        self.assertIs(mapper._contexts[-1].obj, resource.root)
        self.assertEqual(mapper._contexts[-1].level, 0)
        self.assertIs(mapper._contexts[-1].xmlns, xmlns)
        self.assertEqual(mapper._contexts[-1].namespaces,
                         {'': 'http://example.test/foo'})
        self.assertEqual(mapper._contexts[-1].reverse,
                         {'http://example.test/foo': ''})
        self.assertListEqual(xmlns, [('', 'http://example.test/foo')])

        xmlns = mapper.set_context(resource.root, 0)
        self.assertEqual(len(mapper._contexts), 1)
        self.assertIs(mapper._contexts[-1].obj, resource.root)
        self.assertListEqual(xmlns, [('', 'http://example.test/foo')])

        mapper.set_context(resource.root[0], 1)
        self.assertEqual(len(mapper._contexts), 2)
        self.assertIs(mapper._contexts[-1].obj, resource.root[0])

        mapper.set_context(resource.root[1], 1)
        self.assertEqual(len(mapper._contexts), 2)
        self.assertIs(mapper._contexts[-1].obj, resource.root[1])

        resource = XMLResource('<root/>')

        mapper = NamespaceMapper(source=resource)
        self.assertEqual(mapper.xmlns_processing, 'stacked')
        self.assertEqual(len(mapper._contexts), 0)

        xmlns = mapper.set_context(resource.root, 0)
        self.assertEqual(len(mapper._contexts), 0)
        self.assertIsNone(xmlns)

    def test_set_context_with_collapsed_xmlns_processing(self):
        resource = XMLResource(io.StringIO(self.xml_data))

        mapper = NamespaceMapper(source=resource, xmlns_processing='collapsed')
        self.assertEqual(mapper.xmlns_processing, 'collapsed')

        xmlns = mapper.set_context(resource.root, 0)
        self.assertEqual(len(mapper._contexts), 0)
        self.assertIsNone(xmlns)
        self.assertEqual(mapper.namespaces, {'': 'http://example.test/foo'})

        mapper.set_context(resource.root[0], 1)
        self.assertEqual(mapper.namespaces,
                         {'': 'http://example.test/foo',
                          'default': 'http://example.test/bar'})

        mapper.set_context(resource.root[1], 1)
        self.assertEqual(mapper.namespaces,
                         {'': 'http://example.test/foo',
                          'default': 'http://example.test/bar',
                          'bar': 'http://example.test/bar'})

        mapper.set_context(resource.root[2], 1)
        self.assertEqual(mapper.namespaces,
                         {'': 'http://example.test/foo',
                          'default': 'http://example.test/bar',
                          'bar': 'http://example.test/bar'})

        mapper.set_context(resource.root[3], 1)
        self.assertEqual(mapper.namespaces,
                         {'': 'http://example.test/foo',
                          'default': 'http://example.test/bar',
                          'bar': 'http://example.test/bar',
                          'foo': 'http://example.test/foo'})

        mapper.set_context(resource.root[4], 1)
        self.assertEqual(mapper.namespaces,
                         {'': 'http://example.test/foo',
                          'default': 'http://example.test/bar',
                          'bar': 'http://example.test/bar',
                          'foo': 'http://example.test/foo',
                          'foo0': 'http://example.test/bar'})

        mapper.set_context(resource.root[4], 1)
        self.assertEqual(mapper.namespaces,
                         {'': 'http://example.test/foo',
                          'default': 'http://example.test/bar',
                          'bar': 'http://example.test/bar',
                          'foo': 'http://example.test/foo',
                          'foo0': 'http://example.test/bar'})

        mapper.set_context(resource.root[5], 1)
        self.assertEqual(mapper.namespaces,
                         {'': 'http://example.test/foo',
                          'default': 'http://example.test/bar',
                          'bar': 'http://example.test/bar',
                          'foo': 'http://example.test/foo',
                          'foo0': 'http://example.test/bar',
                          'foo1': 'http://example.test/foo2'})

        mapper.set_context(resource.root[6], 1)
        self.assertEqual(mapper.namespaces,
                         {'': 'http://example.test/foo',
                          'default': 'http://example.test/bar',
                          'bar': 'http://example.test/bar',
                          'foo': 'http://example.test/foo',
                          'foo0': 'http://example.test/bar',
                          'foo1': 'http://example.test/foo2'})

        # With default namespaces in non-root elements
        xml_data = dedent("""\
                    <foo:root xmlns:foo="http://example.test/foo">
                       <elem1 xmlns="http://example.test/bar"/>
                       <elem2 xmlns="http://example.test/foo"/>
                    </foo:root>""")
        resource = XMLResource(io.StringIO(xml_data))

        mapper = NamespaceMapper(source=resource, xmlns_processing='collapsed')
        mapper.set_context(resource.root[0], 1)
        self.assertEqual(mapper.namespaces,
                         {'foo': 'http://example.test/foo',
                          'default': 'http://example.test/bar'})

        mapper = NamespaceMapper(source=resource, xmlns_processing='collapsed')
        mapper.set_context(resource.root[0], 0)
        self.assertEqual(mapper.namespaces,
                         {'foo': 'http://example.test/foo',
                          '': 'http://example.test/bar'})

        mapper = NamespaceMapper(source=resource, xmlns_processing='collapsed')
        mapper.set_context(resource.root[1], 0)
        self.assertEqual(mapper.namespaces,
                         {'foo': 'http://example.test/foo',
                          '': 'http://example.test/foo'})

    def test_set_context_with_root_only_xmlns_processing(self):
        resource = XMLResource(io.StringIO(self.xml_data))

        mapper = NamespaceMapper(source=resource, xmlns_processing='root-only')
        self.assertEqual(mapper.xmlns_processing, 'root-only')

        xmlns = mapper.set_context(resource.root, 0)
        self.assertEqual(len(mapper._contexts), 0)
        self.assertIsNone(xmlns)
        self.assertEqual(mapper.namespaces, {'': 'http://example.test/foo'})

        mapper.set_context(resource.root[0], 1)
        self.assertEqual(mapper.namespaces, {'': 'http://example.test/foo'})

    def test_set_context_with_none_xmlns_processing(self):
        resource = XMLResource(io.StringIO(self.xml_data))
        namespaces = {'foo': 'http://example.test/foo'}

        mapper = NamespaceMapper(source=resource, xmlns_processing='none')
        self.assertEqual(mapper.xmlns_processing, 'none')
        xmlns = mapper.set_context(resource.root, 0)
        self.assertEqual(len(mapper._contexts), 0)
        self.assertIsNone(xmlns)
        self.assertEqual(mapper.namespaces, {})

        mapper = NamespaceMapper(namespaces, source=resource, xmlns_processing='none')
        xmlns = mapper.set_context(resource.root, 0)
        self.assertEqual(mapper.namespaces, namespaces)
        self.assertIsNone(xmlns)

    def test_set_context_with_encoding(self):
        mapper = NamespaceMapper()
        obj = {'@xmlns:foo': 'http://example.test/foo'}

        xmlns = mapper.set_context(obj, level=0)
        self.assertEqual(len(mapper._contexts), 0)
        self.assertIsNone(xmlns)


class TestNamespaceResourcesMap(unittest.TestCase):

    def test_init(self):
        nsmap = [('tns0', 'schema1.xsd')]
        self.assertEqual(NamespaceResourcesMap(), {})
        self.assertEqual(NamespaceResourcesMap(nsmap), {'tns0': ['schema1.xsd']})
        nsmap.append(('tns0', 'schema2.xsd'))
        self.assertEqual(NamespaceResourcesMap(nsmap), {'tns0': ['schema1.xsd', 'schema2.xsd']})

    def test_repr(self):
        namespaces = NamespaceResourcesMap()
        namespaces['tns0'] = 'schema1.xsd'
        namespaces['tns1'] = 'schema2.xsd'
        self.assertEqual(repr(namespaces), "{'tns0': ['schema1.xsd'], 'tns1': ['schema2.xsd']}")

    def test_dictionary_methods(self):
        namespaces = NamespaceResourcesMap()
        namespaces['tns0'] = 'schema1.xsd'
        namespaces['tns1'] = 'schema2.xsd'
        self.assertEqual(namespaces, {'tns0': ['schema1.xsd'], 'tns1': ['schema2.xsd']})

        self.assertEqual(len(namespaces), 2)
        self.assertEqual({x for x in namespaces}, {'tns0', 'tns1'})

        del namespaces['tns0']
        self.assertEqual(namespaces, {'tns1': ['schema2.xsd']})
        self.assertEqual(len(namespaces), 1)

        namespaces.clear()
        self.assertEqual(namespaces, {})

    def test_copy(self):
        namespaces = NamespaceResourcesMap(
            (('tns0', 'schema1.xsd'), ('tns1', 'schema2.xsd'), ('tns0', 'schema3.xsd'))
        )
        self.assertEqual(namespaces, namespaces.copy())

    def test_get_locations(self):
        self.assertEqual(get_locations(None), NamespaceResourcesMap())
        self.assertRaises(TypeError, get_locations, 1)
        locations = (('tns0', 'schema1.xsd'), ('tns1', 'schema2.xsd'), ('tns0', 'schema3.xsd'))

        self.assertEqual(get_locations(locations), NamespaceResourcesMap(locations))


if __name__ == '__main__':
    from xmlschema.testing import run_xmlschema_tests
    run_xmlschema_tests('helpers for namespaces')