Skip to content

Commit

Permalink
Merge pull request #250 from danifus/b249
Browse files Browse the repository at this point in the history
Add test and fix for encoding non-root elements
  • Loading branch information
brunato authored Jun 7, 2021
2 parents b5acc26 + 4cc33d4 commit 77cbad4
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
27 changes: 27 additions & 0 deletions tests/validation/test_encoding.py
Original file line number Diff line number Diff line change
Expand Up @@ -568,6 +568,33 @@ def test_attribute_encoding_with_defaults__issue_218(self):
self.assertIsNone(elem.text)
self.assertEqual(elem.attrib, {'b1': 'false', 'b2': 'false', 'b3': 'false'})

def test_encode_sub_tree(self):
"""Test encoding data of a non-root element"""
data = {
"@id": "PAR",
"name": "Pierre-Auguste Renoir",
"born": "1841-02-25",
"dead": "1919-12-03",
"qualification": "painter",
}
elem = self.col_schema.encode(
data,
path=".//author",
namespaces=self.col_namespaces,
)
self.assertEqual(
etree_tostring(elem),
dedent(
"""\
<author id="PAR">
<name>Pierre-Auguste Renoir</name>
<born>1841-02-25</born>
<dead>1919-12-03</dead>
<qualification>painter</qualification>
</author>"""
)
)


class TestEncoding11(TestEncoding):
schema_class = XMLSchema11
Expand Down
5 changes: 4 additions & 1 deletion xmlschema/converters/default.py
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,10 @@ def element_encode(self, obj, xsd_element, level=0):
if level != 0:
tag = xsd_element.name
else:
tag = xsd_element.qualified_name
if xsd_element.is_global():
tag = xsd_element.qualified_name
else:
tag = xsd_element.name
if self.preserve_root and isinstance(obj, MutableMapping):
match_local_name = self.strip_namespaces or self.default_namespace
match = xsd_element.get_matching_item(obj, self.ns_prefix, match_local_name)
Expand Down

0 comments on commit 77cbad4

Please sign in to comment.