From 3ed5d359471c4b873b5a68434f5ade052c4109e0 Mon Sep 17 00:00:00 2001 From: Jan Kowalleck Date: Thu, 28 Sep 2023 11:06:56 +0200 Subject: [PATCH] tests & fixes Signed-off-by: Jan Kowalleck --- serializable/__init__.py | 2 + ...the-phoenix-project-nixed-defaultNS-v4.xml | 44 ++++++++++++++++ .../the-phoenix-project-no-defaultNS-v4.xml | 43 ++++++++++++++++ ...-phoenix-project-no-defaultNS.SNAPSHOT.xml | 2 + ...the-phoenix-project-with-defaultNS-v4.xml} | 0 ...oenix-project-with-defaultNS.SNAPSHOT.xml} | 0 tests/test_xml.py | 51 ++++++++++++++++--- 7 files changed, 134 insertions(+), 8 deletions(-) create mode 100644 tests/fixtures/the-phoenix-project-nixed-defaultNS-v4.xml create mode 100644 tests/fixtures/the-phoenix-project-no-defaultNS-v4.xml create mode 100644 tests/fixtures/the-phoenix-project-no-defaultNS.SNAPSHOT.xml rename tests/fixtures/{the-phoenix-project-defaultNS-v4.xml => the-phoenix-project-with-defaultNS-v4.xml} (100%) rename tests/fixtures/{the-phoenix-project-defaultNS.xml => the-phoenix-project-with-defaultNS.SNAPSHOT.xml} (100%) diff --git a/serializable/__init__.py b/serializable/__init__.py index af531be..7d05970 100644 --- a/serializable/__init__.py +++ b/serializable/__init__.py @@ -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__}') diff --git a/tests/fixtures/the-phoenix-project-nixed-defaultNS-v4.xml b/tests/fixtures/the-phoenix-project-nixed-defaultNS-v4.xml new file mode 100644 index 0000000..80242e1 --- /dev/null +++ b/tests/fixtures/the-phoenix-project-nixed-defaultNS-v4.xml @@ -0,0 +1,44 @@ + + + + f3758bf0-0ff7-4366-a5e5-c209d4352b2d + The Phoenix Project + 5th Anniversary Limited Edition + 2018-04-16 + Gene Kim + George Spafford + Kevin Behr + fiction + + 10 Downing Street + IT Revolution Press LLC + + + + + + + + + + + + + + 1 + Tuesday, September 2 + + + 2 + Tuesday, September 2 + + + 3 + Tuesday, September 2 + + + 4 + Wednesday, September 3 + + + \ No newline at end of file diff --git a/tests/fixtures/the-phoenix-project-no-defaultNS-v4.xml b/tests/fixtures/the-phoenix-project-no-defaultNS-v4.xml new file mode 100644 index 0000000..4d497ce --- /dev/null +++ b/tests/fixtures/the-phoenix-project-no-defaultNS-v4.xml @@ -0,0 +1,43 @@ + + + f3758bf0-0ff7-4366-a5e5-c209d4352b2d + The Phoenix Project + 5th Anniversary Limited Edition + 2018-04-16 + Gene Kim + George Spafford + Kevin Behr + fiction + + 10 Downing Street + IT Revolution Press LLC + + + + + + + + + + + + + + 1 + Tuesday, September 2 + + + 2 + Tuesday, September 2 + + + 3 + Tuesday, September 2 + + + 4 + Wednesday, September 3 + + + \ No newline at end of file diff --git a/tests/fixtures/the-phoenix-project-no-defaultNS.SNAPSHOT.xml b/tests/fixtures/the-phoenix-project-no-defaultNS.SNAPSHOT.xml new file mode 100644 index 0000000..76df6cb --- /dev/null +++ b/tests/fixtures/the-phoenix-project-no-defaultNS.SNAPSHOT.xml @@ -0,0 +1,2 @@ + +f3758bf0-0ff7-4366-a5e5-c209d4352b2dThe Phoenix Project5th Anniversary Limited Edition2018-04-16Karl RanseierfictionIT Revolution Press LLC1Tuesday, September 22Tuesday, September 23Tuesday, September 24Wednesday, September 3 \ No newline at end of file diff --git a/tests/fixtures/the-phoenix-project-defaultNS-v4.xml b/tests/fixtures/the-phoenix-project-with-defaultNS-v4.xml similarity index 100% rename from tests/fixtures/the-phoenix-project-defaultNS-v4.xml rename to tests/fixtures/the-phoenix-project-with-defaultNS-v4.xml diff --git a/tests/fixtures/the-phoenix-project-defaultNS.xml b/tests/fixtures/the-phoenix-project-with-defaultNS.SNAPSHOT.xml similarity index 100% rename from tests/fixtures/the-phoenix-project-defaultNS.xml rename to tests/fixtures/the-phoenix-project-with-defaultNS.SNAPSHOT.xml diff --git a/tests/test_xml.py b/tests/test_xml.py index e775ad6..2511b9c 100644 --- a/tests/test_xml.py +++ b/tests/test_xml.py @@ -76,11 +76,11 @@ 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 @@ -88,8 +88,25 @@ def test_serializable_with_defaultNS(self) -> None: 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 @@ -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