File: test_exports.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 (365 lines) | stat: -rw-r--r-- 16,479 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
#!/usr/bin/env python
#
# Copyright (c), 2016-2024, 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 filecmp
import glob
import os
import re
import pathlib
import platform
import tempfile
import warnings

from xmlschema import XMLSchema10, XMLSchema11
from xmlschema.exports import download_schemas
from xmlschema.testing import SKIP_REMOTE_TESTS, XMLSchemaTestCase, run_xmlschema_tests


class TestExports(XMLSchemaTestCase):
    cases_dir = pathlib.Path(__file__).absolute().parent.joinpath('test_cases')

    @classmethod
    def setUpClass(cls):
        cls.vh_dir = cls.casepath('examples/vehicles')
        cls.vh_xsd_file = vh_xsd_file = cls.casepath('examples/vehicles/vehicles.xsd')
        cls.vh_xml_file = cls.casepath('examples/vehicles/vehicles.xml')
        cls.vh_schema = cls.schema_class(vh_xsd_file)

    def test_export_errors__issue_187(self):
        with self.assertRaises(ValueError) as ctx:
            self.vh_schema.export(target=self.vh_dir)

        self.assertIn("target directory", str(ctx.exception))
        self.assertIn("is not empty", str(ctx.exception))

        with self.assertRaises(ValueError) as ctx:
            self.vh_schema.export(target=self.vh_xsd_file)

        self.assertIn("target", str(ctx.exception))
        self.assertIn("is not a directory", str(ctx.exception))

        with self.assertRaises(ValueError) as ctx:
            self.vh_schema.export(target=self.vh_xsd_file + '/target')

        self.assertIn("target parent", str(ctx.exception))
        self.assertIn("is not a directory", str(ctx.exception))

        with tempfile.TemporaryDirectory() as dirname:
            with self.assertRaises(ValueError) as ctx:
                self.vh_schema.export(target=dirname + 'subdir/target')

            self.assertIn("target parent directory", str(ctx.exception))
            self.assertIn("does not exist", str(ctx.exception))

    def test_export_same_directory__issue_187(self):
        with tempfile.TemporaryDirectory() as dirname:
            self.vh_schema.export(target=dirname)

            for filename in os.listdir(dirname):
                with pathlib.Path(dirname).joinpath(filename).open() as fp:
                    exported_schema = fp.read()
                with pathlib.Path(self.vh_dir).joinpath(filename).open() as fp:
                    original_schema = fp.read()

                if platform.system() == 'Windows':
                    exported_schema = re.sub(r'\s+', '', exported_schema)
                    original_schema = re.sub(r'\s+', '', original_schema)

                self.assertEqual(exported_schema, original_schema)

        self.assertFalse(os.path.isdir(dirname))

    def test_export_another_directory__issue_187(self):
        vh_schema_file = self.casepath('issues/issue_187/issue_187_1.xsd')
        vh_schema = self.schema_class(vh_schema_file)

        with tempfile.TemporaryDirectory() as dirname:
            vh_schema.export(target=dirname)

            path = pathlib.Path(dirname).joinpath('examples/vehicles/*.xsd')
            for filename in glob.iglob(pathname=str(path)):
                with pathlib.Path(dirname).joinpath(filename).open() as fp:
                    exported_schema = fp.read()

                basename = os.path.basename(filename)
                with pathlib.Path(self.vh_dir).joinpath(basename).open() as fp:
                    original_schema = fp.read()

                if platform.system() == 'Windows':
                    exported_schema = re.sub(r'\s+', '', exported_schema)
                    original_schema = re.sub(r'\s+', '', original_schema)

                self.assertEqual(exported_schema, original_schema)

            with pathlib.Path(dirname).joinpath('issue_187_1.xsd').open() as fp:
                exported_schema = fp.read()
            with open(vh_schema_file) as fp:
                original_schema = fp.read()

            if platform.system() == 'Windows':
                exported_schema = re.sub(r'\s+', '', exported_schema)
                original_schema = re.sub(r'\s+', '', original_schema)

            self.assertNotEqual(exported_schema, original_schema)

            if platform.system() != 'Windows':
                repl = str(pathlib.Path('file').joinpath(str(self.cases_dir).lstrip('/')))
                self.assertEqual(
                    exported_schema,
                    original_schema.replace('../..', repl)
                )

            schema_file = pathlib.Path(dirname).joinpath('issue_187_1.xsd')
            schema = self.schema_class(schema_file)
            ns_schemas = schema.maps.namespaces['http://example.com/vehicles']

            self.assertEqual(len(ns_schemas), 4)
            self.assertEqual(ns_schemas[0].name, 'issue_187_1.xsd')
            self.assertEqual(ns_schemas[1].name, 'cars.xsd')
            self.assertEqual(ns_schemas[2].name, 'types.xsd')
            self.assertEqual(ns_schemas[3].name, 'bikes.xsd')

        self.assertFalse(os.path.isdir(dirname))

    @unittest.skipIf(SKIP_REMOTE_TESTS, "Remote networks are not accessible.")
    def test_export_remote__issue_187(self):
        vh_schema_file = self.casepath('issues/issue_187/issue_187_2.xsd')
        vh_schema = self.schema_class(vh_schema_file)

        with tempfile.TemporaryDirectory() as dirname:
            vh_schema.export(target=dirname)

            with pathlib.Path(dirname).joinpath('issue_187_2.xsd').open() as fp:
                exported_schema = fp.read()
            with open(vh_schema_file) as fp:
                original_schema = fp.read()

            if platform.system() == 'Windows':
                exported_schema = re.sub(r'\s+', '', exported_schema)
                original_schema = re.sub(r'\s+', '', original_schema)

            self.assertEqual(exported_schema, original_schema)

        self.assertFalse(os.path.isdir(dirname))

        with tempfile.TemporaryDirectory() as dirname:
            vh_schema.export(target=dirname, save_remote=True)
            path = pathlib.Path(dirname).joinpath('brunato/xmlschema/master/tests/test_cases/'
                                                  'examples/vehicles/*.xsd')

            for filename in glob.iglob(pathname=str(path)):
                with pathlib.Path(dirname).joinpath(filename).open() as fp:
                    exported_schema = fp.read()

                basename = os.path.basename(filename)
                with pathlib.Path(self.vh_dir).joinpath(basename).open() as fp:
                    original_schema = fp.read()
                self.assertEqual(exported_schema, original_schema)

            with pathlib.Path(dirname).joinpath('issue_187_2.xsd').open() as fp:
                exported_schema = fp.read()
            with open(vh_schema_file) as fp:
                original_schema = fp.read()

            if platform.system() == 'Windows':
                exported_schema = re.sub(r'\s+', '', exported_schema)
                original_schema = re.sub(r'\s+', '', original_schema)

            self.assertNotEqual(exported_schema, original_schema)
            self.assertNotIn('https://', exported_schema)
            self.assertEqual(
                exported_schema,
                original_schema.replace('https://raw.githubusercontent.com',
                                        'https/raw.githubusercontent.com')
            )

            schema_file = pathlib.Path(dirname).joinpath('issue_187_2.xsd')
            schema = self.schema_class(schema_file)
            ns_schemas = schema.maps.namespaces['http://example.com/vehicles']

            self.assertEqual(len(ns_schemas), 4)
            self.assertEqual(ns_schemas[0].name, 'issue_187_2.xsd')
            self.assertEqual(ns_schemas[1].name, 'cars.xsd')
            self.assertEqual(ns_schemas[2].name, 'types.xsd')
            self.assertEqual(ns_schemas[3].name, 'bikes.xsd')

        self.assertFalse(os.path.isdir(dirname))

        # Test with DEBUG logging level
        with tempfile.TemporaryDirectory() as dirname:
            with self.assertLogs('xmlschema', level='DEBUG') as ctx:
                vh_schema.export(target=dirname, save_remote=True, loglevel='DEBUG')
                self.assertGreater(len(ctx.output), 0)
                self.assertTrue(any('Write modified ' in line for line in ctx.output))
                self.assertTrue(any('Write unchanged ' in line for line in ctx.output))

        self.assertFalse(os.path.isdir(dirname))

    @unittest.skipIf(platform.system() == 'Windows', 'skip, Windows systems save with <CR><LF>')
    def test_export_other_encoding(self):
        schema_file = self.casepath('examples/menù/menù.xsd')
        schema_ascii_file = self.casepath('examples/menù/menù-ascii.xsd')
        schema_cp1252_file = self.casepath('examples/menù/menù-cp1252.xsd')

        schema = self.schema_class(schema_file)
        with tempfile.TemporaryDirectory() as dirname:
            schema.export(target=dirname)
            exported_schema = pathlib.Path(dirname).joinpath('menù.xsd')
            self.assertTrue(filecmp.cmp(schema_file, exported_schema))
            self.assertFalse(filecmp.cmp(schema_ascii_file, exported_schema))
            self.assertFalse(filecmp.cmp(schema_cp1252_file, exported_schema))

        schema = self.schema_class(schema_ascii_file)
        with tempfile.TemporaryDirectory() as dirname:
            schema.export(target=dirname)
            exported_schema = pathlib.Path(dirname).joinpath('menù-ascii.xsd')
            self.assertFalse(filecmp.cmp(schema_file, exported_schema))
            self.assertTrue(filecmp.cmp(schema_ascii_file, exported_schema))
            self.assertFalse(filecmp.cmp(schema_cp1252_file, exported_schema))

        schema = self.schema_class(schema_cp1252_file)
        with tempfile.TemporaryDirectory() as dirname:
            schema.export(target=dirname)
            exported_schema = pathlib.Path(dirname).joinpath('menù-cp1252.xsd')
            self.assertFalse(filecmp.cmp(schema_file, exported_schema))
            self.assertFalse(filecmp.cmp(schema_ascii_file, exported_schema))
            self.assertTrue(filecmp.cmp(schema_cp1252_file, exported_schema))

    def test_export_more_remote_imports__issue_362(self):
        schema_file = self.casepath('issues/issue_362/issue_362_1.xsd')
        with warnings.catch_warnings(record=True):
            warnings.simplefilter("always")
            schema = self.schema_class(schema_file)

        self.assertIn('{http://xmlschema.test/tns1}root', schema.maps.elements)
        self.assertIn('{http://xmlschema.test/tns1}item1', schema.maps.elements)
        self.assertIn('{http://xmlschema.test/tns2}item2', schema.maps.elements)
        self.assertIn('{http://xmlschema.test/tns2}item3', schema.maps.elements)

        with tempfile.TemporaryDirectory() as dirname:
            schema.export(target=dirname)

            exported_files = {
                str(x.relative_to(dirname)).replace('\\', '/')
                for x in pathlib.Path(dirname).glob('**/*.xsd')
            }
            self.assertSetEqual(
                exported_files,
                {'issue_362_1.xsd', 'dir2/issue_362_2.xsd', 'dir1/issue_362_1.xsd',
                 'dir1/dir2/issue_362_2.xsd', 'issue_362_1.xsd', 'dir2/issue_362_2.xsd',
                 'dir1/issue_362_1.xsd', 'dir1/dir2/issue_362_2.xsd'}
            )

            schema_file = pathlib.Path(dirname).joinpath('issue_362_1.xsd')
            schema = self.schema_class(schema_file)
            self.assertIn('{http://xmlschema.test/tns1}root', schema.maps.elements)
            self.assertIn('{http://xmlschema.test/tns1}item1', schema.maps.elements)
            self.assertIn('{http://xmlschema.test/tns2}item2', schema.maps.elements)
            self.assertIn('{http://xmlschema.test/tns2}item3', schema.maps.elements)


