Skip to content

Commit

Permalink
tests & fixes
Browse files Browse the repository at this point in the history
Signed-off-by: Jan Kowalleck <jan.kowalleck@gmail.com>
  • Loading branch information
jkowalleck committed Sep 28, 2023
1 parent 837ee3e commit 3ed5d35
Show file tree
Hide file tree
Showing 7 changed files with 134 additions and 8 deletions.
2 changes: 2 additions & 0 deletions serializable/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,8 @@ def _from_xml(cls: Type[_T], data: Union[TextIOWrapper, Element],

# Handle attributes on the root element if there are any
for k, v in data.attrib.items():
if default_namespace is not None:
k = k.replace(f'{{{default_namespace}}}', '')
decoded_k = CurrentFormatter.formatter.decode(property_name=k)
if decoded_k in klass.ignore_during_deserialization:
logger.debug(f'Ignoring {decoded_k} when deserializing {cls.__module__}.{cls.__qualname__}')
Expand Down
44 changes: 44 additions & 0 deletions tests/fixtures/the-phoenix-project-nixed-defaultNS-v4.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?xml version='1.0' encoding='utf-8'?>
<!-- elements are in special NS, attributes in defaultNS -->
<my:book xmlns:my="http://the.phoenix.project/testing/defaultNS" isbn_number="978-1942788294">
<my:id>f3758bf0-0ff7-4366-a5e5-c209d4352b2d</my:id>
<my:title>The Phoenix Project</my:title>
<my:edition number="5">5th Anniversary Limited Edition</my:edition>
<my:publish_date>2018-04-16</my:publish_date>
<my:author>Gene Kim</my:author>
<my:author>George Spafford</my:author>
<my:author>Kevin Behr</my:author>
<my:type>fiction</my:type>
<my:publisher>
<my:address>10 Downing Street</my:address>
<my:name>IT Revolution Press LLC</my:name>
</my:publisher>
<my:references>
<my:reference ref="my-ref-1"/>
<my:reference ref="my-ref-3">
<my:reference ref="sub-ref-2"/>
</my:reference>
<my:reference ref="my-ref-2">
<my:reference ref="sub-ref-1"/>
<my:reference ref="sub-ref-3"/>
</my:reference>
</my:references>
<my:chapters>
<my:chapter>
<my:number>1</my:number>
<my:title>Tuesday, September 2</my:title>
</my:chapter>
<my:chapter>
<my:number>2</my:number>
<my:title>Tuesday, September 2</my:title>
</my:chapter>
<my:chapter>
<my:number>3</my:number>
<my:title>Tuesday, September 2</my:title>
</my:chapter>
<my:chapter>
<my:number>4</my:number>
<my:title>Wednesday, September 3</my:title>
</my:chapter>
</my:chapters>
</my:book>
43 changes: 43 additions & 0 deletions tests/fixtures/the-phoenix-project-no-defaultNS-v4.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?xml version='1.0' encoding='utf-8'?>
<my:book xmlns:my="http://the.phoenix.project/testing/defaultNS" my:isbn_number="978-1942788294">
<my:id>f3758bf0-0ff7-4366-a5e5-c209d4352b2d</my:id>
<my:title>The Phoenix Project</my:title>
<my:edition my:number="5">5th Anniversary Limited Edition</my:edition>
<my:publish_date>2018-04-16</my:publish_date>
<my:author>Gene Kim</my:author>
<my:author>George Spafford</my:author>
<my:author>Kevin Behr</my:author>
<my:type>fiction</my:type>
<my:publisher>
<my:address>10 Downing Street</my:address>
<my:name>IT Revolution Press LLC</my:name>
</my:publisher>
<my:references>
<my:reference my:ref="my-ref-1"/>
<my:reference my:ref="my-ref-3">
<my:reference my:ref="sub-ref-2"/>
</my:reference>
<my:reference my:ref="my-ref-2">
<my:reference my:ref="sub-ref-1"/>
<my:reference my:ref="sub-ref-3"/>
</my:reference>
</my:references>
<my:chapters>
<my:chapter>
<my:number>1</my:number>
<my:title>Tuesday, September 2</my:title>
</my:chapter>
<my:chapter>
<my:number>2</my:number>
<my:title>Tuesday, September 2</my:title>
</my:chapter>
<my:chapter>
<my:number>3</my:number>
<my:title>Tuesday, September 2</my:title>
</my:chapter>
<my:chapter>
<my:number>4</my:number>
<my:title>Wednesday, September 3</my:title>
</my:chapter>
</my:chapters>
</my:book>
2 changes: 2 additions & 0 deletions tests/fixtures/the-phoenix-project-no-defaultNS.SNAPSHOT.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?xml version='1.0' encoding='utf-8'?>
<ns0:book xmlns:ns0="http://the.phoenix.project/testing/defaultNS" ns0:isbn_number="978-1942788294"><ns0:id>f3758bf0-0ff7-4366-a5e5-c209d4352b2d</ns0:id><ns0:title>The Phoenix Project</ns0:title><ns0:edition ns0:number="5">5th Anniversary Limited Edition</ns0:edition><ns0:publish_date>2018-04-16</ns0:publish_date><ns0:author>Karl Ranseier</ns0:author><ns0:type>fiction</ns0:type><ns0:publisher><ns0:name>IT Revolution Press LLC</ns0:name></ns0:publisher><ns0:chapters><ns0:chapter><ns0:number>1</ns0:number><ns0:title>Tuesday, September 2</ns0:title></ns0:chapter><ns0:chapter><ns0:number>2</ns0:number><ns0:title>Tuesday, September 2</ns0:title></ns0:chapter><ns0:chapter><ns0:number>3</ns0:number><ns0:title>Tuesday, September 2</ns0:title></ns0:chapter><ns0:chapter><ns0:number>4</ns0:number><ns0:title>Wednesday, September 3</ns0:title></ns0:chapter></ns0:chapters></ns0:book>
51 changes: 43 additions & 8 deletions tests/test_xml.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,20 +76,37 @@ def test_serialize_tfp_sc1(self) -> None:
with open(os.path.join(FIXTURES_DIRECTORY, 'the-phoenix-project-snake-case-1.xml')) as expected_xml:
self.assertEqualXml(expected_xml.read(), ThePhoenixProject.as_xml())

def test_serializable_with_defaultNS(self) -> None:
def test_serializable_no_defaultNS(self) -> None:
"""regression test for https://github.com/madpah/serializable/issues/12"""
from xml.etree import ElementTree
xmlns = 'http://the.phoenix.project/testing/defaultNS'
with open(os.path.join(FIXTURES_DIRECTORY, 'the-phoenix-project-defaultNS.xml')) as expected_xml:
with open(os.path.join(FIXTURES_DIRECTORY, 'the-phoenix-project-no-defaultNS.SNAPSHOT.xml')) as expected_xml:
expected = expected_xml.read()
data = deepcopy(ThePhoenixProject_v1)
data._authors = {'Karl Ranseier', } # only one item, so order is no issue
actual = ElementTree.tostring(
data.as_xml(as_string=False, xmlns=xmlns),
method='xml',
encoding='unicode', xml_declaration=True,
)
# byte-wise string compare is intentional!
self.assertEqual(expected, actual)

def test_serializable_with_defaultNS(self) -> None:
"""regression test for https://github.com/madpah/serializable/issues/12"""
from xml.etree import ElementTree
xmlns = 'http://the.phoenix.project/testing/defaultNS'
with open(os.path.join(FIXTURES_DIRECTORY, 'the-phoenix-project-with-defaultNS.SNAPSHOT.xml')) as expected_xml:
expected = expected_xml.read()
data = deepcopy(ThePhoenixProject_v1)
data._authors = {'Karl Ranseier', } # only one item, so order is no issue
actual = ElementTree.tostring(
data.as_xml(SchemaVersion4, as_string=False, xmlns=xmlns),
method='xml',
encoding='unicode', xml_declaration=True,
default_namespace=xmlns,
)
# byte-wise string compare is intentional!
self.assertEqual(expected, actual)

# endregion test_serialize
Expand Down Expand Up @@ -186,14 +203,32 @@ def test_deserialize_tfp_sc1(self) -> None:
self.assertEqual(ThePhoenixProject_v1.authors, book.authors)
self.assertEqual(ThePhoenixProject_v1.chapters, book.chapters)

def test_deserializable_with_defaultNS_from_element(self) -> None:
def test_deserializable_with_defaultNS(self) -> None:
"""regression test for https://github.com/madpah/serializable/issues/11"""
expected = ThePhoenixProject
with open(os.path.join(FIXTURES_DIRECTORY, 'the-phoenix-project-with-defaultNS-v4.xml')) as fixture_xml:
actual = Book.from_xml(fixture_xml)
self.assertDeepEqual(expected, actual)

def test_deserializable_no_defaultNS_explicite(self) -> None:
"""regression test for https://github.com/madpah/serializable/issues/11"""
expected = ThePhoenixProject
with open(os.path.join(FIXTURES_DIRECTORY, 'the-phoenix-project-no-defaultNS-v4.xml')) as fixture_xml:
actual = Book.from_xml(fixture_xml, 'http://the.phoenix.project/testing/defaultNS')
self.assertDeepEqual(expected, actual)

def test_deserializable_no_defaultNS_autodetect(self) -> None:
"""regression test for https://github.com/madpah/serializable/issues/11"""
expected = ThePhoenixProject
with open(os.path.join(FIXTURES_DIRECTORY, 'the-phoenix-project-no-defaultNS-v4.xml')) as fixture_xml:
actual = Book.from_xml(fixture_xml)
self.assertDeepEqual(expected, actual)

def test_deserializable_mixed_defaultNS_autodetect(self) -> None:
"""regression test for https://github.com/madpah/serializable/issues/11"""
from xml.etree import ElementTree
expected = ThePhoenixProject
with open(os.path.join(FIXTURES_DIRECTORY, 'the-phoenix-project-defaultNS-v4.xml')) as fixture_xml:
fixture = fixture_xml.read()
fixture_element = ElementTree.XML(fixture)
actual = Book.from_xml(fixture_element)
with open(os.path.join(FIXTURES_DIRECTORY, 'the-phoenix-project-no-defaultNS-v4.xml')) as fixture_xml:
actual = Book.from_xml(fixture_xml)
self.assertDeepEqual(expected, actual)

# region test_deserialize

0 comments on commit 3ed5d35

Please sign in to comment.