Skip to content

Commit

Permalink
Add regression tests for issue #298
Browse files Browse the repository at this point in the history
  • Loading branch information
brunato committed May 21, 2022
1 parent 5c34f93 commit 711c4d2
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 3 deletions.
5 changes: 5 additions & 0 deletions tests/test_cases/issues/issue_298/issue_298-1.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<tns:Root xmlns:tns="http://xmlschema.test/ns">
<Container>
<Freeform />
</Container>
</tns:Root>
8 changes: 8 additions & 0 deletions tests/test_cases/issues/issue_298/issue_298-2.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<tns:Root xmlns:tns="http://xmlschema.test/ns" xmlns:zz="http://xmlschema.test/ns2">
<Container>
<Freeform>
<zz:ForeignSchema/>
</Freeform>
</Container>
</tns:Root>
22 changes: 22 additions & 0 deletions tests/test_cases/issues/issue_298/issue_298.xsd
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:tns="http://xmlschema.test/ns"
targetNamespace="http://xmlschema.test/ns">
<xs:element name="Root">
<xs:complexType>
<xs:sequence>
<xs:element name="Container" type="tns:container"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:complexType name="container">
<xs:sequence>
<xs:element name="Freeform" type="tns:freeform"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="freeform" mixed="true">
<xs:sequence minOccurs="0" maxOccurs="unbounded">
<xs:any processContents="lax"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
2 changes: 2 additions & 0 deletions tests/test_cases/testfiles
Original file line number Diff line number Diff line change
Expand Up @@ -135,3 +135,5 @@ issues/issue_266/issue_266-2.xsd
issues/issue_266/issue_266-2.xml
issues/issue_276/schema.xsd
issues/issue_276/dummy.xml
issues/issue_298/issue_298-1.xml -L 'http://xmlschema.test/ns' issue_298.xsd
issues/issue_298/issue_298-2.xml -L 'http://xmlschema.test/ns' issue_298.xsd
33 changes: 30 additions & 3 deletions tests/validation/test_encoding.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@
from elementpath import datatypes

from xmlschema import XMLSchemaEncodeError, XMLSchemaValidationError
from xmlschema.converters import UnorderedConverter
from xmlschema.converters import UnorderedConverter, JsonMLConverter
from xmlschema.etree import etree_element, etree_tostring, ElementTree
from xmlschema.helpers import local_name, is_etree_element
from xmlschema.resources import XMLResource
from xmlschema.validators.exceptions import XMLSchemaChildrenValidationError
from xmlschema.validators import XMLSchema11
from xmlschema.testing import XsdValidatorTestCase
from xmlschema.testing import XsdValidatorTestCase, etree_elements_assert_equal


class TestEncoding(XsdValidatorTestCase):
Expand All @@ -53,7 +53,7 @@ def check_encode(self, xsd_component, data, expected, **kwargs):
self.assertTrue(isinstance(obj, type(expected)))

def test_decode_encode(self):
"""Test encode after a decode, checking the re-encoded tree."""
"""Test encode after decode, checking the re-encoded tree."""
filename = self.casepath('examples/collection/collection.xml')
xt = ElementTree.parse(filename)
xd = self.col_schema.to_dict(filename)
Expand Down Expand Up @@ -669,6 +669,33 @@ def test_float_encoding(self):
self.assertEqual(float_type.encode(float('-inf')), '-INF')
self.assertEqual(float_type.encode(float('nan')), 'NaN')

def test_wildcard_with_foreign_and_jsonml__issue_298(self):
schema = self.schema_class(self.casepath('issues/issue_298/issue_298.xsd'))
xml_data = self.casepath('issues/issue_298/issue_298-2.xml')

instance = [
'tns:Root',
{'xmlns:tns': 'http://xmlschema.test/ns',
'xmlns:zz': 'http://xmlschema.test/ns2'},
['Container', ['Freeform', ['zz:ForeignSchema']]]
]
result = schema.decode(xml_data, converter=JsonMLConverter)
self.assertListEqual(result, instance)

root, errors = schema.encode(instance, validation='lax', converter=JsonMLConverter)
self.assertListEqual(errors, [])
self.assertIsNone(etree_elements_assert_equal(root, ElementTree.parse(xml_data).getroot()))

instance = ['tns:Root', ['Container', ['Freeform', ['zz:ForeignSchema']]]]
namespaces = {'tns': 'http://xmlschema.test/ns',
'zz': 'http://xmlschema.test/ns2'}
root, errors = schema.encode(instance, validation='lax',
namespaces=namespaces,
use_defaults=False,
converter=JsonMLConverter)
self.assertListEqual(errors, [])
self.assertIsNone(etree_elements_assert_equal(root, ElementTree.parse(xml_data).getroot()))


class TestEncoding11(TestEncoding):
schema_class = XMLSchema11
Expand Down

0 comments on commit 711c4d2

Please sign in to comment.