# -*- coding: utf-8 -*- # # Copyright (c), 2016-2019, 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 # """ Tests subpackage module: common definitions for unittest scripts of the 'xmlschema' package. """ import unittest import platform import re import os import xmlschema from xmlschema import XMLSchema from xmlschema.compat import urlopen, URLError, unicode_type from xmlschema.exceptions import XMLSchemaValueError from xmlschema.qnames import XSD_SCHEMA from xmlschema.namespaces import XSD_NAMESPACE, get_namespace from xmlschema.etree import etree_element, etree_register_namespace, etree_elements_assert_equal from xmlschema.resources import fetch_namespaces from xmlschema.helpers import is_etree_element def has_network_access(*locations): for url in locations: try: urlopen(url, timeout=5) except (URLError, OSError): pass else: return True return False SKIP_REMOTE_TESTS = not has_network_access('http://www.sissa.it', 'http://www.w3.org/', 'http://dublincore.org/') PROTECTED_PREFIX_PATTERN = re.compile(r'\bns\d:') TEST_CASES_DIR = os.path.join(os.path.dirname(__file__), 'test_cases/') SCHEMA_TEMPLATE = """ {1} """ def casepath(relative_path): """ Returns the absolute path from a relative path specified from the `xmlschema/tests/test_cases/` dir. """ return os.path.join(TEST_CASES_DIR, relative_path) def print_test_header(): """Print an header thar displays Python version and platform used for test session.""" header1 = "Test %r" % xmlschema header2 = "with Python {} on platform {}".format(platform.python_version(), platform.platform()) print('{0}\n{1}\n{2}\n{0}'.format("*" * max(len(header1), len(header2)), header1, header2)) class XsdValidatorTestCase(unittest.TestCase): """ TestCase class for XSD validators. """ @classmethod def casepath(cls, relative_path): return casepath(relative_path) etree_register_namespace(prefix='xs', uri=XSD_NAMESPACE) etree_register_namespace(prefix='ns', uri="ns") schema_class = XMLSchema @classmethod def setUpClass(cls): cls.errors = [] cls.xsd_types = cls.schema_class.builtin_types() cls.content_pattern = re.compile(r'(<|