From 711c4d2ce8761d36f478364c108fc25bcea0c21c Mon Sep 17 00:00:00 2001 From: Davide Brunato Date: Sat, 21 May 2022 19:18:15 +0200 Subject: [PATCH] Add regression tests for issue #298 --- .../issues/issue_298/issue_298-1.xml | 5 +++ .../issues/issue_298/issue_298-2.xml | 8 +++++ .../test_cases/issues/issue_298/issue_298.xsd | 22 +++++++++++++ tests/test_cases/testfiles | 2 ++ tests/validation/test_encoding.py | 33 +++++++++++++++++-- 5 files changed, 67 insertions(+), 3 deletions(-) create mode 100644 tests/test_cases/issues/issue_298/issue_298-1.xml create mode 100644 tests/test_cases/issues/issue_298/issue_298-2.xml create mode 100644 tests/test_cases/issues/issue_298/issue_298.xsd diff --git a/tests/test_cases/issues/issue_298/issue_298-1.xml b/tests/test_cases/issues/issue_298/issue_298-1.xml new file mode 100644 index 00000000..43a34e27 --- /dev/null +++ b/tests/test_cases/issues/issue_298/issue_298-1.xml @@ -0,0 +1,5 @@ + + + + + diff --git a/tests/test_cases/issues/issue_298/issue_298-2.xml b/tests/test_cases/issues/issue_298/issue_298-2.xml new file mode 100644 index 00000000..52052792 --- /dev/null +++ b/tests/test_cases/issues/issue_298/issue_298-2.xml @@ -0,0 +1,8 @@ + + + + + + + + diff --git a/tests/test_cases/issues/issue_298/issue_298.xsd b/tests/test_cases/issues/issue_298/issue_298.xsd new file mode 100644 index 00000000..5a5de624 --- /dev/null +++ b/tests/test_cases/issues/issue_298/issue_298.xsd @@ -0,0 +1,22 @@ + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/tests/test_cases/testfiles b/tests/test_cases/testfiles index 73c27170..65ef794d 100644 --- a/tests/test_cases/testfiles +++ b/tests/test_cases/testfiles @@ -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 diff --git a/tests/validation/test_encoding.py b/tests/validation/test_encoding.py index cf96b472..536f7ece 100644 --- a/tests/validation/test_encoding.py +++ b/tests/validation/test_encoding.py @@ -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): @@ -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) @@ -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