From c8e5f07401bff23de7e50505c36a975a92132f0c Mon Sep 17 00:00:00 2001 From: Davide Brunato Date: Mon, 2 Aug 2021 15:43:46 +0200 Subject: [PATCH] Fix XSD annotations and tests - Add string serialization for XSD annotations - Relax memory tests - Fix GitHub Action workflow's name --- .github/workflows/test-xmlschema.yml | 2 +- tests/test_memory.py | 8 ++--- tests/validators/test_schema_class.py | 50 +++++++++++++++++++++------ xmlschema/validators/schema.py | 3 +- xmlschema/validators/xsdbase.py | 6 ++-- 5 files changed, 51 insertions(+), 18 deletions(-) diff --git a/.github/workflows/test-xmlschema.yml b/.github/workflows/test-xmlschema.yml index 03725296..81206f74 100644 --- a/.github/workflows/test-xmlschema.yml +++ b/.github/workflows/test-xmlschema.yml @@ -1,4 +1,4 @@ -name: elementpath +name: xmlschema on: [push, pull_request] diff --git a/tests/test_memory.py b/tests/test_memory.py index 7ce8cf16..0dacab3f 100644 --- a/tests/test_memory.py +++ b/tests/test_memory.py @@ -69,8 +69,8 @@ def test_element_tree_memory_usage(self): lazy_iterparse_mem = self.check_memory_profile(output) self.assertLess(parse_mem, 2) - self.assertLessEqual(lazy_iterparse_mem, parse_mem / decimal.Decimal('1.75')) - self.assertLessEqual(lazy_iterparse_mem, iterparse_mem) + self.assertLess(lazy_iterparse_mem, parse_mem) + self.assertLess(lazy_iterparse_mem, iterparse_mem) def test_decode_memory_usage(self): test_dir = os.path.dirname(__file__) or '.' @@ -88,7 +88,7 @@ def test_decode_memory_usage(self): lazy_decode_mem = self.check_memory_profile(output) self.assertLess(decode_mem, 2.6) - self.assertLessEqual(lazy_decode_mem, decode_mem / decimal.Decimal('1.1')) + self.assertLess(lazy_decode_mem, decode_mem) def test_validate_memory_usage(self): test_dir = os.path.dirname(__file__) or '.' @@ -106,7 +106,7 @@ def test_validate_memory_usage(self): lazy_validate_mem = self.check_memory_profile(output) self.assertLess(validate_mem, 2.6) - self.assertLessEqual(lazy_validate_mem, validate_mem / decimal.Decimal('1.1')) + self.assertLess(lazy_validate_mem, validate_mem) if __name__ == '__main__': diff --git a/tests/validators/test_schema_class.py b/tests/validators/test_schema_class.py index 42cd729c..b60bcee5 100644 --- a/tests/validators/test_schema_class.py +++ b/tests/validators/test_schema_class.py @@ -163,7 +163,19 @@ def test_annotations(self): """) - self.assertIsNotNone(schema.elements['foo'].annotation) + xsd_element = schema.elements['foo'] + self.assertIsNone(xsd_element._annotation) # lazy annotation + self.assertIsNotNone(xsd_element.annotation) + self.assertIs(xsd_element.annotation, xsd_element._annotation) + + self.check_schema(""" + + + + + + + """, XMLSchemaParseError) schema = self.check_schema(""" @@ -174,16 +186,34 @@ def test_annotations(self): """) - self.assertIsNotNone(schema.types["Magic"].annotation) - self.check_schema(""" - - - - - - - """, XMLSchemaParseError) + xsd_type = schema.types["Magic"] + self.assertIsNotNone(xsd_type._annotation) # xs:simpleType annotations are not lazy parsed + self.assertEqual(str(xsd_type.annotation), ' stuff ') + + def test_annotation_string(self): + schema = self.check_schema(""" + + + A element info + + + + + B element extended info, line1 + B element extended info, line2 + + """) + + xsd_element = schema.elements['A'] + self.assertEqual(str(xsd_element.annotation), 'A element info') + self.assertEqual(repr(xsd_element.annotation), "XsdAnnotation('A element info')") + + xsd_element = schema.elements['B'] + self.assertEqual(str(xsd_element.annotation), + 'B element extended info, line1\nB element extended info, line2') + self.assertEqual(repr(xsd_element.annotation), + "XsdAnnotation('B element extended info, line1\\nB element')") def test_schema_annotations(self): schema = self.schema_class(dedent("""\ diff --git a/xmlschema/validators/schema.py b/xmlschema/validators/schema.py index be28f741..4248d989 100644 --- a/xmlschema/validators/schema.py +++ b/xmlschema/validators/schema.py @@ -44,7 +44,8 @@ from .exceptions import XMLSchemaParseError, XMLSchemaValidationError, XMLSchemaEncodeError, \ XMLSchemaNotBuiltError, XMLSchemaIncludeWarning, XMLSchemaImportWarning from .helpers import get_xsd_derivation_attribute -from .xsdbase import check_validation_mode, XsdValidator, ValidationMixin, XsdComponent, XsdAnnotation +from .xsdbase import check_validation_mode, XsdValidator, \ + ValidationMixin, XsdComponent, XsdAnnotation from .notations import XsdNotation from .identities import XsdKey, XsdKeyref, XsdUnique, Xsd11Key, Xsd11Unique, Xsd11Keyref from .facets import XSD_10_FACETS, XSD_11_FACETS diff --git a/xmlschema/validators/xsdbase.py b/xmlschema/validators/xsdbase.py index d800ba99..5beec44c 100644 --- a/xmlschema/validators/xsdbase.py +++ b/xmlschema/validators/xsdbase.py @@ -573,8 +573,10 @@ class XsdAnnotation(XsdComponent): annotation = None def __repr__(self): - values = elementpath.select(self.elem, '*/fn:string()') - return '%s(%r)' % (self.__class__.__name__, '\n'.join(values)[:40]) + return '%s(%r)' % (self.__class__.__name__, str(self)[:40]) + + def __str__(self): + return '\n'.join(elementpath.select(self.elem, '*/fn:string()')) @property def built(self):