Skip to content

Commit

Permalink
Fix XSD annotations and tests
Browse files Browse the repository at this point in the history
  - Add string serialization for XSD annotations
  - Relax memory tests
  - Fix GitHub Action workflow's name
  • Loading branch information
brunato committed Aug 2, 2021
1 parent 57449a0 commit c8e5f07
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 18 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test-xmlschema.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: elementpath
name: xmlschema

on: [push, pull_request]

Expand Down
8 changes: 4 additions & 4 deletions tests/test_memory.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 '.'
Expand All @@ -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 '.'
Expand All @@ -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__':
Expand Down
50 changes: 40 additions & 10 deletions tests/validators/test_schema_class.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,19 @@ def test_annotations(self):
<xs:element name='foo'>
<xs:annotation />
</xs:element>""")
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("""
<xs:simpleType name='Magic'>
<xs:annotation />
<xs:annotation />
<xs:restriction base='xs:string'>
<xs:enumeration value='A'/>
</xs:restriction>
</xs:simpleType>""", XMLSchemaParseError)

schema = self.check_schema("""
<xs:simpleType name='Magic'>
Expand All @@ -174,16 +186,34 @@ def test_annotations(self):
<xs:enumeration value='A'/>
</xs:restriction>
</xs:simpleType>""")
self.assertIsNotNone(schema.types["Magic"].annotation)

self.check_schema("""
<xs:simpleType name='Magic'>
<xs:annotation />
<xs:annotation />
<xs:restriction base='xs:string'>
<xs:enumeration value='A'/>
</xs:restriction>
</xs:simpleType>""", 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("""
<xs:element name='A'>
<xs:annotation>
<xs:documentation>A element info</xs:documentation>
</xs:annotation>
</xs:element>
<xs:element name='B'>
<xs:annotation>
<xs:documentation>B element extended info, line1</xs:documentation>
<xs:documentation>B element extended info, line2</xs:documentation>
</xs:annotation>
</xs:element>""")

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("""\
Expand Down
3 changes: 2 additions & 1 deletion xmlschema/validators/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 4 additions & 2 deletions xmlschema/validators/xsdbase.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit c8e5f07

Please sign in to comment.