Fix resource tests for Python 2

This commit is contained in:
Davide Brunato 2019-10-24 06:37:31 +02:00
parent 6942be8ac9
commit a374d15805
4 changed files with 31 additions and 8 deletions

View File

@ -40,7 +40,7 @@ Otherwise the argument can be also an opened file-like object:
.. doctest::
>>> import xmlschema
>>> schema_file = open('xmlschema/tests/test_cases/examples/vehicles/vehicles.xsd')
>>> schema_file = open('xmlschema/tests/test_cases/examples/collection/collection.xsd')
>>> schema = xmlschema.XMLSchema(schema_file)
Alternatively you can pass a string containing the schema definition:
@ -54,8 +54,8 @@ Alternatively you can pass a string containing the schema definition:
... </xs:schema>
... """)
this option might not works when the schema includes other local subschemas, because the package
cannot knows anything about the schema's source location:
Strings and file-like objects might not work when the schema includes other local subschemas,
because the package cannot knows anything about the schema's source location:
.. doctest::
@ -73,6 +73,15 @@ cannot knows anything about the schema's source location:
Path: /xs:schema/xs:element/xs:complexType/xs:sequence/xs:element
In these cases you can provide an appropriate *base_url* optional argument to define the
reference directory path for other includes and imports:
.. doctest::
>>> import xmlschema
>>> schema_file = open('xmlschema/tests/test_cases/examples/vehicles/vehicles.xsd')
>>> schema = xmlschema.XMLSchema(schema_file, base_url='xmlschema/tests/test_cases/examples/vehicles/')
XSD declarations
----------------

View File

@ -551,14 +551,18 @@ class XMLResource(object):
return self.source.seek(position)
try:
return self.source.seek(position)
value = self.source.seek(position)
except AttributeError:
pass
else:
return value if PY3 else position
try:
return self.source.fp.seek(position)
value = self.source.fp.seek(position)
except AttributeError:
pass
else:
return value if PY3 else position
def close(self):
"""

View File

@ -138,8 +138,11 @@ class TestResources(unittest.TestCase):
ambiguous_path = casepath('resources/dummy file #2.txt')
self.assertTrue(fetch_resource(ambiguous_path).endswith('dummy file %232.txt'))
with urlopen(fetch_resource(ambiguous_path)) as res:
res = urlopen(fetch_resource(ambiguous_path))
try:
self.assertEqual(res.read(), b'DUMMY CONTENT')
finally:
res.close()
def test_fetch_namespaces(self):
self.assertFalse(fetch_namespaces(casepath('resources/malformed.xml')))
@ -570,7 +573,7 @@ class TestResources(unittest.TestCase):
self.assertEqual(set(resource.get_namespaces().keys()), {'vh', 'xsi'})
self.assertFalse(xml_file.closed)
if __name__ == '__main__':
from xmlschema.tests import print_test_header

View File

@ -10,6 +10,7 @@
# @author Davide Brunato <brunato@sissa.it>
#
import unittest
import sys
import xmlschema
from xmlschema import XMLSchemaValidationError
@ -55,7 +56,13 @@ class TestValidation(XsdValidatorTestCase):
path_line = str(err).splitlines()[-1]
else:
path_line = ''
self.assertEqual('Path: /vhx:vehicles/vhx:cars', path_line)
if sys.version_info >= (3, 6):
self.assertEqual('Path: /vhx:vehicles/vhx:cars', path_line)
else:
self.assertTrue(
'Path: /vh:vehicles/vh:cars' == path_line or 'Path: /vhx:vehicles/vhx:cars', path_line
) # Due to unordered dicts
# Issue #80
vh_2_xt = ElementTree.parse(vh_2_file)