class TestExports11(TestExports):

    schema_class = XMLSchema11


class TestDownloads(XMLSchemaTestCase):
    cases_dir = pathlib.Path(__file__).absolute().parent.joinpath('test_cases')

    @classmethod
    def setUpClass(cls):
        cls.vh_dir = cls.casepath('examples/vehicles')
        cls.vh_xsd_file = cls.casepath('examples/vehicles/vehicles.xsd')
        cls.vh_xml_file = cls.casepath('examples/vehicles/vehicles.xml')

    def test_download_local_schemas(self):
        with tempfile.TemporaryDirectory() as dirname:
            location_map = download_schemas(self.vh_xsd_file, target=dirname, modify=True)

            self.assertEqual(location_map, {})

            xsd_path = pathlib.Path(dirname).joinpath('vehicles.xsd')
            schema = XMLSchema10(xsd_path)
            for xs in schema.maps.namespaces['http://example.com/vehicles']:
                self.assertTrue(xs.url.startswith('file://'))

            self.assertTrue(pathlib.Path(dirname).joinpath('__init__.py').is_file())

    @unittest.skipIf(SKIP_REMOTE_TESTS, "Remote networks are not accessible.")
    def test_download_local_and_remote_schemas(self):
        vh_schema_file = self.casepath('issues/issue_187/issue_187_2.xsd')
        url_common = ('raw.githubusercontent.com/brunato/xmlschema/master/'
                      'tests/test_cases/examples/vehicles')
        url_map = {
            f'https://{url_common}/bikes.xsd': f'https/{url_common}/bikes.xsd',
            f'https://{url_common}/cars.xsd': f'https/{url_common}/cars.xsd'
        }

        with tempfile.TemporaryDirectory() as dirname:
            location_map = download_schemas(vh_schema_file, target=dirname, modify=True)

            self.assertEqual(location_map, url_map)

            xsd_path = pathlib.Path(dirname).joinpath('issue_187_2.xsd')
            schema = XMLSchema10(xsd_path)
            for xs in schema.maps.namespaces['http://example.com/vehicles']:
                self.assertTrue(xs.url.startswith('file://'))

            self.assertTrue(pathlib.Path(dirname).joinpath('__init__.py').is_file())

        with tempfile.TemporaryDirectory() as dirname:
            location_map = download_schemas(vh_schema_file, target=dirname)

            self.assertEqual(location_map, url_map)

            xsd_path = pathlib.Path(dirname).joinpath('issue_187_2.xsd')
            schema = XMLSchema10(xsd_path)
            for k, xs in enumerate(schema.maps.namespaces['http://example.com/vehicles']):
                if k:
                    self.assertTrue(xs.url.startswith('https://'))
                else:
                    self.assertTrue(xs.url.startswith('file://'))

    @unittest.skipIf(SKIP_REMOTE_TESTS, "Remote networks are not accessible.")
    def test_download_remote_schemas(self):
        url = ("https://raw.githubusercontent.com/brewpoo/"
               "BeerXML-Standard/master/schema/BeerXML.xsd")

        with tempfile.TemporaryDirectory() as dirname:
            location_map = download_schemas(url, target=dirname, modify=True)
            self.assertEqual(location_map, {})

            xsd_files = {x.name for x in pathlib.Path(dirname).glob('*.xsd')}
            self.assertSetEqual(xsd_files, {
                'BeerXML.xsd', 'measureable_units.xsd', 'hops.xsd',
                'yeast.xsd', 'mash.xsd', 'style.xsd', 'water.xsd',
                'grain.xsd', 'misc.xsd', 'recipes.xsd', 'mash_step.xsd'
            })

            xsd_path = pathlib.Path(dirname).joinpath('BeerXML.xsd')
            schema = XMLSchema10(xsd_path)
            for ns in schema.maps.namespaces:
                if ns.startswith('urn:beerxml:'):
                    for k, xs in enumerate(schema.maps.namespaces[ns]):
                        self.assertEqual(k, 0)
                        self.assertTrue(xs.url.startswith('file://'))

    def test_download_with_loglevel(self):
        with tempfile.TemporaryDirectory() as dirname:
            with self.assertLogs('xmlschema', level='DEBUG') as ctx:
                download_schemas(self.vh_xsd_file, target=dirname, loglevel='debug')
                self.assertGreater(len(ctx.output), 10)
                self.assertFalse(any('Write modified ' in line for line in ctx.output))
                self.assertTrue(any('Write unchanged ' in line for line in ctx.output))


if __name__ == '__main__':
    run_xmlschema_tests('exports.py module')